From e6a31a1fdf2085f196e457583bea111a8d13b17f Mon Sep 17 00:00:00 2001 From: Olivier Leger Date: Wed, 20 Oct 2021 14:50:47 -0400 Subject: [PATCH] Add unit tests --- .../tests/viewsets/test_abstract_viewset.py | 38 ++++++++++---- .../api/tests/viewsets/test_briefcase_api.py | 50 +++++++++++++++++++ onadata/apps/api/viewsets/briefcase_api.py | 11 ++-- ...ansport_2011-07-25_19-05-36_with_xmlns.xml | 1 + ...ansport_2011-07-25_19-05-49_with_xmlns.xml | 1 + ...ansport_2011-07-25_19-06-01_with_xmlns.xml | 1 + ...ansport_2011-07-25_19-06-14_with_xmlns.xml | 1 + .../apps/viewer/tests/test_attachment_url.py | 15 ++++++ onadata/settings/base.py | 6 +++ 9 files changed, 110 insertions(+), 14 deletions(-) create mode 100755 onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-05-36/transport_2011-07-25_19-05-36_with_xmlns.xml create mode 100755 onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-05-49/transport_2011-07-25_19-05-49_with_xmlns.xml create mode 100755 onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-06-01/transport_2011-07-25_19-06-01_with_xmlns.xml create mode 100755 onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-06-14/transport_2011-07-25_19-06-14_with_xmlns.xml diff --git a/onadata/apps/api/tests/viewsets/test_abstract_viewset.py b/onadata/apps/api/tests/viewsets/test_abstract_viewset.py index ff57d407b..25313eb55 100644 --- a/onadata/apps/api/tests/viewsets/test_abstract_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_abstract_viewset.py @@ -240,18 +240,38 @@ def _make_submissions(self, username=None, add_uuid=False, self.assertEqual(xform.num_of_submissions, post_count) self.assertEqual(xform.user.profile.num_of_submissions, post_count) - def _submit_transport_instance_w_attachment(self, - survey_at=0, - media_file=None): - s = self.surveys[survey_at] + def _submit_transport_instance_w_attachment( + self, survey_at=0, media_file=None, with_namespace=False + ): + survey_datetime = self.surveys[survey_at] if not media_file: media_file = "1335783522563.jpg" - path = os.path.join(self.main_directory, 'fixtures', - 'transportation', 'instances', s, media_file) + path = os.path.join( + self.main_directory, + 'fixtures', + 'transportation', + 'instances', + survey_datetime, + media_file, + ) + with open(path, 'rb') as f: - self._make_submission(os.path.join( - self.main_directory, 'fixtures', - 'transportation', 'instances', s, s + '.xml'), media_file=f) + xml_filename = ( + f'{survey_datetime}_with_xmlns.xml' + if with_namespace + else f'{survey_datetime}.xml' + ) + self._make_submission( + os.path.join( + self.main_directory, + 'fixtures', + 'transportation', + 'instances', + survey_datetime, + xml_filename, + ), + media_file=f, + ) attachment = Attachment.objects.all().reverse()[0] self.attachment = attachment diff --git a/onadata/apps/api/tests/viewsets/test_briefcase_api.py b/onadata/apps/api/tests/viewsets/test_briefcase_api.py index f44704b94..8b9b973a9 100644 --- a/onadata/apps/api/tests/viewsets/test_briefcase_api.py +++ b/onadata/apps/api/tests/viewsets/test_briefcase_api.py @@ -3,6 +3,7 @@ from django.urls import reverse from django.core.files.storage import get_storage_class +from django.test import override_settings from django_digest.test import DigestAuth from rest_framework.test import APIRequestFactory @@ -225,6 +226,53 @@ def test_view_download_submission(self): self.assertContains(response, instance_id, status_code=200) self.assertMultiLineEqual(response.content.decode('utf-8'), text) + def test_view_download_submission_no_xmlns(self): + view = BriefcaseApi.as_view({'get': 'retrieve'}) + self._publish_xml_form() + self.maxDiff = None + self._submit_transport_instance_w_attachment(with_namespace=True) + instance_id = '5b2cc313-fc09-437e-8149-fcd32f695d41' + instance = Instance.objects.get(uuid=instance_id) + form_id = '%(formId)s[@version=null and @uiVersion=null]/' \ + '%(formId)s[@key=uuid:%(instanceId)s]' % { + 'formId': self.xform.id_string, + 'instanceId': instance_id} + params = {'formId': form_id} + 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) + download_submission_path = os.path.join( + self.main_directory, 'fixtures', 'transportation', + 'view', 'downloadSubmission.xml') + response.render() + self.assertContains(response, instance_id, status_code=200) + self.assertNotIn( + 'transportation id="transportation_2011_07_25" ' + 'xlmns="http://opendatakit.org/submission" ' + 'instanceID="uuid:5b2cc313-fc09-437e-8149-fcd32f695d41"' + f' submissionDate="{instance.date_created.isoformat()}" ', + response.content.decode() + ) + + 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) + request.META.update(auth(request.META, response)) + response = view(request, username=self.user.username) + response.render() + self.assertNotIn( + 'transportation id="transportation_2011_07_25" ' + 'xmlns="http://opendatakit.org/submission" ' + 'instanceID="uuid:5b2cc313-fc09-437e-8149-fcd32f695d41" ' + f'submissionDate="{instance.date_created.isoformat()}" ', + response.content.decode() + ) + def test_view_download_submission_other_user(self): view = BriefcaseApi.as_view({'get': 'retrieve'}) self._publish_xml_form() @@ -397,6 +445,8 @@ def test_submission_with_instance_id_on_root_node(self): self.assertContains(response, instanceId, status_code=201) self.assertEqual(Instance.objects.count(), count + 1) + + def tearDown(self): if self.user: delete_user_storage(self.user.username) diff --git a/onadata/apps/api/viewsets/briefcase_api.py b/onadata/apps/api/viewsets/briefcase_api.py index b636ce100..d5ff676c2 100644 --- a/onadata/apps/api/viewsets/briefcase_api.py +++ b/onadata/apps/api/viewsets/briefcase_api.py @@ -228,7 +228,7 @@ def retrieve(self, request, *args, **kwargs): # Added this because of https://github.com/onaio/onadata/pull/2139 # Should bring support to ODK v1.17+ - if getattr(settings, 'SUPPORT_BRIEFCASE_SUBMISSION_DATE', True): + if settings.SUPPORT_BRIEFCASE_SUBMISSION_DATE: # Remove namespace attribute if any try: submission_xml_root_node.removeAttribute('xmlns') @@ -241,10 +241,11 @@ def retrieve(self, request, *args, **kwargs): 'host': request.build_absolute_uri().replace( request.get_full_path(), '') } - return Response(data, - headers=self.get_openrosa_headers(request, - location=False), - template_name='downloadSubmission.xml') + return Response( + data, + headers=self.get_openrosa_headers(request, location=False), + template_name='downloadSubmission.xml', + ) @action(detail=True, methods=['GET']) def manifest(self, request, *args, **kwargs): diff --git a/onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-05-36/transport_2011-07-25_19-05-36_with_xmlns.xml b/onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-05-36/transport_2011-07-25_19-05-36_with_xmlns.xml new file mode 100755 index 000000000..64144cda0 --- /dev/null +++ b/onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-05-36/transport_2011-07-25_19-05-36_with_xmlns.xml @@ -0,0 +1 @@ +ambulance bicycledailyweeklyuuid:f3d8dc65-91a6-4d0f-9e97-802128083390 diff --git a/onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-05-49/transport_2011-07-25_19-05-49_with_xmlns.xml b/onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-05-49/transport_2011-07-25_19-05-49_with_xmlns.xml new file mode 100755 index 000000000..7c764efc4 --- /dev/null +++ b/onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-05-49/transport_2011-07-25_19-05-49_with_xmlns.xml @@ -0,0 +1 @@ +noneuuid:5b2cc313-fc09-437e-8149-fcd32f695d41 diff --git a/onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-06-01/transport_2011-07-25_19-06-01_with_xmlns.xml b/onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-06-01/transport_2011-07-25_19-06-01_with_xmlns.xml new file mode 100755 index 000000000..132609cbf --- /dev/null +++ b/onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-06-01/transport_2011-07-25_19-06-01_with_xmlns.xml @@ -0,0 +1 @@ +ambulanceweeklyuuid:9c6f3468-cfda-46e8-84c1-75458e72805d diff --git a/onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-06-14/transport_2011-07-25_19-06-14_with_xmlns.xml b/onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-06-14/transport_2011-07-25_19-06-14_with_xmlns.xml new file mode 100755 index 000000000..381b9b026 --- /dev/null +++ b/onadata/apps/main/tests/fixtures/transportation/instances/transport_2011-07-25_19-06-14/transport_2011-07-25_19-06-14_with_xmlns.xml @@ -0,0 +1 @@ +taxi othercameldailyotheruuid:9f0a1508-c3b7-4c99-be00-9b237c26bcbf diff --git a/onadata/apps/viewer/tests/test_attachment_url.py b/onadata/apps/viewer/tests/test_attachment_url.py index 0bdea5a1f..51d107012 100644 --- a/onadata/apps/viewer/tests/test_attachment_url.py +++ b/onadata/apps/viewer/tests/test_attachment_url.py @@ -1,5 +1,8 @@ # coding: utf-8 +import requests from django.urls import reverse +from django_digest.test import DigestAuth +from django_digest.test import Client as DigestClient from onadata.apps.main.tests.test_base import TestBase from onadata.apps.logger.models import Attachment @@ -25,6 +28,18 @@ def test_attachment_url(self): self.url, {"media_file": self.attachment_media_file}) self.assertEqual(response.status_code, 200) # nginx is used as proxy + def test_attachment_url_with_digest_auth(self): + self.client.logout() + response = self.client.get( + self.url, {'media_file': self.attachment_media_file} + ) + self.assertEqual(response.status_code, 401) # nginx is used as proxy + self.assertTrue('WWW-Authenticate' in response) + digest_client = DigestClient() + digest_client.set_authorization(self.login_username, self.login_password) + response = digest_client.get(self.url, {'media_file': self.attachment_media_file}) + self.assertEqual(response.status_code, 200) + def test_attachment_not_found(self): response = self.client.get( self.url, {"media_file": "non_existent_attachment.jpg"}) diff --git a/onadata/settings/base.py b/onadata/settings/base.py index 3acd6b9bf..c0170d269 100644 --- a/onadata/settings/base.py +++ b/onadata/settings/base.py @@ -571,6 +571,12 @@ def skip_suspicious_operations(record): # the database is SQLite (e.g.: running unit tests locally). USE_POSTGRESQL = True +# Added this because of https://github.com/onaio/onadata/pull/2139 +# Should bring support to ODK v1.17+ +SUPPORT_BRIEFCASE_SUBMISSION_DATE = ( + os.environ.get('SUPPORT_BRIEFCASE_SUBMISSION_DATE') != 'True' +) + ################################ # Celery settings # ################################