| 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 |
from importlib import reload |
| 29 |
from importlib.metadata import PackageNotFoundError |
| 30 |
from inspect import cleandoc |
| 31 |
import json |
| 32 |
|
| 33 |
# Disable logging for a cleaner testing output |
| 34 |
import logging |
| 35 |
import os |
| 36 |
from os.path import dirname, join |
| 37 |
import re |
| 38 |
import sys |
| 39 |
from typing import ClassVar |
| 40 |
from unittest import mock |
| 41 |
|
| 42 |
from click.testing import CliRunner |
| 43 |
from helpers import environ |
| 44 |
import pytest |
| 45 |
import requests |
| 46 |
|
| 47 |
from apprise import ( |
| 48 |
Apprise, |
| 49 |
AppriseAsset, |
| 50 |
AppriseResultStatus, |
| 51 |
NotificationManager, |
| 52 |
NotifyBase, |
| 53 |
cli, |
| 54 |
) |
| 55 |
from apprise.locale import gettext_lazy as _ |
| 56 |
from apprise.plugins.base import RequirementsSpec |
| 57 |
|
| 58 |
logging.disable(logging.CRITICAL) |
| 59 |
|
| 60 |
# Grant access to our Notification Manager Singleton |
| 61 |
N_MGR = NotificationManager() |
| 62 |
|
| 63 |
|
| 64 |
def test_apprise_cli_nux_env(tmpdir): |
| 65 |
""" |
| 66 |
CLI: Nux Environment |
| 67 |
|
| 68 |
""" |
| 69 |
|
| 70 |
class GoodNotification(NotifyBase): |
| 71 |
def __init__(self, *args, **kwargs): |
| 72 |
super().__init__(*args, **kwargs) |
| 73 |
|
| 74 |
def notify(self, **kwargs): |
| 75 |
# Pretend everything is okay (when passing --disable-async) |
| 76 |
return True |
| 77 |
|
| 78 |
async def async_notify(self, **kwargs): |
| 79 |
# Pretend everything is okay |
| 80 |
return True |
| 81 |
|
| 82 |
def url(self, *args, **kwargs): |
| 83 |
# Support url() |
| 84 |
return "good://" |
| 85 |
|
| 86 |
class BadNotification(NotifyBase): |
| 87 |
def __init__(self, *args, **kwargs): |
| 88 |
super().__init__(*args, **kwargs) |
| 89 |
|
| 90 |
async def async_notify(self, **kwargs): |
| 91 |
# Pretend everything is okay |
| 92 |
return False |
| 93 |
|
| 94 |
def url(self, *args, **kwargs): |
| 95 |
# Support url() |
| 96 |
return "bad://" |
| 97 |
|
| 98 |
# Set up our notification types |
| 99 |
N_MGR["good"] = GoodNotification |
| 100 |
N_MGR["bad"] = BadNotification |
| 101 |
|
| 102 |
runner = CliRunner() |
| 103 |
result = runner.invoke(cli.main) |
| 104 |
# no services specified; we return 1 (non-zero) |
| 105 |
assert result.exit_code == 1 |
| 106 |
|
| 107 |
result = runner.invoke(cli.main, ["-v"]) |
| 108 |
assert result.exit_code == 1 |
| 109 |
|
| 110 |
result = runner.invoke(cli.main, ["-vv"]) |
| 111 |
assert result.exit_code == 1 |
| 112 |
|
| 113 |
result = runner.invoke(cli.main, ["-vvv"]) |
| 114 |
assert result.exit_code == 1 |
| 115 |
|
| 116 |
result = runner.invoke(cli.main, ["-vvvv"]) |
| 117 |
assert result.exit_code == 1 |
| 118 |
|
| 119 |
# Display version information and exit |
| 120 |
result = runner.invoke(cli.main, ["-V"]) |
| 121 |
assert result.exit_code == 0 |
| 122 |
|
| 123 |
result = runner.invoke( |
| 124 |
cli.main, |
| 125 |
[ |
| 126 |
"-t", |
| 127 |
"test title", |
| 128 |
"-b", |
| 129 |
"test body", |
| 130 |
"good://localhost", |
| 131 |
], |
| 132 |
) |
| 133 |
assert result.exit_code == 0 |
| 134 |
|
| 135 |
with mock.patch("requests.request") as mock_request: |
| 136 |
# Prepare Mock |
| 137 |
mock_request.return_value = requests.Request() |
| 138 |
mock_request.return_value.status_code = requests.codes.ok |
| 139 |
|
| 140 |
result = runner.invoke( |
| 141 |
cli.main, |
| 142 |
[ |
| 143 |
"-t", |
| 144 |
"test title", |
| 145 |
"-b", |
| 146 |
"test body\\nsNewLine", |
| 147 |
# Test using interpret escapes |
| 148 |
"-e", |
| 149 |
# Use our JSON query |
| 150 |
"json://localhost", |
| 151 |
], |
| 152 |
) |
| 153 |
assert result.exit_code == 0 |
| 154 |
|
| 155 |
# Test our call count |
| 156 |
assert mock_request.call_count == 1 |
| 157 |
details = mock_request.call_args_list[0] |
| 158 |
assert details[0][0] == "POST" |
| 159 |
|
| 160 |
# Our string is now escaped correctly |
| 161 |
assert ( |
| 162 |
json.loads(details[1]["data"]).get("message", "") |
| 163 |
== "test body\nsNewLine" |
| 164 |
) |
| 165 |
|
| 166 |
# Reset |
| 167 |
mock_request.reset_mock() |
| 168 |
|
| 169 |
result = runner.invoke( |
| 170 |
cli.main, |
| 171 |
[ |
| 172 |
"-t", |
| 173 |
"test title", |
| 174 |
"-b", |
| 175 |
"test body\\nsNewLine", |
| 176 |
# No -e switch at all (so we don't escape the above) |
| 177 |
# Use our JSON query |
| 178 |
"json://localhost", |
| 179 |
], |
| 180 |
) |
| 181 |
assert result.exit_code == 0 |
| 182 |
|
| 183 |
# Test our call count |
| 184 |
assert mock_request.call_count == 1 |
| 185 |
details = mock_request.call_args_list[0] |
| 186 |
assert details[0][0] == "POST" |
| 187 |
|
| 188 |
# Our string is now escaped correctly |
| 189 |
assert ( |
| 190 |
json.loads(details[1]["data"]).get("message", "") |
| 191 |
== "test body\\nsNewLine" |
| 192 |
) |
| 193 |
|
| 194 |
# Run in synchronous mode |
| 195 |
result = runner.invoke( |
| 196 |
cli.main, |
| 197 |
[ |
| 198 |
"-t", |
| 199 |
"test title", |
| 200 |
"-b", |
| 201 |
"test body", |
| 202 |
"good://localhost", |
| 203 |
"--disable-async", |
| 204 |
], |
| 205 |
) |
| 206 |
assert result.exit_code == 0 |
| 207 |
|
| 208 |
# Test Debug Mode (--debug) |
| 209 |
result = runner.invoke( |
| 210 |
cli.main, |
| 211 |
[ |
| 212 |
"-t", |
| 213 |
"test title", |
| 214 |
"-b", |
| 215 |
"test body", |
| 216 |
"good://localhost", |
| 217 |
"--debug", |
| 218 |
], |
| 219 |
) |
| 220 |
assert result.exit_code == 0 |
| 221 |
|
| 222 |
# Test Debug Mode (-D) |
| 223 |
result = runner.invoke( |
| 224 |
cli.main, |
| 225 |
[ |
| 226 |
"-t", |
| 227 |
"test title", |
| 228 |
"-b", |
| 229 |
"test body", |
| 230 |
"good://localhost", |
| 231 |
"-D", |
| 232 |
], |
| 233 |
) |
| 234 |
assert result.exit_code == 0 |
| 235 |
|
| 236 |
result = runner.invoke( |
| 237 |
cli.main, |
| 238 |
[ |
| 239 |
"-t", |
| 240 |
"test title", |
| 241 |
"good://localhost", |
| 242 |
], |
| 243 |
input="test stdin body\n", |
| 244 |
) |
| 245 |
assert result.exit_code == 0 |
| 246 |
|
| 247 |
# Run in synchronous mode |
| 248 |
result = runner.invoke( |
| 249 |
cli.main, |
| 250 |
[ |
| 251 |
"-t", |
| 252 |
"test title", |
| 253 |
"good://localhost", |
| 254 |
"--disable-async", |
| 255 |
], |
| 256 |
input="test stdin body\n", |
| 257 |
) |
| 258 |
assert result.exit_code == 0 |
| 259 |
|
| 260 |
result = runner.invoke( |
| 261 |
cli.main, |
| 262 |
[ |
| 263 |
"-t", |
| 264 |
"test title", |
| 265 |
"-b", |
| 266 |
"test body", |
| 267 |
"bad://localhost", |
| 268 |
], |
| 269 |
) |
| 270 |
assert result.exit_code == 1 |
| 271 |
|
| 272 |
# Run in synchronous mode |
| 273 |
result = runner.invoke( |
| 274 |
cli.main, |
| 275 |
[ |
| 276 |
"-t", |
| 277 |
"test title", |
| 278 |
"-b", |
| 279 |
"test body", |
| 280 |
"bad://localhost", |
| 281 |
"-Da", |
| 282 |
], |
| 283 |
) |
| 284 |
assert result.exit_code == 1 |
| 285 |
|
| 286 |
# Testing with the --dry-run flag reveals a successful response since we |
| 287 |
# don't actually execute the bad:// notification; we only display it |
| 288 |
result = runner.invoke( |
| 289 |
cli.main, |
| 290 |
[ |
| 291 |
"-t", |
| 292 |
"test title", |
| 293 |
"-b", |
| 294 |
"test body", |
| 295 |
"bad://localhost", |
| 296 |
"--dry-run", |
| 297 |
], |
| 298 |
) |
| 299 |
assert result.exit_code == 0 |
| 300 |
|
| 301 |
# Write a simple text based configuration file |
| 302 |
t = tmpdir.mkdir("apprise-obj").join("apprise") |
| 303 |
buf = f""" |
| 304 |
# Include ourselves |
| 305 |
include {t!s} |
| 306 |
|
| 307 |
taga,tagb=good://localhost |
| 308 |
tagc=good://nuxref.com |
| 309 |
""" |
| 310 |
t.write(buf) |
| 311 |
|
| 312 |
# This will read our configuration and not send any notices at all |
| 313 |
# because we assigned tags to all of our urls and didn't identify |
| 314 |
# a specific match below. |
| 315 |
|
| 316 |
# 'include' reference in configuration file would have included the file a |
| 317 |
# second time (since recursion default is 1). |
| 318 |
result = runner.invoke( |
| 319 |
cli.main, |
| 320 |
[ |
| 321 |
"-b", |
| 322 |
"test config", |
| 323 |
"--config", |
| 324 |
str(t), |
| 325 |
], |
| 326 |
) |
| 327 |
# Even when recursion take place, tags are all honored |
| 328 |
# so 2 is returned because nothing was notified |
| 329 |
assert result.exit_code == 3 |
| 330 |
|
| 331 |
# This will send out 1 notification because our tag matches |
| 332 |
# one of the entries above |
| 333 |
# translation: has taga |
| 334 |
result = runner.invoke( |
| 335 |
cli.main, |
| 336 |
[ |
| 337 |
"-b", |
| 338 |
"has taga", |
| 339 |
"--config", |
| 340 |
str(t), |
| 341 |
"--tag", |
| 342 |
"taga", |
| 343 |
], |
| 344 |
) |
| 345 |
assert result.exit_code == 0 |
| 346 |
|
| 347 |
# Test recursion |
| 348 |
result = runner.invoke( |
| 349 |
cli.main, |
| 350 |
[ |
| 351 |
"-t", |
| 352 |
"test title", |
| 353 |
"-b", |
| 354 |
"test body", |
| 355 |
"--config", |
| 356 |
str(t), |
| 357 |
"--tag", |
| 358 |
"tagc", |
| 359 |
# Invalid entry specified for recursion |
| 360 |
"-R", |
| 361 |
"invalid", |
| 362 |
], |
| 363 |
) |
| 364 |
assert result.exit_code == 2 |
| 365 |
|
| 366 |
result = runner.invoke( |
| 367 |
cli.main, |
| 368 |
[ |
| 369 |
"-t", |
| 370 |
"test title", |
| 371 |
"-b", |
| 372 |
"test body", |
| 373 |
"--config", |
| 374 |
str(t), |
| 375 |
"--tag", |
| 376 |
"tagc", |
| 377 |
# missing entry specified for recursion |
| 378 |
"--recursive-depth", |
| 379 |
], |
| 380 |
) |
| 381 |
assert result.exit_code == 2 |
| 382 |
|
| 383 |
result = runner.invoke( |
| 384 |
cli.main, |
| 385 |
[ |
| 386 |
"-t", |
| 387 |
"test title", |
| 388 |
"-b", |
| 389 |
"test body", |
| 390 |
"--config", |
| 391 |
str(t), |
| 392 |
"--tag", |
| 393 |
"tagc", |
| 394 |
# Disable recursion (thus inclusion will be ignored) |
| 395 |
"-R", |
| 396 |
"0", |
| 397 |
], |
| 398 |
) |
| 399 |
assert result.exit_code == 0 |
| 400 |
|
| 401 |
# Test recursion |
| 402 |
result = runner.invoke( |
| 403 |
cli.main, |
| 404 |
[ |
| 405 |
"-t", |
| 406 |
"test title", |
| 407 |
"-b", |
| 408 |
"test body", |
| 409 |
"--config", |
| 410 |
str(t), |
| 411 |
"--tag", |
| 412 |
"tagc", |
| 413 |
# Recurse up to 5 times |
| 414 |
"--recursion-depth", |
| 415 |
"5", |
| 416 |
], |
| 417 |
) |
| 418 |
assert result.exit_code == 0 |
| 419 |
|
| 420 |
# This will send out 2 notifications because by specifying 2 tag |
| 421 |
# entries, we 'or' them together: |
| 422 |
# translation: has taga or tagb or tagd |
| 423 |
result = runner.invoke( |
| 424 |
cli.main, |
| 425 |
[ |
| 426 |
"-b", |
| 427 |
"has taga OR tagc OR tagd", |
| 428 |
"--config", |
| 429 |
str(t), |
| 430 |
"--tag", |
| 431 |
"taga", |
| 432 |
"--tag", |
| 433 |
"tagc", |
| 434 |
"--tag", |
| 435 |
"tagd", |
| 436 |
], |
| 437 |
) |
| 438 |
assert result.exit_code == 0 |
| 439 |
|
| 440 |
# Write a simple text based configuration file |
| 441 |
t = tmpdir.mkdir("apprise-obj2").join("apprise-test2") |
| 442 |
buf = """ |
| 443 |
good://localhost/1 |
| 444 |
good://localhost/2 |
| 445 |
good://localhost/3 |
| 446 |
good://localhost/4 |
| 447 |
good://localhost/5 |
| 448 |
myTag=good://localhost/6 |
| 449 |
""" |
| 450 |
t.write(buf) |
| 451 |
|
| 452 |
# This will read our configuration and send a notification to |
| 453 |
# the first 5 entries in the list, but not the one that has |
| 454 |
# the tag associated with it |
| 455 |
result = runner.invoke( |
| 456 |
cli.main, |
| 457 |
[ |
| 458 |
"-b", |
| 459 |
"test config", |
| 460 |
"--config", |
| 461 |
str(t), |
| 462 |
], |
| 463 |
) |
| 464 |
assert result.exit_code == 0 |
| 465 |
|
| 466 |
# Test our notification type switch (it defaults to info) so we want to |
| 467 |
# try it as a different value. Should return without a problem |
| 468 |
result = runner.invoke( |
| 469 |
cli.main, |
| 470 |
[ |
| 471 |
"-b", |
| 472 |
"# test config", |
| 473 |
"--config", |
| 474 |
str(t), |
| 475 |
"-n", |
| 476 |
"success", |
| 477 |
], |
| 478 |
) |
| 479 |
assert result.exit_code == 0 |
| 480 |
|
| 481 |
# Test our notification type switch when set to something unsupported |
| 482 |
result = runner.invoke( |
| 483 |
cli.main, |
| 484 |
[ |
| 485 |
"-b", |
| 486 |
"test config", |
| 487 |
"--config", |
| 488 |
str(t), |
| 489 |
"--notification-type", |
| 490 |
"invalid", |
| 491 |
], |
| 492 |
) |
| 493 |
# An error code of 2 is returned if invalid input is specified on the |
| 494 |
# command line |
| 495 |
assert result.exit_code == 2 |
| 496 |
|
| 497 |
# The notification type switch is case-insensitive |
| 498 |
result = runner.invoke( |
| 499 |
cli.main, |
| 500 |
[ |
| 501 |
"-b", |
| 502 |
"test config", |
| 503 |
"--config", |
| 504 |
str(t), |
| 505 |
"--notification-type", |
| 506 |
"WARNING", |
| 507 |
], |
| 508 |
) |
| 509 |
assert result.exit_code == 0 |
| 510 |
|
| 511 |
# Test our formatting switch (it defaults to text) so we want to try it as |
| 512 |
# a different value. Should return without a problem |
| 513 |
result = runner.invoke( |
| 514 |
cli.main, |
| 515 |
[ |
| 516 |
"-b", |
| 517 |
"# test config", |
| 518 |
"--config", |
| 519 |
str(t), |
| 520 |
"-i", |
| 521 |
"markdown", |
| 522 |
], |
| 523 |
) |
| 524 |
assert result.exit_code == 0 |
| 525 |
|
| 526 |
# Test our formatting switch when set to something unsupported |
| 527 |
result = runner.invoke( |
| 528 |
cli.main, |
| 529 |
[ |
| 530 |
"-b", |
| 531 |
"test config", |
| 532 |
"--config", |
| 533 |
str(t), |
| 534 |
"--input-format", |
| 535 |
"invalid", |
| 536 |
], |
| 537 |
) |
| 538 |
# An error code of 2 is returned if invalid input is specified on the |
| 539 |
# command line |
| 540 |
assert result.exit_code == 2 |
| 541 |
|
| 542 |
# The formatting switch is not case sensitive |
| 543 |
result = runner.invoke( |
| 544 |
cli.main, |
| 545 |
[ |
| 546 |
"-b", |
| 547 |
"# test config", |
| 548 |
"--config", |
| 549 |
str(t), |
| 550 |
"--input-format", |
| 551 |
"HTML", |
| 552 |
], |
| 553 |
) |
| 554 |
assert result.exit_code == 0 |
| 555 |
|
| 556 |
# As a way of ensuring we match the first 5 entries, we can run a |
| 557 |
# --dry-run against the same result set above and verify the output |
| 558 |
result = runner.invoke( |
| 559 |
cli.main, |
| 560 |
[ |
| 561 |
"-b", |
| 562 |
"test config", |
| 563 |
"--config", |
| 564 |
str(t), |
| 565 |
"--dry-run", |
| 566 |
], |
| 567 |
) |
| 568 |
assert result.exit_code == 0 |
| 569 |
lines = re.split(r"[\r\n]", result.output.strip()) |
| 570 |
# 5 lines of all good:// entries matched + url id underneath |
| 571 |
assert len(lines) == 10 |
| 572 |
# Verify we match against the remaining good:// entries |
| 573 |
for i in range(0, 10, 2): |
| 574 |
assert lines[i].endswith("good://") |
| 575 |
|
| 576 |
# Tags are case-insensitive; mytag matches myTag |
| 577 |
result = runner.invoke( |
| 578 |
cli.main, |
| 579 |
[ |
| 580 |
"-b", |
| 581 |
"has mytag", |
| 582 |
"--config", |
| 583 |
str(t), |
| 584 |
"--tag", |
| 585 |
"mytag", |
| 586 |
], |
| 587 |
) |
| 588 |
assert result.exit_code == 0 |
| 589 |
|
| 590 |
# Same command with --dry-run; one entry matches so exit code is 0 |
| 591 |
result = runner.invoke( |
| 592 |
cli.main, |
| 593 |
["-b", "has mytag", "--config", str(t), "--tag", "mytag", "--dry-run"], |
| 594 |
) |
| 595 |
assert result.exit_code == 0 |
| 596 |
|
| 597 |
# A tag that does not exist in the config produces no matches; the live |
| 598 |
# run returns exit code 3 to signal "no services found". |
| 599 |
result = runner.invoke( |
| 600 |
cli.main, |
| 601 |
["-b", "no match", "--config", str(t), "--tag", "nonexistent"], |
| 602 |
) |
| 603 |
assert result.exit_code == 3 |
| 604 |
|
| 605 |
# --dry-run must mirror the live result: a non-matching tag also returns |
| 606 |
# exit code 3, not 0. This guards against dry-run masking "no services |
| 607 |
# found" by always reporting success. |
| 608 |
result = runner.invoke( |
| 609 |
cli.main, |
| 610 |
[ |
| 611 |
"-b", |
| 612 |
"no match", |
| 613 |
"--config", |
| 614 |
str(t), |
| 615 |
"--tag", |
| 616 |
"nonexistent", |
| 617 |
"--dry-run", |
| 618 |
], |
| 619 |
) |
| 620 |
assert result.exit_code == 3 |
| 621 |
|
| 622 |
# Here is a case where we get what was expected; we also attach a file |
| 623 |
result = runner.invoke( |
| 624 |
cli.main, |
| 625 |
[ |
| 626 |
"-b", |
| 627 |
"has myTag", |
| 628 |
"--config", |
| 629 |
str(t), |
| 630 |
"--attach", |
| 631 |
join(dirname(__file__), "var", "apprise-test.gif"), |
| 632 |
"--tag", |
| 633 |
"myTag", |
| 634 |
], |
| 635 |
) |
| 636 |
assert result.exit_code == 0 |
| 637 |
|
| 638 |
# Testing with the --dry-run flag reveals the same positive results |
| 639 |
# because there was at least one match |
| 640 |
result = runner.invoke( |
| 641 |
cli.main, |
| 642 |
[ |
| 643 |
"-b", |
| 644 |
"has myTag", |
| 645 |
"--config", |
| 646 |
str(t), |
| 647 |
"--tag", |
| 648 |
"myTag", |
| 649 |
"--dry-run", |
| 650 |
], |
| 651 |
) |
| 652 |
assert result.exit_code == 0 |
| 653 |
|
| 654 |
# |
| 655 |
# Test environment variables |
| 656 |
# |
| 657 |
# Write a simple text based configuration file |
| 658 |
t2 = tmpdir.mkdir("apprise-obj-env").join("apprise") |
| 659 |
buf = """ |
| 660 |
# A general one |
| 661 |
good://localhost |
| 662 |
|
| 663 |
# A failure (if we use the fail tag) |
| 664 |
fail=bad://localhost |
| 665 |
|
| 666 |
# A normal one tied to myTag |
| 667 |
myTag=good://nuxref.com |
| 668 |
""" |
| 669 |
t2.write(buf) |
| 670 |
|
| 671 |
with environ(APPRISE_URLS="good://localhost"): |
| 672 |
# This will load okay because we defined the environment |
| 673 |
# variable with a valid URL |
| 674 |
result = runner.invoke( |
| 675 |
cli.main, |
| 676 |
[ |
| 677 |
"-b", |
| 678 |
"test environment", |
| 679 |
# Test that we ignore our tag |
| 680 |
"--tag", |
| 681 |
"mytag", |
| 682 |
], |
| 683 |
) |
| 684 |
assert result.exit_code == 0 |
| 685 |
|
| 686 |
# Same action but without --tag |
| 687 |
result = runner.invoke( |
| 688 |
cli.main, |
| 689 |
[ |
| 690 |
"-b", |
| 691 |
"test environment", |
| 692 |
], |
| 693 |
) |
| 694 |
assert result.exit_code == 0 |
| 695 |
|
| 696 |
with ( |
| 697 |
mock.patch("apprise.cli.DEFAULT_CONFIG_PATHS", []), |
| 698 |
environ(APPRISE_URLS=" "), |
| 699 |
): |
| 700 |
# An empty string is not valid and therefore not loaded so the below |
| 701 |
# fails. We override the DEFAULT_CONFIG_PATHS because we don't want to |
| 702 |
# detect ones loaded on the machine running the unit tests |
| 703 |
result = runner.invoke( |
| 704 |
cli.main, |
| 705 |
[ |
| 706 |
"-b", |
| 707 |
"test environment", |
| 708 |
], |
| 709 |
) |
| 710 |
assert result.exit_code == 1 |
| 711 |
|
| 712 |
with environ(APPRISE_URLS="bad://localhost"): |
| 713 |
result = runner.invoke( |
| 714 |
cli.main, |
| 715 |
[ |
| 716 |
"-b", |
| 717 |
"test environment", |
| 718 |
], |
| 719 |
) |
| 720 |
assert result.exit_code == 1 |
| 721 |
|
| 722 |
# An inline URL overrides the environment. |
| 723 |
# variable |
| 724 |
result = runner.invoke( |
| 725 |
cli.main, |
| 726 |
[ |
| 727 |
"-t", |
| 728 |
"test title", |
| 729 |
"-b", |
| 730 |
"test body", |
| 731 |
"good://localhost", |
| 732 |
], |
| 733 |
) |
| 734 |
assert result.exit_code == 0 |
| 735 |
|
| 736 |
# A Config file also over-rides the environment variable if |
| 737 |
# specified on the command line: |
| 738 |
result = runner.invoke( |
| 739 |
cli.main, |
| 740 |
[ |
| 741 |
"-b", |
| 742 |
"has myTag", |
| 743 |
"--config", |
| 744 |
str(t2), |
| 745 |
"--tag", |
| 746 |
"myTag", |
| 747 |
], |
| 748 |
) |
| 749 |
assert result.exit_code == 0 |
| 750 |
|
| 751 |
with environ(APPRISE_CONFIG_PATH=str(t2)): |
| 752 |
# Load the configuration from the current environment variable. |
| 753 |
result = runner.invoke( |
| 754 |
cli.main, |
| 755 |
[ |
| 756 |
"-b", |
| 757 |
"has myTag", |
| 758 |
"--tag", |
| 759 |
"myTag", |
| 760 |
], |
| 761 |
) |
| 762 |
assert result.exit_code == 0 |
| 763 |
|
| 764 |
with environ(APPRISE_CONFIG=str(t2)): |
| 765 |
# The deprecated name still works and emits a notice. |
| 766 |
result = runner.invoke( |
| 767 |
cli.main, |
| 768 |
[ |
| 769 |
"-b", |
| 770 |
"has myTag", |
| 771 |
"--tag", |
| 772 |
"myTag", |
| 773 |
], |
| 774 |
) |
| 775 |
assert result.exit_code == 0 |
| 776 |
|
| 777 |
with environ( |
| 778 |
APPRISE_CONFIG_PATH=str(t2), |
| 779 |
APPRISE_CONFIG="garbage/file/path.yaml", |
| 780 |
): |
| 781 |
# Prefer APPRISE_CONFIG_PATH when both names are defined. |
| 782 |
result = runner.invoke( |
| 783 |
cli.main, |
| 784 |
[ |
| 785 |
"-b", |
| 786 |
"has myTag", |
| 787 |
"--tag", |
| 788 |
"myTag", |
| 789 |
], |
| 790 |
) |
| 791 |
assert result.exit_code == 0 |
| 792 |
|
| 793 |
with environ(APPRISE_CONFIG_PATH=str(t2) + ";/another/path"): |
| 794 |
# The current variable accepts multiple configuration paths. |
| 795 |
result = runner.invoke( |
| 796 |
cli.main, |
| 797 |
[ |
| 798 |
"-b", |
| 799 |
"has myTag", |
| 800 |
"--tag", |
| 801 |
"myTag", |
| 802 |
], |
| 803 |
) |
| 804 |
assert result.exit_code == 0 |
| 805 |
|
| 806 |
with ( |
| 807 |
mock.patch("apprise.cli.DEFAULT_CONFIG_PATHS", []), |
| 808 |
environ(APPRISE_CONFIG_PATH=" "), |
| 809 |
): |
| 810 |
# We will fail to send the notification as no path was specified. |
| 811 |
# We override the DEFAULT_CONFIG_PATHS because we don't want to detect |
| 812 |
# ones loaded on the machine running the unit tests |
| 813 |
result = runner.invoke( |
| 814 |
cli.main, |
| 815 |
[ |
| 816 |
"-b", |
| 817 |
"my message", |
| 818 |
], |
| 819 |
) |
| 820 |
assert result.exit_code == 1 |
| 821 |
|
| 822 |
with environ(APPRISE_CONFIG_PATH="garbage/file/path.yaml"): |
| 823 |
# We will fail to send the notification as the path |
| 824 |
# specified is not loadable |
| 825 |
result = runner.invoke( |
| 826 |
cli.main, |
| 827 |
[ |
| 828 |
"-b", |
| 829 |
"my message", |
| 830 |
], |
| 831 |
) |
| 832 |
assert result.exit_code == 1 |
| 833 |
|
| 834 |
# A command-line configuration overrides the invalid environment path. |
| 835 |
result = runner.invoke( |
| 836 |
cli.main, |
| 837 |
[ |
| 838 |
"-b", |
| 839 |
"has myTag", |
| 840 |
"--config", |
| 841 |
str(t2), |
| 842 |
"--tag", |
| 843 |
"myTag", |
| 844 |
], |
| 845 |
) |
| 846 |
assert result.exit_code == 0 |
| 847 |
|
| 848 |
# Explicit URLs take priority over --config. |
| 849 |
result = runner.invoke( |
| 850 |
cli.main, |
| 851 |
[ |
| 852 |
"-b", |
| 853 |
"has myTag", |
| 854 |
"--config", |
| 855 |
str(t2), |
| 856 |
"good://localhost", |
| 857 |
"--tag", |
| 858 |
"fail", |
| 859 |
], |
| 860 |
) |
| 861 |
# Tags are ignored, URL specified, so it trump config |
| 862 |
assert result.exit_code == 0 |
| 863 |
|
| 864 |
# we just repeat the test as a proof that it only executes |
| 865 |
# the urls despite the fact the --config was specified |
| 866 |
result = runner.invoke( |
| 867 |
cli.main, |
| 868 |
[ |
| 869 |
"-b", |
| 870 |
"reads the url entry only", |
| 871 |
"--config", |
| 872 |
str(t2), |
| 873 |
"good://localhost", |
| 874 |
"--tag", |
| 875 |
"fail", |
| 876 |
], |
| 877 |
) |
| 878 |
# Tags are ignored, URL specified, so it trump config |
| 879 |
assert result.exit_code == 0 |
| 880 |
|
| 881 |
# once agian, but we call bad:// |
| 882 |
result = runner.invoke( |
| 883 |
cli.main, |
| 884 |
[ |
| 885 |
"-b", |
| 886 |
"reads the url entry only", |
| 887 |
"--config", |
| 888 |
str(t2), |
| 889 |
"bad://localhost", |
| 890 |
"--tag", |
| 891 |
"myTag", |
| 892 |
], |
| 893 |
) |
| 894 |
assert result.exit_code == 1 |
| 895 |
|
| 896 |
# Test Escaping: |
| 897 |
result = runner.invoke( |
| 898 |
cli.main, |
| 899 |
[ |
| 900 |
"-e", |
| 901 |
"-t", |
| 902 |
"test\ntitle", |
| 903 |
"-b", |
| 904 |
"test\nbody", |
| 905 |
"good://localhost", |
| 906 |
], |
| 907 |
) |
| 908 |
assert result.exit_code == 0 |
| 909 |
|
| 910 |
# Test Escaping (without title) |
| 911 |
result = runner.invoke( |
| 912 |
cli.main, |
| 913 |
[ |
| 914 |
"--interpret-escapes", |
| 915 |
"-b", |
| 916 |
"test\nbody", |
| 917 |
"good://localhost", |
| 918 |
], |
| 919 |
) |
| 920 |
assert result.exit_code == 0 |
| 921 |
|
| 922 |
# Test Emojis: |
| 923 |
result = runner.invoke( |
| 924 |
cli.main, |
| 925 |
[ |
| 926 |
"-j", |
| 927 |
"-t", |
| 928 |
":smile:", |
| 929 |
"-b", |
| 930 |
":grin:", |
| 931 |
"good://localhost", |
| 932 |
], |
| 933 |
) |
| 934 |
assert result.exit_code == 0 |
| 935 |
|
| 936 |
result = runner.invoke( |
| 937 |
cli.main, |
| 938 |
[ |
| 939 |
"--interpret-emojis", |
| 940 |
"-t", |
| 941 |
":smile:", |
| 942 |
"-b", |
| 943 |
":grin:", |
| 944 |
"good://localhost", |
| 945 |
], |
| 946 |
) |
| 947 |
assert result.exit_code == 0 |
| 948 |
|
| 949 |
|
| 950 |
def test_apprise_cli_modules(tmpdir): |
| 951 |
""" |
| 952 |
CLI: --plugin (-P) |
| 953 |
|
| 954 |
""" |
| 955 |
|
| 956 |
runner = CliRunner() |
| 957 |
|
| 958 |
# |
| 959 |
# Loading of modules works correctly |
| 960 |
# |
| 961 |
notify_cmod_base = tmpdir.mkdir("cli_modules") |
| 962 |
notify_cmod = notify_cmod_base.join("hook.py") |
| 963 |
notify_cmod.write( |
| 964 |
cleandoc(""" |
| 965 |
from apprise.decorators import notify |
| 966 |
|
| 967 |
@notify(on="climod") |
| 968 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 969 |
pass |
| 970 |
""") |
| 971 |
) |
| 972 |
|
| 973 |
result = runner.invoke( |
| 974 |
cli.main, |
| 975 |
[ |
| 976 |
"--plugin-path", |
| 977 |
str(notify_cmod), |
| 978 |
"-t", |
| 979 |
"title", |
| 980 |
"-b", |
| 981 |
"body", |
| 982 |
"climod://", |
| 983 |
], |
| 984 |
) |
| 985 |
|
| 986 |
assert result.exit_code == 0 |
| 987 |
|
| 988 |
# Test -P |
| 989 |
result = runner.invoke( |
| 990 |
cli.main, |
| 991 |
[ |
| 992 |
"-P", |
| 993 |
str(notify_cmod), |
| 994 |
"-t", |
| 995 |
"title", |
| 996 |
"-b", |
| 997 |
"body", |
| 998 |
"climod://", |
| 999 |
], |
| 1000 |
) |
| 1001 |
|
| 1002 |
assert result.exit_code == 0 |
| 1003 |
|
| 1004 |
# Test double hooks |
| 1005 |
notify_cmod2 = notify_cmod_base.join("hook2.py") |
| 1006 |
notify_cmod2.write( |
| 1007 |
cleandoc(""" |
| 1008 |
from apprise.decorators import notify |
| 1009 |
|
| 1010 |
@notify(on="climod2") |
| 1011 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 1012 |
pass |
| 1013 |
""") |
| 1014 |
) |
| 1015 |
|
| 1016 |
result = runner.invoke( |
| 1017 |
cli.main, |
| 1018 |
[ |
| 1019 |
"--plugin-path", |
| 1020 |
str(notify_cmod), |
| 1021 |
"--plugin-path", |
| 1022 |
str(notify_cmod2), |
| 1023 |
"-t", |
| 1024 |
"title", |
| 1025 |
"-b", |
| 1026 |
"body", |
| 1027 |
"climod://", |
| 1028 |
"climod2://", |
| 1029 |
], |
| 1030 |
) |
| 1031 |
|
| 1032 |
assert result.exit_code == 0 |
| 1033 |
|
| 1034 |
with environ( |
| 1035 |
APPRISE_PLUGIN_PATH=str(notify_cmod) + ";" + str(notify_cmod2) |
| 1036 |
): |
| 1037 |
# Leverage our environment variables to specify the plugin path |
| 1038 |
result = runner.invoke( |
| 1039 |
cli.main, |
| 1040 |
[ |
| 1041 |
"-b", |
| 1042 |
"body", |
| 1043 |
"climod://", |
| 1044 |
"climod2://", |
| 1045 |
], |
| 1046 |
) |
| 1047 |
|
| 1048 |
assert result.exit_code == 0 |
| 1049 |
|
| 1050 |
|
| 1051 |
@pytest.mark.skipif( |
| 1052 |
sys.platform == "win32", reason="Unreliable results to be determined" |
| 1053 |
) |
| 1054 |
def test_apprise_cli_persistent_storage(tmpdir): |
| 1055 |
""" |
| 1056 |
CLI: test persistent storage |
| 1057 |
|
| 1058 |
""" |
| 1059 |
|
| 1060 |
# This is a made up class that is just used to verify |
| 1061 |
class NoURLIDNotification(NotifyBase): |
| 1062 |
"""A no URL ID.""" |
| 1063 |
|
| 1064 |
# Update URL identifier |
| 1065 |
url_identifier = False |
| 1066 |
|
| 1067 |
def __init__(self, *args, **kwargs): |
| 1068 |
super().__init__(*args, **kwargs) |
| 1069 |
|
| 1070 |
def send(self, **kwargs): |
| 1071 |
|
| 1072 |
# Pretend everything is okay |
| 1073 |
return True |
| 1074 |
|
| 1075 |
def url(self, *args, **kwargs): |
| 1076 |
# Support URL |
| 1077 |
return "noper://" |
| 1078 |
|
| 1079 |
def parse_url(self, *args, **kwargs): |
| 1080 |
# parse our url |
| 1081 |
return {"schema": "noper"} |
| 1082 |
|
| 1083 |
# This is a made up class that is just used to verify |
| 1084 |
class TestNotification(NotifyBase): |
| 1085 |
"""A Testing Script.""" |
| 1086 |
|
| 1087 |
def __init__(self, *args, **kwargs): |
| 1088 |
super().__init__(*args, **kwargs) |
| 1089 |
|
| 1090 |
def send(self, **kwargs): |
| 1091 |
|
| 1092 |
# Test our persistent settings |
| 1093 |
self.store.set("key", "value") |
| 1094 |
assert self.store.get("key") == "value" |
| 1095 |
|
| 1096 |
# Pretend everything is okay |
| 1097 |
return True |
| 1098 |
|
| 1099 |
def url(self, *args, **kwargs): |
| 1100 |
# Support URL |
| 1101 |
return "test://" |
| 1102 |
|
| 1103 |
def parse_url(self, *args, **kwargs): |
| 1104 |
# parse our url |
| 1105 |
return {"schema": "test"} |
| 1106 |
|
| 1107 |
# assign test:// to our notification defined above |
| 1108 |
N_MGR["test"] = TestNotification |
| 1109 |
N_MGR["noper"] = NoURLIDNotification |
| 1110 |
|
| 1111 |
# Write a simple text based configuration file |
| 1112 |
config = tmpdir.join("apprise.cfg") |
| 1113 |
buf = cleandoc(""" |
| 1114 |
# Create a config file we can source easily |
| 1115 |
test=test:// |
| 1116 |
noper=noper:// |
| 1117 |
|
| 1118 |
# Define a second test URL that will |
| 1119 |
two-urls=test:// |
| 1120 |
|
| 1121 |
# Create another entry that has no tag associatd with it |
| 1122 |
test://?entry=2 |
| 1123 |
""") |
| 1124 |
config.write(buf) |
| 1125 |
|
| 1126 |
runner = CliRunner() |
| 1127 |
|
| 1128 |
# Generate notification that creates persistent data |
| 1129 |
result = runner.invoke( |
| 1130 |
cli.main, |
| 1131 |
[ |
| 1132 |
"--storage-path", |
| 1133 |
str(tmpdir), |
| 1134 |
"--config", |
| 1135 |
str(config), |
| 1136 |
"storage", |
| 1137 |
"list", |
| 1138 |
], |
| 1139 |
) |
| 1140 |
# list our entries |
| 1141 |
assert result.exit_code == 0 |
| 1142 |
|
| 1143 |
# our persist storage has not been created yet |
| 1144 |
stdout = result.stdout.strip() |
| 1145 |
|
| 1146 |
# Click output can wrap based on terminal width, and storage backend |
| 1147 |
# sizes are not stable across Python/OS variations. Validate semantics. |
| 1148 |
assert re.search( |
| 1149 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+unused\b", |
| 1150 |
stdout, |
| 1151 |
re.MULTILINE, |
| 1152 |
) |
| 1153 |
assert re.search(r"(?m)^\s*-\s+test://\s*$", stdout) |
| 1154 |
|
| 1155 |
# An invalid mode specified |
| 1156 |
result = runner.invoke( |
| 1157 |
cli.main, |
| 1158 |
[ |
| 1159 |
"--storage-path", |
| 1160 |
str(tmpdir), |
| 1161 |
"--storage-mode", |
| 1162 |
"invalid", |
| 1163 |
"--config", |
| 1164 |
str(config), |
| 1165 |
"-g", |
| 1166 |
"test", |
| 1167 |
"-t", |
| 1168 |
"title", |
| 1169 |
"-b", |
| 1170 |
"body", |
| 1171 |
], |
| 1172 |
) |
| 1173 |
# Bad mode specified |
| 1174 |
assert result.exit_code == 2 |
| 1175 |
|
| 1176 |
# Invalid uid lenth specified |
| 1177 |
result = runner.invoke( |
| 1178 |
cli.main, |
| 1179 |
[ |
| 1180 |
"--storage-path", |
| 1181 |
str(tmpdir), |
| 1182 |
"--storage-mode", |
| 1183 |
"flush", |
| 1184 |
"--storage-uid-length", |
| 1185 |
1, |
| 1186 |
"--config", |
| 1187 |
str(config), |
| 1188 |
"-g", |
| 1189 |
"test", |
| 1190 |
"-t", |
| 1191 |
"title", |
| 1192 |
"-b", |
| 1193 |
"body", |
| 1194 |
], |
| 1195 |
) |
| 1196 |
# storage uid length to small |
| 1197 |
assert result.exit_code == 2 |
| 1198 |
|
| 1199 |
# No files written yet; just config file exists |
| 1200 |
dir_content = os.listdir(str(tmpdir)) |
| 1201 |
assert len(dir_content) == 1 |
| 1202 |
assert "apprise.cfg" in dir_content |
| 1203 |
|
| 1204 |
# Generate notification that creates persistent data |
| 1205 |
result = runner.invoke( |
| 1206 |
cli.main, |
| 1207 |
[ |
| 1208 |
"--storage-path", |
| 1209 |
str(tmpdir), |
| 1210 |
"--storage-mode", |
| 1211 |
"flush", |
| 1212 |
"--config", |
| 1213 |
str(config), |
| 1214 |
"-t", |
| 1215 |
"title", |
| 1216 |
"-b", |
| 1217 |
"body", |
| 1218 |
"-g", |
| 1219 |
"test", |
| 1220 |
], |
| 1221 |
) |
| 1222 |
# We parsed our data accordingly |
| 1223 |
assert result.exit_code == 0 |
| 1224 |
|
| 1225 |
dir_content = os.listdir(str(tmpdir)) |
| 1226 |
assert len(dir_content) == 2 |
| 1227 |
assert "apprise.cfg" in dir_content |
| 1228 |
assert "ea482db7" in dir_content |
| 1229 |
|
| 1230 |
# Have a look at our storage listings |
| 1231 |
result = runner.invoke( |
| 1232 |
cli.main, |
| 1233 |
[ |
| 1234 |
"--storage-path", |
| 1235 |
str(tmpdir), |
| 1236 |
"--config", |
| 1237 |
str(config), |
| 1238 |
"storage", |
| 1239 |
"list", |
| 1240 |
], |
| 1241 |
) |
| 1242 |
# list our entries |
| 1243 |
assert result.exit_code == 0 |
| 1244 |
|
| 1245 |
stdout = result.stdout.strip() |
| 1246 |
assert re.search( |
| 1247 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1248 |
stdout, |
| 1249 |
re.MULTILINE, |
| 1250 |
) |
| 1251 |
|
| 1252 |
# keyword list is not required |
| 1253 |
result = runner.invoke( |
| 1254 |
cli.main, |
| 1255 |
[ |
| 1256 |
"--storage-path", |
| 1257 |
str(tmpdir), |
| 1258 |
"--config", |
| 1259 |
str(config), |
| 1260 |
"storage", |
| 1261 |
], |
| 1262 |
) |
| 1263 |
# list our entries |
| 1264 |
assert result.exit_code == 0 |
| 1265 |
|
| 1266 |
stdout = result.stdout.strip() |
| 1267 |
assert re.search( |
| 1268 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1269 |
stdout, |
| 1270 |
re.MULTILINE, |
| 1271 |
) |
| 1272 |
|
| 1273 |
# search on something that won't match |
| 1274 |
result = runner.invoke( |
| 1275 |
cli.main, |
| 1276 |
[ |
| 1277 |
"--storage-path", |
| 1278 |
str(tmpdir), |
| 1279 |
"--config", |
| 1280 |
str(config), |
| 1281 |
"storage", |
| 1282 |
"list", |
| 1283 |
"nomatch", |
| 1284 |
], |
| 1285 |
) |
| 1286 |
# list our entries |
| 1287 |
assert result.exit_code == 0 |
| 1288 |
|
| 1289 |
assert not result.stdout.strip() |
| 1290 |
|
| 1291 |
# closest match search |
| 1292 |
result = runner.invoke( |
| 1293 |
cli.main, |
| 1294 |
[ |
| 1295 |
"--storage-path", |
| 1296 |
str(tmpdir), |
| 1297 |
"--config", |
| 1298 |
str(config), |
| 1299 |
"storage", |
| 1300 |
"list", |
| 1301 |
# Closest match will hit a result |
| 1302 |
"ea", |
| 1303 |
], |
| 1304 |
) |
| 1305 |
# list our entries |
| 1306 |
assert result.exit_code == 0 |
| 1307 |
|
| 1308 |
stdout = result.stdout.strip() |
| 1309 |
assert re.search( |
| 1310 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1311 |
stdout, |
| 1312 |
re.MULTILINE, |
| 1313 |
) |
| 1314 |
|
| 1315 |
# list is the presumed option if no match |
| 1316 |
result = runner.invoke( |
| 1317 |
cli.main, |
| 1318 |
[ |
| 1319 |
"--storage-path", |
| 1320 |
str(tmpdir), |
| 1321 |
"--config", |
| 1322 |
str(config), |
| 1323 |
"storage", |
| 1324 |
# Closest match will hit a result |
| 1325 |
"ea", |
| 1326 |
], |
| 1327 |
) |
| 1328 |
# list our entries successfully again.. |
| 1329 |
assert result.exit_code == 0 |
| 1330 |
|
| 1331 |
stdout = result.stdout.strip() |
| 1332 |
assert re.search( |
| 1333 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1334 |
stdout, |
| 1335 |
re.MULTILINE, |
| 1336 |
) |
| 1337 |
|
| 1338 |
# Search based on tag |
| 1339 |
result = runner.invoke( |
| 1340 |
cli.main, |
| 1341 |
[ |
| 1342 |
"--storage-path", |
| 1343 |
str(tmpdir), |
| 1344 |
"--config", |
| 1345 |
str(config), |
| 1346 |
"storage", |
| 1347 |
"list", |
| 1348 |
# We can match by tags too |
| 1349 |
"-g", |
| 1350 |
"test", |
| 1351 |
], |
| 1352 |
) |
| 1353 |
# list our entries |
| 1354 |
assert result.exit_code == 0 |
| 1355 |
|
| 1356 |
stdout = result.stdout.strip() |
| 1357 |
assert re.search( |
| 1358 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1359 |
stdout, |
| 1360 |
re.MULTILINE, |
| 1361 |
) |
| 1362 |
|
| 1363 |
# Create a stale 8-char namespace on disk that belongs to a different |
| 1364 |
# URL — it must NOT appear in the output when filtering by URL. |
| 1365 |
tmpdir.mkdir("aaaaaaaa") |
| 1366 |
|
| 1367 |
# Filter by URL directly — no config file required; the URL is loaded |
| 1368 |
# inline and its uid is used to match storage entries. |
| 1369 |
result = runner.invoke( |
| 1370 |
cli.main, |
| 1371 |
[ |
| 1372 |
"--storage-path", |
| 1373 |
str(tmpdir), |
| 1374 |
"storage", |
| 1375 |
"list", |
| 1376 |
# Pass the raw URL instead of an 8-char UID prefix |
| 1377 |
"test://", |
| 1378 |
], |
| 1379 |
) |
| 1380 |
# Should produce exactly one entry (the url's namespace); the |
| 1381 |
# unrelated stale directory 'aaaaaaaa' must not bleed into output. |
| 1382 |
assert result.exit_code == 0 |
| 1383 |
|
| 1384 |
stdout = result.stdout.strip() |
| 1385 |
assert re.search( |
| 1386 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1387 |
stdout, |
| 1388 |
re.MULTILINE, |
| 1389 |
) |
| 1390 |
# Confirm only one entry — stale 'aaaaaaaa' must be absent |
| 1391 |
assert not re.search(r"aaaaaaaa", stdout) |
| 1392 |
|
| 1393 |
# URL filter alongside --config: URL filters trump all other sources, |
| 1394 |
# including --config. 'a' is reset and only the URL-filter plugin |
| 1395 |
# is loaded. Its storage was already created, so exactly one active |
| 1396 |
# entry should appear. |
| 1397 |
result = runner.invoke( |
| 1398 |
cli.main, |
| 1399 |
[ |
| 1400 |
"--storage-path", |
| 1401 |
str(tmpdir), |
| 1402 |
"--config", |
| 1403 |
str(config), |
| 1404 |
"storage", |
| 1405 |
"list", |
| 1406 |
"test://", |
| 1407 |
], |
| 1408 |
) |
| 1409 |
assert result.exit_code == 0 |
| 1410 |
stdout = result.stdout.strip() |
| 1411 |
assert re.search( |
| 1412 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1413 |
stdout, |
| 1414 |
re.MULTILINE, |
| 1415 |
) |
| 1416 |
|
| 1417 |
# Duplicate URL filter: the same URL specified twice. Both entries |
| 1418 |
# are added to the fresh Apprise instance; the uids dict groups them |
| 1419 |
# under one uid key, so only a single storage entry should appear. |
| 1420 |
result = runner.invoke( |
| 1421 |
cli.main, |
| 1422 |
[ |
| 1423 |
"--storage-path", |
| 1424 |
str(tmpdir), |
| 1425 |
"storage", |
| 1426 |
"list", |
| 1427 |
"test://", |
| 1428 |
"test://", |
| 1429 |
], |
| 1430 |
) |
| 1431 |
assert result.exit_code == 0 |
| 1432 |
stdout = result.stdout.strip() |
| 1433 |
assert re.search( |
| 1434 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1435 |
stdout, |
| 1436 |
re.MULTILINE, |
| 1437 |
) |
| 1438 |
# Only one numbered entry should appear despite two URL arguments |
| 1439 |
assert len(re.findall(r"^\d+\.", stdout, re.MULTILINE)) == 1 |
| 1440 |
|
| 1441 |
# URL filter with an unrecognised schema: a.add() silently rejects the |
| 1442 |
# URL, leaving the Apprise instance empty. With no plugin uids resolved, |
| 1443 |
# disk_scan is skipped entirely and the output is empty. |
| 1444 |
result = runner.invoke( |
| 1445 |
cli.main, |
| 1446 |
[ |
| 1447 |
"--storage-path", |
| 1448 |
str(tmpdir), |
| 1449 |
"storage", |
| 1450 |
"list", |
| 1451 |
"nosuchschema://filter", |
| 1452 |
], |
| 1453 |
) |
| 1454 |
assert result.exit_code == 0 |
| 1455 |
assert not result.stdout.strip() |
| 1456 |
|
| 1457 |
# URL filter with --tag: URL filter fires rule #1 which clears the tag |
| 1458 |
# (same as sending a notification — URLs trump all, including tags). |
| 1459 |
# The test:// plugin's storage is shown; the stale dirs are absent |
| 1460 |
# because disk_scan is scoped to the URL-filter plugin's uid. |
| 1461 |
result = runner.invoke( |
| 1462 |
cli.main, |
| 1463 |
[ |
| 1464 |
"--storage-path", |
| 1465 |
str(tmpdir), |
| 1466 |
"--tag", |
| 1467 |
"test", |
| 1468 |
"storage", |
| 1469 |
"list", |
| 1470 |
"test://", |
| 1471 |
], |
| 1472 |
) |
| 1473 |
assert result.exit_code == 0 |
| 1474 |
stdout = result.stdout.strip() |
| 1475 |
assert re.search( |
| 1476 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1477 |
stdout, |
| 1478 |
re.MULTILINE, |
| 1479 |
) |
| 1480 |
# Stale dirs must not bleed into output when URL filter is active |
| 1481 |
assert not re.search(r"aaaaaaaa", stdout) |
| 1482 |
|
| 1483 |
# URL filter scopes PRUNE too: only the test:// plugin's storage |
| 1484 |
# directory is eligible; stale dirs like 'aaaaaaaa' are left alone. |
| 1485 |
# Use --dry-run so we exercise the _had_url_filters namespace-scoping |
| 1486 |
# branch without actually modifying disk (keeping ea482db7 active for |
| 1487 |
# the assertions that follow). |
| 1488 |
result = runner.invoke( |
| 1489 |
cli.main, |
| 1490 |
[ |
| 1491 |
"--storage-path", |
| 1492 |
str(tmpdir), |
| 1493 |
"--storage-prune-days", |
| 1494 |
0, |
| 1495 |
"--dry-run", |
| 1496 |
"storage", |
| 1497 |
"prune", |
| 1498 |
"test://", |
| 1499 |
], |
| 1500 |
) |
| 1501 |
assert result.exit_code == 0 |
| 1502 |
# The stale 'aaaaaaaa' directory must still exist after the URL-scoped |
| 1503 |
# prune because it was outside the test:// namespace. |
| 1504 |
assert os.path.isdir(os.path.join(str(tmpdir), "aaaaaaaa")) |
| 1505 |
|
| 1506 |
# URL filter scopes CLEAR too: only the test:// plugin's namespace is |
| 1507 |
# cleared; unrelated stale directories are untouched. |
| 1508 |
# Use --dry-run so we exercise the _had_url_filters scoping path |
| 1509 |
# without wiping ea482db7 contents (subsequent tests expect it active). |
| 1510 |
result = runner.invoke( |
| 1511 |
cli.main, |
| 1512 |
[ |
| 1513 |
"--storage-path", |
| 1514 |
str(tmpdir), |
| 1515 |
"--dry-run", |
| 1516 |
"storage", |
| 1517 |
"clear", |
| 1518 |
"test://", |
| 1519 |
], |
| 1520 |
) |
| 1521 |
assert result.exit_code == 0 |
| 1522 |
# 'aaaaaaaa' must still be present — it was outside the cleared scope |
| 1523 |
assert os.path.isdir(os.path.join(str(tmpdir), "aaaaaaaa")) |
| 1524 |
|
| 1525 |
# Tag-scoped PRUNE: --tag limits the prune namespace to only the uids |
| 1526 |
# that matched the tag filter. The stale 'aaaaaaaa' directory is |
| 1527 |
# outside that namespace and must not be removed. |
| 1528 |
# Use --dry-run so the test is non-destructive and ea482db7 stays active. |
| 1529 |
result = runner.invoke( |
| 1530 |
cli.main, |
| 1531 |
[ |
| 1532 |
"--storage-path", |
| 1533 |
str(tmpdir), |
| 1534 |
"--config", |
| 1535 |
str(config), |
| 1536 |
"--tag", |
| 1537 |
"test", |
| 1538 |
"--storage-prune-days", |
| 1539 |
0, |
| 1540 |
"--dry-run", |
| 1541 |
"storage", |
| 1542 |
"prune", |
| 1543 |
], |
| 1544 |
) |
| 1545 |
assert result.exit_code == 0 |
| 1546 |
# The stale 'aaaaaaaa' directory must be untouched — it was outside the |
| 1547 |
# tag-scoped prune namespace. |
| 1548 |
assert os.path.isdir(os.path.join(str(tmpdir), "aaaaaaaa")) |
| 1549 |
|
| 1550 |
# Tag-scoped CLEAR: same principle — only the tagged plugin's namespace |
| 1551 |
# is eligible for clearing; everything else is left alone. |
| 1552 |
# Use --dry-run to keep ea482db7 active for subsequent assertions. |
| 1553 |
result = runner.invoke( |
| 1554 |
cli.main, |
| 1555 |
[ |
| 1556 |
"--storage-path", |
| 1557 |
str(tmpdir), |
| 1558 |
"--config", |
| 1559 |
str(config), |
| 1560 |
"--tag", |
| 1561 |
"test", |
| 1562 |
"--dry-run", |
| 1563 |
"storage", |
| 1564 |
"clear", |
| 1565 |
], |
| 1566 |
) |
| 1567 |
assert result.exit_code == 0 |
| 1568 |
# The stale 'aaaaaaaa' directory must survive — it was not in scope |
| 1569 |
assert os.path.isdir(os.path.join(str(tmpdir), "aaaaaaaa")) |
| 1570 |
|
| 1571 |
# When a tag or URL filter is specified but resolves to nothing, |
| 1572 |
# disk_prune() must NOT be called. The tests below confirm that ea482db7 |
| 1573 |
# survives each no-match check. |
| 1574 |
|
| 1575 |
# Tag-scoped PRUNE with a tag that matches no loaded plugin: nothing |
| 1576 |
# resolves so disk_prune() must be skipped entirely. |
| 1577 |
result = runner.invoke( |
| 1578 |
cli.main, |
| 1579 |
[ |
| 1580 |
"--storage-path", |
| 1581 |
str(tmpdir), |
| 1582 |
"--config", |
| 1583 |
str(config), |
| 1584 |
"--tag", |
| 1585 |
"no-such-tag", |
| 1586 |
"--storage-prune-days", |
| 1587 |
0, |
| 1588 |
"storage", |
| 1589 |
"prune", |
| 1590 |
], |
| 1591 |
) |
| 1592 |
assert result.exit_code == 0 |
| 1593 |
# A prune with no matches must leave existing storage intact. |
| 1594 |
assert os.path.isdir(os.path.join(str(tmpdir), "ea482db7")) |
| 1595 |
|
| 1596 |
# Tag-scoped CLEAR with the same unmatchable tag: same guard must fire. |
| 1597 |
result = runner.invoke( |
| 1598 |
cli.main, |
| 1599 |
[ |
| 1600 |
"--storage-path", |
| 1601 |
str(tmpdir), |
| 1602 |
"--config", |
| 1603 |
str(config), |
| 1604 |
"--tag", |
| 1605 |
"no-such-tag", |
| 1606 |
"storage", |
| 1607 |
"clear", |
| 1608 |
], |
| 1609 |
) |
| 1610 |
assert result.exit_code == 0 |
| 1611 |
# A clear with no matches must leave existing storage intact. |
| 1612 |
assert os.path.isdir(os.path.join(str(tmpdir), "ea482db7")) |
| 1613 |
|
| 1614 |
# URL filter that fails to load (unknown schema): _had_url_filters is |
| 1615 |
# True but no uid resolves, so disk_prune() must not be called. |
| 1616 |
result = runner.invoke( |
| 1617 |
cli.main, |
| 1618 |
[ |
| 1619 |
"--storage-path", |
| 1620 |
str(tmpdir), |
| 1621 |
"--storage-prune-days", |
| 1622 |
0, |
| 1623 |
"storage", |
| 1624 |
"prune", |
| 1625 |
"nosuchschema://", |
| 1626 |
], |
| 1627 |
) |
| 1628 |
assert result.exit_code == 0 |
| 1629 |
assert os.path.isdir(os.path.join(str(tmpdir), "ea482db7")) |
| 1630 |
|
| 1631 |
# Same guard applies to clear when the URL filter resolves nothing. |
| 1632 |
result = runner.invoke( |
| 1633 |
cli.main, |
| 1634 |
[ |
| 1635 |
"--storage-path", |
| 1636 |
str(tmpdir), |
| 1637 |
"storage", |
| 1638 |
"clear", |
| 1639 |
"nosuchschema://", |
| 1640 |
], |
| 1641 |
) |
| 1642 |
assert result.exit_code == 0 |
| 1643 |
assert os.path.isdir(os.path.join(str(tmpdir), "ea482db7")) |
| 1644 |
|
| 1645 |
# Prune call but prune-days set incorrectly |
| 1646 |
result = runner.invoke( |
| 1647 |
cli.main, |
| 1648 |
[ |
| 1649 |
"--storage-path", |
| 1650 |
str(tmpdir), |
| 1651 |
"--storage-prune-days", |
| 1652 |
-1, |
| 1653 |
"storage", |
| 1654 |
"prune", |
| 1655 |
], |
| 1656 |
) |
| 1657 |
# storage prune days is invalid |
| 1658 |
assert result.exit_code == 2 |
| 1659 |
|
| 1660 |
# Create a tmporary namespace |
| 1661 |
tmpdir.mkdir("namespace") |
| 1662 |
|
| 1663 |
# Generates another listing |
| 1664 |
result = runner.invoke( |
| 1665 |
cli.main, |
| 1666 |
[ |
| 1667 |
"--storage-path", |
| 1668 |
str(tmpdir), |
| 1669 |
"--config", |
| 1670 |
str(config), |
| 1671 |
"storage", |
| 1672 |
], |
| 1673 |
) |
| 1674 |
|
| 1675 |
# list our entries |
| 1676 |
assert result.exit_code == 0 |
| 1677 |
|
| 1678 |
stdout = result.stdout.strip() |
| 1679 |
assert re.search( |
| 1680 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1681 |
stdout, |
| 1682 |
re.MULTILINE, |
| 1683 |
) |
| 1684 |
assert re.match( |
| 1685 |
r".*\s*[0-9]\.\s+namespace\s+0\.00B\s+stale.*", |
| 1686 |
stdout, |
| 1687 |
(re.MULTILINE | re.DOTALL), |
| 1688 |
) |
| 1689 |
|
| 1690 |
# Generates another listing but utilize the tag |
| 1691 |
result = runner.invoke( |
| 1692 |
cli.main, |
| 1693 |
[ |
| 1694 |
"--storage-path", |
| 1695 |
str(tmpdir), |
| 1696 |
"--config", |
| 1697 |
str(config), |
| 1698 |
"--tag", |
| 1699 |
"test", |
| 1700 |
"storage", |
| 1701 |
], |
| 1702 |
) |
| 1703 |
|
| 1704 |
# list our entries |
| 1705 |
assert result.exit_code == 0 |
| 1706 |
|
| 1707 |
stdout = result.stdout.strip() |
| 1708 |
assert re.search( |
| 1709 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1710 |
stdout, |
| 1711 |
re.MULTILINE, |
| 1712 |
) |
| 1713 |
assert ( |
| 1714 |
re.match( |
| 1715 |
r".*\s*[0-9]\.\s+namespace\s+0\.00B\s+stale.*", |
| 1716 |
stdout, |
| 1717 |
(re.MULTILINE | re.DOTALL), |
| 1718 |
) |
| 1719 |
is None |
| 1720 |
) |
| 1721 |
|
| 1722 |
# Clear all of our accumulated disk space |
| 1723 |
result = runner.invoke( |
| 1724 |
cli.main, |
| 1725 |
[ |
| 1726 |
"--storage-path", |
| 1727 |
str(tmpdir), |
| 1728 |
"--config", |
| 1729 |
str(config), |
| 1730 |
"storage", |
| 1731 |
"clear", |
| 1732 |
], |
| 1733 |
) |
| 1734 |
|
| 1735 |
# successful |
| 1736 |
assert result.exit_code == 0 |
| 1737 |
|
| 1738 |
# Generate another listing |
| 1739 |
result = runner.invoke( |
| 1740 |
cli.main, |
| 1741 |
[ |
| 1742 |
"--storage-path", |
| 1743 |
str(tmpdir), |
| 1744 |
"--config", |
| 1745 |
str(config), |
| 1746 |
"storage", |
| 1747 |
], |
| 1748 |
) |
| 1749 |
|
| 1750 |
# list our entries |
| 1751 |
assert result.exit_code == 0 |
| 1752 |
|
| 1753 |
stdout = result.stdout.strip() |
| 1754 |
# back to unused state and 0 bytes |
| 1755 |
assert re.search( |
| 1756 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+unused\b", |
| 1757 |
stdout, |
| 1758 |
re.MULTILINE, |
| 1759 |
) |
| 1760 |
# namespace is gone now |
| 1761 |
assert ( |
| 1762 |
re.match( |
| 1763 |
r".*\s*[0-9]\.\s+namespace\s+0\.00B\s+stale.*", |
| 1764 |
stdout, |
| 1765 |
(re.MULTILINE | re.DOTALL), |
| 1766 |
) |
| 1767 |
is None |
| 1768 |
) |
| 1769 |
|
| 1770 |
# Provide both tags and uid |
| 1771 |
result = runner.invoke( |
| 1772 |
cli.main, |
| 1773 |
[ |
| 1774 |
"--storage-path", |
| 1775 |
str(tmpdir), |
| 1776 |
"--config", |
| 1777 |
str(config), |
| 1778 |
"storage", |
| 1779 |
"ea", |
| 1780 |
"-g", |
| 1781 |
"test", |
| 1782 |
], |
| 1783 |
) |
| 1784 |
|
| 1785 |
# list our entries |
| 1786 |
assert result.exit_code == 0 |
| 1787 |
|
| 1788 |
stdout = result.stdout.strip() |
| 1789 |
# back to unused state and 0 bytes |
| 1790 |
assert re.match( |
| 1791 |
r"^[0-9]\.\s+[a-z0-9_-]{8}\s+0\.00B\s+unused\s+-\s+test://$", |
| 1792 |
stdout, |
| 1793 |
re.MULTILINE, |
| 1794 |
) |
| 1795 |
|
| 1796 |
# Generate notification that creates persistent data |
| 1797 |
result = runner.invoke( |
| 1798 |
cli.main, |
| 1799 |
[ |
| 1800 |
"--storage-path", |
| 1801 |
str(tmpdir), |
| 1802 |
"--storage-mode", |
| 1803 |
"flush", |
| 1804 |
"--config", |
| 1805 |
str(config), |
| 1806 |
"-t", |
| 1807 |
"title", |
| 1808 |
"-b", |
| 1809 |
"body", |
| 1810 |
"-g", |
| 1811 |
"test", |
| 1812 |
], |
| 1813 |
) |
| 1814 |
# We parsed our data accordingly |
| 1815 |
assert result.exit_code == 0 |
| 1816 |
|
| 1817 |
# Have a look at our storage listings |
| 1818 |
result = runner.invoke( |
| 1819 |
cli.main, |
| 1820 |
[ |
| 1821 |
"--storage-path", |
| 1822 |
str(tmpdir), |
| 1823 |
"--config", |
| 1824 |
str(config), |
| 1825 |
"storage", |
| 1826 |
"list", |
| 1827 |
], |
| 1828 |
) |
| 1829 |
# list our entries |
| 1830 |
assert result.exit_code == 0 |
| 1831 |
|
| 1832 |
stdout = result.stdout.strip() |
| 1833 |
assert re.search( |
| 1834 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1835 |
stdout, |
| 1836 |
re.MULTILINE, |
| 1837 |
) |
| 1838 |
|
| 1839 |
# Prune call but prune-days set incorrectly |
| 1840 |
result = runner.invoke( |
| 1841 |
cli.main, |
| 1842 |
[ |
| 1843 |
"--storage-path", |
| 1844 |
str(tmpdir), |
| 1845 |
"storage", |
| 1846 |
"prune", |
| 1847 |
], |
| 1848 |
) |
| 1849 |
|
| 1850 |
# Run our prune successfully |
| 1851 |
assert result.exit_code == 0 |
| 1852 |
|
| 1853 |
# Have a look at our storage listings (expected no change in output) |
| 1854 |
result = runner.invoke( |
| 1855 |
cli.main, |
| 1856 |
[ |
| 1857 |
"--storage-path", |
| 1858 |
str(tmpdir), |
| 1859 |
"--config", |
| 1860 |
str(config), |
| 1861 |
"storage", |
| 1862 |
"list", |
| 1863 |
], |
| 1864 |
) |
| 1865 |
# list our entries |
| 1866 |
assert result.exit_code == 0 |
| 1867 |
|
| 1868 |
stdout = result.stdout.strip() |
| 1869 |
assert re.search( |
| 1870 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+active\b", |
| 1871 |
stdout, |
| 1872 |
re.MULTILINE, |
| 1873 |
) |
| 1874 |
|
| 1875 |
# Prune call but prune-days set incorrectly |
| 1876 |
result = runner.invoke( |
| 1877 |
cli.main, |
| 1878 |
[ |
| 1879 |
"--storage-path", |
| 1880 |
str(tmpdir), |
| 1881 |
# zero simulates a full clean |
| 1882 |
"--storage-prune-days", |
| 1883 |
0, |
| 1884 |
"storage", |
| 1885 |
"prune", |
| 1886 |
], |
| 1887 |
) |
| 1888 |
|
| 1889 |
# Run our prune successfully |
| 1890 |
assert result.exit_code == 0 |
| 1891 |
|
| 1892 |
# Have a look at our storage listings (expected no change in output) |
| 1893 |
result = runner.invoke( |
| 1894 |
cli.main, |
| 1895 |
[ |
| 1896 |
"--storage-path", |
| 1897 |
str(tmpdir), |
| 1898 |
"--config", |
| 1899 |
str(config), |
| 1900 |
"storage", |
| 1901 |
"list", |
| 1902 |
], |
| 1903 |
) |
| 1904 |
# list our entries |
| 1905 |
assert result.exit_code == 0 |
| 1906 |
|
| 1907 |
# Note: An prune/expiry of zero gets everything except for MS Windows |
| 1908 |
# during testing only. |
| 1909 |
# Until this can be resolved, this is the section of the test that |
| 1910 |
# caused us to disable it in MS Windows |
| 1911 |
stdout = result.stdout.strip() |
| 1912 |
assert re.search( |
| 1913 |
r"^1\.\s+[a-z0-9_-]{8}\s+\d+(?:\.\d{2})?[KMGT]?B\s+unused\b", |
| 1914 |
stdout, |
| 1915 |
re.MULTILINE, |
| 1916 |
) |
| 1917 |
|
| 1918 |
# New Temporary namespace |
| 1919 |
new_persistent_base = tmpdir.mkdir("namespace") |
| 1920 |
with environ(APPRISE_STORAGE_PATH=str(new_persistent_base)): |
| 1921 |
# Reload our module |
| 1922 |
reload(cli) |
| 1923 |
|
| 1924 |
# Nothing in our directory yet |
| 1925 |
dir_content = os.listdir(str(new_persistent_base)) |
| 1926 |
assert len(dir_content) == 0 |
| 1927 |
|
| 1928 |
# Generate notification that creates persistent data |
| 1929 |
# storage path is pulled out of our environment variable |
| 1930 |
result = runner.invoke( |
| 1931 |
cli.main, |
| 1932 |
[ |
| 1933 |
"--storage-mode", |
| 1934 |
"flush", |
| 1935 |
"--config", |
| 1936 |
str(config), |
| 1937 |
"-t", |
| 1938 |
"title", |
| 1939 |
"-b", |
| 1940 |
"body", |
| 1941 |
"-g", |
| 1942 |
"test", |
| 1943 |
], |
| 1944 |
) |
| 1945 |
# We parsed our data accordingly |
| 1946 |
assert result.exit_code == 0 |
| 1947 |
|
| 1948 |
# Now content exists |
| 1949 |
dir_content = os.listdir(str(new_persistent_base)) |
| 1950 |
assert len(dir_content) == 1 |
| 1951 |
|
| 1952 |
# Reload our module with our environment variable gone |
| 1953 |
reload(cli) |
| 1954 |
|
| 1955 |
# Clear loaded modules |
| 1956 |
N_MGR.unload_modules() |
| 1957 |
|
| 1958 |
|
| 1959 |
def test_apprise_cli_details(tmpdir): |
| 1960 |
""" |
| 1961 |
CLI: --details (-l) |
| 1962 |
|
| 1963 |
""" |
| 1964 |
|
| 1965 |
runner = CliRunner() |
| 1966 |
|
| 1967 |
# |
| 1968 |
# Testing the printout of our details |
| 1969 |
# --details or -l |
| 1970 |
# |
| 1971 |
result = runner.invoke( |
| 1972 |
cli.main, |
| 1973 |
[ |
| 1974 |
"--details", |
| 1975 |
], |
| 1976 |
) |
| 1977 |
assert result.exit_code == 0 |
| 1978 |
|
| 1979 |
result = runner.invoke( |
| 1980 |
cli.main, |
| 1981 |
[ |
| 1982 |
"-l", |
| 1983 |
], |
| 1984 |
) |
| 1985 |
assert result.exit_code == 0 |
| 1986 |
|
| 1987 |
# Clear loaded modules |
| 1988 |
N_MGR.unload_modules() |
| 1989 |
|
| 1990 |
# This is a made up class that is just used to verify |
| 1991 |
class TestReq01Notification(NotifyBase): |
| 1992 |
"""This class is used to test various requirement configurations.""" |
| 1993 |
|
| 1994 |
# Set some requirements |
| 1995 |
requirements: ClassVar[RequirementsSpec] = { |
| 1996 |
"packages_required": [ |
| 1997 |
"cryptography <= 3.4", |
| 1998 |
"ultrasync", |
| 1999 |
], |
| 2000 |
"packages_recommended": "django", |
| 2001 |
} |
| 2002 |
|
| 2003 |
def url(self, **kwargs): |
| 2004 |
# Support URL |
| 2005 |
return "" |
| 2006 |
|
| 2007 |
def send(self, **kwargs): |
| 2008 |
# Pretend everything is okay (so we don't break other tests) |
| 2009 |
return True |
| 2010 |
|
| 2011 |
N_MGR["req01"] = TestReq01Notification |
| 2012 |
|
| 2013 |
# This is a made up class that is just used to verify |
| 2014 |
class TestReq02Notification(NotifyBase): |
| 2015 |
"""This class is used to test various requirement configurations.""" |
| 2016 |
|
| 2017 |
# Just not enabled at all |
| 2018 |
enabled = False |
| 2019 |
|
| 2020 |
# Set some requirements |
| 2021 |
requirements: ClassVar[RequirementsSpec] = { |
| 2022 |
# None and/or [] is implied, but jsut to show that the code won't |
| 2023 |
# crash if explicitly set this way: |
| 2024 |
"packages_required": None, |
| 2025 |
"packages_recommended": [ |
| 2026 |
"cryptography <= 3.4", |
| 2027 |
], |
| 2028 |
} |
| 2029 |
|
| 2030 |
def url(self, **kwargs): |
| 2031 |
# Support URL |
| 2032 |
return "" |
| 2033 |
|
| 2034 |
def send(self, **kwargs): |
| 2035 |
# Pretend everything is okay (so we don't break other tests) |
| 2036 |
return True |
| 2037 |
|
| 2038 |
N_MGR["req02"] = TestReq02Notification |
| 2039 |
|
| 2040 |
# This is a made up class that is just used to verify |
| 2041 |
class TestReq03Notification(NotifyBase): |
| 2042 |
"""This class is used to test various requirement configurations.""" |
| 2043 |
|
| 2044 |
# Set requirements with a custom details value. |
| 2045 |
requirements: ClassVar[RequirementsSpec] = { |
| 2046 |
# Allow callers to override the plugin's default details. |
| 2047 |
"details": _("some specified requirement details"), |
| 2048 |
# We can set a string value as well (it does not have to be a list) |
| 2049 |
"packages_recommended": "cryptography <= 3.4", |
| 2050 |
} |
| 2051 |
|
| 2052 |
def url(self, **kwargs): |
| 2053 |
# Support URL |
| 2054 |
return "" |
| 2055 |
|
| 2056 |
def send(self, **kwargs): |
| 2057 |
# Pretend everything is okay (so we don't break other tests) |
| 2058 |
return True |
| 2059 |
|
| 2060 |
N_MGR["req03"] = TestReq03Notification |
| 2061 |
|
| 2062 |
# This is a made up class that is just used to verify |
| 2063 |
class TestReq04Notification(NotifyBase): |
| 2064 |
"""This class is used to test a case where our requirements is fixed to |
| 2065 |
a None.""" |
| 2066 |
|
| 2067 |
# This is the same as saying there are no requirements |
| 2068 |
requirements = None |
| 2069 |
|
| 2070 |
def url(self, **kwargs): |
| 2071 |
# Support URL |
| 2072 |
return "" |
| 2073 |
|
| 2074 |
def send(self, **kwargs): |
| 2075 |
# Pretend everything is okay (so we don't break other tests) |
| 2076 |
return True |
| 2077 |
|
| 2078 |
N_MGR["req04"] = TestReq04Notification |
| 2079 |
|
| 2080 |
# This is a made up class that is just used to verify |
| 2081 |
class TestReq05Notification(NotifyBase): |
| 2082 |
"""This class is used to test a case where only packages_recommended is |
| 2083 |
identified.""" |
| 2084 |
|
| 2085 |
requirements: ClassVar[RequirementsSpec] = { |
| 2086 |
"packages_recommended": "cryptography <= 3.4" |
| 2087 |
} |
| 2088 |
|
| 2089 |
def url(self, **kwargs): |
| 2090 |
# Support URL |
| 2091 |
return "" |
| 2092 |
|
| 2093 |
def send(self, **kwargs): |
| 2094 |
# Pretend everything is okay (so we don't break other tests) |
| 2095 |
return True |
| 2096 |
|
| 2097 |
N_MGR["req05"] = TestReq05Notification |
| 2098 |
|
| 2099 |
class TestDisabled01Notification(NotifyBase): |
| 2100 |
"""This class is used to test a pre-disabled state.""" |
| 2101 |
|
| 2102 |
# Just flat out disable our service |
| 2103 |
enabled = False |
| 2104 |
|
| 2105 |
# we'll use this as a key to make our service easier to find |
| 2106 |
# in the next part of the testing |
| 2107 |
service_name = "na01" |
| 2108 |
|
| 2109 |
def url(self, **kwargs): |
| 2110 |
# Support URL |
| 2111 |
return "" |
| 2112 |
|
| 2113 |
def notify(self, **kwargs): |
| 2114 |
# Pretend everything is okay (so we don't break other tests) |
| 2115 |
return True |
| 2116 |
|
| 2117 |
N_MGR["na01"] = TestDisabled01Notification |
| 2118 |
|
| 2119 |
class TestDisabled02Notification(NotifyBase): |
| 2120 |
"""This class is used to test a post-disabled state.""" |
| 2121 |
|
| 2122 |
# we'll use this as a key to make our service easier to find |
| 2123 |
# in the next part of the testing |
| 2124 |
service_name = "na02" |
| 2125 |
|
| 2126 |
def __init__(self, *args, **kwargs): |
| 2127 |
super().__init__(**kwargs) |
| 2128 |
|
| 2129 |
# enable state changes **AFTER** we initialize |
| 2130 |
self.enabled = False |
| 2131 |
|
| 2132 |
def url(self, **kwargs): |
| 2133 |
# Support URL |
| 2134 |
return "" |
| 2135 |
|
| 2136 |
def notify(self, **kwargs): |
| 2137 |
# Pretend everything is okay (so we don't break other tests) |
| 2138 |
return True |
| 2139 |
|
| 2140 |
N_MGR["na02"] = TestDisabled02Notification |
| 2141 |
|
| 2142 |
# We'll add a good notification to our list |
| 2143 |
class TesEnabled01Notification(NotifyBase): |
| 2144 |
"""This class is just a simple enabled one.""" |
| 2145 |
|
| 2146 |
# we'll use this as a key to make our service easier to find |
| 2147 |
# in the next part of the testing |
| 2148 |
service_name = "good" |
| 2149 |
|
| 2150 |
def url(self, **kwargs): |
| 2151 |
# Support URL |
| 2152 |
return "" |
| 2153 |
|
| 2154 |
def send(self, **kwargs): |
| 2155 |
# Pretend everything is okay (so we don't break other tests) |
| 2156 |
return True |
| 2157 |
|
| 2158 |
N_MGR["good"] = TesEnabled01Notification |
| 2159 |
|
| 2160 |
# Verify that we can pass through all of our different details |
| 2161 |
result = runner.invoke( |
| 2162 |
cli.main, |
| 2163 |
[ |
| 2164 |
"--details", |
| 2165 |
], |
| 2166 |
) |
| 2167 |
assert result.exit_code == 0 |
| 2168 |
|
| 2169 |
result = runner.invoke( |
| 2170 |
cli.main, |
| 2171 |
[ |
| 2172 |
"-l", |
| 2173 |
], |
| 2174 |
) |
| 2175 |
assert result.exit_code == 0 |
| 2176 |
|
| 2177 |
# Clear loaded modules |
| 2178 |
N_MGR.unload_modules() |
| 2179 |
|
| 2180 |
|
| 2181 |
def test_apprise_cli_print_help(): |
| 2182 |
""" |
| 2183 |
CLI: --help (-h) |
| 2184 |
|
| 2185 |
""" |
| 2186 |
runner = CliRunner() |
| 2187 |
|
| 2188 |
# Clear our working variables so they don't obstruct the next test |
| 2189 |
# This simulates an actual call from the CLI. Unfortunately through |
| 2190 |
# testing were occupying the same memory space so our singleton's |
| 2191 |
# have already been populated |
| 2192 |
N_MGR._paths_previously_scanned.clear() |
| 2193 |
N_MGR._custom_module_map.clear() |
| 2194 |
|
| 2195 |
# Print help and exit |
| 2196 |
result = runner.invoke(cli.main, ["--help"]) |
| 2197 |
assert result.exit_code == 0 |
| 2198 |
|
| 2199 |
result = runner.invoke(cli.main, ["-h"]) |
| 2200 |
assert result.exit_code == 0 |
| 2201 |
|
| 2202 |
|
| 2203 |
@mock.patch("requests.request") |
| 2204 |
def test_apprise_cli_plugin_loading(mock_request, tmpdir): |
| 2205 |
""" |
| 2206 |
CLI: --plugin-path (-P) |
| 2207 |
|
| 2208 |
""" |
| 2209 |
# Prepare Mock |
| 2210 |
mock_request.return_value = requests.Request() |
| 2211 |
mock_request.return_value.status_code = requests.codes.ok |
| 2212 |
|
| 2213 |
runner = CliRunner() |
| 2214 |
|
| 2215 |
# Clear our working variables so they don't obstruct the next test |
| 2216 |
# This simulates an actual call from the CLI. Unfortunately through |
| 2217 |
# testing were occupying the same memory space so our singleton's |
| 2218 |
# have already been populated |
| 2219 |
N_MGR._paths_previously_scanned.clear() |
| 2220 |
N_MGR._custom_module_map.clear() |
| 2221 |
|
| 2222 |
# Test a path that has no files to load in it |
| 2223 |
result = runner.invoke( |
| 2224 |
cli.main, |
| 2225 |
[ |
| 2226 |
"--plugin-path", |
| 2227 |
join(str(tmpdir), "invalid_path"), |
| 2228 |
"-b", |
| 2229 |
"test\nbody", |
| 2230 |
"json://localhost", |
| 2231 |
], |
| 2232 |
) |
| 2233 |
# The valid notification still loads despite the invalid path. |
| 2234 |
assert result.exit_code == 0 |
| 2235 |
|
| 2236 |
# Directories that don't exist passed in by the CLI aren't even scanned |
| 2237 |
assert len(N_MGR._paths_previously_scanned) == 0 |
| 2238 |
assert len(N_MGR._custom_module_map) == 0 |
| 2239 |
|
| 2240 |
# Test our current existing path that has no entries in it |
| 2241 |
result = runner.invoke( |
| 2242 |
cli.main, |
| 2243 |
[ |
| 2244 |
"--plugin-path", |
| 2245 |
str(tmpdir.mkdir("empty")), |
| 2246 |
"-b", |
| 2247 |
"test\nbody", |
| 2248 |
"json://localhost", |
| 2249 |
], |
| 2250 |
) |
| 2251 |
# The valid notification still loads despite the invalid path. |
| 2252 |
assert result.exit_code == 0 |
| 2253 |
assert len(N_MGR._paths_previously_scanned) == 1 |
| 2254 |
assert join(str(tmpdir), "empty") in N_MGR._paths_previously_scanned |
| 2255 |
|
| 2256 |
# However there was nothing to load |
| 2257 |
assert len(N_MGR._custom_module_map) == 0 |
| 2258 |
|
| 2259 |
# Clear our working variables so they don't obstruct the next test |
| 2260 |
# This simulates an actual call from the CLI. Unfortunately through |
| 2261 |
# testing were occupying the same memory space so our singleton's |
| 2262 |
# have already been populated |
| 2263 |
N_MGR._paths_previously_scanned.clear() |
| 2264 |
N_MGR._custom_module_map.clear() |
| 2265 |
|
| 2266 |
# Prepare ourselves a file to work with |
| 2267 |
notify_hook_a_base = tmpdir.mkdir("random") |
| 2268 |
notify_hook_a = notify_hook_a_base.join("myhook01.py") |
| 2269 |
notify_hook_a.write( |
| 2270 |
cleandoc(""" |
| 2271 |
raise ImportError |
| 2272 |
""") |
| 2273 |
) |
| 2274 |
|
| 2275 |
result = runner.invoke( |
| 2276 |
cli.main, |
| 2277 |
[ |
| 2278 |
"--plugin-path", |
| 2279 |
str(notify_hook_a), |
| 2280 |
"-b", |
| 2281 |
"test\nbody", |
| 2282 |
# A custom hook: |
| 2283 |
"clihook://", |
| 2284 |
], |
| 2285 |
) |
| 2286 |
# It doesn't exist so it will fail |
| 2287 |
# meanwhile we would have failed to load the myhook path |
| 2288 |
assert result.exit_code == 1 |
| 2289 |
|
| 2290 |
# The valid notification still loads despite the invalid path. |
| 2291 |
assert len(N_MGR._paths_previously_scanned) == 1 |
| 2292 |
assert str(notify_hook_a) in N_MGR._paths_previously_scanned |
| 2293 |
# However there was nothing to load |
| 2294 |
assert len(N_MGR._custom_module_map) == 0 |
| 2295 |
|
| 2296 |
# Prepare ourselves a file to work with |
| 2297 |
notify_hook_aa = notify_hook_a_base.join("myhook02.py") |
| 2298 |
notify_hook_aa.write( |
| 2299 |
cleandoc(""" |
| 2300 |
garbage entry |
| 2301 |
""") |
| 2302 |
) |
| 2303 |
|
| 2304 |
N_MGR.plugins() |
| 2305 |
result = runner.invoke( |
| 2306 |
cli.main, |
| 2307 |
[ |
| 2308 |
"--plugin-path", |
| 2309 |
str(notify_hook_aa), |
| 2310 |
"-b", |
| 2311 |
"test\nbody", |
| 2312 |
# A custom hook: |
| 2313 |
"clihook://custom", |
| 2314 |
], |
| 2315 |
) |
| 2316 |
# It doesn't exist so it will fail |
| 2317 |
# meanwhile we would have failed to load the myhook path |
| 2318 |
assert result.exit_code == 1 |
| 2319 |
|
| 2320 |
# The path is silently loaded but fails... |
| 2321 |
# as a result the path stacks with the last |
| 2322 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2323 |
assert str(notify_hook_a) in N_MGR._paths_previously_scanned |
| 2324 |
assert str(notify_hook_aa) in N_MGR._paths_previously_scanned |
| 2325 |
# However there was nothing to load |
| 2326 |
assert len(N_MGR._custom_module_map) == 0 |
| 2327 |
|
| 2328 |
# Clear our working variables so they don't obstruct the next test |
| 2329 |
# This simulates an actual call from the CLI. Unfortunately through |
| 2330 |
# testing were occupying the same memory space so our singleton's |
| 2331 |
# have already been populated |
| 2332 |
N_MGR._paths_previously_scanned.clear() |
| 2333 |
N_MGR._custom_module_map.clear() |
| 2334 |
|
| 2335 |
# Prepare ourselves a file to work with |
| 2336 |
notify_hook_b = tmpdir.mkdir("goodmodule").join("__init__.py") |
| 2337 |
notify_hook_b.write( |
| 2338 |
cleandoc(""" |
| 2339 |
from apprise.decorators import notify |
| 2340 |
|
| 2341 |
# We want to trigger on anyone who configures a call to clihook:// |
| 2342 |
@notify(on="clihook") |
| 2343 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 2344 |
# A simple test - print to screen |
| 2345 |
print("{}: {} - {}".format(notify_type, title, body)) |
| 2346 |
|
| 2347 |
# No return (so a return of None) get's translated to True |
| 2348 |
|
| 2349 |
# Define another in the same file |
| 2350 |
@notify(on="clihookA") |
| 2351 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 2352 |
# A simple test - print to screen |
| 2353 |
print("!! {}: {} - {}".format(notify_type, title, body)) |
| 2354 |
|
| 2355 |
# No return (so a return of None) get's translated to True |
| 2356 |
""") |
| 2357 |
) |
| 2358 |
|
| 2359 |
result = runner.invoke( |
| 2360 |
cli.main, |
| 2361 |
[ |
| 2362 |
"--plugin-path", |
| 2363 |
str(tmpdir), |
| 2364 |
"-b", |
| 2365 |
"test body", |
| 2366 |
# A custom hook: |
| 2367 |
"clihook://still/valid", |
| 2368 |
], |
| 2369 |
) |
| 2370 |
|
| 2371 |
# We can detect the goodmodule (which has an __init__.py in it) |
| 2372 |
# so we'll load okay |
| 2373 |
assert result.exit_code == 0 |
| 2374 |
|
| 2375 |
# Let's see how things got loaded: |
| 2376 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2377 |
assert str(tmpdir) in N_MGR._paths_previously_scanned |
| 2378 |
# absolute path to detected module is also added |
| 2379 |
assert ( |
| 2380 |
join(str(tmpdir), "goodmodule", "__init__.py") |
| 2381 |
in N_MGR._paths_previously_scanned |
| 2382 |
) |
| 2383 |
|
| 2384 |
# We also loaded our clihook properly |
| 2385 |
assert len(N_MGR._custom_module_map) == 1 |
| 2386 |
|
| 2387 |
# We can find our new hook loaded in our schema map now... |
| 2388 |
assert "clihook" in N_MGR |
| 2389 |
|
| 2390 |
# Capture our key for reference |
| 2391 |
key = next(iter(N_MGR._custom_module_map.keys())) |
| 2392 |
|
| 2393 |
# We loaded 2 entries from the same file |
| 2394 |
assert len(N_MGR._custom_module_map[key]["notify"]) == 2 |
| 2395 |
assert "clihook" in N_MGR._custom_module_map[key]["notify"] |
| 2396 |
# Converted to lower case |
| 2397 |
assert "clihooka" in N_MGR._custom_module_map[key]["notify"] |
| 2398 |
|
| 2399 |
# Our function name |
| 2400 |
assert ( |
| 2401 |
N_MGR._custom_module_map[key]["notify"]["clihook"]["fn_name"] |
| 2402 |
== "mywrapper" |
| 2403 |
) |
| 2404 |
# What we parsed from the `on` keyword in the @notify decorator |
| 2405 |
assert ( |
| 2406 |
N_MGR._custom_module_map[key]["notify"]["clihook"]["url"] |
| 2407 |
== "clihook://" |
| 2408 |
) |
| 2409 |
# our default name Assignment. This can be-overridden on the @notify |
| 2410 |
# decorator by just adding a name= to the parameter list |
| 2411 |
assert N_MGR["clihook"].service_name == "Custom - clihook" |
| 2412 |
|
| 2413 |
# Our Base Notification object when initialized: |
| 2414 |
assert ( |
| 2415 |
len(N_MGR._module_map[N_MGR._custom_module_map[key]["name"]]["plugin"]) |
| 2416 |
== 2 |
| 2417 |
) |
| 2418 |
for plugin in N_MGR._module_map[N_MGR._custom_module_map[key]["name"]][ |
| 2419 |
"plugin" |
| 2420 |
]: |
| 2421 |
assert isinstance(plugin(), NotifyBase) |
| 2422 |
|
| 2423 |
# Clear our working variables so they don't obstruct the next test |
| 2424 |
# This simulates an actual call from the CLI. Unfortunately through |
| 2425 |
# testing were occupying the same memory space so our singleton's |
| 2426 |
# have already been populated |
| 2427 |
N_MGR._paths_previously_scanned.clear() |
| 2428 |
N_MGR._custom_module_map.clear() |
| 2429 |
del N_MGR["clihook"] |
| 2430 |
|
| 2431 |
result = runner.invoke( |
| 2432 |
cli.main, |
| 2433 |
[ |
| 2434 |
"--plugin-path", |
| 2435 |
str(notify_hook_b), |
| 2436 |
"-b", |
| 2437 |
"test body", |
| 2438 |
# A custom hook: |
| 2439 |
"clihook://", |
| 2440 |
], |
| 2441 |
) |
| 2442 |
|
| 2443 |
# Absolute path to __init__.py is okay |
| 2444 |
assert result.exit_code == 0 |
| 2445 |
|
| 2446 |
# we can verify that it prepares our message |
| 2447 |
assert result.stdout.strip() == "info: - test body" |
| 2448 |
|
| 2449 |
# Clear our working variables so they don't obstruct the next test |
| 2450 |
# This simulates an actual call from the CLI. Unfortunately through |
| 2451 |
# testing were occupying the same memory space so our singleton's |
| 2452 |
# have already been populated |
| 2453 |
N_MGR._paths_previously_scanned.clear() |
| 2454 |
N_MGR._custom_module_map.clear() |
| 2455 |
del N_MGR["clihook"] |
| 2456 |
|
| 2457 |
result = runner.invoke( |
| 2458 |
cli.main, |
| 2459 |
[ |
| 2460 |
"--plugin-path", |
| 2461 |
dirname(str(notify_hook_b)), |
| 2462 |
"-b", |
| 2463 |
"test body", |
| 2464 |
# A custom hook: |
| 2465 |
"clihook://", |
| 2466 |
], |
| 2467 |
) |
| 2468 |
|
| 2469 |
# Now we succeed to load our module when pointed to it only because |
| 2470 |
# an __init__.py is found on the inside of it |
| 2471 |
assert result.exit_code == 0 |
| 2472 |
|
| 2473 |
# we can verify that it prepares our message |
| 2474 |
assert result.stdout.strip() == "info: - test body" |
| 2475 |
|
| 2476 |
# Test double paths that are the same; this ensures we only |
| 2477 |
# load the plugin once |
| 2478 |
result = runner.invoke( |
| 2479 |
cli.main, |
| 2480 |
[ |
| 2481 |
"--plugin-path", |
| 2482 |
dirname(str(notify_hook_b)), |
| 2483 |
"--plugin-path", |
| 2484 |
str(notify_hook_b), |
| 2485 |
"--details", |
| 2486 |
], |
| 2487 |
) |
| 2488 |
|
| 2489 |
# Now we succeed to load our module when pointed to it only because |
| 2490 |
# an __init__.py is found on the inside of it |
| 2491 |
assert result.exit_code == 0 |
| 2492 |
|
| 2493 |
# Clear our working variables so they don't obstruct the next test |
| 2494 |
# This simulates an actual call from the CLI. Unfortunately through |
| 2495 |
# testing were occupying the same memory space so our singleton's |
| 2496 |
# have already been populated |
| 2497 |
N_MGR._paths_previously_scanned.clear() |
| 2498 |
N_MGR._custom_module_map.clear() |
| 2499 |
del N_MGR["clihook"] |
| 2500 |
|
| 2501 |
# Prepare ourselves a file to work with |
| 2502 |
notify_hook_b = tmpdir.mkdir("complex").join("complex.py") |
| 2503 |
notify_hook_b.write( |
| 2504 |
cleandoc(""" |
| 2505 |
from apprise.decorators import notify |
| 2506 |
|
| 2507 |
# Existing elements cannot be overridden. |
| 2508 |
# in this case json:// |
| 2509 |
@notify(on="json") |
| 2510 |
def mywrapper_01(body, title, notify_type, *args, **kwargs): |
| 2511 |
# Return True (same as None) |
| 2512 |
return True |
| 2513 |
|
| 2514 |
@notify(on="willfail", name="always failing...") |
| 2515 |
def mywrapper_02(body, title, notify_type, *args, **kwargs): |
| 2516 |
# Simply fail |
| 2517 |
return False |
| 2518 |
|
| 2519 |
@notify(on="clihook1", name="the original clihook entry") |
| 2520 |
def mywrapper_03(body, title, notify_type, *args, **kwargs): |
| 2521 |
# Return True |
| 2522 |
return True |
| 2523 |
|
| 2524 |
# A duplicate schema cannot be loaded. |
| 2525 |
@notify(on="clihook1", name="a duplicate of the clihook entry") |
| 2526 |
def mywrapper_04(body, title, notify_type, *args, **kwargs): |
| 2527 |
# Return True |
| 2528 |
return True |
| 2529 |
|
| 2530 |
# A schema URL may also provide defaults for the wrapper. |
| 2531 |
@notify(on="clihook2://localhost") |
| 2532 |
def mywrapper_05(body, title, notify_type, *args, **kwargs): |
| 2533 |
# Return True |
| 2534 |
return True |
| 2535 |
|
| 2536 |
|
| 2537 |
# This can't load because of the defined schema/on definition |
| 2538 |
@notify(on="", name="an invalid schema was specified") |
| 2539 |
def mywrapper_06(body, title, notify_type, *args, **kwargs): |
| 2540 |
return True |
| 2541 |
""") |
| 2542 |
) |
| 2543 |
|
| 2544 |
result = runner.invoke( |
| 2545 |
cli.main, |
| 2546 |
[ |
| 2547 |
"--plugin-path", |
| 2548 |
join(str(tmpdir), "complex"), |
| 2549 |
"-b", |
| 2550 |
"test body", |
| 2551 |
# A custom hook that does not exist |
| 2552 |
"clihook://", |
| 2553 |
], |
| 2554 |
) |
| 2555 |
|
| 2556 |
# Since clihook:// isn't in our complex listing, this will fail |
| 2557 |
assert result.exit_code == 1 |
| 2558 |
|
| 2559 |
# Let's see how things got loaded |
| 2560 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2561 |
# Our path we specified on the CLI... |
| 2562 |
assert join(str(tmpdir), "complex") in N_MGR._paths_previously_scanned |
| 2563 |
|
| 2564 |
# absolute path to detected module is also added |
| 2565 |
assert ( |
| 2566 |
join(str(tmpdir), "complex", "complex.py") |
| 2567 |
in N_MGR._paths_previously_scanned |
| 2568 |
) |
| 2569 |
|
| 2570 |
# We loaded our one module successfuly |
| 2571 |
assert len(N_MGR._custom_module_map) == 1 |
| 2572 |
|
| 2573 |
# We can find our new hook loaded in our SCHEMA_MAP now... |
| 2574 |
assert "willfail" in N_MGR |
| 2575 |
assert "clihook1" in N_MGR |
| 2576 |
assert "clihook2" in N_MGR |
| 2577 |
|
| 2578 |
# Capture our key for reference |
| 2579 |
key = next(iter(N_MGR._custom_module_map.keys())) |
| 2580 |
|
| 2581 |
assert len(N_MGR._custom_module_map[key]["notify"]) == 3 |
| 2582 |
assert "willfail" in N_MGR._custom_module_map[key]["notify"] |
| 2583 |
assert "clihook1" in N_MGR._custom_module_map[key]["notify"] |
| 2584 |
# We only load 1 instance of the clihook2, the second will fail |
| 2585 |
assert "clihook2" in N_MGR._custom_module_map[key]["notify"] |
| 2586 |
# We can never load previously created notifications |
| 2587 |
assert "json" not in N_MGR._custom_module_map[key]["notify"] |
| 2588 |
|
| 2589 |
result = runner.invoke( |
| 2590 |
cli.main, |
| 2591 |
[ |
| 2592 |
"--plugin-path", |
| 2593 |
join(str(tmpdir), "complex"), |
| 2594 |
"-b", |
| 2595 |
"test body", |
| 2596 |
# A custom notification set up for failure |
| 2597 |
"willfail://", |
| 2598 |
], |
| 2599 |
) |
| 2600 |
# Note that the failure of the decorator carries all the way back |
| 2601 |
# to the CLI |
| 2602 |
assert result.exit_code == 1 |
| 2603 |
|
| 2604 |
result = runner.invoke( |
| 2605 |
cli.main, |
| 2606 |
[ |
| 2607 |
"--plugin-path", |
| 2608 |
join(str(tmpdir), "complex"), |
| 2609 |
"-b", |
| 2610 |
"test body", |
| 2611 |
# our clihook that returns true |
| 2612 |
"clihook1://", |
| 2613 |
# our other loaded clihook |
| 2614 |
"clihook2://", |
| 2615 |
], |
| 2616 |
) |
| 2617 |
# Note that the failure of the decorator carries all the way back |
| 2618 |
# to the CLI |
| 2619 |
assert result.exit_code == 0 |
| 2620 |
|
| 2621 |
result = runner.invoke( |
| 2622 |
cli.main, |
| 2623 |
[ |
| 2624 |
"--plugin-path", |
| 2625 |
join(str(tmpdir), "complex"), |
| 2626 |
"--notification-type", |
| 2627 |
"invalid", |
| 2628 |
"-b", |
| 2629 |
"test body", |
| 2630 |
# our clihook that returns true |
| 2631 |
"clihook1://", |
| 2632 |
], |
| 2633 |
) |
| 2634 |
# Bad notification type specified |
| 2635 |
assert result.exit_code == 2 |
| 2636 |
|
| 2637 |
result = runner.invoke( |
| 2638 |
cli.main, |
| 2639 |
[ |
| 2640 |
"--plugin-path", |
| 2641 |
join(str(tmpdir), "complex"), |
| 2642 |
"-b", |
| 2643 |
"-i", |
| 2644 |
"warningtest body", |
| 2645 |
# our clihook that returns true |
| 2646 |
"clihook1://", |
| 2647 |
], |
| 2648 |
) |
| 2649 |
# Bad notification type specified |
| 2650 |
assert result.exit_code == 0 |
| 2651 |
|
| 2652 |
result = runner.invoke( |
| 2653 |
cli.main, |
| 2654 |
[ |
| 2655 |
"--plugin-path", |
| 2656 |
join(str(tmpdir), "complex"), |
| 2657 |
# Print our custom details to the screen |
| 2658 |
"--details", |
| 2659 |
], |
| 2660 |
) |
| 2661 |
assert "willfail" in result.stdout |
| 2662 |
assert "always failing..." in result.stdout |
| 2663 |
|
| 2664 |
assert "clihook1" in result.stdout |
| 2665 |
assert "the original clihook entry" in result.stdout |
| 2666 |
assert "a duplicate of the clihook entry" not in result.stdout |
| 2667 |
|
| 2668 |
assert "clihook2" in result.stdout |
| 2669 |
assert "Custom - clihook2" in result.stdout |
| 2670 |
|
| 2671 |
# Note that the failure of the decorator carries all the way back |
| 2672 |
# to the CLI |
| 2673 |
assert result.exit_code == 0 |
| 2674 |
|
| 2675 |
|
| 2676 |
@mock.patch("platform.system") |
| 2677 |
def test_apprise_cli_windows_env(mock_system): |
| 2678 |
""" |
| 2679 |
CLI: Windows Environment |
| 2680 |
|
| 2681 |
""" |
| 2682 |
# Force a windows environment |
| 2683 |
mock_system.return_value = "Windows" |
| 2684 |
|
| 2685 |
# Reload our module |
| 2686 |
reload(cli) |
| 2687 |
|
| 2688 |
|
| 2689 |
@mock.patch("apprise.cli.NotificationManager") |
| 2690 |
@mock.patch("importlib.metadata.packages_distributions", create=True) |
| 2691 |
@mock.patch("importlib.metadata.version") |
| 2692 |
@mock.patch("apprise.cli.logger") |
| 2693 |
def test_apprise_cli_runtime_env_skip_when_not_debug( |
| 2694 |
mock_logger, mock_ver, mock_dist, mock_mgr |
| 2695 |
): |
| 2696 |
""" |
| 2697 |
CLI: _log_runtime_env() exits immediately when not at DEBUG level. |
| 2698 |
""" |
| 2699 |
# Simulate a logger that is not at DEBUG level |
| 2700 |
mock_logger.isEnabledFor.return_value = False |
| 2701 |
|
| 2702 |
cli._log_runtime_env() |
| 2703 |
|
| 2704 |
# Nothing should be computed or logged |
| 2705 |
mock_logger.debug.assert_not_called() |
| 2706 |
mock_mgr.assert_not_called() |
| 2707 |
mock_dist.assert_not_called() |
| 2708 |
mock_ver.assert_not_called() |
| 2709 |
|
| 2710 |
|
| 2711 |
@mock.patch("apprise.cli.NotificationManager") |
| 2712 |
@mock.patch("importlib.metadata.packages_distributions", create=True) |
| 2713 |
@mock.patch("importlib.metadata.version") |
| 2714 |
@mock.patch("apprise.cli.logger") |
| 2715 |
def test_apprise_cli_runtime_env_logging( |
| 2716 |
mock_logger, mock_ver, mock_dist, mock_mgr |
| 2717 |
): |
| 2718 |
""" |
| 2719 |
CLI: _log_runtime_env() summary |
| 2720 |
""" |
| 2721 |
# Simulate a DEBUG-level logger |
| 2722 |
mock_logger.isEnabledFor.return_value = True |
| 2723 |
|
| 2724 |
# Plugin whose dep should appear in output |
| 2725 |
class EnabledPlugin: |
| 2726 |
enabled = True |
| 2727 |
|
| 2728 |
@staticmethod |
| 2729 |
def runtime_deps(): |
| 2730 |
return ("testpkg",) |
| 2731 |
|
| 2732 |
# Dependencies from disabled plugins must not appear. |
| 2733 |
class DisabledPlugin: |
| 2734 |
enabled = False |
| 2735 |
|
| 2736 |
@staticmethod |
| 2737 |
def runtime_deps(): |
| 2738 |
return ("disabled-dep",) |
| 2739 |
|
| 2740 |
# Plugins without runtime_deps are skipped. |
| 2741 |
class NoRuntimeDepsPlugin: |
| 2742 |
enabled = True |
| 2743 |
|
| 2744 |
mock_mgr.return_value = [ |
| 2745 |
{ |
| 2746 |
"plugin": { |
| 2747 |
EnabledPlugin, |
| 2748 |
DisabledPlugin, |
| 2749 |
NoRuntimeDepsPlugin, |
| 2750 |
}, |
| 2751 |
}, |
| 2752 |
] |
| 2753 |
mock_dist.return_value = {"testpkg": ["test-package"]} |
| 2754 |
mock_ver.return_value = "9.9.9" |
| 2755 |
|
| 2756 |
cli._log_runtime_env() |
| 2757 |
|
| 2758 |
# Collect positional-arg tuples from each debug() call |
| 2759 |
calls = [a for a, _ in mock_logger.debug.call_args_list] |
| 2760 |
|
| 2761 |
# Environment summary lines must be present |
| 2762 |
assert any(a[0] == "Apprise: %s" for a in calls) |
| 2763 |
assert any(a[0] == "Python: %s" for a in calls) |
| 2764 |
assert any(a[0] == "Platform: %s" for a in calls) |
| 2765 |
assert any(a[0] == "Encoding: %s" for a in calls) |
| 2766 |
|
| 2767 |
# Resolved deps emitted as a single inline line |
| 2768 |
assert any( |
| 2769 |
a[0] == "Runtime deps: %s" and a[1] == "test-package=9.9.9" |
| 2770 |
for a in calls |
| 2771 |
) |
| 2772 |
|
| 2773 |
# Disabled plugin dep must not be logged |
| 2774 |
assert not any("disabled-dep" in str(a) for a in calls) |
| 2775 |
|
| 2776 |
|
| 2777 |
@mock.patch("apprise.cli.NotificationManager") |
| 2778 |
@mock.patch("importlib.metadata.packages_distributions", create=True) |
| 2779 |
@mock.patch("importlib.metadata.version") |
| 2780 |
@mock.patch("apprise.cli.logger") |
| 2781 |
def test_apprise_cli_runtime_env_no_runtime_deps( |
| 2782 |
mock_logger, mock_ver, mock_dist, mock_mgr |
| 2783 |
): |
| 2784 |
""" |
| 2785 |
CLI: _log_runtime_env() |
| 2786 |
""" |
| 2787 |
mock_logger.isEnabledFor.return_value = True |
| 2788 |
|
| 2789 |
# Plugin that declares no deps |
| 2790 |
class NoDepsPlugin: |
| 2791 |
enabled = True |
| 2792 |
|
| 2793 |
@staticmethod |
| 2794 |
def runtime_deps(): |
| 2795 |
return () |
| 2796 |
|
| 2797 |
mock_mgr.return_value = [{"plugin": {NoDepsPlugin}}] |
| 2798 |
|
| 2799 |
cli._log_runtime_env() |
| 2800 |
|
| 2801 |
# Environment header was still logged |
| 2802 |
calls = [a for a, _ in mock_logger.debug.call_args_list] |
| 2803 |
assert any(a[0] == "Apprise: %s" for a in calls) |
| 2804 |
|
| 2805 |
# Package-metadata scan must not have been attempted |
| 2806 |
mock_dist.assert_not_called() |
| 2807 |
mock_ver.assert_not_called() |
| 2808 |
|
| 2809 |
# No " pkg: ver" lines |
| 2810 |
assert not any(a[0] == "Runtime deps: %s" for a in calls) |
| 2811 |
|
| 2812 |
|
| 2813 |
@mock.patch("apprise.cli.NotificationManager") |
| 2814 |
@mock.patch("importlib.metadata.packages_distributions", create=True) |
| 2815 |
@mock.patch("importlib.metadata.version") |
| 2816 |
@mock.patch("apprise.cli.logger") |
| 2817 |
def test_apprise_cli_runtime_env_dist_map_exception( |
| 2818 |
mock_logger, mock_ver, mock_dist, mock_mgr |
| 2819 |
): |
| 2820 |
""" |
| 2821 |
CLI: _log_runtime_env() handles packages_distributions() failures. |
| 2822 |
""" |
| 2823 |
mock_logger.isEnabledFor.return_value = True |
| 2824 |
|
| 2825 |
class DepPlugin: |
| 2826 |
enabled = True |
| 2827 |
|
| 2828 |
@staticmethod |
| 2829 |
def runtime_deps(): |
| 2830 |
return ("somepkg",) |
| 2831 |
|
| 2832 |
mock_mgr.return_value = [{"plugin": {DepPlugin}}] |
| 2833 |
mock_dist.side_effect = Exception("metadata unavailable") |
| 2834 |
|
| 2835 |
# Must not raise |
| 2836 |
cli._log_runtime_env() |
| 2837 |
|
| 2838 |
# pkg_version should never be reached |
| 2839 |
mock_ver.assert_not_called() |
| 2840 |
|
| 2841 |
# No " pkg: ver" lines |
| 2842 |
calls = [a for a, _ in mock_logger.debug.call_args_list] |
| 2843 |
assert not any(a[0] == "Runtime deps: %s" for a in calls) |
| 2844 |
|
| 2845 |
|
| 2846 |
@mock.patch("apprise.cli.NotificationManager") |
| 2847 |
@mock.patch("importlib.metadata.packages_distributions", create=True) |
| 2848 |
@mock.patch("importlib.metadata.version") |
| 2849 |
@mock.patch("apprise.cli.logger") |
| 2850 |
def test_apprise_cli_runtime_env_lookup_errors( |
| 2851 |
mock_logger, mock_ver, mock_dist, mock_mgr |
| 2852 |
): |
| 2853 |
""" |
| 2854 |
CLI: _log_runtime_env() silently skips packages exception raised. |
| 2855 |
""" |
| 2856 |
mock_logger.isEnabledFor.return_value = True |
| 2857 |
|
| 2858 |
# Plugin whose runtime_deps() raises |
| 2859 |
class BrokenPlugin: |
| 2860 |
enabled = True |
| 2861 |
|
| 2862 |
@staticmethod |
| 2863 |
def runtime_deps(): |
| 2864 |
raise RuntimeError("plugin error") |
| 2865 |
|
| 2866 |
# Plugin whose dep import name is not in the dist map |
| 2867 |
class UnmappedPlugin: |
| 2868 |
enabled = True |
| 2869 |
|
| 2870 |
@staticmethod |
| 2871 |
def runtime_deps(): |
| 2872 |
return ("notmapped",) |
| 2873 |
|
| 2874 |
# Plugin whose dep maps to a dist but the version lookup fails |
| 2875 |
class VersionlessPlugin: |
| 2876 |
enabled = True |
| 2877 |
|
| 2878 |
@staticmethod |
| 2879 |
def runtime_deps(): |
| 2880 |
return ("verpkg",) |
| 2881 |
|
| 2882 |
mock_mgr.return_value = [ |
| 2883 |
{ |
| 2884 |
"plugin": { |
| 2885 |
BrokenPlugin, |
| 2886 |
UnmappedPlugin, |
| 2887 |
VersionlessPlugin, |
| 2888 |
}, |
| 2889 |
}, |
| 2890 |
] |
| 2891 |
|
| 2892 |
# "notmapped" absent from map; "verpkg" present but ver raises |
| 2893 |
mock_dist.return_value = {"verpkg": ["ver-package"]} |
| 2894 |
mock_ver.side_effect = PackageNotFoundError("ver-package") |
| 2895 |
|
| 2896 |
# Must not raise |
| 2897 |
cli._log_runtime_env() |
| 2898 |
|
| 2899 |
# None of the error paths should produce a " pkg: ver" line |
| 2900 |
calls = [a for a, _ in mock_logger.debug.call_args_list] |
| 2901 |
assert not any(a[0] == "Runtime deps: %s" for a in calls) |
| 2902 |
|
| 2903 |
|
| 2904 |
@pytest.mark.skipif( |
| 2905 |
sys.version_info < (3, 11), |
| 2906 |
reason="packages_distributions requires Python 3.11+", |
| 2907 |
) |
| 2908 |
@mock.patch("apprise.cli.NotificationManager") |
| 2909 |
@mock.patch("apprise.cli.logger") |
| 2910 |
def test_apprise_cli_runtime_env_no_packages_distributions( |
| 2911 |
mock_logger, mock_mgr |
| 2912 |
): |
| 2913 |
""" |
| 2914 |
CLI: _log_runtime_env() gracefully skips dep listing when |
| 2915 |
packages_distributions() is missing (ImportError path). |
| 2916 |
Simulates the Python < 3.11 scenario on Python 3.11+ by temporarily |
| 2917 |
removing packages_distributions from importlib.metadata. |
| 2918 |
""" |
| 2919 |
import importlib.metadata as _meta |
| 2920 |
|
| 2921 |
mock_logger.isEnabledFor.return_value = True |
| 2922 |
|
| 2923 |
class DepPlugin: |
| 2924 |
enabled = True |
| 2925 |
|
| 2926 |
@staticmethod |
| 2927 |
def runtime_deps(): |
| 2928 |
return ("somepkg",) |
| 2929 |
|
| 2930 |
mock_mgr.return_value = [{"plugin": {DepPlugin}}] |
| 2931 |
|
| 2932 |
# Temporarily hide packages_distributions to trigger the ImportError |
| 2933 |
_pd = _meta.packages_distributions |
| 2934 |
del _meta.packages_distributions |
| 2935 |
try: |
| 2936 |
cli._log_runtime_env() |
| 2937 |
finally: |
| 2938 |
_meta.packages_distributions = _pd |
| 2939 |
|
| 2940 |
# Env summary was still logged despite the missing function |
| 2941 |
calls = [a for a, _ in mock_logger.debug.call_args_list] |
| 2942 |
assert any(a[0] == "Apprise: %s" for a in calls) |
| 2943 |
|
| 2944 |
# No dependency list is logged without packages_distributions. |
| 2945 |
assert not any(a[0] == "Runtime deps: %s" for a in calls) |
| 2946 |
|
| 2947 |
|
| 2948 |
# Remove this test when Python 3.9 support is dropped. |
| 2949 |
@pytest.mark.skipif( |
| 2950 |
sys.version_info >= (3, 11), |
| 2951 |
reason="packages_distributions exists on Python 3.11+", |
| 2952 |
) |
| 2953 |
@mock.patch("apprise.cli.NotificationManager") |
| 2954 |
@mock.patch("apprise.cli.logger") |
| 2955 |
def test_apprise_cli_runtime_env_py39_no_packages_distributions( |
| 2956 |
mock_logger, mock_mgr |
| 2957 |
): |
| 2958 |
""" |
| 2959 |
CLI: _log_runtime_env() gracefully skips dep listing on Python < 3.11 |
| 2960 |
where packages_distributions() is genuinely absent. |
| 2961 |
""" |
| 2962 |
mock_logger.isEnabledFor.return_value = True |
| 2963 |
|
| 2964 |
class DepPlugin: |
| 2965 |
enabled = True |
| 2966 |
|
| 2967 |
@staticmethod |
| 2968 |
def runtime_deps(): |
| 2969 |
return ("somepkg",) |
| 2970 |
|
| 2971 |
mock_mgr.return_value = [{"plugin": {DepPlugin}}] |
| 2972 |
|
| 2973 |
# This Python version lacks packages_distributions natively. |
| 2974 |
cli._log_runtime_env() |
| 2975 |
|
| 2976 |
# Env summary was still logged despite the missing function |
| 2977 |
calls = [a for a, _ in mock_logger.debug.call_args_list] |
| 2978 |
assert any(a[0] == "Apprise: %s" for a in calls) |
| 2979 |
|
| 2980 |
# No dependency list is logged without packages_distributions. |
| 2981 |
assert not any(a[0] == "Runtime deps: %s" for a in calls) |
| 2982 |
|
| 2983 |
|
| 2984 |
def test_apprise_cli_limit_option_in_help(): |
| 2985 |
""" |
| 2986 |
CLI: --limit (-L) appears in --help output |
| 2987 |
""" |
| 2988 |
runner = CliRunner() |
| 2989 |
result = runner.invoke(cli.main, ["--help"]) |
| 2990 |
assert result.exit_code == 0 |
| 2991 |
assert "--limit" in result.output |
| 2992 |
assert "-L" in result.output |
| 2993 |
|
| 2994 |
|
| 2995 |
@mock.patch("requests.request") |
| 2996 |
def test_apprise_cli_notify_runtime_stat_log(mock_request): |
| 2997 |
"""CLI: log runtime, counts, and overall status after notification. |
| 2998 |
|
| 2999 |
Patch only ``logger.debug`` because setup also uses the logger's level. |
| 3000 |
""" |
| 3001 |
response = mock.Mock() |
| 3002 |
response.status_code = requests.codes.ok |
| 3003 |
response.content = b"" |
| 3004 |
mock_request.return_value = response |
| 3005 |
|
| 3006 |
runner = CliRunner() |
| 3007 |
with mock.patch.object(cli.logger, "debug") as mock_debug: |
| 3008 |
result = runner.invoke( |
| 3009 |
cli.main, |
| 3010 |
["-t", "title", "-b", "body", "json://good"], |
| 3011 |
) |
| 3012 |
assert result.exit_code == 0 |
| 3013 |
|
| 3014 |
calls = [a for a, _ in mock_debug.call_args_list] |
| 3015 |
assert any( |
| 3016 |
a[0] |
| 3017 |
== ( |
| 3018 |
"Finished in %.2fs. %d service(s) tried (%s): %d sent / " |
| 3019 |
"%d failed / %d timed out." |
| 3020 |
) |
| 3021 |
# The elapsed-seconds value is the first substitution. |
| 3022 |
and a[2:] == (1, "SUCCESS", 1, 0, 0) |
| 3023 |
for a in calls |
| 3024 |
) |
| 3025 |
|
| 3026 |
|
| 3027 |
@mock.patch("apprise.cli._force_exit") |
| 3028 |
@mock.patch("requests.request") |
| 3029 |
def test_apprise_cli_limit_option_times_out_service( |
| 3030 |
mock_request, mock_force_exit |
| 3031 |
): |
| 3032 |
"""CLI: ``--limit`` reports TIMEOUT without requiring a hard exit. |
| 3033 |
|
| 3034 |
The long service delay avoids timing races. Mock ``_force_exit`` because |
| 3035 |
its real ``os._exit`` would stop the in-process test runner. |
| 3036 |
""" |
| 3037 |
import time |
| 3038 |
|
| 3039 |
def _slow_failure(*args, **kwargs): |
| 3040 |
"""Return a delayed HTTP failure to force the CLI timeout path.""" |
| 3041 |
time.sleep(2.0) |
| 3042 |
response = mock.Mock() |
| 3043 |
response.status_code = requests.codes.internal_server_error |
| 3044 |
response.content = b"" |
| 3045 |
response.text = "" |
| 3046 |
return response |
| 3047 |
|
| 3048 |
mock_request.side_effect = _slow_failure |
| 3049 |
|
| 3050 |
runner = CliRunner() |
| 3051 |
result = runner.invoke( |
| 3052 |
cli.main, |
| 3053 |
[ |
| 3054 |
"-t", |
| 3055 |
"title", |
| 3056 |
"-b", |
| 3057 |
"body", |
| 3058 |
"--limit", |
| 3059 |
"0.05", |
| 3060 |
"json://good?retry=3", |
| 3061 |
], |
| 3062 |
) |
| 3063 |
|
| 3064 |
# A lone timeout produces the TIMEOUT exit code, not generic FAILURE. |
| 3065 |
# Its diagnostic uses logging and is tested separately. |
| 3066 |
assert result.exit_code == AppriseResultStatus.TIMEOUT |
| 3067 |
|
| 3068 |
# The service finishes during the grace period, avoiding a hard exit. |
| 3069 |
mock_force_exit.assert_not_called() |
| 3070 |
|
| 3071 |
|
| 3072 |
@mock.patch("apprise.cli._force_exit") |
| 3073 |
@mock.patch("requests.request") |
| 3074 |
def test_apprise_cli_limit_hard_exit(mock_request, mock_force_exit): |
| 3075 |
"""Force exit when a call outlives the timeout grace period. |
| 3076 |
|
| 3077 |
A two-second delay avoids timing races. ``_force_exit()`` is mocked so the |
| 3078 |
test process stays alive. |
| 3079 |
""" |
| 3080 |
import time |
| 3081 |
|
| 3082 |
def _slow_failure(*args, **kwargs): |
| 3083 |
"""Return a delayed HTTP failure to force the CLI timeout path.""" |
| 3084 |
time.sleep(2.0) |
| 3085 |
response = mock.Mock() |
| 3086 |
response.status_code = requests.codes.internal_server_error |
| 3087 |
response.content = b"" |
| 3088 |
response.text = "" |
| 3089 |
return response |
| 3090 |
|
| 3091 |
mock_request.side_effect = _slow_failure |
| 3092 |
|
| 3093 |
with mock.patch("apprise.cli.CLI_TIMEOUT_EXIT_GRACE_SECONDS", 0.05): |
| 3094 |
runner = CliRunner() |
| 3095 |
result = runner.invoke( |
| 3096 |
cli.main, |
| 3097 |
[ |
| 3098 |
"-t", |
| 3099 |
"title", |
| 3100 |
"-b", |
| 3101 |
"body", |
| 3102 |
"--limit", |
| 3103 |
"0.05", |
| 3104 |
"json://good?retry=3", |
| 3105 |
], |
| 3106 |
) |
| 3107 |
|
| 3108 |
assert result.exit_code == AppriseResultStatus.TIMEOUT |
| 3109 |
mock_force_exit.assert_called_once() |
| 3110 |
assert mock_force_exit.call_args.args[1] == AppriseResultStatus.TIMEOUT |
| 3111 |
|
| 3112 |
|
| 3113 |
def test_wait_for_abandoned_calls_polls_full_grace_period(): |
| 3114 |
"""Poll in short intervals until the full grace period expires.""" |
| 3115 |
with ( |
| 3116 |
mock.patch("apprise.cli.time.sleep") as mock_sleep, |
| 3117 |
mock.patch( |
| 3118 |
"apprise.cli.any_abandoned_calls_still_running", |
| 3119 |
return_value=True, |
| 3120 |
) as mock_still_running, |
| 3121 |
): |
| 3122 |
result = cli._wait_for_abandoned_calls( |
| 3123 |
cli.CLI_TIMEOUT_EXIT_GRACE_SECONDS |
| 3124 |
) |
| 3125 |
|
| 3126 |
assert result is False |
| 3127 |
assert mock_still_running.called |
| 3128 |
total_slept = sum(call.args[0] for call in mock_sleep.call_args_list) |
| 3129 |
assert total_slept == pytest.approx(cli.CLI_TIMEOUT_EXIT_GRACE_SECONDS) |
| 3130 |
for call in mock_sleep.call_args_list: |
| 3131 |
assert call.args[0] <= cli.CLI_TIMEOUT_EXIT_POLL_INTERVAL |
| 3132 |
|
| 3133 |
|
| 3134 |
def test_wait_for_abandoned_calls_logs_masked_services(): |
| 3135 |
"""Log masked descriptions for unfinished calls at DEBUG once.""" |
| 3136 |
with ( |
| 3137 |
mock.patch("apprise.cli.time.sleep"), |
| 3138 |
mock.patch( |
| 3139 |
"apprise.cli.any_abandoned_calls_still_running", |
| 3140 |
return_value=False, |
| 3141 |
), |
| 3142 |
mock.patch( |
| 3143 |
"apprise.cli.abandoned_call_descriptions", |
| 3144 |
return_value=["dummy (dummy://masked@host)"], |
| 3145 |
), |
| 3146 |
mock.patch("apprise.cli.logger.debug") as mock_debug, |
| 3147 |
): |
| 3148 |
cli._wait_for_abandoned_calls(cli.CLI_TIMEOUT_EXIT_GRACE_SECONDS) |
| 3149 |
|
| 3150 |
first_call_message = mock_debug.call_args_list[0].args |
| 3151 |
assert "dummy (dummy://masked@host)" in first_call_message |
| 3152 |
|
| 3153 |
|
| 3154 |
def test_wait_for_abandoned_calls_exits_early_when_calls_finish(): |
| 3155 |
"""Stop polling as soon as every abandoned call finishes.""" |
| 3156 |
# Two busy checks produce two short sleeps before completion. |
| 3157 |
with ( |
| 3158 |
mock.patch("apprise.cli.time.sleep") as mock_sleep, |
| 3159 |
mock.patch( |
| 3160 |
"apprise.cli.any_abandoned_calls_still_running", |
| 3161 |
side_effect=[True, True, False], |
| 3162 |
) as mock_still_running, |
| 3163 |
): |
| 3164 |
result = cli._wait_for_abandoned_calls( |
| 3165 |
cli.CLI_TIMEOUT_EXIT_GRACE_SECONDS |
| 3166 |
) |
| 3167 |
|
| 3168 |
assert result is True |
| 3169 |
assert mock_still_running.call_count == 3 |
| 3170 |
assert mock_sleep.call_count == 2 |
| 3171 |
for call in mock_sleep.call_args_list: |
| 3172 |
assert call.args[0] == cli.CLI_TIMEOUT_EXIT_POLL_INTERVAL |
| 3173 |
|
| 3174 |
|
| 3175 |
def test_wait_for_abandoned_calls_finishes_right_as_grace_period_ends(): |
| 3176 |
"""A final post-loop check can catch work that just finished.""" |
| 3177 |
with ( |
| 3178 |
mock.patch("apprise.cli.time.sleep"), |
| 3179 |
mock.patch( |
| 3180 |
"apprise.cli.any_abandoned_calls_still_running", |
| 3181 |
# Four in-loop polls still report running. The final check |
| 3182 |
# sees that the abandoned work has just finished. |
| 3183 |
side_effect=[True, True, True, True, False], |
| 3184 |
) as mock_still_running, |
| 3185 |
): |
| 3186 |
result = cli._wait_for_abandoned_calls(1.0) |
| 3187 |
|
| 3188 |
assert result is True |
| 3189 |
assert mock_still_running.call_count == 5 |
| 3190 |
|
| 3191 |
|
| 3192 |
def test_force_exit_sequence(): |
| 3193 |
"""Flush stores and output before forcing the process to exit. |
| 3194 |
|
| 3195 |
All external effects are mocked so the sequence is tested safely. |
| 3196 |
""" |
| 3197 |
a = Apprise() |
| 3198 |
services = [mock.Mock(spec=NotifyBase) for _ in range(3)] |
| 3199 |
for service in services: |
| 3200 |
a.add(service) |
| 3201 |
|
| 3202 |
with ( |
| 3203 |
mock.patch("apprise.cli.logging.shutdown") as mock_shutdown, |
| 3204 |
mock.patch("apprise.cli.sys.stdout.flush") as mock_stdout_flush, |
| 3205 |
mock.patch("apprise.cli.sys.stderr.flush") as mock_stderr_flush, |
| 3206 |
mock.patch("apprise.cli.os._exit") as mock_os_exit, |
| 3207 |
): |
| 3208 |
cli._force_exit(a, AppriseResultStatus.TIMEOUT) |
| 3209 |
|
| 3210 |
for service in services: |
| 3211 |
service.flush_store.assert_called_once() |
| 3212 |
mock_shutdown.assert_called_once() |
| 3213 |
mock_stdout_flush.assert_called_once() |
| 3214 |
mock_stderr_flush.assert_called_once() |
| 3215 |
mock_os_exit.assert_called_once_with(AppriseResultStatus.TIMEOUT) |
| 3216 |
|
| 3217 |
|
| 3218 |
def test_force_exit_flush_failure_does_not_skip_others(): |
| 3219 |
"""A failed store flush does not skip other stores or the hard exit. |
| 3220 |
|
| 3221 |
The broken service comes first to exercise the remaining flushes. |
| 3222 |
""" |
| 3223 |
a = Apprise() |
| 3224 |
broken_service = mock.Mock(spec=NotifyBase) |
| 3225 |
broken_service.flush_store.side_effect = RuntimeError("disk full") |
| 3226 |
ok_service = mock.Mock(spec=NotifyBase) |
| 3227 |
a.add(broken_service) |
| 3228 |
a.add(ok_service) |
| 3229 |
|
| 3230 |
with ( |
| 3231 |
mock.patch("apprise.cli.logging.shutdown") as mock_shutdown, |
| 3232 |
mock.patch("apprise.cli.os._exit") as mock_os_exit, |
| 3233 |
): |
| 3234 |
cli._force_exit(a, AppriseResultStatus.TIMEOUT) |
| 3235 |
|
| 3236 |
# The service after the broken one in iteration order still got |
| 3237 |
# its own flush attempt. |
| 3238 |
ok_service.flush_store.assert_called_once() |
| 3239 |
mock_os_exit.assert_called_once_with(AppriseResultStatus.TIMEOUT) |
| 3240 |
# logging.shutdown() is unrelated to the failing store and should |
| 3241 |
# still run normally afterward. |
| 3242 |
mock_shutdown.assert_called_once() |
| 3243 |
|
| 3244 |
|
| 3245 |
def test_force_exit_reaches_exit_despite_logging_failure(): |
| 3246 |
"""A logging shutdown failure does not prevent output flushes or exit.""" |
| 3247 |
a = Apprise() |
| 3248 |
service = mock.Mock(spec=NotifyBase) |
| 3249 |
a.add(service) |
| 3250 |
|
| 3251 |
with ( |
| 3252 |
mock.patch( |
| 3253 |
"apprise.cli.logging.shutdown", |
| 3254 |
side_effect=RuntimeError("logging is broken"), |
| 3255 |
), |
| 3256 |
mock.patch("apprise.cli.sys.stdout.flush") as mock_stdout_flush, |
| 3257 |
mock.patch("apprise.cli.sys.stderr.flush") as mock_stderr_flush, |
| 3258 |
mock.patch("apprise.cli.os._exit") as mock_os_exit, |
| 3259 |
): |
| 3260 |
cli._force_exit(a, AppriseResultStatus.TIMEOUT) |
| 3261 |
|
| 3262 |
service.flush_store.assert_called_once() |
| 3263 |
mock_stdout_flush.assert_called_once() |
| 3264 |
mock_stderr_flush.assert_called_once() |
| 3265 |
mock_os_exit.assert_called_once_with(AppriseResultStatus.TIMEOUT) |
| 3266 |
|
| 3267 |
|
| 3268 |
def test_apprise_cli_limit_option_negative_value_errors(): |
| 3269 |
""" |
| 3270 |
CLI: --limit (-L) rejects a negative value the same way notify() |
| 3271 |
and AppriseAsset(service_timeout=...) do. |
| 3272 |
""" |
| 3273 |
runner = CliRunner() |
| 3274 |
result = runner.invoke( |
| 3275 |
cli.main, |
| 3276 |
["-t", "title", "-b", "body", "--limit", "-5", "json://good"], |
| 3277 |
) |
| 3278 |
assert result.exit_code != 0 |
| 3279 |
|
| 3280 |
|
| 3281 |
def test_apprise_cli_service_limit_option_in_help(): |
| 3282 |
""" |
| 3283 |
CLI: --service-limit (-SL) appears in --help output |
| 3284 |
""" |
| 3285 |
runner = CliRunner() |
| 3286 |
result = runner.invoke(cli.main, ["--help"]) |
| 3287 |
assert result.exit_code == 0 |
| 3288 |
assert "--service-limit" in result.output |
| 3289 |
assert "-SL" in result.output |
| 3290 |
|
| 3291 |
|
| 3292 |
def test_apprise_cli_service_limit_option_negative_value_errors(): |
| 3293 |
""" |
| 3294 |
CLI: --service-limit (-SL) rejects a negative value the same way |
| 3295 |
AppriseAsset(service_timeout=...) does. |
| 3296 |
""" |
| 3297 |
runner = CliRunner() |
| 3298 |
result = runner.invoke( |
| 3299 |
cli.main, |
| 3300 |
[ |
| 3301 |
"-t", |
| 3302 |
"title", |
| 3303 |
"-b", |
| 3304 |
"body", |
| 3305 |
"--service-limit", |
| 3306 |
"-5", |
| 3307 |
"json://good", |
| 3308 |
], |
| 3309 |
) |
| 3310 |
assert result.exit_code != 0 |
| 3311 |
|
| 3312 |
|
| 3313 |
@mock.patch("apprise.cli.AppriseAsset") |
| 3314 |
@mock.patch("requests.request") |
| 3315 |
def test_apprise_cli_service_limit_option_passed_to_asset( |
| 3316 |
mock_request, mock_asset_cls |
| 3317 |
): |
| 3318 |
""" |
| 3319 |
CLI: --service-limit (-SL), when specified, is passed into |
| 3320 |
AppriseAsset(service_timeout=...) independently of --limit. |
| 3321 |
""" |
| 3322 |
mock_asset_cls.side_effect = AppriseAsset |
| 3323 |
mock_request.return_value = requests.Request() |
| 3324 |
mock_request.return_value.status_code = requests.codes.ok |
| 3325 |
|
| 3326 |
runner = CliRunner() |
| 3327 |
result = runner.invoke( |
| 3328 |
cli.main, |
| 3329 |
[ |
| 3330 |
"-t", |
| 3331 |
"title", |
| 3332 |
"-b", |
| 3333 |
"body", |
| 3334 |
"--limit", |
| 3335 |
"30", |
| 3336 |
"--service-limit", |
| 3337 |
"5", |
| 3338 |
"json://localhost", |
| 3339 |
], |
| 3340 |
) |
| 3341 |
assert result.exit_code == 0 |
| 3342 |
|
| 3343 |
# --service-limit is independently set on the AppriseAsset, no |
| 3344 |
# matter what --limit was also set to. |
| 3345 |
assert mock_asset_cls.call_args.kwargs.get("service_timeout") == 5.0 |
| 3346 |
|
| 3347 |
|
| 3348 |
@mock.patch("apprise.cli.AppriseAsset") |
| 3349 |
@mock.patch("requests.request") |
| 3350 |
def test_apprise_cli_service_limit_option_omitted( |
| 3351 |
mock_request, mock_asset_cls |
| 3352 |
): |
| 3353 |
""" |
| 3354 |
CLI: when --service-limit is not specified, AppriseAsset() receives |
| 3355 |
service_timeout=None -- its own built-in default remains in effect, |
| 3356 |
even if --limit was specified. |
| 3357 |
""" |
| 3358 |
mock_asset_cls.side_effect = AppriseAsset |
| 3359 |
mock_request.return_value = requests.Request() |
| 3360 |
mock_request.return_value.status_code = requests.codes.ok |
| 3361 |
|
| 3362 |
runner = CliRunner() |
| 3363 |
result = runner.invoke( |
| 3364 |
cli.main, |
| 3365 |
[ |
| 3366 |
"-t", |
| 3367 |
"title", |
| 3368 |
"-b", |
| 3369 |
"body", |
| 3370 |
"--limit", |
| 3371 |
"30", |
| 3372 |
"json://localhost", |
| 3373 |
], |
| 3374 |
) |
| 3375 |
assert result.exit_code == 0 |
| 3376 |
assert mock_asset_cls.call_args.kwargs.get("service_timeout") is None |