|
| 1 | +from datetime import datetime, timedelta |
| 2 | +from unittest.mock import AsyncMock |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +import components.controller.procedures.notifications_alert_solved as notifications_alert_solved |
| 7 | +from models import Alert, AlertStatus, Monitor, Notification, NotificationStatus |
| 8 | +from tests.test_utils import assert_message_in_log, assert_message_not_in_log |
| 9 | +from utils.time import now |
| 10 | + |
| 11 | +pytestmark = pytest.mark.asyncio(loop_scope="session") |
| 12 | + |
| 13 | + |
| 14 | +def get_time(reference: str) -> datetime | None: |
| 15 | + values = { |
| 16 | + "now": now(), |
| 17 | + "ten_seconds_ago": now() - timedelta(seconds=11), |
| 18 | + "five_minutes_ago": now() - timedelta(seconds=301), |
| 19 | + } |
| 20 | + return values.get(reference) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize("alert_status", [AlertStatus.active, AlertStatus.solved]) |
| 24 | +@pytest.mark.parametrize( |
| 25 | + "notification_status", [NotificationStatus.active, NotificationStatus.closed] |
| 26 | +) |
| 27 | +@pytest.mark.parametrize("alert_solved_at", [None, "now", "five_minutes_ago"]) |
| 28 | +async def test_notification_alert_solved( |
| 29 | + caplog, sample_monitor: Monitor, monkeypatch, alert_status, notification_status, alert_solved_at |
| 30 | +): |
| 31 | + """'_notification_alert_solved' should close notifications that are active but the alert is |
| 32 | + already solved""" |
| 33 | + alert = await Alert.create( |
| 34 | + monitor_id=sample_monitor.id, |
| 35 | + status=alert_status, |
| 36 | + solved_at=get_time(alert_solved_at), # type:ignore[assignment] |
| 37 | + ) |
| 38 | + notification = await Notification.create( |
| 39 | + monitor_id=sample_monitor.id, alert_id=alert.id, target="", status=notification_status |
| 40 | + ) |
| 41 | + |
| 42 | + if alert_status == AlertStatus.active: |
| 43 | + triggered = False |
| 44 | + elif notification_status == NotificationStatus.closed: |
| 45 | + triggered = False |
| 46 | + else: |
| 47 | + triggered = alert_solved_at == "five_minutes_ago" |
| 48 | + |
| 49 | + await notifications_alert_solved.notifications_alert_solved() |
| 50 | + |
| 51 | + await notification.refresh() |
| 52 | + if triggered: |
| 53 | + assert notification.status == NotificationStatus.closed |
| 54 | + assert notification.closed_at > now() - timedelta(seconds=1) |
| 55 | + assert_message_in_log(caplog, f"{notification} closed") |
| 56 | + else: |
| 57 | + assert notification.status == notification_status |
| 58 | + assert notification.closed_at is None |
| 59 | + assert_message_not_in_log(caplog, f"{notification} closed") |
| 60 | + |
| 61 | + |
| 62 | +async def test_notifications_alert_solved_query_result_none(caplog, monkeypatch): |
| 63 | + """'notifications_alert_solved' should log an error if the query result is None""" |
| 64 | + monkeypatch.setattr( |
| 65 | + notifications_alert_solved.databases, "query_application", AsyncMock(return_value=None) |
| 66 | + ) |
| 67 | + |
| 68 | + await notifications_alert_solved.notifications_alert_solved() |
| 69 | + |
| 70 | + assert_message_in_log(caplog, "Error with query result") |
| 71 | + |
| 72 | + |
| 73 | +async def test_notifications_alert_solved_monitor_not_found(caplog, monkeypatch): |
| 74 | + """'notifications_alert_solved' should log an error if the notification is not found""" |
| 75 | + monkeypatch.setattr( |
| 76 | + notifications_alert_solved.databases, |
| 77 | + "query_application", |
| 78 | + AsyncMock(return_value=[{"id": 99999999}]), |
| 79 | + ) |
| 80 | + |
| 81 | + await notifications_alert_solved.notifications_alert_solved() |
| 82 | + |
| 83 | + assert_message_in_log(caplog, "Notification with id '99999999' not found") |
| 84 | + |
| 85 | + |
| 86 | +async def test_notifications_alert_solved_monitor_not_found_2_results( |
| 87 | + caplog, monkeypatch, sample_monitor: Monitor |
| 88 | +): |
| 89 | + """'notifications_alert_solved' should log an error if one notification is not found but should |
| 90 | + continue with the other notifications""" |
| 91 | + alert = await Alert.create(monitor_id=sample_monitor.id, status=AlertStatus.active) |
| 92 | + notification = await Notification.create( |
| 93 | + monitor_id=sample_monitor.id, alert_id=alert.id, target="", status=NotificationStatus.active |
| 94 | + ) |
| 95 | + |
| 96 | + monkeypatch.setattr( |
| 97 | + notifications_alert_solved.databases, |
| 98 | + "query_application", |
| 99 | + AsyncMock(return_value=[{"id": 99999999}, {"id": notification.id}]), |
| 100 | + ) |
| 101 | + |
| 102 | + await notifications_alert_solved.notifications_alert_solved() |
| 103 | + |
| 104 | + assert_message_in_log(caplog, "Notification with id '99999999' not found") |
| 105 | + assert_message_in_log(caplog, f"{notification} closed") |
0 commit comments