| 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 |
# Disable logging for a cleaner testing output |
| 29 |
import asyncio |
| 30 |
from collections import Counter |
| 31 |
import hashlib |
| 32 |
from json import dumps, loads |
| 33 |
import logging |
| 34 |
from unittest import mock |
| 35 |
|
| 36 |
import pytest |
| 37 |
import requests |
| 38 |
|
| 39 |
from apprise import Apprise, NotifyType |
| 40 |
from apprise.plugins.base import ( |
| 41 |
NotifyBase, |
| 42 |
_delivery_index, |
| 43 |
_delivery_memo, |
| 44 |
_delivery_tracker, |
| 45 |
) |
| 46 |
from apprise.plugins.jira import NotifyJira |
| 47 |
from apprise.plugins.opsgenie import NotifyOpsgenie |
| 48 |
|
| 49 |
logging.disable(logging.CRITICAL) |
| 50 |
|
| 51 |
# A Telegram URL aimed at two chats; the second one is made to fail. |
| 52 |
TGRAM = "tgram://123456789:ABCdefGHIjklMNOpqrSTUvwxYZ1234567890/111/222/" |
| 53 |
|
| 54 |
|
| 55 |
def _tgram_post(bad="222", fail_times=None): |
| 56 |
"""Build a requests.post replacement that fails one chat. |
| 57 |
|
| 58 |
- ``bad`` is the chat id that should be refused. |
| 59 |
- ``fail_times`` limits how many times it is refused; None means |
| 60 |
always. |
| 61 |
""" |
| 62 |
seen = [] |
| 63 |
state = {"n": 0} |
| 64 |
|
| 65 |
def handler(url, *args, **kwargs): |
| 66 |
data = kwargs.get("data") or {} |
| 67 |
if not isinstance(data, dict): |
| 68 |
try: |
| 69 |
data = loads(data) |
| 70 |
except (TypeError, ValueError): |
| 71 |
data = {} |
| 72 |
|
| 73 |
chat = str(data.get("chat_id", "")) |
| 74 |
r = requests.Request() |
| 75 |
if chat == bad: |
| 76 |
state["n"] += 1 |
| 77 |
refuse = fail_times is None or state["n"] <= fail_times |
| 78 |
r.status_code = 400 if refuse else requests.codes.ok |
| 79 |
|
| 80 |
else: |
| 81 |
r.status_code = requests.codes.ok |
| 82 |
|
| 83 |
seen.append((chat, len(data.get("text", "") or ""))) |
| 84 |
r.content = dumps({"ok": True, "result": {"message_id": 1}}) |
| 85 |
return r |
| 86 |
|
| 87 |
return handler, seen |
| 88 |
|
| 89 |
|
| 90 |
class _Dummy(NotifyBase): |
| 91 |
"""A tiny plugin used to poke the helpers directly.""" |
| 92 |
|
| 93 |
service_name = "Dummy" |
| 94 |
protocol = "dummy" |
| 95 |
|
| 96 |
def send(self, body, title="", notify_type=NotifyType.INFO, **kwargs): |
| 97 |
return True |
| 98 |
|
| 99 |
def url(self, privacy=False, *args, **kwargs): |
| 100 |
return "dummy://" |
| 101 |
|
| 102 |
|
| 103 |
def test_helpers_are_inert_without_a_tracker(): |
| 104 |
"""Nothing is recorded when retries were never requested.""" |
| 105 |
|
| 106 |
obj = _Dummy() |
| 107 |
|
| 108 |
# No tracker exists, so nothing is ever considered delivered |
| 109 |
assert obj.is_delivered("abc") is False |
| 110 |
|
| 111 |
# Marking is accepted but changes nothing |
| 112 |
obj.mark_delivered("abc") |
| 113 |
assert obj.is_delivered("abc") is False |
| 114 |
|
| 115 |
|
| 116 |
def test_tracker_remembers_marked_targets(): |
| 117 |
"""A marked target reads back as delivered.""" |
| 118 |
|
| 119 |
obj = _Dummy() |
| 120 |
token = _delivery_tracker.set(set()) |
| 121 |
try: |
| 122 |
assert obj.is_delivered("abc") is False |
| 123 |
obj.mark_delivered("abc") |
| 124 |
assert obj.is_delivered("abc") is True |
| 125 |
|
| 126 |
# An unrelated target is untouched |
| 127 |
assert obj.is_delivered("xyz") is False |
| 128 |
|
| 129 |
finally: |
| 130 |
_delivery_tracker.reset(token) |
| 131 |
|
| 132 |
|
| 133 |
def test_memo_is_inert_without_retries(): |
| 134 |
"""Nothing is kept when there is no later attempt to read it.""" |
| 135 |
|
| 136 |
obj = _Dummy() |
| 137 |
|
| 138 |
obj.remember("key", "value") |
| 139 |
assert obj.recall("key") is None |
| 140 |
assert obj.recall("key", "fallback") == "fallback" |
| 141 |
|
| 142 |
|
| 143 |
def test_memo_carries_a_value_between_attempts(): |
| 144 |
"""What one attempt remembers, the next one reads back.""" |
| 145 |
|
| 146 |
obj = _Dummy() |
| 147 |
token = _delivery_memo.set({}) |
| 148 |
try: |
| 149 |
assert obj.recall("key") is None |
| 150 |
|
| 151 |
obj.remember("key", "value") |
| 152 |
assert obj.recall("key") == "value" |
| 153 |
|
| 154 |
# Writing again replaces what was there |
| 155 |
obj.remember("key", "other") |
| 156 |
assert obj.recall("key") == "other" |
| 157 |
|
| 158 |
# An unrelated entry is untouched |
| 159 |
assert obj.recall("elsewhere") is None |
| 160 |
|
| 161 |
finally: |
| 162 |
_delivery_memo.reset(token) |
| 163 |
|
| 164 |
|
| 165 |
def test_memo_pieces_keep_their_own_value(): |
| 166 |
"""Part two of a split message does not read part one's value. |
| 167 |
|
| 168 |
Two pieces can hold identical content, so sharing by default would |
| 169 |
hand the second piece whatever the first stored under the same key. |
| 170 |
""" |
| 171 |
|
| 172 |
obj = _Dummy() |
| 173 |
memo_token = _delivery_memo.set({}) |
| 174 |
piece_token = _delivery_index.set(0) |
| 175 |
try: |
| 176 |
obj.remember("key", "first piece") |
| 177 |
|
| 178 |
# The second piece starts with nothing of its own |
| 179 |
_delivery_index.set(1) |
| 180 |
assert obj.recall("key") is None |
| 181 |
|
| 182 |
obj.remember("key", "second piece") |
| 183 |
assert obj.recall("key") == "second piece" |
| 184 |
|
| 185 |
# ... and the first piece still reads back its own |
| 186 |
_delivery_index.set(0) |
| 187 |
assert obj.recall("key") == "first piece" |
| 188 |
|
| 189 |
finally: |
| 190 |
_delivery_index.reset(piece_token) |
| 191 |
_delivery_memo.reset(memo_token) |
| 192 |
|
| 193 |
|
| 194 |
def test_memo_per_message_spans_pieces(): |
| 195 |
"""``per_message`` shares one value across the whole notification.""" |
| 196 |
|
| 197 |
obj = _Dummy() |
| 198 |
memo_token = _delivery_memo.set({}) |
| 199 |
piece_token = _delivery_index.set(0) |
| 200 |
try: |
| 201 |
obj.remember("upload", "media-1", per_message=True) |
| 202 |
|
| 203 |
# Every piece of the message cites the same upload |
| 204 |
_delivery_index.set(1) |
| 205 |
assert obj.recall("upload", per_message=True) == "media-1" |
| 206 |
|
| 207 |
# The per-piece slot of the same key is untouched |
| 208 |
assert obj.recall("upload") is None |
| 209 |
|
| 210 |
finally: |
| 211 |
_delivery_index.reset(piece_token) |
| 212 |
_delivery_memo.reset(memo_token) |
| 213 |
|
| 214 |
|
| 215 |
def test_memo_keys_stay_distinct(): |
| 216 |
"""Look-alike keys of different types do not collide.""" |
| 217 |
|
| 218 |
obj = _Dummy() |
| 219 |
token = _delivery_memo.set({}) |
| 220 |
try: |
| 221 |
obj.remember(1, "number") |
| 222 |
obj.remember("1", "text") |
| 223 |
obj.remember(["1"], "list") |
| 224 |
|
| 225 |
assert obj.recall(1) == "number" |
| 226 |
assert obj.recall("1") == "text" |
| 227 |
assert obj.recall(["1"]) == "list" |
| 228 |
|
| 229 |
finally: |
| 230 |
_delivery_memo.reset(token) |
| 231 |
|
| 232 |
|
| 233 |
def test_memo_does_not_outlive_its_notification(): |
| 234 |
"""A second notification never reads the first one's working state.""" |
| 235 |
|
| 236 |
obj = _Dummy() |
| 237 |
|
| 238 |
token = _delivery_memo.set({}) |
| 239 |
obj.remember("key", "first") |
| 240 |
assert obj.recall("key") == "first" |
| 241 |
_delivery_memo.reset(token) |
| 242 |
|
| 243 |
# A fresh notification starts with nothing carried over |
| 244 |
token = _delivery_memo.set({}) |
| 245 |
try: |
| 246 |
assert obj.recall("key") is None |
| 247 |
|
| 248 |
finally: |
| 249 |
_delivery_memo.reset(token) |
| 250 |
|
| 251 |
|
| 252 |
def test_message_pieces_are_tracked_separately(): |
| 253 |
"""Part two of a split message is not skipped because of part one.""" |
| 254 |
|
| 255 |
obj = _Dummy() |
| 256 |
token = _delivery_tracker.set(set()) |
| 257 |
try: |
| 258 |
# Mark the target against the first piece |
| 259 |
obj.mark_delivered("abc") |
| 260 |
assert obj.is_delivered("abc") is True |
| 261 |
|
| 262 |
# The second piece has not reached that target yet |
| 263 |
index = _delivery_index.set(1) |
| 264 |
try: |
| 265 |
assert obj.is_delivered("abc") is False |
| 266 |
obj.mark_delivered("abc") |
| 267 |
assert obj.is_delivered("abc") is True |
| 268 |
|
| 269 |
finally: |
| 270 |
_delivery_index.reset(index) |
| 271 |
|
| 272 |
# The first piece is still recorded |
| 273 |
assert obj.is_delivered("abc") is True |
| 274 |
|
| 275 |
finally: |
| 276 |
_delivery_tracker.reset(token) |
| 277 |
|
| 278 |
|
| 279 |
def test_whole_message_keys_ignore_the_piece(): |
| 280 |
"""A key marked for the whole message is seen from every piece.""" |
| 281 |
|
| 282 |
obj = _Dummy() |
| 283 |
token = _delivery_tracker.set(set()) |
| 284 |
try: |
| 285 |
obj.mark_delivered("icon", per_message=True) |
| 286 |
|
| 287 |
# The next piece of the same message still sees it |
| 288 |
index = _delivery_index.set(1) |
| 289 |
try: |
| 290 |
assert obj.is_delivered("icon", per_message=True) is True |
| 291 |
|
| 292 |
# The per-piece slot is left alone |
| 293 |
assert obj.is_delivered("icon") is False |
| 294 |
|
| 295 |
finally: |
| 296 |
_delivery_index.reset(index) |
| 297 |
|
| 298 |
finally: |
| 299 |
_delivery_tracker.reset(token) |
| 300 |
|
| 301 |
|
| 302 |
@pytest.mark.parametrize( |
| 303 |
"key", |
| 304 |
[ |
| 305 |
None, |
| 306 |
"", |
| 307 |
0, |
| 308 |
False, |
| 309 |
"a-string", |
| 310 |
3.14, |
| 311 |
b"bytes", |
| 312 |
("a", "tuple"), |
| 313 |
["a", "list"], |
| 314 |
{"a": "dict"}, |
| 315 |
{"a", "set"}, |
| 316 |
], |
| 317 |
) |
| 318 |
def test_any_kind_of_key_is_accepted(key): |
| 319 |
"""Odd keys are tolerated rather than raising.""" |
| 320 |
|
| 321 |
obj = _Dummy() |
| 322 |
token = _delivery_tracker.set(set()) |
| 323 |
try: |
| 324 |
assert obj.is_delivered(key) is False |
| 325 |
obj.mark_delivered(key) |
| 326 |
assert obj.is_delivered(key) is True |
| 327 |
|
| 328 |
finally: |
| 329 |
_delivery_tracker.reset(token) |
| 330 |
|
| 331 |
|
| 332 |
class _Unhashable: |
| 333 |
"""A value that cannot be hashed and is not a list, dict or set.""" |
| 334 |
|
| 335 |
__hash__ = None |
| 336 |
|
| 337 |
def __init__(self, label): |
| 338 |
self.label = label |
| 339 |
|
| 340 |
def __repr__(self): |
| 341 |
return f"_Unhashable({self.label})" |
| 342 |
|
| 343 |
|
| 344 |
def test_an_unhashable_object_is_still_tracked(): |
| 345 |
"""A custom unhashable value falls back to its text form.""" |
| 346 |
|
| 347 |
obj = _Dummy() |
| 348 |
token = _delivery_tracker.set(set()) |
| 349 |
try: |
| 350 |
first = _Unhashable("one") |
| 351 |
second = _Unhashable("two") |
| 352 |
|
| 353 |
assert obj.is_delivered(first) is False |
| 354 |
obj.mark_delivered(first) |
| 355 |
assert obj.is_delivered(first) is True |
| 356 |
|
| 357 |
# A different value is not confused with the first |
| 358 |
assert obj.is_delivered(second) is False |
| 359 |
|
| 360 |
finally: |
| 361 |
_delivery_tracker.reset(token) |
| 362 |
|
| 363 |
|
| 364 |
def test_unhashable_keys_stay_distinct(): |
| 365 |
"""Two different lists are not treated as the same target.""" |
| 366 |
|
| 367 |
obj = _Dummy() |
| 368 |
token = _delivery_tracker.set(set()) |
| 369 |
try: |
| 370 |
obj.mark_delivered(["a"]) |
| 371 |
assert obj.is_delivered(["a"]) is True |
| 372 |
assert obj.is_delivered(["b"]) is False |
| 373 |
|
| 374 |
finally: |
| 375 |
_delivery_tracker.reset(token) |
| 376 |
|
| 377 |
|
| 378 |
@pytest.mark.parametrize( |
| 379 |
"marked, look_alike", |
| 380 |
[ |
| 381 |
(False, 0), |
| 382 |
(1, "1"), |
| 383 |
(1, True), |
| 384 |
((0,), (False,)), |
| 385 |
("1", b"1"), |
| 386 |
], |
| 387 |
) |
| 388 |
def test_look_alike_keys_stay_distinct(marked, look_alike): |
| 389 |
"""A key keeps its type, so values Python calls equal stay apart.""" |
| 390 |
|
| 391 |
obj = _Dummy() |
| 392 |
token = _delivery_tracker.set(set()) |
| 393 |
try: |
| 394 |
obj.mark_delivered(marked) |
| 395 |
|
| 396 |
assert obj.is_delivered(marked) is True |
| 397 |
assert obj.is_delivered(look_alike) is False |
| 398 |
|
| 399 |
finally: |
| 400 |
_delivery_tracker.reset(token) |
| 401 |
|
| 402 |
|
| 403 |
def test_container_key_does_not_collide_with_text(): |
| 404 |
"""A container and a string resembling it remain separate targets.""" |
| 405 |
|
| 406 |
obj = _Dummy() |
| 407 |
token = _delivery_tracker.set(set()) |
| 408 |
try: |
| 409 |
obj.mark_delivered(["a"]) |
| 410 |
|
| 411 |
assert obj.is_delivered(["a"]) is True |
| 412 |
assert obj.is_delivered('["a"]') is False |
| 413 |
assert obj.is_delivered("['a']") is False |
| 414 |
|
| 415 |
finally: |
| 416 |
_delivery_tracker.reset(token) |
| 417 |
|
| 418 |
|
| 419 |
@pytest.mark.parametrize("plugin_cls", (NotifyJira, NotifyOpsgenie)) |
| 420 |
def test_alert_ids_survive_partial_retry(plugin_cls): |
| 421 |
"""A retry keeps request IDs returned by earlier successful batches.""" |
| 422 |
|
| 423 |
obj = plugin_cls("apikey", targets=["@one", "@two"]) |
| 424 |
cache_key = hashlib.sha1(b"title").hexdigest()[0:10] |
| 425 |
|
| 426 |
tracker_token = _delivery_tracker.set(set()) |
| 427 |
try: |
| 428 |
with mock.patch.object( |
| 429 |
obj, |
| 430 |
"_fetch", |
| 431 |
side_effect=[(True, "id-one"), (False, None)], |
| 432 |
): |
| 433 |
assert ( |
| 434 |
obj.send( |
| 435 |
body="body", |
| 436 |
title="title", |
| 437 |
notify_type=NotifyType.WARNING, |
| 438 |
) |
| 439 |
is False |
| 440 |
) |
| 441 |
|
| 442 |
with mock.patch.object( |
| 443 |
obj, |
| 444 |
"_fetch", |
| 445 |
return_value=(True, "id-two"), |
| 446 |
): |
| 447 |
assert ( |
| 448 |
obj.send( |
| 449 |
body="body", |
| 450 |
title="title", |
| 451 |
notify_type=NotifyType.WARNING, |
| 452 |
) |
| 453 |
is True |
| 454 |
) |
| 455 |
|
| 456 |
assert obj.store.get(cache_key) == ["id-one", "id-two"] |
| 457 |
finally: |
| 458 |
_delivery_tracker.reset(tracker_token) |
| 459 |
|
| 460 |
|
| 461 |
@mock.patch("requests.post") |
| 462 |
def test_no_tracker_survives_a_delivery(mock_post): |
| 463 |
"""The tracker never outlives the call that created it.""" |
| 464 |
|
| 465 |
mock_post.return_value = requests.Request() |
| 466 |
mock_post.return_value.status_code = requests.codes.ok |
| 467 |
mock_post.return_value.content = dumps({"ok": True}) |
| 468 |
|
| 469 |
aobj = Apprise() |
| 470 |
assert aobj.add(TGRAM + "?retry=2&wait=0") |
| 471 |
aobj.notify(body="hello") |
| 472 |
|
| 473 |
# Back at rest, nothing is left behind |
| 474 |
assert _delivery_tracker.get() is None |
| 475 |
|
| 476 |
|
| 477 |
@mock.patch("requests.post") |
| 478 |
def test_tracker_released_after_plugin_error(mock_post): |
| 479 |
"""An exception still leaves the tracker cleaned up.""" |
| 480 |
|
| 481 |
mock_post.side_effect = RuntimeError("boom") |
| 482 |
|
| 483 |
aobj = Apprise() |
| 484 |
assert aobj.add(TGRAM + "?retry=1&wait=0") |
| 485 |
aobj.notify(body="hello") |
| 486 |
|
| 487 |
assert _delivery_tracker.get() is None |
| 488 |
assert _delivery_index.get() == 0 |
| 489 |
|
| 490 |
|
| 491 |
def test_successful_target_not_notified_twice(): |
| 492 |
"""A retry must not re-deliver to targets that already succeeded.""" |
| 493 |
|
| 494 |
handler, seen = _tgram_post() |
| 495 |
with mock.patch("requests.post", side_effect=handler): |
| 496 |
aobj = Apprise() |
| 497 |
assert aobj.add(TGRAM + "?retry=3&wait=0") |
| 498 |
result = aobj.notify(body="hello") |
| 499 |
|
| 500 |
counts = Counter(chat for chat, _ in seen) |
| 501 |
|
| 502 |
# The healthy chat is contacted exactly once |
| 503 |
assert counts["111"] == 1 |
| 504 |
|
| 505 |
# The broken one is retried for every attempt |
| 506 |
assert counts["222"] == 4 |
| 507 |
assert bool(result) is False |
| 508 |
|
| 509 |
|
| 510 |
def test_retry_disabled_preserves_delivery(): |
| 511 |
"""With retry off there is a single pass over the targets.""" |
| 512 |
|
| 513 |
handler, seen = _tgram_post() |
| 514 |
with mock.patch("requests.post", side_effect=handler): |
| 515 |
aobj = Apprise() |
| 516 |
assert aobj.add(TGRAM + "?retry=0") |
| 517 |
aobj.notify(body="hello") |
| 518 |
|
| 519 |
counts = Counter(chat for chat, _ in seen) |
| 520 |
assert counts["111"] == 1 |
| 521 |
assert counts["222"] == 1 |
| 522 |
|
| 523 |
|
| 524 |
def test_recovering_target_is_retried(): |
| 525 |
"""A target that fails once is tried again and then succeeds.""" |
| 526 |
|
| 527 |
handler, seen = _tgram_post(fail_times=1) |
| 528 |
with mock.patch("requests.post", side_effect=handler): |
| 529 |
aobj = Apprise() |
| 530 |
assert aobj.add(TGRAM + "?retry=3&wait=0") |
| 531 |
result = aobj.notify(body="hello") |
| 532 |
|
| 533 |
counts = Counter(chat for chat, _ in seen) |
| 534 |
|
| 535 |
# The healthy chat still only hears from us once |
| 536 |
assert counts["111"] == 1 |
| 537 |
|
| 538 |
# The other one needed a second go |
| 539 |
assert counts["222"] == 2 |
| 540 |
assert bool(result) is True |
| 541 |
|
| 542 |
|
| 543 |
def test_split_message_pieces_are_delivered(): |
| 544 |
"""Each chunk reaches a healthy target exactly once.""" |
| 545 |
|
| 546 |
handler, seen = _tgram_post() |
| 547 |
with mock.patch("requests.post", side_effect=handler): |
| 548 |
aobj = Apprise() |
| 549 |
assert aobj.add(TGRAM + "?overflow=split&retry=2&wait=0") |
| 550 |
aobj.notify(body="y" * 9000) |
| 551 |
|
| 552 |
good = [size for chat, size in seen if chat == "111"] |
| 553 |
|
| 554 |
# Three chunks, each sent once and never repeated by a retry |
| 555 |
assert len(good) == 3 |
| 556 |
assert sum(good) == 9000 |
| 557 |
|
| 558 |
|
| 559 |
def test_split_message_includes_the_image_once(): |
| 560 |
"""The notification image is not repeated for every chunk.""" |
| 561 |
|
| 562 |
seen = [] |
| 563 |
|
| 564 |
def handler(url, *args, **kwargs): |
| 565 |
seen.append("image" if kwargs.get("files") else "body") |
| 566 |
r = requests.Request() |
| 567 |
r.status_code = requests.codes.ok |
| 568 |
r.content = dumps({"ok": True, "result": {"message_id": 1}}) |
| 569 |
r.text = r.content |
| 570 |
r.headers = {} |
| 571 |
return r |
| 572 |
|
| 573 |
with mock.patch("requests.post", side_effect=handler): |
| 574 |
aobj = Apprise() |
| 575 |
assert aobj.add(TGRAM + "?overflow=split&retry=2&wait=0&image=yes") |
| 576 |
aobj.notify(body="y" * 9000) |
| 577 |
|
| 578 |
counts = Counter(seen) |
| 579 |
|
| 580 |
# Two chats, one image each, no matter how many chunks went out |
| 581 |
assert counts["image"] == 2 |
| 582 |
assert counts["body"] > 2 |
| 583 |
|
| 584 |
|
| 585 |
def test_async_delivery_tracking(): |
| 586 |
"""The asynchronous path shares the same tracking.""" |
| 587 |
|
| 588 |
handler, seen = _tgram_post() |
| 589 |
with mock.patch("requests.post", side_effect=handler): |
| 590 |
aobj = Apprise() |
| 591 |
assert aobj.add(TGRAM + "?retry=3&wait=0") |
| 592 |
asyncio.run(aobj.async_notify(body="hello")) |
| 593 |
|
| 594 |
counts = Counter(chat for chat, _ in seen) |
| 595 |
assert counts["111"] == 1 |
| 596 |
assert counts["222"] == 4 |
| 597 |
assert _delivery_tracker.get() is None |
| 598 |
|
| 599 |
|
| 600 |
def test_destination_kinds_stay_distinct(): |
| 601 |
"""A phone number used two ways is two separate destinations.""" |
| 602 |
|
| 603 |
obj = _Dummy() |
| 604 |
token = _delivery_tracker.set(set()) |
| 605 |
try: |
| 606 |
# The pattern every multi-mode plugin uses: pair the value with |
| 607 |
# the kind of destination it is. |
| 608 |
obj.mark_delivered(("sms", "+15551234567")) |
| 609 |
|
| 610 |
assert obj.is_delivered(("sms", "+15551234567")) is True |
| 611 |
|
| 612 |
# The same number over a different mode has not been reached |
| 613 |
assert obj.is_delivered(("whatsapp", "+15551234567")) is False |
| 614 |
|
| 615 |
# ...and the bare value is not the same thing either |
| 616 |
assert obj.is_delivered("+15551234567") is False |
| 617 |
|
| 618 |
finally: |
| 619 |
_delivery_tracker.reset(token) |
| 620 |
|
| 621 |
|
| 622 |
def test_batch_positions_stay_distinct(): |
| 623 |
"""Each batch of recipients is its own delivery.""" |
| 624 |
|
| 625 |
obj = _Dummy() |
| 626 |
token = _delivery_tracker.set(set()) |
| 627 |
try: |
| 628 |
obj.mark_delivered(("batch", 0)) |
| 629 |
|
| 630 |
assert obj.is_delivered(("batch", 0)) is True |
| 631 |
|
| 632 |
# The next batch still has to go out |
| 633 |
assert obj.is_delivered(("batch", 1)) is False |
| 634 |
|
| 635 |
finally: |
| 636 |
_delivery_tracker.reset(token) |