Skip to content
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

Feat/add cache to permissions #9003

Closed
Prev Previous commit
Next Next commit
test: Add new tests for cached permissions
Ehsan200 committed Jun 10, 2023
commit 175f9118e9fd3d1810214ab4e55a838a142048af
28 changes: 28 additions & 0 deletions tests/test_permissions.py
Original file line number Diff line number Diff line change
@@ -735,3 +735,31 @@ def has_object_permission(self, request, view, obj):
composed_perm = (IsAuthenticatedUserOwner | permissions.IsAdminUser)
hasperm = composed_perm().has_object_permission(request, None, None)
assert hasperm is False


class PermissionsCacheTests(TestCase):

class IsAuthenticatedUserOwnerWithCounter(permissions.IsAuthenticated):

def __init__(self):
self.call_counter = 0

def has_permission(self, request, view):
self.call_counter += 1
return True

def test_composed_perm_permissions(self):
request = factory.get('/1', format='json')
request.user = AnonymousUser()

composed_perm = (self.IsAuthenticatedUserOwnerWithCounter | permissions.IsAdminUser)
composed_perm_instance = composed_perm()
# in OR composed permissions has_object_permission will call has_permission too.
# we must ensure that this method (has_permission) is called once
has_permission_value = composed_perm_instance.has_permission(request, None)
has_object_permission_value = composed_perm_instance.has_object_permission(request, None, None)

self.assertTrue(has_permission_value)
self.assertTrue(has_object_permission_value)

self.assertEqual(composed_perm_instance.op1.call_counter, 1)