Debian Codegraph @

tests/api_resources/beta/test_webhooks.py

raw
1 from __future__ import annotations
2
3 import os
4 from datetime import datetime, timezone
5
6 import pytest
7 import standardwebhooks
8
9 from anthropic import Anthropic
10
11 base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
12
13
14 class TestWebhooks:
15 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
16
17 @pytest.mark.parametrize(
18 "client_opt,method_opt",
19 [
20 ("whsec_c2VjcmV0Cg==", None),
21 ("wrong", b"secret\n"),
22 ("wrong", "whsec_c2VjcmV0Cg=="),
23 (None, b"secret\n"),
24 (None, "whsec_c2VjcmV0Cg=="),
25 ],
26 )
27 def test_method_unwrap(self, client: Anthropic, client_opt: str | None, method_opt: str | bytes | None) -> None:
28 hook = standardwebhooks.Webhook(b"secret\n")
29
30 client = client.with_options(webhook_key=client_opt)
31
32 data = """{"id":"whe_0f1e2d3c4b5a69788796a5b4c3d2e1f0","created_at":"2026-03-15T10:00:00Z","data":{"id":"sesn_011CZkZAtmR3yMPDzynEDxu7","organization_id":"org_011CZkZZAe0sMna4vkBdtrfx","type":"session.status_idled","workspace_id":"wrkspc_011CZkZaBF1tNoB5wlCeusgy"},"type":"event"}"""
33 msg_id = "1"
34 timestamp = datetime.now(tz=timezone.utc)
35 sig = hook.sign(msg_id=msg_id, timestamp=timestamp, data=data)
36 headers = {
37 "webhook-id": msg_id,
38 "webhook-timestamp": str(int(timestamp.timestamp())),
39 "webhook-signature": sig,
40 }
41
42 try:
43 _ = client.beta.webhooks.unwrap(data, headers=headers, key=method_opt)
44 except standardwebhooks.WebhookVerificationError as e:
45 raise AssertionError("Failed to unwrap valid webhook") from e
46
47 bad_headers = [
48 {**headers, "webhook-signature": hook.sign(msg_id=msg_id, timestamp=timestamp, data="xxx")},
49 {**headers, "webhook-id": "bad"},
50 {**headers, "webhook-timestamp": "0"},
51 ]
52 for bad_header in bad_headers:
53 with pytest.raises(standardwebhooks.WebhookVerificationError):
54 _ = client.beta.webhooks.unwrap(data, headers=bad_header, key=method_opt)
55
56
57 class TestAsyncWebhooks:
58 parametrize = pytest.mark.parametrize(
59 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
60 )
61
62 @pytest.mark.parametrize(
63 "client_opt,method_opt",
64 [
65 ("whsec_c2VjcmV0Cg==", None),
66 ("wrong", b"secret\n"),
67 ("wrong", "whsec_c2VjcmV0Cg=="),
68 (None, b"secret\n"),
69 (None, "whsec_c2VjcmV0Cg=="),
70 ],
71 )
72 def test_method_unwrap(
73 self, async_client: Anthropic, client_opt: str | None, method_opt: str | bytes | None
74 ) -> None:
75 hook = standardwebhooks.Webhook(b"secret\n")
76
77 async_client = async_client.with_options(webhook_key=client_opt)
78
79 data = """{"id":"whe_0f1e2d3c4b5a69788796a5b4c3d2e1f0","created_at":"2026-03-15T10:00:00Z","data":{"id":"sesn_011CZkZAtmR3yMPDzynEDxu7","organization_id":"org_011CZkZZAe0sMna4vkBdtrfx","type":"session.status_idled","workspace_id":"wrkspc_011CZkZaBF1tNoB5wlCeusgy"},"type":"event"}"""
80 msg_id = "1"
81 timestamp = datetime.now(tz=timezone.utc)
82 sig = hook.sign(msg_id=msg_id, timestamp=timestamp, data=data)
83 headers = {
84 "webhook-id": msg_id,
85 "webhook-timestamp": str(int(timestamp.timestamp())),
86 "webhook-signature": sig,
87 }
88
89 try:
90 _ = async_client.beta.webhooks.unwrap(data, headers=headers, key=method_opt)
91 except standardwebhooks.WebhookVerificationError as e:
92 raise AssertionError("Failed to unwrap valid webhook") from e
93
94 bad_headers = [
95 {**headers, "webhook-signature": hook.sign(msg_id=msg_id, timestamp=timestamp, data="xxx")},
96 {**headers, "webhook-id": "bad"},
97 {**headers, "webhook-timestamp": "0"},
98 ]
99 for bad_header in bad_headers:
100 with pytest.raises(standardwebhooks.WebhookVerificationError):
101 _ = async_client.beta.webhooks.unwrap(data, headers=bad_header, key=method_opt)