| 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 |
from datetime import timezone, tzinfo |
| 30 |
import logging |
| 31 |
import sys |
| 32 |
from zoneinfo import ZoneInfo |
| 33 |
|
| 34 |
import pytest |
| 35 |
|
| 36 |
from apprise.asset import AppriseAsset |
| 37 |
from apprise.exception import AppriseImproperlyConfigured |
| 38 |
|
| 39 |
logging.disable(logging.CRITICAL) |
| 40 |
|
| 41 |
# Ensure we don't create .pyc files for these tests |
| 42 |
sys.dont_write_bytecode = True |
| 43 |
|
| 44 |
|
| 45 |
def test_timezone(): |
| 46 |
"asset: timezone() testing" |
| 47 |
asset = AppriseAsset(timezone="utc") |
| 48 |
assert isinstance(asset.tzinfo, tzinfo) |
| 49 |
|
| 50 |
# Default (uses system value) |
| 51 |
asset = AppriseAsset(timezone=None) |
| 52 |
assert isinstance(asset.tzinfo, tzinfo) |
| 53 |
|
| 54 |
# Timezone can also already be a tzinfo object |
| 55 |
asset = AppriseAsset(timezone=timezone.utc) |
| 56 |
assert isinstance(asset.tzinfo, tzinfo) |
| 57 |
asset = AppriseAsset(timezone=ZoneInfo("America/Toronto")) |
| 58 |
assert isinstance(asset.tzinfo, tzinfo) |
| 59 |
|
| 60 |
with pytest.raises(AppriseImproperlyConfigured): |
| 61 |
AppriseAsset(timezone=object) |
| 62 |
|
| 63 |
with pytest.raises(AppriseImproperlyConfigured): |
| 64 |
AppriseAsset(timezone="invalid") |
| 65 |
|
| 66 |
# The private field cannot bypass the validated timezone argument. |
| 67 |
with pytest.raises(AppriseImproperlyConfigured): |
| 68 |
AppriseAsset(_tzinfo=timezone.utc) |
| 69 |
|
| 70 |
|
| 71 |
def test_language(): |
| 72 |
"asset: language() testing" |
| 73 |
# None leaves the detection of the language up to Apprise |
| 74 |
assert AppriseAsset().language is None |
| 75 |
|
| 76 |
# Both the 2 and the 5 letter forms are accepted; a region is stored |
| 77 |
# upper case and the language lower case no matter how it arrives |
| 78 |
for entry in ("en", "EN", " en "): |
| 79 |
assert AppriseAsset(language=entry).language == "en" |
| 80 |
|
| 81 |
for entry in ("en-CA", "en_CA", "EN-ca", "en_ca"): |
| 82 |
assert AppriseAsset(language=entry).language == "en_CA" |
| 83 |
|
| 84 |
# A script or any other extra part is dropped, leaving the language |
| 85 |
assert AppriseAsset(language="zh-Hans").language == "zh" |
| 86 |
assert AppriseAsset(language="zh-Hans-CN").language == "zh" |
| 87 |
|
| 88 |
# Only the first entry of an Accept-Language styled value is used |
| 89 |
asset = AppriseAsset(language="en-US,en;q=0.9,de;q=0.5") |
| 90 |
assert asset.language == "en_US" |
| 91 |
|
| 92 |
# An empty value is the same as not providing one at all |
| 93 |
assert AppriseAsset(language="").language is None |
| 94 |
assert AppriseAsset(language=" ").language is None |
| 95 |
|
| 96 |
# Anything we can not make sense of is a configuration error |
| 97 |
for entry in ("garbage", "!", 42, {}, object()): |
| 98 |
with pytest.raises(AppriseImproperlyConfigured): |
| 99 |
AppriseAsset(language=entry) |
| 100 |
|
| 101 |
# The language is locked in once the asset is built; a new language |
| 102 |
# means a new asset |
| 103 |
with pytest.raises(AttributeError): |
| 104 |
AppriseAsset().language = "fr" |
| 105 |
|
| 106 |
# The private field cannot bypass the validated language argument. |
| 107 |
with pytest.raises(AppriseImproperlyConfigured): |
| 108 |
AppriseAsset(_language="garbage") |
| 109 |
|
| 110 |
|
| 111 |
def test_service_timeout(): |
| 112 |
"asset: service_timeout() testing" |
| 113 |
|
| 114 |
# Default value is 60 seconds, defined directly on AppriseAsset |
| 115 |
asset = AppriseAsset() |
| 116 |
assert asset._service_timeout == 60.0 |
| 117 |
|
| 118 |
# Accepts an int and stores it as a float |
| 119 |
asset = AppriseAsset(service_timeout=10) |
| 120 |
assert asset._service_timeout == 10.0 |
| 121 |
assert isinstance(asset._service_timeout, float) |
| 122 |
|
| 123 |
# Accepts a float directly |
| 124 |
asset = AppriseAsset(service_timeout=12.5) |
| 125 |
assert asset._service_timeout == 12.5 |
| 126 |
|
| 127 |
# 0 is valid -- it disables the timeout entirely |
| 128 |
asset = AppriseAsset(service_timeout=0) |
| 129 |
assert asset._service_timeout == 0.0 |
| 130 |
|
| 131 |
# Negative values are rejected |
| 132 |
with pytest.raises(AppriseImproperlyConfigured): |
| 133 |
AppriseAsset(service_timeout=-1) |
| 134 |
|
| 135 |
# Non-numeric types are rejected |
| 136 |
with pytest.raises(AppriseImproperlyConfigured): |
| 137 |
AppriseAsset(service_timeout="invalid") |
| 138 |
|
| 139 |
# Booleans are rejected even though bool is technically an int subclass |
| 140 |
with pytest.raises(AppriseImproperlyConfigured): |
| 141 |
AppriseAsset(service_timeout=True) |
| 142 |
|
| 143 |
# inf might look like a second way to spell "unbounded" (0 is the |
| 144 |
# documented one), but concurrent.futures.Future.result(timeout= |
| 145 |
# float("inf")) raises OverflowError on some platforms, silently |
| 146 |
# turning a successful notification into a reported FAILURE -- so |
| 147 |
# it's rejected outright, same as any other non-finite value. |
| 148 |
with pytest.raises(AppriseImproperlyConfigured): |
| 149 |
AppriseAsset(service_timeout=float("inf")) |
| 150 |
|
| 151 |
with pytest.raises(AppriseImproperlyConfigured): |
| 152 |
AppriseAsset(service_timeout=float("-inf")) |
| 153 |
|
| 154 |
# NaN fails every ordering comparison, so it would otherwise slip |
| 155 |
# past a plain "< 0" check and silently disable the timeout as an |
| 156 |
# accidental side effect of its comparison semantics. |
| 157 |
with pytest.raises(AppriseImproperlyConfigured): |
| 158 |
AppriseAsset(service_timeout=float("nan")) |
| 159 |
|
| 160 |
# The private field cannot bypass the validated public argument. |
| 161 |
with pytest.raises(AppriseImproperlyConfigured): |
| 162 |
AppriseAsset(_service_timeout=99.0) |
| 163 |
|
| 164 |
|
| 165 |
def test_payload_max_size(): |
| 166 |
"asset: payload_max_size() testing" |
| 167 |
|
| 168 |
# Zero disables the cap by default. |
| 169 |
asset = AppriseAsset() |
| 170 |
assert asset._payload_max_size == 0 |
| 171 |
|
| 172 |
# Accepts a positive int, turning the cap on |
| 173 |
asset = AppriseAsset(payload_max_size=500) |
| 174 |
assert asset._payload_max_size == 500 |
| 175 |
|
| 176 |
# 0 is valid -- it is also the default, and disables the cap entirely |
| 177 |
asset = AppriseAsset(payload_max_size=0) |
| 178 |
assert asset._payload_max_size == 0 |
| 179 |
|
| 180 |
# Negative values are rejected |
| 181 |
with pytest.raises(AppriseImproperlyConfigured): |
| 182 |
AppriseAsset(payload_max_size=-1) |
| 183 |
|
| 184 |
# Non-int types are rejected -- a float character count makes no sense |
| 185 |
with pytest.raises(AppriseImproperlyConfigured): |
| 186 |
AppriseAsset(payload_max_size=12.5) |
| 187 |
|
| 188 |
with pytest.raises(AppriseImproperlyConfigured): |
| 189 |
AppriseAsset(payload_max_size="invalid") |
| 190 |
|
| 191 |
# Booleans are rejected even though bool is technically an int subclass |
| 192 |
with pytest.raises(AppriseImproperlyConfigured): |
| 193 |
AppriseAsset(payload_max_size=True) |
| 194 |
|
| 195 |
# The private field cannot bypass the validated public argument. |
| 196 |
with pytest.raises(AppriseImproperlyConfigured): |
| 197 |
AppriseAsset(_payload_max_size=250) |
| 198 |
|
| 199 |
|
| 200 |
def test_payload_buffer_threshold_and_min_buffer(): |
| 201 |
"asset: payload_buffer_threshold() / payload_min_buffer() testing" |
| 202 |
|
| 203 |
# Defaults match the documented values. |
| 204 |
asset = AppriseAsset() |
| 205 |
assert asset._payload_buffer_threshold == 10 |
| 206 |
assert asset._payload_min_buffer == 25 |
| 207 |
|
| 208 |
# Public constructor arguments configure both settings. |
| 209 |
asset = AppriseAsset(payload_buffer_threshold=3, payload_min_buffer=5) |
| 210 |
assert asset._payload_buffer_threshold == 3 |
| 211 |
assert asset._payload_min_buffer == 5 |
| 212 |
|
| 213 |
# 0 is valid for both. |
| 214 |
asset = AppriseAsset(payload_buffer_threshold=0, payload_min_buffer=0) |
| 215 |
assert asset._payload_buffer_threshold == 0 |
| 216 |
assert asset._payload_min_buffer == 0 |
| 217 |
|
| 218 |
# Negative values are rejected. |
| 219 |
with pytest.raises(AppriseImproperlyConfigured): |
| 220 |
AppriseAsset(payload_buffer_threshold=-1) |
| 221 |
|
| 222 |
with pytest.raises(AppriseImproperlyConfigured): |
| 223 |
AppriseAsset(payload_min_buffer=-1) |
| 224 |
|
| 225 |
# Non-int types are rejected. |
| 226 |
with pytest.raises(AppriseImproperlyConfigured): |
| 227 |
AppriseAsset(payload_buffer_threshold=12.5) |
| 228 |
|
| 229 |
with pytest.raises(AppriseImproperlyConfigured): |
| 230 |
AppriseAsset(payload_min_buffer="invalid") |
| 231 |
|
| 232 |
# Booleans are rejected even though bool is technically an int subclass. |
| 233 |
with pytest.raises(AppriseImproperlyConfigured): |
| 234 |
AppriseAsset(payload_buffer_threshold=True) |
| 235 |
|
| 236 |
with pytest.raises(AppriseImproperlyConfigured): |
| 237 |
AppriseAsset(payload_min_buffer=True) |
| 238 |
|
| 239 |
# Private fields cannot bypass the validated public arguments. |
| 240 |
with pytest.raises(AppriseImproperlyConfigured): |
| 241 |
AppriseAsset(_payload_buffer_threshold=2) |
| 242 |
|
| 243 |
with pytest.raises(AppriseImproperlyConfigured): |
| 244 |
AppriseAsset(_payload_min_buffer=7) |
| 245 |
|
| 246 |
|
| 247 |
def test_result_log_storage_sizes(): |
| 248 |
"""Result-log storage accepts non-negative byte limits.""" |
| 249 |
asset = AppriseAsset() |
| 250 |
assert asset.result_log_memory_size == 0 |
| 251 |
assert asset.result_log_disk_size == 0 |
| 252 |
|
| 253 |
asset = AppriseAsset( |
| 254 |
result_log_memory_size=2048, result_log_disk_size=4096 |
| 255 |
) |
| 256 |
assert asset.result_log_memory_size == 2048 |
| 257 |
assert asset.result_log_disk_size == 4096 |
| 258 |
|
| 259 |
for name in ("result_log_memory_size", "result_log_disk_size"): |
| 260 |
with pytest.raises(AppriseImproperlyConfigured): |
| 261 |
AppriseAsset(**{name: -1}) |
| 262 |
for value in (True, 1.5, "1024"): |
| 263 |
with pytest.raises(AppriseImproperlyConfigured): |
| 264 |
AppriseAsset(**{name: value}) |
| 265 |
with pytest.raises(AppriseImproperlyConfigured): |
| 266 |
AppriseAsset(**{"_{}".format(name): 1}) |
| 267 |
|
| 268 |
|
| 269 |
def test_enforce_payload_max_size(): |
| 270 |
"asset: enforce_payload_max_size() testing" |
| 271 |
|
| 272 |
# Disabled (the default): nothing is ever trimmed. |
| 273 |
asset = AppriseAsset() |
| 274 |
title, body = asset.enforce_payload_max_size("T" * 100, "B" * 1000) |
| 275 |
assert title == "T" * 100 |
| 276 |
assert body == "B" * 1000 |
| 277 |
|
| 278 |
# Already within budget: left untouched. |
| 279 |
asset = AppriseAsset(payload_max_size=2000) |
| 280 |
title, body = asset.enforce_payload_max_size("T" * 100, "B" * 1000) |
| 281 |
assert title == "T" * 100 |
| 282 |
assert body == "B" * 1000 |
| 283 |
|
| 284 |
|
| 285 |
def test_enforce_payload_max_size_absent_side(): |
| 286 |
"asset: enforce_payload_max_size() never trims a missing side" |
| 287 |
|
| 288 |
asset = AppriseAsset(payload_max_size=20) |
| 289 |
|
| 290 |
# An empty title never competes with the body for room. |
| 291 |
title, body = asset.enforce_payload_max_size("", "B" * 1000) |
| 292 |
assert title == "" |
| 293 |
assert len(body) == 20 |
| 294 |
|
| 295 |
# An empty body never competes with the title for room. |
| 296 |
title, body = asset.enforce_payload_max_size("T" * 1000, "") |
| 297 |
assert len(title) == 20 |
| 298 |
assert body == "" |
| 299 |
|
| 300 |
|
| 301 |
def test_enforce_payload_max_size_short_side_guaranteed(): |
| 302 |
"""asset: keep a short side whole when the other minimum fits""" |
| 303 |
|
| 304 |
# A short title stays whole; the body receives the remaining room. |
| 305 |
asset = AppriseAsset(payload_max_size=40) |
| 306 |
title, body = asset.enforce_payload_max_size("T" * 9, "B" * 1000) |
| 307 |
assert title == "T" * 9 |
| 308 |
assert len(body) == 31 |
| 309 |
|
| 310 |
# The same rule keeps a short body whole. |
| 311 |
title, body = asset.enforce_payload_max_size("T" * 40, "hi!") |
| 312 |
assert title == "T" * 37 |
| 313 |
assert body == "hi!" |
| 314 |
|
| 315 |
# A title exactly at the buffer width still qualifies. |
| 316 |
title, body = asset.enforce_payload_max_size("T" * 10, "B" * 1000) |
| 317 |
assert title == "T" * 10 |
| 318 |
assert len(body) == 30 |
| 319 |
|
| 320 |
|
| 321 |
def test_enforce_payload_max_size_guarantee_unaffordable(): |
| 322 |
"""asset: use proportional sharing when a guarantee cannot fit""" |
| 323 |
|
| 324 |
# Two long sides share the cap proportionally. |
| 325 |
asset = AppriseAsset(payload_max_size=20) |
| 326 |
title, body = asset.enforce_payload_max_size("T" * 20, "B" * 18) |
| 327 |
assert len(title) + len(body) == 20 |
| 328 |
assert len(title) == 10 |
| 329 |
assert len(body) == 10 |
| 330 |
|
| 331 |
# The title is short, but the body minimum cannot fit beside it. |
| 332 |
tiny_asset = AppriseAsset(payload_max_size=11) |
| 333 |
title, body = tiny_asset.enforce_payload_max_size("T" * 10, "B" * 1000) |
| 334 |
assert len(title) + len(body) == 11 |
| 335 |
assert len(title) == 1 |
| 336 |
assert len(body) == 10 |
| 337 |
|
| 338 |
# The mirrored short-body rule also falls back when space is tight. |
| 339 |
balanced_asset = AppriseAsset(payload_max_size=15) |
| 340 |
title, body = balanced_asset.enforce_payload_max_size("T" * 15, "B" * 10) |
| 341 |
assert len(title) + len(body) == 15 |
| 342 |
assert len(title) == 9 |
| 343 |
assert len(body) == 6 |
| 344 |
|
| 345 |
|
| 346 |
def test_enforce_payload_max_size_proportional_tier(): |
| 347 |
"asset: enforce_payload_max_size() proportional split for two long sides" |
| 348 |
|
| 349 |
# Both sides are longer than the buffer, so title and body share the |
| 350 |
# cap in proportion to their original lengths. |
| 351 |
asset = AppriseAsset(payload_max_size=200) |
| 352 |
title, body = asset.enforce_payload_max_size("T" * 100, "B" * 1000) |
| 353 |
assert len(title) + len(body) == 200 |
| 354 |
# The title receives 100/1100 of the budget, rounded down. |
| 355 |
assert len(title) == 18 |
| 356 |
assert len(body) == 182 |
| 357 |
|
| 358 |
# A title-heavy split keeps most of the budget for the title. |
| 359 |
heavy_asset = AppriseAsset(payload_max_size=50) |
| 360 |
title, body = heavy_asset.enforce_payload_max_size("T" * 100, "B" * 60) |
| 361 |
assert len(title) + len(body) == 50 |
| 362 |
assert len(title) > len(body) |
| 363 |
|
| 364 |
# Keep one title character when a title longer than the buffer still |
| 365 |
# has its proportional share round down to zero. |
| 366 |
title, body = asset.enforce_payload_max_size("T" * 11, "B" * 2200) |
| 367 |
assert title == "T" |
| 368 |
assert len(body) == 199 |
| 369 |
|
| 370 |
|
| 371 |
def test_enforce_payload_max_size_configurable_tiers(): |
| 372 |
"asset: payload_buffer_threshold / payload_min_buffer are tunable" |
| 373 |
|
| 374 |
# Public constructor arguments can override both settings. |
| 375 |
asset = AppriseAsset( |
| 376 |
payload_max_size=12, |
| 377 |
payload_buffer_threshold=3, |
| 378 |
payload_min_buffer=5, |
| 379 |
) |
| 380 |
assert asset._payload_buffer_threshold == 3 |
| 381 |
assert asset._payload_min_buffer == 5 |
| 382 |
|
| 383 |
# Both sides exceed the custom buffer threshold (3), so this falls |
| 384 |
# through to the proportional split. |
| 385 |
title, body = asset.enforce_payload_max_size("T" * 4, "B" * 20) |
| 386 |
assert len(title) == 2 |
| 387 |
assert len(body) == 10 |
| 388 |
|
| 389 |
# A title at or below the custom buffer threshold now qualifies for |
| 390 |
# the whole-and-guaranteed rule, using the custom (smaller) minimum. |
| 391 |
custom_asset = AppriseAsset( |
| 392 |
payload_max_size=12, |
| 393 |
payload_buffer_threshold=5, |
| 394 |
payload_min_buffer=3, |
| 395 |
) |
| 396 |
title, body = custom_asset.enforce_payload_max_size("T" * 4, "B" * 20) |
| 397 |
assert title == "T" * 4 |
| 398 |
assert len(body) == 8 |