def test_form_tags(self):
        self._publish_xls_form_to_project()
        view = XFormViewSet.as_view({"get": "labels", "post": "labels", "delete": "labels"})
        list_view = XFormViewSet.as_view({"get": "list"})
        formid = self.xform.pk

        # no tags
        request = self.factory.get("/", **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.data, [])

        # add tag "hello"
        request = self.factory.post("/", data={"tags": "hello"}, **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data, [u"hello"])

        # check filter by tag
        request = self.factory.get("/", data={"tags": "hello"}, **self.extra)
        self.form_data = XFormSerializer(self.xform, context={"request": request}).data
        response = list_view(request, pk=formid)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [self.form_data])

        request = self.factory.get("/", data={"tags": "goodbye"}, **self.extra)
        response = list_view(request, pk=formid)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [])

        # remove tag "hello"
        request = self.factory.delete("/", data={"tags": "hello"}, **self.extra)
        response = view(request, pk=formid, label="hello")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [])
    def test_form_export_with_no_xlsform_returns_200(self):
        self._publish_xml_form()
        self.view = XFormViewSet.as_view({'get': 'retrieve'})

        xform = XForm.objects.get(id_string="transportation_2011_07_25")
        request = self.factory.get('/', **self.extra)
        response = self.view(request, pk=xform.pk, format='csv')

        self.assertEqual(response.status_code, 200)

        self.view = XFormViewSet.as_view({'get': 'form'})
        response = self.view(request, pk=xform.pk, format='xls')
        self.assertEqual(response.status_code, 404)
    def test_submission_to_require_auth_with_perm(self):
        """
        test submission to a private form by non-owner is forbidden.

        TODO send authentication challenge when xform.require_auth is set.
        This is non-trivial because we do not know the xform until we have
        parsed the XML.
        """
        raise SkipTest

        view = XFormViewSet.as_view({
            'patch': 'partial_update'
        })
        data = {'require_auth': True}
        self.assertFalse(self.xform.require_auth)
        request = self.factory.patch('/', data=data, **{
            'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token})
        view(request, pk=self.xform.id)
        self.xform.reload()
        self.assertTrue(self.xform.require_auth)

        # create a new user
        username = '******'
        alice = self._create_user(username, username)

        # assign report perms to user
        assign_perm('report_xform', alice, self.xform)
        auth = DigestAuth(username, username)

        xml_submission_file_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "../fixtures/tutorial/instances/tutorial_2012-06-27_11-27-53.xml"
        )
        self._make_submission(xml_submission_file_path, auth=auth)
        self.assertEqual(self.response.status_code, 201)
    def test_form_clone_endpoint(self):
        self._publish_xls_form_to_project()
        alice_data = {"username": "******", "email": "*****@*****.**"}
        alice_profile = self._create_user_profile(alice_data)
        view = XFormViewSet.as_view({"post": "clone"})
        formid = self.xform.pk
        count = XForm.objects.count()

        data = {"username": "******"}
        request = self.factory.post("/", data=data, **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 400)

        data = {"username": "******"}
        request = self.factory.post("/", data=data, **self.extra)
        response = view(request, pk=formid)
        self.assertFalse(self.user.has_perm("can_add_xform", alice_profile))
        self.assertEqual(response.status_code, 403)

        ManagerRole.add(self.user, alice_profile)
        request = self.factory.post("/", data=data, **self.extra)
        response = view(request, pk=formid)
        self.assertTrue(self.user.has_perm("can_add_xform", alice_profile))
        self.assertEqual(response.status_code, 201)
        self.assertEqual(count + 1, XForm.objects.count())
    def test_manager_can_update_xform(self):
        self._publish_xls_form_to_project()
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)
        view = XFormViewSet.as_view({
            'put': 'update'
        })
        description = 'DESCRIPTION'
        request = self.factory.get('/', **self.extra)
        xfs = XFormSerializer(instance=self.xform,
                              context={'request': request})
        data = json.loads(JSONRenderer().render(xfs.data))
        data.update({'public': True, 'description': description})

        self.assertFalse(self.xform.shared)

        request = self.factory.put('/', data=data, **self.extra)
        response = view(request, pk=self.xform.id)
        self.assertEqual(response.status_code, 404)
        self.assertFalse(self.xform.shared)

        role.ManagerRole.add(self.user, self.xform)
        request = self.factory.put('/', data=data, **self.extra)
        response = view(request, pk=self.xform.id)

        self.xform.refresh_from_db()
        self.assertTrue(self.xform.shared)
        self.assertEqual(self.xform.description, description)
        self.assertEqual(response.data['public'], True)
        self.assertEqual(response.data['description'], description)
    def test_readonly_role(self):
        self._publish_xls_form_to_project()
        self._make_submissions()
        view = XFormViewSet.as_view({
            'get': 'retrieve',
            'put': 'update'
        })
        data_view = DataViewSet.as_view({'get': 'list'})
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)
        formid = self.xform.pk

        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 404)

        response = data_view(request, pk=formid)
        self.assertEqual(response.status_code, 403)

        role.ReadOnlyRole.add(self.user, self.xform)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 200)

        response = data_view(request, pk=formid)
        self.assertEqual(response.status_code, 200)

        data = {'public': True, 'description': "Some description"}
        request = self.factory.put('/', data=data, **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 403)
    def test_public_form_list(self):
        self._publish_xls_form_to_project()
        self.view = XFormViewSet.as_view({"get": "retrieve"})
        request = self.factory.get("/", **self.extra)
        response = self.view(request, pk="public")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [])

        # public shared form
        self.xform.shared = True
        self.xform.save()
        response = self.view(request, pk="public")
        self.assertEqual(response.status_code, 200)
        self.form_data["public"] = True
        del self.form_data["date_modified"]
        del response.data[0]["date_modified"]
        self.assertEqual(response.data, [self.form_data])

        # public shared form data
        self.xform.shared_data = True
        self.xform.shared = False
        self.xform.save()
        response = self.view(request, pk="public")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [])
Exemple #8
0
    def _publish_osm_with_submission(self):
        filenames = [
            'OSMWay234134797.osm',
            'OSMWay34298972.osm',
        ]
        self.fixtures_dir = osm_fixtures_dir = os.path.realpath(os.path.join(
            os.path.dirname(__file__), '..', 'fixtures', 'osm'))
        paths = [
            os.path.join(osm_fixtures_dir, filename)
            for filename in filenames]
        xlsform_path = os.path.join(osm_fixtures_dir, 'osm.xlsx')
        self.combined_osm_path = os.path.join(osm_fixtures_dir, 'combined.osm')
        self._publish_xls_form_to_project(xlsform_path=xlsform_path)
        self.xform.version = '201511091147'
        self.xform.save()

        # look at the forms.json?instances_with_osm=True
        request = self.factory.get('/', {'instances_with_osm': 'True'},
                                   **self.extra)
        view = XFormViewSet.as_view({'get': 'list'})
        response = view(request, format='json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [])

        submission_path = os.path.join(osm_fixtures_dir, 'instance_a.xml')
        files = [open(path) for path in paths]
        count = Attachment.objects.filter(extension='osm').count()
        count_osm = OsmData.objects.count()
        _submission_time = parse_datetime('2013-02-18 15:54:01Z')
        self._make_submission(submission_path, media_file=files,
                              forced_submission_time=_submission_time)
        self.assertTrue(
            Attachment.objects.filter(extension='osm').count() > count)
        self.assertEqual(OsmData.objects.count(), count_osm + 2)
    def test_md_csv_export(self):
        """Test CSV export of a merged dataset"""
        merged_dataset = self._create_merged_dataset()
        merged_xform = MergedXForm.objects.get(pk=merged_dataset['id'])

        _make_submissions_merged_datasets(merged_xform)

        # merged dataset should be available at api/forms/[pk] endpoint
        request = self.factory.get('/', **self.extra)
        view = XFormViewSet.as_view({'get': 'retrieve'})
        response = view(request, pk=merged_dataset['id'], format='csv')
        self.assertEqual(response.status_code, 200)

        csv_file_obj = StringIO(''.join(
            [c.decode('utf-8') for c in response.streaming_content]))
        csv_reader = csv.reader(csv_file_obj)
        # jump over headers first
        headers = next(csv_reader)
        self.assertEqual(headers, [
            'fruit', 'meta/instanceID', '_id', '_uuid', '_submission_time',
            '_tags', '_notes', '_version', '_duration', '_submitted_by',
            '_total_media', '_media_count', '_media_all_received'])
        row1 = next(csv_reader)
        self.assertEqual(row1[0], 'orange')
        row2 = next(csv_reader)
        self.assertEqual(row2[0], 'mango')
Exemple #10
0
    def test_external_export(self):
        self._publish_xls_form_to_project()

        data_value = 'template 1|http://xls_server'
        self._add_form_metadata(self.xform, 'external_export',
                                data_value)
        metadata = MetaData.objects.get(xform=self.xform,
                                        data_type='external_export')
        paths = [os.path.join(
            self.main_directory, 'fixtures', 'transportation',
            'instances_w_uuid', s, s + '.xml')
            for s in ['transport_2011-07-25_19-05-36']]

        self._make_submission(paths[0])
        self.assertEqual(self.response.status_code, 201)

        view = XFormViewSet.as_view({
            'get': 'retrieve',
        })
        data = {'meta': metadata.pk}
        formid = self.xform.pk
        request = self.factory.get('/', data=data,
                                   **self.extra)
        with HTTMock(external_mock):
            # External export
            response = view(
                request,
                pk=formid,
                format='xls')
            self.assertEqual(response.status_code, 302)
            expected_url = \
                'http://xls_server/xls/ee3ff9d8f5184fc4a8fdebc2547cc059'
            self.assertEquals(response.url, expected_url)
Exemple #11
0
    def test_form_clone_endpoint(self):
        self._publish_xls_form_to_project()
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        alice_profile = self._create_user_profile(alice_data)
        view = XFormViewSet.as_view({
            'post': 'clone'
        })
        formid = self.xform.pk
        count = XForm.objects.count()

        data = {'username': '******'}
        request = self.factory.post('/', data=data, **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 400)

        data = {'username': '******'}
        request = self.factory.post('/', data=data, **self.extra)
        response = view(request, pk=formid)
        self.assertFalse(self.user.has_perm('can_add_xform', alice_profile))
        self.assertEqual(response.status_code, 403)

        ManagerRole.add(self.user, alice_profile)
        request = self.factory.post('/', data=data, **self.extra)
        response = view(request, pk=formid)
        self.assertTrue(self.user.has_perm('can_add_xform', alice_profile))
        self.assertEqual(response.status_code, 201)
        self.assertEqual(count + 1, XForm.objects.count())
    def test_update_xform_using_put(self):
        self._publish_xls_form_to_project()
        form_id = self.xform.pk

        version = self.xform.version
        view = XFormViewSet.as_view({
            'put': 'update',
        })

        post_data = {
            'uuid': 'ae631e898bd34ced91d2a309d8b72das',
            'description': 'Transport form',
            'downloadable': False,
            'owner': 'http://testserver/api/v1/users/{0}'.format(self.user),
            'created_by':
            'http://testserver/api/v1/users/{0}'.format(self.user),
            'public': False,
            'public_data': False,
            'project': 'http://testserver/api/v1/projects/{0}'.format(
                self.xform.project.pk),
            'title': 'Transport Form'
        }
        request = self.factory.put('/', data=post_data, **self.extra)
        response = view(request, pk=form_id)
        self.assertEqual(response.status_code, 200, response.data)

        self.xform.reload()

        self.assertEquals(version, self.xform.version)
        self.assertEquals(self.xform.description, u'Transport form')
        self.assertEquals(self.xform.title, u'Transport Form')
        self.assertEquals(form_id, self.xform.pk)
    def test_update_xform_using_put_without_required_field(self):
        self._publish_xls_form_to_project()
        form_id = self.xform.pk

        view = XFormViewSet.as_view({
            'put': 'update',
        })

        post_data = {
            'uuid': 'ae631e898bd34ced91d2a309d8b72das',
            'description': 'Transport form',
            'downloadable': False,
            'owner': 'http://testserver/api/v1/users/{0}'.format(self.user),
            'created_by':
            'http://testserver/api/v1/users/{0}'.format(self.user),
            'public': False,
            'public_data': False,
            'project': 'http://testserver/api/v1/projects/{0}'.format(
                self.xform.project.pk),
        }
        request = self.factory.put('/', data=post_data, **self.extra)
        response = view(request, pk=form_id)

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.get('Last-Modified'), None)
        self.assertEquals(response.data,
                          {'title': [u'This field is required.']})
    def test_update_xform_xls_file_with_submissions(self):
        self._publish_xls_form_to_project()
        self._make_submissions()

        self.assertIsNotNone(self.xform.version)
        version = self.xform.version
        form_id = self.xform.pk

        view = XFormViewSet.as_view({
            'patch': 'partial_update',
        })

        path = os.path.join(
            settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures",
            "transportation", "transportation_updated.xls")
        with open(path) as xls_file:
            post_data = {'xls_file': xls_file}
            request = self.factory.patch('/', data=post_data, **self.extra)
            response = view(request, pk=form_id)

            self.assertEqual(response.status_code, 400)
            self.assertEqual(response.get('Last-Modified'), None)
            self.assertEquals(response.data, u"Cannot update the xls file in "
                                             u"a form that has submissions")

        self.xform.reload()
        new_version = self.xform.version

        # diff versions
        self.assertEquals(version, new_version)
        self.assertEquals(form_id, self.xform.pk)
    def test_update_xform_xls_file_with_version_set(self):
        self._publish_xls_form_to_project()
        form_id = self.xform.pk

        self.assertIsNotNone(self.xform.version)

        view = XFormViewSet.as_view({
            'patch': 'partial_update',
        })

        path = os.path.join(
            settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures",
            "transportation", "transportation_version.xls")
        with open(path) as xls_file:
            post_data = {'xls_file': xls_file}
            request = self.factory.patch('/', data=post_data, **self.extra)
            response = view(request, pk=form_id)

            self.assertEqual(response.status_code, 200)

        self.xform.reload()

        # diff versions
        self.assertEquals(self.xform.version, u"212121211")
        self.assertEquals(form_id, self.xform.pk)
Exemple #16
0
    def test_submission_to_require_auth_without_perm(self):
        """
        test submission to a private form by non-owner without perm is
        forbidden.
        """
        view = XFormViewSet.as_view({
            'patch': 'partial_update'
        })
        data = {'require_auth': True}
        self.assertFalse(self.xform.require_auth)
        request = self.factory.patch('/', data=data, **{
            'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token})
        view(request, pk=self.xform.id)
        self.xform.reload()
        self.assertTrue(self.xform.require_auth)

        xml_submission_file_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "../fixtures/tutorial/instances/tutorial_2012-06-27_11-27-53.xml"
        )

        # create a new user
        username = '******'
        self._create_user(username, username)

        client = DigestClient()
        client.set_authorization(username, username, 'Digest')

        self._make_submission(xml_submission_file_path, client=client)
        self.assertEqual(self.response.status_code, 403)
Exemple #17
0
    def test_external_export_error(self):
        self._publish_xls_form_to_project()

        data_value = 'template 1|http://xls_server'
        self._add_form_metadata(self.xform, 'external_export',
                                data_value)

        paths = [os.path.join(
            self.main_directory, 'fixtures', 'transportation',
            'instances_w_uuid', s, s + '.xml')
            for s in ['transport_2011-07-25_19-05-36']]

        self._make_submission(paths[0])
        self.assertEqual(self.response.status_code, 201)

        view = XFormViewSet.as_view({
            'get': 'retrieve',
        })
        formid = self.xform.pk
        token = 'http://xls_server/xls/' +\
            '8e86d4bdfa7f435ab89485aeae4ea6f5'
        data = {'token': token}
        request = self.factory.get('/', data=data, **self.extra)

        # External export
        response = view(
            request,
            pk=formid,
            format='xls')

        self.assertEqual(response.status_code, 400)
        data = json.loads(response.data)
        self.assertTrue(data.get('error')
                        .startswith("J2X client could not generate report."))
    def test_manager_can_update_xform_tags(self):
        self._publish_xls_form_to_project()
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)
        view = XFormViewSet.as_view({
            'get': 'labels',
            'post': 'labels',
            'delete': 'labels'
        })
        formid = self.xform.pk

        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 404)

        role.ManagerRole.add(self.user, self.xform)
        response = view(request, pk=formid)
        self.assertEqual(response.data, [])

        # add tag "hello"
        request = self.factory.post('/', data={"tags": "hello"}, **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data, [u'hello'])

        # remove tag "hello"
        request = self.factory.delete('/', data={"tags": "hello"},
                                      **self.extra)
        response = view(request, pk=formid, label='hello')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [])
    def test_update_xform_xls_bad_file(self):
        self._publish_xls_form_to_project()

        self.assertIsNotNone(self.xform.version)
        version = self.xform.version
        form_id = self.xform.pk

        view = XFormViewSet.as_view({
            'patch': 'partial_update',
        })

        path = os.path.join(
            settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures",
            "transportation", "transportation.bad_id.xls")
        with open(path) as xls_file:
            post_data = {'xls_file': xls_file}
            request = self.factory.patch('/', data=post_data, **self.extra)
            response = view(request, pk=form_id)

            self.assertEqual(response.status_code, 400)

        self.xform.reload()
        new_version = self.xform.version

        # fails to update the form
        self.assertEquals(version, new_version)
        self.assertEquals(form_id, self.xform.pk)
    def test_editor_role(self):
        self._publish_xls_form_to_project()
        self._make_submissions()
        view = XFormViewSet.as_view({
            'get': 'retrieve',
            'patch': 'update'
        })
        data_view = DataViewSet.as_view({'get': 'list'})
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)
        formid = self.xform.pk

        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 404)

        response = data_view(request, pk=formid)
        self.assertEqual(response.status_code, 404)

        role.EditorRole.add(self.user, self.xform)

        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 200)
        response = data_view(request, pk=formid)
        self.assertEqual(response.status_code, 200)
Exemple #21
0
    def test_partial_update(self):
        self._publish_xls_form_to_project()
        view = XFormViewSet.as_view({
            'patch': 'partial_update'
        })
        title = u'مرحب'
        description = 'DESCRIPTION'
        data = {'public': True, 'description': description, 'title': title,
                'downloadable': True}

        self.assertFalse(self.xform.shared)

        request = self.factory.patch('/', data=data, **self.extra)
        response = view(request, pk=self.xform.id)

        self.xform.reload()
        self.assertTrue(self.xform.downloadable)
        self.assertTrue(self.xform.shared)
        self.assertEqual(self.xform.description, description)
        self.assertEqual(response.data['public'], True)
        self.assertEqual(response.data['description'], description)
        self.assertEqual(response.data['title'], title)
        matches = re.findall(r"<h:title>([^<]+)</h:title>", self.xform.xml)
        self.assertTrue(len(matches) > 0)
        self.assertEqual(matches[0], title)
 def test_add_form_tag_propagates_to_data_tags(self):
     """Test that when a tag is applied on an xform,
     it propagates to the instance submissions
     """
     self._make_submissions()
     xform = XForm.objects.all()[0]
     pk = xform.id
     view = XFormViewSet.as_view({
         'get': 'labels',
         'post': 'labels',
         'delete': 'labels'
     })
     # no tags
     request = self.factory.get('/', **self.extra)
     response = view(request, pk=pk)
     self.assertEqual(response.data, [])
     # add tag "hello"
     request = self.factory.post('/', data={"tags": "hello"}, **self.extra)
     response = view(request, pk=pk)
     self.assertEqual(response.status_code, 201)
     self.assertEqual(response.data, [u'hello'])
     for i in self.xform.instances.all():
         self.assertIn(u'hello', i.tags.names())
     # remove tag "hello"
     request = self.factory.delete('/', data={"tags": "hello"},
                                   **self.extra)
     response = view(request, pk=pk, label='hello')
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.data, [])
     for i in self.xform.instances.all():
         self.assertNotIn(u'hello', i.tags.names())
Exemple #23
0
    def test_osm_csv_export(self):
        self._publish_osm_with_submission()
        count = Export.objects.all().count()

        view = XFormViewSet.as_view({
            'get': 'retrieve'
        })

        request = self.factory.get('/', data={'include_images': False},
                                   **self.extra)
        response = view(request, pk=self.xform.pk, format='csv')
        self.assertEqual(response.status_code, 200)

        self.assertEquals(count + 1, Export.objects.all().count())

        headers = dict(response.items())
        self.assertEqual(headers['Content-Type'], 'application/csv')
        content_disposition = headers['Content-Disposition']
        filename = filename_from_disposition(content_disposition)
        basename, ext = os.path.splitext(filename)
        self.assertEqual(ext, '.csv')

        content = get_response_content(response)
        reader = csv.DictReader(StringIO(content))
        data = [_ for _ in reader]
        test_file_path = os.path.join(self.fixtures_dir, 'osm.csv')
        with open(test_file_path, 'r') as test_file:
            expected_csv_reader = csv.DictReader(test_file)
            for index, row in enumerate(expected_csv_reader):
                self.assertDictContainsSubset(row, data[index])

        request = self.factory.get('/', **self.extra)
        response = view(request, pk=self.xform.pk, format='csv')
        self.assertEqual(response.status_code, 200)
Exemple #24
0
    def test_form_data_export(self):
        self._make_submissions()
        view = XFormViewSet.as_view({
            'get': 'retrieve'
        })
        formid = self.xform.pk
        # csv
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid, format='csv')
        self.assertEqual(response.status_code, 200)
        headers = dict(response.items())
        content_disposition = headers['Content-Disposition']
        filename = self._filename_from_disposition(content_disposition)
        basename, ext = os.path.splitext(filename)
        self.assertEqual(headers['Content-Type'], 'application/csv')
        self.assertEqual(ext, '.csv')

        # xls
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid, format='xls')
        self.assertEqual(response.status_code, 200)
        headers = dict(response.items())
        content_disposition = headers['Content-Disposition']
        filename = self._filename_from_disposition(content_disposition)
        basename, ext = os.path.splitext(filename)
        self.assertEqual(headers['Content-Type'],
                         'application/vnd.openxmlformats')
        self.assertEqual(ext, '.xlsx')
Exemple #25
0
    def test_form_share_endpoint(self):
        self._publish_xls_form_to_project()
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        alice_profile = self._create_user_profile(alice_data)

        view = XFormViewSet.as_view({
            'post': 'share'
        })
        formid = self.xform.pk

        ROLES = [ReadOnlyRole,
                 DataEntryRole,
                 EditorRole,
                 ManagerRole,
                 OwnerRole]
        for role_class in ROLES:
            self.assertFalse(role_class.user_has_role(alice_profile.user,
                                                      self.xform))

            data = {'username': '******', 'role': role_class.name}
            request = self.factory.post('/', data=data, **self.extra)
            response = view(request, pk=formid)

            self.assertEqual(response.status_code, 204)
            self.assertTrue(role_class.user_has_role(alice_profile.user,
                                                     self.xform))
    def test_forms_endpoint_with_metadata(self):
        date_modified = self.xform.date_modified
        for data_type in ['supporting_doc', 'media', 'source']:
            self._add_form_metadata(self.xform, data_type,
                                    self.data_value, self.path)
            self.xform.refresh_from_db()
            self.assertNotEqual(date_modified, self.xform.date_modified)

        # /forms
        view = XFormViewSet.as_view({
            'get': 'retrieve'
        })
        formid = self.xform.pk
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 200)
        data = XFormSerializer(self.xform, context={'request': request}).data
        self.assertEqual(response.data, data)

        # /projects/[pk]/forms
        view = ProjectViewSet.as_view({
            'get': 'forms'
        })
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=self.project.pk)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [data])
    def test_external_export_error(self):
        self._publish_xls_form_to_project()

        data_value = "template 1|http://xls_server"
        self._add_form_metadata(self.xform, "external_export", data_value)

        paths = [
            os.path.join(self.main_directory, "fixtures", "transportation", "instances_w_uuid", s, s + ".xml")
            for s in ["transport_2011-07-25_19-05-36"]
        ]

        self._make_submission(paths[0])
        self.assertEqual(self.response.status_code, 201)

        view = XFormViewSet.as_view({"get": "retrieve"})
        formid = self.xform.pk
        token = "http://xls_server/xls/" + "8e86d4bdfa7f435ab89485aeae4ea6f5"
        data = {"token": token}
        request = self.factory.get("/", data=data, **self.extra)

        # External export
        response = view(request, pk=formid, format="xls")

        self.assertEqual(response.status_code, 400)
        data = json.loads(response.data)
        self.assertTrue(data.get("error").startswith("J2X client could not generate report."))
Exemple #28
0
    def test_public_form_list(self):
        self._publish_xls_form_to_project()
        self.view = XFormViewSet.as_view({
            'get': 'retrieve',
        })
        request = self.factory.get('/', **self.extra)
        response = self.view(request, pk='public')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [])

        # public shared form
        self.xform.shared = True
        self.xform.save()
        response = self.view(request, pk='public')
        self.assertEqual(response.status_code, 200)
        self.form_data['public'] = True
        del self.form_data['date_modified']
        del response.data[0]['date_modified']
        self.assertEqual(response.data, [self.form_data])

        # public shared form data
        self.xform.shared_data = True
        self.xform.shared = False
        self.xform.save()
        response = self.view(request, pk='public')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [])
 def test_publish_xlsform(self):
     view = XFormViewSet.as_view({"post": "create"})
     data = {
         "owner": "http://testserver/api/v1/users/bob",
         "public": False,
         "public_data": False,
         "description": u"transportation_2011_07_25",
         "downloadable": True,
         "allows_sms": False,
         "encrypted": False,
         "sms_id_string": u"transportation_2011_07_25",
         "id_string": u"transportation_2011_07_25",
         "title": u"transportation_2011_07_25",
         "bamboo_dataset": u"",
     }
     path = os.path.join(
         settings.ONADATA_DIR, "apps", "main", "tests", "fixtures", "transportation", "transportation.xls"
     )
     with open(path) as xls_file:
         post_data = {"xls_file": xls_file}
         request = self.factory.post("/", data=post_data, **self.extra)
         response = view(request)
         self.assertEqual(response.status_code, 201)
         xform = self.user.xforms.all()[0]
         data.update({"url": "http://testserver/api/v1/forms/%s" % xform.pk})
         self.assertDictContainsSubset(data, response.data)
         self.assertTrue(OwnerRole.user_has_role(self.user, xform))
         self.assertEquals("owner", response.data["users"][0]["role"])
Exemple #30
0
 def test_publish_xlsform(self):
     view = XFormViewSet.as_view({
         'post': 'create'
     })
     data = {
         'owner': 'http://testserver/api/v1/users/bob',
         'public': False,
         'public_data': False,
         'description': u'transportation_2011_07_25',
         'downloadable': True,
         'allows_sms': False,
         'encrypted': False,
         'sms_id_string': u'transportation_2011_07_25',
         'id_string': u'transportation_2011_07_25',
         'title': u'transportation_2011_07_25',
         'bamboo_dataset': u''
     }
     path = os.path.join(
         settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures",
         "transportation", "transportation.xls")
     with open(path) as xls_file:
         post_data = {'xls_file': xls_file}
         request = self.factory.post('/', data=post_data, **self.extra)
         response = view(request)
         self.assertEqual(response.status_code, 201)
         xform = self.user.xforms.all()[0]
         data.update({
             'url':
             'http://testserver/api/v1/forms/%s' % xform.pk
         })
         self.assertDictContainsSubset(data, response.data)
         self.assertTrue(OwnerRole.user_has_role(self.user, xform))
         self.assertEquals("owner", response.data['users'][0]['role'])
Exemple #31
0
 def test_forms_endpoint_with_metadata(self):
     for data_type in ['supporting_doc', 'media', 'source']:
         self._add_form_metadata(self.xform, data_type,
                                 self.data_value, self.path)
     # /forms
     view = XFormViewSet.as_view({
         'get': 'retrieve'
     })
     formid = self.xform.pk
     request = self.factory.get('/', **self.extra)
     response = view(request, pk=formid)
     self.assertEqual(response.status_code, 200)
     data = XFormSerializer(self.xform, context={'request': request}).data
     self.assertEqual(response.data, data)
Exemple #32
0
    def test_enketo_url_no_account(self):
        self._publish_xls_form_to_project()
        view = XFormViewSet.as_view({
            'get': 'enketo'
        })
        formid = self.xform.pk
        # no tags
        request = self.factory.get('/', **self.extra)
        with HTTMock(enketo_error_mock):
            response = view(request, pk=formid)
            data = {'message': u"Enketo not properly configured."}

            self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
            self.assertEqual(response.data, data)
 def test_publish_invalid_xls_form_no_choices(self):
     view = XFormViewSet.as_view({'post': 'create'})
     path = os.path.join(settings.ONADATA_DIR, "apps", "main", "tests",
                         "fixtures", "transportation",
                         "transportation.no_choices.xls")
     with open(path) as xls_file:
         post_data = {'xls_file': xls_file}
         request = self.factory.post('/', data=post_data, **self.extra)
         response = view(request)
         self.assertEqual(response.status_code, 400)
         error_msg = (
             'There should be a choices sheet in this xlsform. Please '
             'ensure that the choices sheet name is all in small caps.')
         self.assertEqual(response.data.get('text'), error_msg)
    def test_form_format(self):
        self._publish_xls_form_to_project()
        view = XFormViewSet.as_view({'get': 'form'})
        formid = self.xform.pk
        data = {
            "name": "transportation",
            "title": "transportation_2011_07_25",
            "default_language": "default",
            "id_string": "transportation_2011_07_25",
            "type": "survey",
        }
        request = self.factory.get('/', **self.extra)
        # test for unsupported format
        response = view(request, pk=formid, format='csvzip')
        self.assertEqual(response.status_code, 400)

        # test for supported formats
        response = view(request, pk=formid, format='json')
        self.assertEqual(response.status_code, 200)
        self.assertDictContainsSubset(data, response.data)
        response = view(request, pk=formid, format='xml')
        self.assertEqual(response.status_code, 200)
        response_doc = minidom.parseString(response.data)
        response = view(request, pk=formid, format='xls')
        self.assertEqual(response.status_code, 200)

        xml_path = os.path.join(settings.ONADATA_DIR, "apps", "main", "tests",
                                "fixtures", "transportation",
                                "transportation.xml")
        with open(xml_path) as xml_file:
            expected_doc = minidom.parse(xml_file)

        model_node = [
            n
            for n in response_doc.getElementsByTagName("h:head")[0].childNodes
            if n.nodeType == Node.ELEMENT_NODE and n.tagName == "model"
        ][0]

        # check for UUID and remove
        uuid_nodes = [
            node for node in model_node.childNodes
            if node.nodeType == Node.ELEMENT_NODE
            and node.getAttribute("nodeset") == "/transportation/formhub/uuid"
        ]
        self.assertEqual(len(uuid_nodes), 1)
        uuid_node = uuid_nodes[0]
        uuid_node.setAttribute("calculate", "''")

        # check content without UUID
        self.assertEqual(response_doc.toxml(), expected_doc.toxml())
Exemple #35
0
 def test_csv_import_fail_invalid_field_post(self):
     """
     Test that invalid post returns 400 with the error in json response
     """
     self.publish_xls_form()
     view = XFormViewSet.as_view({'post': 'csv_import'})
     csv_import = open(
         os.path.join(settings.ONADATA_DIR, 'libs', 'tests', 'fixtures',
                      'bad.csv'))
     post_data = {'wrong_file_field': csv_import}
     request = self.factory.post('/', data=post_data, **self.extra)
     response = view(request, pk=self.xform.id)
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
     self.assertIsNotNone(response.data.get('error'))
Exemple #36
0
    def test_set_form_bad_key(self):
        self._publish_xls_form_to_project()
        self.xform.save()
        view = XFormViewSet.as_view({
            'patch': 'partial_update'
        })
        data = {'nonExistentField': False}

        request = self.factory.patch('/', data=data, **self.extra)
        response = view(request, pk=self.xform.id)

        self.xform.reload()
        self.assertFalse(self.xform.shared)
        self.assertFalse(response.data['public'])
Exemple #37
0
 def test_publish_invalid_xls_form(self):
     view = XFormViewSet.as_view({
         'post': 'create'
     })
     path = os.path.join(
         settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures",
         "transportation", "transportation.bad_id.xls")
     with open(path) as xls_file:
         post_data = {'xls_file': xls_file}
         request = self.factory.post('/', data=post_data, **self.extra)
         response = view(request)
         self.assertEqual(response.status_code, 400)
         error_msg = '[row : 5] Question or group with no name.'
         self.assertEqual(response.data.get('text'), error_msg)
    def test_set_form_bad_value(self):
        key = 'shared'
        self._publish_xls_form_to_project()
        view = XFormViewSet.as_view({'patch': 'partial_update'})
        data = {'public': 'String'}

        request = self.factory.patch('/', data=data, **self.extra)
        response = view(request, pk=self.xform.id)

        self.xform.reload()
        self.assertFalse(self.xform.__getattribute__(key))
        self.assertEqual(
            response.data,
            {'shared': [u"'String' value must be either True or False."]})
    def test_merged_datasets_retrieve(self):
        """Test retrieving a specific merged dataset"""
        merged_dataset = self._create_merged_dataset(geo=True)
        merged_xform = MergedXForm.objects.get(pk=merged_dataset['id'])

        # make submission to form b
        form_b = merged_xform.xforms.all()[1]
        xml = '<data id="b"><fruit>mango</fruit></data>'
        instance = Instance(xform=form_b, xml=xml)
        instance.save()
        form_b.refresh_from_db()
        form_b.last_submission_time = instance.date_created
        form_b.save()
        view = MergedXFormViewSet.as_view({'get': 'retrieve'})
        request = self.factory.get('/')

        # status_code is 404 when the pk doesn't exist
        response = view(request, pk=(1000 * merged_dataset['id']))
        self.assertEqual(response.status_code, 404)

        # status_code is 404 when: pk exists, user is not authenticated
        response = view(request, pk=merged_dataset['id'])
        self.assertEqual(response.status_code, 404)

        # status_code is 200 when: pk exists, user is authenticated
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=merged_dataset['id'])
        self.assertEqual(response.status_code, 200)

        # data has expected fields
        self.assertIn('id', response.data)
        self.assertIn('title', response.data)
        self.assertIn('xforms', response.data)
        self.assertEqual(response.data['num_of_submissions'], 1)
        self.assertEqual(response.data['last_submission_time'],
                         form_b.last_submission_time.isoformat())

        # merged dataset should be available at api/forms/[pk] endpoint
        request = self.factory.get('/', **self.extra)
        view = XFormViewSet.as_view({'get': 'retrieve'})
        response = view(request, pk=merged_dataset['id'])
        self.assertEqual(response.status_code, 200)
        self.assertEqual(merged_dataset['id'], response.data['formid'])
        self.assertIn('is_merged_dataset', response.data)
        self.assertTrue(response.data['is_merged_dataset'])
        self.assertTrue(response.data['instances_with_geopoints'])
        self.assertEqual(response.data['num_of_submissions'], 1)
        self.assertEqual(response.data['last_submission_time'],
                         form_b.last_submission_time.isoformat())
Exemple #40
0
 def test_publish_invalid_xls_form_no_choices(self):
     view = XFormViewSet.as_view({'post': 'create'})
     path = os.path.join(settings.ONADATA_DIR, "apps", "main", "tests",
                         "fixtures", "transportation",
                         "transportation.no_choices.xls")
     with open(path) as xls_file:
         post_data = {'xls_file': xls_file}
         request = self.factory.post('/', data=post_data, **self.extra)
         response = view(request)
         self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
         error_msg = ("There should be a choices sheet in this xlsform. "
                      "Please ensure that the choices sheet name is all in "
                      "small caps and has columns 'list name', 'name', "
                      "and 'label' (or aliased column names).")
         self.assertEqual(response.data.get('text'), error_msg)
Exemple #41
0
    def _publish_submit_geojson(self):
        path = os.path.join(settings.PROJECT_ROOT, "apps", "main", "tests",
                            "fixtures", "geolocation", "GeoLocationForm.xlsx")

        self._publish_xls_file_and_set_xform(path)

        view = XFormViewSet.as_view({'post': 'csv_import'})
        csv_import = \
            open(os.path.join(settings.PROJECT_ROOT, 'apps', 'main',
                              'tests', 'fixtures', 'geolocation',
                              'GeoLocationForm_2015_01_15_01_28_45.csv'))
        post_data = {'csv_file': csv_import}
        request = self.factory.post('/', data=post_data, **self.extra)
        response = view(request, pk=self.xform.id)
        self.assertEqual(response.status_code, 200)
Exemple #42
0
    def test_set_form_private(self):
        key = 'shared'
        self.publish_xls_form()
        self.xform.__setattr__(key, True)
        self.xform.save()
        view = XFormViewSet.as_view({'patch': 'partial_update'})
        data = {'public': False}

        self.assertTrue(self.xform.__getattribute__(key))

        request = self.factory.patch('/', data=data, **self.extra)
        response = view(request, pk=self.xform.id)
        self.xform.refresh_from_db()
        self.assertFalse(self.xform.__getattribute__(key))
        self.assertFalse(response.data['public'])
Exemple #43
0
    def test_data_retrieve_instance_osm_format(self):
        self._publish_osm_with_submission()
        formid = self.xform.pk
        dataid = self.xform.instances.latest('date_created').pk
        request = self.factory.get('/')

        # look at the data/[pk]/[dataid].osm endpoint
        view = OsmViewSet.as_view({'get': 'retrieve'})
        response = view(request, pk=formid, dataid=dataid, format='osm')
        self.assertEqual(response.status_code, 200)
        with open(self.combined_osm_path) as f:
            osm = f.read()
            response.render()
            self.assertMultiLineEqual(
                response.content.decode('utf-8').strip(), osm.strip())

            # look at the data/[pk].osm endpoint
            view = OsmViewSet.as_view({'get': 'list'})
            response = view(request, pk=formid, format='osm')
            self.assertEqual(response.status_code, 200)
            response.render()
            self.assertMultiLineEqual(
                response.content.decode('utf-8').strip(), osm.strip())

        # look at the data.osm endpoint
        view = OsmViewSet.as_view({'get': 'list'})
        response = view(request, format='osm')
        self.assertEqual(response.status_code, 301)
        self.assertEqual(response['Location'],
                         'http://testserver/api/v1/osm.json')

        response = view(request, format='json')
        self.assertEqual(response.status_code, 200)
        data = [{
            'url': 'http://testserver/api/v1/osm/{}'.format(self.xform.pk),
            'title': self.xform.title,
            'id_string': self.xform.id_string,
            'user': self.xform.user.username
        }]
        self.assertEqual(response.data, data)

        # look at the forms.json?instances_with_osm=True
        request = self.factory.get('/', {'instances_with_osm': 'True'},
                                   **self.extra)
        view = XFormViewSet.as_view({'get': 'list'})
        response = view(request, format='json')
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.data, [])
    def test_form_list_filter_by_user(self):
        # publish bob's form
        self._publish_xls_form_to_project()

        previous_user = self.user
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)
        self.assertEqual(self.user.username, 'alice')
        self.assertNotEqual(previous_user, self.user)

        ReadOnlyRole.add(self.user, self.xform)
        view = XFormViewSet.as_view({'get': 'retrieve'})
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=self.xform.pk)
        bobs_form_data = response.data

        # publish alice's form
        self._publish_xls_form_to_project()

        request = self.factory.get('/', **self.extra)
        response = self.view(request)
        self.assertNotEqual(response.get('Last-Modified'), None)
        self.assertEqual(response.status_code, 200)
        # should be both bob's and alice's form
        self.assertEqual(sorted(response.data),
                         sorted([bobs_form_data, self.form_data]))

        # apply filter, see only bob's forms
        request = self.factory.get('/', data={'owner': 'bob'}, **self.extra)
        response = self.view(request)
        self.assertNotEqual(response.get('Last-Modified'), None)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [bobs_form_data])

        # apply filter, see only alice's forms
        request = self.factory.get('/', data={'owner': 'alice'}, **self.extra)
        response = self.view(request)
        self.assertNotEqual(response.get('Last-Modified'), None)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [self.form_data])

        # apply filter, see a non existent user
        request = self.factory.get('/', data={'owner': 'noone'}, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.get('Last-Modified'), None)
        self.assertEqual(response.data, [])
Exemple #45
0
    def test_form_list_filter_by_user(self):
        # publish bob's form
        self.publish_xls_form()

        previous_user = self.user
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)
        self.assertEqual(self.user.username, 'alice')
        self.assertNotEqual(previous_user, self.user)

        assign_perm(CAN_VIEW_XFORM, self.user, self.xform)
        view = XFormViewSet.as_view({
            'get': 'retrieve'
        })
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=self.xform.pk)
        bobs_form_data = response.data

        # publish alice's form
        self.publish_xls_form()

        request = self.factory.get('/', **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        # should be both bob's and alice's form

        sorted_response_data = sorted(response.data, key=lambda x: x['formid'])
        self.assertEqual(sorted_response_data,
                         [bobs_form_data, self.form_data])

        # apply filter, see only bob's forms
        request = self.factory.get('/', data={'owner': 'bob'}, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, [bobs_form_data])

        # apply filter, see only alice's forms
        request = self.factory.get('/', data={'owner': 'alice'}, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, [self.form_data])

        # apply filter, see a non existent user
        request = self.factory.get('/', data={'owner': 'noone'}, **self.extra)
        response = self.view(request)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, [])
 def test_publish_select_external_xlsform(self):
     view = XFormViewSet.as_view({'post': 'create'})
     path = os.path.join(settings.PROJECT_ROOT, "apps", "api", "tests",
                         "fixtures", "select_one_external.xlsx")
     with open(path) as xls_file:
         meta_count = MetaData.objects.count()
         post_data = {'xls_file': xls_file}
         request = self.factory.post('/', data=post_data, **self.extra)
         response = view(request)
         self.assertEqual(response.status_code, 201)
         self.assertEqual(meta_count + 1, MetaData.objects.count())
         xform = self.user.xforms.all()[0]
         metadata = xform.metadata_set.all()[0]
         self.assertEqual('itemsets.csv', metadata.data_value)
         self.assertTrue(OwnerRole.user_has_role(self.user, xform))
         self.assertTrue(OwnerRole.user_has_role(self.user, metadata))
         self.assertEquals("owner", response.data['users'][0]['role'])
Exemple #47
0
    def test_can_add_xform_to_other_account(self):
        view = XFormViewSet.as_view({
            'post': 'create'
        })
        data = {
            'owner': 'http://testserver/api/v1/users/bob',
            'public': False,
            'public_data': False,
            'description': u'',
            'downloadable': True,
            'allows_sms': False,
            'encrypted': False,
            'sms_id_string': u'transportation_2011_07_25',
            'id_string': u'transportation_2011_07_25',
            'title': u'transportation_2011_07_25',
            'bamboo_dataset': u''
        }
        path = os.path.join(
            settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures",
            "transportation", "transportation.xls")

        bob = self.user
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)

        with open(path) as xls_file:
            post_data = {'xls_file': xls_file, 'owner': 'bob'}
            request = self.factory.post('/', data=post_data, **self.extra)
            response = view(request)
            self.assertEqual(response.status_code, 403)

        with open(path) as xls_file:
            post_data = {'xls_file': xls_file, 'owner': 'bob'}
            request = self.factory.post('/', data=post_data, **self.extra)
            role.ManagerRole.add(self.user, bob.profile)
            with HTTMock(enketo_url_mock, enketo_preview_url_mock):
                response = view(request)
                self.assertEqual(response.status_code, 201)

                xform = bob.xforms.all()[0]
                data.update({
                    'url':
                    'http://testserver/api/v1/forms/%s' % xform.pk
                })
                self.assertDictContainsSubset(data, response.data)
Exemple #48
0
    def test_owner_role(self):
        self._publish_xls_form_to_project()
        self._make_submissions()
        view = XFormViewSet.as_view({
            'get': 'retrieve',
            'put': 'update',
            'delete': 'destroy'
        })
        data_view = DataViewSet.as_view({'get': 'list'})
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)
        formid = self.xform.pk

        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 404)

        response = data_view(request, pk=formid)
        self.assertEqual(response.status_code, 404)

        role.OwnerRole.add(self.user, self.xform)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 200)

        response = data_view(request, pk=formid)
        self.assertEqual(response.status_code, 200)

        xfs = XFormSerializer(instance=self.xform,
                              context={'request': request})
        data = json.loads(JSONRenderer().render(xfs.data))
        data.update({'public': True, 'description': "Some description"})

        # Remove key:value pairs where the value is None.
        # More info: https://code.djangoproject.com/ticket/30024
        data.pop('enketo_preview_url')
        data.pop('last_submission_time')

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

        request = self.factory.delete('/', **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 204)
    def test_merged_datasets_list(self):
        """Test list endpoint of a merged dataset"""
        view = MergedXFormViewSet.as_view({
            'get': 'list',
        })
        request = self.factory.get('/')

        # Empty list when there are no merged datasets
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.data, list)
        self.assertEqual([], response.data)

        # create a merged dataset
        merged_dataset = self._create_merged_dataset()

        # Empty list for anonymous user
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.data, list)
        self.assertEqual([], response.data)

        # A list containing the merged datasets for user bob
        request = self.factory.get('/', **self.extra)
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.data, list)
        self.assertIn(merged_dataset, response.data)

        # merged dataset included in api/forms endpoint
        request = self.factory.get('/', **self.extra)
        view = XFormViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.data, list)
        self.assertEqual(len(response.data), 3)
        self.assertIn(merged_dataset['id'],
                      [d['formid'] for d in response.data])
        data = [
            _ for _ in response.data if _['formid'] == merged_dataset['id']
        ][0]
        self.assertIn('is_merged_dataset', data)
        self.assertTrue(data['is_merged_dataset'])
    def test_partial_update_anon(self):
        self._publish_xls_form_to_project()
        view = XFormViewSet.as_view({'patch': 'partial_update'})
        title = u'مرحب'
        description = 'DESCRIPTION'
        username = '******'
        error_msg = 'User with username %s does not exist.' % username
        data = {
            'public': True,
            'description': description,
            'title': title,
            'downloadable': True,
            'owner': username
        }

        self.assertFalse(self.xform.shared)

        request = self.factory.patch('/', data=data, **self.extra)
        response = view(request, pk=self.xform.id)
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data.get('message'), error_msg)
    def test_update_xform_using_put(self):
        self._publish_xls_form_to_project()
        form_id = self.xform.pk

        version = self.xform.version
        view = XFormViewSet.as_view({
            'put': 'update',
        })

        post_data = {
            'uuid':
            'ae631e898bd34ced91d2a309d8b72das',
            'description':
            'Transport form',
            'downloadable':
            False,
            'owner':
            'http://testserver/api/v1/users/{0}'.format(self.user),
            'created_by':
            'http://testserver/api/v1/users/{0}'.format(self.user),
            'public':
            False,
            'public_data':
            False,
            'project':
            'http://testserver/api/v1/projects/{0}'.format(
                self.xform.project.pk),
            'title':
            'Transport Form'
        }
        request = self.factory.put('/', data=post_data, **self.extra)
        response = view(request, pk=form_id)
        self.assertEqual(response.status_code, 200, response.data)

        self.xform.reload()

        self.assertEquals(version, self.xform.version)
        self.assertEquals(self.xform.description, u'Transport form')
        self.assertEquals(self.xform.title, u'Transport Form')
        self.assertEquals(form_id, self.xform.pk)
    def test_form_share_endpoint(self):
        self._publish_xls_form_to_project()
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        alice_profile = self._create_user_profile(alice_data)

        view = XFormViewSet.as_view({'post': 'share'})
        formid = self.xform.pk

        ROLES = [
            ReadOnlyRole, DataEntryRole, EditorRole, ManagerRole, OwnerRole
        ]
        for role_class in ROLES:
            self.assertFalse(
                role_class.user_has_role(alice_profile.user, self.xform))

            data = {'username': '******', 'role': role_class.name}
            request = self.factory.post('/', data=data, **self.extra)
            response = view(request, pk=formid)

            self.assertEqual(response.status_code, 204)
            self.assertTrue(
                role_class.user_has_role(alice_profile.user, self.xform))
    def test_update_xform_using_put_without_required_field(self):
        self._publish_xls_form_to_project()
        form_id = self.xform.pk

        view = XFormViewSet.as_view({
            'put': 'update',
        })

        post_data = {
            'uuid': 'ae631e898bd34ced91d2a309d8b72das',
            'description': 'Transport form',
            'downloadable': False,
            'owner': 'http://testserver/api/v1/users/{0}'.format(self.user),
            'public': False,
            'public_data': False,
        }
        request = self.factory.put('/', data=post_data, **self.extra)
        response = view(request, pk=form_id)

        self.assertEqual(response.status_code, 400)
        self.assertEquals(response.data,
                          {'title': [u'This field is required.']})
Exemple #54
0
    def test_editor_role(self):
        self._publish_xls_form_to_project()
        self._make_submissions()
        view = XFormViewSet.as_view({'get': 'retrieve', 'patch': 'update'})
        data_view = DataViewSet.as_view({'get': 'list'})
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)
        formid = self.xform.pk

        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 404)

        response = data_view(request, pk=formid)
        self.assertEqual(response.status_code, 404)

        role.EditorRole.add(self.user, self.xform)

        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 200)
        response = data_view(request, pk=formid)
        self.assertEqual(response.status_code, 200)
    def test_form_inherits_permision_from_project(self):
        self._publish_xls_form_to_project()
        self._make_submissions()

        project_view = ProjectViewSet.as_view({
            'get': 'retrieve'
        })

        xform_view = XFormViewSet.as_view({
            'get': 'retrieve'
        })

        data_view = DataViewSet.as_view({'get': 'list'})

        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)

        formid = self.xform.pk
        project_id = self.project.pk

        request = self.factory.get('/', **self.extra)
        response = xform_view(request, pk=formid)
        self.assertEqual(response.status_code, 404)

        response = data_view(request, pk=formid)
        self.assertEqual(response.status_code, 404)

        # Give owner role to the project
        role.OwnerRole.add(self.user, self.project)

        response = project_view(request, pk=project_id)
        self.assertEqual(response.status_code, 200)

        response = xform_view(request, pk=formid)
        self.assertEqual(response.status_code, 200)

        response = data_view(request, pk=formid)
        self.assertEqual(response.status_code, 200)
Exemple #56
0
    def test_csv_export_form_w_notes(self):
        self._add_notes_to_data_point()
        self._add_notes_to_data_point()

        time = datetime(2016, 7, 1)
        for instance in self.xform.instances.all():
            instance.date_created = time
            instance.save()
            instance.parsed_instance.save()

        view = XFormViewSet.as_view({'get': 'retrieve'})

        request = self.factory.get('/', **self.extra)
        response = view(request, pk=self.xform.pk, format='csv')

        content = get_response_content(response)

        test_file_path = os.path.join(settings.PROJECT_ROOT, 'apps', 'viewer',
                                      'tests', 'fixtures',
                                      'transportation_w_notes.csv')

        with open(test_file_path, 'r') as test_file:
            self.assertEqual(content, test_file.read())
Exemple #57
0
    def test_submission_to_require_auth_anon(self):
        """
        test submission to a private form by non-owner without perm is
        forbidden.
        """
        view = XFormViewSet.as_view({
            'patch': 'partial_update'
        })
        data = {'require_auth': True}
        self.assertFalse(self.xform.require_auth)
        request = self.factory.patch('/', data=data, **{
            'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token})
        view(request, pk=self.xform.id)
        self.xform.reload()
        self.assertTrue(self.xform.require_auth)

        xml_submission_file_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "../fixtures/tutorial/instances/tutorial_2012-06-27_11-27-53.xml"
        )

        self._make_submission(xml_submission_file_path)
        self.assertEqual(self.response.status_code, 403)
Exemple #58
0
    def test_partial_update(self):
        self._publish_xls_form_to_project()
        view = XFormViewSet.as_view({
            'patch': 'partial_update'
        })
        title = u'مرحب'
        description = 'DESCRIPTION'
        data = {'public': True, 'description': description, 'title': title}

        self.assertFalse(self.xform.shared)

        request = self.factory.patch('/', data=data, **self.extra)
        response = view(request, pk=self.xform.id)

        self.xform.reload()
        self.assertTrue(self.xform.shared)
        self.assertEqual(self.xform.description, description)
        self.assertEqual(response.data['public'], True)
        self.assertEqual(response.data['description'], description)
        self.assertEqual(response.data['title'], title)
        matches = re.findall(r"<h:title>([^<]+)</h:title>", self.xform.xml)
        self.assertTrue(len(matches) > 0)
        self.assertEqual(matches[0], title)
Exemple #59
0
    def test_forms_endpoint_with_metadata(self):
        date_modified = self.xform.date_modified
        for data_type in ['supporting_doc', 'media', 'source']:
            self._add_form_metadata(self.xform, data_type, self.data_value,
                                    self.path)
            self.xform.refresh_from_db()
            self.assertNotEqual(date_modified, self.xform.date_modified)

        # /forms
        view = XFormViewSet.as_view({'get': 'retrieve'})
        formid = self.xform.pk
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 200)
        data = XFormSerializer(self.xform, context={'request': request}).data
        self.assertEqual(response.data, data)

        # /projects/[pk]/forms
        view = ProjectViewSet.as_view({'get': 'forms'})
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=self.project.pk)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [data])
    def test_merged_datasets_deleted_parent_retrieve(self):
        """Test retrieving a specific merged dataset when the parent is deleted
        """
        merged_dataset = self._create_merged_dataset(geo=True)
        merged_xform = MergedXForm.objects.get(pk=merged_dataset['id'])

        # make submission to form b
        form_b = merged_xform.xforms.all()[1]
        xml = '<data id="b"><fruit>mango</fruit></data>'
        instance = Instance(xform=form_b, xml=xml)
        instance.save()
        form_b.refresh_from_db()
        form_b.last_submission_time = instance.date_created
        form_b.save()
        view = MergedXFormViewSet.as_view({'get': 'retrieve'})

        # status_code is 200 when: pk exists, user is authenticated

        request = self.factory.get('/', **self.extra)
        response = view(request, pk=merged_dataset['id'])
        self.assertEqual(response.status_code, 200)

        # delete parents
        [parent.delete() for parent in merged_xform.xforms.all()]
        merged_xform.refresh_from_db()

        # merged dataset should be available at api/forms/[pk] endpoint
        request = self.factory.get('/', **self.extra)
        view = XFormViewSet.as_view({'get': 'retrieve'})
        response = view(request, pk=merged_dataset['id'])
        self.assertEqual(response.status_code, 200)
        self.assertEqual(merged_dataset['id'], response.data['formid'])
        self.assertTrue(response.data['is_merged_dataset'])
        self.assertTrue(response.data['instances_with_geopoints'])
        # deleted parents, 0 submissions
        self.assertEqual(response.data['num_of_submissions'], 0)