Refactor API unit tests for part

This commit is contained in:
Oliver Walters 2021-02-26 21:45:37 +11:00
parent 6e3cb326fb
commit ee744be5fe
2 changed files with 28 additions and 37 deletions

View File

@ -60,11 +60,16 @@ class RolePermission(permissions.BasePermission):
permission = rolemap[request.method] permission = rolemap[request.method]
# Extract the model name associated with this request try:
model = view.serializer_class.Meta.model # Extract the model name associated with this request
model = view.serializer_class.Meta.model
# And the specific database table # And the specific database table
table = model._meta.db_table table = model._meta.db_table
except AttributeError:
# We will assume that if the serializer class does *not* have a Meta,
# then we don't need a permission
return True
result = users.models.RuleSet.check_table_permission(user, table, permission) result = users.models.RuleSet.check_table_permission(user, table, permission)

View File

@ -9,10 +9,11 @@ from part.models import Part
from stock.models import StockItem from stock.models import StockItem
from company.models import Company from company.models import Company
from InvenTree.api_tester import InvenTreeAPITestCase
from InvenTree.status_codes import StockStatus from InvenTree.status_codes import StockStatus
class PartAPITest(APITestCase): class PartAPITest(InvenTreeAPITestCase):
""" """
Series of tests for the Part DRF API Series of tests for the Part DRF API
- Tests for Part API - Tests for Part API
@ -27,32 +28,16 @@ class PartAPITest(APITestCase):
'test_templates', 'test_templates',
] ]
roles = [
'part.change',
'part.add',
'part.delete',
'part_category.change',
'part_category.add',
]
def setUp(self): def setUp(self):
# Create a user for auth super().setUp()
user = get_user_model()
self.user = user.objects.create_user(
username='testuser',
email='test@testing.com',
password='password'
)
# Put the user into a group with the correct permissions
group = Group.objects.create(name='mygroup')
self.user.groups.add(group)
# Give the group *all* the permissions!
for rule in group.rule_sets.all():
rule.can_view = True
rule.can_change = True
rule.can_add = True
rule.can_delete = True
rule.save()
group.save()
self.client.login(username='testuser', password='password')
def test_get_categories(self): def test_get_categories(self):
""" Test that we can retrieve list of part categories """ """ Test that we can retrieve list of part categories """
@ -254,7 +239,7 @@ class PartAPITest(APITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
class PartAPIAggregationTest(APITestCase): class PartAPIAggregationTest(InvenTreeAPITestCase):
""" """
Tests to ensure that the various aggregation annotations are working correctly... Tests to ensure that the various aggregation annotations are working correctly...
""" """
@ -268,13 +253,14 @@ class PartAPIAggregationTest(APITestCase):
'test_templates', 'test_templates',
] ]
roles = [
'part.view',
'part.change',
]
def setUp(self): def setUp(self):
# Create a user for auth
user = get_user_model()
user.objects.create_user('testuser', 'test@testing.com', 'password') super().setUp()
self.client.login(username='testuser', password='password')
# Add a new part # Add a new part
self.part = Part.objects.create( self.part = Part.objects.create(