1
1
import os
2
2
3
3
import pytest
4
- from django .core .exceptions import PermissionDenied , SuspiciousFileOperation
4
+ from django .core .exceptions import PermissionDenied
5
5
from django .core .files .base import ContentFile
6
6
from django .core .files .uploadedfile import SimpleUploadedFile
7
7
@@ -17,7 +17,7 @@ def test_get_files_from_storage(self, freeze_upload_folder):
17
17
)
18
18
files = S3FileMiddleware .get_files_from_storage (
19
19
[os .path .join (storage .aws_location , name )],
20
- "tFV9nGZlq9WX1I5Sotit18z1f4C_3lPnj33_zo4LZRc " ,
20
+ "VRIPlI1LCjUh1EtplrgxQrG8gSAaIwT48mMRlwaCytI " ,
21
21
)
22
22
file = next (files )
23
23
assert file .read () == content
@@ -35,7 +35,7 @@ def test_process_request(self, freeze_upload_folder, rf):
35
35
data = {
36
36
"file" : "custom/location/tmp/s3file/s3_file.txt" ,
37
37
"s3file" : "file" ,
38
- "file-s3f-signature" : "tFV9nGZlq9WX1I5Sotit18z1f4C_3lPnj33_zo4LZRc " ,
38
+ "file-s3f-signature" : "VRIPlI1LCjUh1EtplrgxQrG8gSAaIwT48mMRlwaCytI " ,
39
39
},
40
40
)
41
41
S3FileMiddleware (lambda x : None )(request )
@@ -49,7 +49,7 @@ def test_process_request__location_escape(self, freeze_upload_folder, rf):
49
49
data = {
50
50
"file" : "custom/location/secrets/passwords.txt" ,
51
51
"s3file" : "file" ,
52
- "file-s3f-signature" : "tFV9nGZlq9WX1I5Sotit18z1f4C_3lPnj33_zo4LZRc " ,
52
+ "file-s3f-signature" : "VRIPlI1LCjUh1EtplrgxQrG8gSAaIwT48mMRlwaCytI " ,
53
53
},
54
54
)
55
55
with pytest .raises (PermissionDenied ) as e :
@@ -66,8 +66,8 @@ def test_process_request__multiple_files(self, freeze_upload_folder, rf):
66
66
"custom/location/tmp/s3file/s3_file.txt" ,
67
67
"custom/location/tmp/s3file/s3_other_file.txt" ,
68
68
],
69
- "file-s3f-signature" : "tFV9nGZlq9WX1I5Sotit18z1f4C_3lPnj33_zo4LZRc " ,
70
- "other_file-s3f-signature" : "tFV9nGZlq9WX1I5Sotit18z1f4C_3lPnj33_zo4LZRc " ,
69
+ "file-s3f-signature" : "VRIPlI1LCjUh1EtplrgxQrG8gSAaIwT48mMRlwaCytI " ,
70
+ "other_file-s3f-signature" : "VRIPlI1LCjUh1EtplrgxQrG8gSAaIwT48mMRlwaCytI " ,
71
71
"s3file" : ["file" , "other_file" ],
72
72
},
73
73
)
@@ -88,9 +88,9 @@ def test_process_request__no_location(self, freeze_upload_folder, rf, settings):
88
88
request = rf .post (
89
89
"/" ,
90
90
data = {
91
- "file" : f "tmp/s3file/s3_file.txt" ,
91
+ "file" : "tmp/s3file/s3_file.txt" ,
92
92
"s3file" : "file" ,
93
- "file-s3f-signature" : "scjzm3N8njBQIVSGEhOchtM0TkGyb2U6OXGLVlRUZhY " ,
93
+ "file-s3f-signature" : "pJYaM4x7RzLDLVXWuphK2dMqqc0oLr_jZFasfGU7BhU " ,
94
94
},
95
95
)
96
96
S3FileMiddleware (lambda x : None )(request )
@@ -103,7 +103,7 @@ def test_process_request__no_file(self, freeze_upload_folder, rf, caplog):
103
103
data = {
104
104
"file" : "custom/location/tmp/s3file/does_not_exist.txt" ,
105
105
"s3file" : "file" ,
106
- "file-s3f-signature" : "tFV9nGZlq9WX1I5Sotit18z1f4C_3lPnj33_zo4LZRc " ,
106
+ "file-s3f-signature" : "VRIPlI1LCjUh1EtplrgxQrG8gSAaIwT48mMRlwaCytI " ,
107
107
},
108
108
)
109
109
S3FileMiddleware (lambda x : None )(request )
@@ -119,6 +119,7 @@ def test_process_request__no_signature(self, rf, caplog):
119
119
)
120
120
with pytest .raises (PermissionDenied ) as e :
121
121
S3FileMiddleware (lambda x : None )(request )
122
+ assert "No signature provided." in str (e .value )
122
123
123
124
def test_process_request__wrong_signature (self , rf , caplog ):
124
125
request = rf .post (
@@ -131,3 +132,10 @@ def test_process_request__wrong_signature(self, rf, caplog):
131
132
)
132
133
with pytest .raises (PermissionDenied ) as e :
133
134
S3FileMiddleware (lambda x : None )(request )
135
+ assert "Illegal signature!" in str (e .value )
136
+
137
+ def test_sign_s3_key_prefix (self , rf ):
138
+ assert (
139
+ S3FileMiddleware .sign_s3_key_prefix ("test/test" )
140
+ == "a8KINhIf1IpSD5sgdXE4wEQodZorq_8CmwkqZ5V6nr4"
141
+ )
0 commit comments