Skip to content

Commit

Permalink
Clear owner project cache when sharing project with users and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Apr 30, 2020
1 parent 0252cd6 commit 85dea3f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions onadata/apps/api/tests/viewsets/test_project_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from django.conf import settings
from django.db.models import Q
from django.core.cache import cache
from httmock import HTTMock, urlmatch
from mock import MagicMock, patch
import requests
Expand All @@ -26,6 +27,7 @@
from onadata.apps.main.models import MetaData
from onadata.libs import permissions as role
from onadata.libs.models.share_project import ShareProject
from onadata.libs.utils.cache_tools import PROJ_OWNER_CACHE, safe_key
from onadata.libs.permissions import (ROLES_ORDERED, DataEntryMinorRole,
DataEntryOnlyRole, DataEntryRole,
EditorMinorRole, EditorRole, ManagerRole,
Expand Down Expand Up @@ -1700,6 +1702,8 @@ def test_project_share_inactive_user(self):
response = view(request, pk=projectid)

self.assertEqual(response.status_code, 400)
self.assertIsNone(
cache.get(safe_key(f'{PROJ_OWNER_CACHE}{self.project.pk}')))
self.assertEqual(
response.data,
{'username': [u'The following user(s) is/are not active: alice']})
Expand Down Expand Up @@ -1729,6 +1733,8 @@ def test_project_share_remove_inactive_user(self):
response = view(request, pk=projectid)

self.assertEqual(response.status_code, 204)
self.assertIsNone(
cache.get(safe_key(f'{PROJ_OWNER_CACHE}{self.project.pk}')))

self.assertTrue(ReadOnlyRole.user_has_role(alice_profile.user,
self.project))
Expand Down Expand Up @@ -2197,6 +2203,8 @@ def test_project_list_by_owner(self):
response = view(request, pk=projectid)

self.assertEqual(response.status_code, 204)
self.assertIsNone(
cache.get(safe_key(f'{PROJ_OWNER_CACHE}{self.project.pk}')))

self.assertTrue(ReadOnlyRole.user_has_role(alice_profile.user,
self.project))
Expand Down
6 changes: 5 additions & 1 deletion onadata/apps/api/viewsets/project_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
(RemoveUserFromProjectSerializer, ShareProjectSerializer)
from onadata.libs.serializers.user_profile_serializer import \
UserProfileSerializer
from onadata.libs.utils.cache_tools import PROJ_OWNER_CACHE
from onadata.libs.utils.cache_tools import PROJ_OWNER_CACHE, safe_delete
from onadata.libs.serializers.xform_serializer import (XFormCreateSerializer,
XFormSerializer)
from onadata.libs.utils.common_tools import merge_dicts
Expand Down Expand Up @@ -152,6 +152,10 @@ def share(self, request, *args, **kwargs):
else:
return Response(data=serializer.errors,
status=status.HTTP_400_BAD_REQUEST)

# clear cache
safe_delete(f'{PROJ_OWNER_CACHE}{self.object.pk}')

return Response(status=status.HTTP_204_NO_CONTENT)

@action(methods=['DELETE', 'GET', 'POST'], detail=True)
Expand Down

0 comments on commit 85dea3f

Please sign in to comment.