|
| 1 | +""" |
| 2 | +Tests for Site24x7 provider bug fixes. |
| 3 | +
|
| 4 | +Bug 1 (issue #6195): STATUS "UP" was never mapped to AlertStatus.RESOLVED — |
| 5 | + _format_alert() passed no `status` argument to AlertDto, so it always |
| 6 | + defaulted to AlertStatus.FIRING via the root_validator fallback. |
| 7 | +
|
| 8 | +Bug 2 (issue #6196): TAGS field from the webhook payload was never read — |
| 9 | + _format_alert() ignored TAGS entirely, so AlertDto.labels was always {}. |
| 10 | +""" |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from keep.providers.site24x7_provider.site24x7_provider import Site24X7Provider |
| 15 | + |
| 16 | + |
| 17 | +def _minimal_event(**kwargs) -> dict: |
| 18 | + """Return a minimal valid Site24x7 webhook payload, with optional overrides.""" |
| 19 | + base = { |
| 20 | + "MONITORURL": "https://example.com", |
| 21 | + "INCIDENT_TIME_ISO": "2026-04-04T10:00:00Z", |
| 22 | + "INCIDENT_REASON": "Connection refused", |
| 23 | + "MONITORNAME": "Website - example.com", |
| 24 | + "MONITOR_ID": "12345", |
| 25 | + "STATUS": "DOWN", |
| 26 | + "TAGS": "", |
| 27 | + } |
| 28 | + base.update(kwargs) |
| 29 | + return base |
| 30 | + |
| 31 | + |
| 32 | +class TestStatusMapping: |
| 33 | + """Bug #6195: STATUS field must drive AlertDto.status, not just severity.""" |
| 34 | + |
| 35 | + def test_status_up_resolves_alert(self): |
| 36 | + """STATUS 'UP' (monitor recovered) must produce AlertStatus.RESOLVED.""" |
| 37 | + alert = Site24X7Provider._format_alert(_minimal_event(STATUS="UP")) |
| 38 | + assert alert.status == "resolved" |
| 39 | + |
| 40 | + def test_status_down_fires_alert(self): |
| 41 | + """STATUS 'DOWN' must produce AlertStatus.FIRING.""" |
| 42 | + alert = Site24X7Provider._format_alert(_minimal_event(STATUS="DOWN")) |
| 43 | + assert alert.status == "firing" |
| 44 | + |
| 45 | + def test_status_trouble_fires_alert(self): |
| 46 | + """STATUS 'TROUBLE' must produce AlertStatus.FIRING.""" |
| 47 | + alert = Site24X7Provider._format_alert(_minimal_event(STATUS="TROUBLE")) |
| 48 | + assert alert.status == "firing" |
| 49 | + |
| 50 | + def test_status_critical_fires_alert(self): |
| 51 | + """STATUS 'CRITICAL' must produce AlertStatus.FIRING.""" |
| 52 | + alert = Site24X7Provider._format_alert(_minimal_event(STATUS="CRITICAL")) |
| 53 | + assert alert.status == "firing" |
| 54 | + |
| 55 | + def test_status_unknown_defaults_to_firing(self): |
| 56 | + """Unknown STATUS values must fall back to AlertStatus.FIRING.""" |
| 57 | + alert = Site24X7Provider._format_alert(_minimal_event(STATUS="SOMETHING_NEW")) |
| 58 | + assert alert.status == "firing" |
| 59 | + |
| 60 | + def test_status_up_preserves_info_severity(self): |
| 61 | + """STATUS 'UP' must still map severity to INFO (existing SEVERITIES_MAP).""" |
| 62 | + alert = Site24X7Provider._format_alert(_minimal_event(STATUS="UP")) |
| 63 | + assert alert.severity == "info" |
| 64 | + |
| 65 | + |
| 66 | +class TestTagsParsing: |
| 67 | + """Bug #6196: TAGS field must be parsed and stored in AlertDto.labels.""" |
| 68 | + |
| 69 | + def test_tags_key_value_pairs(self): |
| 70 | + """'env:prod,team:backend' must produce {'env': 'prod', 'team': 'backend'}.""" |
| 71 | + alert = Site24X7Provider._format_alert( |
| 72 | + _minimal_event(TAGS="env:prod,team:backend") |
| 73 | + ) |
| 74 | + assert alert.labels == {"env": "prod", "team": "backend"} |
| 75 | + |
| 76 | + def test_tags_empty_string(self): |
| 77 | + """Empty TAGS string must produce an empty labels dict.""" |
| 78 | + alert = Site24X7Provider._format_alert(_minimal_event(TAGS="")) |
| 79 | + assert alert.labels == {} |
| 80 | + |
| 81 | + def test_tags_field_absent(self): |
| 82 | + """Missing TAGS key in payload must produce an empty labels dict.""" |
| 83 | + event = _minimal_event() |
| 84 | + event.pop("TAGS") |
| 85 | + alert = Site24X7Provider._format_alert(event) |
| 86 | + assert alert.labels == {} |
| 87 | + |
| 88 | + def test_tags_plain_name_without_value(self): |
| 89 | + """Tag without ':' separator must map to {'tagname': 'tagname'}.""" |
| 90 | + alert = Site24X7Provider._format_alert(_minimal_event(TAGS="tagname")) |
| 91 | + assert alert.labels == {"tagname": "tagname"} |
| 92 | + |
| 93 | + def test_tags_mixed_plain_and_kv(self): |
| 94 | + """Mix of plain tags and key:value tags must all be captured.""" |
| 95 | + alert = Site24X7Provider._format_alert( |
| 96 | + _minimal_event(TAGS="env:prod,critical") |
| 97 | + ) |
| 98 | + assert alert.labels == {"env": "prod", "critical": "critical"} |
| 99 | + |
| 100 | + def test_tags_value_containing_colon(self): |
| 101 | + """Tag value may itself contain ':' — only split on the first one.""" |
| 102 | + alert = Site24X7Provider._format_alert( |
| 103 | + _minimal_event(TAGS="url:https://example.com") |
| 104 | + ) |
| 105 | + assert alert.labels == {"url": "https://example.com"} |
| 106 | + |
| 107 | + def test_tags_whitespace_trimmed(self): |
| 108 | + """Spaces around tag names and values must be stripped.""" |
| 109 | + alert = Site24X7Provider._format_alert( |
| 110 | + _minimal_event(TAGS=" env : prod , team : backend ") |
| 111 | + ) |
| 112 | + assert alert.labels == {"env": "prod", "team": "backend"} |
| 113 | + |
| 114 | + def test_tags_as_dict_passed_through(self): |
| 115 | + """If TAGS is already a dict, it must be used as-is.""" |
| 116 | + alert = Site24X7Provider._format_alert( |
| 117 | + _minimal_event(TAGS={"env": "prod", "team": "backend"}) |
| 118 | + ) |
| 119 | + assert alert.labels == {"env": "prod", "team": "backend"} |
| 120 | + |
| 121 | + |
| 122 | +class TestFormatAlertFields: |
| 123 | + """Verify that the other AlertDto fields are still mapped correctly.""" |
| 124 | + |
| 125 | + def test_source_is_site24x7(self): |
| 126 | + """source must always be ['site24x7'].""" |
| 127 | + alert = Site24X7Provider._format_alert(_minimal_event()) |
| 128 | + assert alert.source == ["site24x7"] |
| 129 | + |
| 130 | + def test_basic_fields_mapped(self): |
| 131 | + """Core fields from the webhook payload must be present.""" |
| 132 | + alert = Site24X7Provider._format_alert(_minimal_event()) |
| 133 | + assert "example.com" in str(alert.url) |
| 134 | + assert alert.name == "Website - example.com" |
| 135 | + assert alert.id == "12345" |
| 136 | + assert alert.description == "Connection refused" |
| 137 | + assert alert.lastReceived.startswith("2026-04-04T10:00:00") |
0 commit comments