-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #842 from onaio/840-handle-a-list-in-xfrom-viewset…
…-POST Not have to send a request for each user we share a form with.
- Loading branch information
Showing
19 changed files
with
689 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
|
@@ -12,6 +12,9 @@ | |
from onadata.apps.main.models.meta_data import MetaData | ||
from onadata.libs.serializers.xform_serializer import XFormSerializer | ||
from onadata.libs.serializers.metadata_serializer import UNIQUE_TOGETHER_ERROR | ||
from onadata.libs.utils.common_tags import XFORM_META_PERMS | ||
from onadata.libs.permissions import (EditorRole, EditorMinorRole, | ||
DataEntryRole, DataEntryOnlyRole) | ||
|
||
|
||
class TestMetaDataViewSet(TestAbstractViewSet): | ||
|
@@ -434,3 +437,121 @@ def test_invalid_form_metadata(self): | |
self.assertEqual(response.status_code, 400) | ||
self.assertEqual(response.data, | ||
{'xform': ['XForm does not exist']}) | ||
|
||
def test_xform_meta_permission(self): | ||
view = MetaDataViewSet.as_view({'post': 'create'}) | ||
|
||
data = { | ||
'data_type': XFORM_META_PERMS, | ||
'data_value': 'editor-minor|dataentry', | ||
'xform': self.xform.pk | ||
} | ||
request = self.factory.post('/', data, **self.extra) | ||
response = view(request) | ||
|
||
self.assertEqual(response.status_code, 201) | ||
|
||
meta = MetaData.xform_meta_permission(self.xform) | ||
self.assertEqual(meta.data_value, response.data.get('data_value')) | ||
|
||
data = { | ||
'data_type': XFORM_META_PERMS, | ||
'data_value': 'editor-minors|invalid_role', | ||
'xform': self.xform.pk | ||
} | ||
request = self.factory.post('/', data, **self.extra) | ||
response = view(request) | ||
|
||
self.assertEqual(response.status_code, 400) | ||
error = u"Format 'role'|'role' or Invalid role" | ||
self.assertEqual(response.data, {'non_field_errors': [error]}) | ||
|
||
def test_role_update_xform_meta_perms(self): | ||
alice_data = {'username': 'alice', 'email': '[email protected]'} | ||
alice_profile = self._create_user_profile(alice_data) | ||
|
||
EditorRole.add(alice_profile.user, self.xform) | ||
|
||
view = MetaDataViewSet.as_view({ | ||
'post': 'create', | ||
'put': 'update' | ||
}) | ||
|
||
data = { | ||
'data_type': XFORM_META_PERMS, | ||
'data_value': 'editor-minor|dataentry', | ||
'xform': self.xform.pk | ||
} | ||
request = self.factory.post('/', data, **self.extra) | ||
response = view(request) | ||
|
||
self.assertEqual(response.status_code, 201) | ||
|
||
self.assertFalse( | ||
EditorRole.user_has_role(alice_profile.user, self.xform)) | ||
|
||
self.assertTrue( | ||
EditorMinorRole.user_has_role(alice_profile.user, self.xform)) | ||
|
||
meta = MetaData.xform_meta_permission(self.xform) | ||
|
||
DataEntryRole.add(alice_profile.user, self.xform) | ||
|
||
data = { | ||
'data_type': XFORM_META_PERMS, | ||
'data_value': 'editor|dataentry-only', | ||
'xform': self.xform.pk | ||
} | ||
request = self.factory.put('/', data, **self.extra) | ||
response = view(request, pk=meta.pk) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
|
||
self.assertFalse( | ||
DataEntryRole.user_has_role(alice_profile.user, self.xform)) | ||
|
||
self.assertTrue( | ||
DataEntryOnlyRole.user_has_role(alice_profile.user, self.xform)) | ||
|
||
def test_xform_meta_perms_duplicates(self): | ||
view = MetaDataViewSet.as_view({ | ||
'post': 'create', | ||
'put': 'update' | ||
}) | ||
|
||
ct = ContentType.objects.get_for_model(self.xform) | ||
|
||
data = { | ||
'data_type': XFORM_META_PERMS, | ||
'data_value': 'editor-minor|dataentry', | ||
'xform': self.xform.pk | ||
} | ||
request = self.factory.post('/', data, **self.extra) | ||
response = view(request) | ||
|
||
self.assertEqual(response.status_code, 201) | ||
|
||
count = MetaData.objects.filter(data_type=XFORM_META_PERMS, | ||
object_id=self.xform.pk, | ||
content_type=ct.pk).count() | ||
|
||
self.assertEqual(1, count) | ||
|
||
data = { | ||
'data_type': XFORM_META_PERMS, | ||
'data_value': 'editor-minor|dataentry-only', | ||
'xform': self.xform.pk | ||
} | ||
request = self.factory.post('/', data, **self.extra) | ||
response = view(request) | ||
|
||
self.assertEqual(response.status_code, 201) | ||
|
||
count = MetaData.objects.filter(data_type=XFORM_META_PERMS, | ||
object_id=self.xform.pk, | ||
content_type=ct.pk).count() | ||
|
||
self.assertEqual(1, count) | ||
|
||
metadata = MetaData.xform_meta_permission(self.xform) | ||
self.assertEqual(metadata.data_value, "editor-minor|dataentry-only") |
This file contains 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 |
---|---|---|
|
@@ -14,8 +14,9 @@ | |
TestAbstractViewSet | ||
from onadata.apps.api.viewsets.project_viewset import ProjectViewSet | ||
from onadata.libs.permissions import ( | ||
OwnerRole, ReadOnlyRole, ManagerRole, DataEntryRole, EditorRole, | ||
ReadOnlyRoleNoDownload) | ||
OwnerRole, ReadOnlyRole, ManagerRole, DataEntryRole, EditorMinorRole, | ||
EditorRole, ReadOnlyRoleNoDownload, DataEntryMinorRole, DataEntryOnlyRole, | ||
ROLES_ORDERED) | ||
from onadata.libs.serializers.project_serializer import ProjectSerializer,\ | ||
BaseProjectSerializer | ||
from onadata.libs import permissions as role | ||
|
@@ -1910,14 +1911,61 @@ def test_two_dataviews_count(self): | |
# assert count | ||
self.assertIn('data_views', response.data) | ||
self.assertEqual(len(response.data['data_views']), 2) | ||
self.assertEqual(response.data['data_views'][1]['count'], 4) | ||
self.assertEqual(response.data['data_views'][0]['count'], 0) | ||
count_one = response.data['data_views'][1]['count'] | ||
count_two = response.data['data_views'][0]['count'] | ||
self.assertEqual([count_one, count_two].sort(), [0, 4].sort()) | ||
|
||
request = self.factory.get('/', **self.extra) | ||
response = view(request, pk=self.project.pk) | ||
|
||
# assert count | ||
self.assertIn('data_views', response.data) | ||
self.assertTrue(len(response.data['data_views']) == 2) | ||
self.assertTrue(response.data['data_views'][1]['count'] == 4) | ||
self.assertTrue(response.data['data_views'][0]['count'] == 0) | ||
count_one = response.data['data_views'][1]['count'] | ||
count_two = response.data['data_views'][0]['count'] | ||
self.assertEqual([count_one, count_two].sort(), [0, 4].sort()) | ||
|
||
def test_project_share_xform_meta_perms(self): | ||
# create project and publish form to project | ||
self._publish_xls_form_to_project() | ||
alice_data = {'username': 'alice', 'email': '[email protected]'} | ||
alice_profile = self._create_user_profile(alice_data) | ||
projectid = self.project.pk | ||
|
||
data_value = "editor-minor|dataentry" | ||
|
||
MetaData.xform_meta_permission(self.xform, data_value=data_value) | ||
|
||
for role_class in ROLES_ORDERED: | ||
self.assertFalse(role_class.user_has_role(alice_profile.user, | ||
self.project)) | ||
|
||
data = {'username': 'alice', 'role': role_class.name} | ||
request = self.factory.post('/', data=data, **self.extra) | ||
|
||
view = ProjectViewSet.as_view({ | ||
'post': 'share' | ||
}) | ||
response = view(request, pk=projectid) | ||
|
||
self.assertEqual(response.status_code, 204) | ||
|
||
self.assertTrue(role_class.user_has_role(alice_profile.user, | ||
self.project)) | ||
|
||
if role_class in [EditorRole, EditorMinorRole]: | ||
self.assertFalse( | ||
EditorRole.user_has_role(alice_profile.user, self.xform)) | ||
self.assertTrue( | ||
EditorMinorRole.user_has_role(alice_profile.user, | ||
self.xform)) | ||
|
||
elif role_class in [DataEntryRole, DataEntryMinorRole, | ||
DataEntryOnlyRole]: | ||
self.assertTrue( | ||
DataEntryRole.user_has_role(alice_profile.user, | ||
self.xform)) | ||
|
||
else: | ||
self.assertTrue( | ||
role_class.user_has_role(alice_profile.user, self.xform)) |
Oops, something went wrong.