-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest_views.py
77 lines (69 loc) · 2.92 KB
/
test_views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import base64
import hmac
import http
from s3file import views
class TestS3MockView:
url = "/__s3_mock__/"
policy = (
"eyJDb25kaXRpb25zIjogW3siYnVja2V0IjogInRlc3QtYnVja2V0In0sIFsic3RhcnRz"
"LXdpdGgiLCAiJGtleSIsICJ0bXAvczNmaWxlLzNlUWhwOTZYU1dldFFwZ1VVQmZzWHci"
"XSwgeyJzdWNjZXNzX2FjdGlvbl9zdGF0dXMiOiAiMjAxIn0sIFsic3RhcnRzLXdpdGgi"
"LCAiJENvbnRlbnQtVHlwZSIsICIiXV0sICJFeHBpcmVzSW4iOiAxMjA5NjAwfQ=="
)
date = "20190616T184210Z"
def test_post__bad_request(self, rf):
request = rf.post(self.url, data={})
response = views.S3MockView.as_view()(request)
assert response.status_code == http.HTTPStatus.BAD_REQUEST
def test_post__created(self, client, upload_file):
with open(upload_file) as fp:
response = client.post(
self.url,
data={
"x-amz-signature": "T1Mb71D45h1u2SBoUHOMBNFCI2AgrKAg1UzKdUHUHig=",
"x-amz-algorithm": "AWS4-HMAC-SHA256",
"x-amz-date": self.date,
"x-amz-credential": "testaccessid",
"policy": self.policy,
"key": "tmp/s3file/3eQhp96XSWetQpgUUBfsXw/${filename}",
"success_action_status": "201",
"file": fp,
},
)
assert response.status_code == http.HTTPStatus.CREATED
def test_post__bad_signature(self, client, upload_file):
bad_signature = base64.b64encode(
hmac.new(b"eve", (self.policy + self.date).encode(), "sha256").digest()
).decode()
with open(upload_file) as fp:
response = client.post(
self.url,
data={
"x-amz-signature": bad_signature,
"x-amz-algorithm": "AWS4-HMAC-SHA256",
"x-amz-date": self.date,
"x-amz-credential": "testaccessid",
"policy": self.policy,
"key": "tmp/s3file/3eQhp96XSWetQpgUUBfsXw/${filename}",
"success_action_status": "201",
"file": fp,
},
)
assert response.status_code == http.HTTPStatus.FORBIDDEN
def test_post__not_a_signature(self, client, upload_file):
bad_signature = "eve"
with open(upload_file) as fp:
response = client.post(
self.url,
data={
"x-amz-signature": bad_signature,
"x-amz-algorithm": "AWS4-HMAC-SHA256",
"x-amz-date": self.date,
"x-amz-credential": "testaccessid",
"policy": self.policy,
"key": "tmp/s3file/3eQhp96XSWetQpgUUBfsXw/${filename}",
"success_action_status": "201",
"file": fp,
},
)
assert response.status_code == http.HTTPStatus.BAD_REQUEST