-
Notifications
You must be signed in to change notification settings - Fork 0
Ensure webhook sync uses independent sessions #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,14 @@ | |
| import hmac | ||
| import json | ||
| import re | ||
| import uuid | ||
|
|
||
| import pytest | ||
| from fastapi.testclient import TestClient | ||
| from sqlalchemy import text | ||
|
|
||
| from api.main import app | ||
| from api.routes_sync import settings as sync_settings | ||
|
|
||
|
|
||
| def _sig(secret: str, body: bytes) -> str: | ||
|
|
@@ -16,8 +19,7 @@ def _sig(secret: str, body: bytes) -> str: | |
|
|
||
|
|
||
| @pytest.fixture() | ||
| def client(monkeypatch): | ||
| monkeypatch.setenv("HYGRAPH_WEBHOOK_SECRET", "whsec") | ||
| def client(): | ||
| return TestClient(app) | ||
|
|
||
|
|
||
|
|
@@ -30,8 +32,9 @@ async def fake_pull_all(db, page_size=None): | |
|
|
||
| monkeypatch.setattr(svc.HygraphService, "pull_all", fake_pull_all) | ||
|
|
||
| body = json.dumps({"ping": "ok"}).encode() | ||
| sig = _sig("whsec", body) | ||
| unique_payload = {"ping": str(uuid.uuid4())} | ||
| body = json.dumps(unique_payload).encode() | ||
| sig = _sig(sync_settings.hygraph_webhook_secret, body) | ||
|
|
||
| # First delivery -> 202 Accepted, background processing logs counts | ||
| r1 = client.post("/api/sync/hygraph", data=body, headers={"x-hygraph-signature": sig}) | ||
|
|
@@ -55,6 +58,28 @@ async def fake_pull_all(db, page_size=None): | |
| assert found, "missing structured sync summary log" | ||
|
|
||
|
|
||
| def test_webhook_background_uses_new_session(client, caplog, monkeypatch): | ||
| from services import hygraph_service as svc | ||
|
|
||
| calls: list[bool] = [] | ||
|
|
||
| async def fake_pull_all(db, page_size=None): | ||
| db.execute(text("SELECT 1")) | ||
| calls.append(db.is_active) | ||
| return {"materials": 0, "modules": 0, "systems": 0} | ||
|
|
||
| monkeypatch.setattr(svc.HygraphService, "pull_all", fake_pull_all) | ||
|
|
||
| body = json.dumps({"ping": str(uuid.uuid4())}).encode() | ||
| sig = _sig(sync_settings.hygraph_webhook_secret, body) | ||
|
|
||
| r = client.post("/api/sync/hygraph", data=body, headers={"x-hygraph-signature": sig}) | ||
| assert r.status_code == 202 | ||
| assert r.json()["ok"] is True | ||
| assert calls == [True] | ||
| assert not any(rec.getMessage() == "hygraph_webhook_failure" for rec in caplog.records) | ||
|
|
||
|
||
|
|
||
| def test_pull_alias_and_page_size_validation(client, monkeypatch): | ||
| from services import hygraph_service as svc | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.