@@ -312,6 +312,8 @@ def setUp(self):
312312 def _setUpS3 (self ):
313313 """setup class"""
314314
315+ djpyfs .S3CONN = None
316+
315317 # Start mocking S3
316318 self .mock_s3 = mock_s3 ()
317319 self .mock_s3 .start ()
@@ -320,19 +322,23 @@ def _setUpS3(self):
320322 self .conn = boto3 .resource ('s3' )
321323 self .conn .create_bucket (Bucket = djpyfs .DJFS_SETTINGS ['bucket' ])
322324
323- def test_aws_credentials (self ):
325+ def test_aws_options (self ):
324326 fs = djpyfs .get_filesystem (self .namespace )
325327 self .assertEqual (fs .aws_access_key_id , 'foo' )
326328 self .assertEqual (fs .aws_secret_access_key , 'bar' )
329+ self .assertEqual (fs .region , djpyfs .DJFS_SETTINGS .get ('region' , None ))
327330
328331 # This test is only relevant for S3. Generate some fake errors to make
329332 # sure we cover the retry code.
330333 def test_get_url_retry (self ):
334+ fs = djpyfs .get_filesystem (self .namespace )
335+ # Call get_url() once to initialise global S3CONN so we can patch its
336+ # generate_presigned_url() method below.
337+ fs .get_url (self .relative_path_to_test_file )
331338 with patch ('boto3.client' ) as mock_client :
332339 mock_client .side_effect = AttributeError ("Some attribute error occurred" )
333340 with patch .object (djpyfs .S3CONN , "generate_presigned_url" ) as mock_client :
334341 mock_client .side_effect = AttributeError ("Some attribute error occurred" )
335- fs = djpyfs .get_filesystem (self .namespace )
336342 with self .assertRaises (AttributeError ):
337343 fs .get_url (self .relative_path_to_test_file ).startswith (self .expected_url_prefix )
338344
@@ -364,3 +370,28 @@ def setUp(self):
364370 f"{ djpyfs .DJFS_SETTINGS ['prefix' ]} /{ self .namespace } /"
365371 f"{ self .relative_path_to_test_file } " )
366372 self ._setUpS3 ()
373+
374+
375+ # pylint: disable=test-inherits-tests
376+ class S3TestRegion (S3Test ):
377+ """
378+ Same as S3Test above, but specifies AWS region.
379+ """
380+
381+ djfs_settings = {
382+ 'type' : 's3fs' ,
383+ 'directory_root' : 'django-pyfs/static/django-pyfs-test' ,
384+ 'url_root' : '/static/django-pyfs-test' ,
385+ 'aws_access_key_id' : 'foo' ,
386+ 'aws_secret_access_key' : 'bar' ,
387+ 'bucket' : 'test_bucket' ,
388+ 'region' : 'me-south-1'
389+ }
390+
391+ def setUp (self ):
392+ super ().setUp ()
393+
394+ self .expected_url_prefix = (f"https://s3.{ djpyfs .DJFS_SETTINGS ['region' ]} .amazonaws.com/"
395+ f"{ djpyfs .DJFS_SETTINGS ['bucket' ]} /{ self .namespace } /"
396+ f"{ self .relative_path_to_test_file } " )
397+ self ._setUpS3 ()
0 commit comments