| 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 |
import re |
| 34 |
from unittest import mock |
| 35 |
|
| 36 |
from helpers import AppriseURLTester |
| 37 |
import requests |
| 38 |
|
| 39 |
import apprise |
| 40 |
from apprise import NotifyFormat |
| 41 |
from apprise.exception import AppriseImproperlyConfigured |
| 42 |
from apprise.plugins.ntfy import NotifyNtfy, NtfyPriority |
| 43 |
|
| 44 |
logging.disable(logging.CRITICAL) |
| 45 |
|
| 46 |
# Attachment Directory |
| 47 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 48 |
|
| 49 |
# For testing our return response |
| 50 |
GOOD_RESPONSE_TEXT = { |
| 51 |
"code": "0", |
| 52 |
"error": "success", |
| 53 |
} |
| 54 |
|
| 55 |
# Our Testing URLs |
| 56 |
apprise_url_tests = ( |
| 57 |
( |
| 58 |
"ntfy://", |
| 59 |
{ |
| 60 |
# Initializes okay (as cloud mode) but has no topics to notify |
| 61 |
"instance": NotifyNtfy, |
| 62 |
# invalid topics specified (nothing to notify) |
| 63 |
# as a result the response type will be false |
| 64 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 65 |
"response": False, |
| 66 |
}, |
| 67 |
), |
| 68 |
( |
| 69 |
"ntfys://", |
| 70 |
{ |
| 71 |
# Initializes okay (as cloud mode) but has no topics to notify |
| 72 |
"instance": NotifyNtfy, |
| 73 |
# invalid topics specified (nothing to notify) |
| 74 |
# as a result the response type will be false |
| 75 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 76 |
"response": False, |
| 77 |
}, |
| 78 |
), |
| 79 |
( |
| 80 |
"ntfy://:@/", |
| 81 |
{ |
| 82 |
# Initializes okay (as cloud mode) but has no topics to notify |
| 83 |
"instance": NotifyNtfy, |
| 84 |
# invalid topics specified (nothing to notify) |
| 85 |
# as a result the response type will be false |
| 86 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 87 |
"response": False, |
| 88 |
}, |
| 89 |
), |
| 90 |
# No topics |
| 91 |
( |
| 92 |
"ntfy://user:pass@localhost?mode=private", |
| 93 |
{ |
| 94 |
"instance": NotifyNtfy, |
| 95 |
# invalid topics specified (nothing to notify) |
| 96 |
# as a result the response type will be false |
| 97 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 98 |
"response": False, |
| 99 |
}, |
| 100 |
), |
| 101 |
# No valid topics |
| 102 |
( |
| 103 |
"ntfy://user:pass@localhost/#/!/@", |
| 104 |
{ |
| 105 |
"instance": NotifyNtfy, |
| 106 |
# invalid topics specified (nothing to notify) |
| 107 |
# as a result the response type will be false |
| 108 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 109 |
"response": False, |
| 110 |
}, |
| 111 |
), |
| 112 |
# user/pass combos |
| 113 |
( |
| 114 |
"ntfy://user@localhost/topic/", |
| 115 |
{ |
| 116 |
"instance": NotifyNtfy, |
| 117 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 118 |
# Our expected url(privacy=True) startswith() response: |
| 119 |
"privacy_url": "ntfy://user@localhost/topic", |
| 120 |
}, |
| 121 |
), |
| 122 |
# Ntfy cloud mode (enforced) |
| 123 |
( |
| 124 |
"ntfy://ntfy.sh/topic1/topic2/", |
| 125 |
{ |
| 126 |
"instance": NotifyNtfy, |
| 127 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 128 |
}, |
| 129 |
), |
| 130 |
# No user/pass combo |
| 131 |
( |
| 132 |
"ntfy://localhost/topic1/topic2/", |
| 133 |
{ |
| 134 |
"instance": NotifyNtfy, |
| 135 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 136 |
}, |
| 137 |
), |
| 138 |
# A Email Testing |
| 139 |
( |
| 140 |
"ntfy://localhost/topic1/[email protected]", |
| 141 |
{ |
| 142 |
"instance": NotifyNtfy, |
| 143 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 144 |
}, |
| 145 |
), |
| 146 |
# X-Tags via canonical xtags= parameter |
| 147 |
( |
| 148 |
"ntfy://localhost/topic1/?xtags=tag1,tag2,tag3", |
| 149 |
{ |
| 150 |
"instance": NotifyNtfy, |
| 151 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 152 |
}, |
| 153 |
), |
| 154 |
# X-Tags via legacy tags= parameter (backward compat) |
| 155 |
( |
| 156 |
"ntfy://localhost/topic1/?tags=tag1,tag2,tag3", |
| 157 |
{ |
| 158 |
"instance": NotifyNtfy, |
| 159 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 160 |
}, |
| 161 |
), |
| 162 |
# Actions |
| 163 |
( |
| 164 |
"ntfy://localhost/topic1/?actions=view%2CExample%2Chttp://www.example.com/%3Bview%2CTest%2Chttp://www.test.com/", |
| 165 |
{ |
| 166 |
"instance": NotifyNtfy, |
| 167 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 168 |
}, |
| 169 |
), |
| 170 |
# Delay |
| 171 |
( |
| 172 |
"ntfy://localhost/topic1/?delay=3600", |
| 173 |
{ |
| 174 |
"instance": NotifyNtfy, |
| 175 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 176 |
}, |
| 177 |
), |
| 178 |
# Title |
| 179 |
( |
| 180 |
"ntfy://localhost/topic1/?title=A%20Great%20Title", |
| 181 |
{ |
| 182 |
"instance": NotifyNtfy, |
| 183 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 184 |
}, |
| 185 |
), |
| 186 |
# Click |
| 187 |
( |
| 188 |
"ntfy://localhost/topic1/?click=yes", |
| 189 |
{ |
| 190 |
"instance": NotifyNtfy, |
| 191 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 192 |
}, |
| 193 |
), |
| 194 |
# Email |
| 195 |
( |
| 196 |
"ntfy://localhost/topic1/[email protected]", |
| 197 |
{ |
| 198 |
"instance": NotifyNtfy, |
| 199 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 200 |
}, |
| 201 |
), |
| 202 |
# No images |
| 203 |
( |
| 204 |
"ntfy://localhost/topic1/?image=False", |
| 205 |
{ |
| 206 |
"instance": NotifyNtfy, |
| 207 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 208 |
}, |
| 209 |
), |
| 210 |
# Over-ride Image Path |
| 211 |
( |
| 212 |
"ntfy://localhost/topic1/?avatar_url=ttp://localhost/test.jpg", |
| 213 |
{ |
| 214 |
"instance": NotifyNtfy, |
| 215 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 216 |
}, |
| 217 |
), |
| 218 |
# Attach |
| 219 |
( |
| 220 |
"ntfy://localhost/topic1/?attach=http://example.com/file.jpg", |
| 221 |
{ |
| 222 |
"instance": NotifyNtfy, |
| 223 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 224 |
}, |
| 225 |
), |
| 226 |
# Attach with filename over-ride |
| 227 |
( |
| 228 |
( |
| 229 |
"ntfy://localhost/topic1/" |
| 230 |
"?attach=http://example.com/file.jpg&filename=smoke.jpg" |
| 231 |
), |
| 232 |
{"instance": NotifyNtfy, "requests_response_text": GOOD_RESPONSE_TEXT}, |
| 233 |
), |
| 234 |
# Attach with bad url |
| 235 |
( |
| 236 |
"ntfy://localhost/topic1/?attach=http://-%20", |
| 237 |
{ |
| 238 |
"instance": NotifyNtfy, |
| 239 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 240 |
}, |
| 241 |
), |
| 242 |
# Auth Token Types (tk_ gets detected as a auth=token) |
| 243 |
( |
| 244 |
"ntfy://tk_abcd123456@localhost/topic1", |
| 245 |
{ |
| 246 |
"instance": NotifyNtfy, |
| 247 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 248 |
# Our expected url(privacy=True) startswith() response: |
| 249 |
"privacy_url": "ntfy://t...6@localhost/topic1", |
| 250 |
}, |
| 251 |
), |
| 252 |
# Force an auth token since lack of tk_ prevents auto-detection |
| 253 |
( |
| 254 |
"ntfy://abcd123456@localhost/topic1?auth=token", |
| 255 |
{ |
| 256 |
"instance": NotifyNtfy, |
| 257 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 258 |
# Our expected url(privacy=True) startswith() response: |
| 259 |
"privacy_url": "ntfy://a...6@localhost/topic1", |
| 260 |
}, |
| 261 |
), |
| 262 |
# Force an auth token since lack of tk_ prevents auto-detection |
| 263 |
( |
| 264 |
"ntfy://:abcd123456@localhost/topic1?auth=token", |
| 265 |
{ |
| 266 |
"instance": NotifyNtfy, |
| 267 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 268 |
# Our expected url(privacy=True) startswith() response: |
| 269 |
"privacy_url": "ntfy://a...6@localhost/topic1", |
| 270 |
}, |
| 271 |
), |
| 272 |
# Token detection already implied when token keyword is set |
| 273 |
( |
| 274 |
"ntfy://localhost/topic1?token=abc1234", |
| 275 |
{ |
| 276 |
"instance": NotifyNtfy, |
| 277 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 278 |
# Our expected url(privacy=True) startswith() response: |
| 279 |
"privacy_url": "ntfy://a...4@localhost/topic1", |
| 280 |
}, |
| 281 |
), |
| 282 |
# Token enforced, but since a user/pass provided, only the pass is kept |
| 283 |
( |
| 284 |
"ntfy://user:token@localhost/topic1?auth=token", |
| 285 |
{ |
| 286 |
"instance": NotifyNtfy, |
| 287 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 288 |
# Our expected url(privacy=True) startswith() response: |
| 289 |
"privacy_url": "ntfy://t...n@localhost/topic1", |
| 290 |
}, |
| 291 |
), |
| 292 |
# Token mode force, but there was no token provided |
| 293 |
( |
| 294 |
"ntfy://localhost/topic1?auth=token", |
| 295 |
{ |
| 296 |
"instance": NotifyNtfy, |
| 297 |
# We'll out-right fail to send the notification |
| 298 |
"response": False, |
| 299 |
# Our expected url(privacy=True) startswith() response: |
| 300 |
"privacy_url": "ntfy://localhost/topic1", |
| 301 |
}, |
| 302 |
), |
| 303 |
# Priority |
| 304 |
( |
| 305 |
"ntfy://localhost/topic1/?priority=default", |
| 306 |
{ |
| 307 |
"instance": NotifyNtfy, |
| 308 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 309 |
# Our expected url(privacy=True) startswith() response: |
| 310 |
"privacy_url": "ntfy://localhost/topic1", |
| 311 |
}, |
| 312 |
), |
| 313 |
# Priority higher |
| 314 |
( |
| 315 |
"ntfy://localhost/topic1/?priority=high", |
| 316 |
{ |
| 317 |
"instance": NotifyNtfy, |
| 318 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 319 |
}, |
| 320 |
), |
| 321 |
# A topic and port identifier |
| 322 |
( |
| 323 |
"ntfy://user:pass@localhost:8080/topic/", |
| 324 |
{ |
| 325 |
"instance": NotifyNtfy, |
| 326 |
# The response text is expected to be the following on a success |
| 327 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 328 |
}, |
| 329 |
), |
| 330 |
# A topic (using the to=) |
| 331 |
( |
| 332 |
"ntfys://user:pass@localhost?to=topic", |
| 333 |
{ |
| 334 |
"instance": NotifyNtfy, |
| 335 |
# The response text is expected to be the following on a success |
| 336 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 337 |
}, |
| 338 |
), |
| 339 |
( |
| 340 |
"https://just/a/random/host/that/means/nothing", |
| 341 |
{ |
| 342 |
# Nothing transpires from this |
| 343 |
"instance": None |
| 344 |
}, |
| 345 |
), |
| 346 |
# reference the ntfy.sh url |
| 347 |
( |
| 348 |
"https://ntfy.sh?to=topic", |
| 349 |
{ |
| 350 |
"instance": NotifyNtfy, |
| 351 |
# The response text is expected to be the following on a success |
| 352 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 353 |
}, |
| 354 |
), |
| 355 |
# Several topics |
| 356 |
( |
| 357 |
"ntfy://user:pass@topic1/topic2/topic3/?mode=cloud", |
| 358 |
{ |
| 359 |
"instance": NotifyNtfy, |
| 360 |
# The response text is expected to be the following on a success |
| 361 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 362 |
}, |
| 363 |
), |
| 364 |
# Several topics (but do not add ntfy.sh) |
| 365 |
( |
| 366 |
"ntfy://user:[email protected]/topic1/topic2/?mode=cloud", |
| 367 |
{ |
| 368 |
"instance": NotifyNtfy, |
| 369 |
# The response text is expected to be the following on a success |
| 370 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 371 |
}, |
| 372 |
), |
| 373 |
( |
| 374 |
"ntfys://user:web/token@localhost/topic/?mode=invalid", |
| 375 |
{ |
| 376 |
# Invalid mode |
| 377 |
"instance": AppriseImproperlyConfigured, |
| 378 |
}, |
| 379 |
), |
| 380 |
( |
| 381 |
"ntfys://token@localhost/topic/?auth=invalid", |
| 382 |
{ |
| 383 |
# Invalid Authentication type |
| 384 |
"instance": AppriseImproperlyConfigured, |
| 385 |
}, |
| 386 |
), |
| 387 |
# Invalid hostname on localhost/private mode |
| 388 |
( |
| 389 |
"ntfys://user:web@-_/topic1/topic2/?mode=private", |
| 390 |
{ |
| 391 |
"instance": None, |
| 392 |
}, |
| 393 |
), |
| 394 |
( |
| 395 |
"ntfy://user:pass@localhost:8089/topic/topic2", |
| 396 |
{ |
| 397 |
"instance": NotifyNtfy, |
| 398 |
# force a failure using basic mode |
| 399 |
"response": False, |
| 400 |
"requests_response_code": requests.codes.internal_server_error, |
| 401 |
}, |
| 402 |
), |
| 403 |
( |
| 404 |
"ntfy://user:pass@localhost:8082/topic", |
| 405 |
{ |
| 406 |
"instance": NotifyNtfy, |
| 407 |
# throw a bizarre code forcing us to fail to look it up |
| 408 |
"response": False, |
| 409 |
"requests_response_code": 999, |
| 410 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 411 |
}, |
| 412 |
), |
| 413 |
( |
| 414 |
"ntfy://user:pass@localhost:8083/topic1/topic2/", |
| 415 |
{ |
| 416 |
"instance": NotifyNtfy, |
| 417 |
# Throws a series of i/o exceptions with this flag |
| 418 |
# is set and tests that we gracefully handle them |
| 419 |
"test_requests_exceptions": True, |
| 420 |
"requests_response_text": GOOD_RESPONSE_TEXT, |
| 421 |
}, |
| 422 |
), |
| 423 |
) |
| 424 |
|
| 425 |
|
| 426 |
def test_plugin_ntfy_chat_urls(): |
| 427 |
"""NotifyNtfy() Apprise URLs.""" |
| 428 |
|
| 429 |
# Run our general tests |
| 430 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 431 |
|
| 432 |
|
| 433 |
@mock.patch("requests.post") |
| 434 |
def test_plugin_ntfy_attachments(mock_post): |
| 435 |
"""NotifyNtfy() Attachment Checks.""" |
| 436 |
|
| 437 |
# Prepare Mock return object |
| 438 |
response = mock.Mock() |
| 439 |
response.content = GOOD_RESPONSE_TEXT |
| 440 |
response.status_code = requests.codes.ok |
| 441 |
mock_post.return_value = response |
| 442 |
|
| 443 |
# Test how the notifications work without attachments as they use the |
| 444 |
# JSON type posting instead |
| 445 |
|
| 446 |
# Reset our mock object |
| 447 |
mock_post.reset_mock() |
| 448 |
|
| 449 |
# Prepare our object |
| 450 |
obj = apprise.Apprise.instantiate("ntfy://user:pass@localhost:8080/topic") |
| 451 |
|
| 452 |
# Send a good attachment |
| 453 |
assert obj.notify(title="hello", body="world") |
| 454 |
assert mock_post.call_count == 1 |
| 455 |
|
| 456 |
assert mock_post.call_args_list[0][0][0] == "http://localhost:8080" |
| 457 |
|
| 458 |
response = json.loads(mock_post.call_args_list[0][1]["data"]) |
| 459 |
assert response["topic"] == "topic" |
| 460 |
assert response["title"] == "hello" |
| 461 |
assert response["message"] == "world" |
| 462 |
assert "attach" not in response |
| 463 |
|
| 464 |
# Reset our mock object |
| 465 |
mock_post.reset_mock() |
| 466 |
|
| 467 |
# prepare our attachment |
| 468 |
attach = apprise.AppriseAttachment( |
| 469 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 470 |
) |
| 471 |
|
| 472 |
# Prepare our object |
| 473 |
obj = apprise.Apprise.instantiate("ntfy://user:pass@localhost:8084/topic") |
| 474 |
|
| 475 |
# Send a good attachment |
| 476 |
assert bool(obj.notify(body="test", attach=attach)) is True |
| 477 |
|
| 478 |
# Test our call count; includes both image and message |
| 479 |
assert mock_post.call_count == 1 |
| 480 |
|
| 481 |
assert mock_post.call_args_list[0][0][0] == "http://localhost:8084/topic" |
| 482 |
|
| 483 |
assert mock_post.call_args_list[0][1]["params"]["message"] == "test" |
| 484 |
assert "title" not in mock_post.call_args_list[0][1]["params"] |
| 485 |
assert ( |
| 486 |
mock_post.call_args_list[0][1]["params"]["filename"] |
| 487 |
== "apprise-test.gif" |
| 488 |
) |
| 489 |
|
| 490 |
# Reset our mock object |
| 491 |
mock_post.reset_mock() |
| 492 |
|
| 493 |
# Add another attachment so we drop into the area of the PushBullet code |
| 494 |
# that sends remaining attachments (if more detected) |
| 495 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png")) |
| 496 |
|
| 497 |
# Send our attachments |
| 498 |
assert ( |
| 499 |
bool(obj.notify(body="test", title="wonderful", attach=attach)) is True |
| 500 |
) |
| 501 |
|
| 502 |
# Test our call count |
| 503 |
assert mock_post.call_count == 2 |
| 504 |
# Image + Message sent |
| 505 |
assert mock_post.call_args_list[0][0][0] == "http://localhost:8084/topic" |
| 506 |
assert mock_post.call_args_list[0][1]["params"]["message"] == "test" |
| 507 |
assert mock_post.call_args_list[0][1]["params"]["title"] == "wonderful" |
| 508 |
assert ( |
| 509 |
mock_post.call_args_list[0][1]["params"]["filename"] |
| 510 |
== "apprise-test.gif" |
| 511 |
) |
| 512 |
|
| 513 |
# Image no 2 (no message) |
| 514 |
assert mock_post.call_args_list[1][0][0] == "http://localhost:8084/topic" |
| 515 |
assert "message" not in mock_post.call_args_list[1][1]["params"] |
| 516 |
assert "title" not in mock_post.call_args_list[1][1]["params"] |
| 517 |
assert ( |
| 518 |
mock_post.call_args_list[1][1]["params"]["filename"] |
| 519 |
== "apprise-test.png" |
| 520 |
) |
| 521 |
|
| 522 |
# Reset our mock object |
| 523 |
mock_post.reset_mock() |
| 524 |
|
| 525 |
# An invalid attachment will cause a failure |
| 526 |
path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 527 |
attach = apprise.AppriseAttachment(path) |
| 528 |
assert bool(obj.notify(body="test", attach=attach)) is False |
| 529 |
|
| 530 |
# Test our call count |
| 531 |
assert mock_post.call_count == 0 |
| 532 |
|
| 533 |
# prepare our attachment |
| 534 |
attach = apprise.AppriseAttachment( |
| 535 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 536 |
) |
| 537 |
|
| 538 |
# Throw an exception on the first call to requests.post() |
| 539 |
mock_post.return_value = None |
| 540 |
for side_effect in (requests.RequestException(), OSError()): |
| 541 |
mock_post.side_effect = side_effect |
| 542 |
|
| 543 |
# We'll fail now because of our error handling |
| 544 |
assert obj.send(body="test", attach=attach) is False |
| 545 |
|
| 546 |
|
| 547 |
@mock.patch("requests.post") |
| 548 |
def test_plugin_custom_ntfy_edge_cases(mock_post): |
| 549 |
"""NotifyNtfy() Edge Cases.""" |
| 550 |
|
| 551 |
# Prepare our response |
| 552 |
response = requests.Request() |
| 553 |
response.status_code = requests.codes.ok |
| 554 |
response.content = json.dumps(GOOD_RESPONSE_TEXT) |
| 555 |
|
| 556 |
# Prepare Mock |
| 557 |
mock_post.return_value = response |
| 558 |
|
| 559 |
results = NotifyNtfy.parse_url( |
| 560 |
"ntfys://abc---,topic2,~~,,?priority=max&tags=smile,de" |
| 561 |
) |
| 562 |
|
| 563 |
assert isinstance(results, dict) |
| 564 |
assert results["user"] is None |
| 565 |
assert results["password"] is None |
| 566 |
assert results["port"] is None |
| 567 |
assert results["host"] == "abc---,topic2,~~,," |
| 568 |
assert results["fullpath"] is None |
| 569 |
assert results["path"] is None |
| 570 |
assert results["query"] is None |
| 571 |
assert results["schema"] == "ntfys" |
| 572 |
assert results["url"] == "ntfys://abc---,topic2,~~,," |
| 573 |
assert isinstance(results["qsd:"], dict) is True |
| 574 |
assert results["qsd"]["priority"] == "max" |
| 575 |
assert results["qsd"]["tags"] == "smile,de" |
| 576 |
# Legacy tags= is translated to xtags internally; must not leak as |
| 577 |
# results["tags"] or the config/base YAML parser would treat it as |
| 578 |
# an Apprise routing tag and silently drop the notification. |
| 579 |
assert "tags" not in results |
| 580 |
assert results["xtags"] == ["de", "smile"] |
| 581 |
|
| 582 |
instance = NotifyNtfy(**results) |
| 583 |
assert isinstance(instance, NotifyNtfy) |
| 584 |
assert len(instance.topics) == 2 |
| 585 |
assert "abc---" in instance.topics |
| 586 |
assert "topic2" in instance.topics |
| 587 |
# X-Tags header must be set correctly |
| 588 |
assert instance._NotifyNtfy__tags == ["de", "smile"] |
| 589 |
|
| 590 |
results = NotifyNtfy.parse_url( |
| 591 |
"ntfy://localhost/topic1/" |
| 592 |
"?attach=http://example.com/file.jpg&filename=smoke.jpg" |
| 593 |
) |
| 594 |
|
| 595 |
assert isinstance(results, dict) |
| 596 |
assert results["user"] is None |
| 597 |
assert results["password"] is None |
| 598 |
assert results["port"] is None |
| 599 |
assert results["host"] == "localhost" |
| 600 |
assert results["fullpath"] == "/topic1/" |
| 601 |
assert results["path"] == "/topic1/" |
| 602 |
assert results["query"] is None |
| 603 |
assert results["schema"] == "ntfy" |
| 604 |
assert results["url"] == "ntfy://localhost/topic1/" |
| 605 |
assert results["attach"] == "http://example.com/file.jpg" |
| 606 |
assert results["filename"] == "smoke.jpg" |
| 607 |
|
| 608 |
instance = NotifyNtfy(**results) |
| 609 |
assert isinstance(instance, NotifyNtfy) |
| 610 |
assert len(instance.topics) == 1 |
| 611 |
assert "topic1" in instance.topics |
| 612 |
|
| 613 |
assert ( |
| 614 |
bool( |
| 615 |
instance.notify( |
| 616 |
body="body", title="title", notify_type=apprise.NotifyType.INFO |
| 617 |
) |
| 618 |
) |
| 619 |
is True |
| 620 |
) |
| 621 |
|
| 622 |
# Test our call count |
| 623 |
assert mock_post.call_count == 1 |
| 624 |
assert mock_post.call_args_list[0][0][0] == "http://localhost" |
| 625 |
|
| 626 |
response = json.loads(mock_post.call_args_list[0][1]["data"]) |
| 627 |
assert response["topic"] == "topic1" |
| 628 |
assert response["message"] == "body" |
| 629 |
assert response["title"] == "title" |
| 630 |
assert response["attach"] == "http://example.com/file.jpg" |
| 631 |
assert response["filename"] == "smoke.jpg" |
| 632 |
|
| 633 |
# Reset our mock object |
| 634 |
mock_post.reset_mock() |
| 635 |
|
| 636 |
# Markdown Support |
| 637 |
results = NotifyNtfy.parse_url("ntfys://topic/?format=markdown") |
| 638 |
assert isinstance(results, dict) |
| 639 |
instance = NotifyNtfy(**results) |
| 640 |
|
| 641 |
assert ( |
| 642 |
bool( |
| 643 |
instance.notify( |
| 644 |
body="body", title="title", notify_type=apprise.NotifyType.INFO |
| 645 |
) |
| 646 |
) |
| 647 |
is True |
| 648 |
) |
| 649 |
|
| 650 |
assert mock_post.call_count == 1 |
| 651 |
assert mock_post.call_args_list[0][0][0] == "https://ntfy.sh" |
| 652 |
assert "X-Markdown" in mock_post.call_args_list[0][1]["headers"] |
| 653 |
|
| 654 |
|
| 655 |
def test_plugin_ntfy_attach_filename(): |
| 656 |
"""NotifyNtfy() attach= filename detection and url() round-trip.""" |
| 657 |
|
| 658 |
# The filename is taken from the path of the attach= url |
| 659 |
results = NotifyNtfy.parse_url( |
| 660 |
"ntfy://localhost/topic1/?attach=http://example.com/file.jpg" |
| 661 |
) |
| 662 |
assert isinstance(results, dict) |
| 663 |
assert results["filename"] == "file.jpg" |
| 664 |
|
| 665 |
# An attach= url that has no usable last path element gives us |
| 666 |
# nothing to name the attachment with |
| 667 |
for url in ( |
| 668 |
"ntfy://localhost/topic1/?attach=http://example.com", |
| 669 |
"ntfy://localhost/topic1/?attach=http://example.com/", |
| 670 |
"ntfy://localhost/topic1/?attach=http://example.com/dir/", |
| 671 |
): |
| 672 |
results = NotifyNtfy.parse_url(url) |
| 673 |
assert isinstance(results, dict) |
| 674 |
assert results["filename"] is None |
| 675 |
|
| 676 |
# Loading such a url must not fail |
| 677 |
obj = apprise.Apprise.instantiate( |
| 678 |
"ntfy://localhost/topic1/?attach=http://example.com" |
| 679 |
) |
| 680 |
assert isinstance(obj, NotifyNtfy) |
| 681 |
assert obj.filename is None |
| 682 |
assert "filename=" not in obj.url() |
| 683 |
|
| 684 |
# The detected filename is preserved through a url() round-trip |
| 685 |
obj = apprise.Apprise.instantiate( |
| 686 |
"ntfy://localhost/topic1/?attach=http://example.com/file.jpg" |
| 687 |
) |
| 688 |
assert isinstance(obj, NotifyNtfy) |
| 689 |
assert obj.filename == "file.jpg" |
| 690 |
assert "filename=file.jpg" in obj.url() |
| 691 |
|
| 692 |
# A filename= over-ride survives a url() round-trip |
| 693 |
obj = apprise.Apprise.instantiate( |
| 694 |
"ntfy://localhost/topic1/" |
| 695 |
"?attach=http://example.com/file.jpg&filename=smoke.jpg" |
| 696 |
) |
| 697 |
assert isinstance(obj, NotifyNtfy) |
| 698 |
assert obj.filename == "smoke.jpg" |
| 699 |
assert "filename=smoke.jpg" in obj.url() |
| 700 |
|
| 701 |
obj = apprise.Apprise.instantiate(obj.url()) |
| 702 |
assert isinstance(obj, NotifyNtfy) |
| 703 |
assert obj.attach == "http://example.com/file.jpg" |
| 704 |
assert obj.filename == "smoke.jpg" |
| 705 |
|
| 706 |
|
| 707 |
@mock.patch("requests.post") |
| 708 |
@mock.patch("requests.get") |
| 709 |
def test_plugin_ntfy_config_files(mock_post, mock_get): |
| 710 |
"""NotifyNtfy() Config File Cases.""" |
| 711 |
content = """ |
| 712 |
urls: |
| 713 |
- ntfy://localhost/topic1: |
| 714 |
- priority: 1 |
| 715 |
tag: ntfy_int min |
| 716 |
- priority: "1" |
| 717 |
tag: ntfy_str_int min |
| 718 |
- priority: min |
| 719 |
tag: ntfy_str min |
| 720 |
|
| 721 |
# This will take on normal (default) priority |
| 722 |
- priority: invalid |
| 723 |
tag: ntfy_invalid |
| 724 |
|
| 725 |
- ntfy://localhost/topic2: |
| 726 |
- priority: 5 |
| 727 |
tag: ntfy_int max |
| 728 |
- priority: "5" |
| 729 |
tag: ntfy_str_int max |
| 730 |
- priority: emergency |
| 731 |
tag: ntfy_str max |
| 732 |
- priority: max |
| 733 |
tag: ntfy_str max |
| 734 |
""" |
| 735 |
|
| 736 |
# Prepare Mock |
| 737 |
mock_post.return_value = requests.Request() |
| 738 |
mock_post.return_value.status_code = requests.codes.ok |
| 739 |
mock_get.return_value = requests.Request() |
| 740 |
mock_get.return_value.status_code = requests.codes.ok |
| 741 |
|
| 742 |
# Create ourselves a config object |
| 743 |
ac = apprise.AppriseConfig() |
| 744 |
assert ac.add_config(content=content) is True |
| 745 |
|
| 746 |
aobj = apprise.Apprise() |
| 747 |
|
| 748 |
# Add our configuration |
| 749 |
aobj.add(ac) |
| 750 |
|
| 751 |
# We should be able to read our 8 services from that |
| 752 |
# 3x min |
| 753 |
# 4x max |
| 754 |
# 1x invalid (so takes on normal priority) |
| 755 |
assert len(ac.services()) == 8 |
| 756 |
assert len(aobj) == 8 |
| 757 |
assert len(list(aobj.find(tag="min"))) == 3 |
| 758 |
for s in aobj.find(tag="min"): |
| 759 |
assert s.priority == NtfyPriority.MIN |
| 760 |
|
| 761 |
assert len(list(aobj.find(tag="max"))) == 4 |
| 762 |
for s in aobj.find(tag="max"): |
| 763 |
assert s.priority == NtfyPriority.MAX |
| 764 |
|
| 765 |
assert len(list(aobj.find(tag="ntfy_str"))) == 3 |
| 766 |
assert len(list(aobj.find(tag="ntfy_str_int"))) == 2 |
| 767 |
assert len(list(aobj.find(tag="ntfy_int"))) == 2 |
| 768 |
|
| 769 |
assert len(list(aobj.find(tag="ntfy_invalid"))) == 1 |
| 770 |
assert next(aobj.find(tag="ntfy_invalid")).priority == NtfyPriority.NORMAL |
| 771 |
|
| 772 |
# A cloud reference without any identifiers; the ntfy:// (insecure mode) |
| 773 |
# is not considered during the id generation as ntfys:// is always |
| 774 |
# implied |
| 775 |
results = NotifyNtfy.parse_url("ntfy://") |
| 776 |
obj = NotifyNtfy(**results) |
| 777 |
new_results = NotifyNtfy.parse_url(obj.url()) |
| 778 |
obj2 = NotifyNtfy(**new_results) |
| 779 |
assert obj.url_id() == obj2.url_id() |
| 780 |
|
| 781 |
|
| 782 |
@mock.patch("requests.post") |
| 783 |
def test_plugin_ntfy_internationalized_urls(mock_post): |
| 784 |
"""NotifyNtfy() Internationalized URL Support.""" |
| 785 |
|
| 786 |
# Prepare Mock return object |
| 787 |
response = mock.Mock() |
| 788 |
response.content = GOOD_RESPONSE_TEXT |
| 789 |
response.status_code = requests.codes.ok |
| 790 |
mock_post.return_value = response |
| 791 |
|
| 792 |
# Our input |
| 793 |
title = "My Title" |
| 794 |
body = "My Body" |
| 795 |
|
| 796 |
# Google Translate promised me this just says 'Apprise Example' (I hope |
| 797 |
# this is the case 🙏). Below is a URL requiring encoding so that it |
| 798 |
# can be correctly passed into an http header: |
| 799 |
click = "https://通知の例" |
| 800 |
|
| 801 |
# Prepare our object |
| 802 |
obj = apprise.Apprise.instantiate(f"ntfy://ntfy.sh/topic1?click={click}") |
| 803 |
|
| 804 |
# Send our notification |
| 805 |
assert obj.notify(title=title, body=body) |
| 806 |
assert mock_post.call_count == 1 |
| 807 |
|
| 808 |
assert mock_post.call_args_list[0][0][0] == "http://ntfy.sh" |
| 809 |
|
| 810 |
# Verify that our International URL was correctly escaped |
| 811 |
assert ( |
| 812 |
"https://%25E9%2580%259A%25E7%259F%25A5%25E3%2581%25AE%25E4%25BE%258B" |
| 813 |
in mock_post.call_args_list[0][1]["headers"]["X-Click"] |
| 814 |
) |
| 815 |
|
| 816 |
# Validate that we did not obstruct our URL in anyway |
| 817 |
assert apprise.Apprise.instantiate(obj.url()).url() == obj.url() |
| 818 |
|
| 819 |
|
| 820 |
@mock.patch("requests.post") |
| 821 |
def test_plugin_ntfy_message_to_attach(mock_post): |
| 822 |
"""NotifyNtfy() large messages converted into attachments.""" |
| 823 |
|
| 824 |
# Prepare Mock return object |
| 825 |
response = mock.Mock() |
| 826 |
response.content = GOOD_RESPONSE_TEXT |
| 827 |
response.status_code = requests.codes.ok |
| 828 |
mock_post.return_value = response |
| 829 |
|
| 830 |
# Create a very, very big message |
| 831 |
title = "My Title" |
| 832 |
body = "b" * NotifyNtfy.ntfy_json_upstream_size_limit |
| 833 |
|
| 834 |
for fmt in apprise.NOTIFY_FORMATS: |
| 835 |
# Prepare our object |
| 836 |
obj = apprise.Apprise.instantiate( |
| 837 |
f"ntfy://user:pass@localhost:8080/topic?format={fmt}" |
| 838 |
) |
| 839 |
|
| 840 |
# Our content will actually transfer as an attachment |
| 841 |
assert obj.notify(title=title, body=body) |
| 842 |
assert mock_post.call_count == 1 |
| 843 |
|
| 844 |
assert ( |
| 845 |
mock_post.call_args_list[0][0][0] == "http://localhost:8080/topic" |
| 846 |
) |
| 847 |
|
| 848 |
response = mock_post.call_args_list[0][1] |
| 849 |
assert "data" in response |
| 850 |
assert response["data"].decode("utf-8").startswith(title) |
| 851 |
assert response["data"].decode("utf-8").endswith(body) |
| 852 |
assert "params" in response |
| 853 |
assert "filename" in response["params"] |
| 854 |
# Our filename is automatically generated (with .txt) |
| 855 |
assert re.match( |
| 856 |
r"^[a-z0-9-]+\.txt$", response["params"]["filename"], re.I |
| 857 |
) |
| 858 |
|
| 859 |
# Reset our mock object |
| 860 |
mock_post.reset_mock() |
| 861 |
|
| 862 |
|
| 863 |
def test_plugin_ntfy_xtags_no_apprise_tag_collision(): |
| 864 |
"""Regression: ntfy xtags= / tags= must never become an Apprise routing |
| 865 |
tag. |
| 866 |
|
| 867 |
Prior to the xtags= rename (commit that introduced this test), ntfy stored |
| 868 |
its X-Tags value in results["tags"]. The config/base YAML parser |
| 869 |
intercepts results["tags"] and converts it to an Apprise routing tag, |
| 870 |
which caused the plugin to be silently skipped when a notify request |
| 871 |
arrived without a matching tag filter. |
| 872 |
""" |
| 873 |
|
| 874 |
# --- parse_url: canonical xtags= form --- |
| 875 |
r = NotifyNtfy.parse_url("ntfy://myhost/mytopic?xtags=warning,critical") |
| 876 |
assert r is not None |
| 877 |
assert "tags" not in r, "xtags= must not appear as results['tags']" |
| 878 |
assert r["xtags"] == ["critical", "warning"] |
| 879 |
|
| 880 |
n = NotifyNtfy(**r) |
| 881 |
assert n._NotifyNtfy__tags == ["critical", "warning"] |
| 882 |
# apprise routing tag must remain empty |
| 883 |
assert not n.tags |
| 884 |
# canonical URL must use xtags= and must not contain a bare tags= param |
| 885 |
assert "xtags=" in n.url() |
| 886 |
assert "&tags=" not in n.url() and "?tags=" not in n.url() |
| 887 |
|
| 888 |
# --- parse_url: legacy tags= form (backward compat) --- |
| 889 |
r2 = NotifyNtfy.parse_url("ntfy://myhost/mytopic?tags=info,storage") |
| 890 |
assert r2 is not None |
| 891 |
assert "tags" not in r2, "legacy tags= must be re-keyed as xtags, not tags" |
| 892 |
assert r2["xtags"] == ["info", "storage"] |
| 893 |
|
| 894 |
n2 = NotifyNtfy(**r2) |
| 895 |
assert n2._NotifyNtfy__tags == ["info", "storage"] |
| 896 |
assert not n2.tags |
| 897 |
|
| 898 |
# --- YAML config: tags= in URL must not become an Apprise routing tag --- |
| 899 |
cfg = apprise.AppriseConfig() |
| 900 |
cfg.add_config( |
| 901 |
"urls:\n - ntfy://myhost/mytopic?tags=warning\n", |
| 902 |
format="yaml", |
| 903 |
) |
| 904 |
a = apprise.Apprise() |
| 905 |
a.add(cfg) |
| 906 |
services = list(cfg.services()) |
| 907 |
assert len(services) == 1 |
| 908 |
s = services[0] |
| 909 |
# X-Tags must be preserved |
| 910 |
assert s._NotifyNtfy__tags == ["warning"] |
| 911 |
# No Apprise routing tag should have been created |
| 912 |
assert not s.tags, ( |
| 913 |
"tags= in a YAML ntfy URL must not create an Apprise routing tag" |
| 914 |
) |
| 915 |
|
| 916 |
|
| 917 |
@mock.patch("requests.post") |
| 918 |
def test_plugin_ntfy_html_to_markdown_format(mock_post): |
| 919 |
"""NotifyNtfy(): HTML body is converted to Markdown.""" |
| 920 |
|
| 921 |
# Prepare Mock |
| 922 |
mock_post.return_value = requests.Request() |
| 923 |
mock_post.return_value.status_code = requests.codes.ok |
| 924 |
mock_post.return_value.content = b"{}" |
| 925 |
|
| 926 |
# Instantiate an ntfy plugin in Markdown mode using a private server |
| 927 |
aobj = apprise.Apprise() |
| 928 |
assert aobj.add("ntfy://localhost/topic?format=markdown") |
| 929 |
|
| 930 |
# Notify with an HTML body; the framework should convert it |
| 931 |
# to Markdown before dispatching to ntfy |
| 932 |
assert ( |
| 933 |
bool( |
| 934 |
aobj.notify( |
| 935 |
body="<b>hello</b> <i>world</i>", |
| 936 |
body_format=NotifyFormat.HTML, |
| 937 |
) |
| 938 |
) |
| 939 |
is True |
| 940 |
) |
| 941 |
assert mock_post.call_count == 1 |
| 942 |
|
| 943 |
# The body must arrive as Markdown, not as stripped plain text |
| 944 |
payload = json.loads(mock_post.call_args_list[0][1]["data"]) |
| 945 |
assert payload["message"] == "**hello** *world*" |