Skip to content

Commit 0cf521e

Browse files
authored
Merge pull request #2482 from onaio/append-azure-token-to-attachment-blob-url
Ensure sas token is appended to azure blob attachment url
2 parents 2f77359 + 468c0c5 commit 0cf521e

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

onadata/apps/viewer/tests/test_attachment_url.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import os
22

33
from django.conf import settings
4+
from django.contrib.auth import authenticate
5+
from django.http import HttpResponseRedirect
46
from django.urls import reverse
7+
from mock import patch
8+
from rest_framework.test import APIRequestFactory
59

610
from onadata.apps.logger.models import Attachment
11+
from onadata.apps.logger.views import submission
712
from onadata.apps.main.tests.test_base import TestBase
813
from onadata.apps.viewer.views import attachment_url
914

@@ -18,6 +23,8 @@ def setUp(self):
1823
self._submit_transport_instance_w_attachment()
1924
self.url = reverse(
2025
attachment_url, kwargs={'size': 'original'})
26+
self._submission_url = reverse(
27+
'submissions', kwargs={'username': self.user.username})
2128

2229
def test_attachment_url(self):
2330
self.assertEqual(
@@ -61,6 +68,54 @@ def test_attachment_url_w_media_id_no_redirect(self):
6168
'no_redirect': 'true'})
6269
self.assertEqual(response.status_code, 200) # no redirects to amazon
6370

71+
@patch("onadata.apps.viewer.views.generate_media_download_url")
72+
def test_attachment_url_has_azure_sas_token(self, mock_media_url):
73+
"""Test attachment url has azure sas token"""
74+
self._publish_xls_file(
75+
os.path.join(
76+
settings.PROJECT_ROOT,
77+
"apps",
78+
"main",
79+
"tests",
80+
"fixtures",
81+
"transportation",
82+
"transportation_encrypted.xlsx",
83+
)
84+
)
85+
files = {}
86+
for filename in ["submission.xml", "submission.xml.enc"]:
87+
files[filename] = os.path.join(
88+
settings.PROJECT_ROOT,
89+
"apps",
90+
"main",
91+
"tests",
92+
"fixtures",
93+
"transportation",
94+
"instances_encrypted",
95+
filename,
96+
)
97+
with open(files["submission.xml.enc"], "rb") as encryped_file:
98+
with open(files["submission.xml"], "rb") as f:
99+
post_data = {
100+
"xml_submission_file": f,
101+
"submission.xml.enc": encryped_file,
102+
}
103+
self.factory = APIRequestFactory()
104+
request = self.factory.post(self._submission_url, post_data)
105+
request.user = authenticate(username="bob", password="bob")
106+
response = submission(request, username=self.user.username)
107+
self.assertEqual(response.status_code, 201)
108+
109+
# get submission enc attachment
110+
attachment = Attachment.objects.all()[1]
111+
sas_token = "se=ab736fba7261" # nosec
112+
expected_url = f"http://testserver/{attachment.media_file.name}?{sas_token}"
113+
mock_media_url.return_value = HttpResponseRedirect(redirect_to=expected_url)
114+
response = self.client.get(self.url, {"media_file": attachment.media_file.name})
115+
self.assertEqual(response.status_code, 302)
116+
self.assertEqual(response.url, expected_url)
117+
self.assertIn(f"?{sas_token}", str(response.url))
118+
64119
def tearDown(self):
65120
path = os.path.join(settings.MEDIA_ROOT, self.user.username)
66121
for root, dirs, files in os.walk(path, topdown=False):

onadata/apps/viewer/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
str_to_bool,
5757
)
5858
from onadata.libs.utils.google import create_flow
59-
from onadata.libs.utils.image_tools import image_url
59+
from onadata.libs.utils.image_tools import image_url, generate_media_download_url
6060
from onadata.libs.utils.log import Actions, audit_log
6161
from onadata.libs.utils.logger_tools import (
6262
generate_content_disposition_header,
@@ -894,7 +894,7 @@ def attachment_url(request, size="medium"):
894894

895895
return response
896896
if not attachment.mimetype.startswith("image"):
897-
return redirect(attachment.media_file.url)
897+
return generate_media_download_url(attachment)
898898
media_url = image_url(attachment, size)
899899
if media_url:
900900
return redirect(media_url)

0 commit comments

Comments
 (0)