2
2
from unittest import mock
3
3
4
4
import pytest
5
- from b2sdk .exception import B2Error , ServiceError
6
- from b2sdk .v1 import B2Api
5
+ from b2sdk .exception import FileNotPresent
6
+ from b2sdk .file_version import FileVersionInfoFactory
7
+ from b2sdk .v1 import B2Api , Bucket
7
8
from b2sdk .v1 .exception import NonExistentBucket
8
9
from django .core .exceptions import ImproperlyConfigured
9
10
from django_backblaze_b2 import BackblazeB2Storage
10
- from django_backblaze_b2 .b2_filemeta_shim import FileMetaShim
11
- from requests import HTTPError , Response
12
11
13
12
14
13
def test_requiresConfiguration ():
@@ -130,9 +129,15 @@ def test_urlRequiresName(settings):
130
129
131
130
132
131
def test_get_available_nameWithOverwrites (settings ):
132
+ mockedBucket = mock .Mock (spec = Bucket )
133
+ mockedBucket .get_file_info_by_name .return_value = FileVersionInfoFactory .from_response_headers (
134
+ {"id_" : 1 , "file_name" : "some_name.txt" }
135
+ )
136
+
133
137
with mock .patch .object (settings , "BACKBLAZE_CONFIG" , _settingsDict ({})), mock .patch .object (
134
138
B2Api , "authorize_account"
135
- ), mock .patch .object (B2Api , "get_bucket_by_name" ), mock .patch .object (FileMetaShim , "exists" , return_value = True ):
139
+ ), mock .patch .object (B2Api , "get_bucket_by_name" ) as api :
140
+ api .return_value = mockedBucket
136
141
storage = BackblazeB2Storage (opts = {"allowFileOverwrites" : True })
137
142
138
143
availableName = storage .get_available_name ("some_name.txt" , max_length = None )
@@ -157,62 +162,19 @@ def test_notImplementedMethods(settings):
157
162
158
163
159
164
def test_existsFileDoesNotExist (settings ):
160
- fileMetaResponse = mock .Mock (spec = Response )
161
- fileMetaResponse . status_code = 404
165
+ mockedBucket = mock .Mock (spec = Bucket )
166
+ mockedBucket . get_file_info_by_name . side_effect = FileNotPresent ()
162
167
163
168
with mock .patch .object (settings , "BACKBLAZE_CONFIG" , _settingsDict ({})), mock .patch .object (
164
169
B2Api , "authorize_account"
165
- ), mock .patch .object (B2Api , "get_bucket_by_name" ), mock .patch .object (
166
- FileMetaShim , "_get_head_response" , side_effect = HTTPError (response = fileMetaResponse )
167
- ) as patchedHead :
170
+ ), mock .patch .object (B2Api , "get_bucket_by_name" ) as api :
171
+ api .return_value = mockedBucket
168
172
storage = BackblazeB2Storage (opts = {})
169
173
170
174
doesFileExist = storage .exists ("some/file.txt" )
171
175
172
176
assert not doesFileExist
173
- assert patchedHead .call_count == 1
174
-
175
-
176
- def test_existsServerErrorInterpretationAndRetry (settings ):
177
- fileMetaResponse = mock .Mock (spec = Response )
178
- fileMetaResponse .status_code = 500
179
- fileMetaResponse .headers = {}
180
-
181
- with mock .patch .object (settings , "BACKBLAZE_CONFIG" , _settingsDict ({})), mock .patch .object (
182
- B2Api , "authorize_account"
183
- ), mock .patch .object (B2Api , "get_bucket_by_name" ), mock .patch .object (
184
- B2Api , "authorize_automatically" , return_value = True
185
- ), mock .patch .object (
186
- FileMetaShim , "_get_head_response" , side_effect = HTTPError (response = fileMetaResponse )
187
- ) as patchedHead :
188
- storage = BackblazeB2Storage (opts = {})
189
-
190
- with pytest .raises (B2Error ) as raised :
191
- storage .exists ("some/file.txt" )
192
-
193
- assert isinstance (raised .value , ServiceError )
194
- assert patchedHead .call_count == 4
195
-
196
-
197
- def test_existsServerErrorDoesNotRetryIfCantAuth (settings ):
198
- fileMetaResponse = mock .Mock (spec = Response )
199
- fileMetaResponse .status_code = 500
200
- fileMetaResponse .headers = {}
201
-
202
- with mock .patch .object (settings , "BACKBLAZE_CONFIG" , _settingsDict ({})), mock .patch .object (
203
- B2Api , "authorize_account"
204
- ), mock .patch .object (B2Api , "get_bucket_by_name" ), mock .patch .object (
205
- B2Api , "authorize_automatically" , return_value = False
206
- ), mock .patch .object (
207
- FileMetaShim , "_get_head_response" , side_effect = HTTPError (response = fileMetaResponse )
208
- ) as patchedHead :
209
- storage = BackblazeB2Storage (opts = {})
210
-
211
- with pytest .raises (B2Error ) as raised :
212
- storage .exists ("some/file.txt" )
213
-
214
- assert isinstance (raised .value , ServiceError )
215
- assert patchedHead .call_count == 1
177
+ assert mockedBucket .get_file_info_by_name .call_count == 1
216
178
217
179
218
180
def _settingsDict (config : Dict [str , Any ]) -> Dict [str , Any ]:
0 commit comments