1- from datetime import datetime , timedelta , tzinfo , timezone
1+ from datetime import datetime , timedelta
2+ from os import environ
23from unittest import TestCase
34from uuid import uuid4
45
56import pytest
67
8+ from storages .backends .amazon_s3 import AmazonS3Storage
79from storages .backends .base import Storage
8- from storages .backends . amazon_s3 import amazon_s3_storage , AmazonS3Storage
10+ from storages .exceptions import ImproperlyConfiguredError
911
1012
1113class aws_temp_file :
@@ -30,19 +32,33 @@ def __exit__(self, exc_type, exc_val, exc_tb):
3032
3133
3234class TestAWSS3Storage (TestCase ):
33- _DATE_TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
34-
3535 @pytest .fixture (autouse = True )
3636 def init_storage (self , tmpdir ):
37- self ._storage = amazon_s3_storage
37+ self ._storage = AmazonS3Storage (
38+ aws_access_key_id = environ .get ("STORAGES_AWS_ACCESS_KEY_ID" ),
39+ aws_secret_access_key = environ .get (
40+ "STORAGES_AWS_SECRET_ACCESS_KEY"
41+ ),
42+ bucket_name = environ .get ("STORAGES_BUCKET_NAME" ),
43+ )
3844
3945 def test_improper_initialization (self ):
40- with pytest .raises (Exception ):
41- AmazonS3Storage (aws_access_key_id = None , aws_secret_access_key = None , bucket_name = None )
42- with pytest .raises (Exception ):
43- AmazonS3Storage (aws_access_key_id = "somekey" , aws_secret_access_key = None , bucket_name = None )
44- with pytest .raises (Exception ):
45- AmazonS3Storage (aws_access_key_id = "some_key" , aws_secret_access_key = "some_secret" , bucket_name = None )
46+ with pytest .raises (ImproperlyConfiguredError ):
47+ AmazonS3Storage (
48+ aws_access_key_id = "" , aws_secret_access_key = "" , bucket_name = ""
49+ )
50+ with pytest .raises (ImproperlyConfiguredError ):
51+ AmazonS3Storage (
52+ aws_access_key_id = "somekey" ,
53+ aws_secret_access_key = "" ,
54+ bucket_name = "" ,
55+ )
56+ with pytest .raises (ImproperlyConfiguredError ):
57+ AmazonS3Storage (
58+ aws_access_key_id = "some_key" ,
59+ aws_secret_access_key = "some_secret" ,
60+ bucket_name = "" ,
61+ )
4662
4763 def test_file_not_exists (self ):
4864 assert not self ._storage .exists ("some_non_existent.file" )
@@ -57,7 +73,10 @@ def test_file_contains_written_data(self):
5773
5874 def test_file_contains_binary_written_data (self ):
5975 with aws_temp_file (storage = self ._storage , binary = True ) as temp_file :
60- assert self ._storage .read (temp_file , mode = "rb" ) == aws_temp_file .CONTENT_BINARY
76+ assert (
77+ self ._storage .read (temp_file , mode = "rb" )
78+ == aws_temp_file .CONTENT_BINARY
79+ )
6180
6281 def test_file_does_not_exist_upon_writing_and_deletion (self ):
6382 with aws_temp_file (storage = self ._storage ) as temp_file :
@@ -69,14 +88,16 @@ def test_file_size_matches_content_size(self):
6988 assert self ._storage .size (temp_file ) == len (aws_temp_file .CONTENT )
7089
7190 def test_file_creation_time (self ):
72- with pytest .raises (Exception ):
91+ with pytest .raises (NotImplementedError ):
7392 self ._storage .get_created_time ("any_name.ext" )
7493
7594 def test_file_access_time (self ):
76- with pytest .raises (Exception ):
95+ with pytest .raises (NotImplementedError ):
7796 self ._storage .get_access_time ("any_name.ext" )
7897
7998 def test_file_modification_time (self ):
8099 with aws_temp_file (storage = self ._storage ) as temp_file :
81- modification_time = self ._storage .get_modified_time (temp_file ).replace (tzinfo = None )
100+ modification_time = self ._storage .get_modified_time (
101+ temp_file
102+ ).replace (tzinfo = None )
82103 assert modification_time - datetime .now () < timedelta (seconds = 10 )
0 commit comments