Files
security-alert-center/backend/tests/test_telegram_notify.py
T

74 lines
2.2 KiB
Python

"""Tests for SAC Telegram notification helpers."""
from datetime import datetime, timezone
from unittest.mock import patch
import pytest
from app.models import Event, Host, Problem
from app.services import telegram_notify
def test_notify_event_respects_min_severity(monkeypatch):
sent: list[str] = []
def fake_send(message: str) -> None:
sent.append(message)
monkeypatch.setenv("TELEGRAM_ENABLED", "true")
monkeypatch.setenv("TELEGRAM_BOT_TOKEN", "test-token")
monkeypatch.setenv("TELEGRAM_CHAT_ID", "123")
monkeypatch.setenv("TELEGRAM_MIN_SEVERITY", "warning")
telegram_notify.get_settings.cache_clear()
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={},
)
with patch.object(telegram_notify, "_send_text", side_effect=fake_send):
telegram_notify.notify_event(event)
assert sent == []
event.severity = "warning"
with patch.object(telegram_notify, "_send_text", side_effect=fake_send):
telegram_notify.notify_event(event)
assert len(sent) == 1
def test_notify_problem_respects_min_severity(monkeypatch):
sent: list[str] = []
def fake_send(message: str) -> None:
sent.append(message)
monkeypatch.setenv("TELEGRAM_ENABLED", "true")
monkeypatch.setenv("TELEGRAM_BOT_TOKEN", "test-token")
monkeypatch.setenv("TELEGRAM_CHAT_ID", "123")
monkeypatch.setenv("TELEGRAM_MIN_SEVERITY", "high")
telegram_notify.get_settings.cache_clear()
problem = Problem(
title="brute",
summary="burst",
severity="warning",
status="open",
fingerprint="fp1",
)
with patch.object(telegram_notify, "_send_text", side_effect=fake_send):
telegram_notify.notify_problem(problem)
assert sent == []
problem.severity = "high"
with patch.object(telegram_notify, "_send_text", side_effect=fake_send):
telegram_notify.notify_problem(problem)
assert len(sent) == 1