Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Jul 19, 2023
1 parent 719cd82 commit 0c4bdd9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion onadata/apps/logger/tests/test_briefcase_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ def test_view_downloadSubmission(self):
for var in (
("{{submissionDate}}", instance.date_created.isoformat()),
("{{form_id}}", str(self.xform.id)),
("{{attachment_id}}", str(self.attachment.id)),
):
text = text.replace(*var)

self.assertContains(response, instanceId, status_code=200)
self.assertMultiLineEqual(response.content.decode("utf-8"), text)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<mediaFile>
<filename>1335783522563.jpg</filename>
<hash>md5:2ca0d22073a9b6b4ebe51368b08da60c</hash>
<downloadUrl>http://testserver/attachment/original?media_file=bob/attachments/{{form_id}}_transportation_2011_07_25/1335783522563.jpg</downloadUrl>
<downloadUrl>http://testserver/attachment/original?media_file=bob/attachments/{{form_id}}_transportation_2011_07_25/1335783522563.jpg&attachment_id={{attachment_id}}</downloadUrl>
</mediaFile>
</submission>
15 changes: 15 additions & 0 deletions onadata/apps/viewer/tests/test_attachment_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ def test_attachment_has_mimetype(self):
attachment = Attachment.objects.all().reverse()[0]
self.assertEqual(attachment.mimetype, 'image/jpeg')

def test_attachment_url_w_media_id(self):
self.assertEqual(
Attachment.objects.count(), self.attachment_count + 1)
response = self.client.get(
self.url, {"attachment_id": self.attachment.id})
self.assertEqual(response.status_code, 302) # redirects to amazon

def test_attachment_url_w_media_id_no_redirect(self):
self.assertEqual(
Attachment.objects.count(), self.attachment_count + 1)
response = self.client.get(
self.url, {"attachment_id": self.attachment.id,
'no_redirect': 'true'})
self.assertEqual(response.status_code, 200) # no redirects to amazon

def tearDown(self):
path = os.path.join(settings.MEDIA_ROOT, self.user.username)
for root, dirs, files in os.walk(path, topdown=False):
Expand Down
3 changes: 2 additions & 1 deletion onadata/apps/viewer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,8 @@ def attachment_url(request, size="medium"):
media_file = request.GET.get("media_file")
no_redirect = request.GET.get("no_redirect")
attachment_id = request.GET.get("attachment_id")
if not media_file:

if not media_file and not attachment_id:
return HttpResponseNotFound(_("Attachment not found"))
if attachment_id:
attachment = get_object_or_404(Attachment, pk=attachment_id)
Expand Down

0 comments on commit 0c4bdd9

Please sign in to comment.