| 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 |
|
| 29 |
import logging |
| 30 |
from unittest import mock |
| 31 |
from urllib.parse import urlparse |
| 32 |
|
| 33 |
from helpers import AppriseURLTester |
| 34 |
import pytest |
| 35 |
import requests |
| 36 |
|
| 37 |
from apprise.common import NotifyType |
| 38 |
from apprise.plugins.notifico import NotificoMode, NotifyNotifico |
| 39 |
|
| 40 |
logging.disable(logging.CRITICAL) |
| 41 |
|
| 42 |
|
| 43 |
apprise_url_tests = ( |
| 44 |
( |
| 45 |
"notifico://", |
| 46 |
{ |
| 47 |
"instance": TypeError, |
| 48 |
}, |
| 49 |
), |
| 50 |
( |
| 51 |
"notifico://:@/", |
| 52 |
{ |
| 53 |
"instance": TypeError, |
| 54 |
}, |
| 55 |
), |
| 56 |
( |
| 57 |
"notifico://1234", |
| 58 |
{ |
| 59 |
|
| 60 |
"instance": TypeError, |
| 61 |
}, |
| 62 |
), |
| 63 |
( |
| 64 |
"notifico://abcd/ckhrjW8w672m6HG", |
| 65 |
{ |
| 66 |
|
| 67 |
"instance": TypeError, |
| 68 |
}, |
| 69 |
), |
| 70 |
( |
| 71 |
"notifico://1234/ckhrjW8w672m6HG", |
| 72 |
{ |
| 73 |
|
| 74 |
"instance": NotifyNotifico, |
| 75 |
}, |
| 76 |
), |
| 77 |
( |
| 78 |
"notifico://1234/ckhrjW8w672m6HG?prefix=no", |
| 79 |
{ |
| 80 |
|
| 81 |
"instance": NotifyNotifico, |
| 82 |
}, |
| 83 |
), |
| 84 |
( |
| 85 |
"notifico://1234/ckhrjW8w672m6HG?color=yes", |
| 86 |
{ |
| 87 |
"instance": NotifyNotifico, |
| 88 |
"notify_type": "info", |
| 89 |
}, |
| 90 |
), |
| 91 |
( |
| 92 |
"notifico://1234/ckhrjW8w672m6HG?color=yes", |
| 93 |
{ |
| 94 |
"instance": NotifyNotifico, |
| 95 |
"notify_type": "success", |
| 96 |
}, |
| 97 |
), |
| 98 |
( |
| 99 |
"notifico://1234/ckhrjW8w672m6HG?color=yes", |
| 100 |
{ |
| 101 |
"instance": NotifyNotifico, |
| 102 |
"notify_type": "warning", |
| 103 |
}, |
| 104 |
), |
| 105 |
( |
| 106 |
"notifico://1234/ckhrjW8w672m6HG?color=yes", |
| 107 |
{ |
| 108 |
"instance": NotifyNotifico, |
| 109 |
"notify_type": "failure", |
| 110 |
}, |
| 111 |
), |
| 112 |
( |
| 113 |
"notifico://1234/ckhrjW8w672m6HG?color=yes", |
| 114 |
{ |
| 115 |
"instance": NotifyNotifico, |
| 116 |
"notify_type": "invalid", |
| 117 |
}, |
| 118 |
), |
| 119 |
( |
| 120 |
"notifico://1234/ckhrjW8w672m6HG?color=no", |
| 121 |
{ |
| 122 |
|
| 123 |
"instance": NotifyNotifico, |
| 124 |
|
| 125 |
"privacy_url": "notifico://1...4/c...G", |
| 126 |
}, |
| 127 |
), |
| 128 |
|
| 129 |
( |
| 130 |
"https://n.tkte.ch/h/2144/uJmKaBW9WFk42miB146ci3Kj", |
| 131 |
{ |
| 132 |
"instance": NotifyNotifico, |
| 133 |
}, |
| 134 |
), |
| 135 |
|
| 136 |
( |
| 137 |
"notifico://example.com/1234/ckhrjW8w672m6HG", |
| 138 |
{ |
| 139 |
"instance": NotifyNotifico, |
| 140 |
}, |
| 141 |
), |
| 142 |
|
| 143 |
( |
| 144 |
"notificos://example.com/1234/ckhrjW8w672m6HG", |
| 145 |
{ |
| 146 |
"instance": NotifyNotifico, |
| 147 |
"privacy_url": "notificos://example.com/1...4/c...G", |
| 148 |
}, |
| 149 |
), |
| 150 |
|
| 151 |
( |
| 152 |
"notifico://[email protected]:20/1234/ckhrjW8w672m6HG?color=yes", |
| 153 |
{ |
| 154 |
"instance": NotifyNotifico, |
| 155 |
"notify_type": "info", |
| 156 |
}, |
| 157 |
), |
| 158 |
|
| 159 |
( |
| 160 |
"notificos://user:[email protected]/1234/ckhrjW8w672m6HG?color=yes", |
| 161 |
{ |
| 162 |
"instance": NotifyNotifico, |
| 163 |
"notify_type": "success", |
| 164 |
"privacy_url": "notificos://user:****@example.com/1...4/c...G", |
| 165 |
}, |
| 166 |
), |
| 167 |
|
| 168 |
( |
| 169 |
"notificos://user:[email protected]/" |
| 170 |
"?project=1234&token=ckhrjW8w672m6HG&color=yes", |
| 171 |
{ |
| 172 |
"instance": NotifyNotifico, |
| 173 |
"notify_type": "success", |
| 174 |
"privacy_url": "notificos://user:****@example.com/1...4/c...G", |
| 175 |
}, |
| 176 |
), |
| 177 |
|
| 178 |
( |
| 179 |
"notifico://example.com/abcd/ckhrjW8w672m6HG", |
| 180 |
{ |
| 181 |
"instance": TypeError, |
| 182 |
}, |
| 183 |
), |
| 184 |
( |
| 185 |
"notifico://1234/ckhrjW8w672m6HG", |
| 186 |
{ |
| 187 |
"instance": NotifyNotifico, |
| 188 |
|
| 189 |
"include_image": False, |
| 190 |
}, |
| 191 |
), |
| 192 |
|
| 193 |
( |
| 194 |
"notifico://1234/ckhrjW8w672m6HG", |
| 195 |
{ |
| 196 |
"instance": NotifyNotifico, |
| 197 |
|
| 198 |
"response": False, |
| 199 |
"requests_response_code": requests.codes.internal_server_error, |
| 200 |
}, |
| 201 |
), |
| 202 |
( |
| 203 |
"notifico://1234/ckhrjW8w672m6HG", |
| 204 |
{ |
| 205 |
"instance": NotifyNotifico, |
| 206 |
|
| 207 |
"response": False, |
| 208 |
"requests_response_code": 999, |
| 209 |
}, |
| 210 |
), |
| 211 |
( |
| 212 |
"notifico://1234/ckhrjW8w672m6HG", |
| 213 |
{ |
| 214 |
"instance": NotifyNotifico, |
| 215 |
|
| 216 |
|
| 217 |
"test_requests_exceptions": True, |
| 218 |
}, |
| 219 |
), |
| 220 |
|
| 221 |
( |
| 222 |
"notifico://example.com/1234/ckhrjW8w672m6HG", |
| 223 |
{ |
| 224 |
"instance": NotifyNotifico, |
| 225 |
"response": False, |
| 226 |
"requests_response_code": requests.codes.internal_server_error, |
| 227 |
}, |
| 228 |
), |
| 229 |
( |
| 230 |
"notifico://example.com/1234/ckhrjW8w672m6HG", |
| 231 |
{ |
| 232 |
"instance": NotifyNotifico, |
| 233 |
"response": False, |
| 234 |
"requests_response_code": 999, |
| 235 |
}, |
| 236 |
), |
| 237 |
( |
| 238 |
"notifico://example.com/1234/ckhrjW8w672m6HG", |
| 239 |
{ |
| 240 |
"instance": NotifyNotifico, |
| 241 |
"test_requests_exceptions": True, |
| 242 |
}, |
| 243 |
), |
| 244 |
) |
| 245 |
|
| 246 |
|
| 247 |
def test_plugin_notifico_urls(): |
| 248 |
"""NotifyNotifico() Apprise URLs.""" |
| 249 |
|
| 250 |
|
| 251 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 252 |
|
| 253 |
|
| 254 |
@mock.patch("requests.get") |
| 255 |
def test_plugin_notifico_official_mode(mock_get): |
| 256 |
"""NotifyNotifico() official-mode init and round-trip.""" |
| 257 |
|
| 258 |
|
| 259 |
response = mock.Mock() |
| 260 |
response.status_code = requests.codes.ok |
| 261 |
mock_get.return_value = response |
| 262 |
|
| 263 |
|
| 264 |
obj = NotifyNotifico( |
| 265 |
project_id="2144", |
| 266 |
msghook="uJmKaBW9WFk42miB146ci3Kj", |
| 267 |
) |
| 268 |
assert obj.mode == NotificoMode.OFFICIAL |
| 269 |
assert not obj.host |
| 270 |
|
| 271 |
|
| 272 |
url = obj.url() |
| 273 |
assert "2144" in url |
| 274 |
assert "uJmKaBW9WFk42miB146ci3Kj" in url |
| 275 |
|
| 276 |
result = NotifyNotifico.parse_url(url) |
| 277 |
assert result is not None |
| 278 |
obj2 = NotifyNotifico(**result) |
| 279 |
assert obj2.url_identifier == obj.url_identifier |
| 280 |
|
| 281 |
|
| 282 |
assert obj.send(body="test") is True |
| 283 |
assert mock_get.call_count == 1 |
| 284 |
call_url = mock_get.call_args[0][0] |
| 285 |
assert "n.tkte.ch" in call_url |
| 286 |
assert "2144" in call_url |
| 287 |
assert "uJmKaBW9WFk42miB146ci3Kj" in call_url |
| 288 |
|
| 289 |
|
| 290 |
@mock.patch("requests.get") |
| 291 |
def test_plugin_notifico_selfhosted_http(mock_get): |
| 292 |
"""NotifyNotifico() self-hosted HTTP mode.""" |
| 293 |
|
| 294 |
response = mock.Mock() |
| 295 |
response.status_code = requests.codes.ok |
| 296 |
mock_get.return_value = response |
| 297 |
|
| 298 |
obj = NotifyNotifico( |
| 299 |
project_id="1234", |
| 300 |
msghook="ckhrjW8w672m6HG", |
| 301 |
host="myhost.local", |
| 302 |
) |
| 303 |
assert obj.mode == NotificoMode.SELFHOSTED |
| 304 |
assert obj.host == "myhost.local" |
| 305 |
assert obj.secure is False |
| 306 |
|
| 307 |
|
| 308 |
url = obj.url() |
| 309 |
assert url.startswith("notifico://myhost.local/1234/ckhrjW8w672m6HG") |
| 310 |
|
| 311 |
|
| 312 |
result = NotifyNotifico.parse_url(url) |
| 313 |
assert result is not None |
| 314 |
obj2 = NotifyNotifico(**result) |
| 315 |
assert obj2.url_identifier == obj.url_identifier |
| 316 |
|
| 317 |
|
| 318 |
assert obj.send(body="test") is True |
| 319 |
call_url = mock_get.call_args[0][0] |
| 320 |
parsed = urlparse(call_url) |
| 321 |
assert parsed.scheme == "http" |
| 322 |
assert parsed.hostname == "myhost.local" |
| 323 |
assert "1234" in call_url |
| 324 |
assert "ckhrjW8w672m6HG" in call_url |
| 325 |
|
| 326 |
|
| 327 |
@mock.patch("requests.get") |
| 328 |
def test_plugin_notifico_selfhosted_https(mock_get): |
| 329 |
"""NotifyNotifico() self-hosted HTTPS mode with auth and port.""" |
| 330 |
|
| 331 |
response = mock.Mock() |
| 332 |
response.status_code = requests.codes.ok |
| 333 |
mock_get.return_value = response |
| 334 |
|
| 335 |
|
| 336 |
result = NotifyNotifico.parse_url( |
| 337 |
"notificos://user:[email protected]:8443/9999/AbCdEfGh" |
| 338 |
) |
| 339 |
assert result is not None |
| 340 |
obj = NotifyNotifico(**result) |
| 341 |
|
| 342 |
assert obj.mode == NotificoMode.SELFHOSTED |
| 343 |
assert obj.secure is True |
| 344 |
assert obj.host == "myhost.local" |
| 345 |
assert obj.port == 8443 |
| 346 |
assert obj.user == "user" |
| 347 |
|
| 348 |
|
| 349 |
url = obj.url() |
| 350 |
assert url.startswith("notificos://user:") |
| 351 |
assert "myhost.local:8443" in url |
| 352 |
assert "9999" in url |
| 353 |
|
| 354 |
|
| 355 |
privacy = obj.url(privacy=True) |
| 356 |
assert "****" in privacy |
| 357 |
|
| 358 |
|
| 359 |
assert obj.send(body="test") is True |
| 360 |
call_url = mock_get.call_args[0][0] |
| 361 |
parsed = urlparse(call_url) |
| 362 |
assert parsed.scheme == "https" |
| 363 |
assert parsed.hostname == "myhost.local" |
| 364 |
assert parsed.port == 8443 |
| 365 |
assert "9999" in call_url |
| 366 |
assert "AbCdEfGh" in call_url |
| 367 |
|
| 368 |
|
| 369 |
@mock.patch("requests.get") |
| 370 |
def test_plugin_notifico_invalid_params(mock_get): |
| 371 |
"""NotifyNotifico() invalid init parameters raise TypeError.""" |
| 372 |
|
| 373 |
|
| 374 |
with pytest.raises(TypeError): |
| 375 |
NotifyNotifico(project_id="abc", msghook="validhook") |
| 376 |
|
| 377 |
|
| 378 |
with pytest.raises(TypeError): |
| 379 |
NotifyNotifico(project_id="1234", msghook="bad hook!") |
| 380 |
|
| 381 |
|
| 382 |
with pytest.raises(TypeError): |
| 383 |
NotifyNotifico(project_id=None, msghook="validhook") |
| 384 |
|
| 385 |
|
| 386 |
with pytest.raises(TypeError): |
| 387 |
NotifyNotifico(project_id="1234", msghook=None) |
| 388 |
|
| 389 |
|
| 390 |
assert mock_get.call_count == 0 |
| 391 |
|
| 392 |
|
| 393 |
@mock.patch("requests.get") |
| 394 |
def test_plugin_notifico_parse_url(mock_get): |
| 395 |
"""NotifyNotifico() parse_url edge cases.""" |
| 396 |
|
| 397 |
|
| 398 |
result = NotifyNotifico.parse_url("notifico://1234/hookABC") |
| 399 |
assert result is not None |
| 400 |
assert result["project_id"] == "1234" |
| 401 |
assert result["msghook"] == "hookABC" |
| 402 |
assert not result["host"] |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
result = NotifyNotifico.parse_url("notifico://user:pass@1234:8080/hookABC") |
| 408 |
assert result is not None |
| 409 |
assert result["project_id"] == "1234" |
| 410 |
assert result["msghook"] == "hookABC" |
| 411 |
assert not result["host"] |
| 412 |
assert result["port"] is None |
| 413 |
assert result["user"] is None |
| 414 |
assert result["password"] is None |
| 415 |
|
| 416 |
|
| 417 |
result = NotifyNotifico.parse_url("notifico://myhost.example/5678/hookXYZ") |
| 418 |
assert result is not None |
| 419 |
assert result["project_id"] == "5678" |
| 420 |
assert result["msghook"] == "hookXYZ" |
| 421 |
assert result["host"] == "myhost.example" |
| 422 |
|
| 423 |
|
| 424 |
result = NotifyNotifico.parse_url( |
| 425 |
"notificos://example.com/?project=9999&token=hookQQQ" |
| 426 |
) |
| 427 |
assert result is not None |
| 428 |
assert result["project_id"] == "9999" |
| 429 |
assert result["msghook"] == "hookQQQ" |
| 430 |
|
| 431 |
|
| 432 |
result = NotifyNotifico.parse_url("notifico://1234/hook") |
| 433 |
assert result["color"] is True |
| 434 |
assert result["prefix"] is True |
| 435 |
|
| 436 |
|
| 437 |
result = NotifyNotifico.parse_url( |
| 438 |
"notifico://1234/hook?color=no&prefix=no" |
| 439 |
) |
| 440 |
assert result["color"] is False |
| 441 |
assert result["prefix"] is False |
| 442 |
|
| 443 |
|
| 444 |
assert NotifyNotifico.parse_url(None) is None |
| 445 |
|
| 446 |
assert mock_get.call_count == 0 |
| 447 |
|
| 448 |
|
| 449 |
@mock.patch("requests.get") |
| 450 |
def test_plugin_notifico_send_http_errors(mock_get): |
| 451 |
"""NotifyNotifico() handles HTTP error responses gracefully.""" |
| 452 |
|
| 453 |
obj = NotifyNotifico(project_id="1234", msghook="validHook") |
| 454 |
|
| 455 |
|
| 456 |
response = mock.Mock() |
| 457 |
response.status_code = requests.codes.internal_server_error |
| 458 |
response.content = b"" |
| 459 |
mock_get.return_value = response |
| 460 |
assert obj.send(body="test") is False |
| 461 |
|
| 462 |
|
| 463 |
response.status_code = 999 |
| 464 |
assert obj.send(body="test") is False |
| 465 |
|
| 466 |
|
| 467 |
mock_get.side_effect = requests.RequestException("connection refused") |
| 468 |
assert obj.send(body="test") is False |
| 469 |
|
| 470 |
|
| 471 |
@mock.patch("requests.get") |
| 472 |
def test_plugin_notifico_send_notify_types(mock_get): |
| 473 |
"""NotifyNotifico() sends all four notification types successfully.""" |
| 474 |
|
| 475 |
response = mock.Mock() |
| 476 |
response.status_code = requests.codes.ok |
| 477 |
mock_get.return_value = response |
| 478 |
|
| 479 |
obj = NotifyNotifico( |
| 480 |
project_id="1234", |
| 481 |
msghook="validHook", |
| 482 |
color=True, |
| 483 |
prefix=True, |
| 484 |
) |
| 485 |
|
| 486 |
for notify_type in ( |
| 487 |
NotifyType.INFO, |
| 488 |
NotifyType.SUCCESS, |
| 489 |
NotifyType.WARNING, |
| 490 |
NotifyType.FAILURE, |
| 491 |
): |
| 492 |
mock_get.reset_mock() |
| 493 |
assert obj.send(body="msg", notify_type=notify_type) is True |
| 494 |
assert mock_get.call_count == 1 |
| 495 |
|
| 496 |
|
| 497 |
@mock.patch("requests.get") |
| 498 |
def test_plugin_notifico_color_prefix_flags(mock_get): |
| 499 |
"""NotifyNotifico() color=False strips IRC codes; prefix=False omits it.""" |
| 500 |
|
| 501 |
response = mock.Mock() |
| 502 |
response.status_code = requests.codes.ok |
| 503 |
mock_get.return_value = response |
| 504 |
|
| 505 |
|
| 506 |
obj = NotifyNotifico( |
| 507 |
project_id="1234", |
| 508 |
msghook="validHook", |
| 509 |
color=False, |
| 510 |
prefix=False, |
| 511 |
) |
| 512 |
assert obj.send(body="plain text") is True |
| 513 |
payload = mock_get.call_args[1]["params"]["payload"] |
| 514 |
assert payload == "plain text" |
| 515 |
|
| 516 |
|
| 517 |
mock_get.reset_mock() |
| 518 |
obj2 = NotifyNotifico( |
| 519 |
project_id="1234", |
| 520 |
msghook="validHook", |
| 521 |
color=True, |
| 522 |
prefix=True, |
| 523 |
) |
| 524 |
assert obj2.send(body="colored text", notify_type=NotifyType.INFO) is True |
| 525 |
payload2 = mock_get.call_args[1]["params"]["payload"] |
| 526 |
assert "colored text" in payload2 |
| 527 |
assert "[" in payload2 |
| 528 |
|
| 529 |
|
| 530 |
@mock.patch("requests.get") |
| 531 |
def test_plugin_notifico_native_url(mock_get): |
| 532 |
"""NotifyNotifico() parse_native_url() parses n.tkte.ch URLs.""" |
| 533 |
|
| 534 |
response = mock.Mock() |
| 535 |
response.status_code = requests.codes.ok |
| 536 |
mock_get.return_value = response |
| 537 |
|
| 538 |
|
| 539 |
result = NotifyNotifico.parse_native_url( |
| 540 |
"https://n.tkte.ch/h/2144/uJmKaBW9WFk42miB146ci3Kj" |
| 541 |
) |
| 542 |
assert result is not None |
| 543 |
obj = NotifyNotifico(**result) |
| 544 |
assert obj.mode == NotificoMode.OFFICIAL |
| 545 |
assert obj.project_id == "2144" |
| 546 |
assert obj.msghook == "uJmKaBW9WFk42miB146ci3Kj" |
| 547 |
assert obj.send(body="test") is True |
| 548 |
|
| 549 |
|
| 550 |
assert ( |
| 551 |
NotifyNotifico.parse_native_url("https://other.host/h/1/abc") is None |
| 552 |
) |