11import os
22
33from django .conf import settings
4+ from django .contrib .auth import authenticate
5+ from django .http import HttpResponseRedirect
46from django .urls import reverse
7+ from mock import patch
8+ from rest_framework .test import APIRequestFactory
59
610from onadata .apps .logger .models import Attachment
11+ from onadata .apps .logger .views import submission
712from onadata .apps .main .tests .test_base import TestBase
813from 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 ):
0 commit comments