| 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 |
from json import loads |
| 30 |
import logging |
| 31 |
from unittest import mock |
| 32 |
|
| 33 |
from helpers import AppriseURLTester |
| 34 |
import pytest |
| 35 |
import requests |
| 36 |
|
| 37 |
from apprise import Apprise, NotifyFormat |
| 38 |
from apprise.exception import AppriseImproperlyConfigured |
| 39 |
from apprise.plugins.chime import NotifyChime |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
# Fake but realistic-looking credentials |
| 44 |
TEST_WEBHOOK_ID = "aabbccdd-1234-5678-abcd-ef1234567890" |
| 45 |
TEST_TOKEN = "AaBbCcDdEeFf1234567890==" |
| 46 |
|
| 47 |
|
| 48 |
# Our Testing URLs |
| 49 |
apprise_url_tests = ( |
| 50 |
( |
| 51 |
# No webhook_id or token |
| 52 |
"chime://", |
| 53 |
{ |
| 54 |
"instance": AppriseImproperlyConfigured, |
| 55 |
}, |
| 56 |
), |
| 57 |
( |
| 58 |
# Empty URL |
| 59 |
"chime://:@/", |
| 60 |
{ |
| 61 |
"instance": AppriseImproperlyConfigured, |
| 62 |
}, |
| 63 |
), |
| 64 |
( |
| 65 |
# Webhook ID supplied but no token |
| 66 |
"chime://aabbccdd-1234-5678-abcd-ef1234567890", |
| 67 |
{ |
| 68 |
"instance": AppriseImproperlyConfigured, |
| 69 |
}, |
| 70 |
), |
| 71 |
( |
| 72 |
# Valid webhook_id + token in path (token has base64 padding) |
| 73 |
"chime://aabbccdd-1234-5678-abcd-ef1234567890/AaBbCcDd1234%3D%3D", |
| 74 |
{ |
| 75 |
"instance": NotifyChime, |
| 76 |
# Expected privacy URL prefix; pprint shows the decoded token |
| 77 |
# character, so the trailing = of AaBbCcDd1234== appears as = |
| 78 |
"privacy_url": "chime://a...0/A...=/", |
| 79 |
}, |
| 80 |
), |
| 81 |
( |
| 82 |
# Webhook token supplied via query string |
| 83 |
"chime://aabbccdd-1234-5678-abcd-ef1234567890" |
| 84 |
"?token=AaBbCcDd1234%3D%3D", |
| 85 |
{ |
| 86 |
"instance": NotifyChime, |
| 87 |
}, |
| 88 |
), |
| 89 |
( |
| 90 |
# Webhook ID supplied via query string |
| 91 |
"chime://?webhook_id=aabbccdd-1234-5678-abcd-ef1234567890" |
| 92 |
"&token=AaBbCcDd1234%3D%3D", |
| 93 |
{ |
| 94 |
"instance": NotifyChime, |
| 95 |
}, |
| 96 |
), |
| 97 |
( |
| 98 |
# Valid credentials; HTTP 500 response |
| 99 |
"chime://aabbccdd-1234-5678-abcd-ef1234567890/AaBbCcDd1234%3D%3D", |
| 100 |
{ |
| 101 |
"instance": NotifyChime, |
| 102 |
"response": False, |
| 103 |
"requests_response_code": (requests.codes.internal_server_error), |
| 104 |
}, |
| 105 |
), |
| 106 |
( |
| 107 |
# Valid credentials; unknown response code |
| 108 |
"chime://aabbccdd-1234-5678-abcd-ef1234567890/AaBbCcDd1234%3D%3D", |
| 109 |
{ |
| 110 |
"instance": NotifyChime, |
| 111 |
# throw a bizarre code forcing us to fail to look it up |
| 112 |
"response": False, |
| 113 |
"requests_response_code": 999, |
| 114 |
}, |
| 115 |
), |
| 116 |
( |
| 117 |
# Valid credentials; request exceptions |
| 118 |
"chime://aabbccdd-1234-5678-abcd-ef1234567890/AaBbCcDd1234%3D%3D", |
| 119 |
{ |
| 120 |
"instance": NotifyChime, |
| 121 |
# Throws a series of i/o exceptions with this flag set |
| 122 |
# and tests that we gracefully handle them |
| 123 |
"test_requests_exceptions": True, |
| 124 |
}, |
| 125 |
), |
| 126 |
( |
| 127 |
# Native Chime webhook URL |
| 128 |
"https://hooks.chime.aws/incomingwebhooks/" |
| 129 |
"aabbccdd-1234-5678-abcd-ef1234567890" |
| 130 |
"?token=AaBbCcDd1234%3D%3D", |
| 131 |
{ |
| 132 |
"instance": NotifyChime, |
| 133 |
}, |
| 134 |
), |
| 135 |
) |
| 136 |
|
| 137 |
|
| 138 |
def test_plugin_chime_urls(): |
| 139 |
"""NotifyChime() Apprise URLs.""" |
| 140 |
|
| 141 |
# Run our general tests |
| 142 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 143 |
|
| 144 |
|
| 145 |
@mock.patch("requests.post") |
| 146 |
def test_plugin_chime_init(mock_post): |
| 147 |
"""NotifyChime() Initialization Checks.""" |
| 148 |
|
| 149 |
# Prepare a valid mock response |
| 150 |
mock_post.return_value = requests.Request() |
| 151 |
mock_post.return_value.status_code = requests.codes.ok |
| 152 |
|
| 153 |
# A webhook ID is required. |
| 154 |
with pytest.raises(AppriseImproperlyConfigured): |
| 155 |
NotifyChime(webhook_id=None, token=TEST_TOKEN) |
| 156 |
|
| 157 |
# A blank webhook ID is invalid. |
| 158 |
with pytest.raises(AppriseImproperlyConfigured): |
| 159 |
NotifyChime(webhook_id=" ", token=TEST_TOKEN) |
| 160 |
|
| 161 |
# A token is required. |
| 162 |
with pytest.raises(AppriseImproperlyConfigured): |
| 163 |
NotifyChime(webhook_id=TEST_WEBHOOK_ID, token=None) |
| 164 |
|
| 165 |
# A blank token is invalid. |
| 166 |
with pytest.raises(AppriseImproperlyConfigured): |
| 167 |
NotifyChime(webhook_id=TEST_WEBHOOK_ID, token=" ") |
| 168 |
|
| 169 |
# Valid object instantiates without error |
| 170 |
obj = NotifyChime(webhook_id=TEST_WEBHOOK_ID, token=TEST_TOKEN) |
| 171 |
assert isinstance(obj, NotifyChime) |
| 172 |
|
| 173 |
# Confirm the webhook_id and token are stored correctly |
| 174 |
assert obj.webhook_id == TEST_WEBHOOK_ID |
| 175 |
assert obj.token == TEST_TOKEN |
| 176 |
|
| 177 |
|
| 178 |
@mock.patch("requests.post") |
| 179 |
def test_plugin_chime_send(mock_post): |
| 180 |
"""NotifyChime() Send Checks.""" |
| 181 |
|
| 182 |
def _mk_resp(code): |
| 183 |
"""Build a minimal mock response with the given status code.""" |
| 184 |
resp = requests.Request() |
| 185 |
resp.status_code = code |
| 186 |
# content is accessed in error-path debug logging |
| 187 |
resp.content = b"" |
| 188 |
return resp |
| 189 |
|
| 190 |
# Prepare a successful mock response |
| 191 |
mock_post.return_value = _mk_resp(requests.codes.ok) |
| 192 |
|
| 193 |
# Instantiate a valid object |
| 194 |
obj = NotifyChime(webhook_id=TEST_WEBHOOK_ID, token=TEST_TOKEN) |
| 195 |
assert isinstance(obj, NotifyChime) |
| 196 |
|
| 197 |
# Perform a successful send |
| 198 |
assert bool(obj.notify(body="Test body", title="Test title")) is True |
| 199 |
|
| 200 |
# Verify the POST was made exactly once |
| 201 |
assert mock_post.call_count == 1 |
| 202 |
|
| 203 |
# Confirm the URL used matches the expected Chime webhook URL pattern |
| 204 |
call_url = mock_post.call_args_list[0][0][0] |
| 205 |
assert "hooks.chime.aws/incomingwebhooks/" in call_url |
| 206 |
assert TEST_WEBHOOK_ID in call_url |
| 207 |
# The token is URL-encoded in the query string |
| 208 |
assert "token=" in call_url |
| 209 |
|
| 210 |
mock_post.reset_mock() |
| 211 |
|
| 212 |
# A 401 response returns False |
| 213 |
mock_post.return_value = _mk_resp(requests.codes.unauthorized) |
| 214 |
assert bool(obj.notify(body="Test body")) is False |
| 215 |
|
| 216 |
mock_post.reset_mock() |
| 217 |
|
| 218 |
# A 403 response returns False |
| 219 |
mock_post.return_value = _mk_resp(requests.codes.forbidden) |
| 220 |
assert bool(obj.notify(body="Test body")) is False |
| 221 |
|
| 222 |
mock_post.reset_mock() |
| 223 |
|
| 224 |
# A 404 response returns False |
| 225 |
mock_post.return_value = _mk_resp(requests.codes.not_found) |
| 226 |
assert bool(obj.notify(body="Test body")) is False |
| 227 |
|
| 228 |
mock_post.reset_mock() |
| 229 |
|
| 230 |
# A 429 too-many-requests response returns False |
| 231 |
mock_post.return_value = _mk_resp(429) |
| 232 |
assert bool(obj.notify(body="Test body")) is False |
| 233 |
|
| 234 |
mock_post.reset_mock() |
| 235 |
|
| 236 |
# A 500 response returns False |
| 237 |
mock_post.return_value = _mk_resp(requests.codes.internal_server_error) |
| 238 |
assert bool(obj.notify(body="Test body")) is False |
| 239 |
|
| 240 |
mock_post.reset_mock() |
| 241 |
|
| 242 |
# An unknown response code returns False |
| 243 |
mock_post.return_value = _mk_resp(999) |
| 244 |
assert bool(obj.notify(body="Test body")) is False |
| 245 |
|
| 246 |
mock_post.reset_mock() |
| 247 |
|
| 248 |
# A RequestException returns False |
| 249 |
mock_post.side_effect = requests.RequestException("Network error") |
| 250 |
assert bool(obj.notify(body="Test body")) is False |
| 251 |
|
| 252 |
|
| 253 |
@mock.patch("requests.post") |
| 254 |
def test_plugin_chime_url_roundtrip(mock_post): |
| 255 |
"""NotifyChime() URL round-trip invariant.""" |
| 256 |
|
| 257 |
# Prepare our mock response |
| 258 |
mock_post.return_value = requests.Request() |
| 259 |
mock_post.return_value.status_code = requests.codes.ok |
| 260 |
|
| 261 |
# Build a simple token that contains base64 padding (=) |
| 262 |
token_with_padding = "AaBbCcDd1234==" |
| 263 |
|
| 264 |
obj = NotifyChime( |
| 265 |
webhook_id=TEST_WEBHOOK_ID, |
| 266 |
token=token_with_padding, |
| 267 |
) |
| 268 |
assert isinstance(obj, NotifyChime) |
| 269 |
|
| 270 |
# Generate a URL and parse it back |
| 271 |
generated_url = obj.url() |
| 272 |
results = NotifyChime.parse_url(generated_url) |
| 273 |
assert results is not None |
| 274 |
|
| 275 |
# Reconstruct from parsed results |
| 276 |
obj2 = NotifyChime(**results) |
| 277 |
assert isinstance(obj2, NotifyChime) |
| 278 |
|
| 279 |
# The connection identity must be identical |
| 280 |
assert obj.url_identifier == obj2.url_identifier |
| 281 |
|
| 282 |
# Token must survive the round-trip (including = padding) |
| 283 |
assert obj.token == obj2.token |
| 284 |
assert obj.webhook_id == obj2.webhook_id |
| 285 |
|
| 286 |
|
| 287 |
@mock.patch("requests.post") |
| 288 |
def test_plugin_chime_url_parsing(mock_post): |
| 289 |
"""NotifyChime() URL Parsing Checks.""" |
| 290 |
|
| 291 |
# Prepare our mock response |
| 292 |
mock_post.return_value = requests.Request() |
| 293 |
mock_post.return_value.status_code = requests.codes.ok |
| 294 |
|
| 295 |
# Parse a standard Apprise URL |
| 296 |
results = NotifyChime.parse_url( |
| 297 |
"chime://{}/{}".format(TEST_WEBHOOK_ID, TEST_TOKEN) |
| 298 |
) |
| 299 |
assert results is not None |
| 300 |
assert results["webhook_id"] == TEST_WEBHOOK_ID |
| 301 |
assert results["token"] == TEST_TOKEN |
| 302 |
|
| 303 |
# ?token= query parameter overrides path-supplied token |
| 304 |
results = NotifyChime.parse_url( |
| 305 |
"chime://{}/ignored-token?token={}".format(TEST_WEBHOOK_ID, TEST_TOKEN) |
| 306 |
) |
| 307 |
assert results is not None |
| 308 |
assert results["token"] == TEST_TOKEN |
| 309 |
|
| 310 |
# ?webhook_id= query parameter overrides host-supplied ID |
| 311 |
results = NotifyChime.parse_url( |
| 312 |
"chime://ignored-id/{}?webhook_id={}".format( |
| 313 |
TEST_TOKEN, TEST_WEBHOOK_ID |
| 314 |
) |
| 315 |
) |
| 316 |
assert results is not None |
| 317 |
assert results["webhook_id"] == TEST_WEBHOOK_ID |
| 318 |
|
| 319 |
# None is returned for an invalid URL |
| 320 |
assert NotifyChime.parse_url(None) is None |
| 321 |
|
| 322 |
# Missing token returns a result dict with no token |
| 323 |
results = NotifyChime.parse_url("chime://{}".format(TEST_WEBHOOK_ID)) |
| 324 |
# split_path on an empty fullpath returns no entries |
| 325 |
assert results is not None |
| 326 |
assert not results.get("token") |
| 327 |
|
| 328 |
|
| 329 |
@mock.patch("requests.post") |
| 330 |
def test_plugin_chime_native_url(mock_post): |
| 331 |
"""NotifyChime() Native URL Parsing.""" |
| 332 |
|
| 333 |
# Prepare our mock response |
| 334 |
mock_post.return_value = requests.Request() |
| 335 |
mock_post.return_value.status_code = requests.codes.ok |
| 336 |
|
| 337 |
# A native URL with a URL-encoded token |
| 338 |
native_url = ( |
| 339 |
"https://hooks.chime.aws/incomingwebhooks/" |
| 340 |
"{webhook_id}?token={token}".format( |
| 341 |
webhook_id=TEST_WEBHOOK_ID, |
| 342 |
# URL-encode the token as it would appear in a real Chime URL |
| 343 |
token=NotifyChime.quote(TEST_TOKEN, safe=""), |
| 344 |
) |
| 345 |
) |
| 346 |
|
| 347 |
results = NotifyChime.parse_native_url(native_url) |
| 348 |
assert results is not None |
| 349 |
assert results["webhook_id"] == TEST_WEBHOOK_ID |
| 350 |
assert results["token"] == TEST_TOKEN |
| 351 |
|
| 352 |
# The constructed object must be functional |
| 353 |
obj = NotifyChime(**results) |
| 354 |
assert isinstance(obj, NotifyChime) |
| 355 |
assert bool(obj.notify(body="Native URL test")) is True |
| 356 |
|
| 357 |
# A non-matching URL returns None |
| 358 |
assert ( |
| 359 |
NotifyChime.parse_native_url("https://example.com/not-a-chime-url") |
| 360 |
is None |
| 361 |
) |
| 362 |
|
| 363 |
|
| 364 |
@mock.patch("requests.post") |
| 365 |
def test_plugin_chime_privacy_url(mock_post): |
| 366 |
"""NotifyChime() Privacy URL.""" |
| 367 |
|
| 368 |
# Prepare our mock response |
| 369 |
mock_post.return_value = requests.Request() |
| 370 |
mock_post.return_value.status_code = requests.codes.ok |
| 371 |
|
| 372 |
obj = NotifyChime(webhook_id=TEST_WEBHOOK_ID, token=TEST_TOKEN) |
| 373 |
assert isinstance(obj, NotifyChime) |
| 374 |
|
| 375 |
# Privacy URL must mask credentials |
| 376 |
privacy = obj.url(privacy=True) |
| 377 |
assert TEST_TOKEN not in privacy |
| 378 |
assert TEST_WEBHOOK_ID not in privacy |
| 379 |
# Scheme must still be present |
| 380 |
assert privacy.startswith("chime://") |
| 381 |
|
| 382 |
|
| 383 |
@mock.patch("requests.post") |
| 384 |
def test_plugin_chime_apprise_integration(mock_post): |
| 385 |
"""NotifyChime() Apprise integration.""" |
| 386 |
|
| 387 |
# Prepare a valid mock response |
| 388 |
mock_post.return_value = requests.Request() |
| 389 |
mock_post.return_value.status_code = requests.codes.ok |
| 390 |
|
| 391 |
# Test loading via Apprise URL |
| 392 |
a = Apprise() |
| 393 |
url = "chime://{}/{}".format(TEST_WEBHOOK_ID, TEST_TOKEN) |
| 394 |
assert a.add(url) is True |
| 395 |
assert len(a) == 1 |
| 396 |
assert isinstance(a[0], NotifyChime) |
| 397 |
|
| 398 |
# Notify succeeds |
| 399 |
assert bool(a.notify(body="Test", title="Title")) is True |
| 400 |
assert mock_post.call_count == 1 |
| 401 |
|
| 402 |
mock_post.reset_mock() |
| 403 |
|
| 404 |
# Test failure response |
| 405 |
mock_post.return_value.status_code = requests.codes.internal_server_error |
| 406 |
assert bool(a.notify(body="Test", title="Title")) is False |
| 407 |
|
| 408 |
mock_post.reset_mock() |
| 409 |
|
| 410 |
# Test with native URL |
| 411 |
a2 = Apprise() |
| 412 |
native = ( |
| 413 |
"https://hooks.chime.aws/incomingwebhooks/" |
| 414 |
"{webhook_id}?token={token}".format( |
| 415 |
webhook_id=TEST_WEBHOOK_ID, |
| 416 |
token=NotifyChime.quote(TEST_TOKEN, safe=""), |
| 417 |
) |
| 418 |
) |
| 419 |
assert a2.add(native) is True |
| 420 |
assert len(a2) == 1 |
| 421 |
assert isinstance(a2[0], NotifyChime) |
| 422 |
|
| 423 |
mock_post.return_value.status_code = requests.codes.ok |
| 424 |
assert bool(a2.notify(body="Native test", title="Title")) is True |
| 425 |
|
| 426 |
|
| 427 |
@mock.patch("requests.post") |
| 428 |
def test_plugin_chime_url_identifier(mock_post): |
| 429 |
"""NotifyChime() url_identifier uniqueness.""" |
| 430 |
|
| 431 |
# Two objects with the same credentials share an identifier |
| 432 |
obj1 = NotifyChime(webhook_id=TEST_WEBHOOK_ID, token=TEST_TOKEN) |
| 433 |
obj2 = NotifyChime(webhook_id=TEST_WEBHOOK_ID, token=TEST_TOKEN) |
| 434 |
assert obj1.url_identifier == obj2.url_identifier |
| 435 |
|
| 436 |
# Different webhook_id produces a different identifier |
| 437 |
obj3 = NotifyChime(webhook_id="different-id-abcd", token=TEST_TOKEN) |
| 438 |
assert obj1.url_identifier != obj3.url_identifier |
| 439 |
|
| 440 |
# Different token produces a different identifier |
| 441 |
obj4 = NotifyChime(webhook_id=TEST_WEBHOOK_ID, token="DifferentToken123") |
| 442 |
assert obj1.url_identifier != obj4.url_identifier |
| 443 |
|
| 444 |
# Targets must not affect the identifier (no targets for Chime) |
| 445 |
assert obj1.url_identifier[0] == NotifyChime.secure_protocol |
| 446 |
assert obj1.url_identifier[1] == TEST_WEBHOOK_ID |
| 447 |
assert obj1.url_identifier[2] == TEST_TOKEN |
| 448 |
|
| 449 |
|
| 450 |
@mock.patch("requests.post") |
| 451 |
def test_plugin_chime_html_to_markdown_format(mock_post): |
| 452 |
"""NotifyChime(): HTML body is converted to Markdown.""" |
| 453 |
|
| 454 |
# Prepare Mock |
| 455 |
mock_post.return_value = requests.Request() |
| 456 |
mock_post.return_value.status_code = requests.codes.ok |
| 457 |
|
| 458 |
# Chime's notify_format is MARKDOWN by default |
| 459 |
aobj = Apprise() |
| 460 |
assert aobj.add("chime://{}/{}".format(TEST_WEBHOOK_ID, TEST_TOKEN)) |
| 461 |
|
| 462 |
# Notify with an HTML body; the framework should convert it |
| 463 |
# to Markdown before dispatching to Chime |
| 464 |
assert ( |
| 465 |
bool( |
| 466 |
aobj.notify( |
| 467 |
body="<b>hello</b> <i>world</i>", |
| 468 |
body_format=NotifyFormat.HTML, |
| 469 |
) |
| 470 |
) |
| 471 |
is True |
| 472 |
) |
| 473 |
assert mock_post.call_count == 1 |
| 474 |
|
| 475 |
# The body must arrive as Markdown, not as stripped plain text |
| 476 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 477 |
assert payload["Content"] == "**hello** *world*" |