chore: commit all pending changes
This commit is contained in:
@@ -5,6 +5,12 @@ from pathlib import Path
|
||||
from urllib import request
|
||||
|
||||
|
||||
def _payload_for_webhook(payload: dict) -> dict:
|
||||
outbound = dict(payload)
|
||||
outbound.pop("tracks", None)
|
||||
return outbound
|
||||
|
||||
|
||||
def dispatch_json_event(
|
||||
path: str | Path,
|
||||
payload: dict,
|
||||
@@ -21,7 +27,7 @@ def dispatch_json_event(
|
||||
|
||||
req = request.Request(
|
||||
url=webhook_url,
|
||||
data=json.dumps(payload).encode("utf-8"),
|
||||
data=json.dumps(_payload_for_webhook(payload)).encode("utf-8"),
|
||||
method="POST",
|
||||
)
|
||||
req.add_header("Content-Type", "application/json")
|
||||
|
||||
52
managed/people_flow_project/tests/test_webhook.py
Normal file
52
managed/people_flow_project/tests/test_webhook.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
from src.people_flow.webhook import dispatch_json_event
|
||||
|
||||
|
||||
def test_dispatch_json_event_omits_tracks_from_webhook_but_keeps_local_log(
|
||||
tmp_path, monkeypatch
|
||||
):
|
||||
sent: dict[str, object] = {}
|
||||
|
||||
class DummyResponse:
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc, tb):
|
||||
return False
|
||||
|
||||
def fake_urlopen(req, timeout):
|
||||
sent["url"] = req.full_url
|
||||
sent["timeout"] = timeout
|
||||
sent["payload"] = json.loads(req.data.decode("utf-8"))
|
||||
return DummyResponse()
|
||||
|
||||
monkeypatch.setattr("src.people_flow.webhook.request.urlopen", fake_urlopen)
|
||||
|
||||
output = tmp_path / "logs" / "events.jsonl"
|
||||
payload = {
|
||||
"event": "half_hour_report",
|
||||
"total_people": 3,
|
||||
"tracks": [
|
||||
{"track_id": 1, "direction": "in"},
|
||||
{"track_id": 2, "direction": "out"},
|
||||
],
|
||||
}
|
||||
|
||||
dispatch_json_event(
|
||||
output,
|
||||
payload,
|
||||
webhook_url="https://example.test/webhook",
|
||||
timeout_seconds=7.5,
|
||||
)
|
||||
|
||||
lines = output.read_text(encoding="utf-8").splitlines()
|
||||
assert json.loads(lines[0]) == payload
|
||||
assert sent["url"] == "https://example.test/webhook"
|
||||
assert sent["timeout"] == 7.5
|
||||
assert sent["payload"] == {
|
||||
"event": "half_hour_report",
|
||||
"total_people": 3,
|
||||
}
|
||||
Reference in New Issue
Block a user