Refactor API tests for stock

This commit is contained in:
Oliver Walters 2021-02-26 21:53:54 +11:00
parent ee744be5fe
commit 9d099c81a7
3 changed files with 32 additions and 37 deletions

View File

@ -83,4 +83,12 @@ class InvenTreeAPITestCase(APITestCase):
self.assertEqual(response.status_code, code) self.assertEqual(response.status_code, code)
return response return response
def post(self, url, data):
"""
Issue a POST request
"""
response = self.client.post(url, data=data, format='json')
return response

View File

@ -14,13 +14,14 @@ from django.contrib.auth import get_user_model
from InvenTree.helpers import addUserPermissions from InvenTree.helpers import addUserPermissions
from InvenTree.status_codes import StockStatus from InvenTree.status_codes import StockStatus
from InvenTree.api_tester import InvenTreeAPITestCase
from common.models import InvenTreeSetting from common.models import InvenTreeSetting
from .models import StockItem, StockLocation from .models import StockItem, StockLocation
class StockAPITestCase(APITestCase): class StockAPITestCase(InvenTreeAPITestCase):
fixtures = [ fixtures = [
'category', 'category',
@ -32,34 +33,16 @@ class StockAPITestCase(APITestCase):
'stock_tests', 'stock_tests',
] ]
roles = [
'stock.change',
'stock.add',
'stock_location.change',
'stock_location.add',
]
def setUp(self): def setUp(self):
# Create a user for auth
user = get_user_model()
self.user = user.objects.create_user('testuser', 'test@testing.com', 'password') super().setUp()
self.user.is_staff = True
self.user.save()
# Add the necessary permissions to the user
perms = [
'view_stockitemtestresult',
'change_stockitemtestresult',
'add_stockitemtestresult',
'add_stocklocation',
'change_stocklocation',
'add_stockitem',
'change_stockitem',
]
addUserPermissions(self.user, perms)
self.client.login(username='testuser', password='password')
def doPost(self, url, data={}):
response = self.client.post(url, data=data, format='json')
return response
class StockLocationTest(StockAPITestCase): class StockLocationTest(StockAPITestCase):
@ -232,6 +215,9 @@ class StockItemListTest(StockAPITestCase):
response = self.get_stock(expired=1) response = self.get_stock(expired=1)
self.assertEqual(len(response), 20) self.assertEqual(len(response), 20)
self.user.is_staff = True
self.user.save()
# Now, ensure that the expiry date feature is enabled! # Now, ensure that the expiry date feature is enabled!
InvenTreeSetting.set_setting('STOCK_ENABLE_EXPIRY', True, self.user) InvenTreeSetting.set_setting('STOCK_ENABLE_EXPIRY', True, self.user)
@ -449,7 +435,7 @@ class StocktakeTest(StockAPITestCase):
data = {} data = {}
# POST with a valid action # POST with a valid action
response = self.doPost(url, data) response = self.post(url, data)
self.assertContains(response, "must contain list", status_code=status.HTTP_400_BAD_REQUEST) self.assertContains(response, "must contain list", status_code=status.HTTP_400_BAD_REQUEST)
data['items'] = [{ data['items'] = [{
@ -457,7 +443,7 @@ class StocktakeTest(StockAPITestCase):
}] }]
# POST without a PK # POST without a PK
response = self.doPost(url, data) response = self.post(url, data)
self.assertContains(response, 'must contain a valid pk', status_code=status.HTTP_400_BAD_REQUEST) self.assertContains(response, 'must contain a valid pk', status_code=status.HTTP_400_BAD_REQUEST)
# POST with a PK but no quantity # POST with a PK but no quantity
@ -465,14 +451,14 @@ class StocktakeTest(StockAPITestCase):
'pk': 10 'pk': 10
}] }]
response = self.doPost(url, data) response = self.post(url, data)
self.assertContains(response, 'must contain a valid pk', status_code=status.HTTP_400_BAD_REQUEST) self.assertContains(response, 'must contain a valid pk', status_code=status.HTTP_400_BAD_REQUEST)
data['items'] = [{ data['items'] = [{
'pk': 1234 'pk': 1234
}] }]
response = self.doPost(url, data) response = self.post(url, data)
self.assertContains(response, 'must contain a valid quantity', status_code=status.HTTP_400_BAD_REQUEST) self.assertContains(response, 'must contain a valid quantity', status_code=status.HTTP_400_BAD_REQUEST)
data['items'] = [{ data['items'] = [{
@ -480,7 +466,7 @@ class StocktakeTest(StockAPITestCase):
'quantity': '10x0d' 'quantity': '10x0d'
}] }]
response = self.doPost(url, data) response = self.post(url, data)
self.assertContains(response, 'must contain a valid quantity', status_code=status.HTTP_400_BAD_REQUEST) self.assertContains(response, 'must contain a valid quantity', status_code=status.HTTP_400_BAD_REQUEST)
data['items'] = [{ data['items'] = [{
@ -488,7 +474,7 @@ class StocktakeTest(StockAPITestCase):
'quantity': "-1.234" 'quantity': "-1.234"
}] }]
response = self.doPost(url, data) response = self.post(url, data)
self.assertContains(response, 'must not be less than zero', status_code=status.HTTP_400_BAD_REQUEST) self.assertContains(response, 'must not be less than zero', status_code=status.HTTP_400_BAD_REQUEST)
# Test with a single item # Test with a single item
@ -499,7 +485,7 @@ class StocktakeTest(StockAPITestCase):
} }
} }
response = self.doPost(url, data) response = self.post(url, data)
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_transfer(self): def test_transfer(self):
@ -518,13 +504,13 @@ class StocktakeTest(StockAPITestCase):
url = reverse('api-stock-transfer') url = reverse('api-stock-transfer')
response = self.doPost(url, data) response = self.post(url, data)
self.assertContains(response, "Moved 1 parts to", status_code=status.HTTP_200_OK) self.assertContains(response, "Moved 1 parts to", status_code=status.HTTP_200_OK)
# Now try one which will fail due to a bad location # Now try one which will fail due to a bad location
data['location'] = 'not a location' data['location'] = 'not a location'
response = self.doPost(url, data) response = self.post(url, data)
self.assertContains(response, 'Valid location must be specified', status_code=status.HTTP_400_BAD_REQUEST) self.assertContains(response, 'Valid location must be specified', status_code=status.HTTP_400_BAD_REQUEST)

View File

@ -183,6 +183,7 @@ class RuleSet(models.Model):
if check_user_role(user, role, permission): if check_user_role(user, role, permission):
return True return True
print("failed permission check for", table, permission)
return False return False
@staticmethod @staticmethod