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