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 Sep 22, 2023
1 parent d1ecff1 commit 2c6ff43
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions onadata/apps/viewer/tests/test_attachment_url.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import os

from django.conf import settings
from django.contrib.auth import authenticate
from django.http import HttpResponseRedirect
from django.urls import reverse
from mock import patch
from rest_framework.test import APIRequestFactory

from onadata.apps.logger.models import Attachment
from onadata.apps.logger.views import submission
from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.viewer.views import attachment_url

Expand Down Expand Up @@ -61,6 +66,51 @@ def test_attachment_url_w_media_id_no_redirect(self):
'no_redirect': 'true'})
self.assertEqual(response.status_code, 200) # no redirects to amazon

@patch("onadata.apps.viewer.views.generate_media_download_url")
def test_attachment_url_has_azure_sas_token(self, mock_media_url):
self._publish_xls_file(
os.path.join(
settings.PROJECT_ROOT,
"apps",
"main",
"tests",
"fixtures",
"transportation",
"transportation_encrypted.xlsx",
)
)
files = {}
for filename in ["submission.xml", "submission.xml.enc"]:
files[filename] = os.path.join(
settings.PROJECT_ROOT,
"apps",
"main",
"tests",
"fixtures",
"transportation",
"instances_encrypted",
filename,
)
with open(files["submission.xml.enc"], "rb") as encryped_file:
with open(files["submission.xml"], "rb") as f:
post_data = {
"xml_submission_file": f,
"submission.xml.enc": encryped_file,
}
self.factory = APIRequestFactory()
request = self.factory.post(self._submission_url, post_data)
request.user = authenticate(username="bob", password="bob")
response = submission(request, username=self.user.username)
self.assertEqual(response.status_code, 201)
attachment = Attachment.objects.all()[1]
sas_token = "se=ab736fba7261"
expected_url = f"http://testserver/{attachment.media_file.name}?{sas_token}"
mock_media_url.return_value = HttpResponseRedirect(redirect_to=expected_url)
response = self.client.get(self.url, {"media_file": attachment.media_file.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, expected_url)
self.assertIn(f"?{sas_token}", str(response.url))

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

0 comments on commit 2c6ff43

Please sign in to comment.