| 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 |
from inspect import cleandoc |
| 29 |
|
| 30 |
|
| 31 |
import logging |
| 32 |
import re |
| 33 |
import sys |
| 34 |
import threading |
| 35 |
import types |
| 36 |
|
| 37 |
import pytest |
| 38 |
|
| 39 |
from apprise import Apprise, NotificationManager |
| 40 |
from apprise.logger import LogCapture |
| 41 |
from apprise.plugins import NotifyBase |
| 42 |
|
| 43 |
logging.disable(logging.CRITICAL) |
| 44 |
|
| 45 |
|
| 46 |
N_MGR = NotificationManager() |
| 47 |
|
| 48 |
|
| 49 |
def test_notification_manager_general(): |
| 50 |
""" |
| 51 |
N_MGR: Notification Manager General testing |
| 52 |
|
| 53 |
""" |
| 54 |
|
| 55 |
N_MGR.unload_modules() |
| 56 |
assert isinstance(N_MGR.schemas(), list) |
| 57 |
assert len(N_MGR.schemas()) > 0 |
| 58 |
N_MGR.unload_modules(disable_native=True) |
| 59 |
assert isinstance(N_MGR.schemas(), list) |
| 60 |
assert len(N_MGR.schemas()) == 0 |
| 61 |
|
| 62 |
N_MGR.unload_modules() |
| 63 |
assert len(N_MGR) > 0 |
| 64 |
|
| 65 |
N_MGR.unload_modules() |
| 66 |
iter(N_MGR) |
| 67 |
iter(N_MGR) |
| 68 |
|
| 69 |
N_MGR.unload_modules() |
| 70 |
assert bool(N_MGR) is False |
| 71 |
assert len(list(iter(N_MGR))) > 0 |
| 72 |
assert bool(N_MGR) |
| 73 |
|
| 74 |
N_MGR.unload_modules() |
| 75 |
assert isinstance(N_MGR.plugins(), types.GeneratorType) |
| 76 |
assert len(list(N_MGR.plugins())) > 0 |
| 77 |
N_MGR.unload_modules(disable_native=True) |
| 78 |
assert isinstance(N_MGR.plugins(), types.GeneratorType) |
| 79 |
assert len(list(N_MGR.plugins())) == 0 |
| 80 |
N_MGR.unload_modules() |
| 81 |
assert isinstance(N_MGR["json"](host="localhost"), NotifyBase) |
| 82 |
N_MGR.unload_modules() |
| 83 |
assert "json" in N_MGR |
| 84 |
|
| 85 |
|
| 86 |
class DisabledNotification(NotifyBase): |
| 87 |
|
| 88 |
enabled = False |
| 89 |
|
| 90 |
def __init__(self, *args, **kwargs): |
| 91 |
super().__init__(*args, **kwargs) |
| 92 |
|
| 93 |
def notify(self, *args, **kwargs): |
| 94 |
|
| 95 |
return True |
| 96 |
|
| 97 |
def url(self, **kwargs): |
| 98 |
|
| 99 |
return "" |
| 100 |
|
| 101 |
|
| 102 |
class GoodNotification(NotifyBase): |
| 103 |
secure_protocol = ("good", "goods") |
| 104 |
|
| 105 |
def __init__(self, *args, **kwargs): |
| 106 |
super().__init__(*args, **kwargs) |
| 107 |
|
| 108 |
def notify(self, *args, **kwargs): |
| 109 |
|
| 110 |
return True |
| 111 |
|
| 112 |
def url(self, **kwargs): |
| 113 |
|
| 114 |
return "" |
| 115 |
|
| 116 |
N_MGR.unload_modules() |
| 117 |
assert N_MGR.add(GoodNotification) |
| 118 |
assert "good" in N_MGR |
| 119 |
assert "goods" in N_MGR |
| 120 |
assert "abcd" not in N_MGR |
| 121 |
assert "xyz" not in N_MGR |
| 122 |
|
| 123 |
N_MGR.unload_modules() |
| 124 |
assert N_MGR.add(GoodNotification, "abcd") |
| 125 |
assert "good" in N_MGR |
| 126 |
assert "goods" in N_MGR |
| 127 |
assert "abcd" in N_MGR |
| 128 |
assert "xyz" not in N_MGR |
| 129 |
|
| 130 |
N_MGR.unload_modules() |
| 131 |
assert N_MGR.add(GoodNotification, ["abcd", "xYz"]) |
| 132 |
assert "good" in N_MGR |
| 133 |
assert "goods" in N_MGR |
| 134 |
assert "abcd" in N_MGR |
| 135 |
|
| 136 |
assert "xyz" in N_MGR |
| 137 |
|
| 138 |
N_MGR.unload_modules() |
| 139 |
|
| 140 |
assert N_MGR.add(GoodNotification, object) is False |
| 141 |
|
| 142 |
N_MGR.unload_modules() |
| 143 |
with pytest.raises(KeyError): |
| 144 |
del N_MGR["good"] |
| 145 |
N_MGR["good"] = GoodNotification |
| 146 |
del N_MGR["good"] |
| 147 |
|
| 148 |
N_MGR.unload_modules() |
| 149 |
N_MGR["good"] = GoodNotification |
| 150 |
assert N_MGR["good"].enabled is True |
| 151 |
N_MGR.enable_only("json", "xml") |
| 152 |
assert N_MGR["good"].enabled is False |
| 153 |
assert N_MGR["json"].enabled is True |
| 154 |
assert N_MGR["jsons"].enabled is True |
| 155 |
assert N_MGR["xml"].enabled is True |
| 156 |
assert N_MGR["xmls"].enabled is True |
| 157 |
|
| 158 |
|
| 159 |
assert len(list(N_MGR.plugins(include_disabled=False))) == 2 |
| 160 |
|
| 161 |
N_MGR.enable_only("good") |
| 162 |
assert N_MGR["good"].enabled is True |
| 163 |
assert N_MGR["json"].enabled is False |
| 164 |
assert N_MGR["jsons"].enabled is False |
| 165 |
assert N_MGR["xml"].enabled is False |
| 166 |
assert N_MGR["xmls"].enabled is False |
| 167 |
|
| 168 |
assert len(list(N_MGR.plugins(include_disabled=False))) == 1 |
| 169 |
|
| 170 |
N_MGR.unload_modules() |
| 171 |
N_MGR["disabled"] = DisabledNotification |
| 172 |
assert N_MGR["disabled"].enabled is False |
| 173 |
N_MGR.enable_only("disabled") |
| 174 |
|
| 175 |
assert N_MGR["disabled"].enabled is False |
| 176 |
|
| 177 |
N_MGR["good"] = GoodNotification |
| 178 |
assert N_MGR["good"].enabled is True |
| 179 |
|
| 180 |
|
| 181 |
N_MGR.disable("disabled") |
| 182 |
assert N_MGR["disabled"].enabled is False |
| 183 |
|
| 184 |
N_MGR.unload_modules() |
| 185 |
N_MGR.enable_only("form", "xml") |
| 186 |
for schema in N_MGR.schemas(include_disabled=False): |
| 187 |
assert re.match(r"^(form|xml)s?$", schema, re.IGNORECASE) is not None |
| 188 |
|
| 189 |
N_MGR.unload_modules() |
| 190 |
assert N_MGR["form"].enabled is True |
| 191 |
assert N_MGR["xml"].enabled is True |
| 192 |
assert N_MGR["json"].enabled is True |
| 193 |
N_MGR.enable_only("form", "xml") |
| 194 |
assert N_MGR["form"].enabled is True |
| 195 |
assert N_MGR["xml"].enabled is True |
| 196 |
assert N_MGR["json"].enabled is False |
| 197 |
|
| 198 |
N_MGR.disable("invalid", "xml") |
| 199 |
assert N_MGR["form"].enabled is True |
| 200 |
assert N_MGR["xml"].enabled is False |
| 201 |
assert N_MGR["json"].enabled is False |
| 202 |
|
| 203 |
|
| 204 |
with pytest.raises(KeyError): |
| 205 |
|
| 206 |
N_MGR["invalid"] |
| 207 |
|
| 208 |
N_MGR.unload_modules() |
| 209 |
N_MGR.disable("invalid", "xml") |
| 210 |
|
| 211 |
N_MGR.unload_modules() |
| 212 |
assert N_MGR["json"].enabled is True |
| 213 |
|
| 214 |
|
| 215 |
N_MGR.unload_modules(disable_native=True) |
| 216 |
with pytest.raises(KeyError): |
| 217 |
|
| 218 |
N_MGR["good"] |
| 219 |
|
| 220 |
N_MGR.unload_modules() |
| 221 |
assert "hello" not in N_MGR |
| 222 |
assert "good" not in N_MGR |
| 223 |
assert "goods" not in N_MGR |
| 224 |
|
| 225 |
N_MGR["hello"] = GoodNotification |
| 226 |
assert "hello" in N_MGR |
| 227 |
assert "good" in N_MGR |
| 228 |
assert "goods" in N_MGR |
| 229 |
|
| 230 |
N_MGR.unload_modules() |
| 231 |
N_MGR["good"] = GoodNotification |
| 232 |
|
| 233 |
with pytest.raises(KeyError): |
| 234 |
|
| 235 |
N_MGR["good"] = GoodNotification |
| 236 |
|
| 237 |
N_MGR.unload_modules() |
| 238 |
N_MGR.remove("good", "invalid") |
| 239 |
assert "good" not in N_MGR |
| 240 |
assert "goods" not in N_MGR |
| 241 |
|
| 242 |
|
| 243 |
def test_notification_manager_add_force_overrides_schema_without_unload(): |
| 244 |
"""Verify add(force=True) overrides existing schema without unloading.""" |
| 245 |
import sys |
| 246 |
|
| 247 |
from apprise.plugins import N_MGR |
| 248 |
|
| 249 |
class NotifyDiscordCustom: |
| 250 |
"""A minimal custom discord override used for testing.""" |
| 251 |
|
| 252 |
protocol = "discord" |
| 253 |
secure_protocol = None |
| 254 |
service_name = "Discord (Custom)" |
| 255 |
|
| 256 |
def __init__(self, *args, **kwargs): |
| 257 |
pass |
| 258 |
|
| 259 |
def send(self, *args, **kwargs): |
| 260 |
return True |
| 261 |
|
| 262 |
def url(self, **kwargs): |
| 263 |
return "discord://" |
| 264 |
|
| 265 |
|
| 266 |
N_MGR.unload_modules() |
| 267 |
N_MGR.load_modules() |
| 268 |
|
| 269 |
|
| 270 |
assert "discord" in N_MGR |
| 271 |
native_plugin = N_MGR["discord"] |
| 272 |
native_module = native_plugin.__module__ |
| 273 |
assert native_module in sys.modules |
| 274 |
|
| 275 |
|
| 276 |
assert N_MGR.add(NotifyDiscordCustom, schemas="discord") is False |
| 277 |
|
| 278 |
|
| 279 |
assert ( |
| 280 |
N_MGR.add(NotifyDiscordCustom, schemas="discord", force=True) is True |
| 281 |
) |
| 282 |
assert N_MGR["discord"] is NotifyDiscordCustom |
| 283 |
assert native_module in sys.modules |
| 284 |
|
| 285 |
|
| 286 |
def test_notification_manager_module_loading(tmpdir): |
| 287 |
""" |
| 288 |
N_MGR: Notification Manager Module Loading |
| 289 |
|
| 290 |
""" |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
N_MGR.load_modules() |
| 295 |
N_MGR.load_modules() |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
thread_count = 10 |
| 308 |
|
| 309 |
def thread_test(result, no): |
| 310 |
"""Load our apprise object with valid URLs and store our result.""" |
| 311 |
apobj = Apprise() |
| 312 |
result[no] = ( |
| 313 |
apobj.add("json://localhost") |
| 314 |
and apobj.add("form://localhost") |
| 315 |
and apobj.add("xml://localhost") |
| 316 |
) |
| 317 |
|
| 318 |
|
| 319 |
N_MGR.unload_modules() |
| 320 |
|
| 321 |
|
| 322 |
results = [None] * thread_count |
| 323 |
threads = [ |
| 324 |
threading.Thread(target=thread_test, args=(results, no)) |
| 325 |
for no in range(thread_count) |
| 326 |
] |
| 327 |
|
| 328 |
|
| 329 |
for t in threads: |
| 330 |
t.start() |
| 331 |
|
| 332 |
for t in threads: |
| 333 |
t.join() |
| 334 |
|
| 335 |
|
| 336 |
for result in results: |
| 337 |
assert result is True |
| 338 |
|
| 339 |
|
| 340 |
def test_notification_manager_decorators(tmpdir): |
| 341 |
""" |
| 342 |
N_MGR: Notification Manager Decorator testing |
| 343 |
|
| 344 |
""" |
| 345 |
|
| 346 |
|
| 347 |
notify_hook = tmpdir.mkdir("goodmodule").join("__init__.py") |
| 348 |
notify_hook.write( |
| 349 |
cleandoc(""" |
| 350 |
from apprise.decorators import notify |
| 351 |
|
| 352 |
# We want to trigger on anyone who configures a call to clihook:// |
| 353 |
@notify(on="clihooka") |
| 354 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 355 |
# A simple test - print to screen |
| 356 |
print("A {}: {} - {}".format(notify_type, title, body)) |
| 357 |
|
| 358 |
# No return (so a return of None) get's translated to True |
| 359 |
|
| 360 |
# Define another in the same file; uppercase goes to lower |
| 361 |
@notify(on="CLIhookb") |
| 362 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 363 |
# A simple test - print to screen |
| 364 |
print("B {}: {} - {}".format(notify_type, title, body)) |
| 365 |
|
| 366 |
# No return (so a return of None) get's translated to True |
| 367 |
""") |
| 368 |
) |
| 369 |
|
| 370 |
N_MGR.module_detection(str(notify_hook)) |
| 371 |
|
| 372 |
assert "clihooka" in N_MGR |
| 373 |
assert "clihookb" in N_MGR |
| 374 |
N_MGR.unload_modules() |
| 375 |
assert "clihooka" not in N_MGR |
| 376 |
assert "clihookb" not in N_MGR |
| 377 |
|
| 378 |
N_MGR.module_detection(str(notify_hook)) |
| 379 |
assert "clihooka" in N_MGR |
| 380 |
assert "clihookb" in N_MGR |
| 381 |
del N_MGR["clihookb"] |
| 382 |
assert "clihooka" in N_MGR |
| 383 |
assert "clihookb" not in N_MGR |
| 384 |
del N_MGR["clihooka"] |
| 385 |
assert "clihooka" not in N_MGR |
| 386 |
assert "clihookb" not in N_MGR |
| 387 |
|
| 388 |
|
| 389 |
notify_base = tmpdir.mkdir("plugins") |
| 390 |
notify_test = notify_base.join("NotifyTest.py") |
| 391 |
notify_test.write( |
| 392 |
cleandoc(""" |
| 393 |
# |
| 394 |
# Bare Minimum Valid Object |
| 395 |
# |
| 396 |
from apprise.plugins import NotifyBase |
| 397 |
from apprise.common import NotifyType |
| 398 |
|
| 399 |
class NotifyTest(NotifyBase): |
| 400 |
|
| 401 |
service_name = 'Test' |
| 402 |
|
| 403 |
# The services URL |
| 404 |
service_url = 'https://github.com/caronc/apprise/' |
| 405 |
|
| 406 |
# Define our protocol |
| 407 |
secure_protocol = 'myservice' |
| 408 |
|
| 409 |
# A URL that takes you to the setup/help of the specific protocol |
| 410 |
setup_url = 'https://appriseit.com/services/myservice/' |
| 411 |
|
| 412 |
# Define object templates |
| 413 |
templates = ( |
| 414 |
'{schema}://', |
| 415 |
) |
| 416 |
|
| 417 |
def __init__(self, **kwargs): |
| 418 |
super().__init__(**kwargs) |
| 419 |
|
| 420 |
def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs): |
| 421 |
return True |
| 422 |
|
| 423 |
def url(self): |
| 424 |
return 'myservice://' |
| 425 |
""") |
| 426 |
) |
| 427 |
assert "myservice" not in N_MGR |
| 428 |
N_MGR.load_modules(path=str(notify_base)) |
| 429 |
assert "myservice" in N_MGR |
| 430 |
del N_MGR["myservice"] |
| 431 |
assert "myservice" not in N_MGR |
| 432 |
|
| 433 |
assert "myservice" not in N_MGR |
| 434 |
N_MGR.load_modules(path=str(notify_base)) |
| 435 |
|
| 436 |
|
| 437 |
assert "myservice" not in N_MGR |
| 438 |
N_MGR.load_modules(path=str(notify_base), force=True) |
| 439 |
assert "myservice" in N_MGR |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
N_MGR.load_modules(path=str(notify_base)) |
| 444 |
|
| 445 |
assert "myservice" in N_MGR |
| 446 |
|
| 447 |
|
| 448 |
N_MGR.load_modules(path=str(notify_base), force=True) |
| 449 |
N_MGR.load_modules(path=str(notify_base), force=True) |
| 450 |
|
| 451 |
|
| 452 |
def test_notification_manager_add_force_returns_false_if_conflict_persists( |
| 453 |
monkeypatch: pytest.MonkeyPatch, |
| 454 |
) -> None: |
| 455 |
""" |
| 456 |
N_MGR.add(force=True) must still fail safely if conflicts persist after the |
| 457 |
attempted unmap. |
| 458 |
|
| 459 |
This explicitly targets the defensive re-check branch in add(). |
| 460 |
""" |
| 461 |
|
| 462 |
N_MGR.unload_modules() |
| 463 |
N_MGR.load_modules() |
| 464 |
assert "discord" in N_MGR |
| 465 |
|
| 466 |
class NotifyDiscordCustom: |
| 467 |
protocol = "discord" |
| 468 |
secure_protocol = None |
| 469 |
service_name = "Discord (Custom)" |
| 470 |
|
| 471 |
def __init__(self, *args, **kwargs) -> None: |
| 472 |
pass |
| 473 |
|
| 474 |
def send(self, *args, **kwargs) -> bool: |
| 475 |
return True |
| 476 |
|
| 477 |
def url(self, **kwargs) -> str: |
| 478 |
return "discord://" |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
def _noop_remove(*args, **kwargs) -> None: |
| 484 |
return None |
| 485 |
|
| 486 |
monkeypatch.setattr(N_MGR, "remove", _noop_remove) |
| 487 |
|
| 488 |
assert ( |
| 489 |
N_MGR.add(NotifyDiscordCustom, schemas="discord", force=True) is False |
| 490 |
) |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
def _make_dep_plugin(schema, *deps): |
| 499 |
"""Return a minimal NotifyBase subclass that declares *deps in |
| 500 |
runtime_deps() and is always enabled.""" |
| 501 |
|
| 502 |
class _Plugin(NotifyBase): |
| 503 |
protocol = schema |
| 504 |
enabled = True |
| 505 |
|
| 506 |
def notify(self, *args, **kwargs): |
| 507 |
return True |
| 508 |
|
| 509 |
def url(self, **kwargs): |
| 510 |
return f"{schema}://" |
| 511 |
|
| 512 |
@staticmethod |
| 513 |
def runtime_deps(): |
| 514 |
return deps |
| 515 |
|
| 516 |
_Plugin.__name__ = f"Notify_{schema}" |
| 517 |
return _Plugin |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
def test_manager_dep_counter_built_on_load(): |
| 524 |
"""_dep_counter is populated after native modules are loaded.""" |
| 525 |
N_MGR.unload_modules() |
| 526 |
|
| 527 |
assert len(N_MGR.schemas()) > 0 |
| 528 |
|
| 529 |
|
| 530 |
assert isinstance(N_MGR._dep_counter, dict) |
| 531 |
|
| 532 |
|
| 533 |
for plugin in N_MGR.plugins(include_disabled=False): |
| 534 |
for lib in plugin.runtime_deps(): |
| 535 |
assert lib in N_MGR._dep_counter |
| 536 |
assert N_MGR._dep_counter[lib] >= 1 |
| 537 |
|
| 538 |
|
| 539 |
def test_manager_dep_counter_custom_plugins(): |
| 540 |
"""_dep_counter reflects only enabled plugins with runtime_deps.""" |
| 541 |
N_MGR.unload_modules() |
| 542 |
|
| 543 |
PlugA = _make_dep_plugin("plugA", "fakelib1") |
| 544 |
PlugB = _make_dep_plugin("plugB", "fakelib1", "fakelib2") |
| 545 |
PlugC = _make_dep_plugin("plugC") |
| 546 |
|
| 547 |
N_MGR["plugA"] = PlugA |
| 548 |
N_MGR["plugB"] = PlugB |
| 549 |
N_MGR["plugC"] = PlugC |
| 550 |
|
| 551 |
|
| 552 |
N_MGR._build_dep_counter() |
| 553 |
|
| 554 |
assert N_MGR._dep_counter.get("fakelib1", 0) >= 2 |
| 555 |
assert N_MGR._dep_counter.get("fakelib2", 0) >= 1 |
| 556 |
|
| 557 |
assert "fakelib_none" not in N_MGR._dep_counter |
| 558 |
|
| 559 |
|
| 560 |
def test_manager_disable_decrements_counter(): |
| 561 |
"""disable() decrements the dep counter for the disabled plugin's libs.""" |
| 562 |
N_MGR.unload_modules() |
| 563 |
|
| 564 |
PlugA = _make_dep_plugin("solodep", "uniquelib99") |
| 565 |
N_MGR["solodep"] = PlugA |
| 566 |
N_MGR._build_dep_counter() |
| 567 |
|
| 568 |
assert N_MGR._dep_counter.get("uniquelib99", 0) >= 1 |
| 569 |
before = N_MGR._dep_counter["uniquelib99"] |
| 570 |
|
| 571 |
N_MGR.disable("solodep") |
| 572 |
assert N_MGR._dep_counter.get("uniquelib99", 0) == before - 1 |
| 573 |
|
| 574 |
|
| 575 |
def test_manager_shared_dep_not_evicted_until_all_disabled(): |
| 576 |
"""A shared library is not evicted until every plugin using it is off.""" |
| 577 |
import sys |
| 578 |
|
| 579 |
N_MGR.unload_modules() |
| 580 |
N_MGR.evict_on_disable = True |
| 581 |
|
| 582 |
|
| 583 |
PlugX = _make_dep_plugin("sharedX", "sharedfakelib") |
| 584 |
PlugY = _make_dep_plugin("sharedY", "sharedfakelib") |
| 585 |
|
| 586 |
|
| 587 |
fake_mod = types.ModuleType("sharedfakelib") |
| 588 |
sys.modules["sharedfakelib"] = fake_mod |
| 589 |
|
| 590 |
N_MGR["sharedX"] = PlugX |
| 591 |
N_MGR["sharedY"] = PlugY |
| 592 |
N_MGR._build_dep_counter() |
| 593 |
|
| 594 |
assert N_MGR._dep_counter.get("sharedfakelib", 0) >= 2 |
| 595 |
|
| 596 |
0) |
| 597 |
N_MGR.disable("sharedX") |
| 598 |
assert "sharedfakelib" in sys.modules |
| 599 |
|
| 600 |
ed |
| 601 |
N_MGR.disable("sharedY") |
| 602 |
assert "sharedfakelib" not in sys.modules |
| 603 |
|
| 604 |
N_MGR.evict_on_disable = False |
| 605 |
|
| 606 |
|
| 607 |
def test_manager_evict_on_disable_false_by_default(): |
| 608 |
"""evict_on_disable defaults to False; no eviction even at counter 0.""" |
| 609 |
import sys |
| 610 |
|
| 611 |
N_MGR.unload_modules() |
| 612 |
assert N_MGR.evict_on_disable is False |
| 613 |
|
| 614 |
PlugZ = _make_dep_plugin("nodiscard", "nodiscardlib") |
| 615 |
fake_mod = types.ModuleType("nodiscardlib") |
| 616 |
sys.modules["nodiscardlib"] = fake_mod |
| 617 |
|
| 618 |
N_MGR["nodiscard"] = PlugZ |
| 619 |
N_MGR._build_dep_counter() |
| 620 |
|
| 621 |
N_MGR.disable("nodiscard") |
| 622 |
|
| 623 |
|
| 624 |
assert "nodiscardlib" in sys.modules |
| 625 |
|
| 626 |
|
| 627 |
del sys.modules["nodiscardlib"] |
| 628 |
|
| 629 |
|
| 630 |
def test_manager_evict_library_graceful_keyerror(): |
| 631 |
"""_evict_library handles a missing sys.modules key gracefully.""" |
| 632 |
import sys |
| 633 |
|
| 634 |
N_MGR.unload_modules() |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
fake_mod = types.ModuleType("ghostlib") |
| 639 |
sys.modules["ghostlib"] = fake_mod |
| 640 |
del sys.modules["ghostlib"] |
| 641 |
|
| 642 |
|
| 643 |
N_MGR._evict_library("ghostlib") |
| 644 |
|
| 645 |
|
| 646 |
def test_manager_evict_library_removes_submodules(): |
| 647 |
"""_evict_library removes the top-level package and all its submodules.""" |
| 648 |
import sys |
| 649 |
|
| 650 |
N_MGR.unload_modules() |
| 651 |
|
| 652 |
root = types.ModuleType("mypkg") |
| 653 |
sub = types.ModuleType("mypkg.sub") |
| 654 |
deep = types.ModuleType("mypkg.sub.deep") |
| 655 |
|
| 656 |
sys.modules["mypkg"] = root |
| 657 |
sys.modules["mypkg.sub"] = sub |
| 658 |
sys.modules["mypkg.sub.deep"] = deep |
| 659 |
|
| 660 |
N_MGR._evict_library("mypkg") |
| 661 |
|
| 662 |
assert "mypkg" not in sys.modules |
| 663 |
assert "mypkg.sub" not in sys.modules |
| 664 |
assert "mypkg.sub.deep" not in sys.modules |
| 665 |
|
| 666 |
|
| 667 |
def test_manager_enable_only_updates_dep_counter(): |
| 668 |
"""enable_only() decrements counters for disabled plugins. |
| 669 |
|
| 670 |
We isolate this test to fake libraries only to avoid evicting real native |
| 671 |
extensions (e.g. cryptography/PyO3) that cannot be safely re-imported in |
| 672 |
the same interpreter session. |
| 673 |
""" |
| 674 |
import sys |
| 675 |
|
| 676 |
N_MGR.unload_modules() |
| 677 |
N_MGR.evict_on_disable = True |
| 678 |
|
| 679 |
PlugKeep = _make_dep_plugin("keepme2", "keeplib2") |
| 680 |
PlugDrop = _make_dep_plugin("dropme2", "droplib2") |
| 681 |
|
| 682 |
fake_keep = types.ModuleType("keeplib2") |
| 683 |
fake_drop = types.ModuleType("droplib2") |
| 684 |
sys.modules["keeplib2"] = fake_keep |
| 685 |
sys.modules["droplib2"] = fake_drop |
| 686 |
|
| 687 |
N_MGR["keepme2"] = PlugKeep |
| 688 |
N_MGR["dropme2"] = PlugDrop |
| 689 |
|
| 690 |
|
| 691 |
al |
| 692 |
|
| 693 |
N_MGR._dep_counter["keeplib2"] = 1 |
| 694 |
N_MGR._dep_counter["droplib2"] = 1 |
| 695 |
|
| 696 |
|
| 697 |
N_MGR.disable("dropme2") |
| 698 |
|
| 699 |
assert N_MGR["keepme2"].enabled is True |
| 700 |
assert N_MGR["dropme2"].enabled is False |
| 701 |
assert "keeplib2" in sys.modules ed |
| 702 |
assert "droplib2" not in sys.modules ed |
| 703 |
|
| 704 |
|
| 705 |
del sys.modules["keeplib2"] |
| 706 |
N_MGR.evict_on_disable = False |
| 707 |
|
| 708 |
|
| 709 |
def test_manager_unload_resets_dep_counter(): |
| 710 |
"""unload_modules() resets _dep_counter to an empty dict. |
| 711 |
|
| 712 |
Seeds the counter via a fake plugin rather than relying on native |
| 713 |
plugins whose optional libraries may not be installed (the minimal |
| 714 |
tox env disables all such plugins, leaving _dep_counter empty after |
| 715 |
a native load). |
| 716 |
""" |
| 717 |
N_MGR.unload_modules() |
| 718 |
|
| 719 |
PlugR = _make_dep_plugin("resettest_xyz", "resetlib_xyz") |
| 720 |
N_MGR["resettest_xyz"] = PlugR |
| 721 |
N_MGR._build_dep_counter() |
| 722 |
assert N_MGR._dep_counter.get("resetlib_xyz", 0) >= 1 |
| 723 |
|
| 724 |
N_MGR.unload_modules() |
| 725 |
assert N_MGR._dep_counter == {} |
| 726 |
|
| 727 |
|
| 728 |
def test_manager_known_plugin_runtime_deps(): |
| 729 |
"""Plugins known to carry optional deps must declare runtime_deps().""" |
| 730 |
N_MGR.unload_modules() |
| 731 |
|
| 732 |
from apprise.plugins.blink1 import NotifyBlink1 |
| 733 |
from apprise.plugins.fcm import NotifyFCM |
| 734 |
from apprise.plugins.growl import NotifyGrowl |
| 735 |
from apprise.plugins.mqtt import NotifyMQTT |
| 736 |
from apprise.plugins.simplepush import NotifySimplePush |
| 737 |
from apprise.plugins.smpp import NotifySMPP |
| 738 |
from apprise.plugins.vapid import NotifyVapid |
| 739 |
from apprise.plugins.xmpp.base import NotifyXMPP |
| 740 |
|
| 741 |
assert "hid" in NotifyBlink1.runtime_deps() |
| 742 |
assert "paho" in NotifyMQTT.runtime_deps() |
| 743 |
assert "gntp" in NotifyGrowl.runtime_deps() |
| 744 |
assert "smpplib" in NotifySMPP.runtime_deps() |
| 745 |
assert "slixmpp" in NotifyXMPP.runtime_deps() |
| 746 |
assert "cryptography" in NotifySimplePush.runtime_deps() |
| 747 |
assert "cryptography" in NotifyFCM.runtime_deps() |
| 748 |
assert "cryptography" in NotifyVapid.runtime_deps() |
| 749 |
|
| 750 |
|
| 751 |
assert NotifyBase.runtime_deps() == () |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
def test_manager_len_already_loaded(): |
| 760 |
"""Verify __len__ works correctly when modules are already loaded.""" |
| 761 |
N_MGR.unload_modules() |
| 762 |
|
| 763 |
_ = len(N_MGR) |
| 764 |
|
| 765 |
assert len(N_MGR) > 0 |
| 766 |
|
| 767 |
|
| 768 |
def test_manager_unload_orphan_custom_module(): |
| 769 |
"""Verify unload_modules() skips custom module map entries whose |
| 770 |
name is not present in the native module map.""" |
| 771 |
N_MGR.unload_modules() |
| 772 |
N_MGR.load_modules() |
| 773 |
|
| 774 |
|
| 775 |
N_MGR._custom_module_map["orphan_key_xyz"] = { |
| 776 |
"name": "orphan_module_xyz_not_native", |
| 777 |
"path": "/fake/path.py", |
| 778 |
"notify": {}, |
| 779 |
} |
| 780 |
N_MGR.unload_modules() |
| 781 |
assert "orphan_key_xyz" not in N_MGR._custom_module_map |
| 782 |
|
| 783 |
|
| 784 |
def test_module_detection_invalid_paths(): |
| 785 |
"""Verify module_detection returns early for invalid path inputs.""" |
| 786 |
N_MGR.unload_modules() |
| 787 |
N_MGR.module_detection(None) |
| 788 |
N_MGR.module_detection([]) |
| 789 |
N_MGR.module_detection(123) |
| 790 |
|
| 791 |
|
| 792 |
def test_module_detection_nonexistent_path(): |
| 793 |
"""Verify a path that does not exist is silently skipped.""" |
| 794 |
N_MGR.unload_modules() |
| 795 |
N_MGR.module_detection(["/tmp/__no_such_path_xyz_abc_123__"]) |
| 796 |
|
| 797 |
|
| 798 |
def test_module_detection_directory_scan(tmpdir): |
| 799 |
"""Scans a plain directory (no __init__.py at root) containing several |
| 800 |
fixture files that each exercise a different branch.""" |
| 801 |
N_MGR.unload_modules() |
| 802 |
|
| 803 |
|
| 804 |
tmpdir.join(".hidden_xyz").write("") |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
tmpdir.join("noext_xyz").write("") |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
tmpdir.join("badimport_xyz.py").write( |
| 813 |
"import this_library_does_not_exist_xyz_abc\n" |
| 814 |
) |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
tmpdir.join("nohooks_xyz.py").write("x = 1\n") |
| 819 |
|
| 820 |
|
| 821 |
tmpdir.mkdir("subpkg_no_init_xyz") |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
cached_sub = tmpdir.mkdir("subpkg_cached_xyz") |
| 827 |
cached_init = cached_sub.join("__init__.py") |
| 828 |
cached_init.write("# cached\n") |
| 829 |
N_MGR._paths_previously_scanned.add(str(cached_init)) |
| 830 |
|
| 831 |
N_MGR.module_detection(str(tmpdir)) |
| 832 |
N_MGR.unload_modules() |
| 833 |
|
| 834 |
|
| 835 |
def test_module_detection_package_dir(tmpdir): |
| 836 |
"""Directory with __init__.py: already-scanned path is skipped; |
| 837 |
fresh path is imported but yields no hooks.""" |
| 838 |
N_MGR.unload_modules() |
| 839 |
|
| 840 |
|
| 841 |
pkgdir = tmpdir.mkdir("mypkg_xyz") |
| 842 |
pkg_path = str(pkgdir) |
| 843 |
init_path = str(pkgdir.join("__init__.py")) |
| 844 |
pkgdir.join("__init__.py").write("# no hooks\n") |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
N_MGR._paths_previously_scanned.add(init_path) |
| 849 |
N_MGR.module_detection(pkg_path) |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
N_MGR._paths_previously_scanned.discard(init_path) |
| 854 |
N_MGR._paths_previously_scanned.discard(pkg_path) |
| 855 |
N_MGR.module_detection(pkg_path) |
| 856 |
N_MGR.unload_modules() |
| 857 |
|
| 858 |
|
| 859 |
def test_module_detection_non_matching_file(tmpdir): |
| 860 |
"""File path whose basename fails module_re is silently ignored.""" |
| 861 |
N_MGR.unload_modules() |
| 862 |
|
| 863 |
|
| 864 |
hidden = tmpdir.join(".hidden_direct_xyz") |
| 865 |
hidden.write("") |
| 866 |
|
| 867 |
N_MGR.module_detection(str(hidden)) |
| 868 |
N_MGR.unload_modules() |
| 869 |
|
| 870 |
|
| 871 |
def test_module_detection_reload_existing(tmpdir): |
| 872 |
"""_import_module clears and re-registers a custom module that was |
| 873 |
previously loaded into _custom_module_map.""" |
| 874 |
N_MGR.unload_modules() |
| 875 |
|
| 876 |
hook = tmpdir.mkdir("reloadpkg_xyz").join("__init__.py") |
| 877 |
hook.write( |
| 878 |
cleandoc(""" |
| 879 |
from apprise.decorators import notify |
| 880 |
|
| 881 |
@notify(on="reloadhookxyz") |
| 882 |
def handler(body, title, notify_type, *args, **kwargs): |
| 883 |
pass |
| 884 |
""") |
| 885 |
) |
| 886 |
|
| 887 |
ap |
| 888 |
N_MGR.module_detection(str(hook)) |
| 889 |
assert "reloadhookxyz" in N_MGR |
| 890 |
|
| 891 |
|
| 892 |
N_MGR._paths_previously_scanned.discard(str(hook)) |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
N_MGR.module_detection(str(hook)) |
| 897 |
assert "reloadhookxyz" in N_MGR |
| 898 |
N_MGR.unload_modules() |
| 899 |
|
| 900 |
|
| 901 |
def test_build_dep_counter_empty_map(): |
| 902 |
"""_build_dep_counter returns early when _module_map is empty.""" |
| 903 |
|
| 904 |
N_MGR.unload_modules(disable_native=True) |
| 905 |
N_MGR._build_dep_counter() |
| 906 |
assert N_MGR._dep_counter == {} |
| 907 |
|
| 908 |
|
| 909 |
def test_build_dep_counter_no_runtime_deps_attr(): |
| 910 |
"""Plugin without a callable runtime_deps is skipped by |
| 911 |
_build_dep_counter.""" |
| 912 |
N_MGR.unload_modules() |
| 913 |
N_MGR.load_modules() |
| 914 |
|
| 915 |
class MinimalPlugin: |
| 916 |
"""Minimal plugin: enabled but has no runtime_deps attribute.""" |
| 917 |
|
| 918 |
enabled = True |
| 919 |
|
| 920 |
N_MGR._module_map["minimal_test_xyz_657"] = { |
| 921 |
"plugin": {MinimalPlugin}, |
| 922 |
"module": None, |
| 923 |
"path": "apprise.adhoc.minimal_test_xyz_657", |
| 924 |
"native": False, |
| 925 |
} |
| 926 |
|
| 927 |
N_MGR._build_dep_counter() |
| 928 |
N_MGR.unload_modules() |
| 929 |
|
| 930 |
|
| 931 |
def test_update_dep_counter_no_runtime_deps(): |
| 932 |
"""_update_dep_counter returns early when the plugin has no callable |
| 933 |
runtime_deps.""" |
| 934 |
N_MGR.unload_modules() |
| 935 |
|
| 936 |
class NoDepPlugin: |
| 937 |
"""Plugin with no runtime_deps attribute at all.""" |
| 938 |
|
| 939 |
|
| 940 |
N_MGR._update_dep_counter(NoDepPlugin, -1) |
| 941 |
|
| 942 |
|
| 943 |
def test_evict_library_keyerror_race(monkeypatch): |
| 944 |
"""KeyError during sys.modules eviction is handled gracefully. |
| 945 |
|
| 946 |
sys.modules is replaced with a subclass whose __delitem__ always |
| 947 |
raises KeyError. The key is found but the delete fails, exercising |
| 948 |
the except-KeyError branch. With evicted == 0 and to_remove |
| 949 |
non-empty the elif warning branch is also reached. |
| 950 |
""" |
| 951 |
N_MGR.unload_modules() |
| 952 |
|
| 953 |
class BrokenModulesDict(dict): |
| 954 |
def __delitem__(self, key): |
| 955 |
raise KeyError(key) |
| 956 |
|
| 957 |
broken = BrokenModulesDict(sys.modules) |
| 958 |
broken["evict_race_lib_xyz"] = types.ModuleType("evict_race_lib_xyz") |
| 959 |
monkeypatch.setattr(sys, "modules", broken) |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
N_MGR._evict_library("evict_race_lib_xyz") |
| 964 |
|
| 965 |
|
| 966 |
def test_load_modules_import_fallback_fails(tmpdir): |
| 967 |
"""__import__ raises ImportError and the path-based fallback also |
| 968 |
returns None because the file itself fails to import.""" |
| 969 |
N_MGR.unload_modules() |
| 970 |
|
| 971 |
|
| 972 |
tmpdir.join("alwaysfail_xyz.py").write( |
| 973 |
"import this_library_does_not_exist_xyz_abc\n" |
| 974 |
) |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
N_MGR.load_modules(path=str(tmpdir)) |
| 979 |
N_MGR.unload_modules() |
| 980 |
|
| 981 |
|
| 982 |
def test_load_modules_no_valid_class(tmpdir): |
| 983 |
"""Module loads successfully but contains no qualifying class, so |
| 984 |
module_class stays None and the module is skipped.""" |
| 985 |
N_MGR.unload_modules() |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
tmpdir.join("notifyempty_xyz.py").write('"""Empty module."""\n') |
| 991 |
|
| 992 |
N_MGR.load_modules(path=str(tmpdir)) |
| 993 |
N_MGR.unload_modules() |
| 994 |
|
| 995 |
|
| 996 |
def test_load_modules_schema_conflict(tmpdir): |
| 997 |
"""A schema already registered is logged as a conflict and |
| 998 |
skipped.""" |
| 999 |
N_MGR.unload_modules() |
| 1000 |
N_MGR.load_modules() |
| 1001 |
|
| 1002 |
tmpdir.join("notifyconflict_xyz.py").write( |
| 1003 |
cleandoc(""" |
| 1004 |
from apprise.plugins.base import NotifyBase |
| 1005 |
|
| 1006 |
class NotifyConflict(NotifyBase): |
| 1007 |
service_name = "Conflict" |
| 1008 |
protocol = "json" |
| 1009 |
secure_protocol = None |
| 1010 |
service_url = "https://example.com" |
| 1011 |
setup_url = "https://example.com" |
| 1012 |
notify_url = "" |
| 1013 |
templates = ('{schema}://',) |
| 1014 |
|
| 1015 |
def send(self, body, title="", **kwargs): |
| 1016 |
return True |
| 1017 |
|
| 1018 |
def url(self, **kwargs): |
| 1019 |
return "json://" |
| 1020 |
""") |
| 1021 |
) |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
N_MGR.load_modules(path=str(tmpdir)) |
| 1027 |
N_MGR.unload_modules() |
| 1028 |
|
| 1029 |
|
| 1030 |
def test_notification_manager_no_import_failures(): |
| 1031 |
"""Verify all native plugins load without TRACE import errors.""" |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
logging.disable(logging.NOTSET) |
| 1036 |
|
| 1037 |
|
| 1038 |
N_MGR.unload_modules() |
| 1039 |
|
| 1040 |
with LogCapture(level=logging.TRACE) as stream: |
| 1041 |
|
| 1042 |
len(N_MGR) |
| 1043 |
|
| 1044 |
|
| 1045 |
content = stream.getvalue() |
| 1046 |
|
| 1047 |
|
| 1048 |
logging.disable(logging.CRITICAL) |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
failures = [ |
| 1054 |
line for line in content.splitlines() if "import failed" in line |
| 1055 |
] |
| 1056 |
assert not failures, "Plugin import failures detected:\n" + "\n".join( |
| 1057 |
failures |
| 1058 |
) |
| 1059 |
|
| 1060 |
|
| 1061 |
N_MGR.unload_modules() |