43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
import pytest
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from glitchup_bot.api.app import app
|
|
from glitchup_bot.config import clear_settings_cache
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_webhook_rejects_invalid_secret(monkeypatch):
|
|
monkeypatch.setenv("WEBHOOK_SECRET", "expected-secret")
|
|
clear_settings_cache()
|
|
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://testserver") as client:
|
|
response = await client.post(
|
|
"/webhooks/glitchtip", json={"text": "GlitchTip Alert", "attachments": []}
|
|
)
|
|
|
|
assert response.status_code == 403
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_webhook_accepts_valid_secret(monkeypatch):
|
|
received = []
|
|
|
|
async def fake_process(payload: dict) -> None:
|
|
received.append(payload)
|
|
|
|
monkeypatch.setenv("WEBHOOK_SECRET", "expected-secret")
|
|
clear_settings_cache()
|
|
monkeypatch.setattr("glitchup_bot.api.webhook.process_webhook_payload", fake_process)
|
|
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://testserver") as client:
|
|
response = await client.post(
|
|
"/webhooks/glitchtip",
|
|
headers={"X-Webhook-Secret": "expected-secret"},
|
|
json={"text": "GlitchTip Alert", "attachments": []},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert received == [{"text": "GlitchTip Alert", "attachments": []}]
|