| 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 |
| 13 |
# notice, this list of conditions and the following disclaimer in the |
| 14 |
# documentation 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 json |
| 30 |
import logging |
| 31 |
import os |
| 32 |
from unittest import mock |
| 33 |
from urllib.parse import urlparse |
| 34 |
|
| 35 |
from helpers import AppriseURLTester |
| 36 |
import pytest |
| 37 |
import requests |
| 38 |
|
| 39 |
from apprise import Apprise, AppriseAttachment, NotifyType |
| 40 |
from apprise.exception import AppriseImproperlyConfigured |
| 41 |
from apprise.plugins.notifyre import NotifyNotifyre, NotifyreMode |
| 42 |
|
| 43 |
logging.disable(logging.CRITICAL) |
| 44 |
|
| 45 |
# Attachment directory |
| 46 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 47 |
|
| 48 |
# A valid API key used across test cases |
| 49 |
API_KEY = "abcdef1234567890" |
| 50 |
|
| 51 |
# Successful API response |
| 52 |
GOOD_RESPONSE = { |
| 53 |
"success": True, |
| 54 |
"statusCode": 200, |
| 55 |
"payload": {"smsMessageID": "msg-001", "friendlyID": "ABC-001"}, |
| 56 |
"message": "OK", |
| 57 |
"errors": [], |
| 58 |
} |
| 59 |
|
| 60 |
# Failure API response (HTTP 200 but success=false) |
| 61 |
FAIL_RESPONSE = { |
| 62 |
"success": False, |
| 63 |
"statusCode": 400, |
| 64 |
"payload": None, |
| 65 |
"message": "Invalid recipient", |
| 66 |
"errors": ["recipient format error"], |
| 67 |
} |
| 68 |
|
| 69 |
# Our Testing URLs |
| 70 |
apprise_url_tests = ( |
| 71 |
( |
| 72 |
"notifyre://", |
| 73 |
{ |
| 74 |
# No API key |
| 75 |
"instance": AppriseImproperlyConfigured, |
| 76 |
}, |
| 77 |
), |
| 78 |
( |
| 79 |
"notifyre://:@/", |
| 80 |
{ |
| 81 |
# Empty credentials |
| 82 |
"instance": AppriseImproperlyConfigured, |
| 83 |
}, |
| 84 |
), |
| 85 |
( |
| 86 |
"notifyre://{}".format(API_KEY), |
| 87 |
{ |
| 88 |
# No targets -- loads but notify returns False |
| 89 |
"instance": NotifyNotifyre, |
| 90 |
"notify_response": False, |
| 91 |
}, |
| 92 |
), |
| 93 |
( |
| 94 |
"notifyre://{}/+15551234567".format(API_KEY), |
| 95 |
{ |
| 96 |
# Valid SMS notification |
| 97 |
"instance": NotifyNotifyre, |
| 98 |
"privacy_url": "notifyre://a...0/+15551234567/?mode=sms", |
| 99 |
}, |
| 100 |
), |
| 101 |
( |
| 102 |
"notifyre://{}/+15551234567/+15559876543".format(API_KEY), |
| 103 |
{ |
| 104 |
# Multiple targets |
| 105 |
"instance": NotifyNotifyre, |
| 106 |
"privacy_url": ( |
| 107 |
"notifyre://a...0/+15551234567/+15559876543/?mode=sms" |
| 108 |
), |
| 109 |
}, |
| 110 |
), |
| 111 |
( |
| 112 |
"notifyre://{}/+15551234567?mode=sms".format(API_KEY), |
| 113 |
{ |
| 114 |
# Explicit SMS mode |
| 115 |
"instance": NotifyNotifyre, |
| 116 |
}, |
| 117 |
), |
| 118 |
( |
| 119 |
"notifyre://{}/+15551234567?mode=fax".format(API_KEY), |
| 120 |
{ |
| 121 |
# Fax mode |
| 122 |
"instance": NotifyNotifyre, |
| 123 |
"privacy_url": "notifyre://a...0/+15551234567/?mode=fax", |
| 124 |
}, |
| 125 |
), |
| 126 |
( |
| 127 |
"notifyre://{}/+15551234567?mode=fax&from=+15559876543".format( |
| 128 |
API_KEY |
| 129 |
), |
| 130 |
{ |
| 131 |
# Fax mode with source number |
| 132 |
"instance": NotifyNotifyre, |
| 133 |
}, |
| 134 |
), |
| 135 |
( |
| 136 |
"notifyre://{}/+15551234567?mode=invalid".format(API_KEY), |
| 137 |
{ |
| 138 |
# Invalid mode |
| 139 |
"instance": AppriseImproperlyConfigured, |
| 140 |
}, |
| 141 |
), |
| 142 |
( |
| 143 |
"notifyre://{}/+15551234567".format(API_KEY), |
| 144 |
{ |
| 145 |
# HTTP 500 failure |
| 146 |
"instance": NotifyNotifyre, |
| 147 |
"response": False, |
| 148 |
"requests_response_code": requests.codes.internal_server_error, |
| 149 |
}, |
| 150 |
), |
| 151 |
( |
| 152 |
"notifyre://{}/+15551234567".format(API_KEY), |
| 153 |
{ |
| 154 |
# Non-standard HTTP error code |
| 155 |
"instance": NotifyNotifyre, |
| 156 |
"response": False, |
| 157 |
"requests_response_code": 999, |
| 158 |
}, |
| 159 |
), |
| 160 |
( |
| 161 |
"notifyre://{}/+15551234567".format(API_KEY), |
| 162 |
{ |
| 163 |
# RequestException |
| 164 |
"instance": NotifyNotifyre, |
| 165 |
"test_requests_exceptions": True, |
| 166 |
}, |
| 167 |
), |
| 168 |
( |
| 169 |
"notifyre://{}/+15551234567?mode=fax".format(API_KEY), |
| 170 |
{ |
| 171 |
# Fax -- HTTP 500 failure |
| 172 |
"instance": NotifyNotifyre, |
| 173 |
"response": False, |
| 174 |
"requests_response_code": requests.codes.internal_server_error, |
| 175 |
}, |
| 176 |
), |
| 177 |
( |
| 178 |
"notifyre://{}/+15551234567?mode=fax".format(API_KEY), |
| 179 |
{ |
| 180 |
# Fax -- non-standard HTTP error code |
| 181 |
"instance": NotifyNotifyre, |
| 182 |
"response": False, |
| 183 |
"requests_response_code": 999, |
| 184 |
}, |
| 185 |
), |
| 186 |
( |
| 187 |
"notifyre://{}/+15551234567?mode=fax".format(API_KEY), |
| 188 |
{ |
| 189 |
# Fax -- RequestException |
| 190 |
"instance": NotifyNotifyre, |
| 191 |
"test_requests_exceptions": True, |
| 192 |
}, |
| 193 |
), |
| 194 |
( |
| 195 |
"notifyre://{}/+15551234567?campaign=MyCampaign".format(API_KEY), |
| 196 |
{ |
| 197 |
# SMS with custom campaign name |
| 198 |
"instance": NotifyNotifyre, |
| 199 |
}, |
| 200 |
), |
| 201 |
( |
| 202 |
"notifyre://{}/+15551234567?mode=fax&template=MyTpl".format(API_KEY), |
| 203 |
{ |
| 204 |
# Fax with template name |
| 205 |
"instance": NotifyNotifyre, |
| 206 |
}, |
| 207 |
), |
| 208 |
( |
| 209 |
( |
| 210 |
"notifyre://{}/+15551234567?mode=fax&hq=no&ref=CR123&header=CONF" |
| 211 |
).format(API_KEY), |
| 212 |
{ |
| 213 |
# Fax with hq=no, client ref, and header |
| 214 |
"instance": NotifyNotifyre, |
| 215 |
}, |
| 216 |
), |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
def test_plugin_notifyre_urls(mock_post): |
| 221 |
"""AppriseURLTester coverage for all URL entries.""" |
| 222 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 223 |
|
| 224 |
|
| 225 |
test_plugin_notifyre_urls = mock.patch("requests.post")( |
| 226 |
test_plugin_notifyre_urls |
| 227 |
) |
| 228 |
|
| 229 |
|
| 230 |
def test_plugin_notifyre_init(): |
| 231 |
"""Initialization and validation coverage.""" |
| 232 |
|
| 233 |
# Missing API key |
| 234 |
with pytest.raises(AppriseImproperlyConfigured): |
| 235 |
NotifyNotifyre(apikey=None, targets=["+15551234567"]) |
| 236 |
|
| 237 |
# Empty API key |
| 238 |
with pytest.raises(AppriseImproperlyConfigured): |
| 239 |
NotifyNotifyre(apikey="", targets=["+15551234567"]) |
| 240 |
|
| 241 |
# Invalid mode |
| 242 |
with pytest.raises(AppriseImproperlyConfigured): |
| 243 |
NotifyNotifyre( |
| 244 |
apikey=API_KEY, targets=["+15551234567"], mode="invalid" |
| 245 |
) |
| 246 |
|
| 247 |
# Invalid source phone number |
| 248 |
with pytest.raises(AppriseImproperlyConfigured): |
| 249 |
NotifyNotifyre( |
| 250 |
apikey=API_KEY, targets=["+15551234567"], source="not-a-phone" |
| 251 |
) |
| 252 |
|
| 253 |
# Valid initialization -- SMS (default) |
| 254 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"]) |
| 255 |
assert obj.mode == NotifyreMode.SMS |
| 256 |
assert obj.source is None |
| 257 |
assert len(obj.targets) == 1 |
| 258 |
assert obj.body_maxlen == 160 |
| 259 |
|
| 260 |
# New parameter defaults |
| 261 |
assert obj.campaign == obj.app_id |
| 262 |
assert obj.template == "" |
| 263 |
assert obj.ref == "" |
| 264 |
assert obj.hq is True |
| 265 |
assert obj.header == "" |
| 266 |
|
| 267 |
# Custom campaign name |
| 268 |
obj = NotifyNotifyre( |
| 269 |
apikey=API_KEY, targets=["+15551234567"], campaign="MyCampaign" |
| 270 |
) |
| 271 |
assert obj.campaign == "MyCampaign" |
| 272 |
|
| 273 |
# Empty/whitespace campaign falls back to app_id |
| 274 |
obj = NotifyNotifyre( |
| 275 |
apikey=API_KEY, targets=["+15551234567"], campaign=" " |
| 276 |
) |
| 277 |
assert obj.campaign == obj.app_id |
| 278 |
|
| 279 |
# Fax-specific parameters |
| 280 |
obj = NotifyNotifyre( |
| 281 |
apikey=API_KEY, |
| 282 |
targets=["+15551234567"], |
| 283 |
mode="fax", |
| 284 |
template="MyTemplate", |
| 285 |
ref="CR-001", |
| 286 |
hq=False, |
| 287 |
header="CONFIDENTIAL", |
| 288 |
) |
| 289 |
assert obj.template == "MyTemplate" |
| 290 |
assert obj.ref == "CR-001" |
| 291 |
assert obj.hq is False |
| 292 |
assert obj.header == "CONFIDENTIAL" |
| 293 |
|
| 294 |
# hq parsed from string "no" |
| 295 |
obj = NotifyNotifyre( |
| 296 |
apikey=API_KEY, targets=["+15551234567"], mode="fax", hq="no" |
| 297 |
) |
| 298 |
assert obj.hq is False |
| 299 |
|
| 300 |
# hq parsed from string "yes" |
| 301 |
obj = NotifyNotifyre( |
| 302 |
apikey=API_KEY, targets=["+15551234567"], mode="fax", hq="yes" |
| 303 |
) |
| 304 |
assert obj.hq is True |
| 305 |
|
| 306 |
# Valid initialization -- explicit SMS mode |
| 307 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"], mode="sms") |
| 308 |
assert obj.mode == NotifyreMode.SMS |
| 309 |
assert obj.body_maxlen == 160 |
| 310 |
|
| 311 |
# Valid initialization -- fax mode |
| 312 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"], mode="fax") |
| 313 |
assert obj.mode == NotifyreMode.FAX |
| 314 |
assert obj.body_maxlen == 32768 |
| 315 |
|
| 316 |
# Valid initialization -- fax with source |
| 317 |
obj = NotifyNotifyre( |
| 318 |
apikey=API_KEY, |
| 319 |
targets=["+15551234567"], |
| 320 |
mode="fax", |
| 321 |
source="+15559876543", |
| 322 |
) |
| 323 |
assert obj.source == "+15559876543" |
| 324 |
|
| 325 |
# Invalid phone targets are silently dropped |
| 326 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["not-valid", "+15551234567"]) |
| 327 |
assert len(obj.targets) == 1 |
| 328 |
|
| 329 |
# No valid targets -- plugin loads but send() will fail |
| 330 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["not-valid"]) |
| 331 |
assert len(obj.targets) == 0 |
| 332 |
|
| 333 |
|
| 334 |
@mock.patch("requests.post") |
| 335 |
def test_plugin_notifyre_sms_send(mock_post): |
| 336 |
"""SMS send success and failure paths.""" |
| 337 |
|
| 338 |
def _mk_resp(d, code=requests.codes.ok): |
| 339 |
r = mock.Mock() |
| 340 |
r.status_code = code |
| 341 |
r.content = json.dumps(d).encode() |
| 342 |
return r |
| 343 |
|
| 344 |
# Successful send |
| 345 |
mock_post.return_value = _mk_resp(GOOD_RESPONSE) |
| 346 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"]) |
| 347 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is True |
| 348 |
assert mock_post.call_count == 1 |
| 349 |
|
| 350 |
# Verify the correct endpoint was used |
| 351 |
call_url = mock_post.call_args[0][0] |
| 352 |
assert urlparse(call_url).hostname == "api.notifyre.com" |
| 353 |
|
| 354 |
# HTTP error response |
| 355 |
mock_post.reset_mock() |
| 356 |
mock_post.return_value = _mk_resp({}, code=requests.codes.bad_request) |
| 357 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is False |
| 358 |
|
| 359 |
# Unknown HTTP error |
| 360 |
mock_post.reset_mock() |
| 361 |
mock_post.return_value = _mk_resp({}, code=999) |
| 362 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is False |
| 363 |
|
| 364 |
# API-level failure (success=false on HTTP 200) |
| 365 |
mock_post.reset_mock() |
| 366 |
mock_post.return_value = _mk_resp(FAIL_RESPONSE) |
| 367 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is False |
| 368 |
|
| 369 |
# Unparseable response body on HTTP 200 is treated as success |
| 370 |
mock_post.reset_mock() |
| 371 |
r = mock.Mock() |
| 372 |
r.status_code = requests.codes.ok |
| 373 |
r.content = b"not-json" |
| 374 |
mock_post.return_value = r |
| 375 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is True |
| 376 |
|
| 377 |
# RequestException |
| 378 |
mock_post.reset_mock() |
| 379 |
mock_post.side_effect = requests.RequestException("connection refused") |
| 380 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is False |
| 381 |
|
| 382 |
|
| 383 |
@mock.patch("requests.post") |
| 384 |
def test_plugin_notifyre_sms_no_targets(mock_post): |
| 385 |
"""SMS send with no valid targets returns False without HTTP call.""" |
| 386 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["not-valid"]) |
| 387 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is False |
| 388 |
assert mock_post.call_count == 0 |
| 389 |
|
| 390 |
|
| 391 |
@mock.patch("requests.post") |
| 392 |
def test_plugin_notifyre_sms_with_source(mock_post): |
| 393 |
"""SMS send includes the source (from) number in the payload.""" |
| 394 |
|
| 395 |
def _mk_resp(d, code=requests.codes.ok): |
| 396 |
r = mock.Mock() |
| 397 |
r.status_code = code |
| 398 |
r.content = json.dumps(d).encode() |
| 399 |
return r |
| 400 |
|
| 401 |
mock_post.return_value = _mk_resp(GOOD_RESPONSE) |
| 402 |
obj = NotifyNotifyre( |
| 403 |
apikey=API_KEY, |
| 404 |
targets=["+15551234567"], |
| 405 |
source="+15559876543", |
| 406 |
) |
| 407 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is True |
| 408 |
|
| 409 |
# Verify 'from' was included in the posted payload |
| 410 |
posted = json.loads(mock_post.call_args[1]["data"]) |
| 411 |
assert posted["from"] == "+15559876543" |
| 412 |
|
| 413 |
|
| 414 |
@mock.patch("requests.post") |
| 415 |
def test_plugin_notifyre_sms_attachment_warning(mock_post): |
| 416 |
"""SMS mode warns when attachments are provided but still sends.""" |
| 417 |
|
| 418 |
def _mk_resp(d, code=requests.codes.ok): |
| 419 |
r = mock.Mock() |
| 420 |
r.status_code = code |
| 421 |
r.content = json.dumps(d).encode() |
| 422 |
return r |
| 423 |
|
| 424 |
mock_post.return_value = _mk_resp(GOOD_RESPONSE) |
| 425 |
|
| 426 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"]) |
| 427 |
attach = AppriseAttachment() |
| 428 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png")) |
| 429 |
|
| 430 |
# Should warn but still succeed (attachments are ignored in SMS mode) |
| 431 |
assert ( |
| 432 |
bool( |
| 433 |
obj.notify( |
| 434 |
body="Hello", |
| 435 |
notify_type=NotifyType.INFO, |
| 436 |
attach=attach, |
| 437 |
) |
| 438 |
) |
| 439 |
is True |
| 440 |
) |
| 441 |
assert mock_post.call_count == 1 |
| 442 |
|
| 443 |
|
| 444 |
@mock.patch("requests.post") |
| 445 |
def test_plugin_notifyre_fax_send(mock_post): |
| 446 |
"""Fax send success and failure paths.""" |
| 447 |
|
| 448 |
def _mk_resp(d, code=requests.codes.ok): |
| 449 |
r = mock.Mock() |
| 450 |
r.status_code = code |
| 451 |
r.content = json.dumps(d).encode() |
| 452 |
return r |
| 453 |
|
| 454 |
# Successful send (body text as only document) |
| 455 |
mock_post.return_value = _mk_resp(GOOD_RESPONSE) |
| 456 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"], mode="fax") |
| 457 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is True |
| 458 |
assert mock_post.call_count == 1 |
| 459 |
|
| 460 |
call_url = mock_post.call_args[0][0] |
| 461 |
assert urlparse(call_url).hostname == "api.notifyre.com" |
| 462 |
|
| 463 |
# HTTP error |
| 464 |
mock_post.reset_mock() |
| 465 |
mock_post.return_value = _mk_resp({}, code=requests.codes.bad_request) |
| 466 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is False |
| 467 |
|
| 468 |
# Unknown HTTP error |
| 469 |
mock_post.reset_mock() |
| 470 |
mock_post.return_value = _mk_resp({}, code=999) |
| 471 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is False |
| 472 |
|
| 473 |
# API-level failure (success=false on HTTP 200) |
| 474 |
mock_post.reset_mock() |
| 475 |
mock_post.return_value = _mk_resp(FAIL_RESPONSE) |
| 476 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is False |
| 477 |
|
| 478 |
# Unparseable JSON on HTTP 200 -- treated as success |
| 479 |
mock_post.reset_mock() |
| 480 |
r = mock.Mock() |
| 481 |
r.status_code = requests.codes.ok |
| 482 |
r.content = b"not-json" |
| 483 |
mock_post.return_value = r |
| 484 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is True |
| 485 |
|
| 486 |
# RequestException |
| 487 |
mock_post.reset_mock() |
| 488 |
mock_post.side_effect = requests.RequestException("timeout") |
| 489 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is False |
| 490 |
|
| 491 |
|
| 492 |
@mock.patch("requests.post") |
| 493 |
def test_plugin_notifyre_fax_attachments(mock_post): |
| 494 |
"""Fax attachment handling -- all guard branches.""" |
| 495 |
|
| 496 |
def _mk_resp(d, code=requests.codes.ok): |
| 497 |
r = mock.Mock() |
| 498 |
r.status_code = code |
| 499 |
r.content = json.dumps(d).encode() |
| 500 |
return r |
| 501 |
|
| 502 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"], mode="fax") |
| 503 |
|
| 504 |
# Successful send with a valid attachment |
| 505 |
mock_post.return_value = _mk_resp(GOOD_RESPONSE) |
| 506 |
attach = AppriseAttachment() |
| 507 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png")) |
| 508 |
assert ( |
| 509 |
bool( |
| 510 |
obj.notify( |
| 511 |
body="Fax body", |
| 512 |
notify_type=NotifyType.INFO, |
| 513 |
attach=attach, |
| 514 |
) |
| 515 |
) |
| 516 |
is True |
| 517 |
) |
| 518 |
assert mock_post.call_count == 1 |
| 519 |
|
| 520 |
# Inaccessible file -- Guard 1 |
| 521 |
mock_post.reset_mock() |
| 522 |
attach = AppriseAttachment() |
| 523 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png")) |
| 524 |
with mock.patch("os.path.isfile", return_value=False): |
| 525 |
assert ( |
| 526 |
bool( |
| 527 |
obj.notify( |
| 528 |
body="Fax body", |
| 529 |
notify_type=NotifyType.INFO, |
| 530 |
attach=attach, |
| 531 |
) |
| 532 |
) |
| 533 |
is False |
| 534 |
) |
| 535 |
assert mock_post.call_count == 0 |
| 536 |
|
| 537 |
# OSError on open -- Guard 2 |
| 538 |
mock_post.reset_mock() |
| 539 |
attach = AppriseAttachment() |
| 540 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png")) |
| 541 |
with mock.patch("builtins.open", side_effect=OSError("perm denied")): |
| 542 |
assert ( |
| 543 |
bool( |
| 544 |
obj.notify( |
| 545 |
body="Fax body", |
| 546 |
notify_type=NotifyType.INFO, |
| 547 |
attach=attach, |
| 548 |
) |
| 549 |
) |
| 550 |
is False |
| 551 |
) |
| 552 |
assert mock_post.call_count == 0 |
| 553 |
|
| 554 |
# OSError on read -- handle opened but read fails |
| 555 |
mock_post.reset_mock() |
| 556 |
attach = AppriseAttachment() |
| 557 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png")) |
| 558 |
mock_fh = mock.MagicMock() |
| 559 |
mock_fh.__enter__ = mock.Mock(return_value=mock_fh) |
| 560 |
mock_fh.__exit__ = mock.Mock(return_value=False) |
| 561 |
mock_fh.read.side_effect = OSError("read error") |
| 562 |
with mock.patch("builtins.open", return_value=mock_fh): |
| 563 |
assert ( |
| 564 |
bool( |
| 565 |
obj.notify( |
| 566 |
body="Fax body", |
| 567 |
notify_type=NotifyType.INFO, |
| 568 |
attach=attach, |
| 569 |
) |
| 570 |
) |
| 571 |
is False |
| 572 |
) |
| 573 |
assert mock_post.call_count == 0 |
| 574 |
|
| 575 |
# HTTP error on the attachment POST |
| 576 |
mock_post.reset_mock() |
| 577 |
mock_post.side_effect = None |
| 578 |
mock_post.return_value = _mk_resp({}, code=requests.codes.bad_request) |
| 579 |
attach = AppriseAttachment() |
| 580 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png")) |
| 581 |
assert ( |
| 582 |
bool( |
| 583 |
obj.notify( |
| 584 |
body="Fax body", |
| 585 |
notify_type=NotifyType.INFO, |
| 586 |
attach=attach, |
| 587 |
) |
| 588 |
) |
| 589 |
is False |
| 590 |
) |
| 591 |
|
| 592 |
# RequestException on the attachment POST |
| 593 |
mock_post.reset_mock() |
| 594 |
mock_post.side_effect = requests.RequestException("timeout") |
| 595 |
attach = AppriseAttachment() |
| 596 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png")) |
| 597 |
assert ( |
| 598 |
bool( |
| 599 |
obj.notify( |
| 600 |
body="Fax body", |
| 601 |
notify_type=NotifyType.INFO, |
| 602 |
attach=attach, |
| 603 |
) |
| 604 |
) |
| 605 |
is False |
| 606 |
) |
| 607 |
|
| 608 |
# Multiple attachments -- partial failure closes opened handles |
| 609 |
mock_post.reset_mock() |
| 610 |
mock_post.side_effect = None |
| 611 |
mock_post.return_value = _mk_resp(GOOD_RESPONSE) |
| 612 |
attach = AppriseAttachment() |
| 613 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png")) |
| 614 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 615 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.jpeg")) |
| 616 |
assert ( |
| 617 |
bool( |
| 618 |
obj.notify( |
| 619 |
body="Multi-page fax", |
| 620 |
notify_type=NotifyType.INFO, |
| 621 |
attach=attach, |
| 622 |
) |
| 623 |
) |
| 624 |
is True |
| 625 |
) |
| 626 |
|
| 627 |
|
| 628 |
@mock.patch("requests.post") |
| 629 |
def test_plugin_notifyre_fax_no_content(mock_post): |
| 630 |
"""Fax with empty body and no attachments returns False.""" |
| 631 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"], mode="fax") |
| 632 |
# Simulate empty body (post title-merge the framework would send "") |
| 633 |
result = obj._send_fax("") |
| 634 |
assert result is False |
| 635 |
assert mock_post.call_count == 0 |
| 636 |
|
| 637 |
|
| 638 |
def test_plugin_notifyre_body_maxlen(): |
| 639 |
"""body_maxlen property returns the correct limit per mode.""" |
| 640 |
obj_sms = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"]) |
| 641 |
assert obj_sms.body_maxlen == 160 |
| 642 |
|
| 643 |
obj_fax = NotifyNotifyre( |
| 644 |
apikey=API_KEY, targets=["+15551234567"], mode="fax" |
| 645 |
) |
| 646 |
assert obj_fax.body_maxlen == 32768 |
| 647 |
|
| 648 |
|
| 649 |
def test_plugin_notifyre_url_round_trip(): |
| 650 |
"""url() -> parse_url() -> re-instantiate produces an equivalent object.""" |
| 651 |
|
| 652 |
# SMS mode |
| 653 |
obj1 = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"]) |
| 654 |
result = NotifyNotifyre.parse_url(obj1.url()) |
| 655 |
obj2 = NotifyNotifyre(**result) |
| 656 |
assert obj1.url_identifier == obj2.url_identifier |
| 657 |
assert len(obj1.targets) == len(obj2.targets) |
| 658 |
|
| 659 |
# Fax mode with source |
| 660 |
obj1 = NotifyNotifyre( |
| 661 |
apikey=API_KEY, |
| 662 |
targets=["+15551234567", "+15559876543"], |
| 663 |
mode="fax", |
| 664 |
source="+15550000001", |
| 665 |
) |
| 666 |
result = NotifyNotifyre.parse_url(obj1.url()) |
| 667 |
obj2 = NotifyNotifyre(**result) |
| 668 |
assert obj1.url_identifier == obj2.url_identifier |
| 669 |
assert len(obj1.targets) == len(obj2.targets) |
| 670 |
|
| 671 |
# Fax mode with all optional parameters |
| 672 |
obj1 = NotifyNotifyre( |
| 673 |
apikey=API_KEY, |
| 674 |
targets=["+15551234567"], |
| 675 |
mode="fax", |
| 676 |
campaign="MyCampaign", |
| 677 |
template="MyTemplate", |
| 678 |
ref="CR-001", |
| 679 |
hq=False, |
| 680 |
header="CONFIDENTIAL", |
| 681 |
) |
| 682 |
result = NotifyNotifyre.parse_url(obj1.url()) |
| 683 |
obj2 = NotifyNotifyre(**result) |
| 684 |
assert obj2.campaign == "MyCampaign" |
| 685 |
assert obj2.template == "MyTemplate" |
| 686 |
assert obj2.ref == "CR-001" |
| 687 |
assert obj2.hq is False |
| 688 |
assert obj2.header == "CONFIDENTIAL" |
| 689 |
|
| 690 |
# SMS mode with custom campaign -- campaign appears in URL |
| 691 |
obj1 = NotifyNotifyre( |
| 692 |
apikey=API_KEY, |
| 693 |
targets=["+15551234567"], |
| 694 |
campaign="SmsCampaign", |
| 695 |
) |
| 696 |
url = obj1.url() |
| 697 |
assert "campaign=SmsCampaign" in url |
| 698 |
result = NotifyNotifyre.parse_url(url) |
| 699 |
obj2 = NotifyNotifyre(**result) |
| 700 |
assert obj2.campaign == "SmsCampaign" |
| 701 |
|
| 702 |
# Default campaign (== app_id) is NOT included in the URL |
| 703 |
obj1 = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"]) |
| 704 |
assert "campaign" not in obj1.url() |
| 705 |
|
| 706 |
|
| 707 |
def test_plugin_notifyre_url_parsing(): |
| 708 |
"""parse_url() handles all supported query-parameter forms.""" |
| 709 |
|
| 710 |
# Basic SMS URL |
| 711 |
result = NotifyNotifyre.parse_url( |
| 712 |
"notifyre://{}/+15551234567".format(API_KEY) |
| 713 |
) |
| 714 |
assert result["apikey"] == API_KEY |
| 715 |
assert "+15551234567" in result["targets"] |
| 716 |
|
| 717 |
# ?to= parameter appends additional targets |
| 718 |
result = NotifyNotifyre.parse_url( |
| 719 |
"notifyre://{}/?to=+15551234567".format(API_KEY) |
| 720 |
) |
| 721 |
assert "+15551234567" in result["targets"] |
| 722 |
|
| 723 |
# ?mode=fax |
| 724 |
result = NotifyNotifyre.parse_url( |
| 725 |
"notifyre://{}/+15551234567?mode=fax".format(API_KEY) |
| 726 |
) |
| 727 |
assert result["mode"] == "fax" |
| 728 |
|
| 729 |
# ?from= source number |
| 730 |
result = NotifyNotifyre.parse_url( |
| 731 |
"notifyre://{}/+15551234567?from=%2B15559876543".format(API_KEY) |
| 732 |
) |
| 733 |
assert result["source"] == "+15559876543" |
| 734 |
|
| 735 |
# ?campaign= custom campaign name |
| 736 |
result = NotifyNotifyre.parse_url( |
| 737 |
"notifyre://{}/+15551234567?campaign=MyCampaign".format(API_KEY) |
| 738 |
) |
| 739 |
assert result["campaign"] == "MyCampaign" |
| 740 |
|
| 741 |
# ?template= fax template name |
| 742 |
result = NotifyNotifyre.parse_url( |
| 743 |
"notifyre://{}/+15551234567?template=MyTpl".format(API_KEY) |
| 744 |
) |
| 745 |
assert result["template"] == "MyTpl" |
| 746 |
|
| 747 |
# ?ref= client reference |
| 748 |
result = NotifyNotifyre.parse_url( |
| 749 |
"notifyre://{}/+15551234567?ref=CR-001".format(API_KEY) |
| 750 |
) |
| 751 |
assert result["ref"] == "CR-001" |
| 752 |
|
| 753 |
# ?hq=no -> False |
| 754 |
result = NotifyNotifyre.parse_url( |
| 755 |
"notifyre://{}/+15551234567?hq=no".format(API_KEY) |
| 756 |
) |
| 757 |
assert result["hq"] is False |
| 758 |
|
| 759 |
# ?hq=yes -> True |
| 760 |
result = NotifyNotifyre.parse_url( |
| 761 |
"notifyre://{}/+15551234567?hq=yes".format(API_KEY) |
| 762 |
) |
| 763 |
assert result["hq"] is True |
| 764 |
|
| 765 |
# ?header= fax cover page header |
| 766 |
result = NotifyNotifyre.parse_url( |
| 767 |
"notifyre://{}/+15551234567?header=CONF".format(API_KEY) |
| 768 |
) |
| 769 |
assert result["header"] == "CONF" |
| 770 |
|
| 771 |
# A URL with a different schema is not matched by parse_native_url |
| 772 |
assert NotifyNotifyre.parse_native_url("https://notifyre.com/") is None |
| 773 |
|
| 774 |
|
| 775 |
def test_plugin_notifyre_privacy_url(): |
| 776 |
"""Privacy URL masks the API key.""" |
| 777 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"]) |
| 778 |
priv = obj.url(privacy=True) |
| 779 |
# API key should be masked |
| 780 |
assert API_KEY not in priv |
| 781 |
# Targets should remain visible |
| 782 |
assert "+15551234567" in priv |
| 783 |
|
| 784 |
|
| 785 |
@mock.patch("requests.post") |
| 786 |
def test_plugin_notifyre_sms_campaign(mock_post): |
| 787 |
"""campaign parameter appears in the SMS API payload.""" |
| 788 |
|
| 789 |
def _mk_resp(d, code=requests.codes.ok): |
| 790 |
r = mock.Mock() |
| 791 |
r.status_code = code |
| 792 |
r.content = json.dumps(d).encode() |
| 793 |
return r |
| 794 |
|
| 795 |
mock_post.return_value = _mk_resp(GOOD_RESPONSE) |
| 796 |
|
| 797 |
# Custom campaign name |
| 798 |
obj = NotifyNotifyre( |
| 799 |
apikey=API_KEY, |
| 800 |
targets=["+15551234567"], |
| 801 |
campaign="MyCampaign", |
| 802 |
) |
| 803 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is True |
| 804 |
posted = json.loads(mock_post.call_args[1]["data"]) |
| 805 |
assert posted["campaignName"] == "MyCampaign" |
| 806 |
|
| 807 |
# Default campaign = app_id |
| 808 |
mock_post.reset_mock() |
| 809 |
mock_post.return_value = _mk_resp(GOOD_RESPONSE) |
| 810 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"]) |
| 811 |
assert bool(obj.notify(body="Hello", notify_type=NotifyType.INFO)) is True |
| 812 |
posted = json.loads(mock_post.call_args[1]["data"]) |
| 813 |
assert posted["campaignName"] == obj.app_id |
| 814 |
|
| 815 |
|
| 816 |
@mock.patch("requests.post") |
| 817 |
def test_plugin_notifyre_fax_params(mock_post): |
| 818 |
"""template, ref, hq, header, and campaign appear in the fax payload.""" |
| 819 |
|
| 820 |
def _mk_resp(d, code=requests.codes.ok): |
| 821 |
r = mock.Mock() |
| 822 |
r.status_code = code |
| 823 |
r.content = json.dumps(d).encode() |
| 824 |
return r |
| 825 |
|
| 826 |
mock_post.return_value = _mk_resp(GOOD_RESPONSE) |
| 827 |
|
| 828 |
obj = NotifyNotifyre( |
| 829 |
apikey=API_KEY, |
| 830 |
targets=["+15551234567"], |
| 831 |
mode="fax", |
| 832 |
campaign="FaxCampaign", |
| 833 |
template="CoverTpl", |
| 834 |
ref="CR-999", |
| 835 |
hq=False, |
| 836 |
header="CONFIDENTIAL", |
| 837 |
) |
| 838 |
assert ( |
| 839 |
bool(obj.notify(body="Fax body", notify_type=NotifyType.INFO)) is True |
| 840 |
) |
| 841 |
posted = json.loads(mock_post.call_args[1]["data"]) |
| 842 |
assert posted["campaignName"] == "FaxCampaign" |
| 843 |
assert posted["templateName"] == "CoverTpl" |
| 844 |
assert posted["clientReference"] == "CR-999" |
| 845 |
assert posted["isHighQuality"] is False |
| 846 |
assert posted["header"] == "CONFIDENTIAL" |
| 847 |
|
| 848 |
# subject is the full body without truncation |
| 849 |
assert posted["subject"] == "Fax body" |
| 850 |
|
| 851 |
# Defaults: hq=True, empty strings for template/ref/header |
| 852 |
mock_post.reset_mock() |
| 853 |
mock_post.return_value = _mk_resp(GOOD_RESPONSE) |
| 854 |
obj = NotifyNotifyre(apikey=API_KEY, targets=["+15551234567"], mode="fax") |
| 855 |
assert bool(obj.notify(body="Test", notify_type=NotifyType.INFO)) is True |
| 856 |
posted = json.loads(mock_post.call_args[1]["data"]) |
| 857 |
assert posted["isHighQuality"] is True |
| 858 |
assert posted["templateName"] == "" |
| 859 |
assert posted["clientReference"] == "" |
| 860 |
assert posted["header"] == "" |
| 861 |
assert posted["campaignName"] == obj.app_id |
| 862 |
|
| 863 |
|
| 864 |
@mock.patch("requests.post") |
| 865 |
def test_plugin_notifyre_apprise_integration(mock_post): |
| 866 |
"""Apprise.add() + Apprise.notify() integration for both modes.""" |
| 867 |
|
| 868 |
def _mk_resp(d, code=requests.codes.ok): |
| 869 |
r = mock.Mock() |
| 870 |
r.status_code = code |
| 871 |
r.content = json.dumps(d).encode() |
| 872 |
return r |
| 873 |
|
| 874 |
mock_post.return_value = _mk_resp(GOOD_RESPONSE) |
| 875 |
|
| 876 |
# SMS via string URL |
| 877 |
app = Apprise() |
| 878 |
assert app.add("notifyre://{}/+15551234567".format(API_KEY)) |
| 879 |
assert bool(app.notify(title="Title", body="Body")) is True |
| 880 |
|
| 881 |
# Fax via string URL |
| 882 |
app = Apprise() |
| 883 |
assert app.add("notifyre://{}/+15551234567?mode=fax".format(API_KEY)) |
| 884 |
assert bool(app.notify(title="Title", body="Body")) is True |