|
| 1 | +"""Webhook verification tests.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import hashlib |
| 6 | +import hmac |
| 7 | +import unittest |
| 8 | +from datetime import datetime, timedelta, timezone |
| 9 | + |
| 10 | +from delega import verify_webhook |
| 11 | + |
| 12 | + |
| 13 | +def _timestamp(delta: timedelta = timedelta()) -> str: |
| 14 | + return (datetime.now(timezone.utc) + delta).isoformat().replace("+00:00", "Z") |
| 15 | + |
| 16 | + |
| 17 | +def _signature(payload: bytes, timestamp: str, secret: str) -> str: |
| 18 | + digest = hmac.new( |
| 19 | + secret.encode("utf-8"), |
| 20 | + timestamp.encode("utf-8") + b"." + payload, |
| 21 | + hashlib.sha256, |
| 22 | + ).hexdigest() |
| 23 | + return f"sha256={digest}" |
| 24 | + |
| 25 | + |
| 26 | +class TestVerifyWebhook(unittest.TestCase): |
| 27 | + def setUp(self) -> None: |
| 28 | + self.payload = b'{"event":"task.created","task":{"id":"abc123"}}' |
| 29 | + self.secret = "whsec_test_secret" |
| 30 | + |
| 31 | + def test_verifies_valid_signature(self) -> None: |
| 32 | + timestamp = _timestamp() |
| 33 | + signature = _signature(self.payload, timestamp, self.secret) |
| 34 | + |
| 35 | + self.assertTrue( |
| 36 | + verify_webhook(self.payload, signature, timestamp, self.secret) |
| 37 | + ) |
| 38 | + |
| 39 | + def test_rejects_bad_signature_format(self) -> None: |
| 40 | + timestamp = _timestamp() |
| 41 | + |
| 42 | + with self.assertRaisesRegex(ValueError, "bad signature format"): |
| 43 | + verify_webhook(self.payload, "not-a-sha256", timestamp, self.secret) |
| 44 | + |
| 45 | + def test_rejects_stale_timestamp(self) -> None: |
| 46 | + timestamp = _timestamp(timedelta(minutes=-6)) |
| 47 | + signature = _signature(self.payload, timestamp, self.secret) |
| 48 | + |
| 49 | + with self.assertRaisesRegex(ValueError, "stale timestamp"): |
| 50 | + verify_webhook(self.payload, signature, timestamp, self.secret) |
| 51 | + |
| 52 | + def test_rejects_signature_mismatch(self) -> None: |
| 53 | + timestamp = _timestamp() |
| 54 | + signature = _signature(self.payload, timestamp, "wrong_secret") |
| 55 | + |
| 56 | + with self.assertRaisesRegex(ValueError, "signature mismatch"): |
| 57 | + verify_webhook(self.payload, signature, timestamp, self.secret) |
| 58 | + |
| 59 | + def test_rejects_invalid_timestamp(self) -> None: |
| 60 | + signature = _signature(self.payload, "not-a-timestamp", self.secret) |
| 61 | + |
| 62 | + with self.assertRaisesRegex(ValueError, "invalid timestamp"): |
| 63 | + verify_webhook( |
| 64 | + self.payload, |
| 65 | + signature, |
| 66 | + "not-a-timestamp", |
| 67 | + self.secret, |
| 68 | + ) |
0 commit comments