77import requests
88from django .core .files .storage import FileSystemStorage
99from django .test import TestCase
10- from django_s3_storage .storage import S3Storage
1110from mock import MagicMock
1211
1312from ..base import StudioTestCase
1413from contentcuration .models import generate_object_storage_name
15- from contentcuration .utils .storage .common import _get_gcs_presigned_put_url
1614from contentcuration .utils .storage .common import determine_content_type
1715from contentcuration .utils .storage .common import get_presigned_upload_url
1816from contentcuration .utils .storage .common import UnknownStorageBackendError
19- # The modules we'll test
17+ from contentcuration .utils .storage .dev import Storage as DevStorage
18+ from contentcuration .utils .storage .gcs import GoogleCloudStorage
2019
2120
2221class MimeTypesTestCase (TestCase ):
@@ -77,7 +76,10 @@ def test_raises_error(self):
7776 """
7877 with pytest .raises (UnknownStorageBackendError ):
7978 get_presigned_upload_url (
80- "nice" , "err" , 5 , 0 , storage = self .STORAGE ,
79+ "nice" ,
80+ "err" ,
81+ 5 ,
82+ storage = self .STORAGE ,
8183 )
8284
8385
@@ -90,7 +92,9 @@ class GoogleCloudStoragePresignedURLUnitTestCase(TestCase):
9092 """
9193
9294 def setUp (self ):
95+ super ().setUp ()
9396 self .client = MagicMock ()
97+ self .storage = GoogleCloudStorage (self .client , "fake" )
9498 self .generate_signed_url_method = (
9599 self .client .get_bucket .return_value .blob .return_value .generate_signed_url
96100 )
@@ -102,19 +106,15 @@ def test_that_generate_signed_url_is_called(self):
102106 """
103107 Check that we even call blob.generate_signed_url in the first place.
104108 """
105- bucket = "fake"
106- _get_gcs_presigned_put_url (self .client , bucket , "/object.jpg" , "aBc" , 0 , 0 )
109+ get_presigned_upload_url ("/object.jpg" , "aBc" , 0 , storage = self .storage )
107110 self .generate_signed_url_method .assert_called_once ()
108111
109112 def test_that_we_return_a_string (self ):
110113 """
111114 Check that _get_gcs_presigned_put_url returns a string.
112115 """
113- bucket = "fake"
114- ret = _get_gcs_presigned_put_url (
115- self .client , bucket , "/object.jpg" , "aBc" , 0 , 0
116- )
117- assert isinstance (ret , str )
116+ ret = get_presigned_upload_url ("/object.jpg" , "aBc" , 0 , storage = self .storage )
117+ assert isinstance (ret ["uploadURL" ], str )
118118
119119 def test_generate_signed_url_called_with_required_arguments (self ):
120120 """
@@ -132,11 +132,9 @@ def test_generate_signed_url_called_with_required_arguments(self):
132132 bucket_name = "fake"
133133 filepath = "object.jpg"
134134 lifetime = 20 # seconds
135- mimetype = "doesntmatter "
135+ mimetype = "image/jpeg "
136136
137- _get_gcs_presigned_put_url (
138- self .client , bucket_name , filepath , content_md5 , lifetime , mimetype
139- )
137+ get_presigned_upload_url (filepath , content_md5 , lifetime , storage = self .storage )
140138
141139 # assert that we're creating the right object
142140 self .client .get_bucket .assert_called_once_with (bucket_name )
@@ -148,8 +146,8 @@ def test_generate_signed_url_called_with_required_arguments(self):
148146 self .generate_signed_url_method .assert_called_once_with (
149147 method = method ,
150148 content_md5 = content_md5 ,
151- expiration = lifetime_timedelta ,
152149 content_type = mimetype ,
150+ expiration = lifetime_timedelta ,
153151 )
154152
155153
@@ -158,11 +156,9 @@ class S3StoragePresignedURLUnitTestCase(StudioTestCase):
158156 Test cases for generating presigned URLs for S3 storage, i.e. Minio.
159157 """
160158
161- STORAGE = S3Storage ()
162-
163159 def setUp (self ):
164- self .client = MagicMock ()
165160 super ().setUp ()
161+ self .storage = DevStorage ()
166162
167163 def test_returns_string_if_inputs_are_valid (self ):
168164 """
@@ -171,9 +167,7 @@ def test_returns_string_if_inputs_are_valid(self):
171167 """
172168
173169 # use a real connection here as a sanity check
174- ret = get_presigned_upload_url (
175- "a/b/abc.jpg" , "aBc" , 10 , 1 , storage = self .STORAGE , client = None
176- )
170+ ret = get_presigned_upload_url ("a/b/abc.jpg" , "aBc" , 10 , storage = self .storage )
177171 url = ret ["uploadURL" ]
178172
179173 assert isinstance (url , str )
@@ -187,12 +181,14 @@ def test_can_upload_file_to_presigned_url(self):
187181 # S3 expects a base64-encoded MD5 checksum
188182 md5 = hashlib .md5 (file_contents )
189183 md5_checksum = md5 .hexdigest ()
190- md5_checksum_base64 = codecs .encode (codecs .decode (md5_checksum , "hex" ), "base64" ).decode ()
184+ md5_checksum_base64 = codecs .encode (
185+ codecs .decode (md5_checksum , "hex" ), "base64"
186+ ).decode ()
191187
192188 filename = "blahfile.jpg"
193189 filepath = generate_object_storage_name (md5_checksum , filename )
194190
195- ret = get_presigned_upload_url (filepath , md5_checksum_base64 , 1000 , len ( file_contents ) )
191+ ret = get_presigned_upload_url (filepath , md5_checksum_base64 , 1000 )
196192 url = ret ["uploadURL" ]
197193 content_type = ret ["mimetype" ]
198194
@@ -201,6 +197,6 @@ def test_can_upload_file_to_presigned_url(self):
201197 data = file ,
202198 headers = {
203199 "Content-Type" : content_type ,
204- }
200+ },
205201 )
206202 resp .raise_for_status ()
0 commit comments