Files
security-alert-center/backend/tests/test_webhook_notify.py
T
PapaTramp 20fa7e2c27 feat: webhook notification channel with UI and ingest dispatch
Add webhook config (DB/env), JSON POST on events/problems, settings API,
Settings UI section, and notify_dispatch for multi-channel ingest.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-29 16:09:40 +10:00

56 lines
1.7 KiB
Python

"""Tests for SAC webhook notification helpers."""
from datetime import datetime, timezone
from unittest.mock import patch
from app.models import Event
from app.services import webhook_notify
from app.services.notification_settings import WebhookConfig
def test_notify_event_respects_min_severity():
sent: list[dict] = []
def fake_send(payload: dict, **kwargs) -> None:
sent.append(payload)
event = Event(
event_id="00000000-0000-4000-8000-000000000201",
host_id=1,
occurred_at=datetime(2026, 5, 29, 12, 0, tzinfo=timezone.utc),
category="auth",
type="rdp.login.failed",
severity="warning",
title="failed",
summary="e2e",
payload={},
)
cfg = WebhookConfig(
enabled=True,
url="https://example.com/hook",
secret_header="",
secret="",
min_severity="high",
source="env",
)
with patch.object(webhook_notify, "get_effective_webhook_config", return_value=cfg):
with patch.object(webhook_notify, "send_webhook_payload", side_effect=fake_send):
webhook_notify.notify_event(event)
assert sent == []
cfg_high = WebhookConfig(
enabled=True,
url="https://example.com/hook",
secret_header="",
secret="",
min_severity="warning",
source="env",
)
event.severity = "warning"
with patch.object(webhook_notify, "get_effective_webhook_config", return_value=cfg_high):
with patch.object(webhook_notify, "send_webhook_payload", side_effect=fake_send):
webhook_notify.notify_event(event)
assert len(sent) == 1
assert sent[0]["kind"] == "event"