GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — xmlvalid-python/tests/test_webhooks.py at main · xmlvalid/xmlvalid-python

Expand file tree

/

test_webhooks.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
73 lines (48 loc) · 2.35 KB
/

test_webhooks.py

Copy path

File metadata and controls

73 lines (48 loc) · 2.35 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from __future__ import annotations
import hashlib
import hmac
import json
import pytest
from xmlvalid import Event, SignatureVerificationError, webhooks
from .conftest import validation
SECRET = "whsec_test"
NOW = 1_800_000_000
def sign(body: bytes, timestamp: int = NOW, secret: str = SECRET) -> str:
return f"t={timestamp},v1=" + hmac.new(secret.encode(), f"{timestamp}.".encode() + body, hashlib.sha256).hexdigest()
def body(event_type: str = "validation.completed") -> bytes:
data = validation() if event_type == "validation.completed" else {"message": "A test event from xmlvalid.com."}
return json.dumps(
{"id": "evt_1", "type": event_type, "created_at": "2026-10-18T10:00:00+00:00", "data": data}
).encode()
def test_a_signed_event_is_read() -> None:
event = webhooks.verify(body(), sign(body()), SECRET, now=NOW)
assert (event.id, event.type) == ("evt_1", Event.VALIDATION_COMPLETED)
assert event.validation is not None and event.validation.is_valid
assert event.created_at is not None and event.created_at.year == 2026
def test_a_ping_has_no_validation_and_a_str_body_works() -> None:
raw = body("ping")
event = webhooks.verify(raw.decode(), sign(raw), SECRET, now=NOW)
assert event.type == Event.PING and event.validation is None
def test_any_v1_signature_may_match() -> None:
raw = body()
signature = f"t={NOW},v1={'0' * 64}," + sign(raw).split(",")[1]
assert webhooks.verify(raw, signature, SECRET, now=NOW).id == "evt_1"
@pytest.mark.parametrize(
("signature", "message"),
[
(None, "missing or malformed"),
("v1=abc", "missing or malformed"),
(sign(body(), NOW - 301), "too old"),
(sign(body(), secret="whsec_other"), "does not match"),
(sign(b"not json"), "not JSON"),
(sign(b"[1]"), "not a JSON object"),
],
)
def test_bad_signatures_are_refused(signature: str | None, message: str) -> None:
raw = b"not json" if signature is not None and signature == sign(b"not json") else body()
raw = b"[1]" if signature is not None and signature == sign(b"[1]") else raw
with pytest.raises(SignatureVerificationError, match=message):
webhooks.verify(raw, signature, SECRET, now=NOW)
def test_the_age_check_can_be_skipped() -> None:
raw = body()
assert webhooks.verify(raw, sign(raw, 1), SECRET, tolerance=0).id == "evt_1"
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.