"""Integration: Send test on an alert rule (#5466). The button on a rule's page, and send_test_alert over MCP, send a TEST alert for that rule now, by every channel a real alert of it would use, and say what happened to each. Nothing fires and the rule's history is unchanged; the audit log records the test. Only the delivery itself is replaced. """ from __future__ import annotations import asyncio import json import os import time import uuid from zoneinfo import ZoneInfo import pytest pytestmark = pytest.mark.integration @pytest.fixture def app(): if not os.environ.get("STEWARD_DATABASE_URL"): pytest.skip("needs Postgres") from steward.app import create_app return create_app(testing=False) def test_send_test_reaches_each_channel_and_leaves_the_rule_alone(app, monkeypatch): from sqlalchemy import func, select from steward.alerts import service as alert_svc from steward.core import mail, notifications from steward.core.alerts import record_metric from steward.core.api_tokens import Caller from steward.mcp import _context from steward.mcp.tools import alerts as mcp_alerts from steward.models.alerts import AlertEvent, AlertState, AlertStateEnum from steward.models.audit import AuditEvent from steward.models.users import User, UserRole from tests.integration._mail import capture hooks = [] async def fake_hook(cfg, alert): hooks.append(alert) return {"sent": False, "error": "connection refused"} async def fake_recipients(db, kind, smtp): return [mail.Recipient("ann@example.com", ZoneInfo("UTC"), False, "ann")] emails = capture(app, monkeypatch) monkeypatch.setattr(notifications, "_send_webhook", fake_hook) monkeypatch.setattr(mail, "recipients", fake_recipients) app.config["WEBHOOK"] = {"url": "https://hook.example/api/1"} async def _go(): tag = uuid.uuid4().hex[:8] host = f"st-{tag}" async with app.db_sessionmaker() as s: async with s.begin(): user = User(username=f"st-{tag}", email=f"st-{tag}@test", password_hash="x", role=UserRole.operator, is_active=True) s.add(user) await s.flush() await record_metric(s, "host_agent", host, "cpu_pct", 42.0) fields = await alert_svc.rule_fields( s, name=f"Hot {tag}", source_module="host_agent", resource_name=host, metric_name="cpu_pct", operator=">", threshold=90) rule = await alert_svc.add_rule(s, fields, created_by=user.id) uid, rule_id = user.id, rule.id client = app.test_client() async with client.session_transaction() as sess: sess.update(user_id=uid, user_role="operator", username=f"st-{tag}", _csrf="test-token", login_at=time.time()) resp = await client.post(f"/alerts/rules/{rule_id}/test", headers={"X-CSRF-Token": "test-token"}) assert resp.status_code == 200 page = await resp.get_data(as_text=True) assert "alert-error" in page assert "Email: Sent to ann@example.com." in page assert "Webhook to hook.example failed: connection refused" in page # What went out is this rule, marked TEST, at its latest reading: one # queued test email (recorded, not retried) and the webhook. (msg,) = emails (alert,) = hooks assert msg["To"] == "ann@example.com" and f"Hot {tag}" in msg["Subject"] from steward.models.outbox import EmailOutbox async with app.db_sessionmaker() as s: row = (await s.execute(select(EmailOutbox).where( EmailOutbox.recipient == "ann@example.com", EmailOutbox.kind == "test", EmailOutbox.subject == msg["Subject"]))).scalars().one() assert (row.state, row.retry, row.attempts) == ("sent", False, 1) assert (alert["rule_name"], alert["state"], alert["resource"], alert["value"]) == ( f"Hot {tag}", "TEST", host, 42.0) # Nothing fired: no event, the alert still inactive. The audit log has it. async with app.db_sessionmaker() as s: events = (await s.execute(select(func.count()).select_from(AlertEvent) .where(AlertEvent.rule_id == rule_id))).scalar_one() state = (await s.execute(select(AlertState).where( AlertState.rule_id == rule_id))).scalar_one() audits = (await s.execute(select(AuditEvent).where( AuditEvent.user_id == uid))).scalars().all() assert events == 0 and state.state == AlertStateEnum.inactive assert [a.action for a in audits] == ["alert_rule.tested"] assert json.loads(audits[0].detail_json)["ok"] is False # Over MCP, with nothing set up to send to, it says so. app.config["SMTP"], app.config["WEBHOOK"] = {}, {} _context._app_ctx.set(app) _context._caller_ctx.set(Caller(user_id=uid, username=f"st-{tag}", role=UserRole.operator, token_id=7)) got = await mcp_alerts.send_test_alert(f"Hot {tag}") assert got["ok"] is False and got["result"][0].startswith("Nothing is set up to send to") assert len(emails) == 1 and len(hooks) == 1 asyncio.run(_go())