Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed Oct 20, 2021
1 parent 918eb6e commit e6a31a1
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 14 deletions.
38 changes: 29 additions & 9 deletions onadata/apps/api/tests/viewsets/test_abstract_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions onadata/apps/api/tests/viewsets/test_briefcase_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
11 changes: 6 additions & 5 deletions onadata/apps/api/viewsets/briefcase_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version='1.0' ?><transportation xmlns="http://opendatakit.org/submission" id="transportation_2011_07_25"><transport><available_transportation_types_to_referral_facility>ambulance bicycle</available_transportation_types_to_referral_facility><loop_over_transport_types_frequency><ambulance><frequency_to_referral_facility>daily</frequency_to_referral_facility></ambulance><bicycle><frequency_to_referral_facility>weekly</frequency_to_referral_facility></bicycle><boat_canoe /><bus /><donkey_mule_cart /><keke_pepe /><lorry /><motorbike /><taxi /><other /></loop_over_transport_types_frequency></transport><meta><instanceID>uuid:f3d8dc65-91a6-4d0f-9e97-802128083390</instanceID></meta></transportation>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version='1.0' ?><transportation xmlns="http://opendatakit.org/submission" id="transportation_2011_07_25"><transport><available_transportation_types_to_referral_facility>none</available_transportation_types_to_referral_facility><loop_over_transport_types_frequency><ambulance /><bicycle /><boat_canoe /><bus /><donkey_mule_cart /><keke_pepe /><lorry /><motorbike /><taxi /><other /></loop_over_transport_types_frequency></transport><meta><instanceID>uuid:5b2cc313-fc09-437e-8149-fcd32f695d41</instanceID></meta></transportation>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version='1.0' ?><transportation xmlns="http://opendatakit.org/submission" id="transportation_2011_07_25"><transport><available_transportation_types_to_referral_facility>ambulance</available_transportation_types_to_referral_facility><loop_over_transport_types_frequency><ambulance><frequency_to_referral_facility>weekly</frequency_to_referral_facility></ambulance><bicycle /><boat_canoe /><bus /><donkey_mule_cart /><keke_pepe /><lorry /><motorbike /><taxi /><other /></loop_over_transport_types_frequency></transport><meta><instanceID>uuid:9c6f3468-cfda-46e8-84c1-75458e72805d</instanceID></meta></transportation>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version='1.0' ?><transportation xmlns="http://opendatakit.org/submission" id="transportation_2011_07_25"><transport><available_transportation_types_to_referral_facility>taxi other</available_transportation_types_to_referral_facility><available_transportation_types_to_referral_facility_other>camel</available_transportation_types_to_referral_facility_other><loop_over_transport_types_frequency><ambulance /><bicycle /><boat_canoe /><bus /><donkey_mule_cart /><keke_pepe /><lorry /><motorbike /><taxi><frequency_to_referral_facility>daily</frequency_to_referral_facility></taxi><other><frequency_to_referral_facility>other</frequency_to_referral_facility></other></loop_over_transport_types_frequency></transport><meta><instanceID>uuid:9f0a1508-c3b7-4c99-be00-9b237c26bcbf</instanceID></meta></transportation>
15 changes: 15 additions & 0 deletions onadata/apps/viewer/tests/test_attachment_url.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"})
Expand Down
6 changes: 6 additions & 0 deletions onadata/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
################################
Expand Down

0 comments on commit e6a31a1

Please sign in to comment.