| 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 helpers import AppriseURLTester |
| 33 |
import pytest |
| 34 |
import requests |
| 35 |
|
| 36 |
import apprise |
| 37 |
from apprise import NotifyType |
| 38 |
from apprise.exception import AppriseImproperlyConfigured |
| 39 |
from apprise.plugins.join import JoinPriority, NotifyJoin |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
# Our Testing URLs |
| 44 |
apprise_url_tests = ( |
| 45 |
( |
| 46 |
"join://", |
| 47 |
{ |
| 48 |
"instance": AppriseImproperlyConfigured, |
| 49 |
}, |
| 50 |
), |
| 51 |
# API Key + bad url |
| 52 |
( |
| 53 |
"join://:@/", |
| 54 |
{ |
| 55 |
"instance": AppriseImproperlyConfigured, |
| 56 |
}, |
| 57 |
), |
| 58 |
# APIkey; no device |
| 59 |
( |
| 60 |
"join://%s" % ("a" * 32), |
| 61 |
{ |
| 62 |
"instance": NotifyJoin, |
| 63 |
}, |
| 64 |
), |
| 65 |
# API Key + device (using to=) |
| 66 |
( |
| 67 |
"join://{}?to={}".format("a" * 32, "d" * 32), |
| 68 |
{ |
| 69 |
"instance": NotifyJoin, |
| 70 |
# Our expected url(privacy=True) startswith() response: |
| 71 |
"privacy_url": "join://a...a/", |
| 72 |
}, |
| 73 |
), |
| 74 |
# API Key + priority setting |
| 75 |
( |
| 76 |
"join://%s?priority=high" % ("a" * 32), |
| 77 |
{ |
| 78 |
"instance": NotifyJoin, |
| 79 |
}, |
| 80 |
), |
| 81 |
# API Key + invalid priority setting |
| 82 |
( |
| 83 |
"join://%s?priority=invalid" % ("a" * 32), |
| 84 |
{ |
| 85 |
"instance": NotifyJoin, |
| 86 |
}, |
| 87 |
), |
| 88 |
# API Key + priority setting (empty) |
| 89 |
( |
| 90 |
"join://%s?priority=" % ("a" * 32), |
| 91 |
{ |
| 92 |
"instance": NotifyJoin, |
| 93 |
}, |
| 94 |
), |
| 95 |
# API Key + device |
| 96 |
( |
| 97 |
"join://{}@{}?image=True".format("a" * 32, "d" * 32), |
| 98 |
{ |
| 99 |
"instance": NotifyJoin, |
| 100 |
}, |
| 101 |
), |
| 102 |
# No image |
| 103 |
( |
| 104 |
"join://{}@{}?image=False".format("a" * 32, "d" * 32), |
| 105 |
{ |
| 106 |
"instance": NotifyJoin, |
| 107 |
}, |
| 108 |
), |
| 109 |
# API Key + Device Name |
| 110 |
( |
| 111 |
"join://{}/{}".format("a" * 32, "My Device"), |
| 112 |
{ |
| 113 |
"instance": NotifyJoin, |
| 114 |
}, |
| 115 |
), |
| 116 |
# API Key + device |
| 117 |
( |
| 118 |
"join://{}/{}".format("a" * 32, "d" * 32), |
| 119 |
{ |
| 120 |
"instance": NotifyJoin, |
| 121 |
# don't include an image by default |
| 122 |
"include_image": False, |
| 123 |
}, |
| 124 |
), |
| 125 |
# API Key + 2 devices |
| 126 |
( |
| 127 |
"join://{}/{}/{}".format("a" * 32, "d" * 32, "e" * 32), |
| 128 |
{ |
| 129 |
"instance": NotifyJoin, |
| 130 |
# don't include an image by default |
| 131 |
"include_image": False, |
| 132 |
}, |
| 133 |
), |
| 134 |
# API Key + 1 device and 1 group |
| 135 |
( |
| 136 |
"join://{}/{}/{}".format("a" * 32, "d" * 32, "group.chrome"), |
| 137 |
{ |
| 138 |
"instance": NotifyJoin, |
| 139 |
}, |
| 140 |
), |
| 141 |
( |
| 142 |
"join://%s" % ("a" * 32), |
| 143 |
{ |
| 144 |
"instance": NotifyJoin, |
| 145 |
# force a failure |
| 146 |
"response": False, |
| 147 |
"requests_response_code": requests.codes.internal_server_error, |
| 148 |
}, |
| 149 |
), |
| 150 |
( |
| 151 |
"join://%s" % ("a" * 32), |
| 152 |
{ |
| 153 |
"instance": NotifyJoin, |
| 154 |
# throw a bizarre code forcing us to fail to look it up |
| 155 |
"response": False, |
| 156 |
"requests_response_code": 999, |
| 157 |
}, |
| 158 |
), |
| 159 |
( |
| 160 |
"join://%s" % ("a" * 32), |
| 161 |
{ |
| 162 |
"instance": NotifyJoin, |
| 163 |
# Throws a series of i/o exceptions with this flag |
| 164 |
# is set and tests that we gracefully handle them |
| 165 |
"test_requests_exceptions": True, |
| 166 |
}, |
| 167 |
), |
| 168 |
) |
| 169 |
|
| 170 |
|
| 171 |
def test_plugin_join_urls(): |
| 172 |
"""NotifyJoin() Apprise URLs.""" |
| 173 |
|
| 174 |
# Run our general tests |
| 175 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 176 |
|
| 177 |
|
| 178 |
@mock.patch("requests.get") |
| 179 |
@mock.patch("requests.post") |
| 180 |
def test_plugin_join_edge_cases(mock_post, mock_get): |
| 181 |
"""NotifyJoin() Edge Cases.""" |
| 182 |
|
| 183 |
# Generate some generic message types |
| 184 |
device = "A" * 32 |
| 185 |
group = "group.chrome" |
| 186 |
apikey = "a" * 32 |
| 187 |
|
| 188 |
# Initializes the plugin with devices set to a string |
| 189 |
NotifyJoin(apikey=apikey, targets=group) |
| 190 |
|
| 191 |
# Initializes the plugin with devices set to None |
| 192 |
NotifyJoin(apikey=apikey, targets=None) |
| 193 |
|
| 194 |
# Initializes the plugin with an invalid apikey |
| 195 |
with pytest.raises(AppriseImproperlyConfigured): |
| 196 |
NotifyJoin(apikey=None) |
| 197 |
|
| 198 |
# Whitespace also acts as an invalid apikey |
| 199 |
with pytest.raises(AppriseImproperlyConfigured): |
| 200 |
NotifyJoin(apikey=" ") |
| 201 |
|
| 202 |
# Initializes the plugin with devices set to a set |
| 203 |
p = NotifyJoin(apikey=apikey, targets=[group, device]) |
| 204 |
|
| 205 |
# Prepare our mock responses |
| 206 |
req = requests.Request() |
| 207 |
req.status_code = requests.codes.created |
| 208 |
req.content = "" |
| 209 |
mock_get.return_value = req |
| 210 |
mock_post.return_value = req |
| 211 |
|
| 212 |
# Test notifications without a body or a title; nothing to send |
| 213 |
# so we return False |
| 214 |
assert ( |
| 215 |
bool(p.notify(body=None, title=None, notify_type=NotifyType.INFO)) |
| 216 |
is False |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
@mock.patch("requests.post") |
| 221 |
def test_plugin_join_config_files(mock_post): |
| 222 |
"""NotifyJoin() Config File Cases.""" |
| 223 |
content = """ |
| 224 |
urls: |
| 225 |
- join://{}@{}: |
| 226 |
- priority: -2 |
| 227 |
tag: join_int low |
| 228 |
- priority: "-2" |
| 229 |
tag: join_str_int low |
| 230 |
- priority: low |
| 231 |
tag: join_str low |
| 232 |
|
| 233 |
# This will take on normal (default) priority |
| 234 |
- priority: invalid |
| 235 |
tag: join_invalid |
| 236 |
|
| 237 |
- join://{}@{}: |
| 238 |
- priority: 2 |
| 239 |
tag: join_int emerg |
| 240 |
- priority: "2" |
| 241 |
tag: join_str_int emerg |
| 242 |
- priority: emergency |
| 243 |
tag: join_str emerg |
| 244 |
""".format("a" * 32, "b" * 32, "c" * 32, "d" * 32) |
| 245 |
|
| 246 |
# Prepare Mock |
| 247 |
mock_post.return_value = requests.Request() |
| 248 |
mock_post.return_value.status_code = requests.codes.ok |
| 249 |
|
| 250 |
# Create ourselves a config object |
| 251 |
ac = apprise.AppriseConfig() |
| 252 |
assert ac.add_config(content=content) is True |
| 253 |
|
| 254 |
aobj = apprise.Apprise() |
| 255 |
|
| 256 |
# Add our configuration |
| 257 |
aobj.add(ac) |
| 258 |
|
| 259 |
# We should be able to read our 7 services from that |
| 260 |
# 3x low |
| 261 |
# 3x emerg |
| 262 |
# 1x invalid (so takes on normal priority) |
| 263 |
assert len(ac.services()) == 7 |
| 264 |
assert len(aobj) == 7 |
| 265 |
assert len(list(aobj.find(tag="low"))) == 3 |
| 266 |
for s in aobj.find(tag="low"): |
| 267 |
assert s.priority == JoinPriority.LOW |
| 268 |
|
| 269 |
assert len(list(aobj.find(tag="emerg"))) == 3 |
| 270 |
for s in aobj.find(tag="emerg"): |
| 271 |
assert s.priority == JoinPriority.EMERGENCY |
| 272 |
|
| 273 |
assert len(list(aobj.find(tag="join_str"))) == 2 |
| 274 |
assert len(list(aobj.find(tag="join_str_int"))) == 2 |
| 275 |
assert len(list(aobj.find(tag="join_int"))) == 2 |
| 276 |
|
| 277 |
assert len(list(aobj.find(tag="join_invalid"))) == 1 |
| 278 |
assert next(aobj.find(tag="join_invalid")).priority == JoinPriority.NORMAL |
| 279 |
|
| 280 |
# Notifications work |
| 281 |
assert bool(aobj.notify(title="title", body="body")) is True |