Skip to content

Commit 84bcc6e

Browse files
authored
Fix webhook sync session usage and add regression test (#90)
1 parent 5cde972 commit 84bcc6e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

backend/api/security.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313

1414
from api.config import Settings, get_settings
1515

16+
17+
def _load_expected_write_token(settings: Settings) -> str:
18+
"""Resolve the configured API write token from settings or environment."""
19+
20+
token = getattr(settings, "api_write_token", None)
21+
if isinstance(token, str) and token.strip():
22+
return token.strip()
23+
env_token = os.getenv("API_WRITE_TOKEN", "")
24+
return env_token.strip()
25+
26+
1627
# Write token (admin) auth
1728
api_key_header = APIKeyHeader(name="Authorization", auto_error=False)
1829

backend/tests/test_sync_routes_metrics.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import hmac
55
import json
66
import re
7+
import uuid
78

89
import pytest
910
from fastapi.testclient import TestClient
1011
from sqlalchemy import text
1112

1213
from api.config import get_settings
1314
from api.main import app
15+
from api.routes_sync import settings as sync_settings
1416

1517

1618
def _sig(secret: str, body: bytes) -> str:
@@ -37,8 +39,9 @@ async def fake_pull_all(db, page_size=None):
3739

3840
monkeypatch.setattr(svc.HygraphService, "pull_all", fake_pull_all)
3941

40-
body = json.dumps({"ping": "ok"}).encode()
41-
sig = _sig("whsec", body)
42+
unique_payload = {"ping": str(uuid.uuid4())}
43+
body = json.dumps(unique_payload).encode()
44+
sig = _sig(sync_settings.hygraph_webhook_secret, body)
4245

4346
# First delivery -> 202 Accepted, background processing logs counts
4447
r1 = client.post("/api/sync/hygraph", data=body, headers={"x-hygraph-signature": sig})

0 commit comments

Comments
 (0)