| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
"""Proves that a retry never re-notifies a target that already succeeded. |
| 29 |
|
| 30 |
Every service able to reach more than one target in a single call is |
| 31 |
driven here with two targets, one of which always refuses the message. |
| 32 |
Two things are then checked: |
| 33 |
|
| 34 |
- the healthy target is contacted exactly once, no matter how many |
| 35 |
retries were asked for, and |
| 36 |
- the broken one is retried on every attempt. |
| 37 |
|
| 38 |
A per-service baseline runs first so a badly written entry in the table |
| 39 |
below shows up as a broken entry rather than a false pass. |
| 40 |
""" |
| 41 |
|
| 42 |
# Disable logging for a cleaner testing output |
| 43 |
from json import dumps |
| 44 |
import logging |
| 45 |
from unittest import mock |
| 46 |
|
| 47 |
from helpers import OK_FIELDS |
| 48 |
import pytest |
| 49 |
import requests |
| 50 |
|
| 51 |
from apprise import Apprise |
| 52 |
|
| 53 |
logging.disable(logging.CRITICAL) |
| 54 |
|
| 55 |
# Number of retries requested by every case below. |
| 56 |
RETRY = 2 |
| 57 |
|
| 58 |
# Stand-in credentials; long enough to satisfy the usual token checks. |
| 59 |
TOKEN = "a" * 32 |
| 60 |
|
| 61 |
# Each entry drives one service. |
| 62 |
# |
| 63 |
# name -- the plugin, used only to name the test |
| 64 |
# url -- a URL carrying exactly two targets |
| 65 |
# good -- text that appears in the request for the healthy target |
| 66 |
# bad -- text that appears in the request for the broken target |
| 67 |
# body -- extra response fields this service needs to see. The |
| 68 |
# reserved key "__http__" sets the status code this |
| 69 |
# service treats as a success (default 200). |
| 70 |
FLEET = ( |
| 71 |
( |
| 72 |
"telegram", |
| 73 |
"tgram://123456789:ABCdefGHIjklMNOpqrSTUvwxYZ1234567890/111/999/", |
| 74 |
"111", |
| 75 |
"999", |
| 76 |
{}, |
| 77 |
), |
| 78 |
( |
| 79 |
"bulkvs", |
| 80 |
"bulkvs://user:pass@12125550000/12125550001/12125559999/", |
| 81 |
"12125550001", |
| 82 |
"12125559999", |
| 83 |
{}, |
| 84 |
), |
| 85 |
( |
| 86 |
"httpsms", |
| 87 |
f"httpsms://{TOKEN}@12125550000/12125550001/12125559999/", |
| 88 |
"12125550001", |
| 89 |
"12125559999", |
| 90 |
{}, |
| 91 |
), |
| 92 |
( |
| 93 |
"telnyx", |
| 94 |
f"telnyx://{TOKEN}@12125550000/12125550001/12125559999/", |
| 95 |
"12125550001", |
| 96 |
"12125559999", |
| 97 |
{}, |
| 98 |
), |
| 99 |
( |
| 100 |
"mobilemessage", |
| 101 |
# Batching is on by default; this drives one call per recipient so |
| 102 |
# each target can be answered separately. |
| 103 |
"mobilemessage://user:pass@ALERTS/0412345678/0498765432?batch=no", |
| 104 |
"61412345678", |
| 105 |
"61498765432", |
| 106 |
{"status": "complete", "results": [{"status": "success"}]}, |
| 107 |
), |
| 108 |
( |
| 109 |
"zulip", |
| 110 |
f"zulip://botname@apprise/{'b' * 32}/channel1/channel999", |
| 111 |
"channel1", |
| 112 |
"channel999", |
| 113 |
{}, |
| 114 |
), |
| 115 |
( |
| 116 |
"nextcloudtalk", |
| 117 |
"nctalk://user:pass@localhost/room1/room999", |
| 118 |
"room1", |
| 119 |
"room999", |
| 120 |
{}, |
| 121 |
), |
| 122 |
( |
| 123 |
"brevo", |
| 124 |
f"brevo://{TOKEN}:[email protected]/[email protected]/[email protected]", |
| 125 |
"[email protected]", |
| 126 |
"[email protected]", |
| 127 |
{}, |
| 128 |
), |
| 129 |
( |
| 130 |
"resend", |
| 131 |
f"resend://{TOKEN}:[email protected]/[email protected]/[email protected]", |
| 132 |
"[email protected]", |
| 133 |
"[email protected]", |
| 134 |
{}, |
| 135 |
), |
| 136 |
( |
| 137 |
"postmark", |
| 138 |
f"postmark://{TOKEN}:[email protected]/[email protected]/[email protected]", |
| 139 |
"[email protected]", |
| 140 |
"[email protected]", |
| 141 |
{}, |
| 142 |
), |
| 143 |
( |
| 144 |
"sendgrid", |
| 145 |
f"sendgrid://{TOKEN}:[email protected]/[email protected]/[email protected]", |
| 146 |
"[email protected]", |
| 147 |
"[email protected]", |
| 148 |
{}, |
| 149 |
), |
| 150 |
( |
| 151 |
"mailersend", |
| 152 |
( |
| 153 |
f"mailersend://{TOKEN}:[email protected]" |
| 154 |
"/[email protected]/[email protected]" |
| 155 |
), |
| 156 |
"[email protected]", |
| 157 |
"[email protected]", |
| 158 |
{}, |
| 159 |
), |
| 160 |
( |
| 161 |
"pushbullet", |
| 162 |
f"pbul://{TOKEN}/#chan1/#chan999", |
| 163 |
"chan1", |
| 164 |
"chan999", |
| 165 |
{}, |
| 166 |
), |
| 167 |
( |
| 168 |
"seven", |
| 169 |
f"seven://{TOKEN}/12125550001/12125559999", |
| 170 |
"12125550001", |
| 171 |
"12125559999", |
| 172 |
{}, |
| 173 |
), |
| 174 |
( |
| 175 |
"line", |
| 176 |
f"line://{TOKEN}/target0001/target9999", |
| 177 |
"target0001", |
| 178 |
"target9999", |
| 179 |
{}, |
| 180 |
), |
| 181 |
( |
| 182 |
"join", |
| 183 |
f"join://{TOKEN}/device0001/device9999", |
| 184 |
"device0001", |
| 185 |
"device9999", |
| 186 |
{}, |
| 187 |
), |
| 188 |
( |
| 189 |
"kavenegar", |
| 190 |
"kavenegar://12125550000/12125550001/12125559999", |
| 191 |
"12125550001", |
| 192 |
"12125559999", |
| 193 |
{}, |
| 194 |
), |
| 195 |
( |
| 196 |
"viber", |
| 197 |
f"viber://{TOKEN}/t0001/t9999?from=Bot", |
| 198 |
"t0001", |
| 199 |
"t9999", |
| 200 |
# Viber reports its own outcome in a numeric status field |
| 201 |
{"status": 0}, |
| 202 |
), |
| 203 |
( |
| 204 |
"clickatell", |
| 205 |
f"clickatell://12125550000@{TOKEN}/12125550001/12125559999", |
| 206 |
"12125550001", |
| 207 |
"12125559999", |
| 208 |
{}, |
| 209 |
), |
| 210 |
( |
| 211 |
"d7networks", |
| 212 |
f"d7sms://{TOKEN}@12125550001/12125559999", |
| 213 |
"12125550001", |
| 214 |
"12125559999", |
| 215 |
{}, |
| 216 |
), |
| 217 |
( |
| 218 |
"exotel", |
| 219 |
f"exotel://sid:{TOKEN}@12125550000/12125550001/12125559999", |
| 220 |
"12125550001", |
| 221 |
"12125559999", |
| 222 |
{}, |
| 223 |
), |
| 224 |
( |
| 225 |
"vonage", |
| 226 |
f"vonage://AC{TOKEN}:{TOKEN}@12125550000/12125550001/12125559999", |
| 227 |
"12125550001", |
| 228 |
"12125559999", |
| 229 |
{}, |
| 230 |
), |
| 231 |
( |
| 232 |
"whatsapp", |
| 233 |
f"whatsapp://{TOKEN}@12125550000/12125550001/12125559999", |
| 234 |
"12125550001", |
| 235 |
"12125559999", |
| 236 |
{}, |
| 237 |
), |
| 238 |
( |
| 239 |
"voipms", |
| 240 |
"voipms://pass:[email protected]/12125550000/12125550001/12125559999", |
| 241 |
# VoIPms drops the leading country digit before sending |
| 242 |
"2125550001", |
| 243 |
"2125559999", |
| 244 |
{"status": "success", "message": "ok"}, |
| 245 |
), |
| 246 |
( |
| 247 |
"sfr", |
| 248 |
"sfr://user:pass@spaceid/12125550001/12125559999", |
| 249 |
"12125550001", |
| 250 |
"12125559999", |
| 251 |
{}, |
| 252 |
), |
| 253 |
( |
| 254 |
"bulksms", |
| 255 |
"bulksms://user:pass@12125550001/12125559999", |
| 256 |
"12125550001", |
| 257 |
"12125559999", |
| 258 |
{}, |
| 259 |
), |
| 260 |
( |
| 261 |
"pingram", |
| 262 |
f"pingram://pingram_sk_{TOKEN}/12125550001/12125559999", |
| 263 |
"12125550001", |
| 264 |
"12125559999", |
| 265 |
{}, |
| 266 |
), |
| 267 |
( |
| 268 |
"rocketchat", |
| 269 |
"rocket://user:pass@localhost/#chan0001/#chan9999", |
| 270 |
"chan0001", |
| 271 |
"chan9999", |
| 272 |
{"status": "success"}, |
| 273 |
), |
| 274 |
( |
| 275 |
"revolt", |
| 276 |
f"revolt://{TOKEN}/chan0001/chan9999", |
| 277 |
"chan0001", |
| 278 |
"chan9999", |
| 279 |
{}, |
| 280 |
), |
| 281 |
( |
| 282 |
"ntfy", |
| 283 |
"ntfy://localhost/topic0001/topic9999", |
| 284 |
"topic0001", |
| 285 |
"topic9999", |
| 286 |
{}, |
| 287 |
), |
| 288 |
( |
| 289 |
"reddit", |
| 290 |
f"reddit://user:pass@{TOKEN}/{TOKEN}/sub0001/sub9999", |
| 291 |
"sub0001", |
| 292 |
"sub9999", |
| 293 |
# Reddit authenticates first, then posts |
| 294 |
{"json": {"errors": []}, "access_token": "abc", "expires_in": 3600}, |
| 295 |
), |
| 296 |
( |
| 297 |
"bark", |
| 298 |
"bark://localhost/dev0001/dev9999", |
| 299 |
"dev0001", |
| 300 |
"dev9999", |
| 301 |
{}, |
| 302 |
), |
| 303 |
( |
| 304 |
"threema", |
| 305 |
f"threema://*GATEWAY@{TOKEN}/user0001/user9999", |
| 306 |
"user0001", |
| 307 |
"user9999", |
| 308 |
{}, |
| 309 |
), |
| 310 |
( |
| 311 |
"twilio", |
| 312 |
f"twilio://AC{TOKEN}:{TOKEN}@12125550000/12125550001/12125559999", |
| 313 |
"12125550001", |
| 314 |
"12125559999", |
| 315 |
{}, |
| 316 |
), |
| 317 |
( |
| 318 |
"ifttt", |
| 319 |
f"ifttt://{TOKEN}@event0001/event9999", |
| 320 |
"event0001", |
| 321 |
"event9999", |
| 322 |
{}, |
| 323 |
), |
| 324 |
( |
| 325 |
"fortysixelks", |
| 326 |
"46elks://user:pass@sender/12125550001/12125559999", |
| 327 |
"12125550001", |
| 328 |
"12125559999", |
| 329 |
{}, |
| 330 |
), |
| 331 |
( |
| 332 |
"evolution", |
| 333 |
f"evolution://{TOKEN}@localhost/inst/12125550001/12125559999", |
| 334 |
"12125550001", |
| 335 |
"12125559999", |
| 336 |
{}, |
| 337 |
), |
| 338 |
( |
| 339 |
"notifiarr", |
| 340 |
f"notifiarr://{TOKEN}/#1001/#9009", |
| 341 |
"1001", |
| 342 |
"9009", |
| 343 |
{}, |
| 344 |
), |
| 345 |
( |
| 346 |
"dapnet", |
| 347 |
"dapnet://user:pass@localhost/DL0001/DL9999", |
| 348 |
"DL0001", |
| 349 |
"DL9999", |
| 350 |
{"__http__": 201}, |
| 351 |
), |
| 352 |
( |
| 353 |
"flock", |
| 354 |
f"flock://{TOKEN}/u:0001/u:9999", |
| 355 |
"0001", |
| 356 |
"9999", |
| 357 |
{}, |
| 358 |
), |
| 359 |
( |
| 360 |
"pushed", |
| 361 |
f"pushed://{TOKEN}/{TOKEN}/#chan0001/#chan9999", |
| 362 |
"chan0001", |
| 363 |
"chan9999", |
| 364 |
{}, |
| 365 |
), |
| 366 |
( |
| 367 |
"sns", |
| 368 |
f"sns://{TOKEN}/{'b' * 40}/us-east-1/12125550001/12125559999", |
| 369 |
"12125550001", |
| 370 |
"12125559999", |
| 371 |
{}, |
| 372 |
), |
| 373 |
) |
| 374 |
|
| 375 |
# Names only, for readable test ids |
| 376 |
FLEET_IDS = [row[0] for row in FLEET] |
| 377 |
|
| 378 |
|
| 379 |
def _drive(url, body, bad=None): |
| 380 |
"""Send one notification and report every request that was made. |
| 381 |
|
| 382 |
``bad`` names the target that should be refused; pass None to let |
| 383 |
every target succeed. |
| 384 |
""" |
| 385 |
calls = [] |
| 386 |
payload = dict(OK_FIELDS) |
| 387 |
payload.update(body) |
| 388 |
|
| 389 |
# Some services only accept a specific success code |
| 390 |
ok_status = payload.pop("__http__", requests.codes.ok) |
| 391 |
encoded = dumps(payload) |
| 392 |
|
| 393 |
def handler(target_url, *args, **kwargs): |
| 394 |
# Everything the plugin passed, so a target can be spotted |
| 395 |
# wherever the service happens to put it. |
| 396 |
blob = repr(target_url) + repr(args) + repr(kwargs) |
| 397 |
calls.append(blob) |
| 398 |
|
| 399 |
response = requests.Request() |
| 400 |
response.status_code = ( |
| 401 |
400 if bad is not None and bad in blob else ok_status |
| 402 |
) |
| 403 |
response.content = encoded |
| 404 |
response.text = encoded |
| 405 |
response.headers = {"Content-Type": "application/json"} |
| 406 |
return response |
| 407 |
|
| 408 |
separator = "&" if "?" in url else "?" |
| 409 |
with ( |
| 410 |
mock.patch("requests.post", side_effect=handler), |
| 411 |
mock.patch("requests.get", side_effect=handler), |
| 412 |
mock.patch("requests.put", side_effect=handler), |
| 413 |
mock.patch("requests.patch", side_effect=handler), |
| 414 |
# Keep the run quick; the delays are not what is being tested. |
| 415 |
mock.patch("apprise.plugins.base.time.sleep"), |
| 416 |
mock.patch("apprise.apprise.time.sleep"), |
| 417 |
): |
| 418 |
aobj = Apprise() |
| 419 |
assert aobj.add(f"{url}{separator}retry={RETRY}&wait=0") |
| 420 |
result = aobj.notify(body="hello") |
| 421 |
|
| 422 |
return result, calls |
| 423 |
|
| 424 |
|
| 425 |
@pytest.mark.parametrize( |
| 426 |
("name", "url", "good", "bad", "body"), FLEET, ids=FLEET_IDS |
| 427 |
) |
| 428 |
def test_fleet_entry_is_usable(name, url, good, bad, body): |
| 429 |
"""Each table entry reaches two targets when nothing goes wrong.""" |
| 430 |
|
| 431 |
obj = Apprise.instantiate(url) |
| 432 |
assert obj is not None, f"{name}: URL did not load" |
| 433 |
|
| 434 |
# Two separate endpoints are required for this to mean anything |
| 435 |
assert len(obj) == 2, f"{name}: expected 2 targets, got {len(obj)}" |
| 436 |
|
| 437 |
result, calls = _drive(url, body) |
| 438 |
assert bool(result) is True, f"{name}: baseline delivery failed" |
| 439 |
|
| 440 |
# One request per target, and each target is recognisable |
| 441 |
assert sum(1 for c in calls if good in c) == 1 |
| 442 |
assert sum(1 for c in calls if bad in c) == 1 |
| 443 |
|
| 444 |
|
| 445 |
@pytest.mark.parametrize( |
| 446 |
("name", "url", "good", "bad", "body"), FLEET, ids=FLEET_IDS |
| 447 |
) |
| 448 |
def test_fleet_retry_skips_delivered_targets(name, url, good, bad, body): |
| 449 |
"""A retry only re-contacts the target that is still failing.""" |
| 450 |
|
| 451 |
result, calls = _drive(url, body, bad=bad) |
| 452 |
|
| 453 |
delivered = [c for c in calls if good in c] |
| 454 |
refused = [c for c in calls if bad in c] |
| 455 |
|
| 456 |
# The healthy target hears from us once and is never repeated |
| 457 |
assert len(delivered) == 1, ( |
| 458 |
f"{name}: healthy target notified {len(delivered)} times;" |
| 459 |
" mark_delivered() is missing or in the wrong place" |
| 460 |
) |
| 461 |
|
| 462 |
# The broken one is tried again on every attempt. A service that |
| 463 |
# re-authenticates before each post makes more than one request per |
| 464 |
# attempt, so this is a floor rather than an exact count. |
| 465 |
assert len(refused) >= RETRY + 1, ( |
| 466 |
f"{name}: broken target tried {len(refused)} times," |
| 467 |
f" expected at least {RETRY + 1}" |
| 468 |
) |
| 469 |
|
| 470 |
# Something genuinely failed, so the call reports failure |
| 471 |
assert bool(result) is False |