66from celery_once import AlreadyQueued
77from django .conf import settings
88from django .db import transaction
9- from django .http import StreamingHttpResponse
109from mock import patch , Mock , ANY , PropertyMock
1110from mock .mock import call
1211from rest_framework .exceptions import ErrorDetail
@@ -1004,11 +1003,10 @@ def test_get_204_version(self, s3_has_path_mock):
10041003 self .assertEqual (response .status_code , 204 )
10051004 s3_has_path_mock .assert_called_once_with ("users/username/username_source1_v1." )
10061005
1007- @patch ('core.services.storages.cloud.aws.S3.get_streaming_response ' )
1006+ @patch ('core.services.storages.cloud.aws.S3.url_for ' )
10081007 @patch ('core.services.storages.cloud.aws.S3.exists' )
1009- def test_get_200_head (self , s3_exists_mock , s3_streaming_response ):
1010- response = StreamingHttpResponse ()
1011- s3_streaming_response .return_value = response
1008+ def test_get_302_head (self , s3_exists_mock , s3_url_for_mock ):
1009+ s3_url_for_mock .return_value = 'https://signed.example/head.zip'
10121010 s3_exists_mock .return_value = True
10131011
10141012 response = self .client .get (
@@ -1017,15 +1015,16 @@ def test_get_200_head(self, s3_exists_mock, s3_streaming_response):
10171015 format = 'json'
10181016 )
10191017
1020- self .assertEqual (response .status_code , 200 )
1018+ self .assertEqual (response .status_code , 302 )
1019+ self .assertEqual (response ['Location' ], 'https://signed.example/head.zip' )
10211020 s3_exists_mock .assert_called_once_with (f"users/username/username_source1_vHEAD.{ self .HEAD_updated_at } .zip" )
1021+ s3_url_for_mock .assert_called_once_with (f"users/username/username_source1_vHEAD.{ self .HEAD_updated_at } .zip" )
10221022
1023- @patch ('core.services.storages.cloud.aws.S3.get_streaming_response ' )
1023+ @patch ('core.services.storages.cloud.aws.S3.url_for ' )
10241024 @patch ('core.services.storages.cloud.aws.S3.get_last_key_from_path' )
10251025 @patch ('core.services.storages.cloud.aws.S3.has_path' )
1026- def test_get_200_version (self , s3_has_path_mock , s3_get_last_key_from_path_mock , s3_streaming_response ):
1027- response = StreamingHttpResponse ()
1028- s3_streaming_response .return_value = response
1026+ def test_get_302_version (self , s3_has_path_mock , s3_get_last_key_from_path_mock , s3_url_for_mock ):
1027+ s3_url_for_mock .return_value = 'https://signed.example/v1.zip'
10291028 s3_has_path_mock .return_value = True
10301029 s3_get_last_key_from_path_mock .return_value = f'users/username/username_source1_v1.{ self .v1_updated_at } .zip'
10311030
@@ -1035,10 +1034,28 @@ def test_get_200_version(self, s3_has_path_mock, s3_get_last_key_from_path_mock,
10351034 format = 'json'
10361035 )
10371036
1038- self .assertEqual (response .status_code , 200 )
1039- self .assertEqual (response , response )
1037+ self .assertEqual (response .status_code , 302 )
1038+ self .assertEqual (response [ 'Location' ], 'https://signed.example/v1.zip' )
10401039 s3_has_path_mock .assert_called_once_with ("users/username/username_source1_v1." )
10411040 s3_get_last_key_from_path_mock .assert_called_once_with ("users/username/username_source1_v1." )
1041+ s3_url_for_mock .assert_called_once_with (f'users/username/username_source1_v1.{ self .v1_updated_at } .zip' )
1042+
1043+ @patch ('core.services.storages.cloud.aws.S3.url_for' )
1044+ @patch ('core.services.storages.cloud.aws.S3.exists' )
1045+ def test_get_500_head_when_signed_url_generation_fails (self , s3_exists_mock , s3_url_for_mock ):
1046+ s3_exists_mock .return_value = True
1047+ s3_url_for_mock .return_value = None
1048+
1049+ response = self .client .get (
1050+ self .source .uri + 'HEAD/export/' ,
1051+ HTTP_AUTHORIZATION = 'Token ' + self .admin_token ,
1052+ format = 'json'
1053+ )
1054+
1055+ self .assertEqual (response .status_code , 500 )
1056+ self .assertEqual (response .data , {'detail' : 'Export exists but could not generate a download URL.' })
1057+ s3_exists_mock .assert_called_once_with (f"users/username/username_source1_vHEAD.{ self .HEAD_updated_at } .zip" )
1058+ s3_url_for_mock .assert_called_once_with (f"users/username/username_source1_vHEAD.{ self .HEAD_updated_at } .zip" )
10421059
10431060 @patch ('core.sources.models.Source.is_exporting' , new_callable = PropertyMock )
10441061 @patch ('core.services.storages.cloud.aws.S3.exists' )
0 commit comments