test_views.py 2.9 KB

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