2501ecc4fe
Ensure server line, compact spacing, and one user per row when ingesting agent reports or sending SAC-generated daily reports. Co-authored-by: Cursor <cursoragent@cursor.com>
104 lines
3.6 KiB
Python
104 lines
3.6 KiB
Python
"""Unified daily report text format."""
|
||
|
||
from datetime import datetime, timezone
|
||
|
||
from app.models import Host
|
||
from app.services.daily_report_format import (
|
||
build_report_body,
|
||
enrich_stats_for_storage,
|
||
normalize_active_users_list,
|
||
normalize_report_body,
|
||
)
|
||
|
||
|
||
def test_build_report_body_ssh_matches_agent_layout():
|
||
host = Host(
|
||
hostname="haproxy",
|
||
display_name="HaProxy Kalina",
|
||
ipv4="192.168.160.117",
|
||
os_family="linux",
|
||
product="ssh-monitor",
|
||
)
|
||
when = datetime(2026, 5, 29, 9, 0, 5, tzinfo=timezone.utc)
|
||
stats = enrich_stats_for_storage(
|
||
"ssh",
|
||
{
|
||
"successful_ssh": 0,
|
||
"failed_ssh": 0,
|
||
"sudo_commands": 5,
|
||
"active_bans": 0,
|
||
"top_failed_ips": [],
|
||
"active_users": [
|
||
"👤 papatramp | pts/0 | с 2026-05-27 12:28 | 🌐 192.168.160.3",
|
||
],
|
||
},
|
||
)
|
||
body = build_report_body("ssh", host, stats, when, sac_generated=True)
|
||
assert "ЕЖЕДНЕВНЫЙ ОТЧЕТ SSH МОНИТОРИНГА" in body
|
||
assert "🖥️ Сервер: HaProxy Kalina (192.168.160.117)" in body
|
||
assert " 📈 СТАТИСТИКА" in body
|
||
assert "Команд через sudo: 5" in body
|
||
assert " 👥 АКТИВНЫЕ ПОЛЬЗОВАТЕЛИ (1)" in body
|
||
assert "papatramp" in body
|
||
assert "\n\n\n" not in body
|
||
|
||
|
||
def test_build_report_body_windows_layout():
|
||
host = Host(
|
||
hostname="WIN01",
|
||
display_name="K6A-DC3",
|
||
ipv4="10.0.0.10",
|
||
os_family="windows",
|
||
product="rdp-login-monitor",
|
||
)
|
||
when = datetime(2026, 5, 29, 9, 0, 5, tzinfo=timezone.utc)
|
||
stats = enrich_stats_for_storage(
|
||
"windows",
|
||
{
|
||
"rdp_success": 1,
|
||
"rdp_failed": 2,
|
||
"active_bans": 0,
|
||
"top_failed_ips": ["1.2.3.4 — 2"],
|
||
"active_users": ["👤 DOMAIN\\user1", "👤 user2"],
|
||
},
|
||
)
|
||
body = build_report_body("windows", host, stats, when, sac_generated=True)
|
||
assert "ЕЖЕДНЕВНЫЙ ОТЧЕТ МОНИТОРИНГА WINDOWS" in body
|
||
assert "K6A-DC3 (10.0.0.10)" in body
|
||
assert "Успешных RDP подключений: 1" in body
|
||
assert " 👥 АКТИВНЫЕ ПОЛЬЗОВАТЕЛИ (2)" in body
|
||
assert "user2" in body
|
||
lines = [ln for ln in body.split("\n") if ln.strip().startswith("👤")]
|
||
assert len(lines) == 2
|
||
|
||
|
||
def test_normalize_active_users_list_splits_combined_line():
|
||
users = normalize_active_users_list(["👤 k.khodasevich 👤 papatramp"])
|
||
assert len(users) == 2
|
||
assert users[0].strip().startswith("👤 k.khodasevich")
|
||
assert users[1].strip().startswith("👤 papatramp")
|
||
|
||
|
||
def test_normalize_report_body_adds_server_and_collapses_blanks():
|
||
host = Host(hostname="srv", display_name="Unimus Kalina", ipv4="192.168.160.17")
|
||
raw = "\n".join(
|
||
[
|
||
"📊 ЕЖЕДНЕВНЫЙ ОТЧЕТ SSH МОНИТОРИНГА",
|
||
"",
|
||
"",
|
||
"🕐 Время отчета: 30.05.2026 09:00:00",
|
||
"",
|
||
"",
|
||
" 📈 СТАТИСТИКА ЗА ПОСЛЕДНИЕ 24 ЧАСА:",
|
||
" ✅ Успешных SSH подключений: 0",
|
||
"",
|
||
" 👥 АКТИВНЫЕ ПОЛЬЗОВАТЕЛИ (2):",
|
||
" 👤 u1 👤 u2",
|
||
]
|
||
)
|
||
body = normalize_report_body(raw, host, "ssh")
|
||
assert "🖥️ Сервер: Unimus Kalina (192.168.160.17)" in body
|
||
assert "\n\n\n" not in body
|
||
user_lines = [ln for ln in body.split("\n") if ln.strip().startswith("👤")]
|
||
assert len(user_lines) == 2
|