Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Sep 4, 2023
1 parent 64fe818 commit 250ca65
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 5 deletions.
107 changes: 106 additions & 1 deletion onadata/apps/api/tests/viewsets/test_briefcase_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def test_view_submission_list_w_xformid(self):
view = BriefcaseViewset.as_view({'get': 'list'})
self._publish_xml_form()
self._make_submissions()
self._submission_list_url = reverse(
'view-submission-list',
kwargs={'xform_pk': self.xform.pk})
request = self.factory.get(
self._submission_list_url,
data={'formId': self.xform.id_string})
Expand All @@ -143,7 +146,37 @@ def test_view_submission_list_w_xformid(self):
request.META.update(auth(request.META, response))
response = view(request, xform_pk=self.xform.pk)
self.assertEqual(response.status_code, 200)
import ipdb; ipdb.set_trace()
submission_list_path = os.path.join(
self.main_directory, 'fixtures', 'transportation',
'view', 'submissionList.xml')
instances = ordered_instances(self.xform)

self.assertEqual(instances.count(), NUM_INSTANCES)

last_index = instances[instances.count() - 1].pk
with codecs.open(submission_list_path, 'rb', encoding='utf-8') as f:
expected_submission_list = f.read()
expected_submission_list = \
expected_submission_list.replace(
'{{resumptionCursor}}', '%s' % last_index)
self.assertContains(response, expected_submission_list)

def test_view_submission_list_w_projectid(self):
view = BriefcaseViewset.as_view({'get': 'list'})
self._publish_xml_form()
self._make_submissions()
self._submission_list_url = reverse(
'view-submission-list',
kwargs={'project_pk': self.xform.project.pk})
request = self.factory.get(
self._submission_list_url,
data={'formId': self.xform.id_string})
response = view(request, project_pk=self.xform.project.pk)
self.assertEqual(response.status_code, 401)
auth = DigestAuth(self.login_username, self.login_password)
request.META.update(auth(request.META, response))
response = view(request, project_pk=self.xform.project.pk)
self.assertEqual(response.status_code, 200)
submission_list_path = os.path.join(
self.main_directory, 'fixtures', 'transportation',
'view', 'submissionList.xml')
Expand Down Expand Up @@ -336,6 +369,78 @@ def test_view_downloadSubmission(self):
self.assertContains(response, instanceId, status_code=200)
self.assertMultiLineEqual(response.content.decode('utf-8'), text)

def test_view_downloadSubmission_w_xformid(self):
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)
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)
self._download_submission_url = reverse(
'view-download-submission',
kwargs={'xform_pk': self.xform.pk})
request = self.factory.get(
self._download_submission_url, data=params)
response = view(request, xform_pk=self.xform.pk)
self.assertEqual(response.status_code, 401)
request.META.update(auth(request.META, response))
response = view(request, xform_pk=self.xform.pk)
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)),
(u'{{media_id}}', str(self.attachment.id))):
text = text.replace(*var)
self.assertContains(response, instanceId, status_code=200)
self.assertMultiLineEqual(response.content.decode('utf-8'), text)

def test_view_downloadSubmission_w_projectid(self):
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)
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)
self._download_submission_url = reverse(
'view-download-submission',
kwargs={'project_pk': self.xform.project.pk})
request = self.factory.get(
self._download_submission_url, data=params)
response = view(request, project_pk=self.xform.project.pk)
self.assertEqual(response.status_code, 401)
request.META.update(auth(request.META, response))
response = view(request, project_pk=self.xform.project.pk)
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)),
(u'{{media_id}}', str(self.attachment.id))):
text = text.replace(*var)
self.assertContains(response, instanceId, status_code=200)
self.assertMultiLineEqual(response.content.decode('utf-8'), text)

def test_view_downloadSubmission_OtherUser(self):
view = BriefcaseViewset.as_view({'get': 'retrieve'})
self._publish_xml_form()
Expand Down
11 changes: 7 additions & 4 deletions onadata/apps/api/viewsets/briefcase_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,10 @@ def get_object(self, queryset=None):
if queryset.first():
username = queryset.first().user.username
elif project_pk:
queryset = queryset.filter(project__pk=project_pk)
queryset = self.queryset.filter(project__pk=project_pk)
if queryset.first():
username = queryset.first().user.username


obj = get_object_or_404(
Instance,
xform__user__username__iexact=username,
Expand All @@ -131,14 +130,18 @@ def filter_queryset(self, queryset):
form_pk = self.kwargs.get("xform_pk")
project_pk = self.kwargs.get("project_pk")

if (not username or not form_pk or not project_pk) and self.request.user.is_anonymous:
if (
not username or not form_pk or not project_pk
) and self.request.user.is_anonymous:
# raises a permission denied exception, forces authentication
self.permission_denied(self.request)

if username is not None and self.request.user.is_anonymous:
profile = None
if username:
profile = get_object_or_404(UserProfile, user__username__iexact=username)
profile = get_object_or_404(
UserProfile, user__username__iexact=username
)
elif form_pk:
queryset = queryset.filter(pk=form_pk)
if queryset.first():
Expand Down

0 comments on commit 250ca65

Please sign in to comment.