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 21, 03:37 UTC
| 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 |