| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
import logging |
| 29 |
from unittest import mock |
| 30 |
|
| 31 |
from helpers import AppriseURLTester |
| 32 |
import pytest |
| 33 |
import requests |
| 34 |
|
| 35 |
import apprise |
| 36 |
from apprise.plugins.trigv import NotifyTrigv |
| 37 |
|
| 38 |
logging.disable(logging.CRITICAL) |
| 39 |
|
| 40 |
VALID_API_KEY = "trgv_a1b2c3d4_0123456789abcdef0123456789abcdef" |
| 41 |
|
| 42 |
apprise_url_tests = ( |
| 43 |
( |
| 44 |
"trigvs://", |
| 45 |
{ |
| 46 |
"instance": TypeError, |
| 47 |
}, |
| 48 |
), |
| 49 |
( |
| 50 |
"trigv://", |
| 51 |
{ |
| 52 |
"instance": TypeError, |
| 53 |
}, |
| 54 |
), |
| 55 |
( |
| 56 |
f"trigvs://{VALID_API_KEY}", |
| 57 |
{ |
| 58 |
"instance": NotifyTrigv, |
| 59 |
}, |
| 60 |
), |
| 61 |
( |
| 62 |
f"trigvs://{VALID_API_KEY}/general", |
| 63 |
{ |
| 64 |
"instance": NotifyTrigv, |
| 65 |
}, |
| 66 |
), |
| 67 |
( |
| 68 |
f"trigvs://{VALID_API_KEY}/deploys", |
| 69 |
{ |
| 70 |
"instance": NotifyTrigv, |
| 71 |
}, |
| 72 |
), |
| 73 |
( |
| 74 |
f"trigv://{VALID_API_KEY}@trigv-platform.test/general", |
| 75 |
{ |
| 76 |
"instance": NotifyTrigv, |
| 77 |
}, |
| 78 |
), |
| 79 |
( |
| 80 |
f"trigv://{VALID_API_KEY}@trigv-platform.test:8080/general", |
| 81 |
{ |
| 82 |
"instance": NotifyTrigv, |
| 83 |
}, |
| 84 |
), |
| 85 |
( |
| 86 |
f"trigvs://{VALID_API_KEY}/alerts/ops", |
| 87 |
{ |
| 88 |
"instance": NotifyTrigv, |
| 89 |
}, |
| 90 |
), |
| 91 |
( |
| 92 |
f"trigvs://{VALID_API_KEY}/?to=alerts,ops", |
| 93 |
{ |
| 94 |
"instance": NotifyTrigv, |
| 95 |
}, |
| 96 |
), |
| 97 |
( |
| 98 |
f"trigvs://{VALID_API_KEY}/?url=https://example.com", |
| 99 |
{ |
| 100 |
"instance": NotifyTrigv, |
| 101 |
}, |
| 102 |
), |
| 103 |
( |
| 104 |
f"trigvs://{VALID_API_KEY}/?urgency=time_sensitive", |
| 105 |
{ |
| 106 |
"instance": NotifyTrigv, |
| 107 |
}, |
| 108 |
), |
| 109 |
( |
| 110 |
"trigvs://not-a-valid-key", |
| 111 |
{ |
| 112 |
"instance": TypeError, |
| 113 |
}, |
| 114 |
), |
| 115 |
( |
| 116 |
f"trigvs://{VALID_API_KEY}/bad.channel/", |
| 117 |
{ |
| 118 |
"instance": TypeError, |
| 119 |
}, |
| 120 |
), |
| 121 |
( |
| 122 |
f"trigvs://{VALID_API_KEY}", |
| 123 |
{ |
| 124 |
"instance": NotifyTrigv, |
| 125 |
"response": False, |
| 126 |
"requests_response_code": requests.codes.unauthorized, |
| 127 |
}, |
| 128 |
), |
| 129 |
( |
| 130 |
f"trigvs://{VALID_API_KEY}", |
| 131 |
{ |
| 132 |
"instance": NotifyTrigv, |
| 133 |
"response": False, |
| 134 |
"requests_response_code": requests.codes.unprocessable_entity, |
| 135 |
}, |
| 136 |
), |
| 137 |
( |
| 138 |
f"trigvs://{VALID_API_KEY}", |
| 139 |
{ |
| 140 |
"instance": NotifyTrigv, |
| 141 |
"test_requests_exceptions": True, |
| 142 |
}, |
| 143 |
), |
| 144 |
) |
| 145 |
|
| 146 |
|
| 147 |
def test_plugin_trigv_urls(): |
| 148 |
"""NotifyTrigv() Apprise URLs.""" |
| 149 |
|
| 150 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 151 |
|
| 152 |
|
| 153 |
@mock.patch("requests.post") |
| 154 |
def test_plugin_trigv_general(mock_post): |
| 155 |
"""NotifyTrigv() General Checks.""" |
| 156 |
|
| 157 |
response = mock.Mock() |
| 158 |
response.content = b'{"event":{"public_id":"evt_test","status":"queued"}}' |
| 159 |
response.status_code = requests.codes.accepted |
| 160 |
mock_post.return_value = response |
| 161 |
|
| 162 |
obj = apprise.Apprise.instantiate(f"trigvs://{VALID_API_KEY}/alerts") |
| 163 |
assert isinstance(obj, NotifyTrigv) |
| 164 |
assert obj.targets == ["alerts"] |
| 165 |
assert len(obj) == 1 |
| 166 |
|
| 167 |
assert ( |
| 168 |
obj.notify( |
| 169 |
body="Backup failed", |
| 170 |
title="Cron", |
| 171 |
notify_type=apprise.NotifyType.FAILURE, |
| 172 |
) |
| 173 |
is True |
| 174 |
) |
| 175 |
|
| 176 |
assert mock_post.call_count == 1 |
| 177 |
assert mock_post.call_args_list[0][0][0] == ( |
| 178 |
"https://api.trigv.com/api/v1/events" |
| 179 |
) |
| 180 |
|
| 181 |
headers = mock_post.call_args_list[0][1]["headers"] |
| 182 |
assert headers["Authorization"] == f"Bearer {VALID_API_KEY}" |
| 183 |
assert headers["Content-Type"] == "application/json" |
| 184 |
|
| 185 |
import json |
| 186 |
|
| 187 |
payload = json.loads(mock_post.call_args_list[0][1]["data"]) |
| 188 |
assert payload["channel"] == "alerts" |
| 189 |
assert payload["title"] == "Cron" |
| 190 |
assert payload["description"] == "Backup failed" |
| 191 |
assert payload["level"] == "error" |
| 192 |
assert payload["delivery_urgency"] == "time_sensitive" |
| 193 |
|
| 194 |
|
| 195 |
@mock.patch("requests.post") |
| 196 |
def test_plugin_trigv_local_host(mock_post): |
| 197 |
"""NotifyTrigv() custom hostname for local development.""" |
| 198 |
|
| 199 |
response = mock.Mock() |
| 200 |
response.status_code = requests.codes.accepted |
| 201 |
mock_post.return_value = response |
| 202 |
|
| 203 |
obj = apprise.Apprise.instantiate( |
| 204 |
f"trigv://{VALID_API_KEY}@trigv-platform.test/general" |
| 205 |
) |
| 206 |
assert isinstance(obj, NotifyTrigv) |
| 207 |
|
| 208 |
assert obj.notify(body="Hello", title="Test") is True |
| 209 |
assert ( |
| 210 |
mock_post.call_args_list[0][0][0] |
| 211 |
== "http://trigv-platform.test/api/v1/events" |
| 212 |
) |
| 213 |
|
| 214 |
|
| 215 |
@mock.patch("requests.post") |
| 216 |
def test_plugin_trigv_custom_port(mock_post): |
| 217 |
"""NotifyTrigv() custom hostname with an explicit port.""" |
| 218 |
|
| 219 |
response = mock.Mock() |
| 220 |
response.status_code = requests.codes.accepted |
| 221 |
mock_post.return_value = response |
| 222 |
|
| 223 |
obj = apprise.Apprise.instantiate( |
| 224 |
f"trigvs://{VALID_API_KEY}@trigv-platform.test:8443/general" |
| 225 |
) |
| 226 |
assert isinstance(obj, NotifyTrigv) |
| 227 |
assert obj.port == 8443 |
| 228 |
|
| 229 |
assert obj.notify(body="Hello", title="Test") is True |
| 230 |
assert ( |
| 231 |
mock_post.call_args_list[0][0][0] |
| 232 |
== "https://trigv-platform.test:8443/api/v1/events" |
| 233 |
) |
| 234 |
|
| 235 |
|
| 236 |
@mock.patch("requests.post") |
| 237 |
def test_plugin_trigv_multiple_channels(mock_post): |
| 238 |
"""NotifyTrigv() notifies every channel specified.""" |
| 239 |
|
| 240 |
response = mock.Mock() |
| 241 |
response.status_code = requests.codes.accepted |
| 242 |
mock_post.return_value = response |
| 243 |
|
| 244 |
obj = NotifyTrigv(api_key=VALID_API_KEY, targets=["alerts", "ops"]) |
| 245 |
assert obj.targets == ["alerts", "ops"] |
| 246 |
assert len(obj) == 2 |
| 247 |
|
| 248 |
assert obj.send(body="test", title="title") is True |
| 249 |
assert mock_post.call_count == 2 |
| 250 |
|
| 251 |
import json |
| 252 |
|
| 253 |
channels_notified = { |
| 254 |
json.loads(call[1]["data"])["channel"] |
| 255 |
for call in mock_post.call_args_list |
| 256 |
} |
| 257 |
assert channels_notified == {"alerts", "ops"} |
| 258 |
|
| 259 |
|
| 260 |
@mock.patch("requests.post") |
| 261 |
def test_plugin_trigv_multiple_channels_partial_failure(mock_post): |
| 262 |
"""NotifyTrigv() still tries every channel if one of them fails.""" |
| 263 |
|
| 264 |
ok_response = mock.Mock() |
| 265 |
ok_response.status_code = requests.codes.accepted |
| 266 |
|
| 267 |
error_response = mock.Mock() |
| 268 |
error_response.status_code = requests.codes.internal_server_error |
| 269 |
error_response.content = b"" |
| 270 |
|
| 271 |
mock_post.side_effect = [error_response, ok_response] |
| 272 |
|
| 273 |
obj = NotifyTrigv(api_key=VALID_API_KEY, targets=["alerts", "ops"]) |
| 274 |
assert obj.send(body="test", title="title") is False |
| 275 |
assert mock_post.call_count == 2 |
| 276 |
|
| 277 |
|
| 278 |
@mock.patch("requests.post") |
| 279 |
def test_plugin_trigv_multiple_channels_request_exception(mock_post): |
| 280 |
"""NotifyTrigv() marks a failure when one channel raises.""" |
| 281 |
|
| 282 |
ok_response = mock.Mock() |
| 283 |
ok_response.status_code = requests.codes.accepted |
| 284 |
|
| 285 |
mock_post.side_effect = [ |
| 286 |
requests.RequestException(), |
| 287 |
ok_response, |
| 288 |
] |
| 289 |
|
| 290 |
obj = NotifyTrigv(api_key=VALID_API_KEY, targets=["alerts", "ops"]) |
| 291 |
assert obj.send(body="test", title="title") is False |
| 292 |
assert mock_post.call_count == 2 |
| 293 |
|
| 294 |
|
| 295 |
@mock.patch("requests.post") |
| 296 |
def test_plugin_trigv_duplicate_success(mock_post): |
| 297 |
"""NotifyTrigv() treats HTTP 200 idempotent duplicate as success.""" |
| 298 |
|
| 299 |
response = mock.Mock() |
| 300 |
response.status_code = requests.codes.ok |
| 301 |
mock_post.return_value = response |
| 302 |
|
| 303 |
obj = NotifyTrigv(api_key=VALID_API_KEY) |
| 304 |
assert obj.send(body="test", title="title") is True |
| 305 |
|
| 306 |
|
| 307 |
@mock.patch("requests.post") |
| 308 |
def test_plugin_trigv_optional_payload_and_priority(mock_post): |
| 309 |
"""NotifyTrigv() includes optional fields and maps numeric priority.""" |
| 310 |
|
| 311 |
response = mock.Mock() |
| 312 |
response.status_code = requests.codes.accepted |
| 313 |
mock_post.return_value = response |
| 314 |
|
| 315 |
obj = NotifyTrigv( |
| 316 |
api_key=VALID_API_KEY, |
| 317 |
image_url="https://example.com/image.png", |
| 318 |
event_type="backup.failed", |
| 319 |
priority=1, |
| 320 |
) |
| 321 |
assert obj.send(body="", title="") is True |
| 322 |
|
| 323 |
import json |
| 324 |
|
| 325 |
payload = json.loads(mock_post.call_args_list[0][1]["data"]) |
| 326 |
assert payload["title"] == obj.app_desc |
| 327 |
assert payload["description"] == obj.app_desc |
| 328 |
assert payload["image_url"] == "https://example.com/image.png" |
| 329 |
assert payload["event_type"] == "backup.failed" |
| 330 |
assert payload["delivery_urgency"] == "time_sensitive" |
| 331 |
|
| 332 |
|
| 333 |
@mock.patch("requests.post") |
| 334 |
def test_plugin_trigv_empty_body_and_invalid_priority(mock_post): |
| 335 |
"""NotifyTrigv() omits an empty body and ignores invalid priority.""" |
| 336 |
|
| 337 |
response = mock.Mock() |
| 338 |
response.status_code = requests.codes.accepted |
| 339 |
mock_post.return_value = response |
| 340 |
|
| 341 |
obj = NotifyTrigv(api_key=VALID_API_KEY, priority="invalid") |
| 342 |
assert obj.send(body="", title="Title", notify_type="unknown") is True |
| 343 |
|
| 344 |
import json |
| 345 |
|
| 346 |
payload = json.loads(mock_post.call_args_list[0][1]["data"]) |
| 347 |
assert "description" not in payload |
| 348 |
assert payload["level"] == "info" |
| 349 |
assert payload["delivery_urgency"] == "standard" |
| 350 |
|
| 351 |
|
| 352 |
def test_plugin_trigv_invalid_urgency(): |
| 353 |
"""NotifyTrigv() rejects unknown urgency.""" |
| 354 |
|
| 355 |
with pytest.raises(TypeError): |
| 356 |
NotifyTrigv(api_key=VALID_API_KEY, urgency="critical") |
| 357 |
|
| 358 |
|
| 359 |
def test_plugin_trigv_to_alias(): |
| 360 |
"""NotifyTrigv() parses ?to= into additional targets.""" |
| 361 |
|
| 362 |
parsed = NotifyTrigv.parse_url( |
| 363 |
f"trigvs://{VALID_API_KEY}/alerts/?to=ops,escalation" |
| 364 |
) |
| 365 |
obj = NotifyTrigv(**parsed) |
| 366 |
|
| 367 |
assert obj.targets == ["alerts", "escalation", "ops"] |
| 368 |
|
| 369 |
|
| 370 |
@mock.patch("requests.post") |
| 371 |
def test_plugin_trigv_url_roundtrip(mock_post): |
| 372 |
"""NotifyTrigv() url() round-trips optional query parameters.""" |
| 373 |
|
| 374 |
mock_post.return_value = mock.Mock() |
| 375 |
mock_post.return_value.status_code = requests.codes.accepted |
| 376 |
|
| 377 |
obj = NotifyTrigv( |
| 378 |
api_key=VALID_API_KEY, |
| 379 |
targets="deploys", |
| 380 |
supplemental_url="https://example.com/run/1", |
| 381 |
image_url="https://example.com/image.png", |
| 382 |
urgency="time_sensitive", |
| 383 |
event_type="deploy.completed", |
| 384 |
priority=2, |
| 385 |
) |
| 386 |
|
| 387 |
generated = obj.url() |
| 388 |
assert "deploys" in generated |
| 389 |
assert "url=" in generated |
| 390 |
assert "image_url=" in generated |
| 391 |
assert "urgency=time_sensitive" in generated |
| 392 |
assert "event_type=deploy.completed" in generated |
| 393 |
assert "priority=2" in generated |
| 394 |
|
| 395 |
parsed = NotifyTrigv.parse_url(generated) |
| 396 |
obj2 = NotifyTrigv(**parsed) |
| 397 |
assert obj2.targets == ["deploys"] |
| 398 |
assert obj2.supplemental_url == "https://example.com/run/1" |
| 399 |
assert obj2.image_url == "https://example.com/image.png" |
| 400 |
assert obj2.urgency == "time_sensitive" |
| 401 |
assert obj2.event_type == "deploy.completed" |
| 402 |
assert obj2.priority == 2 |