From b3e4efd96e79cfe1562d249bbd17502c08e12deb Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 6 Oct 2020 16:03:19 +1100 Subject: [PATCH] Unit testing fixes --- InvenTree/part/test_api.py | 35 ++++++++++++++--------------------- InvenTree/part/test_views.py | 31 +++++++++++-------------------- InvenTree/users/models.py | 11 +++++------ 3 files changed, 30 insertions(+), 47 deletions(-) diff --git a/InvenTree/part/test_api.py b/InvenTree/part/test_api.py index 9fdc2688cb..328ad3ef9e 100644 --- a/InvenTree/part/test_api.py +++ b/InvenTree/part/test_api.py @@ -3,13 +3,13 @@ from rest_framework import status from django.urls import reverse from django.contrib.auth import get_user_model +from django.contrib.auth.models import Group from part.models import Part from stock.models import StockItem from company.models import Company from InvenTree.status_codes import StockStatus -from InvenTree.helpers import addUserPermissions class PartAPITest(APITestCase): @@ -36,27 +36,20 @@ class PartAPITest(APITestCase): password='password' ) - # Add the permissions required to access the API endpoints - perms = [ - 'view_part', - 'add_part', - 'change_part', - 'delete_part', - 'view_partcategory', - 'add_partcategory', - 'change_partcategory', - 'view_bomitem', - 'add_bomitem', - 'change_bomitem', - 'view_partattachment', - 'change_partattachment', - 'add_partattachment', - 'view_parttesttemplate', - 'add_parttesttemplate', - 'change_parttesttemplate', - ] + # Put the user into a group with the correct permissions + group = Group.objects.create(name='mygroup') + self.user.groups.add(group) - addUserPermissions(self.user, perms) + # 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') diff --git a/InvenTree/part/test_views.py b/InvenTree/part/test_views.py index b1ae991a0c..d8c345d243 100644 --- a/InvenTree/part/test_views.py +++ b/InvenTree/part/test_views.py @@ -3,8 +3,7 @@ from django.test import TestCase from django.urls import reverse from django.contrib.auth import get_user_model - -from InvenTree.helpers import addUserPermissions +from django.contrib.auth.models import Group from .models import Part @@ -31,26 +30,18 @@ class PartViewTestCase(TestCase): password='password' ) - # Add the permissions required to access the pages - perms = [ - 'view_part', - 'add_part', - 'change_part', - 'delete_part', - 'view_partcategory', - 'add_partcategory', - 'change_partcategory', - 'view_bomitem', - 'add_bomitem', - 'change_bomitem', - 'view_partattachment', - 'change_partattachment', - 'add_partattachment', - ] + # Put the user into a group with the correct permissions + group = Group.objects.create(name='mygroup') + self.user.groups.add(group) - addUserPermissions(self.user, perms) + # 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 - self.user.save() + rule.save() self.client.login(username='username', password='password') diff --git a/InvenTree/users/models.py b/InvenTree/users/models.py index 773dec36f7..050bbe2ef9 100644 --- a/InvenTree/users/models.py +++ b/InvenTree/users/models.py @@ -171,6 +171,10 @@ class RuleSet(models.Model): super().save(*args, **kwargs) + if self.group: + # Update the group too! + self.group.save() + def get_models(self): """ Return the database tables / models that this ruleset covers. @@ -328,12 +332,7 @@ def create_missing_rule_sets(sender, instance, **kwargs): group permissions. """ - created = kwargs.get('created', False) - # To trigger the group permissions update: update_fields should not be None - update_fields = kwargs.get('update_fields', None) - - if created or update_fields: - update_group_roles(instance) + update_group_roles(instance) def check_user_role(user, role, permission):