Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 22, 12:00 UTC
feat(py): add tests for cross-sdk webhook handling parity between Python and TypeScriptSDK
jkomyno committed
Jan 29, 2026 at 04:11 UTC
4ea885cd216ad034a1b17cf6024ae2958d4f6f53
7 files changed
+537
python/tests/conftest.py
+90
@@ -1 +1,91 @@
1
"""pytest configuration module"""
2
+
3
+import base64
4
+import hashlib
5
+import hmac
6
+import json
7
+from pathlib import Path
8
+from unittest.mock import Mock
9
+
10
+import pytest
11
+
12
+from composio.core.models.triggers import Triggers
13
+
14
+
15
+def get_py_fixtures_dir() -> Path:
16
+ """Get the Python fixtures directory path."""
17
+ return Path(__file__).parent / "fixtures" / "webhook"
18
+
19
+
20
+def get_ts_fixtures_dir() -> Path:
21
+ """Get the TypeScript fixtures directory path."""
22
+ return (
23
+ Path(__file__).parent.parent.parent
24
+ / "ts"
25
+ / "packages"
26
+ / "core"
27
+ / "test"
28
+ / "fixtures"
29
+ / "webhook"
30
+ )
31
+
32
+
33
+def compute_signature(
34
+ webhook_id: str, timestamp: str, payload: str, secret: str
35
+) -> str:
36
+ """Compute webhook signature using HMAC-SHA256."""
37
+ to_sign = f"{webhook_id}.{timestamp}.{payload}"
38
+ signature = hmac.new(
39
+ key=secret.encode("utf-8"),
40
+ msg=to_sign.encode("utf-8"),
41
+ digestmod=hashlib.sha256,
42
+ ).digest()
43
+ return f"v1,{base64.b64encode(signature).decode('utf-8')}"
44
+
45
+
46
+def load_fixtures() -> list[dict]:
47
+ """Load all webhook fixtures from the fixtures directory."""
48
+ fixtures_dir = get_py_fixtures_dir()
49
+ fixtures = []
50
+ for fixture_file in fixtures_dir.glob("v*.json"):
51
+ if "golden" in fixture_file.name:
52
+ continue
53
+ with open(fixture_file) as f:
54
+ fixtures.append(json.load(f))
55
+ return fixtures
56
+
57
+
58
+def load_golden_signatures() -> dict:
59
+ """Load golden signatures for contract testing."""
60
+ fixtures_dir = get_py_fixtures_dir()
61
+ with open(fixtures_dir / "golden-signatures.json") as f:
62
+ return json.load(f)
63
+
64
+
65
+@pytest.fixture
66
+def mock_client() -> Mock:
67
+ """Create a mock HTTP client."""
68
+ client = Mock()
69
+ client.triggers_types = Mock()
70
+ client.trigger_instances = Mock()
71
+ client.trigger_instances.manage = Mock()
72
+ client.connected_accounts = Mock()
73
+ return client
74
+
75
+
76
+@pytest.fixture
77
+def triggers(mock_client: Mock) -> Triggers:
78
+ """Create a Triggers instance."""
79
+ return Triggers(client=mock_client)
80
+
81
+
82
+@pytest.fixture
83
+def webhook_fixtures() -> list[dict]:
84
+ """Load all webhook fixtures."""
85
+ return load_fixtures()
86
+
87
+
88
+@pytest.fixture
89
+def golden_signatures() -> dict:
90
+ """Load golden signatures."""
91
+ return load_golden_signatures()
python/tests/fixtures/webhook/golden-signatures.json
new
+47
@@ -0,0 +1,47 @@
1
+{
2
+ "description": "Golden signature test cases - DO NOT MODIFY unless algorithm changes",
3
+ "algorithm": "HMAC-SHA256",
4
+ "format": "v1,base64(HMAC-SHA256(id.timestamp.payload, secret))",
5
+ "testCases": [
6
+ {
7
+ "name": "simple payload",
8
+ "id": "msg_test_001",
9
+ "timestamp": "1700000000",
10
+ "payload": "{\"test\":\"data\"}",
11
+ "secret": "test-secret",
12
+ "expectedSignature": "v1,4Fay6rvxXDEbH+4oA1fYP2BR/Bwzn3/7ONj40iJQagY="
13
+ },
14
+ {
15
+ "name": "unicode payload",
16
+ "id": "msg_test_002",
17
+ "timestamp": "1700000001",
18
+ "payload": "{\"message\":\"Hello World\"}",
19
+ "secret": "unicode-secret-key",
20
+ "expectedSignature": "v1,ICmAFUlesRqIFA44bU6Wlce5GszoHjG+w7nurcu5EWc="
21
+ },
22
+ {
23
+ "name": "complex nested payload",
24
+ "id": "msg_test_003",
25
+ "timestamp": "1700000002",
26
+ "payload": "{\"nested\":{\"array\":[1,2,3],\"object\":{\"key\":\"value\"}}}",
27
+ "secret": "complex-secret",
28
+ "expectedSignature": "v1,Vx/pw0yOiRRoxdfifaZ3zBKt4B3KkRKFVr6gLQNJs9Q="
29
+ },
30
+ {
31
+ "name": "empty data object",
32
+ "id": "msg_test_004",
33
+ "timestamp": "1700000003",
34
+ "payload": "{\"data\":{}}",
35
+ "secret": "empty-data-secret",
36
+ "expectedSignature": "v1,IbLtu+G65OH8Gw7ZFqy38ru+Jrg96yWfueYMMdaFYs0="
37
+ },
38
+ {
39
+ "name": "special characters in secret",
40
+ "id": "msg_test_005",
41
+ "timestamp": "1700000004",
42
+ "payload": "{\"test\":true}",
43
+ "secret": "secret!@#$%^&*()",
44
+ "expectedSignature": "v1,kUvMXJ0rouLoROxY70WIzV+zLQGpSRuCXv8evLTPjXg="
45
+ }
46
+ ]
47
+}
python/tests/fixtures/webhook/v1-github-push.json
new
+16
@@ -0,0 +1,16 @@
1
+{
2
+ "description": "V1 GitHub push webhook - legacy format sanitized",
3
+ "capturedAt": "2026-01-29T10:30:00Z",
4
+ "headers": {
5
+ "webhook-id": "msg_fixture_v1_001",
6
+ "webhook-timestamp": "1738150200",
7
+ "webhook-signature": "v1,A/VFbvOJ8JAzXZweWvc+IHaBie19NVKUpQKbeobt5xo="
8
+ },
9
+ "payload": "{\"trigger_name\":\"GITHUB_PUSH_EVENT\",\"connection_id\":\"conn_fixture_001\",\"trigger_id\":\"trigger_fixture_001\",\"payload\":{\"ref\":\"refs/heads/main\",\"commits\":[{\"id\":\"def456\",\"message\":\"Legacy commit\"}]},\"log_id\":\"log_fixture_003\"}",
10
+ "testSecret": "test-webhook-secret-for-fixtures",
11
+ "expectedResult": {
12
+ "version": "V1",
13
+ "triggerSlug": "GITHUB_PUSH_EVENT",
14
+ "triggerId": "trigger_fixture_001"
15
+ }
16
+}
python/tests/fixtures/webhook/v2-github-push.json
new
+16
@@ -0,0 +1,16 @@
1
+{
2
+ "description": "V2 GitHub push webhook - sanitized real-world structure",
3
+ "capturedAt": "2026-01-29T10:30:00Z",
4
+ "headers": {
5
+ "webhook-id": "msg_fixture_v2_001",
6
+ "webhook-timestamp": "1738150200",
7
+ "webhook-signature": "v1,a/mTZohEnMvzupLGWsiDLhTctk+hgx3Q2r1ACjybkhk="
8
+ },
9
+ "payload": "{\"type\":\"github_push_event\",\"timestamp\":\"2026-01-29T10:30:00Z\",\"log_id\":\"log_fixture_002\",\"data\":{\"connection_id\":\"conn_uuid_fixture_002\",\"connection_nano_id\":\"ca_fixture_002\",\"trigger_nano_id\":\"ti_fixture_002\",\"trigger_id\":\"trigger_uuid_fixture_002\",\"user_id\":\"user_fixture_002\",\"ref\":\"refs/heads/main\",\"repository\":{\"name\":\"test-repo\"}}}",
10
+ "testSecret": "test-webhook-secret-for-fixtures",
11
+ "expectedResult": {
12
+ "version": "V2",
13
+ "triggerSlug": "GITHUB_PUSH_EVENT",
14
+ "userId": "user_fixture_002"
15
+ }
16
+}
python/tests/fixtures/webhook/v3-github-push.json
new
+17
@@ -0,0 +1,17 @@
1
+{
2
+ "description": "V3 GitHub push webhook - sanitized real-world structure",
3
+ "capturedAt": "2026-01-29T10:30:00Z",
4
+ "headers": {
5
+ "webhook-id": "msg_fixture_v3_001",
6
+ "webhook-timestamp": "1738150200",
7
+ "webhook-signature": "v1,GqTsMaDtroeNNR7rDDzEGVTtHfFtIDknXhmkCXss7cA="
8
+ },
9
+ "payload": "{\"id\":\"evt_fixture_v3_001\",\"timestamp\":\"2026-01-29T10:30:00Z\",\"type\":\"composio.trigger.message\",\"metadata\":{\"log_id\":\"log_fixture_001\",\"trigger_slug\":\"GITHUB_PUSH_EVENT\",\"trigger_id\":\"ti_fixture_001\",\"connected_account_id\":\"ca_fixture_001\",\"auth_config_id\":\"ac_fixture_001\",\"user_id\":\"user_fixture_001\"},\"data\":{\"ref\":\"refs/heads/main\",\"repository\":{\"name\":\"test-repo\",\"full_name\":\"user/test-repo\"},\"pusher\":{\"name\":\"test-user\"},\"commits\":[{\"id\":\"abc123\",\"message\":\"Test commit\"}]}}",
10
+ "testSecret": "test-webhook-secret-for-fixtures",
11
+ "expectedResult": {
12
+ "version": "V3",
13
+ "triggerSlug": "GITHUB_PUSH_EVENT",
14
+ "userId": "user_fixture_001",
15
+ "connectedAccountId": "ca_fixture_001"
16
+ }
17
+}
python/tests/test_cross_sdk_compatibility.py
new
+40
@@ -0,0 +1,40 @@
1
+"""Cross-SDK compatibility tests.
2
+
3
+These tests verify that the Python SDK uses identical fixtures
4
+to the TypeScript SDK, ensuring webhook verification works consistently
5
+across both SDKs.
6
+"""
7
+
8
+import json
9
+
10
+import pytest
11
+
12
+from tests.conftest import get_py_fixtures_dir, get_ts_fixtures_dir
13
+
14
+
15
+class TestCrossSDKFixtureCompatibility:
16
+ """Tests verifying Python and TypeScript fixtures are synchronized."""
17
+
18
+ @pytest.mark.parametrize(
19
+ "fixture_name",
20
+ [
21
+ "golden-signatures.json",
22
+ "v1-github-push.json",
23
+ "v2-github-push.json",
24
+ "v3-github-push.json",
25
+ ],
26
+ )
27
+ def test_fixtures_are_identical(self, fixture_name: str) -> None:
28
+ """Verify TypeScript and Python fixtures are byte-identical."""
29
+ ts_path = get_ts_fixtures_dir() / fixture_name
30
+ py_path = get_py_fixtures_dir() / fixture_name
31
+
32
+ with open(ts_path) as f:
33
+ ts_content = json.load(f)
34
+ with open(py_path) as f:
35
+ py_content = json.load(f)
36
+
37
+ assert ts_content == py_content, (
38
+ f"Fixture {fixture_name} differs between TypeScript and Python SDKs. "
39
+ "Ensure fixtures are kept in sync."
40
+ )
python/tests/test_verify_webhook_integration.py
new
+311
@@ -0,0 +1,311 @@
1
+"""Integration tests for verify_webhook using fixtures.
2
+
3
+These tests use the same fixtures as the TypeScript SDK to ensure
4
+cross-SDK compatibility and algorithm correctness.
5
+"""
6
+
7
+import base64
8
+import hashlib
9
+import hmac
10
+import json
11
+
12
+import pytest
13
+
14
+from composio import exceptions
15
+from composio.core.models.triggers import Triggers, WebhookVersion
16
+from tests.conftest import compute_signature, load_fixtures, load_golden_signatures
17
+
18
+
19
+class TestVerifyWebhook:
20
+ """Tests for webhook verification against fixture data."""
21
+
22
+ @pytest.mark.parametrize(
23
+ "fixture",
24
+ load_fixtures(),
25
+ ids=lambda f: f["description"],
26
+ )
27
+ def test_verify_fixture(self, triggers: Triggers, fixture: dict) -> None:
28
+ """Verify each fixture passes verification."""
29
+ result = triggers.verify_webhook(
30
+ id=fixture["headers"]["webhook-id"],
31
+ payload=fixture["payload"],
32
+ signature=fixture["headers"]["webhook-signature"],
33
+ timestamp=fixture["headers"]["webhook-timestamp"],
34
+ secret=fixture["testSecret"],
35
+ tolerance=0,
36
+ )
37
+
38
+ assert result["version"].value == fixture["expectedResult"]["version"]
39
+ expected_slug = fixture["expectedResult"]["triggerSlug"]
40
+ assert result["payload"]["trigger_slug"] == expected_slug
41
+
42
+ if "userId" in fixture["expectedResult"]:
43
+ assert result["payload"]["user_id"] == fixture["expectedResult"]["userId"]
44
+
45
+ if "connectedAccountId" in fixture["expectedResult"]:
46
+ assert (
47
+ result["payload"]["metadata"]["connected_account"]["id"]
48
+ == fixture["expectedResult"]["connectedAccountId"]
49
+ )
50
+
51
+ if "triggerId" in fixture["expectedResult"]:
52
+ assert result["payload"]["id"] == fixture["expectedResult"]["triggerId"]
53
+
54
+ @pytest.mark.parametrize(
55
+ "version,expected_enum,expected_keys",
56
+ [
57
+ ("V3", WebhookVersion.V3, ["type", "metadata"]),
58
+ ("V2", WebhookVersion.V2, ["type", "data"]),
59
+ ("V1", WebhookVersion.V1, ["trigger_name", "connection_id"]),
60
+ ],
61
+ )
62
+ def test_detects_version(
63
+ self,
64
+ triggers: Triggers,
65
+ webhook_fixtures: list[dict],
66
+ version: str,
67
+ expected_enum: WebhookVersion,
68
+ expected_keys: list[str],
69
+ ) -> None:
70
+ """Test version detection for V1, V2, V3."""
71
+ fixture = next(
72
+ (f for f in webhook_fixtures if f["expectedResult"]["version"] == version),
73
+ None,
74
+ )
75
+ if fixture is None:
76
+ pytest.skip(f"No {version} fixture found")
77
+
78
+ result = triggers.verify_webhook(
79
+ id=fixture["headers"]["webhook-id"],
80
+ payload=fixture["payload"],
81
+ signature=fixture["headers"]["webhook-signature"],
82
+ timestamp=fixture["headers"]["webhook-timestamp"],
83
+ secret=fixture["testSecret"],
84
+ tolerance=0,
85
+ )
86
+
87
+ assert result["version"] == expected_enum
88
+ for key in expected_keys:
89
+ assert key in result["raw_payload"]
90
+
91
+
92
+class TestGoldenSignatures:
93
+ """Contract tests for signature algorithm."""
94
+
95
+ @pytest.mark.parametrize(
96
+ "test_case",
97
+ load_golden_signatures()["testCases"],
98
+ ids=lambda tc: tc["name"],
99
+ )
100
+ def test_produces_identical_signature(self, test_case: dict) -> None:
101
+ """Verify signature algorithm produces identical output."""
102
+ computed = compute_signature(
103
+ test_case["id"],
104
+ test_case["timestamp"],
105
+ test_case["payload"],
106
+ test_case["secret"],
107
+ )
108
+ assert computed == test_case["expectedSignature"]
109
+
110
+ def test_algorithm_matches_documented_format(self, golden_signatures: dict) -> None:
111
+ """Verify algorithm documentation."""
112
+ assert golden_signatures["algorithm"] == "HMAC-SHA256"
113
+ assert (
114
+ golden_signatures["format"]
115
+ == "v1,base64(HMAC-SHA256(id.timestamp.payload, secret))"
116
+ )
117
+
118
+
119
+class TestSignatureValidation:
120
+ """Tests for signature algorithm validation."""
121
+
122
+ def test_computes_signature_using_id_timestamp_payload_format(
123
+ self, triggers: Triggers, webhook_fixtures: list[dict]
124
+ ) -> None:
125
+ """Verify signature is computed using id.timestamp.payload format."""
126
+ fixture = webhook_fixtures[0]
127
+ expected_signature = compute_signature(
128
+ fixture["headers"]["webhook-id"],
129
+ fixture["headers"]["webhook-timestamp"],
130
+ fixture["payload"],
131
+ fixture["testSecret"],
132
+ )
133
+ assert expected_signature == fixture["headers"]["webhook-signature"]
134
+
135
+ def test_rejects_signature_computed_with_payload_only(
136
+ self, triggers: Triggers, webhook_fixtures: list[dict]
137
+ ) -> None:
138
+ """Reject signature computed with only payload (wrong format)."""
139
+ fixture = webhook_fixtures[0]
140
+ wrong_signature = "v1," + base64.b64encode(
141
+ hmac.new(
142
+ key=fixture["testSecret"].encode("utf-8"),
143
+ msg=fixture["payload"].encode("utf-8"),
144
+ digestmod=hashlib.sha256,
145
+ ).digest()
146
+ ).decode("utf-8")
147
+
148
+ with pytest.raises(exceptions.WebhookSignatureVerificationError):
149
+ triggers.verify_webhook(
150
+ id=fixture["headers"]["webhook-id"],
151
+ payload=fixture["payload"],
152
+ signature=wrong_signature,
153
+ timestamp=fixture["headers"]["webhook-timestamp"],
154
+ secret=fixture["testSecret"],
155
+ tolerance=0,
156
+ )
157
+
158
+ def test_rejects_signature_missing_id(
159
+ self, triggers: Triggers, webhook_fixtures: list[dict]
160
+ ) -> None:
161
+ """Reject signature computed with timestamp.payload (missing id)."""
162
+ fixture = webhook_fixtures[0]
163
+ to_sign = f"{fixture['headers']['webhook-timestamp']}.{fixture['payload']}"
164
+ wrong_signature = "v1," + base64.b64encode(
165
+ hmac.new(
166
+ key=fixture["testSecret"].encode("utf-8"),
167
+ msg=to_sign.encode("utf-8"),
168
+ digestmod=hashlib.sha256,
169
+ ).digest()
170
+ ).decode("utf-8")
171
+
172
+ with pytest.raises(exceptions.WebhookSignatureVerificationError):
173
+ triggers.verify_webhook(
174
+ id=fixture["headers"]["webhook-id"],
175
+ payload=fixture["payload"],
176
+ signature=wrong_signature,
177
+ timestamp=fixture["headers"]["webhook-timestamp"],
178
+ secret=fixture["testSecret"],
179
+ tolerance=0,
180
+ )
181
+
182
+
183
+class TestPayloadStructure:
184
+ """Tests for payload structure validation."""
185
+
186
+ def test_preserves_exact_json_structure(
187
+ self, triggers: Triggers, webhook_fixtures: list[dict]
188
+ ) -> None:
189
+ """Verify raw payload matches fixture exactly."""
190
+ v3_fixture = next(
191
+ (f for f in webhook_fixtures if f["expectedResult"]["version"] == "V3"),
192
+ None,
193
+ )
194
+ if v3_fixture is None:
195
+ pytest.skip("No V3 fixture found")
196
+
197
+ result = triggers.verify_webhook(
198
+ id=v3_fixture["headers"]["webhook-id"],
199
+ payload=v3_fixture["payload"],
200
+ signature=v3_fixture["headers"]["webhook-signature"],
201
+ timestamp=v3_fixture["headers"]["webhook-timestamp"],
202
+ secret=v3_fixture["testSecret"],
203
+ tolerance=0,
204
+ )
205
+
206
+ parsed_payload = json.loads(v3_fixture["payload"])
207
+ assert result["raw_payload"] == parsed_payload
208
+
209
+ def test_normalizes_v3_payload(
210
+ self, triggers: Triggers, webhook_fixtures: list[dict]
211
+ ) -> None:
212
+ """Verify V3 payload is normalized correctly."""
213
+ v3_fixture = next(
214
+ (f for f in webhook_fixtures if f["expectedResult"]["version"] == "V3"),
215
+ None,
216
+ )
217
+ if v3_fixture is None:
218
+ pytest.skip("No V3 fixture found")
219
+
220
+ result = triggers.verify_webhook(
221
+ id=v3_fixture["headers"]["webhook-id"],
222
+ payload=v3_fixture["payload"],
223
+ signature=v3_fixture["headers"]["webhook-signature"],
224
+ timestamp=v3_fixture["headers"]["webhook-timestamp"],
225
+ secret=v3_fixture["testSecret"],
226
+ tolerance=0,
227
+ )
228
+
229
+ payload = result["payload"]
230
+ assert "id" in payload
231
+ assert "uuid" in payload
232
+ assert "trigger_slug" in payload
233
+ assert "toolkit_slug" in payload
234
+ assert "user_id" in payload
235
+ assert "payload" in payload
236
+ assert "metadata" in payload
237
+ assert "connected_account" in payload["metadata"]
238
+
239
+
240
+class TestWhitespaceSensitivity:
241
+ """Tests for whitespace sensitivity in payload verification."""
242
+
243
+ def test_fails_verification_if_whitespace_changes(
244
+ self, triggers: Triggers, webhook_fixtures: list[dict]
245
+ ) -> None:
246
+ """Verify whitespace changes cause verification failure."""
247
+ fixture = webhook_fixtures[0]
248
+ modified_payload = fixture["payload"].replace("{", "{ ")
249
+
250
+ expected_errors = (
251
+ exceptions.WebhookSignatureVerificationError,
252
+ exceptions.WebhookPayloadError,
253
+ )
254
+ with pytest.raises(expected_errors):
255
+ triggers.verify_webhook(
256
+ id=fixture["headers"]["webhook-id"],
257
+ payload=modified_payload,
258
+ signature=fixture["headers"]["webhook-signature"],
259
+ timestamp=fixture["headers"]["webhook-timestamp"],
260
+ secret=fixture["testSecret"],
261
+ tolerance=0,
262
+ )
263
+
264
+ def test_fails_verification_if_payload_reserialized(
265
+ self, triggers: Triggers, webhook_fixtures: list[dict]
266
+ ) -> None:
267
+ """Verify re-serialization may cause verification failure."""
268
+ fixture = webhook_fixtures[0]
269
+ reserialized = json.dumps(json.loads(fixture["payload"]))
270
+
271
+ # Only test if re-serialization actually changed the payload
272
+ if reserialized != fixture["payload"]:
273
+ expected_errors = (
274
+ exceptions.WebhookSignatureVerificationError,
275
+ exceptions.WebhookPayloadError,
276
+ )
277
+ with pytest.raises(expected_errors):
278
+ triggers.verify_webhook(
279
+ id=fixture["headers"]["webhook-id"],
280
+ payload=reserialized,
281
+ signature=fixture["headers"]["webhook-signature"],
282
+ timestamp=fixture["headers"]["webhook-timestamp"],
283
+ secret=fixture["testSecret"],
284
+ tolerance=0,
285
+ )
286
+
287
+
288
+class TestFixtureConsistency:
289
+ """Tests for fixture consistency."""
290
+
291
+ def test_all_fixtures_use_same_test_secret(
292
+ self, webhook_fixtures: list[dict]
293
+ ) -> None:
294
+ """Verify all fixtures use the same test secret."""
295
+ secrets = {f["testSecret"] for f in webhook_fixtures}
296
+ assert len(secrets) == 1
297
+ assert "test-webhook-secret-for-fixtures" in secrets
298
+
299
+ def test_all_fixtures_have_unique_webhook_ids(
300
+ self, webhook_fixtures: list[dict]
301
+ ) -> None:
302
+ """Verify all fixtures have unique webhook IDs."""
303
+ ids = [f["headers"]["webhook-id"] for f in webhook_fixtures]
304
+ assert len(set(ids)) == len(webhook_fixtures)
305
+
306
+ def test_fixtures_cover_all_supported_versions(
307
+ self, webhook_fixtures: list[dict]
308
+ ) -> None:
309
+ """Verify fixtures cover V1, V2, and V3."""
310
+ versions = {f["expectedResult"]["version"] for f in webhook_fixtures}
311
+ assert {"V1", "V2", "V3"} <= versions