| 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 |
from unittest import mock |
| 31 |
|
| 32 |
from apprise import AppriseAsset |
| 33 |
from apprise.config.file import ConfigFile |
| 34 |
from apprise.plugins import NotifyBase |
| 35 |
|
| 36 |
logging.disable(logging.CRITICAL) |
| 37 |
|
| 38 |
|
| 39 |
def test_config_file(tmpdir): |
| 40 |
""" |
| 41 |
API: ConfigFile() object |
| 42 |
|
| 43 |
""" |
| 44 |
|
| 45 |
assert ConfigFile.parse_url("garbage://") is None |
| 46 |
|
| 47 |
# Test cases where our URL is invalid |
| 48 |
t = tmpdir.mkdir("testing").join("apprise") |
| 49 |
t.write("gnome://") |
| 50 |
|
| 51 |
assert ConfigFile.parse_url("file://?") is None |
| 52 |
|
| 53 |
# Create an Apprise asset we can reference |
| 54 |
asset = AppriseAsset() |
| 55 |
|
| 56 |
# Initialize our object |
| 57 |
cf = ConfigFile(path=str(t), format="text", asset=asset) |
| 58 |
|
| 59 |
# one entry added |
| 60 |
assert len(cf) == 1 |
| 61 |
|
| 62 |
assert isinstance(cf.url(), str) is True |
| 63 |
|
| 64 |
# Verify that we're using the same asset |
| 65 |
assert cf[0].asset is asset |
| 66 |
|
| 67 |
# Testing of pop |
| 68 |
cf = ConfigFile(path=str(t), format="text") |
| 69 |
|
| 70 |
ref = cf[0] |
| 71 |
assert isinstance(ref, NotifyBase) is True |
| 72 |
|
| 73 |
ref_popped = cf.pop(0) |
| 74 |
assert isinstance(ref_popped, NotifyBase) is True |
| 75 |
|
| 76 |
assert ref == ref_popped |
| 77 |
|
| 78 |
assert len(cf) == 0 |
| 79 |
|
| 80 |
# reference to calls on initial reference |
| 81 |
cf = ConfigFile(path=str(t), format="text") |
| 82 |
assert isinstance(cf.pop(0), NotifyBase) is True |
| 83 |
|
| 84 |
cf = ConfigFile(path=str(t), format="text") |
| 85 |
assert isinstance(cf[0], NotifyBase) is True |
| 86 |
# Second reference actually uses cache |
| 87 |
assert isinstance(cf[0], NotifyBase) is True |
| 88 |
|
| 89 |
cf = ConfigFile(path=str(t), format="text") |
| 90 |
# Itereator creation (nothing needed to assert here) |
| 91 |
iter(cf) |
| 92 |
# Second reference actually uses cache |
| 93 |
iter(cf) |
| 94 |
|
| 95 |
# Cache Handling; cache each request for 30 seconds |
| 96 |
results = ConfigFile.parse_url(f"file://{t!s}?cache=30") |
| 97 |
assert isinstance(results, dict) |
| 98 |
cf = ConfigFile(**results) |
| 99 |
assert isinstance(cf.url(), str) is True |
| 100 |
assert isinstance(cf.read(), str) is True |
| 101 |
|
| 102 |
|
| 103 |
def test_config_file_exceptions(tmpdir): |
| 104 |
""" |
| 105 |
API: ConfigFile() i/o exception handling |
| 106 |
|
| 107 |
""" |
| 108 |
|
| 109 |
# Test cases where our URL is invalid |
| 110 |
t = tmpdir.mkdir("testing").join("apprise") |
| 111 |
t.write("gnome://") |
| 112 |
|
| 113 |
# Initialize our object |
| 114 |
cf = ConfigFile(path=str(t), format="text") |
| 115 |
|
| 116 |
# Internal Exception would have been thrown and this would fail |
| 117 |
with mock.patch("builtins.open", side_effect=OSError): |
| 118 |
assert cf.read() is None |
| 119 |
|
| 120 |
# handle case where the file is to large for what was expected: |
| 121 |
max_buffer_size = cf.max_buffer_size |
| 122 |
cf.max_buffer_size = 1 |
| 123 |
assert cf.read() is None |
| 124 |
|
| 125 |
# Restore default value |
| 126 |
cf.max_buffer_size = max_buffer_size |