From 345d011ef3590d33c4e56c939ce3c8f013ba937c Mon Sep 17 00:00:00 2001 From: sankalp Date: Wed, 2 Mar 2022 00:46:35 +0530 Subject: [PATCH] [tests] Update TestMultitenantAdminMixin to use default operator group #106 Closes #106 --- openwisp_users/tests/test_admin.py | 2 ++ openwisp_users/tests/utils.py | 15 ++++++++------ tests/testapp/tests/test_filter_classes.py | 24 ++++++++++++++++++++++ tests/testapp/tests/test_multitenancy.py | 15 +++++++++----- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/openwisp_users/tests/test_admin.py b/openwisp_users/tests/test_admin.py index acb29a3d..8784f9c7 100644 --- a/openwisp_users/tests/test_admin.py +++ b/openwisp_users/tests/test_admin.py @@ -1663,6 +1663,7 @@ def _create_multitenancy_test_env(self): organization_user=organization_user2, organization=org2 ) operator = self._create_operator() + self._add_permissions(operator, [{'codename__endswith': 'user'}]) organization_user3 = self._create_org_user( organization=org3, user=operator, is_admin=True ) @@ -1693,6 +1694,7 @@ def _create_multitenancy_test_env(self): def _make_org_manager(self, user, org): ou = OrganizationUser.objects.get(organization=org, user=user) + self._add_permissions(user, [{'codename__contains': 'organization'}]) ou.is_admin = True ou.save() diff --git a/openwisp_users/tests/utils.py b/openwisp_users/tests/utils.py index 9aef1ece..7bb202c6 100644 --- a/openwisp_users/tests/utils.py +++ b/openwisp_users/tests/utils.py @@ -9,6 +9,7 @@ Organization = load_model('openwisp_users', 'Organization') OrganizationOwner = load_model('openwisp_users', 'OrganizationOwner') OrganizationUser = load_model('openwisp_users', 'OrganizationUser') +Group = load_model('openwisp_users', 'Group') User = get_user_model() @@ -42,13 +43,13 @@ def _login(self, username='admin', password='tester'): def _logout(self): self.client.logout() - operator_permission_filters = [] - - def get_operator_permissions(self): + def _add_permissions(self, user, permission_filters=[]): + if not permission_filters: + return filters = Q() - for filter in self.operator_permission_filters: + for filter in permission_filters: filters = filters | Q(**filter) - return Permission.objects.filter(filters) + user.user_permissions.add(*Permission.objects.filter(filters)) def _create_operator(self, organizations=[], **kwargs): opts = dict( @@ -59,7 +60,9 @@ def _create_operator(self, organizations=[], **kwargs): ) opts.update(kwargs) operator = User.objects.create_user(**opts) - operator.user_permissions.add(*self.get_operator_permissions()) + groups = Group.objects.filter(name='Operator') + operator.groups.set(groups) + operator.user_permissions.add(*groups.first().permissions.all()) for organization in organizations: OrganizationUser.objects.create( user=operator, organization=organization, is_admin=True diff --git a/tests/testapp/tests/test_filter_classes.py b/tests/testapp/tests/test_filter_classes.py index ed5dcb3c..13bf0d62 100644 --- a/tests/testapp/tests/test_filter_classes.py +++ b/tests/testapp/tests/test_filter_classes.py @@ -194,6 +194,14 @@ def test_presence_of_null_org_field(self): self._create_org_user( user=operator, is_admin=True, organization=self._get_org('org_a') ) + self._add_permissions( + operator, + [ + { + 'codename__contains': 'template', + } + ], + ) token = self._obtain_auth_token(operator) url = reverse('test_template_list') response = self.client.get( @@ -239,6 +247,14 @@ def test_get_book_nested_shelf(self): self._create_org_user( user=operator, is_admin=True, organization=self._get_org('org_a') ) + self._add_permissions( + operator, + [ + { + 'codename__contains': 'book', + } + ], + ) token = self._obtain_auth_token(operator) url = reverse('test_book_nested_shelf') with self.assertNumQueries(6): @@ -251,6 +267,14 @@ def test_post_book_nested_shelf(self): self._create_org_user( user=operator, is_admin=True, organization=self._get_org('org_a') ) + self._add_permissions( + operator, + [ + { + 'codename__contains': 'book', + } + ], + ) token = self._obtain_auth_token(operator) url = reverse('test_book_nested_shelf') data = { diff --git a/tests/testapp/tests/test_multitenancy.py b/tests/testapp/tests/test_multitenancy.py index 96524492..042818fb 100644 --- a/tests/testapp/tests/test_multitenancy.py +++ b/tests/testapp/tests/test_multitenancy.py @@ -8,16 +8,21 @@ class TestMultitenancy(TestMultitenancyMixin, TestCase): book_model = Book shelf_model = Shelf - operator_permission_filter = [ - {'codename__endswith': 'book'}, - {'codename__endswith': 'shelf'}, - ] def _create_multitenancy_test_env(self): org1 = self._create_org(name='org1') org2 = self._create_org(name='org2') inactive = self._create_org(name='inactive-org', is_active=False) - operator = self._create_operator(organizations=[org1, inactive]) + operator = self._create_operator( + organizations=[org1, inactive], + ) + self._add_permissions( + user=operator, + permission_filters=[ + {'codename__endswith': 'book'}, + {'codename__endswith': 'shelf'}, + ], + ) s1 = self._create_shelf(name='shell1', organization=org1) s2 = self._create_shelf(name='shell2', organization=org2) s3 = self._create_shelf(name='shell3', organization=inactive)