| 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 |
import json |
| 29 |
|
| 30 |
# Disable logging for a cleaner testing output |
| 31 |
import logging |
| 32 |
import os |
| 33 |
from unittest import mock |
| 34 |
|
| 35 |
from helpers import AppriseURLTester |
| 36 |
import requests |
| 37 |
|
| 38 |
from apprise import Apprise, AppriseAttachment, NotifyFormat, NotifyType |
| 39 |
from apprise.exception import AppriseImproperlyConfigured |
| 40 |
from apprise.plugins.custom_json import NotifyJSON |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
# Attachment Directory |
| 45 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 46 |
|
| 47 |
# Our Testing URLs |
| 48 |
apprise_url_tests = ( |
| 49 |
( |
| 50 |
"json://:@/", |
| 51 |
{ |
| 52 |
"instance": None, |
| 53 |
}, |
| 54 |
), |
| 55 |
( |
| 56 |
"json://", |
| 57 |
{ |
| 58 |
"instance": None, |
| 59 |
}, |
| 60 |
), |
| 61 |
( |
| 62 |
"jsons://", |
| 63 |
{ |
| 64 |
"instance": None, |
| 65 |
}, |
| 66 |
), |
| 67 |
( |
| 68 |
"json://localhost", |
| 69 |
{ |
| 70 |
"instance": NotifyJSON, |
| 71 |
}, |
| 72 |
), |
| 73 |
( |
| 74 |
"json://user@localhost?method=invalid", |
| 75 |
{ |
| 76 |
"instance": AppriseImproperlyConfigured, |
| 77 |
}, |
| 78 |
), |
| 79 |
( |
| 80 |
"json://user:pass@localhost", |
| 81 |
{ |
| 82 |
"instance": NotifyJSON, |
| 83 |
# Our expected url(privacy=True) startswith() response: |
| 84 |
"privacy_url": "json://user:****@localhost", |
| 85 |
}, |
| 86 |
), |
| 87 |
( |
| 88 |
"json://user@localhost", |
| 89 |
{ |
| 90 |
"instance": NotifyJSON, |
| 91 |
}, |
| 92 |
), |
| 93 |
# Test method variations |
| 94 |
( |
| 95 |
"json://user@localhost?method=put", |
| 96 |
{ |
| 97 |
"instance": NotifyJSON, |
| 98 |
}, |
| 99 |
), |
| 100 |
( |
| 101 |
"json://user@localhost?method=get", |
| 102 |
{ |
| 103 |
"instance": NotifyJSON, |
| 104 |
}, |
| 105 |
), |
| 106 |
( |
| 107 |
"json://user@localhost?method=post", |
| 108 |
{ |
| 109 |
"instance": NotifyJSON, |
| 110 |
"force_debug": True, |
| 111 |
}, |
| 112 |
), |
| 113 |
( |
| 114 |
"json://user@localhost?method=head", |
| 115 |
{ |
| 116 |
"instance": NotifyJSON, |
| 117 |
}, |
| 118 |
), |
| 119 |
( |
| 120 |
"json://user@localhost?method=delete", |
| 121 |
{ |
| 122 |
"instance": NotifyJSON, |
| 123 |
}, |
| 124 |
), |
| 125 |
( |
| 126 |
"json://user@localhost?method=patch", |
| 127 |
{ |
| 128 |
"instance": NotifyJSON, |
| 129 |
}, |
| 130 |
), |
| 131 |
( |
| 132 |
"json://user@localhost?method=update", |
| 133 |
{ |
| 134 |
"instance": NotifyJSON, |
| 135 |
}, |
| 136 |
), |
| 137 |
( |
| 138 |
"json://user@localhost?method=options", |
| 139 |
{ |
| 140 |
"instance": NotifyJSON, |
| 141 |
}, |
| 142 |
), |
| 143 |
# Continue testing other cases |
| 144 |
( |
| 145 |
"json://localhost:8080", |
| 146 |
{ |
| 147 |
"instance": NotifyJSON, |
| 148 |
}, |
| 149 |
), |
| 150 |
( |
| 151 |
"json://user:pass@localhost:8080", |
| 152 |
{ |
| 153 |
"instance": NotifyJSON, |
| 154 |
}, |
| 155 |
), |
| 156 |
( |
| 157 |
"jsons://localhost", |
| 158 |
{ |
| 159 |
"instance": NotifyJSON, |
| 160 |
}, |
| 161 |
), |
| 162 |
( |
| 163 |
"jsons://user:pass@localhost", |
| 164 |
{ |
| 165 |
"instance": NotifyJSON, |
| 166 |
}, |
| 167 |
), |
| 168 |
( |
| 169 |
"jsons://localhost:8080/path/", |
| 170 |
{ |
| 171 |
"instance": NotifyJSON, |
| 172 |
# Our expected url(privacy=True) startswith() response: |
| 173 |
"privacy_url": "jsons://localhost:8080/path/", |
| 174 |
}, |
| 175 |
), |
| 176 |
# Test our GET params |
| 177 |
( |
| 178 |
"json://localhost:8080/path?-ParamA=Value", |
| 179 |
{ |
| 180 |
"instance": NotifyJSON, |
| 181 |
}, |
| 182 |
), |
| 183 |
( |
| 184 |
"jsons://user:password@localhost:8080", |
| 185 |
{ |
| 186 |
"instance": NotifyJSON, |
| 187 |
# Our expected url(privacy=True) startswith() response: |
| 188 |
"privacy_url": "jsons://user:****@localhost:8080", |
| 189 |
}, |
| 190 |
), |
| 191 |
# Test our Headers |
| 192 |
( |
| 193 |
"json://localhost:8080/path?+HeaderKey=HeaderValue", |
| 194 |
{ |
| 195 |
"instance": NotifyJSON, |
| 196 |
}, |
| 197 |
), |
| 198 |
( |
| 199 |
"json://user:pass@localhost:8081", |
| 200 |
{ |
| 201 |
"instance": NotifyJSON, |
| 202 |
# force a failure |
| 203 |
"response": False, |
| 204 |
"requests_response_code": requests.codes.internal_server_error, |
| 205 |
}, |
| 206 |
), |
| 207 |
( |
| 208 |
"json://user:pass@localhost:8082", |
| 209 |
{ |
| 210 |
"instance": NotifyJSON, |
| 211 |
# throw a bizarre code forcing us to fail to look it up |
| 212 |
"response": False, |
| 213 |
"requests_response_code": 999, |
| 214 |
}, |
| 215 |
), |
| 216 |
( |
| 217 |
"json://user:pass@localhost:8083", |
| 218 |
{ |
| 219 |
"instance": NotifyJSON, |
| 220 |
# Throws a series of i/o exceptions with this flag |
| 221 |
# is set and tests that we gracefully handle them |
| 222 |
"test_requests_exceptions": True, |
| 223 |
}, |
| 224 |
), |
| 225 |
) |
| 226 |
|
| 227 |
|
| 228 |
def test_plugin_custom_json_urls(): |
| 229 |
"""NotifyJSON() Apprise URLs.""" |
| 230 |
|
| 231 |
# Run our general tests |
| 232 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 233 |
|
| 234 |
|
| 235 |
@mock.patch("requests.request") |
| 236 |
def test_plugin_custom_json_multi_format(mock_request): |
| 237 |
"""NotifyJSON() supports all 3 notify formats as a pass-through.""" |
| 238 |
|
| 239 |
response = requests.Request() |
| 240 |
response.status_code = requests.codes.ok |
| 241 |
mock_request.return_value = response |
| 242 |
|
| 243 |
assert NotifyJSON.notify_format == ( |
| 244 |
NotifyFormat.TEXT, |
| 245 |
NotifyFormat.HTML, |
| 246 |
NotifyFormat.MARKDOWN, |
| 247 |
) |
| 248 |
|
| 249 |
a = Apprise() |
| 250 |
assert a.add("json://localhost") is True |
| 251 |
|
| 252 |
# Markdown input aligns directly to the plugin's supported markdown |
| 253 |
# format; no conversion should alter the content. |
| 254 |
assert ( |
| 255 |
bool(a.notify(body="# hello", body_format=NotifyFormat.MARKDOWN)) |
| 256 |
is True |
| 257 |
) |
| 258 |
payload = json.loads(mock_request.call_args_list[-1][1]["data"]) |
| 259 |
assert payload["message"] == "# hello" |
| 260 |
|
| 261 |
# ?format=html forces the resolved target regardless of the caller's |
| 262 |
# input format. |
| 263 |
a = Apprise() |
| 264 |
assert a.add("json://localhost/?format=html") is True |
| 265 |
assert bool(a.notify(body="a < b", body_format=NotifyFormat.TEXT)) is True |
| 266 |
payload = json.loads(mock_request.call_args_list[-1][1]["data"]) |
| 267 |
assert payload["message"] == "a < b" |
| 268 |
|
| 269 |
|
| 270 |
@mock.patch("requests.request") |
| 271 |
def test_plugin_custom_json_edge_cases(mock_request): |
| 272 |
"""NotifyJSON() Edge Cases.""" |
| 273 |
|
| 274 |
# Prepare our response |
| 275 |
response = requests.Request() |
| 276 |
response.status_code = requests.codes.ok |
| 277 |
|
| 278 |
# Prepare Mock |
| 279 |
mock_request.return_value = response |
| 280 |
|
| 281 |
# This string also tests that type is set to nothing |
| 282 |
results = NotifyJSON.parse_url( |
| 283 |
"json://localhost:8080/command?" |
| 284 |
":message=msg&:test=value&method=GET" |
| 285 |
"&:type=" |
| 286 |
) |
| 287 |
|
| 288 |
assert isinstance(results, dict) |
| 289 |
assert results["user"] is None |
| 290 |
assert results["password"] is None |
| 291 |
assert results["port"] == 8080 |
| 292 |
assert results["host"] == "localhost" |
| 293 |
assert results["fullpath"] == "/command" |
| 294 |
assert results["path"] == "/" |
| 295 |
assert results["query"] == "command" |
| 296 |
assert results["schema"] == "json" |
| 297 |
assert results["url"] == "json://localhost:8080/command" |
| 298 |
assert isinstance(results["qsd:"], dict) |
| 299 |
assert results["qsd:"]["message"] == "msg" |
| 300 |
# empty special mapping |
| 301 |
assert results["qsd:"]["type"] == "" |
| 302 |
|
| 303 |
instance = NotifyJSON(**results) |
| 304 |
assert isinstance(instance, NotifyJSON) |
| 305 |
|
| 306 |
response = instance.send(title="title", body="body") |
| 307 |
assert response is True |
| 308 |
assert mock_request.call_count == 1 |
| 309 |
|
| 310 |
details = mock_request.call_args_list[0] |
| 311 |
assert details[0][0] == "GET" |
| 312 |
assert details[0][1] == "http://localhost:8080/command" |
| 313 |
assert "title" in details[1]["data"] |
| 314 |
dataset = json.loads(details[1]["data"]) |
| 315 |
assert dataset["title"] == "title" |
| 316 |
assert "message" not in dataset |
| 317 |
# Ensure NotifyType does not leak as an Enum string in JSON |
| 318 |
assert "NotifyType." not in details[1]["data"] |
| 319 |
assert "msg" in dataset |
| 320 |
# type was set to nothing which implies it should be removed |
| 321 |
assert "type" not in dataset |
| 322 |
# message over-ride was provided; the body is now in `msg` and not |
| 323 |
# `message` |
| 324 |
assert dataset["msg"] == "body" |
| 325 |
|
| 326 |
assert "test" in dataset |
| 327 |
assert dataset["test"] == "value" |
| 328 |
|
| 329 |
assert instance.url(privacy=False).startswith( |
| 330 |
"json://localhost:8080/command?" |
| 331 |
) |
| 332 |
|
| 333 |
# Generate a new URL based on our last and verify key values are the same |
| 334 |
new_results = NotifyJSON.parse_url(instance.url(safe=False)) |
| 335 |
for k in ( |
| 336 |
"user", |
| 337 |
"password", |
| 338 |
"port", |
| 339 |
"host", |
| 340 |
"fullpath", |
| 341 |
"path", |
| 342 |
"query", |
| 343 |
"schema", |
| 344 |
"url", |
| 345 |
"method", |
| 346 |
): |
| 347 |
assert new_results[k] == results[k] |
| 348 |
|
| 349 |
mock_request.reset_mock() |
| 350 |
|
| 351 |
results = NotifyJSON.parse_url( |
| 352 |
"json://localhost:8080/command?:message=msg&method=GET" |
| 353 |
) |
| 354 |
instance = NotifyJSON(**results) |
| 355 |
assert instance.send(title="title", body="body") is True |
| 356 |
details = mock_request.call_args_list[0] |
| 357 |
assert details[0][0] == "GET" |
| 358 |
dataset = json.loads(details[1]["data"]) |
| 359 |
|
| 360 |
assert dataset["type"] == NotifyType.INFO.value |
| 361 |
assert "NotifyType." not in details[1]["data"] |
| 362 |
|
| 363 |
mock_request.reset_mock() |
| 364 |
|
| 365 |
# Verify UPDATE method is forwarded correctly |
| 366 |
results = NotifyJSON.parse_url("json://localhost?method=UPDATE") |
| 367 |
instance = NotifyJSON(**results) |
| 368 |
assert instance.send(title="title", body="body") is True |
| 369 |
assert mock_request.call_count == 1 |
| 370 |
details = mock_request.call_args_list[0] |
| 371 |
assert details[0][0] == "UPDATE" |
| 372 |
assert details[0][1] == "http://localhost" |
| 373 |
|
| 374 |
mock_request.reset_mock() |
| 375 |
|
| 376 |
# Verify OPTIONS method is forwarded correctly |
| 377 |
results = NotifyJSON.parse_url("json://localhost?method=OPTIONS") |
| 378 |
instance = NotifyJSON(**results) |
| 379 |
assert instance.send(title="title", body="body") is True |
| 380 |
assert mock_request.call_count == 1 |
| 381 |
details = mock_request.call_args_list[0] |
| 382 |
assert details[0][0] == "OPTIONS" |
| 383 |
assert details[0][1] == "http://localhost" |
| 384 |
|
| 385 |
|
| 386 |
@mock.patch("requests.request") |
| 387 |
def test_notify_json_plugin_attachments(mock_request): |
| 388 |
"""NotifyJSON() Attachments.""" |
| 389 |
|
| 390 |
okay_response = requests.Request() |
| 391 |
okay_response.status_code = requests.codes.ok |
| 392 |
okay_response.content = "" |
| 393 |
|
| 394 |
# Assign our mock object our return value |
| 395 |
mock_request.return_value = okay_response |
| 396 |
|
| 397 |
obj = Apprise.instantiate("json://localhost.localdomain/") |
| 398 |
assert isinstance(obj, NotifyJSON) |
| 399 |
|
| 400 |
# Test Valid Attachment |
| 401 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 402 |
attach = AppriseAttachment(path) |
| 403 |
assert ( |
| 404 |
bool( |
| 405 |
obj.notify( |
| 406 |
body="body", |
| 407 |
title="title", |
| 408 |
notify_type=NotifyType.INFO, |
| 409 |
attach=attach, |
| 410 |
) |
| 411 |
) |
| 412 |
is True |
| 413 |
) |
| 414 |
|
| 415 |
# Test invalid attachment |
| 416 |
path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 417 |
assert ( |
| 418 |
bool( |
| 419 |
obj.notify( |
| 420 |
body="body", |
| 421 |
title="title", |
| 422 |
notify_type=NotifyType.INFO, |
| 423 |
attach=path, |
| 424 |
) |
| 425 |
) |
| 426 |
is False |
| 427 |
) |
| 428 |
|
| 429 |
# Test Valid Attachment (load 3) |
| 430 |
path = ( |
| 431 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 432 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 433 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 434 |
) |
| 435 |
attach = AppriseAttachment(path) |
| 436 |
|
| 437 |
# Return our good configuration |
| 438 |
mock_request.side_effect = None |
| 439 |
mock_request.return_value = okay_response |
| 440 |
with mock.patch("builtins.open", side_effect=OSError()): |
| 441 |
# We can't send the message we can't open the attachment for reading |
| 442 |
assert ( |
| 443 |
bool( |
| 444 |
obj.notify( |
| 445 |
body="body", |
| 446 |
title="title", |
| 447 |
notify_type=NotifyType.INFO, |
| 448 |
attach=attach, |
| 449 |
) |
| 450 |
) |
| 451 |
is False |
| 452 |
) |
| 453 |
|
| 454 |
# test the handling of our batch modes |
| 455 |
obj = Apprise.instantiate("json://[email protected]/") |
| 456 |
assert isinstance(obj, NotifyJSON) |
| 457 |
|
| 458 |
# Now send an attachment normally without issues |
| 459 |
mock_request.reset_mock() |
| 460 |
assert ( |
| 461 |
bool( |
| 462 |
obj.notify( |
| 463 |
body="body", |
| 464 |
title="title", |
| 465 |
notify_type=NotifyType.INFO, |
| 466 |
attach=attach, |
| 467 |
) |
| 468 |
) |
| 469 |
is True |
| 470 |
) |
| 471 |
assert mock_request.call_count == 1 |
| 472 |
|
| 473 |
|
| 474 |
# Based on incomming webhook details defined here: |
| 475 |
# https://kb.synology.com/en-au/DSM/help/Chat/chat_integration |
| 476 |
@mock.patch("requests.request") |
| 477 |
def test_plugin_custom_form_for_synology(mock_request): |
| 478 |
"""NotifyJSON() Synology Chat Test Case.""" |
| 479 |
|
| 480 |
# Prepare our response |
| 481 |
response = requests.Request() |
| 482 |
response.status_code = requests.codes.ok |
| 483 |
|
| 484 |
# Prepare Mock |
| 485 |
mock_request.return_value = response |
| 486 |
|
| 487 |
# This is rather confusing, it may be easier to leverage the |
| 488 |
# synology:// and synologys:// plugins instead, but this is just to prove |
| 489 |
# that the same message can be sent using the json:// plugin. |
| 490 |
|
| 491 |
results = NotifyJSON.parse_url( |
| 492 |
"jsons://localhost:8081/webapi/entry.cgi?" |
| 493 |
"-api=SYNO.Chat.External&-method=incoming&-version=2&-token=abc123" |
| 494 |
"&:message=text&:version=&:type=&:title=&:attachments" |
| 495 |
"&:file_url=https://i.redd.it/my2t4d2fx0u31.jpg" |
| 496 |
) |
| 497 |
|
| 498 |
assert isinstance(results, dict) |
| 499 |
assert results["user"] is None |
| 500 |
assert results["password"] is None |
| 501 |
assert results["port"] == 8081 |
| 502 |
assert results["host"] == "localhost" |
| 503 |
assert results["fullpath"] == "/webapi/entry.cgi" |
| 504 |
assert results["path"] == "/webapi/" |
| 505 |
assert results["query"] == "entry.cgi" |
| 506 |
assert results["schema"] == "jsons" |
| 507 |
assert results["url"] == "jsons://localhost:8081/webapi/entry.cgi" |
| 508 |
assert isinstance(results["qsd:"], dict) |
| 509 |
# Header Entries |
| 510 |
assert results["qsd-"]["api"] == "SYNO.Chat.External" |
| 511 |
assert results["qsd-"]["method"] == "incoming" |
| 512 |
assert results["qsd-"]["version"] == "2" |
| 513 |
assert results["qsd-"]["token"] == "abc123" |
| 514 |
|
| 515 |
instance = NotifyJSON(**results) |
| 516 |
assert isinstance(instance, NotifyJSON) |
| 517 |
|
| 518 |
response = instance.send(title="title", body="body") |
| 519 |
assert response is True |
| 520 |
assert mock_request.call_count == 1 |
| 521 |
|
| 522 |
details = mock_request.call_args_list[0] |
| 523 |
assert details[0][0] == "POST" |
| 524 |
assert details[0][1] == "https://localhost:8081/webapi/entry.cgi" |
| 525 |
|
| 526 |
params = details[1]["params"] |
| 527 |
assert params.get("api") == "SYNO.Chat.External" |
| 528 |
assert params.get("method") == "incoming" |
| 529 |
assert params.get("version") == "2" |
| 530 |
assert params.get("token") == "abc123" |
| 531 |
|
| 532 |
payload = json.loads(details[1]["data"]) |
| 533 |
assert "version" not in payload |
| 534 |
assert "title" not in payload |
| 535 |
assert "message" not in payload |
| 536 |
assert "type" not in payload |
| 537 |
assert payload.get("text") == "body" |
| 538 |
assert payload.get("file_url") == "https://i.redd.it/my2t4d2fx0u31.jpg" |