-
-
Notifications
You must be signed in to change notification settings - Fork 84
[feature] Implemented view permissions by extending DjangoModelPermissions class #249
#251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nemesifier
merged 5 commits into
master
from
issues/249-class-to-implement-view-permission
Jun 1, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9665dbc
[feature] Implemented view permissions by extending DjangoModelPermis…
ManishShah120 3e9ad53
[tests] Updated tests for ViewDjangoModelPermissions class
ManishShah120 2fe5487
[change] Set `perms_map` attribute
ManishShah120 c446b0d
[change] Improved code quality
ManishShah120 b78c4a1
[docs] Added docu for `DjangoModelPermissions` in DRF section
ManishShah120 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,23 @@ | ||
| from django.contrib.auth import get_user_model | ||
| from django.contrib.auth.models import Permission | ||
| from django.test import TestCase | ||
| from django.urls import reverse | ||
| from swapper import load_model | ||
|
|
||
| from openwisp_users.api.throttling import AuthRateThrottle | ||
|
|
||
| from ..models import Template | ||
| from .mixins import TestMultitenancyMixin | ||
|
|
||
| User = get_user_model() | ||
| Group = load_model('openwisp_users', 'Group') | ||
| OrganizationUser = load_model('openwisp_users', 'OrganizationUser') | ||
|
|
||
|
|
||
| class TestPermissionClasses(TestMultitenancyMixin, TestCase): | ||
| def setUp(self): | ||
| AuthRateThrottle.rate = 0 | ||
| self.template_model = Template | ||
| self.member_url = reverse('test_api_member_view') | ||
| self.manager_url = reverse('test_api_manager_view') | ||
| self.owner_url = reverse('test_api_owner_view') | ||
|
|
@@ -117,3 +126,106 @@ def test_organization_field_with_errored_parent(self): | |
| with self.assertRaises(AttributeError) as error: | ||
| self.client.get(reverse('test_error_field_view'), **auth) | ||
| self.assertIn('Organization not found', str(error.exception)) | ||
|
|
||
| def _get_auth_template(self, user, org1): | ||
| OrganizationUser.objects.create(user=user, organization=org1, is_admin=True) | ||
| self.client.force_login(user) | ||
| token = self._obtain_auth_token(user) | ||
| auth = dict(HTTP_AUTHORIZATION=f'Bearer {token}') | ||
| t1 = self._create_template(organization=org1) | ||
| return (auth, t1) | ||
|
|
||
| def test_view_permission_with_operator(self): | ||
| user = self._get_user() | ||
| operator_group = Group.objects.filter(name='Operator') | ||
| user.groups.set(operator_group) | ||
|
Comment on lines
+139
to
+141
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No @atb00ker. |
||
| org1 = self._get_org() | ||
| auth, t1 = self._get_auth_template(user, org1) | ||
| with self.subTest('Get Template List'): | ||
| response = self.client.get(reverse('test_template_list'), **auth) | ||
| self.assertEqual(response.status_code, 403) | ||
| with self.subTest('Get Template Detail'): | ||
| response = self.client.get( | ||
| reverse('test_template_detail', args=[t1.pk]), **auth | ||
| ) | ||
| self.assertEqual(response.status_code, 403) | ||
|
|
||
| def test_view_permission_with_administrator(self): | ||
| user = self._get_user() | ||
| administrator_group = Group.objects.get(name='Administrator') | ||
| change_perm = Permission.objects.get(codename='change_template') | ||
| administrator_group.permissions.add(change_perm) | ||
| user.groups.add(administrator_group) | ||
| org1 = self._get_org() | ||
| auth, t1 = self._get_auth_template(user, org1) | ||
| with self.subTest('Get Template List'): | ||
| response = self.client.get(reverse('test_template_list'), **auth) | ||
| self.assertEqual(response.status_code, 200) | ||
| with self.subTest('Get Template Detail'): | ||
| response = self.client.get( | ||
| reverse('test_template_detail', args=[t1.pk]), **auth | ||
| ) | ||
| self.assertEqual(response.status_code, 200) | ||
| permissions = administrator_group.permissions.values_list('codename', flat=True) | ||
| self.assertFalse('view_template' in permissions) | ||
| self.assertTrue('change_template' in permissions) | ||
|
|
||
| def test_view_permission_with_operator_having_view_perm(self): | ||
| user = self._get_user() | ||
| operator_group = Group.objects.get(name='Operator') | ||
| view_perm = Permission.objects.get(codename='view_template') | ||
| operator_group.permissions.add(view_perm) | ||
| user.groups.add(operator_group) | ||
| org1 = self._get_org() | ||
| auth, t1 = self._get_auth_template(user, org1) | ||
| with self.subTest('Get Template List'): | ||
| response = self.client.get(reverse('test_template_list'), **auth) | ||
| self.assertEqual(response.status_code, 200) | ||
| with self.subTest('Get Template Detail'): | ||
| response = self.client.get( | ||
| reverse('test_template_detail', args=[t1.pk]), **auth | ||
| ) | ||
| self.assertEqual(response.status_code, 200) | ||
| with self.subTest('Change Template Detail'): | ||
| data = {'name': 'change-template'} | ||
| response = self.client.patch( | ||
| reverse('test_template_detail', args=[t1.pk]), data, **auth | ||
| ) | ||
| self.assertEqual(response.status_code, 403) | ||
| with self.subTest('Delete Template'): | ||
| response = self.client.delete( | ||
| reverse('test_template_detail', args=[t1.pk]), **auth | ||
| ) | ||
| self.assertEqual(response.status_code, 403) | ||
|
|
||
| def test_view_django_model_permission_with_view_perm(self): | ||
| user = self._get_user() | ||
| user_permissions = Permission.objects.filter(codename='view_template') | ||
| user.user_permissions.add(*user_permissions) | ||
| user.organizations_dict # force caching | ||
| org1 = self._get_org() | ||
| auth, t1 = self._get_auth_template(user, org1) | ||
| with self.subTest('Get Template List'): | ||
| response = self.client.get(reverse('test_template_list'), **auth) | ||
| self.assertEqual(response.status_code, 200) | ||
| with self.subTest('Get Template Detail'): | ||
| response = self.client.get( | ||
| reverse('test_template_detail', args=[t1.pk]), **auth | ||
| ) | ||
| self.assertEqual(response.status_code, 200) | ||
|
|
||
| def test_view_django_model_permission_with_change_perm(self): | ||
| user = self._get_user() | ||
| user_permissions = Permission.objects.filter(codename='change_template') | ||
| user.user_permissions.add(*user_permissions) | ||
| user.organizations_dict # force caching | ||
| org1 = self._get_org() | ||
| auth, t1 = self._get_auth_template(user, org1) | ||
| with self.subTest('Get Template List'): | ||
| response = self.client.get(reverse('test_template_list'), **auth) | ||
| self.assertEqual(response.status_code, 200) | ||
| with self.subTest('Get Template Detail'): | ||
| response = self.client.get( | ||
| reverse('test_template_detail', args=[t1.pk]), **auth | ||
| ) | ||
| self.assertEqual(response.status_code, 200) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. Find function here: https://github.com/openwisp/openwisp-users/blob/master/openwisp_users/tests/utils.py#L128