| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
# Disable logging for a cleaner testing output |
| 29 |
import logging |
| 30 |
import sys |
| 31 |
import time |
| 32 |
from unittest import mock |
| 33 |
|
| 34 |
import pytest |
| 35 |
|
| 36 |
from apprise import ( |
| 37 |
Apprise, |
| 38 |
AppriseAsset, |
| 39 |
AppriseConfig, |
| 40 |
ConfigFormat, |
| 41 |
ConfigurationManager, |
| 42 |
ContentIncludeMode, |
| 43 |
NotificationManager, |
| 44 |
NotifyFormat, |
| 45 |
) |
| 46 |
from apprise.config import ConfigBase |
| 47 |
from apprise.config.file import ConfigFile |
| 48 |
from apprise.logger import LogCapture |
| 49 |
from apprise.plugins import NotifyBase |
| 50 |
|
| 51 |
logging.disable(logging.CRITICAL) |
| 52 |
|
| 53 |
# Grant access to our Notification Manager Singleton |
| 54 |
N_MGR = NotificationManager() |
| 55 |
|
| 56 |
# Grant access to our Configuration Manager Singleton |
| 57 |
C_MGR = ConfigurationManager() |
| 58 |
|
| 59 |
|
| 60 |
def test_apprise_config(tmpdir): |
| 61 |
""" |
| 62 |
API: AppriseConfig basic testing |
| 63 |
|
| 64 |
""" |
| 65 |
|
| 66 |
# Create ourselves a config object |
| 67 |
ac = AppriseConfig() |
| 68 |
|
| 69 |
# There are no services loaded |
| 70 |
assert len(ac) == 0 |
| 71 |
|
| 72 |
# Object can be directly checked as a boolean; response is False |
| 73 |
# when there are no entries loaded |
| 74 |
assert not ac |
| 75 |
|
| 76 |
# lets try anyway |
| 77 |
assert len(ac.services()) == 0 |
| 78 |
|
| 79 |
t = tmpdir.mkdir("simple-formatting").join("apprise") |
| 80 |
t.write(""" |
| 81 |
# A comment line over top of a URL |
| 82 |
mailto://usera:[email protected] |
| 83 |
|
| 84 |
# A line with mulitiple tag assignments to it |
| 85 |
taga,tagb=gnome:// |
| 86 |
|
| 87 |
# Event if there is accidental leading spaces, this configuation |
| 88 |
# is accepting of htat and will not exclude them |
| 89 |
tagc=kde:// |
| 90 |
|
| 91 |
# A very poorly structured url |
| 92 |
sns://:@/ |
| 93 |
|
| 94 |
# Just 1 token provided causes exception |
| 95 |
sns://T1JJ3T3L2/ |
| 96 |
|
| 97 |
# XML |
| 98 |
xml://localhost/?+HeaderEntry=Test&:IgnoredEntry=Ignored |
| 99 |
""") |
| 100 |
|
| 101 |
# Create ourselves a config object |
| 102 |
ac = AppriseConfig(paths=str(t)) |
| 103 |
|
| 104 |
# One configuration file should have been found |
| 105 |
assert len(ac) == 1 |
| 106 |
|
| 107 |
# Object can be directly checked as a boolean; response is True |
| 108 |
# when there is at least one entry |
| 109 |
assert ac |
| 110 |
|
| 111 |
# We should be able to read our 4 services from that |
| 112 |
assert len(ac.services()) == 4 |
| 113 |
|
| 114 |
# Get our URL back |
| 115 |
assert isinstance(ac[0].url(), str) |
| 116 |
|
| 117 |
# Test cases where our URL is invalid |
| 118 |
t = tmpdir.mkdir("strange-lines").join("apprise") |
| 119 |
t.write(""" |
| 120 |
# basicly this consists of defined tags and no url |
| 121 |
tag= |
| 122 |
""") |
| 123 |
|
| 124 |
# Create ourselves a config object |
| 125 |
ac = AppriseConfig(paths=str(t), asset=AppriseAsset()) |
| 126 |
|
| 127 |
# One configuration file should have been found |
| 128 |
assert len(ac) == 1 |
| 129 |
|
| 130 |
# No urls were set |
| 131 |
assert len(ac.services()) == 0 |
| 132 |
|
| 133 |
# Create a ConfigBase object |
| 134 |
cb = ConfigBase() |
| 135 |
|
| 136 |
# Test adding of all entries |
| 137 |
assert ac.add(configs=cb, asset=AppriseAsset(), tag="test") is True |
| 138 |
|
| 139 |
# Test adding of all entries |
| 140 |
assert ( |
| 141 |
ac.add( |
| 142 |
configs=[ |
| 143 |
"file://?", |
| 144 |
], |
| 145 |
asset=AppriseAsset(), |
| 146 |
tag="test", |
| 147 |
) |
| 148 |
is False |
| 149 |
) |
| 150 |
|
| 151 |
# Test the adding of garbage |
| 152 |
assert ac.add(configs=object()) is False |
| 153 |
|
| 154 |
# Try again but enforce our format |
| 155 |
ac = AppriseConfig(paths=f"file://{t!s}?format=text") |
| 156 |
|
| 157 |
# One configuration file should have been found |
| 158 |
assert len(ac) == 1 |
| 159 |
|
| 160 |
# No urls were set |
| 161 |
assert len(ac.services()) == 0 |
| 162 |
|
| 163 |
# |
| 164 |
# Test Internatialization and the handling of unicode characters |
| 165 |
# |
| 166 |
istr = """ |
| 167 |
# Iñtërnâtiônàlization Testing |
| 168 |
windows://""" |
| 169 |
|
| 170 |
# Write our content to our file |
| 171 |
t = tmpdir.mkdir("internationalization").join("apprise") |
| 172 |
with open(str(t), "wb") as f: |
| 173 |
f.write(istr.encode("latin-1")) |
| 174 |
|
| 175 |
# Create ourselves a config object |
| 176 |
ac = AppriseConfig(paths=str(t)) |
| 177 |
|
| 178 |
# One configuration file should have been found |
| 179 |
assert len(ac) == 1 |
| 180 |
|
| 181 |
# This will fail because our default encoding is utf-8; however the file |
| 182 |
# we opened was not; it was latin-1 and could not be parsed. |
| 183 |
assert len(ac.services()) == 0 |
| 184 |
|
| 185 |
# Test iterator |
| 186 |
count = 0 |
| 187 |
for _entry in ac: |
| 188 |
count += 1 |
| 189 |
assert len(ac) == count |
| 190 |
|
| 191 |
# We can fix this though; set our encoding to latin-1 |
| 192 |
ac = AppriseConfig(paths=f"file://{t!s}?encoding=latin-1") |
| 193 |
|
| 194 |
# One configuration file should have been found |
| 195 |
assert len(ac) == 1 |
| 196 |
|
| 197 |
# Our URL should be found |
| 198 |
assert len(ac.services()) == 1 |
| 199 |
|
| 200 |
# Get our URL back |
| 201 |
assert isinstance(ac[0].url(), str) |
| 202 |
|
| 203 |
# pop an entry from our list |
| 204 |
assert isinstance(ac.pop(0), ConfigBase) |
| 205 |
|
| 206 |
# Determine we have no more configuration entries loaded |
| 207 |
assert len(ac) == 0 |
| 208 |
|
| 209 |
# |
| 210 |
# Test buffer handling (and overflow) |
| 211 |
t = tmpdir.mkdir("buffer-handling").join("apprise") |
| 212 |
buf = "gnome://" |
| 213 |
t.write(buf) |
| 214 |
|
| 215 |
# Reset our config object |
| 216 |
ac.clear() |
| 217 |
|
| 218 |
# Create ourselves a config object |
| 219 |
ac = AppriseConfig(paths=str(t)) |
| 220 |
|
| 221 |
# update our length to be the size of our actual file |
| 222 |
ac[0].max_buffer_size = len(buf) |
| 223 |
|
| 224 |
# One configuration file should have been found |
| 225 |
assert len(ac) == 1 |
| 226 |
|
| 227 |
assert len(ac.services()) == 1 |
| 228 |
|
| 229 |
# update our buffer size to be slightly smaller then what we allow |
| 230 |
ac[0].max_buffer_size = len(buf) - 1 |
| 231 |
|
| 232 |
# Content is automatically cached; so even though we adjusted the buffer |
| 233 |
# above, our results have been cached so we get a 1 response. |
| 234 |
assert len(ac.services()) == 1 |
| 235 |
|
| 236 |
|
| 237 |
def test_apprise_multi_config_entries(tmpdir): |
| 238 |
""" |
| 239 |
API: AppriseConfig basic multi-adding functionality |
| 240 |
|
| 241 |
""" |
| 242 |
# temporary file to work with |
| 243 |
t = tmpdir.mkdir("apprise-multi-add").join("apprise") |
| 244 |
buf = """ |
| 245 |
good://hostname |
| 246 |
""" |
| 247 |
t.write(buf) |
| 248 |
|
| 249 |
# temporary empty file to work with |
| 250 |
te = tmpdir.join("apprise-multi-add", "apprise-empty") |
| 251 |
te.write("") |
| 252 |
|
| 253 |
# Define our good:// url |
| 254 |
class GoodNotification(NotifyBase): |
| 255 |
def __init__(self, **kwargs): |
| 256 |
super().__init__(notify_format=NotifyFormat.HTML, **kwargs) |
| 257 |
|
| 258 |
def notify(self, **kwargs): |
| 259 |
# Pretend everything is okay |
| 260 |
return True |
| 261 |
|
| 262 |
def url(self, **kwargs): |
| 263 |
# support url() |
| 264 |
return "" |
| 265 |
|
| 266 |
# Store our good notification in our schema map |
| 267 |
N_MGR._schema_map["good"] = GoodNotification |
| 268 |
|
| 269 |
# Create ourselves a config object |
| 270 |
ac = AppriseConfig() |
| 271 |
|
| 272 |
# There are no services loaded |
| 273 |
assert len(ac) == 0 |
| 274 |
|
| 275 |
# Support adding of muilt strings and objects: |
| 276 |
assert ac.add(configs=(str(t), str(t))) is True |
| 277 |
assert ( |
| 278 |
ac.add(configs=(ConfigFile(path=str(te)), ConfigFile(path=str(t)))) |
| 279 |
is True |
| 280 |
) |
| 281 |
|
| 282 |
# don't support the adding of invalid content |
| 283 |
assert ac.add(configs=(object(), object())) is False |
| 284 |
assert ac.add(configs=object()) is False |
| 285 |
|
| 286 |
# Try to pop an element out of range |
| 287 |
with pytest.raises(IndexError): |
| 288 |
ac.service_pop(len(ac.services())) |
| 289 |
|
| 290 |
# Pop our elements |
| 291 |
while len(ac.services()) > 0: |
| 292 |
assert isinstance(ac.service_pop(len(ac.services()) - 1), NotifyBase) |
| 293 |
|
| 294 |
|
| 295 |
def test_apprise_add_config(): |
| 296 |
"""API AppriseConfig.add_config()""" |
| 297 |
content = """ |
| 298 |
# A comment line over top of a URL |
| 299 |
mailto://usera:[email protected] |
| 300 |
|
| 301 |
# A line with mulitiple tag assignments to it |
| 302 |
taga,tagb=gnome:// |
| 303 |
|
| 304 |
# Event if there is accidental leading spaces, this configuation |
| 305 |
# is accepting of htat and will not exclude them |
| 306 |
tagc=kde:// |
| 307 |
|
| 308 |
# A very poorly structured url |
| 309 |
sns://:@/ |
| 310 |
|
| 311 |
# Just 1 token provided causes exception |
| 312 |
sns://T1JJ3T3L2/ |
| 313 |
""" |
| 314 |
# Create ourselves a config object |
| 315 |
ac = AppriseConfig() |
| 316 |
assert ac.add_config(content=content) is True |
| 317 |
|
| 318 |
# One configuration file should have been found |
| 319 |
assert len(ac) == 1 |
| 320 |
assert ac[0].config_format is ConfigFormat.TEXT |
| 321 |
|
| 322 |
# Object can be directly checked as a boolean; response is True |
| 323 |
# when there is at least one entry |
| 324 |
assert ac |
| 325 |
|
| 326 |
# We should be able to read our 3 services from that |
| 327 |
assert len(ac.services()) == 3 |
| 328 |
|
| 329 |
# Get our URL back |
| 330 |
assert isinstance(ac[0].url(), str) |
| 331 |
|
| 332 |
# Test invalid content |
| 333 |
assert ac.add_config(content=object()) is False |
| 334 |
assert ac.add_config(content=42) is False |
| 335 |
assert ac.add_config(content=None) is False |
| 336 |
|
| 337 |
# Still only one service loaded |
| 338 |
assert len(ac) == 1 |
| 339 |
|
| 340 |
# Test having a pre-defined asset object and tag created |
| 341 |
assert ( |
| 342 |
ac.add_config(content=content, asset=AppriseAsset(), tag="a") is True |
| 343 |
) |
| 344 |
|
| 345 |
# Now there are 2 services loaded |
| 346 |
assert len(ac) == 2 |
| 347 |
|
| 348 |
# and 6 urls.. (as we've doubled up) |
| 349 |
assert len(ac.services()) == 6 |
| 350 |
|
| 351 |
content = """ |
| 352 |
# A YAML File |
| 353 |
urls: |
| 354 |
- mailto://usera:[email protected] |
| 355 |
- gnome://: |
| 356 |
tag: taga,tagb |
| 357 |
|
| 358 |
- json://localhost: |
| 359 |
+HeaderEntry1: 'a header entry' |
| 360 |
-HeaderEntryDepricated: 'a deprecated entry' |
| 361 |
:HeaderEntryIgnored: 'an ignored header entry' |
| 362 |
|
| 363 |
- xml://localhost: |
| 364 |
+HeaderEntry1: 'a header entry' |
| 365 |
-HeaderEntryDepricated: 'a deprecated entry' |
| 366 |
:HeaderEntryIgnored: 'an ignored header entry' |
| 367 |
""" |
| 368 |
|
| 369 |
# Create ourselves a config object |
| 370 |
ac = AppriseConfig() |
| 371 |
assert ac.add_config(content=content) is True |
| 372 |
|
| 373 |
# One configuration file should have been found |
| 374 |
assert len(ac) == 1 |
| 375 |
assert ac[0].config_format is ConfigFormat.YAML |
| 376 |
|
| 377 |
# Object can be directly checked as a boolean; response is True |
| 378 |
# when there is at least one entry |
| 379 |
assert ac |
| 380 |
|
| 381 |
# We should be able to read our 4 services from that |
| 382 |
assert len(ac.services()) == 4 |
| 383 |
|
| 384 |
# Now an invalid configuration file |
| 385 |
content = "invalid" |
| 386 |
|
| 387 |
# Create ourselves a config object |
| 388 |
ac = AppriseConfig() |
| 389 |
assert ac.add_config(content=content) is False |
| 390 |
|
| 391 |
# Nothing is loaded |
| 392 |
assert len(ac.services()) == 0 |
| 393 |
|
| 394 |
|
| 395 |
def test_apprise_config_tagging(tmpdir): |
| 396 |
""" |
| 397 |
API: AppriseConfig tagging |
| 398 |
|
| 399 |
""" |
| 400 |
|
| 401 |
# temporary file to work with |
| 402 |
t = tmpdir.mkdir("tagging").join("apprise") |
| 403 |
buf = "gnome://" |
| 404 |
t.write(buf) |
| 405 |
|
| 406 |
# Create ourselves a config object |
| 407 |
ac = AppriseConfig() |
| 408 |
|
| 409 |
# Add an item associated with tag a |
| 410 |
assert ac.add(configs=str(t), asset=AppriseAsset(), tag="a") is True |
| 411 |
# Add an item associated with tag b |
| 412 |
assert ac.add(configs=str(t), asset=AppriseAsset(), tag="b") is True |
| 413 |
# Add an item associated with tag a or b |
| 414 |
assert ac.add(configs=str(t), asset=AppriseAsset(), tag="a,b") is True |
| 415 |
|
| 416 |
# Now filter: a: |
| 417 |
assert len(ac.services(tag="a")) == 2 |
| 418 |
# Now filter: a or b: |
| 419 |
assert len(ac.services(tag="a,b")) == 3 |
| 420 |
# Now filter: a and b |
| 421 |
assert len(ac.services(tag=[("a", "b")])) == 1 |
| 422 |
# all matches everything |
| 423 |
assert len(ac.services(tag="all")) == 3 |
| 424 |
|
| 425 |
# Test cases using the `always` keyword |
| 426 |
# Create ourselves a config object |
| 427 |
ac = AppriseConfig() |
| 428 |
|
| 429 |
# Add an item associated with tag a |
| 430 |
assert ac.add(configs=str(t), asset=AppriseAsset(), tag="a,always") is True |
| 431 |
# Add an item associated with tag b |
| 432 |
assert ac.add(configs=str(t), asset=AppriseAsset(), tag="b") is True |
| 433 |
# Add an item associated with tag a or b |
| 434 |
assert ac.add(configs=str(t), asset=AppriseAsset(), tag="c,d") is True |
| 435 |
|
| 436 |
# Now filter: a: |
| 437 |
assert len(ac.services(tag="a")) == 1 |
| 438 |
# Now filter: a or b: |
| 439 |
assert len(ac.services(tag="a,b")) == 2 |
| 440 |
# Now filter: e |
| 441 |
# we'll match the `always' |
| 442 |
assert len(ac.services(tag="e")) == 1 |
| 443 |
assert len(ac.services(tag="e", match_always=False)) == 0 |
| 444 |
# all matches everything |
| 445 |
assert len(ac.services(tag="all")) == 3 |
| 446 |
|
| 447 |
# Now filter: d |
| 448 |
# we'll match the `always' tag |
| 449 |
assert len(ac.services(tag="d")) == 2 |
| 450 |
assert len(ac.services(tag="d", match_always=False)) == 1 |
| 451 |
|
| 452 |
|
| 453 |
def test_apprise_config_instantiate(): |
| 454 |
""" |
| 455 |
API: AppriseConfig.instantiate() |
| 456 |
|
| 457 |
""" |
| 458 |
assert ( |
| 459 |
AppriseConfig.instantiate("file://?", suppress_exceptions=True) is None |
| 460 |
) |
| 461 |
|
| 462 |
assert ( |
| 463 |
AppriseConfig.instantiate("invalid://?", suppress_exceptions=True) |
| 464 |
is None |
| 465 |
) |
| 466 |
|
| 467 |
class BadConfig(ConfigBase): |
| 468 |
# always allow incusion |
| 469 |
allow_cross_includes = ContentIncludeMode.ALWAYS |
| 470 |
|
| 471 |
def __init__(self, **kwargs): |
| 472 |
super().__init__(**kwargs) |
| 473 |
|
| 474 |
# We fail whenever we're initialized |
| 475 |
raise TypeError() |
| 476 |
|
| 477 |
@staticmethod |
| 478 |
def parse_url(url, *args, **kwargs): |
| 479 |
# always parseable |
| 480 |
return ConfigBase.parse_url(url, verify_host=False) |
| 481 |
|
| 482 |
# Store our bad configuration in our schema map |
| 483 |
C_MGR["bad"] = BadConfig |
| 484 |
|
| 485 |
with pytest.raises(TypeError): |
| 486 |
AppriseConfig.instantiate("bad://path", suppress_exceptions=False) |
| 487 |
|
| 488 |
# Same call but exceptions suppressed |
| 489 |
assert ( |
| 490 |
AppriseConfig.instantiate("bad://path", suppress_exceptions=True) |
| 491 |
is None |
| 492 |
) |
| 493 |
|
| 494 |
|
| 495 |
def test_invalid_apprise_config(tmpdir): |
| 496 |
"""Parse invalid configuration includes.""" |
| 497 |
|
| 498 |
class BadConfig(ConfigBase): |
| 499 |
# always allow incusion |
| 500 |
allow_cross_includes = ContentIncludeMode.ALWAYS |
| 501 |
|
| 502 |
def __init__(self, **kwargs): |
| 503 |
super().__init__(**kwargs) |
| 504 |
|
| 505 |
# We intentionally fail whenever we're initialized |
| 506 |
raise TypeError() |
| 507 |
|
| 508 |
@staticmethod |
| 509 |
def parse_url(url, *args, **kwargs): |
| 510 |
# always parseable |
| 511 |
return ConfigBase.parse_url(url, verify_host=False) |
| 512 |
|
| 513 |
# Store our bad configuration in our schema map |
| 514 |
C_MGR["bad"] = BadConfig |
| 515 |
|
| 516 |
# temporary file to work with |
| 517 |
t = tmpdir.mkdir("apprise-bad-obj").join("invalid") |
| 518 |
buf = f""" |
| 519 |
# Include an invalid schema |
| 520 |
include invalid:// |
| 521 |
|
| 522 |
# An unparsable valid schema |
| 523 |
include https:// |
| 524 |
|
| 525 |
# A valid configuration that will throw an exception |
| 526 |
include bad:// |
| 527 |
|
| 528 |
# Include ourselves (So our recursive includes fails as well) |
| 529 |
include {t!s} |
| 530 |
|
| 531 |
""" |
| 532 |
t.write(buf) |
| 533 |
|
| 534 |
# Create ourselves a config object with caching disbled |
| 535 |
ac = AppriseConfig(recursion=2, insecure_includes=True, cache=False) |
| 536 |
|
| 537 |
# Nothing loaded yet |
| 538 |
assert len(ac) == 0 |
| 539 |
|
| 540 |
# Add our config |
| 541 |
assert ac.add(configs=str(t), asset=AppriseAsset()) is True |
| 542 |
|
| 543 |
# One configuration file |
| 544 |
assert len(ac) == 1 |
| 545 |
|
| 546 |
# All of the services were invalid and would not load |
| 547 |
assert len(ac.services()) == 0 |
| 548 |
|
| 549 |
|
| 550 |
def test_apprise_config_with_apprise_obj(tmpdir): |
| 551 |
""" |
| 552 |
API: ConfigBase - parse valid config |
| 553 |
|
| 554 |
""" |
| 555 |
|
| 556 |
# temporary file to work with |
| 557 |
t = tmpdir.mkdir("apprise-obj").join("apprise") |
| 558 |
buf = """ |
| 559 |
good://hostname |
| 560 |
localhost=good://localhost |
| 561 |
""" |
| 562 |
t.write(buf) |
| 563 |
|
| 564 |
# Define our good:// url |
| 565 |
class GoodNotification(NotifyBase): |
| 566 |
def __init__(self, **kwargs): |
| 567 |
super().__init__(notify_format=NotifyFormat.HTML, **kwargs) |
| 568 |
|
| 569 |
def notify(self, **kwargs): |
| 570 |
# Pretend everything is okay |
| 571 |
return True |
| 572 |
|
| 573 |
def url(self, **kwargs): |
| 574 |
# support url() |
| 575 |
return "" |
| 576 |
|
| 577 |
# Store our good notification in our schema map |
| 578 |
N_MGR._schema_map["good"] = GoodNotification |
| 579 |
|
| 580 |
# Create ourselves a config object |
| 581 |
ac = AppriseConfig(cache=False) |
| 582 |
|
| 583 |
# Nothing loaded yet |
| 584 |
assert len(ac) == 0 |
| 585 |
|
| 586 |
# Add an item associated with tag a |
| 587 |
assert ac.add(configs=str(t), asset=AppriseAsset(), tag="a") is True |
| 588 |
|
| 589 |
# One configuration file |
| 590 |
assert len(ac) == 1 |
| 591 |
|
| 592 |
# 2 services found in it |
| 593 |
assert len(ac.services()) == 2 |
| 594 |
|
| 595 |
# Pop one of them (at index 0) |
| 596 |
ac.service_pop(0) |
| 597 |
|
| 598 |
# Verify that it no longer listed |
| 599 |
assert len(ac.services()) == 1 |
| 600 |
|
| 601 |
# Test our ability to add Config objects to our apprise object |
| 602 |
a = Apprise() |
| 603 |
|
| 604 |
# Add our configuration object |
| 605 |
assert a.add(services=ac) is True |
| 606 |
|
| 607 |
# Detect our 1 entry (originally there were 2 but we deleted one) |
| 608 |
assert len(a) == 1 |
| 609 |
|
| 610 |
# Notify our service |
| 611 |
assert bool(a.notify(body="apprise configuration power!")) is True |
| 612 |
|
| 613 |
# Add our configuration object |
| 614 |
assert ( |
| 615 |
a.add(services=[AppriseConfig(str(t)), AppriseConfig(str(t))]) is True |
| 616 |
) |
| 617 |
|
| 618 |
# Detect our 5 loaded entries now; 1 from first config, and another |
| 619 |
# 2x2 based on adding our list above |
| 620 |
assert len(a) == 5 |
| 621 |
|
| 622 |
# We can't add garbage |
| 623 |
assert a.add(services=object()) is False |
| 624 |
assert a.add(services=[object(), object()]) is False |
| 625 |
|
| 626 |
# Our length is unchanged |
| 627 |
assert len(a) == 5 |
| 628 |
|
| 629 |
# reference index 0 of our list |
| 630 |
ref = a[0] |
| 631 |
assert isinstance(ref, NotifyBase) |
| 632 |
|
| 633 |
# Our length is unchanged |
| 634 |
assert len(a) == 5 |
| 635 |
|
| 636 |
# pop the index |
| 637 |
ref_popped = a.pop(0) |
| 638 |
|
| 639 |
# Verify our response |
| 640 |
assert isinstance(ref_popped, NotifyBase) |
| 641 |
|
| 642 |
# Our length drops by 1 |
| 643 |
assert len(a) == 4 |
| 644 |
|
| 645 |
# Content popped is the same as one referenced by index |
| 646 |
# earlier |
| 647 |
assert ref == ref_popped |
| 648 |
|
| 649 |
# pop an index out of range |
| 650 |
with pytest.raises(IndexError): |
| 651 |
a.pop(len(a)) |
| 652 |
|
| 653 |
# Our length remains unchanged |
| 654 |
assert len(a) == 4 |
| 655 |
|
| 656 |
# Reference content out of range |
| 657 |
with pytest.raises(IndexError): |
| 658 |
a[len(a)] |
| 659 |
|
| 660 |
# reference index at the end of our list |
| 661 |
ref = a[len(a) - 1] |
| 662 |
|
| 663 |
# Verify our response |
| 664 |
assert isinstance(ref, NotifyBase) |
| 665 |
|
| 666 |
# Our length stays the same |
| 667 |
assert len(a) == 4 |
| 668 |
|
| 669 |
# We can pop from the back of the list without a problem too |
| 670 |
ref_popped = a.pop(len(a) - 1) |
| 671 |
|
| 672 |
# Verify our response |
| 673 |
assert isinstance(ref_popped, NotifyBase) |
| 674 |
|
| 675 |
# Content popped is the same as one referenced by index |
| 676 |
# earlier |
| 677 |
assert ref == ref_popped |
| 678 |
|
| 679 |
# Our length drops by 1 |
| 680 |
assert len(a) == 3 |
| 681 |
|
| 682 |
# Now we'll test adding another element to the list so that it mixes up |
| 683 |
# our response object. |
| 684 |
# Below we add 3 different types, a ConfigBase, NotifyBase, and URL |
| 685 |
assert ( |
| 686 |
a.add( |
| 687 |
services=[ |
| 688 |
ConfigFile(path=(str(t))), |
| 689 |
"good://another.host", |
| 690 |
GoodNotification(**{"host": "nuxref.com"}), |
| 691 |
] |
| 692 |
) |
| 693 |
is True |
| 694 |
) |
| 695 |
|
| 696 |
# Our length increases by 4 (2 entries in the config file, + 2 others) |
| 697 |
assert len(a) == 7 |
| 698 |
|
| 699 |
# reference index at the end of our list |
| 700 |
ref = a[len(a) - 1] |
| 701 |
|
| 702 |
# Verify our response |
| 703 |
assert isinstance(ref, NotifyBase) |
| 704 |
|
| 705 |
# We can pop from the back of the list without a problem too |
| 706 |
ref_popped = a.pop(len(a) - 1) |
| 707 |
|
| 708 |
# Verify our response |
| 709 |
assert isinstance(ref_popped, NotifyBase) |
| 710 |
|
| 711 |
# Content popped is the same as one referenced by index |
| 712 |
# earlier |
| 713 |
assert ref == ref_popped |
| 714 |
|
| 715 |
# Our length drops by 1 |
| 716 |
assert len(a) == 6 |
| 717 |
|
| 718 |
# pop our list |
| 719 |
while len(a) > 0: |
| 720 |
assert isinstance(a.pop(len(a) - 1), NotifyBase) |
| 721 |
|
| 722 |
|
| 723 |
def test_recursive_config_inclusion(tmpdir): |
| 724 |
""" |
| 725 |
API: Apprise() Recursive Config Inclusion |
| 726 |
|
| 727 |
""" |
| 728 |
|
| 729 |
# To test our config classes, we make three dummy configs |
| 730 |
class ConfigCrossPostAlways(ConfigFile): |
| 731 |
"""A dummy config that is set to always allow inclusion.""" |
| 732 |
|
| 733 |
service_name = "always" |
| 734 |
|
| 735 |
# protocol |
| 736 |
protocol = "always" |
| 737 |
|
| 738 |
# Always type |
| 739 |
allow_cross_includes = ContentIncludeMode.ALWAYS |
| 740 |
|
| 741 |
class ConfigCrossPostStrict(ConfigFile): |
| 742 |
"""A dummy config that is set to strict inclusion.""" |
| 743 |
|
| 744 |
service_name = "strict" |
| 745 |
|
| 746 |
# protocol |
| 747 |
protocol = "strict" |
| 748 |
|
| 749 |
# Always type |
| 750 |
allow_cross_includes = ContentIncludeMode.STRICT |
| 751 |
|
| 752 |
class ConfigCrossPostNever(ConfigFile): |
| 753 |
"""A dummy config that is set to never allow inclusion.""" |
| 754 |
|
| 755 |
service_name = "never" |
| 756 |
|
| 757 |
# protocol |
| 758 |
protocol = "never" |
| 759 |
|
| 760 |
# Always type |
| 761 |
allow_cross_includes = ContentIncludeMode.NEVER |
| 762 |
|
| 763 |
# store our entries |
| 764 |
C_MGR["never"] = ConfigCrossPostNever |
| 765 |
C_MGR["strict"] = ConfigCrossPostStrict |
| 766 |
C_MGR["always"] = ConfigCrossPostAlways |
| 767 |
|
| 768 |
# Make our new path valid |
| 769 |
suite = tmpdir.mkdir("apprise_config_recursion") |
| 770 |
|
| 771 |
cfg01 = suite.join("cfg01.cfg") |
| 772 |
cfg02 = suite.mkdir("dir1").join("cfg02.cfg") |
| 773 |
cfg03 = suite.mkdir("dir2").join("cfg03.cfg") |
| 774 |
cfg04 = suite.mkdir("dir3").join("cfg04.cfg") |
| 775 |
|
| 776 |
# Populate our files with valid configuration include lines |
| 777 |
cfg01.write(f""" |
| 778 |
# json entry |
| 779 |
json://localhost:8080 |
| 780 |
|
| 781 |
# absolute path inclusion to ourselves |
| 782 |
include {cfg01!s}""") |
| 783 |
|
| 784 |
cfg02.write(""" |
| 785 |
# json entry |
| 786 |
json://localhost:8080 |
| 787 |
|
| 788 |
# recursively include ourselves |
| 789 |
include cfg02.cfg""") |
| 790 |
|
| 791 |
cfg03.write(""" |
| 792 |
# xml entry |
| 793 |
xml://localhost:8080 |
| 794 |
|
| 795 |
# relative path inclusion |
| 796 |
include ../dir1/cfg02.cfg |
| 797 |
|
| 798 |
# test that we can't include invalid entries |
| 799 |
include invalid://entry |
| 800 |
|
| 801 |
# Include non includable type |
| 802 |
include memory://""") |
| 803 |
|
| 804 |
cfg04.write(f""" |
| 805 |
# xml entry |
| 806 |
xml://localhost:8080 |
| 807 |
|
| 808 |
# always include of our file |
| 809 |
include always://{cfg04!s} |
| 810 |
|
| 811 |
# never include of our file |
| 812 |
include never://{cfg04!s} |
| 813 |
|
| 814 |
# strict include of our file |
| 815 |
include strict://{cfg04!s}""") |
| 816 |
|
| 817 |
# Create ourselves a config object |
| 818 |
ac = AppriseConfig() |
| 819 |
|
| 820 |
# There are no services loaded |
| 821 |
assert len(ac) == 0 |
| 822 |
|
| 823 |
# load our configuration |
| 824 |
assert ac.add(configs=str(cfg01)) is True |
| 825 |
|
| 826 |
# verify it loaded |
| 827 |
assert len(ac) == 1 |
| 828 |
|
| 829 |
# 1 service will be loaded as there is no recursion at this point |
| 830 |
assert len(ac.services()) == 1 |
| 831 |
|
| 832 |
# Create ourselves a config object |
| 833 |
ac = AppriseConfig(recursion=1) |
| 834 |
|
| 835 |
# load our configuration |
| 836 |
assert ac.add(configs=str(cfg01)) is True |
| 837 |
|
| 838 |
# verify one configuration file loaded however since it recursively |
| 839 |
# loaded itself 1 more time, it still doesn't impact the load count: |
| 840 |
assert len(ac) == 1 |
| 841 |
|
| 842 |
# 2 services loaded now that we loaded the same file twice |
| 843 |
assert len(ac.services()) == 2 |
| 844 |
|
| 845 |
# |
| 846 |
# Now we test relative file inclusion |
| 847 |
# |
| 848 |
|
| 849 |
# Create ourselves a config object |
| 850 |
ac = AppriseConfig(recursion=10) |
| 851 |
|
| 852 |
# There are no services loaded |
| 853 |
assert len(ac) == 0 |
| 854 |
|
| 855 |
# load our configuration |
| 856 |
assert ac.add(configs=str(cfg02)) is True |
| 857 |
|
| 858 |
# verify it loaded |
| 859 |
assert len(ac) == 1 |
| 860 |
|
| 861 |
# 11 services loaded because we reloaded ourselves 10 times |
| 862 |
# after loading the first entry |
| 863 |
assert len(ac.services()) == 11 |
| 864 |
|
| 865 |
# Test our include modes (strict, always, and never) |
| 866 |
|
| 867 |
# Create ourselves a config object |
| 868 |
ac = AppriseConfig(recursion=1) |
| 869 |
|
| 870 |
# There are no services loaded |
| 871 |
assert len(ac) == 0 |
| 872 |
|
| 873 |
# load our configuration |
| 874 |
assert ac.add(configs=str(cfg04)) is True |
| 875 |
|
| 876 |
# verify it loaded |
| 877 |
assert len(ac) == 1 |
| 878 |
|
| 879 |
# 2 services loaded |
| 880 |
# 1 - from the file read (which is set at mode STRICT |
| 881 |
# 1 - from the always:// |
| 882 |
# |
| 883 |
# The never:// can ever be includeed, and the strict:// is ot of type |
| 884 |
# file:// (the one doing the include) so it is also ignored. |
| 885 |
# |
| 886 |
# By turning on the insecure_includes, we can include the strict files too |
| 887 |
assert len(ac.services()) == 2 |
| 888 |
|
| 889 |
# Create ourselves a config object |
| 890 |
ac = AppriseConfig(recursion=1, insecure_includes=True) |
| 891 |
|
| 892 |
# There are no services loaded |
| 893 |
assert len(ac) == 0 |
| 894 |
|
| 895 |
# load our configuration |
| 896 |
assert ac.add(configs=str(cfg04)) is True |
| 897 |
|
| 898 |
# verify it loaded |
| 899 |
assert len(ac) == 1 |
| 900 |
|
| 901 |
# 3 services loaded |
| 902 |
# 1 - from the file read (which is set at mode STRICT |
| 903 |
# 1 - from the always:// |
| 904 |
# 1 - from the strict:// (due to insecure_includes set) |
| 905 |
assert len(ac.services()) == 3 |
| 906 |
|
| 907 |
|
| 908 |
def test_apprise_config_file_loading(tmpdir): |
| 909 |
""" |
| 910 |
API: AppriseConfig() URL Testing |
| 911 |
|
| 912 |
""" |
| 913 |
|
| 914 |
config_path = tmpdir / "apprise.yml" |
| 915 |
|
| 916 |
# Create a temporary config file |
| 917 |
config_path.write("urls:\n - json://localhost") |
| 918 |
|
| 919 |
# Flow from README.md |
| 920 |
ap = Apprise() |
| 921 |
ap.add("xml://localhost") |
| 922 |
config = AppriseConfig() |
| 923 |
config.add(str(config_path)) |
| 924 |
ap.add(config) |
| 925 |
|
| 926 |
# Using urls() |
| 927 |
assert len(ap.urls()) == 2 |
| 928 |
|
| 929 |
|
| 930 |
def test_apprise_config_matrix_load(): |
| 931 |
""" |
| 932 |
API: AppriseConfig() matrix initialization |
| 933 |
|
| 934 |
""" |
| 935 |
|
| 936 |
import apprise |
| 937 |
|
| 938 |
class ConfigDummy(ConfigBase): |
| 939 |
"""A dummy wrapper for testing the different options in the load_matrix |
| 940 |
function.""" |
| 941 |
|
| 942 |
# The default descriptive name associated with the Notification |
| 943 |
service_name = "dummy" |
| 944 |
|
| 945 |
# protocol as tuple |
| 946 |
protocol = ("uh", "oh") |
| 947 |
|
| 948 |
# secure protocol as tuple |
| 949 |
secure_protocol = ("no", "yes") |
| 950 |
|
| 951 |
class ConfigDummy2(ConfigBase): |
| 952 |
"""A dummy wrapper for testing the different options in the load_matrix |
| 953 |
function.""" |
| 954 |
|
| 955 |
# The default descriptive name associated with the Notification |
| 956 |
service_name = "dummy2" |
| 957 |
|
| 958 |
# secure protocol as tuple |
| 959 |
secure_protocol = ("true", "false") |
| 960 |
|
| 961 |
class ConfigDummy3(ConfigBase): |
| 962 |
"""A dummy wrapper for testing the different options in the load_matrix |
| 963 |
function.""" |
| 964 |
|
| 965 |
# The default descriptive name associated with the Notification |
| 966 |
service_name = "dummy3" |
| 967 |
|
| 968 |
# secure protocol as string |
| 969 |
secure_protocol = "true" |
| 970 |
|
| 971 |
class ConfigDummy4(ConfigBase): |
| 972 |
"""A dummy wrapper for testing the different options in the load_matrix |
| 973 |
function.""" |
| 974 |
|
| 975 |
# The default descriptive name associated with the Notification |
| 976 |
service_name = "dummy4" |
| 977 |
|
| 978 |
# protocol as string |
| 979 |
protocol = "true" |
| 980 |
|
| 981 |
# Generate ourselves a fake entry |
| 982 |
apprise.config.ConfigDummy = ConfigDummy |
| 983 |
apprise.config.ConfigDummy2 = ConfigDummy2 |
| 984 |
apprise.config.ConfigDummy3 = ConfigDummy3 |
| 985 |
apprise.config.ConfigDummy4 = ConfigDummy4 |
| 986 |
|
| 987 |
|
| 988 |
def test_configmatrix_dynamic_importing(tmpdir): |
| 989 |
""" |
| 990 |
API: Apprise() Config Matrix Importing |
| 991 |
|
| 992 |
""" |
| 993 |
|
| 994 |
# Make our new path valid |
| 995 |
suite = tmpdir.mkdir("apprise_config_test_suite") |
| 996 |
suite.join("__init__.py").write("") |
| 997 |
|
| 998 |
module_name = "badconfig" |
| 999 |
|
| 1000 |
# Update our path to point to our new test suite |
| 1001 |
sys.path.insert(0, str(suite)) |
| 1002 |
|
| 1003 |
# Create a base area to work within |
| 1004 |
base = suite.mkdir(module_name) |
| 1005 |
base.join("__init__.py").write("") |
| 1006 |
|
| 1007 |
# Test no app_id |
| 1008 |
base.join("ConfigBadFile1.py").write(""" |
| 1009 |
class ConfigBadFile1: |
| 1010 |
pass""") |
| 1011 |
|
| 1012 |
# No class of the same name |
| 1013 |
base.join("ConfigBadFile2.py").write(""" |
| 1014 |
class BadClassName: |
| 1015 |
pass""") |
| 1016 |
|
| 1017 |
# Exception thrown |
| 1018 |
base.join("ConfigBadFile3.py").write("""raise ImportError()""") |
| 1019 |
|
| 1020 |
# Utilizes a schema:// already occupied (as string) |
| 1021 |
base.join("ConfigGoober.py").write(""" |
| 1022 |
from apprise.config import ConfigBase |
| 1023 |
class ConfigGoober(ConfigBase): |
| 1024 |
# This class tests the fact we have a new class name, but we're |
| 1025 |
# trying to over-ride items previously used |
| 1026 |
|
| 1027 |
# The default simple (insecure) protocol (used by ConfigHTTP) |
| 1028 |
protocol = ('http', 'goober') |
| 1029 |
|
| 1030 |
# The default secure protocol (used by ConfigHTTP) |
| 1031 |
secure_protocol = 'https' |
| 1032 |
|
| 1033 |
@staticmethod |
| 1034 |
def parse_url(url, *args, **kwargs): |
| 1035 |
# always parseable |
| 1036 |
return ConfigBase.parse_url(url, verify_host=False)""") |
| 1037 |
|
| 1038 |
# Utilizes a schema:// already occupied (as tuple) |
| 1039 |
base.join("ConfigBugger.py").write(""" |
| 1040 |
from apprise.config import ConfigBase |
| 1041 |
class ConfigBugger(ConfigBase): |
| 1042 |
# This class tests the fact we have a new class name, but we're |
| 1043 |
# trying to over-ride items previously used |
| 1044 |
|
| 1045 |
# The default simple (insecure) protocol (used by ConfigHTTP), the other |
| 1046 |
# isn't |
| 1047 |
protocol = ('http', 'bugger-test' ) |
| 1048 |
|
| 1049 |
# The default secure protocol (used by ConfigHTTP), the other isn't |
| 1050 |
secure_protocol = ('https', ['garbage']) |
| 1051 |
|
| 1052 |
@staticmethod |
| 1053 |
def parse_url(url, *args, **kwargs): |
| 1054 |
# always parseable |
| 1055 |
return ConfigBase.parse_url(url, verify_host=False)""") |
| 1056 |
|
| 1057 |
|
| 1058 |
@mock.patch("os.path.getsize") |
| 1059 |
def test_config_base_inaccessible_text_file(mock_getsize, tmpdir): |
| 1060 |
""" |
| 1061 |
API: ConfigBase.parse_inaccessible_text_file |
| 1062 |
|
| 1063 |
""" |
| 1064 |
|
| 1065 |
# temporary file to work with |
| 1066 |
t = tmpdir.mkdir("inaccessible").join("apprise") |
| 1067 |
buf = "gnome://" |
| 1068 |
t.write(buf) |
| 1069 |
|
| 1070 |
# Set getsize return value |
| 1071 |
mock_getsize.return_value = None |
| 1072 |
mock_getsize.side_effect = OSError |
| 1073 |
|
| 1074 |
# Create ourselves a config object |
| 1075 |
ac = AppriseConfig(paths=str(t)) |
| 1076 |
|
| 1077 |
# The following internally throws an exception but still counts |
| 1078 |
# as a loaded configuration file |
| 1079 |
assert len(ac) == 1 |
| 1080 |
|
| 1081 |
# Thus no notifications are loaded |
| 1082 |
assert len(ac.services()) == 0 |
| 1083 |
|
| 1084 |
|
| 1085 |
def test_config_base_parse_yaml_file01(tmpdir): |
| 1086 |
""" |
| 1087 |
API: ConfigBase.parse_yaml_file (#1) |
| 1088 |
|
| 1089 |
""" |
| 1090 |
t = tmpdir.mkdir("empty-file").join("apprise.yml") |
| 1091 |
t.write("") |
| 1092 |
|
| 1093 |
# Create ourselves a config object |
| 1094 |
ac = AppriseConfig(paths=str(t)) |
| 1095 |
|
| 1096 |
# The number of configuration files that exist |
| 1097 |
assert len(ac) == 1 |
| 1098 |
|
| 1099 |
# no notifications are loaded |
| 1100 |
assert len(ac.services()) == 0 |
| 1101 |
|
| 1102 |
|
| 1103 |
def test_config_base_parse_yaml_file02(tmpdir): |
| 1104 |
""" |
| 1105 |
API: ConfigBase.parse_yaml_file (#2) |
| 1106 |
|
| 1107 |
""" |
| 1108 |
t = tmpdir.mkdir("matching-tags").join("apprise.yml") |
| 1109 |
t.write("""urls: |
| 1110 |
- pover://nsisxnvnqixq39t0cw54pxieyvtdd9@2jevtmstfg5a7hfxndiybasttxxfku: |
| 1111 |
- tag: test1 |
| 1112 |
- pover://rg8ta87qngcrkc6t4qbykxktou0uug@tqs3i88xlufexwl8t4asglt4zp5wfn: |
| 1113 |
- tag: test2 |
| 1114 |
- pover://jcqgnlyq2oetea4qg3iunahj8d5ijm@evalvutkhc8ipmz2lcgc70wtsm0qpb: |
| 1115 |
- tag: test3""") |
| 1116 |
|
| 1117 |
# Create ourselves a config object |
| 1118 |
ac = AppriseConfig(paths=str(t)) |
| 1119 |
|
| 1120 |
# The number of configuration files that exist |
| 1121 |
assert len(ac) == 1 |
| 1122 |
|
| 1123 |
# no notifications are loaded |
| 1124 |
assert len(ac.services()) == 3 |
| 1125 |
|
| 1126 |
# Test our ability to add Config objects to our apprise object |
| 1127 |
a = Apprise() |
| 1128 |
|
| 1129 |
# Add our configuration object |
| 1130 |
assert a.add(services=ac) is True |
| 1131 |
|
| 1132 |
# Detect our 3 entry as they should have loaded successfully |
| 1133 |
assert len(a) == 3 |
| 1134 |
|
| 1135 |
# No match |
| 1136 |
assert sum(1 for _ in a.find("no-match")) == 0 |
| 1137 |
# Match everything |
| 1138 |
assert sum(1 for _ in a.find("all")) == 3 |
| 1139 |
# Match test1 entry |
| 1140 |
assert sum(1 for _ in a.find("test1")) == 1 |
| 1141 |
# Match test2 entry |
| 1142 |
assert sum(1 for _ in a.find("test2")) == 1 |
| 1143 |
# Match test3 entry |
| 1144 |
assert sum(1 for _ in a.find("test3")) == 1 |
| 1145 |
# Match test1 or test3 entry |
| 1146 |
assert sum(1 for _ in a.find("test1, test3")) == 2 |
| 1147 |
|
| 1148 |
|
| 1149 |
def test_config_base_parse_yaml_file03(tmpdir): |
| 1150 |
""" |
| 1151 |
API: ConfigBase.parse_yaml_file (#3) |
| 1152 |
|
| 1153 |
""" |
| 1154 |
|
| 1155 |
t = tmpdir.mkdir("bad-first-entry").join("apprise.yml") |
| 1156 |
# The first entry is -tag and not <dash><space>tag |
| 1157 |
# The element is therefore not picked up; This causes us to display |
| 1158 |
# some warning messages to the screen complaining of this typo yet |
| 1159 |
# still allowing us to load the URL since it is valid |
| 1160 |
t.write("""urls: |
| 1161 |
- pover://nsisxnvnqixq39t0cw54pxieyvtdd9@2jevtmstfg5a7hfxndiybasttxxfku: |
| 1162 |
-tag: test1 |
| 1163 |
- pover://rg8ta87qngcrkc6t4qbykxktou0uug@tqs3i88xlufexwl8t4asglt4zp5wfn: |
| 1164 |
- tag: test2 |
| 1165 |
- pover://jcqgnlyq2oetea4qg3iunahj8d5ijm@evalvutkhc8ipmz2lcgc70wtsm0qpb: |
| 1166 |
- tag: test3""") |
| 1167 |
|
| 1168 |
# Create ourselves a config object |
| 1169 |
ac = AppriseConfig(paths=str(t)) |
| 1170 |
|
| 1171 |
# The number of configuration files that exist |
| 1172 |
assert len(ac) == 1 |
| 1173 |
|
| 1174 |
# no notifications lines processed is 3 |
| 1175 |
assert len(ac.services()) == 3 |
| 1176 |
|
| 1177 |
# Test our ability to add Config objects to our apprise object |
| 1178 |
a = Apprise() |
| 1179 |
|
| 1180 |
# Add our configuration object |
| 1181 |
assert a.add(services=ac) is True |
| 1182 |
|
| 1183 |
# Detect our 3 entry as they should have loaded successfully |
| 1184 |
assert len(a) == 3 |
| 1185 |
|
| 1186 |
# No match |
| 1187 |
assert sum(1 for _ in a.find("no-match")) == 0 |
| 1188 |
# Match everything |
| 1189 |
assert sum(1 for _ in a.find("all")) == 3 |
| 1190 |
# No match for bad entry |
| 1191 |
assert sum(1 for _ in a.find("test1")) == 0 |
| 1192 |
# Match test2 entry |
| 1193 |
assert sum(1 for _ in a.find("test2")) == 1 |
| 1194 |
# Match test3 entry |
| 1195 |
assert sum(1 for _ in a.find("test3")) == 1 |
| 1196 |
# Match test1 or test3 entry; (only matches test3) |
| 1197 |
assert sum(1 for _ in a.find("test1, test3")) == 1 |
| 1198 |
|
| 1199 |
|
| 1200 |
def test_config_base_parse_yaml_file04(tmpdir): |
| 1201 |
""" |
| 1202 |
API: ConfigBase.parse_yaml_file (#4) |
| 1203 |
|
| 1204 |
Test the always keyword |
| 1205 |
|
| 1206 |
""" |
| 1207 |
t = tmpdir.mkdir("always-keyword").join("apprise.yml") |
| 1208 |
t.write("""urls: |
| 1209 |
- pover://nsisxnvnqixq39t0cw54pxieyvtdd9@2jevtmstfg5a7hfxndiybasttxxfku: |
| 1210 |
- tag: test1,always |
| 1211 |
- pover://rg8ta87qngcrkc6t4qbykxktou0uug@tqs3i88xlufexwl8t4asglt4zp5wfn: |
| 1212 |
- tag: test2 |
| 1213 |
- pover://jcqgnlyq2oetea4qg3iunahj8d5ijm@evalvutkhc8ipmz2lcgc70wtsm0qpb: |
| 1214 |
- tag: test3""") |
| 1215 |
|
| 1216 |
# Create ourselves a config object |
| 1217 |
ac = AppriseConfig(paths=str(t)) |
| 1218 |
|
| 1219 |
# The number of configuration files that exist |
| 1220 |
assert len(ac) == 1 |
| 1221 |
|
| 1222 |
# no notifications are loaded |
| 1223 |
assert len(ac.services()) == 3 |
| 1224 |
|
| 1225 |
# Test our ability to add Config objects to our apprise object |
| 1226 |
a = Apprise() |
| 1227 |
|
| 1228 |
# Add our configuration object |
| 1229 |
assert a.add(services=ac) is True |
| 1230 |
|
| 1231 |
# Detect our 3 entry as they should have loaded successfully |
| 1232 |
assert len(a) == 3 |
| 1233 |
|
| 1234 |
# No match still matches `always` keyword |
| 1235 |
assert sum(1 for _ in a.find("no-match")) == 1 |
| 1236 |
# Unless we explicitly do not look for that file |
| 1237 |
assert sum(1 for _ in a.find("no-match", match_always=False)) == 0 |
| 1238 |
# Match everything |
| 1239 |
assert sum(1 for _ in a.find("all")) == 3 |
| 1240 |
# Match test1 entry (also has `always` keyword |
| 1241 |
assert sum(1 for _ in a.find("test1")) == 1 |
| 1242 |
assert sum(1 for _ in a.find("test1", match_always=False)) == 1 |
| 1243 |
# Match test2 entry (and test1 due to always keyword) |
| 1244 |
assert sum(1 for _ in a.find("test2")) == 2 |
| 1245 |
assert sum(1 for _ in a.find("test2", match_always=False)) == 1 |
| 1246 |
# Match test3 entry (and test1 due to always keyword) |
| 1247 |
assert sum(1 for _ in a.find("test3")) == 2 |
| 1248 |
assert sum(1 for _ in a.find("test3", match_always=False)) == 1 |
| 1249 |
# Match test1 or test3 entry |
| 1250 |
assert sum(1 for _ in a.find("test1, test3")) == 2 |
| 1251 |
|
| 1252 |
|
| 1253 |
def test_apprise_config_template_parse(tmpdir): |
| 1254 |
""" |
| 1255 |
API: AppriseConfig parsing of templates |
| 1256 |
|
| 1257 |
""" |
| 1258 |
|
| 1259 |
# Create ourselves a config object |
| 1260 |
ac = AppriseConfig() |
| 1261 |
|
| 1262 |
t = tmpdir.mkdir("template-testing").join("apprise.yml") |
| 1263 |
t.write(""" |
| 1264 |
|
| 1265 |
tag: |
| 1266 |
- company |
| 1267 |
|
| 1268 |
# A comment line over top of a URL |
| 1269 |
urls: |
| 1270 |
- mailto://user:[email protected]: |
| 1271 |
- to: [email protected] |
| 1272 |
cc: [email protected] |
| 1273 |
|
| 1274 |
- to: [email protected] |
| 1275 |
tag: co-worker |
| 1276 |
""") |
| 1277 |
|
| 1278 |
# Create ourselves a config object |
| 1279 |
ac = AppriseConfig(paths=str(t)) |
| 1280 |
|
| 1281 |
# 2 emails to be sent |
| 1282 |
assert len(ac.services()) == 2 |
| 1283 |
|
| 1284 |
# The below checks are very customized for NotifyMail but just |
| 1285 |
# test that the content got passed correctly |
| 1286 |
assert (False, "[email protected]") in ac[0][0].targets |
| 1287 |
assert "[email protected]" in ac[0][0].cc |
| 1288 |
assert "company" in ac[0][1].tags |
| 1289 |
|
| 1290 |
assert (False, "[email protected]") in ac[0][1].targets |
| 1291 |
assert "company" in ac[0][1].tags |
| 1292 |
assert "co-worker" in ac[0][1].tags |
| 1293 |
|
| 1294 |
# |
| 1295 |
# Specifically test _special_token_handler() |
| 1296 |
# |
| 1297 |
tokens = { |
| 1298 |
# This maps to itself (bcc); no change here |
| 1299 |
"bcc": "[email protected]", |
| 1300 |
# This should get mapped to 'targets' |
| 1301 |
"to": "[email protected]", |
| 1302 |
# white space and tab is intentionally added to the end to verify we |
| 1303 |
# do not play/tamper with information |
| 1304 |
"targets": "[email protected], [email protected] \t", |
| 1305 |
# If the end user provides a configuration for data we simply don't use |
| 1306 |
# this isn't a proble... we simply don't touch it either; we leave it |
| 1307 |
# as is. |
| 1308 |
"ignore": "not-used", |
| 1309 |
} |
| 1310 |
|
| 1311 |
result = ConfigBase._special_token_handler("mailto", tokens) |
| 1312 |
# to gets mapped to targets |
| 1313 |
assert "to" not in result |
| 1314 |
|
| 1315 |
# bcc is allowed here |
| 1316 |
assert "bcc" in result |
| 1317 |
assert "targets" in result |
| 1318 |
# Not used, but also not touched; this entry should still be in our result |
| 1319 |
# set |
| 1320 |
assert "ignore" in result |
| 1321 |
# We'll concatinate all of our targets together |
| 1322 |
assert len(result["targets"]) == 2 |
| 1323 |
assert "[email protected]" in result["targets"] |
| 1324 |
# Content is passed as is |
| 1325 |
assert "[email protected], [email protected] \t" in result["targets"] |
| 1326 |
|
| 1327 |
# We re-do the simmiar test above. The very key difference is the |
| 1328 |
# `targets` is a list already (it's expected type) so `to` can properly be |
| 1329 |
# concatinated into the list vs the above (which tries to correct the |
| 1330 |
# situation) |
| 1331 |
tokens = { |
| 1332 |
# This maps to itself (bcc); no change here |
| 1333 |
"bcc": "[email protected]", |
| 1334 |
# This should get mapped to 'targets' |
| 1335 |
"to": "[email protected]", |
| 1336 |
# similar to the above test except targets is now a proper |
| 1337 |
# dictionary allowing the `to` (when translated to `targets`) to get |
| 1338 |
# appended to it |
| 1339 |
"targets": ["[email protected]", "[email protected]"], |
| 1340 |
# If the end user provides a configuration for data we simply don't use |
| 1341 |
# this isn't a proble... we simply don't touch it either; we leave it |
| 1342 |
# as is. |
| 1343 |
"ignore": "not-used", |
| 1344 |
} |
| 1345 |
|
| 1346 |
result = ConfigBase._special_token_handler("mailto", tokens) |
| 1347 |
# to gets mapped to targets |
| 1348 |
assert "to" not in result |
| 1349 |
|
| 1350 |
# bcc is allowed here |
| 1351 |
assert "bcc" in result |
| 1352 |
assert "targets" in result |
| 1353 |
# Not used, but also not touched; this entry should still be in our result |
| 1354 |
# set |
| 1355 |
assert "ignore" in result |
| 1356 |
|
| 1357 |
# Now we'll see the new user added as expected (concatinated into our list) |
| 1358 |
assert len(result["targets"]) == 3 |
| 1359 |
assert "[email protected]" in result["targets"] |
| 1360 |
assert "[email protected]" in result["targets"] |
| 1361 |
assert "[email protected]" in result["targets"] |
| 1362 |
|
| 1363 |
# Test the False branch of the isinstance guard in the template_kwargs |
| 1364 |
# loop -- where the kwarg key already holds a valid dict AND prefix |
| 1365 |
# matches exist (so we do NOT reset the existing dict to {}). The |
| 1366 |
# slack schema has template_kwargs with a ':' prefix on the 'tokens' |
| 1367 |
# key, which mirrors the old msteams layout used to cover this path. |
| 1368 |
tokens = { |
| 1369 |
# Pre-populated dict; must be preserved, not reset |
| 1370 |
"tokens": {"existing": "value"}, |
| 1371 |
# ':' prefix match -- keeps matches non-empty so the loop body runs |
| 1372 |
":newkey": "newvalue", |
| 1373 |
} |
| 1374 |
|
| 1375 |
result = ConfigBase._special_token_handler("slack", tokens) |
| 1376 |
# The prefixed entry must be stripped from the top-level result |
| 1377 |
assert ":newkey" not in result |
| 1378 |
# The pre-existing dict must survive (not be wiped to {}) |
| 1379 |
assert isinstance(result.get("tokens"), dict) |
| 1380 |
assert result["tokens"].get("existing") == "value" |
| 1381 |
# The prefix match must be merged in alongside the existing entry |
| 1382 |
assert result["tokens"].get("newkey") == "newvalue" |
| 1383 |
|
| 1384 |
# Test providing a list |
| 1385 |
t.write(""" |
| 1386 |
# A comment line over top of a URL |
| 1387 |
urls: |
| 1388 |
- mailtos://user:[email protected]: |
| 1389 |
- smtp: smtp3-dev.google.gmail.com |
| 1390 |
to: |
| 1391 |
- John Smith <[email protected]> |
| 1392 |
- Jason Tater <[email protected]> |
| 1393 |
- [email protected] |
| 1394 |
|
| 1395 |
- to: Henry Fisher <[email protected]>, Jason Archie <[email protected]> |
| 1396 |
smtp_host: smtp5-dev.google.gmail.com |
| 1397 |
tag: drinking-buddy |
| 1398 |
|
| 1399 |
# provide case where the URL includes some input too |
| 1400 |
# In both of these cases, the cc and targets (to) get over-ridden |
| 1401 |
# by values below |
| 1402 |
- mailtos://user:[email protected]/[email protected]/[email protected]/: |
| 1403 |
to: |
| 1404 |
- [email protected] |
| 1405 |
cc: |
| 1406 |
- [email protected] |
| 1407 |
|
| 1408 |
- sinch://: |
| 1409 |
- spi: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| 1410 |
token: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb |
| 1411 |
|
| 1412 |
# Test a case where we expect a string, but yaml reads it in as |
| 1413 |
# a number |
| 1414 |
from: 10005243890 |
| 1415 |
to: +1(123)555-1234 |
| 1416 |
""") |
| 1417 |
|
| 1418 |
# Create ourselves a config object |
| 1419 |
ac = AppriseConfig(paths=str(t)) |
| 1420 |
|
| 1421 |
# 2 emails to be sent and 1 Sinch service call |
| 1422 |
assert len(ac.services()) == 4 |
| 1423 |
|
| 1424 |
# Verify our users got placed into the to |
| 1425 |
assert len(ac[0][0].targets) == 3 |
| 1426 |
assert ("John Smith", "[email protected]") in ac[0][0].targets |
| 1427 |
assert ("Jason Tater", "[email protected]") in ac[0][0].targets |
| 1428 |
assert (False, "[email protected]") in ac[0][0].targets |
| 1429 |
assert ac[0][0].smtp_host == "smtp3-dev.google.gmail.com" |
| 1430 |
|
| 1431 |
assert len(ac[0][1].targets) == 2 |
| 1432 |
assert ("Henry Fisher", "[email protected]") in ac[0][1].targets |
| 1433 |
assert ("Jason Archie", "[email protected]") in ac[0][1].targets |
| 1434 |
assert "drinking-buddy" in ac[0][1].tags |
| 1435 |
assert ac[0][1].smtp_host == "smtp5-dev.google.gmail.com" |
| 1436 |
|
| 1437 |
# Our third test tests cases where some variables are defined inline |
| 1438 |
# and additional ones are defined below that share the same token space |
| 1439 |
assert len(ac[0][2].targets) == 1 |
| 1440 |
assert len(ac[0][2].cc) == 1 |
| 1441 |
assert (False, "[email protected]") in ac[0][2].targets |
| 1442 |
assert "[email protected]" in ac[0][2].cc |
| 1443 |
|
| 1444 |
# Test our Since configuration now: |
| 1445 |
assert len(ac[0][3].targets) == 1 |
| 1446 |
assert ac[0][3].service_plan_id == "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 1447 |
assert ac[0][3].source == "+10005243890" |
| 1448 |
assert ac[0][3].targets[0] == "+11235551234" |
| 1449 |
|
| 1450 |
|
| 1451 |
def test_config_base_yaml_trace_logging(tmpdir): |
| 1452 |
""" |
| 1453 |
ConfigBase: cover TRACE-only logging path in YAML parsing |
| 1454 |
|
| 1455 |
This specifically targets: |
| 1456 |
if ConfigBase.logger.isEnabledFor(logging.TRACE): |
| 1457 |
ConfigBase.logger.trace(...) |
| 1458 |
""" |
| 1459 |
t = tmpdir.mkdir("trace-logging").join("apprise.yml") |
| 1460 |
t.write("""urls: |
| 1461 |
- json://localhost |
| 1462 |
""") |
| 1463 |
|
| 1464 |
# Patch logger methods to force and observe TRACE logging behaviour |
| 1465 |
with ( |
| 1466 |
mock.patch.object( |
| 1467 |
ConfigBase.logger, |
| 1468 |
"isEnabledFor", |
| 1469 |
side_effect=lambda level: level == logging.TRACE, |
| 1470 |
), |
| 1471 |
mock.patch.object(ConfigBase.logger, "trace") as m_trace, |
| 1472 |
): |
| 1473 |
ac = AppriseConfig(paths=str(t)) |
| 1474 |
assert len(ac) == 1 |
| 1475 |
assert len(ac.services()) == 1 |
| 1476 |
|
| 1477 |
# We expect our TRACE log call to have happened at least once |
| 1478 |
assert m_trace.called is True |
| 1479 |
|
| 1480 |
|
| 1481 |
def test_config_base_clear_cache(tmpdir): |
| 1482 |
""" |
| 1483 |
ConfigBase: cover clear_cache() behaviour |
| 1484 |
""" |
| 1485 |
t = tmpdir.mkdir("clear-cache").join("apprise.yml") |
| 1486 |
t.write("""urls: |
| 1487 |
- json://localhost |
| 1488 |
""") |
| 1489 |
|
| 1490 |
ac = AppriseConfig(paths=str(t)) |
| 1491 |
|
| 1492 |
# Trigger a load to populate cache inside the config instance |
| 1493 |
assert len(ac.services()) == 1 |
| 1494 |
|
| 1495 |
# The underlying config object should have cached state |
| 1496 |
cfg = ac[0] |
| 1497 |
assert isinstance(cfg, ConfigBase) |
| 1498 |
assert isinstance(getattr(cfg, "_cached_services", None), list) |
| 1499 |
assert getattr(cfg, "_cached_time", None) is not None |
| 1500 |
|
| 1501 |
# Now clear it and confirm it resets as expected |
| 1502 |
cfg.clear_cache() |
| 1503 |
assert getattr(cfg, "_cached_services", None) is None |
| 1504 |
assert getattr(cfg, "_cached_time", None) is None |
| 1505 |
|
| 1506 |
|
| 1507 |
def test_config_base_parse_url_cache_variants(): |
| 1508 |
""" |
| 1509 |
ConfigBase.parse_url(): cover cache parsing for int and bool values |
| 1510 |
""" |
| 1511 |
# Integer cache value |
| 1512 |
r = ConfigBase.parse_url("file:///tmp/apprise.yml?cache=60") |
| 1513 |
assert isinstance(r, dict) |
| 1514 |
assert r.get("cache") == 60 |
| 1515 |
|
| 1516 |
# Boolean cache value (non-int) |
| 1517 |
r = ConfigBase.parse_url("file:///tmp/apprise.yml?cache=no") |
| 1518 |
assert isinstance(r, dict) |
| 1519 |
assert r.get("cache") is False |
| 1520 |
|
| 1521 |
r = ConfigBase.parse_url("file:///tmp/apprise.yml?cache=yes") |
| 1522 |
assert isinstance(r, dict) |
| 1523 |
assert r.get("cache") is True |
| 1524 |
|
| 1525 |
|
| 1526 |
def test_config_base_parse_url_invalid_format_removed(): |
| 1527 |
""" |
| 1528 |
ConfigBase.parse_url(): cover invalid format handling (format removed) |
| 1529 |
""" |
| 1530 |
r = ConfigBase.parse_url( |
| 1531 |
"file:///tmp/apprise.yml?format=definitely-not-a-format" |
| 1532 |
) |
| 1533 |
assert isinstance(r, dict) |
| 1534 |
|
| 1535 |
# Invalid format should be dropped from results |
| 1536 |
assert "format" not in r |
| 1537 |
|
| 1538 |
|
| 1539 |
def test_config_base_expired_with_int_cache(monkeypatch): |
| 1540 |
""" |
| 1541 |
ConfigBase.expired(): cover int cache expiry checks |
| 1542 |
|
| 1543 |
Simulate time movement to ensure both non-expired and expired paths. |
| 1544 |
""" |
| 1545 |
cb = ConfigBase(cache=30) |
| 1546 |
|
| 1547 |
# Seed cache |
| 1548 |
cb._cached_services = [] |
| 1549 |
cb._cached_time = 1000.0 |
| 1550 |
|
| 1551 |
# Within cache window |
| 1552 |
monkeypatch.setattr(time, "time", lambda: 1020.0) |
| 1553 |
assert cb.expired() is False |
| 1554 |
|
| 1555 |
# Beyond cache window |
| 1556 |
monkeypatch.setattr(time, "time", lambda: 1031.0) |
| 1557 |
assert cb.expired() is True |
| 1558 |
|
| 1559 |
|
| 1560 |
def test_configuration_manager_no_import_failures(): |
| 1561 |
"""Verify all native config plugins load without TRACE import errors.""" |
| 1562 |
|
| 1563 |
# Remove the global test-suite log suppression so TRACE records |
| 1564 |
# are visible during the scan below |
| 1565 |
logging.disable(logging.NOTSET) |
| 1566 |
|
| 1567 |
# Force a clean reload of the native config plugin directory |
| 1568 |
C_MGR.unload_modules() |
| 1569 |
|
| 1570 |
with LogCapture(level=logging.TRACE) as stream: |
| 1571 |
# Trigger load_modules() via the lazy-load path |
| 1572 |
len(C_MGR) |
| 1573 |
|
| 1574 |
# Grab the captured log content before the handler is removed |
| 1575 |
content = stream.getvalue() |
| 1576 |
|
| 1577 |
# Restore log suppression for the remainder of the test suite |
| 1578 |
logging.disable(logging.CRITICAL) |
| 1579 |
|
| 1580 |
# Any "import failed" line means a plugin directory was found but |
| 1581 |
# contained no loadable Config* class (e.g. a stale __pycache__ |
| 1582 |
# directory left behind after a rename). |
| 1583 |
failures = [ |
| 1584 |
line for line in content.splitlines() if "import failed" in line |
| 1585 |
] |
| 1586 |
assert not failures, ( |
| 1587 |
"Config plugin import failures detected:\n" + "\n".join(failures) |
| 1588 |
) |
| 1589 |
|
| 1590 |
# Leave the manager in a clean state for subsequent tests |
| 1591 |
C_MGR.unload_modules() |