From d6be66c110cb59a5664646a46d0398385874994f Mon Sep 17 00:00:00 2001 From: Davis Raymond Muro Date: Fri, 20 Aug 2021 16:31:00 +0300 Subject: [PATCH] Add test to ensure the namespace tag is removed correctly --- .../tests/viewsets/test_briefcase_viewset.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/onadata/apps/api/tests/viewsets/test_briefcase_viewset.py b/onadata/apps/api/tests/viewsets/test_briefcase_viewset.py index 8f6734264b..dcea1abf7e 100644 --- a/onadata/apps/api/tests/viewsets/test_briefcase_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_briefcase_viewset.py @@ -6,6 +6,7 @@ from django.core.files.storage import get_storage_class from django.urls import reverse from django.utils import timezone +from django.test import override_settings from django_digest.test import DigestAuth from rest_framework.test import APIRequestFactory @@ -399,6 +400,61 @@ def test_form_export_with_no_xlsform_returns_200(self): response = self.view(request, pk=xform.pk, format='xls') self.assertEqual(response.status_code, 404) + @mock.patch.object(BriefcaseViewset, 'get_object') + def test_view_downloadSubmission_no_xmlns(self, mock_get_object): + view = BriefcaseViewset.as_view({'get': 'retrieve'}) + self._publish_xml_form() + self.maxDiff = None + self._submit_transport_instance_w_attachment() + instanceId = u'5b2cc313-fc09-437e-8149-fcd32f695d41' + instance = Instance.objects.get(uuid=instanceId) + instance.xml = u'nonenoneuuid:5b2cc313-fc09-437e-8149-fcd32f695d41\n' # noqa + mock_get_object.return_value = instance + formId = u'%(formId)s[@version=null and @uiVersion=null]/' \ + u'%(formId)s[@key=uuid:%(instanceId)s]' % { + 'formId': self.xform.id_string, + 'instanceId': instanceId} + params = {'formId': formId} + auth = DigestAuth(self.login_username, self.login_password) + request = self.factory.get( + self._download_submission_url, data=params) + response = view(request, username=self.user.username) + self.assertEqual(response.status_code, 401) + request.META.update(auth(request.META, response)) + response = view(request, username=self.user.username) + text = "uuid:%s" % instanceId + download_submission_path = os.path.join( + self.main_directory, 'fixtures', 'transportation', + 'view', 'downloadSubmission.xml') + with codecs.open(download_submission_path, encoding='utf-8') as f: + text = f.read() + for var in ((u'{{submissionDate}}', + instance.date_created.isoformat()), + (u'{{form_id}}', str(self.xform.id))): + text = text.replace(*var) + self.assertNotIn( + 'transportation id="transportation_2011_07_25"' + ' instanceID="uuid:5b2cc313-fc09-437e-8149-fcd32f695d41"' + f' submissionDate="{ instance.date_created.isoformat() }" ' + 'xlmns="http://opendatakit.org/submission"', + text) + self.assertContains(response, instanceId, status_code=200) + + with override_settings(SUPPORT_BRIEFCASE_SUBMISSION_DATE=False): + request = self.factory.get( + self._download_submission_url, data=params) + response = view(request, username=self.user.username) + self.assertEqual(response.status_code, 401) + request.META.update(auth(request.META, response)) + response = view(request, username=self.user.username) + response.render() + self.assertIn( + 'transportation id="transportation_2011_07_25"' + ' instanceID="uuid:5b2cc313-fc09-437e-8149-fcd32f695d41"' + f' submissionDate="{ instance.date_created.isoformat() }" ' + 'xlmns="http://opendatakit.org/submission"', + response.content.decode('utf-8')) + @mock.patch.object(BriefcaseViewset, 'get_object') def test_view_downloadSubmission_multiple_nodes(self, mock_get_object): view = BriefcaseViewset.as_view({'get': 'retrieve'})