Files
PTah 79a78dc7c9 feat: Telegram settings in DB with UI edit and test send
Store notification_channels (migration 004), effective config DB over env,
PUT/test API endpoints, Settings form, and pass db session into ingest notify.

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

77 lines
2.4 KiB
Python

"""Tests for SAC Telegram notification helpers."""
from datetime import datetime, timezone
from unittest.mock import patch
from app.models import Event, Problem
from app.services import telegram_notify
from app.services.notification_settings import TelegramConfig
def _telegram_cfg(*, min_severity: str) -> TelegramConfig:
return TelegramConfig(
enabled=True,
bot_token="test-token",
chat_id="123",
min_severity=min_severity,
source="env",
)
def test_notify_event_respects_min_severity():
sent: list[str] = []
def fake_send(message: str, **kwargs) -> None:
sent.append(message)
event = Event(
event_id="00000000-0000-4000-8000-000000000001",
host_id=1,
occurred_at=datetime(2026, 5, 28, 12, 0, tzinfo=timezone.utc),
category="auth",
type="rdp.login.success",
severity="info",
title="ok",
summary="info event",
payload={},
)
cfg = _telegram_cfg(min_severity="warning")
with patch.object(telegram_notify, "get_effective_telegram_config", return_value=cfg):
with patch.object(telegram_notify, "send_telegram_text", side_effect=fake_send):
telegram_notify.notify_event(event)
assert sent == []
event.severity = "warning"
with patch.object(telegram_notify, "get_effective_telegram_config", return_value=cfg):
with patch.object(telegram_notify, "send_telegram_text", side_effect=fake_send):
telegram_notify.notify_event(event)
assert len(sent) == 1
def test_notify_problem_respects_min_severity():
sent: list[str] = []
def fake_send(message: str, **kwargs) -> None:
sent.append(message)
problem = Problem(
title="brute",
summary="burst",
severity="warning",
status="open",
fingerprint="fp1",
)
cfg = _telegram_cfg(min_severity="high")
with patch.object(telegram_notify, "get_effective_telegram_config", return_value=cfg):
with patch.object(telegram_notify, "send_telegram_text", side_effect=fake_send):
telegram_notify.notify_problem(problem)
assert sent == []
problem.severity = "high"
with patch.object(telegram_notify, "get_effective_telegram_config", return_value=cfg):
with patch.object(telegram_notify, "send_telegram_text", side_effect=fake_send):
telegram_notify.notify_problem(problem)
assert len(sent) == 1