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