Ejemplo n.º 1
0
    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]})
Ejemplo n.º 2
0
    def test_should_return_both_xform_and_project_metadata(self):
        # delete all existing metadata
        MetaData.objects.all().delete()
        expected_metadata_count = 2

        project_response = self._add_project_metadata(self.project, 'media',
                                                      "check.png", self.path)
        self.assertTrue("image/png" in project_response.data['data_file_type'])

        form_response = self._add_form_metadata(self.xform, 'supporting_doc',
                                                "bla.png", self.path)
        self.assertTrue("image/png" in form_response.data['data_file_type'])

        view = MetaDataViewSet.as_view({'get': 'list'})
        request = self.factory.get("/", **self.extra)
        response = view(request)

        self.assertEquals(MetaData.objects.count(), expected_metadata_count)

        for record in response.data:
            if record.get("xform"):
                self.assertEquals(record.get('xform'), self.xform.id)
                self.assertIsNone(record.get('project'))
            else:
                self.assertEquals(record.get('project'), self.project.id)
                self.assertIsNone(record.get('xform'))
Ejemplo n.º 3
0
    def test_should_return_both_xform_and_project_metadata(self):
        # delete all existing metadata
        MetaData.objects.all().delete()
        expected_metadata_count = 2

        project_response = self._add_project_metadata(
            self.project, 'media', "check.png", self.path)
        self.assertTrue("image/png" in project_response.data['data_file_type'])

        form_response = self._add_form_metadata(
            self.xform, 'supporting_doc', "bla.png", self.path)
        self.assertTrue("image/png" in form_response.data['data_file_type'])

        view = MetaDataViewSet.as_view({'get': 'list'})
        request = self.factory.get("/", **self.extra)
        response = view(request)

        self.assertEquals(MetaData.objects.count(), expected_metadata_count)

        for record in response.data:
            if record.get("xform"):
                self.assertEquals(record.get('xform'), self.xform.id)
                self.assertIsNone(record.get('project'))
            else:
                self.assertEquals(record.get('project'), self.project.id)
                self.assertIsNone(record.get('xform'))
Ejemplo n.º 4
0
    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]})
Ejemplo n.º 5
0
    def test_list_metadata_for_specific_form(self):
        self._add_test_metadata()
        self.view = MetaDataViewSet.as_view({'get': 'list'})
        data = {'xform': self.xform.pk}

        # Access with anonymous user
        request = self.factory.get('/', data)
        response = self.view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [])

        # Access with user bob
        request = self.factory.get('/', data, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, 200)
        self.assertTrue('xform' in response.data[0])
        self.assertTrue(response.data[0]['xform'], self.xform.pk)

        data['xform'] = 1234509909
        request = self.factory.get('/', data, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, 404)

        data['xform'] = "INVALID"
        request = self.factory.get('/', data, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, 400)
Ejemplo n.º 6
0
    def test_list_metadata(self):
        self._add_test_metadata()
        self.view = MetaDataViewSet.as_view({'get': 'list'})
        request = self.factory.get('/')
        response = self.view(request)
        self.assertEqual(response.status_code, 401)

        request = self.factory.get('/', **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, 200)
Ejemplo n.º 7
0
    def test_list_metadata(self):
        self._add_test_metadata()
        self.view = MetaDataViewSet.as_view({'get': 'list'})
        request = self.factory.get('/')
        response = self.view(request)
        self.assertEqual(response.status_code, 401)

        request = self.factory.get('/', **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, 200)
Ejemplo n.º 8
0
    def test_team_members_meta_perms_restrictions(self):
        self._team_create()
        self._publish_xls_form_to_project()
        user_alice = self._create_user('alice', 'alice')

        members_team = Team.objects.get(
            name='%s#%s' % (self.organization.user.username, 'members'))

        # add alice to members team
        add_user_to_team(members_team, user_alice)

        # confirm that the team and members have no permissions on form
        self.assertFalse(get_perms(members_team, self.xform))
        self.assertFalse(get_perms(user_alice, self.xform))

        # share project to team
        view = TeamViewSet.as_view({
            'get': 'list',
            'post': 'share'})

        post_data = {
            'role': EditorRole.name,
            'project': self.project.pk,
            'remove': False
        }
        request = self.factory.post('/', data=post_data, **self.extra)
        response = view(request, pk=members_team.pk)
        self.assertEqual(response.status_code, 204)

        # team members should have editor permissions now
        alice_perms = get_perms(user_alice, self.xform)
        alice_role = get_role(alice_perms, self.xform)
        self.assertEqual(EditorRole.name, alice_role)
        self.assertTrue(EditorRole.user_has_role(user_alice, self.xform))

        # change meta permissions
        meta_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 = meta_view(request)
        self.assertEqual(response.status_code, 201)

        # members should now have EditorMinor role
        self.assertTrue(EditorMinorRole.user_has_role(user_alice, self.xform))
Ejemplo n.º 9
0
 def setUp(self):
     super(TestMetaDataViewSet, self).setUp()
     self.view = MetaDataViewSet.as_view({
         'delete': 'destroy',
         'get': 'retrieve',
         'post': 'create'
     })
     self._publish_xls_form_to_project()
     self.data_value = "screenshot.png"
     self.fixture_dir = os.path.join(settings.PROJECT_ROOT, "apps", "main",
                                     "tests", "fixtures", "transportation")
     self.path = os.path.join(self.fixture_dir, self.data_value)
Ejemplo n.º 10
0
    def test_team_members_meta_perms_restrictions(self):
        self._team_create()
        self._publish_xls_form_to_project()
        user_alice = self._create_user('alice', 'alice')

        members_team = Team.objects.get(
            name='%s#%s' % (self.organization.user.username, 'members'))

        # add alice to members team
        add_user_to_team(members_team, user_alice)

        # confirm that the team and members have no permissions on form
        self.assertFalse(get_perms(members_team, self.xform))
        self.assertFalse(get_perms(user_alice, self.xform))

        # share project to team
        view = TeamViewSet.as_view({
            'get': 'list',
            'post': 'share'})

        post_data = {
            'role': EditorRole.name,
            'project': self.project.pk,
            'remove': False
        }
        request = self.factory.post('/', data=post_data, **self.extra)
        response = view(request, pk=members_team.pk)
        self.assertEqual(response.status_code, 204)

        # team members should have editor permissions now
        alice_perms = get_perms(user_alice, self.xform)
        alice_role = get_role(alice_perms, self.xform)
        self.assertEqual(EditorRole.name, alice_role)
        self.assertTrue(EditorRole.user_has_role(user_alice, self.xform))

        # change meta permissions
        meta_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 = meta_view(request)
        self.assertEqual(response.status_code, 201)

        # members should now have EditorMinor role
        self.assertTrue(EditorMinorRole.user_has_role(user_alice, self.xform))
Ejemplo n.º 11
0
 def setUp(self):
     super().setUp()
     self.view = MetaDataViewSet.as_view({
         'delete': 'destroy',
         'get': 'retrieve',
         'post': 'create'
     })
     self.publish_xls_form()
     self.data_value = "screenshot.png"
     self.fixture_dir = os.path.join(settings.ONADATA_DIR, "apps", "main",
                                     "tests", "fixtures", "transportation")
     self.path = os.path.join(self.fixture_dir, self.data_value)
Ejemplo n.º 12
0
    def _create_metadata_object(self):
        view = MetaDataViewSet.as_view({'post': 'create'})
        with open(self.path, 'rb') as media_file:
            data = {
                'data_type': 'media',
                'data_value': 'check.png',
                'data_file': media_file,
                'project': self.project.id
            }
            request = self.factory.post('/', data, **self.extra)
            response = view(request)

            return response
Ejemplo n.º 13
0
    def _create_metadata_object(self):
        view = MetaDataViewSet.as_view({'post': 'create'})
        with open(self.path) as media_file:
            data = {
                'data_type': 'media',
                'data_value': 'check.png',
                'data_file': media_file,
                'project': self.project.id
            }
            request = self.factory.post('/', data, **self.extra)
            response = view(request)

            return response
Ejemplo n.º 14
0
    def _post_form_metadata(self, data, test=True):
        count = MetaData.objects.count()
        view = MetaDataViewSet.as_view({'post': 'create'})
        request = self.factory.post('/', data, **self.extra)
        response = view(request)

        if test:
            self.assertEqual(response.status_code, 201)
            another_count = MetaData.objects.count()
            self.assertEqual(another_count, count + 1)
            self.metadata = MetaData.objects.get(pk=response.data['id'])
            self.metadata_data = response.data

        return response
Ejemplo n.º 15
0
    def _post_form_metadata(self, data, test=True):
        count = MetaData.objects.count()
        view = MetaDataViewSet.as_view({'post': 'create'})
        request = self.factory.post('/', data, **self.extra)
        response = view(request)

        if test:
            self.assertEqual(response.status_code, 201)
            another_count = MetaData.objects.count()
            self.assertEqual(another_count, count + 1)
            self.metadata = MetaData.objects.get(pk=response.data['id'])
            self.metadata_data = response.data

        return response
Ejemplo n.º 16
0
 def setUp(self):
     super(TestMetaDataViewSet, self).setUp()
     self.view = MetaDataViewSet.as_view({
         'delete': 'destroy',
         'get': 'retrieve',
         'post': 'create'
     })
     self._publish_xls_form_to_project()
     self.data_value = "screenshot.png"
     self.fixture_dir = os.path.join(
         settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures",
         "transportation"
     )
     self.path = os.path.join(self.fixture_dir, self.data_value)
Ejemplo n.º 17
0
    def test_invalid_form_metadata(self):
        view = MetaDataViewSet.as_view({'post': 'create'})
        with open(self.path, 'rb') as media_file:
            data = {
                'data_type': "media",
                'data_value': self.data_value,
                'xform': 999912,
                'data_file': media_file,
            }

            request = self.factory.post('/', data, **self.extra)
            response = view(request)

            self.assertEqual(response.status_code, 400)
            self.assertEqual(response.data,
                             {'xform': ['XForm does not exist']})
Ejemplo n.º 18
0
    def test_invalid_form_metadata(self):
        view = MetaDataViewSet.as_view({'post': 'create'})
        with open(self.path) as media_file:
            data = {
                'data_type': "media",
                'data_value': self.data_value,
                'xform': 999912,
                'data_file': media_file,
            }

            request = self.factory.post('/', data, **self.extra)
            response = view(request)

            self.assertEqual(response.status_code, 400)
            self.assertEqual(response.data,
                             {'xform': ['XForm does not exist']})
Ejemplo n.º 19
0
    def test_role_update_xform_meta_perms(self):
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        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))
Ejemplo n.º 20
0
    def test_role_update_xform_meta_perms(self):
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        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))
Ejemplo n.º 21
0
    def test_should_only_return_xform_metadata(self):
        # delete all existing metadata
        MetaData.objects.all().delete()

        self._add_project_metadata(self.project, 'media', "check.png",
                                   self.path)

        self._add_form_metadata(self.xform, 'supporting_doc', "bla.png",
                                self.path)

        view = MetaDataViewSet.as_view({'get': 'list'})
        query_data = {"xform": self.xform.id}
        request = self.factory.get("/", data=query_data, **self.extra)
        response = view(request)

        self.assertEqual(len(response.data), 1)
        self.assertIn("xform", response.data[0])
        self.assertNotIn("project", response.data[0])
Ejemplo n.º 22
0
    def test_should_only_return_xform_metadata(self):
        # delete all existing metadata
        MetaData.objects.all().delete()

        self._add_project_metadata(
            self.project, 'media', "check.png", self.path)

        self._add_form_metadata(
            self.xform, 'supporting_doc', "bla.png", self.path)

        view = MetaDataViewSet.as_view({'get': 'list'})
        query_data = {"xform": self.xform.id}
        request = self.factory.get("/", data=query_data, **self.extra)
        response = view(request)

        self.assertEqual(len(response.data), 1)
        self.assertIn("xform", response.data[0])
        self.assertNotIn("project", response.data[0])
Ejemplo n.º 23
0
    def test_unique_submission_review_metadata(self):
        """Don't create duplicate submission_review for a form"""
        data_type = "submission_review"
        data_value = True

        response = self._add_form_metadata(self.xform, data_type, data_value)
        # Duplicate with different Data Value

        view = MetaDataViewSet.as_view({'post': 'create'})
        data = {
            'xform': response.data['xform'],
            'data_type': data_type,
            'data_value': False,
        }
        request = self.factory.post('/', data, **self.extra)
        d_response = view(request)

        self.assertEquals(d_response.status_code, 400)
        self.assertIn(UNIQUE_TOGETHER_ERROR, d_response.data)
Ejemplo n.º 24
0
    def test_unique_submission_review_metadata(self):
        """Don't create duplicate submission_review for a form"""
        data_type = "submission_review"
        data_value = True

        response = self._add_form_metadata(self.xform, data_type, data_value)
        # Duplicate with different Data Value

        view = MetaDataViewSet.as_view({'post': 'create'})
        data = {
            'xform': response.data['xform'],
            'data_type': data_type,
            'data_value': False,
        }
        request = self.factory.post('/', data, **self.extra)
        d_response = view(request)

        self.assertEquals(d_response.status_code, 400)
        self.assertIn(UNIQUE_TOGETHER_ERROR, d_response.data)
Ejemplo n.º 25
0
    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")
Ejemplo n.º 26
0
    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")
Ejemplo n.º 27
0
    def test_list_metadata_for_specific_form(self):
        self._add_test_metadata()
        self.view = MetaDataViewSet.as_view({'get': 'list'})
        data = {'xform': self.xform.pk}
        request = self.factory.get('/', data)
        response = self.view(request)
        self.assertEqual(response.status_code, 401)

        request = self.factory.get('/', data, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, 200)

        data['xform'] = 1234509909
        request = self.factory.get('/', data, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, 404)

        data['xform'] = "INVALID"
        request = self.factory.get('/', data, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, 400)
Ejemplo n.º 28
0
    def test_list_metadata_for_specific_form(self):
        self._add_test_metadata()
        self.view = MetaDataViewSet.as_view({'get': 'list'})
        data = {'xform': self.xform.pk}
        request = self.factory.get('/', data)
        response = self.view(request)
        self.assertEqual(response.status_code, 401)

        request = self.factory.get('/', data, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, 200)

        data['xform'] = 1234509909
        request = self.factory.get('/', data, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, 404)

        data['xform'] = "INVALID"
        request = self.factory.get('/', data, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, 400)
    def setUp(self):
        TestBase.setUp(self)
        self._create_user_and_login()
        self.extra = {'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token}

        self.xls_file_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "../fixtures/tutorial/tutorial.xls")

        self._publish_transportation_form_and_submit_instance()
        self.project = Project.objects.create(name="Test Project",
                                              organization=self.user,
                                              created_by=self.user)
        self.xform.project = self.project
        self.xform.save()

        self.instance = self.xform.instances.first()
        self._add_external_export_metadata(self.instance)

        self.view = MetaDataViewSet.as_view({'get': 'list'})

        ContentType.objects.get_or_create(app_label="logger", model="instance")
    def setUp(self):
        TestBase.setUp(self)
        self._create_user_and_login()
        self.extra = {'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token}

        self.xls_file_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "../fixtures/tutorial/tutorial.xls"
        )

        self._publish_transportation_form_and_submit_instance()
        self.project = Project.objects.create(name="Test Project",
                                              organization=self.user,
                                              created_by=self.user)
        self.xform.project = self.project
        self.xform.save()

        self.instance = self.xform.instances.first()
        self._add_external_export_metadata(self.instance)

        self.view = MetaDataViewSet.as_view({'get': 'list'})

        ContentType.objects.get_or_create(app_label="logger", model="instance")