Example #1
0
    def test_retrieve_xform_media_linked_xform(self):
        data_type = 'media'
        data_value = 'xform {} transportation'.format(self.xform.pk)
        self._add_form_metadata(self.xform, data_type, data_value)

        self.view = XFormListViewSet.as_view({
            "get": "manifest"
        })
        request = self.factory.head('/')
        response = self.view(request, pk=self.xform.pk)
        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        response = self.view(request, pk=self.xform.pk)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data[0]['filename'], 'transportation.csv')

        self.view = XFormListViewSet.as_view({
            "get": "media"
        })
        request = self.factory.head('/')
        response = self.view(request, pk=self.xform.pk,
                             metadata=self.metadata.pk, format='csv')
        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        response = self.view(request, pk=self.xform.pk,
                             metadata=self.metadata.pk, format='csv')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Disposition'],
                         'attachment; filename=transportation.csv')
Example #2
0
    def test_retrieve_xform_media_linked_xform(self):
        data_type = 'media'
        data_value = 'xform {} transportation'.format(self.xform.pk)
        self._add_form_metadata(self.xform, data_type, data_value)
        self._make_submissions()
        self.xform.refresh_from_db()

        self.view = XFormListViewSet.as_view(
            {
                "get": "manifest",
                "head": "manifest"
            }
        )
        request = self.factory.head('/')
        response = self.view(request, pk=self.xform.pk)
        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        response = self.view(request, pk=self.xform.pk)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data[0]['filename'], 'transportation.csv')
        self.assertEqual(
            response.data[0]['hash'], 'md5:%s' % md5(
                self.xform.last_submission_time.isoformat().encode(
                    'utf-8')).hexdigest())

        self.view = XFormListViewSet.as_view(
            {
                "get": "media",
                "head": "media"
            }
        )
        request = self.factory.get('/')
        response = self.view(
            request, pk=self.xform.pk, metadata=self.metadata.pk, format='csv')
        self.assertEqual(response.status_code, 401)

        request = self.factory.head('/')
        response = self.view(
            request, pk=self.xform.pk, metadata=self.metadata.pk, format='csv')
        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        response = self.view(
            request, pk=self.xform.pk, metadata=self.metadata.pk, format='csv')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Disposition'],
                         'attachment; filename=transportation.csv')
Example #3
0
    def test_retrieve_xform_media_linked_xform(self):
        data_type = 'media'
        data_value = 'xform {} transportation'.format(self.xform.pk)
        self._add_form_metadata(self.xform, data_type, data_value)
        self._make_submissions()
        self.xform.refresh_from_db()

        self.view = XFormListViewSet.as_view(
            {
                "get": "manifest",
                "head": "manifest"
            }
        )
        request = self.factory.head('/')
        response = self.view(request, pk=self.xform.pk)
        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        response = self.view(request, pk=self.xform.pk)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data[0]['filename'], 'transportation.csv')
        self.assertEqual(
            response.data[0]['hash'], 'md5:%s' % md5(
                self.xform.last_submission_time.isoformat().encode(
                    'utf-8')).hexdigest())

        self.view = XFormListViewSet.as_view(
            {
                "get": "media",
                "head": "media"
            }
        )
        request = self.factory.get('/')
        response = self.view(
            request, pk=self.xform.pk, metadata=self.metadata.pk, format='csv')
        self.assertEqual(response.status_code, 401)

        request = self.factory.head('/')
        response = self.view(
            request, pk=self.xform.pk, metadata=self.metadata.pk, format='csv')
        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        response = self.view(
            request, pk=self.xform.pk, metadata=self.metadata.pk, format='csv')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Disposition'],
                         'attachment; filename=transportation.csv')
Example #4
0
    def test_get_xform_anonymous_user_xform_require_auth(self):
        self.view = XFormListViewSet.as_view(
            {
                "get": "retrieve",
                "head": "retrieve"
            }
        )
        request = self.factory.head('/')
        response = self.view(request, username='******', pk=self.xform.pk)
        # no authentication prompted
        self.assertEqual(response.status_code, 200)

        self.assertFalse(self.xform.require_auth)
        self.assertFalse(self.user.profile.require_auth)

        self.xform.require_auth = True
        self.xform.save()

        request = self.factory.head('/')
        response = self.view(request, username='******', pk=self.xform.pk)
        # authentication prompted
        self.assertEqual(response.status_code, 401)

        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        response = self.view(request, username='******', pk=self.xform.pk)
        # success with authentication
        self.assertEqual(response.status_code, 200)
    def test_retrieve_xform_xml(self):
        self.view = XFormListViewSet.as_view({
            "get": "retrieve"
        })
        request = self.factory.head('/')
        response = self.view(request, pk=self.xform.pk)
        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        response = self.view(request, pk=self.xform.pk)
        self.assertEqual(response.status_code, 200)

        path = os.path.join(
            os.path.dirname(__file__),
            '..', 'fixtures', 'Transportation Form.xml')

        with open(path) as f:
            form_xml = f.read().strip()
            data = {"form_uuid": self.xform.uuid}
            content = response.render().content.strip()
            content = content.replace(
                self.xform.version, u"20141112071722")
            self.assertEqual(content, form_xml % data)
            self.assertTrue(response.has_header('X-OpenRosa-Version'))
            self.assertTrue(
                response.has_header('X-OpenRosa-Accept-Content-Length'))
            self.assertTrue(response.has_header('Date'))
            self.assertEqual(response['Content-Type'],
                             'text/xml; charset=utf-8')
Example #6
0
    def test_retrieve_xform_manifest(self):
        self._load_metadata(self.xform)
        self.view = XFormListViewSet.as_view(
            {
                "get": "manifest",
                "head": "manifest"
            }
        )
        request = self.factory.head('/')

        response = self.view(request, pk=self.xform.pk)
        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        response = self.view(request, pk=self.xform.pk)
        self.assertEqual(response.status_code, 200)

        manifest_xml = """<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns="http://openrosa.org/xforms/xformsManifest"><mediaFile><filename>screenshot.png</filename><hash>%(hash)s</hash><downloadUrl>http://testserver/bob/xformsMedia/%(xform)s/%(pk)s.png</downloadUrl></mediaFile></manifest>"""  # noqa
        data = {
            "hash": self.metadata.hash,
            "pk": self.metadata.pk,
            "xform": self.xform.pk
        }
        content = response.render().content.decode('utf-8').strip()
        self.assertEqual(content, manifest_xml % data)
        self.assertTrue(response.has_header('X-OpenRosa-Version'))
        self.assertTrue(
            response.has_header('X-OpenRosa-Accept-Content-Length'))
        self.assertTrue(response.has_header('Date'))
        self.assertEqual(response['Content-Type'], 'text/xml; charset=utf-8')
Example #7
0
    def test_retrieve_xform_xml(self):
        self.view = XFormListViewSet.as_view(
            {
                "get": "retrieve",
                "head": "retrieve"
            }
        )
        request = self.factory.head('/')
        response = self.view(request, pk=self.xform.pk)
        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        response = self.view(request, pk=self.xform.pk)
        self.assertEqual(response.status_code, 200)

        path = os.path.join(
            os.path.dirname(__file__), '..', 'fixtures',
            'Transportation Form.xml')

        with open(path, encoding='utf-8') as f:
            form_xml = f.read().strip()
            data = {"form_uuid": self.xform.uuid}
            content = response.render().content.decode('utf-8').strip()
            content = content.replace(self.xform.version, u"20141112071722")
            self.assertEqual(content, form_xml % data)
            self.assertTrue(response.has_header('X-OpenRosa-Version'))
            self.assertTrue(
                response.has_header('X-OpenRosa-Accept-Content-Length'))
            self.assertTrue(response.has_header('Date'))
            self.assertEqual(response['Content-Type'],
                             'text/xml; charset=utf-8')
Example #8
0
    def test_get_xform_anonymous_user_xform_require_auth(self):
        self.view = XFormListViewSet.as_view(
            {
                "get": "retrieve",
                "head": "retrieve"
            }
        )
        request = self.factory.head('/')
        response = self.view(request, username='******', pk=self.xform.pk)
        # no authentication prompted
        self.assertEqual(response.status_code, 200)

        self.assertFalse(self.xform.require_auth)
        self.assertFalse(self.user.profile.require_auth)

        self.xform.require_auth = True
        self.xform.save()

        request = self.factory.head('/')
        response = self.view(request, username='******', pk=self.xform.pk)
        # authentication prompted
        self.assertEqual(response.status_code, 401)

        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        response = self.view(request, username='******', pk=self.xform.pk)
        # success with authentication
        self.assertEqual(response.status_code, 200)
Example #9
0
    def test_retrieve_xform_manifest(self):
        self._load_metadata(self.xform)
        self.view = XFormListViewSet.as_view(
            {
                "get": "manifest",
                "head": "manifest"
            }
        )
        request = self.factory.head('/')

        response = self.view(request, pk=self.xform.pk)
        auth = DigestAuth('bob', 'bobbob')
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        response = self.view(request, pk=self.xform.pk)
        self.assertEqual(response.status_code, 200)

        manifest_xml = """<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns="http://openrosa.org/xforms/xformsManifest"><mediaFile><filename>screenshot.png</filename><hash>%(hash)s</hash><downloadUrl>http://testserver/bob/xformsMedia/%(xform)s/%(pk)s.png</downloadUrl></mediaFile></manifest>"""  # noqa
        data = {
            "hash": self.metadata.hash,
            "pk": self.metadata.pk,
            "xform": self.xform.pk
        }
        content = response.render().content.decode('utf-8').strip()
        self.assertEqual(content, manifest_xml % data)
        self.assertTrue(response.has_header('X-OpenRosa-Version'))
        self.assertTrue(
            response.has_header('X-OpenRosa-Accept-Content-Length'))
        self.assertTrue(response.has_header('Date'))
        self.assertEqual(response['Content-Type'], 'text/xml; charset=utf-8')
    def test_retrieve_xform_manifest_linked_form(self):
        # for linked forms check if manifest media download url for csv
        # has a group_delimiter param
        data_type = 'media'
        data_value = 'xform {} transportation'.format(self.xform.pk)
        media = self._add_form_metadata(self.xform, data_type, data_value)

        self.view = XFormListViewSet.as_view({"get": "manifest"})

        # sign in bob
        request = self.factory.head('/')
        auth_response = self.view(request, pk=self.xform.pk)
        auth = DigestAuth('bob', 'bobbob')

        # set up bob's request
        request = self.factory.get('/xformsManifest')
        request.META.update(auth(request.META, auth_response))

        # make request
        response = self.view(request, pk=self.xform.pk, format='csv')

        # test
        manifest_media_url = "%s?%s=%s" % (media.data['media_url'],
                                           GROUP_DELIMETER_TAG,
                                           ExportBuilder.GROUP_DELIMITER_DOT)
        self.assertEqual(manifest_media_url, response.data[0]['downloadUrl'])
Example #11
0
 def test_retrieve_xform_media_anonymous_user_require_auth(self):
     self.user.profile.require_auth = True
     self.user.profile.save()
     self._load_metadata(self.xform)
     self.view = XFormListViewSet.as_view({"get": "media"})
     request = self.factory.get('/')
     response = self.view(
         request, pk=self.xform.pk, metadata=self.metadata.pk, format='png')
     self.assertEqual(response.status_code, 401)
Example #12
0
 def test_retrieve_xform_media_anonymous_user_require_auth(self):
     self.user.profile.require_auth = True
     self.user.profile.save()
     self._load_metadata(self.xform)
     self.view = XFormListViewSet.as_view({"get": "media"})
     request = self.factory.get('/')
     response = self.view(
         request, pk=self.xform.pk, metadata=self.metadata.pk, format='png')
     self.assertEqual(response.status_code, 401)
 def test_openrosa_form_list(self):
     """Test merged dataset form is not included in /formList"""
     merged_dataset = self._create_merged_dataset()
     merged_xform = XForm.objects.get(pk=merged_dataset['id'])
     view = XFormListViewSet.as_view({"get": "list"})
     request = self.factory.get('/')
     response = view(request, username=self.user.username)
     self.assertEqual(response.status_code, 200)
     self.assertNotIn(merged_xform.id_string,
                      [_['formID'] for _ in response.data])
 def test_openrosa_form_list(self):
     """Test merged dataset form is not included in /formList"""
     merged_dataset = self._create_merged_dataset()
     merged_xform = XForm.objects.get(pk=merged_dataset['id'])
     view = XFormListViewSet.as_view({"get": "list"})
     request = self.factory.get('/')
     response = view(request, username=self.user.username)
     self.assertEqual(response.status_code, 200)
     self.assertNotIn(merged_xform.id_string,
                      [_['formID'] for _ in response.data])
Example #15
0
    def test_retrieve_xform_manifest_linked_form(self):
        # for linked forms check if manifest media download url for csv
        # has a group_delimiter param
        data_type = 'media'
        data_value = 'xform {} transportation'.format(self.xform.pk)
        media = self._add_form_metadata(self.xform, data_type, data_value)

        self.view = XFormListViewSet.as_view(
            {
                "get": "manifest",
                "head": "manifest"
            }
        )

        # sign in bob
        request = self.factory.head('/')
        auth_response = self.view(request, pk=self.xform.pk)
        auth = DigestAuth('bob', 'bobbob')

        # set up bob's request
        request = self.factory.get('/xformsManifest')
        request.META.update(auth(request.META, auth_response))

        # make request
        response = self.view(request, pk=self.xform.pk, format='csv')

        # test
        manifest_media_url = '{}{}'.format(
            media.data['media_url'],
            '?group_delimiter=.&repeat_index_tags=_,_')
        download_url = response.data[0]['downloadUrl']
        self.assertEqual(manifest_media_url, download_url)

        url = '/bob/xformsMedia/{}/{}.csv?group_delimiter=.'\
            .format(self.xform.pk, self.metadata.pk)
        username = '******'
        password = '******'

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

        req = client.get(url)
        self.assertEqual(req.status_code, 200)

        # enable meta perms
        data_value = "editor-minor|dataentry"
        MetaData.xform_meta_permission(self.xform, data_value=data_value)

        req = client.get(url)
        self.assertEqual(req.status_code, 401)
Example #16
0
    def test_retrieve_xform_manifest_linked_form(self):
        # for linked forms check if manifest media download url for csv
        # has a group_delimiter param
        data_type = 'media'
        data_value = 'xform {} transportation'.format(self.xform.pk)
        media = self._add_form_metadata(self.xform, data_type, data_value)

        self.view = XFormListViewSet.as_view(
            {
                "get": "manifest",
                "head": "manifest"
            }
        )

        # sign in bob
        request = self.factory.head('/')
        auth_response = self.view(request, pk=self.xform.pk)
        auth = DigestAuth('bob', 'bobbob')

        # set up bob's request
        request = self.factory.get('/xformsManifest')
        request.META.update(auth(request.META, auth_response))

        # make request
        response = self.view(request, pk=self.xform.pk, format='csv')

        # test
        manifest_media_url = '{}{}'.format(
            media.data['media_url'],
            '?group_delimiter=.&repeat_index_tags=_,_')
        download_url = response.data[0]['downloadUrl']
        self.assertEqual(manifest_media_url, download_url)

        url = '/bob/xformsMedia/{}/{}.csv?group_delimiter=.'\
            .format(self.xform.pk, self.metadata.pk)
        username = '******'
        password = '******'

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

        req = client.get(url)
        self.assertEqual(req.status_code, 200)

        # enable meta perms
        data_value = "editor-minor|dataentry"
        MetaData.xform_meta_permission(self.xform, data_value=data_value)

        req = client.get(url)
        self.assertEqual(req.status_code, 401)
Example #17
0
    def test_retrieve_xform_media_anonymous_user(self):
        self._load_metadata(self.xform)
        self.view = XFormListViewSet.as_view({
            "get": "media"
        })
        request = self.factory.get('/')
        response = self.view(request, pk=self.xform.pk,
                             metadata=self.metadata.pk, format='png')
        self.assertEqual(response.status_code, 401)

        response = self.view(request, pk=self.xform.pk,
                             username=self.user.username,
                             metadata=self.metadata.pk, format='png')
        self.assertEqual(response.status_code, 200)
Example #18
0
 def test_retrieve_xform_media(self):
     self._load_metadata(self.xform)
     self.view = XFormListViewSet.as_view({
         "get": "media"
     })
     request = self.factory.head('/')
     response = self.view(request, pk=self.xform.pk,
                          metadata=self.metadata.pk, format='png')
     auth = DigestAuth('bob', 'bobbob')
     request = self.factory.get('/')
     request.META.update(auth(request.META, response))
     response = self.view(request, pk=self.xform.pk,
                          metadata=self.metadata.pk, format='png')
     self.assertEqual(response.status_code, 200)
Example #19
0
    def test_manifest_url_tag_is_empty_when_no_media(self):
        """
        Test that content contains an empty tag for manifest url
        only when the form has no media
        """
        request = self.factory.get('/')
        view = XFormListViewSet.as_view({"get": "list"})
        response = view(request, username='******', pk=self.xform.pk)
        self.assertEqual(response.status_code, 200)
        content = response.render().content.decode('utf-8')
        manifest_url = ('<manifestUrl></manifestUrl>')
        self.assertTrue(manifest_url in content)

        # Add media and test that manifest url exists
        data_type = 'media'
        data_value = 'xform {} transportation'.format(self.xform.pk)
        self._add_form_metadata(self.xform, data_type, data_value)
        response = view(request, username='******', pk=self.xform.pk)
        self.assertEqual(response.status_code, 200)
        content = response.render().content.decode('utf-8')
        manifest_url = (
            '<manifestUrl>http://testserver/%s/xformsManifest'
            '/%s</manifestUrl>') % (self.user.username, self.xform.id)
        self.assertTrue(manifest_url in content)
Example #20
0
    def test_manifest_url_tag_is_not_present_when_no_media(self):
        """
        Test that content does not contain a manifest url
        only when the form has no media
        """
        request = self.factory.get('/')
        view = XFormListViewSet.as_view({"get": "list"})
        response = view(request, username='******', pk=self.xform.pk)
        self.assertEqual(response.status_code, 200)
        content = response.render().content.decode('utf-8')
        manifest_url = ('<manifestUrl></manifestUrl>')
        self.assertNotIn(manifest_url, content)

        # Add media and test that manifest url exists
        data_type = 'media'
        data_value = 'xform {} transportation'.format(self.xform.pk)
        self._add_form_metadata(self.xform, data_type, data_value)
        response = view(request, username='******', pk=self.xform.pk)
        self.assertEqual(response.status_code, 200)
        content = response.render().content.decode('utf-8')
        manifest_url = (
            '<manifestUrl>http://testserver/%s/xformsManifest'
            '/%s</manifestUrl>') % (self.user.username, self.xform.id)
        self.assertTrue(manifest_url in content)
Example #21
0
 def setUp(self):
     super(self.__class__, self).setUp()
     self.view = XFormListViewSet.as_view({"get": "list"})
     self._publish_xls_form_to_project()
 def setUp(self):
     super(self.__class__, self).setUp()
     self.view = XFormListViewSet.as_view({
         "get": "list"
     })
     self._publish_xls_form_to_project()