-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Modify permission operands to use custom messages #9649
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e5162e1
Modify `AND` operand to have the ability to use custom messages
samitnuk 42976d5
Modify `OR` operand to have the ability to use custom messages
samitnuk a3ac11e
Modify `NOT` operand to have the ability to use custom message
samitnuk 2856523
Update tests/test_permissions.py
auvipy 8ffa898
Update tests/test_permissions.py
auvipy dbdcb20
Update tests/test_permissions.py
auvipy 431f8fe
refactor: Refactor permissions to allow list
last-partizan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,10 @@ | |
|
|
||
| factory = APIRequestFactory() | ||
|
|
||
| CUSTOM_MESSAGE_1 = 'Custom: You cannot access this resource' | ||
| CUSTOM_MESSAGE_2 = 'Custom: You do not have permission to view this resource' | ||
| INVERTED_MESSAGE = 'Inverted: Your account already active' | ||
|
|
||
|
|
||
| class BasicSerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
|
|
@@ -454,26 +458,42 @@ def has_permission(self, request, view): | |
|
|
||
|
|
||
| class BasicPermWithDetail(permissions.BasePermission): | ||
| message = 'Custom: You cannot access this resource' | ||
| message = CUSTOM_MESSAGE_1 | ||
| message_inverted = INVERTED_MESSAGE | ||
| code = 'permission_denied_custom' | ||
|
|
||
| def has_permission(self, request, view): | ||
| return False | ||
|
|
||
|
|
||
| class AnotherBasicPermWithDetail(permissions.BasePermission): | ||
| message = CUSTOM_MESSAGE_2 | ||
|
|
||
| def has_permission(self, request, view): | ||
| return False | ||
|
|
||
|
|
||
| class BasicObjectPerm(permissions.BasePermission): | ||
| def has_object_permission(self, request, view, obj): | ||
| return False | ||
|
|
||
|
|
||
| class BasicObjectPermWithDetail(permissions.BasePermission): | ||
| message = 'Custom: You cannot access this resource' | ||
| message = CUSTOM_MESSAGE_1 | ||
| message_inverted = INVERTED_MESSAGE | ||
| code = 'permission_denied_custom' | ||
|
|
||
| def has_object_permission(self, request, view, obj): | ||
| return False | ||
|
|
||
|
|
||
| class AnotherBasicObjectPermWithDetail(permissions.BasePermission): | ||
| message = CUSTOM_MESSAGE_2 | ||
|
|
||
| def has_object_permission(self, request, view, obj): | ||
| return False | ||
|
|
||
|
|
||
| class PermissionInstanceView(generics.RetrieveUpdateDestroyAPIView): | ||
| queryset = BasicModel.objects.all() | ||
| serializer_class = BasicSerializer | ||
|
|
@@ -487,6 +507,34 @@ class DeniedViewWithDetail(PermissionInstanceView): | |
| permission_classes = (BasicPermWithDetail,) | ||
|
|
||
|
|
||
| class DeniedViewWithDetailAND1(PermissionInstanceView): | ||
| permission_classes = (BasicPermWithDetail & permissions.AllowAny,) | ||
|
|
||
|
|
||
| class DeniedViewWithDetailAND2(PermissionInstanceView): | ||
| permission_classes = (permissions.AllowAny & AnotherBasicPermWithDetail,) | ||
|
|
||
|
|
||
| class DeniedViewWithDetailAND3(PermissionInstanceView): | ||
| permission_classes = (BasicPermWithDetail & AnotherBasicPermWithDetail,) | ||
|
|
||
|
|
||
| class DeniedViewWithDetailOR1(PermissionInstanceView): | ||
| permission_classes = (BasicPerm | BasicPermWithDetail,) | ||
|
|
||
|
|
||
| class DeniedViewWithDetailOR2(PermissionInstanceView): | ||
| permission_classes = (BasicPermWithDetail | BasicPerm,) | ||
|
|
||
|
|
||
| class DeniedViewWithDetailOR3(PermissionInstanceView): | ||
| permission_classes = (BasicPermWithDetail | AnotherBasicPermWithDetail,) | ||
|
|
||
|
|
||
| class DeniedViewWithDetailNOT(PermissionInstanceView): | ||
| permission_classes = (~BasicPermWithDetail,) | ||
|
|
||
|
|
||
| class DeniedObjectView(PermissionInstanceView): | ||
| permission_classes = (BasicObjectPerm,) | ||
|
|
||
|
|
@@ -495,52 +543,185 @@ class DeniedObjectViewWithDetail(PermissionInstanceView): | |
| permission_classes = (BasicObjectPermWithDetail,) | ||
|
|
||
|
|
||
| class DeniedObjectViewWithDetailAND1(PermissionInstanceView): | ||
| permission_classes = (BasicObjectPermWithDetail & permissions.AllowAny,) | ||
|
|
||
|
|
||
| class DeniedObjectViewWithDetailAND2(PermissionInstanceView): | ||
| permission_classes = (permissions.AllowAny & AnotherBasicObjectPermWithDetail) | ||
|
|
||
|
|
||
| class DeniedObjectViewWithDetailAND3(PermissionInstanceView): | ||
| permission_classes = (AnotherBasicObjectPermWithDetail & BasicObjectPermWithDetail) | ||
|
|
||
|
|
||
| class DeniedObjectViewWithDetailOR1(PermissionInstanceView): | ||
| permission_classes = (BasicObjectPerm | BasicObjectPermWithDetail) | ||
|
|
||
|
|
||
| class DeniedObjectViewWithDetailOR2(PermissionInstanceView): | ||
| permission_classes = (BasicObjectPermWithDetail | BasicObjectPerm,) | ||
|
|
||
|
|
||
| class DeniedObjectViewWithDetailOR3(PermissionInstanceView): | ||
| permission_classes = (BasicObjectPermWithDetail | AnotherBasicObjectPermWithDetail,) | ||
|
|
||
|
|
||
| class DeniedObjectViewWithDetailNOT(PermissionInstanceView): | ||
| permission_classes = (~BasicObjectPermWithDetail,) | ||
|
|
||
|
|
||
| denied_view = DeniedView.as_view() | ||
|
|
||
| denied_view_with_detail = DeniedViewWithDetail.as_view() | ||
|
|
||
| denied_view_with_detail_and_1 = DeniedViewWithDetailAND1.as_view() | ||
| denied_view_with_detail_and_2 = DeniedViewWithDetailAND2.as_view() | ||
| denied_view_with_detail_and_3 = DeniedViewWithDetailAND3.as_view() | ||
|
|
||
| denied_view_with_detail_or_1 = DeniedViewWithDetailOR1.as_view() | ||
| denied_view_with_detail_or_2 = DeniedViewWithDetailOR2.as_view() | ||
| denied_view_with_detail_or_3 = DeniedViewWithDetailOR3.as_view() | ||
|
|
||
| denied_view_with_detail_not = DeniedObjectViewWithDetailNOT.as_view() | ||
|
|
||
| denied_object_view = DeniedObjectView.as_view() | ||
|
|
||
| denied_object_view_with_detail = DeniedObjectViewWithDetail.as_view() | ||
|
|
||
| denied_object_view_with_detail_and_1 = DeniedObjectViewWithDetailAND1.as_view() | ||
| denied_object_view_with_detail_and_2 = DeniedObjectViewWithDetailAND2.as_view() | ||
| denied_object_view_with_detail_and_3 = DeniedObjectViewWithDetailAND3.as_view() | ||
|
|
||
| denied_object_view_with_detail_or_1 = DeniedObjectViewWithDetailOR1.as_view() | ||
| denied_object_view_with_detail_or_2 = DeniedObjectViewWithDetailOR2.as_view() | ||
| denied_object_view_with_detail_or_3 = DeniedObjectViewWithDetailOR3.as_view() | ||
|
|
||
| denied_object_view_with_detail_not = DeniedObjectViewWithDetailNOT.as_view() | ||
|
|
||
|
|
||
| class CustomPermissionsTests(TestCase): | ||
| def setUp(self): | ||
| BasicModel(text='foo').save() | ||
| User.objects.create_user('username', '[email protected]', 'password') | ||
| credentials = basic_auth_header('username', 'password') | ||
| self.request = factory.get('/1', format='json', HTTP_AUTHORIZATION=credentials) | ||
| self.custom_message = 'Custom: You cannot access this resource' | ||
| self.custom_code = 'permission_denied_custom' | ||
|
|
||
| def test_permission_denied(self): | ||
| response = denied_view(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertNotEqual(detail, self.custom_message) | ||
| self.assertNotEqual(detail, CUSTOM_MESSAGE_1) | ||
| self.assertNotEqual(detail.code, self.custom_code) | ||
|
|
||
| def test_permission_denied_with_custom_detail(self): | ||
| response = denied_view_with_detail(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, self.custom_message) | ||
| self.assertEqual(detail, CUSTOM_MESSAGE_1) | ||
| self.assertEqual(detail.code, self.custom_code) | ||
|
|
||
| def test_permission_denied_with_custom_detail_and_1(self): | ||
| response = denied_view_with_detail_and_1(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, CUSTOM_MESSAGE_1) | ||
|
|
||
| def test_permission_denied_with_custom_detail_and_2(self): | ||
| response = denied_view_with_detail_and_2(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, CUSTOM_MESSAGE_2) | ||
|
|
||
| def test_permission_denied_with_custom_detail_and_3(self): | ||
| response = denied_view_with_detail_and_3(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, CUSTOM_MESSAGE_1) | ||
|
|
||
| def test_permission_denied_with_custom_detail_or_1(self): | ||
| response = denied_view_with_detail_or_1(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, CUSTOM_MESSAGE_1) | ||
|
|
||
| def test_permission_denied_with_custom_detail_or_2(self): | ||
| response = denied_view_with_detail_or_2(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, CUSTOM_MESSAGE_1) | ||
|
|
||
| def test_permission_denied_with_custom_detail_or_3(self): | ||
| response = denied_view_with_detail_or_3(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| expected_message = '"{0}" OR "{1}"'.format(CUSTOM_MESSAGE_1, CUSTOM_MESSAGE_2) | ||
| self.assertEqual(detail, expected_message) | ||
|
|
||
| def test_permission_denied_with_custom_detail_not(self): | ||
| response = denied_view_with_detail_not(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, INVERTED_MESSAGE) | ||
|
|
||
| def test_permission_denied_for_object(self): | ||
| response = denied_object_view(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertNotEqual(detail, self.custom_message) | ||
| self.assertNotEqual(detail, CUSTOM_MESSAGE_1) | ||
| self.assertNotEqual(detail.code, self.custom_code) | ||
|
|
||
| def test_permission_denied_for_object_with_custom_detail(self): | ||
| response = denied_object_view_with_detail(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, self.custom_message) | ||
| self.assertEqual(detail, CUSTOM_MESSAGE_1) | ||
| self.assertEqual(detail.code, self.custom_code) | ||
|
|
||
| def test_permission_denied_for_object_with_custom_detail_and_1(self): | ||
| response = denied_object_view_with_detail_and_1(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, CUSTOM_MESSAGE_1) | ||
|
|
||
| def test_permission_denied_for_object_with_custom_detail_and_2(self): | ||
| response = denied_object_view_with_detail_and_2(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, CUSTOM_MESSAGE_2) | ||
|
|
||
| def test_permission_denied_for_object_with_custom_detail_and_3(self): | ||
| response = denied_object_view_with_detail_and_3(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, CUSTOM_MESSAGE_2) | ||
|
|
||
| def test_permission_denied_for_object_with_custom_detail_or_1(self): | ||
| response = denied_object_view_with_detail_or_1(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, CUSTOM_MESSAGE_1) | ||
|
|
||
| def test_permission_denied_for_object_with_custom_detail_or_2(self): | ||
| response = denied_object_view_with_detail_or_2(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, CUSTOM_MESSAGE_1) | ||
|
|
||
| def test_permission_denied_for_object_with_custom_detail_or_3(self): | ||
| response = denied_object_view_with_detail_or_3(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| expected_message = '"{0}" OR "{1}"'.format(CUSTOM_MESSAGE_1, CUSTOM_MESSAGE_2) | ||
| self.assertEqual(detail, expected_message) | ||
|
|
||
| def test_permission_denied_for_object_with_custom_detail_not(self): | ||
| response = denied_object_view_with_detail_not(self.request, pk=1) | ||
| detail = response.data.get('detail') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
| self.assertEqual(detail, INVERTED_MESSAGE) | ||
|
|
||
|
|
||
| class PermissionsCompositionTests(TestCase): | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
I started working on getting this into proper shape, and I'm having trouble with this part.
And I need some help
Imagine a case:
Here, in case of error, message will be "Path not whitelisted", completely ignoring the first one. This is wrong. And we do not have
messageavailable at the init time, because "AND" (and some of my permission classes) are setting self.message dynamically inhas_permissioncall.I come up with something like this:
Each failed permission adds it's message to a list, and we can handle this later, to produce a list of errors.
In this example, that will be:
Maybe we should wrap it into additional container, something like {"detail": "Any of the listed perms required", "errors": [{"message": ..., "code": ...}]`... to make it clearer...
What do you think? @auvipy @tomchristie @samitnuk
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.
I would suggest make the CI green first
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.
Done.
And, now it includes my proposed changes.