Repository navigation
Expand file tree
/
Copy pathproof.py
More file actions
85 lines (72 loc) · 3.31 KB
/
Copy pathproof.py
File metadata and controls
85 lines (72 loc) · 3.31 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
"""Use only against a disposable app with a synthetic webhook secret."""
import hashlib
import hmac
import json
import os
import secrets
from uuid import uuid4
import httpx
SECRET = os.environ["GITHUB_WEBHOOK_SECRET"].encode()
REPO_ID = int(os.environ["DEMO_REPO_ID"])
WORKER = {"X-Demo-Worker-Key": os.environ["DEMO_WORKER_KEY"]}
TOKEN = os.environ.get("DEMO_INVOKE_TOKEN")
AUTH = {"Authorization": "Bearer " + TOKEN} if TOKEN else {}
def payload(pr, repo=REPO_ID, action="opened"):
return json.dumps(
{"action": action, "repository": {"id": repo}, "pull_request": {"id": pr}},
indent=2,
).encode()
def signature(raw):
return "sha256=" + hmac.new(SECRET, raw, hashlib.sha256).hexdigest()
def test():
base = os.environ.get("DEMO_BASE_URL", "http://127.0.0.1:8080")
with httpx.Client(base_url=base, headers=AUTH, timeout=18) as client:
pr = secrets.randbelow(2**50) + 1
delivery = str(uuid4())
body = payload(pr)
route = "/webhooks/github"
def send(raw, delivery_id=delivery, sig=None, event="pull_request"):
return client.post(
route,
content=raw,
headers={
"Content-Type": "application/json",
"X-Hub-Signature-256": signature(raw) if sig is None else sig,
"X-GitHub-Delivery": delivery_id,
"X-GitHub-Event": event,
},
)
assert send(body, sig="").status_code == 401
assert send(body + b" ", sig=signature(body)).status_code == 401
assert send(payload(pr, repo=REPO_ID + 1)).status_code == 403
assert send(payload(pr, action="closed")).status_code == 204
assert client.get(f"/demo/effects/{pr}", headers=WORKER).json() == {
"deliveries": 0, "jobs": 0, "effects": 0
}
first = send(body)
assert first.status_code == 202 and first.json() == {
"delivery_id": delivery, "duplicate_delivery": False, "job_created": True
}, first.text
repeat = send(body)
assert repeat.status_code == 202 and repeat.json() == {
"delivery_id": delivery, "duplicate_delivery": True, "job_created": False
}, repeat.text
assert send(payload(pr + 1)).status_code == 409
second = send(body, delivery_id=str(uuid4()))
assert second.status_code == 202 and not second.json()["job_created"], second.text
assert client.get(f"/demo/effects/{pr}", headers=WORKER).json() == {
"deliveries": 2, "jobs": 1, "effects": 0
}
assert client.get(f"/demo/effects/{pr+1}", headers=WORKER).json() == {
"deliveries": 0, "jobs": 0, "effects": 0
}
assert client.post("/demo/work").status_code == 401
assert client.post("/demo/work", headers=WORKER).json() == {"processed": 1}
assert client.post("/demo/work", headers=WORKER).json() == {"processed": 0}
assert client.get(f"/demo/effects/{pr}", headers=WORKER).json() == {
"deliveries": 2, "jobs": 1, "effects": 1
}
print("PASS: invalid requests wrote nothing; repeat delivery and separate notification created one job.")
print("PASS: before work there were zero effects; after two worker calls there was exactly one.")
if __name__ == "__main__":
test()