Skip to content

Commit d94ada8

Browse files
chore(tests): update webhook tests
1 parent 39d38db commit d94ada8

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

tests/api_resources/test_webhooks.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,20 @@
1616
class TestWebhooks:
1717
parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
1818

19-
def test_method_unwrap(self, client: ImageKit) -> None:
20-
key = b"secret"
21-
hook = standardwebhooks.Webhook(key)
19+
@pytest.mark.parametrize(
20+
"client_opt,method_opt",
21+
[
22+
("whsec_c2VjcmV0Cg==", None),
23+
("wrong", b"secret\n"),
24+
("wrong", "whsec_c2VjcmV0Cg=="),
25+
(None, b"secret\n"),
26+
(None, "whsec_c2VjcmV0Cg=="),
27+
],
28+
)
29+
def test_method_unwrap(self, client: ImageKit, client_opt: str | None, method_opt: str | bytes | None) -> None:
30+
hook = standardwebhooks.Webhook(b"secret\n")
31+
32+
client = client.with_options(webhook_secret=client_opt)
2233

2334
data = """{"id":"id","type":"video.transformation.accepted","created_at":"2019-12-27T18:11:19.117Z","data":{"asset":{"url":"https://example.com"},"transformation":{"type":"video-transformation","options":{"audio_codec":"aac","auto_rotate":true,"format":"mp4","quality":0,"stream_protocol":"HLS","variants":["string"],"video_codec":"h264"}}},"request":{"url":"https://example.com","x_request_id":"x_request_id","user_agent":"user_agent"}}"""
2435
msg_id = "1"
@@ -31,7 +42,7 @@ def test_method_unwrap(self, client: ImageKit) -> None:
3142
}
3243

3344
try:
34-
_ = client.webhooks.unwrap(data, headers=headers, key=key)
45+
_ = client.webhooks.unwrap(data, headers=headers, key=method_opt)
3546
except standardwebhooks.WebhookVerificationError as e:
3647
raise AssertionError("Failed to unwrap valid webhook") from e
3748

@@ -42,17 +53,30 @@ def test_method_unwrap(self, client: ImageKit) -> None:
4253
]
4354
for bad_header in bad_headers:
4455
with pytest.raises(standardwebhooks.WebhookVerificationError):
45-
_ = client.webhooks.unwrap(data, headers=bad_header, key=key)
56+
_ = client.webhooks.unwrap(data, headers=bad_header, key=method_opt)
4657

4758

4859
class TestAsyncWebhooks:
4960
parametrize = pytest.mark.parametrize(
5061
"async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
5162
)
5263

53-
def test_method_unwrap(self, client: ImageKit) -> None:
54-
key = b"secret"
55-
hook = standardwebhooks.Webhook(key)
64+
@pytest.mark.parametrize(
65+
"client_opt,method_opt",
66+
[
67+
("whsec_c2VjcmV0Cg==", None),
68+
("wrong", b"secret\n"),
69+
("wrong", "whsec_c2VjcmV0Cg=="),
70+
(None, b"secret\n"),
71+
(None, "whsec_c2VjcmV0Cg=="),
72+
],
73+
)
74+
def test_method_unwrap(
75+
self, async_client: ImageKit, client_opt: str | None, method_opt: str | bytes | None
76+
) -> None:
77+
hook = standardwebhooks.Webhook(b"secret\n")
78+
79+
async_client = async_client.with_options(webhook_secret=client_opt)
5680

5781
data = """{"id":"id","type":"video.transformation.accepted","created_at":"2019-12-27T18:11:19.117Z","data":{"asset":{"url":"https://example.com"},"transformation":{"type":"video-transformation","options":{"audio_codec":"aac","auto_rotate":true,"format":"mp4","quality":0,"stream_protocol":"HLS","variants":["string"],"video_codec":"h264"}}},"request":{"url":"https://example.com","x_request_id":"x_request_id","user_agent":"user_agent"}}"""
5882
msg_id = "1"
@@ -65,7 +89,7 @@ def test_method_unwrap(self, client: ImageKit) -> None:
6589
}
6690

6791
try:
68-
_ = client.webhooks.unwrap(data, headers=headers, key=key)
92+
_ = async_client.webhooks.unwrap(data, headers=headers, key=method_opt)
6993
except standardwebhooks.WebhookVerificationError as e:
7094
raise AssertionError("Failed to unwrap valid webhook") from e
7195

@@ -76,4 +100,4 @@ def test_method_unwrap(self, client: ImageKit) -> None:
76100
]
77101
for bad_header in bad_headers:
78102
with pytest.raises(standardwebhooks.WebhookVerificationError):
79-
_ = client.webhooks.unwrap(data, headers=bad_header, key=key)
103+
_ = async_client.webhooks.unwrap(data, headers=bad_header, key=method_opt)

0 commit comments

Comments
 (0)