Skip to content

Commit

Permalink
[tests] Update TestMultitenantAdminMixin to use default operator group
Browse files Browse the repository at this point in the history
…#106

Closes #106
  • Loading branch information
codesankalp committed Mar 1, 2022
1 parent 747fb45 commit 345d011
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
2 changes: 2 additions & 0 deletions openwisp_users/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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()

Expand Down
15 changes: 9 additions & 6 deletions openwisp_users/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions tests/testapp/tests/test_filter_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand All @@ -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 = {
Expand Down
15 changes: 10 additions & 5 deletions tests/testapp/tests/test_multitenancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 345d011

Please sign in to comment.