|
| 1 | +import hashlib |
| 2 | +import hmac |
| 3 | + |
| 4 | +from mailtrap.webhooks import SIGNATURE_HEX_LENGTH |
| 5 | +from mailtrap.webhooks import verify_signature |
| 6 | + |
| 7 | +# --------------------------------------------------------------------------- |
| 8 | +# Cross-SDK fixture |
| 9 | +# |
| 10 | +# The (payload, signing_secret, expected_signature) triple below is the |
| 11 | +# canonical fixture shared verbatim by every official Mailtrap SDK |
| 12 | +# (mailtrap-ruby, mailtrap-python, mailtrap-php, mailtrap-nodejs, |
| 13 | +# mailtrap-java, mailtrap-dotnet). Any change here MUST be mirrored in the |
| 14 | +# equivalent test files in the other SDKs so the helpers stay byte-for-byte |
| 15 | +# compatible across languages. |
| 16 | +# --------------------------------------------------------------------------- |
| 17 | +FIXTURE_PAYLOAD = ( |
| 18 | + '{"event":"delivery","sending_stream":"transactional","category":"welcome",' |
| 19 | + '"message_id":"a8b1d8f6-1f8d-4a3c-9b2e-1a2b3c4d5e6f",' |
| 20 | + '"email":"recipient@example.com",' |
| 21 | + '"event_id":"f1e2d3c4-b5a6-7890-1234-567890abcdef",' |
| 22 | + '"timestamp":1716070000}' |
| 23 | +) |
| 24 | +FIXTURE_SIGNING_SECRET = "8d9a3c0e7f5b2d4a6c1e9f8b3a7d5c2e" |
| 25 | +FIXTURE_EXPECTED_SIGNATURE = ( |
| 26 | + "6d262e2611cd09be1f948382b5c611d63b0e585c4c9c5e40139d6ac3876d5433" |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +class TestVerifySignature: |
| 31 | + # --- 1. Valid signature for given payload + secret ---------------------- |
| 32 | + def test_returns_true_for_valid_signature_payload_and_secret(self) -> None: |
| 33 | + assert ( |
| 34 | + verify_signature( |
| 35 | + FIXTURE_PAYLOAD, |
| 36 | + FIXTURE_EXPECTED_SIGNATURE, |
| 37 | + FIXTURE_SIGNING_SECRET, |
| 38 | + ) |
| 39 | + is True |
| 40 | + ) |
| 41 | + |
| 42 | + # --- 2. Wrong secret ---------------------------------------------------- |
| 43 | + def test_returns_false_with_wrong_signing_secret(self) -> None: |
| 44 | + assert ( |
| 45 | + verify_signature( |
| 46 | + FIXTURE_PAYLOAD, |
| 47 | + FIXTURE_EXPECTED_SIGNATURE, |
| 48 | + "ffffffffffffffffffffffffffffffff", |
| 49 | + ) |
| 50 | + is False |
| 51 | + ) |
| 52 | + |
| 53 | + # --- 3. Payload tampered (one byte changed) ----------------------------- |
| 54 | + def test_returns_false_when_payload_is_tampered(self) -> None: |
| 55 | + tampered = FIXTURE_PAYLOAD.replace("delivery", "Delivery") |
| 56 | + |
| 57 | + assert ( |
| 58 | + verify_signature( |
| 59 | + tampered, |
| 60 | + FIXTURE_EXPECTED_SIGNATURE, |
| 61 | + FIXTURE_SIGNING_SECRET, |
| 62 | + ) |
| 63 | + is False |
| 64 | + ) |
| 65 | + |
| 66 | + # --- 4. Signature with wrong length ------------------------------------- |
| 67 | + def test_returns_false_without_raising_when_signature_too_short(self) -> None: |
| 68 | + too_short = FIXTURE_EXPECTED_SIGNATURE[:31] |
| 69 | + |
| 70 | + assert ( |
| 71 | + verify_signature(FIXTURE_PAYLOAD, too_short, FIXTURE_SIGNING_SECRET) is False |
| 72 | + ) |
| 73 | + |
| 74 | + # --- 5. Signature with non-hex characters ------------------------------- |
| 75 | + def test_returns_false_without_raising_for_non_hex_signature(self) -> None: |
| 76 | + not_hex = "z" * SIGNATURE_HEX_LENGTH |
| 77 | + |
| 78 | + assert verify_signature(FIXTURE_PAYLOAD, not_hex, FIXTURE_SIGNING_SECRET) is False |
| 79 | + |
| 80 | + # --- 6. Empty signature string ------------------------------------------ |
| 81 | + def test_returns_false_for_empty_signature(self) -> None: |
| 82 | + assert verify_signature(FIXTURE_PAYLOAD, "", FIXTURE_SIGNING_SECRET) is False |
| 83 | + |
| 84 | + # --- 7. Empty signing_secret -------------------------------------------- |
| 85 | + def test_returns_false_for_empty_signing_secret(self) -> None: |
| 86 | + assert verify_signature(FIXTURE_PAYLOAD, FIXTURE_EXPECTED_SIGNATURE, "") is False |
| 87 | + |
| 88 | + # --- 8. Empty payload + non-empty signature ----------------------------- |
| 89 | + def test_returns_false_for_empty_payload(self) -> None: |
| 90 | + assert ( |
| 91 | + verify_signature("", FIXTURE_EXPECTED_SIGNATURE, FIXTURE_SIGNING_SECRET) |
| 92 | + is False |
| 93 | + ) |
| 94 | + |
| 95 | + # --- 9. Known-good cross-SDK fixture ------------------------------------ |
| 96 | + def test_matches_hardcoded_hmac_sha256_digest_for_shared_fixture(self) -> None: |
| 97 | + # Recompute the digest in-place so a regression in the stdlib or the |
| 98 | + # fixture itself fails loudly: this is the byte-for-byte contract |
| 99 | + # every other Mailtrap SDK must satisfy. |
| 100 | + computed = hmac.new( |
| 101 | + FIXTURE_SIGNING_SECRET.encode("utf-8"), |
| 102 | + FIXTURE_PAYLOAD.encode("utf-8"), |
| 103 | + hashlib.sha256, |
| 104 | + ).hexdigest() |
| 105 | + |
| 106 | + assert computed == FIXTURE_EXPECTED_SIGNATURE |
| 107 | + assert ( |
| 108 | + verify_signature( |
| 109 | + FIXTURE_PAYLOAD, |
| 110 | + FIXTURE_EXPECTED_SIGNATURE, |
| 111 | + FIXTURE_SIGNING_SECRET, |
| 112 | + ) |
| 113 | + is True |
| 114 | + ) |
| 115 | + |
| 116 | + # --- Bonus: accepts bytes payload --------------------------------------- |
| 117 | + def test_accepts_bytes_payload(self) -> None: |
| 118 | + assert ( |
| 119 | + verify_signature( |
| 120 | + FIXTURE_PAYLOAD.encode("utf-8"), |
| 121 | + FIXTURE_EXPECTED_SIGNATURE, |
| 122 | + FIXTURE_SIGNING_SECRET, |
| 123 | + ) |
| 124 | + is True |
| 125 | + ) |
0 commit comments