Skip to content

Commit e495c86

Browse files
committed
Add a test class for testing a (mocked) signature authentication process.
1 parent 4b78f26 commit e495c86

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

tests/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import datetime
2+
3+
14
def request_body_matcher(pattern):
25
def _callback(request):
36
return pattern in request.text
47

58
return _callback
9+
10+
11+
def get_test_time():
12+
return datetime.datetime.fromisoformat("2024-04-04T04:44:44Z")
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import copy
2+
import json
3+
import unittest
4+
from datetime import timedelta
5+
6+
import requests_mock
7+
8+
from tests import get_test_time, request_body_matcher
9+
from transloadit.client import Transloadit
10+
from transloadit.request import Request
11+
12+
13+
class TestSignatureAuthentication(unittest.TestCase):
14+
def setUp(self):
15+
self.mock_client = MockClient("TRANSLOADIT_KEY", "TRANSLOADIT_SECRET")
16+
self.json_response = (
17+
'{"ok": "ASSEMBLY_COMPLETED", "assembly_id": "abcdef45673"}'
18+
)
19+
20+
@requests_mock.Mocker()
21+
def test_fixed_signature(self, mock):
22+
# Test a request with a fixed timestamp in order to have reproducible results
23+
assembly = self.mock_client.new_assembly()
24+
assembly.add_step("import", "/http/import",
25+
options={"url": "https://demos.transloadit.com/inputs/chameleon.jpg"})
26+
assembly.add_step("resize", "/image/resize", {"use:": "import", "width": 70, "height": 70})
27+
28+
url = f"{self.mock_client.service}/assemblies"
29+
mock.post(
30+
url,
31+
text=self.json_response,
32+
additional_matcher=request_body_matcher(
33+
"signature=sha384"
34+
"%3A46943b5542af95679940d94507865b20b36cb1808a7a9dc64c6255f26d1e921bd6574443b80b0dcd595769268f74273c"),
35+
)
36+
37+
assembly.create(resumable=False)
38+
39+
40+
class MockClient(Transloadit):
41+
"""
42+
Mock Class of the Transloadit Clients, which loads the modified MockRequest Class
43+
instead of the Standard Request class.
44+
"""
45+
46+
def __init__(self,
47+
auth_key: str,
48+
auth_secret: str,
49+
service: str = "https://api2.transloadit.com",
50+
duration: int = 300):
51+
if not service.startswith(("http://", "https://")):
52+
service = "https://" + service
53+
54+
self.service = service
55+
self.auth_key = auth_key
56+
self.auth_secret = auth_secret
57+
self.duration = duration
58+
self.request = MockRequest(self)
59+
60+
61+
class MockRequest(Request):
62+
"""
63+
Mock Request Class, which uses a fixed datetime for generating the signature.
64+
This is for having a reproducible value to test against.
65+
"""
66+
def _to_payload(self, data):
67+
data = copy.deepcopy(data or {})
68+
expiry = timedelta(seconds=self.transloadit.duration) + get_test_time()
69+
data["auth"] = {
70+
"key": self.transloadit.auth_key,
71+
"expires": expiry.strftime("%Y/%m/%d %H:%M:%S+00:00"),
72+
}
73+
json_data = json.dumps(data)
74+
75+
return {"params": json, "signature": self._sign_data(json_data)}

0 commit comments

Comments
 (0)