| 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 logging |
| 30 |
import os |
| 31 |
from unittest import mock |
| 32 |
|
| 33 |
from helpers import AppriseURLTester |
| 34 |
import pytest |
| 35 |
import requests |
| 36 |
|
| 37 |
from apprise import Apprise, AppriseAttachment, NotifyType |
| 38 |
from apprise.plugins.postmark import NotifyPostmark |
| 39 |
|
| 40 |
logging.disable(logging.CRITICAL) |
| 41 |
|
| 42 |
|
| 43 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 44 |
|
| 45 |
|
| 46 |
apprise_url_tests = ( |
| 47 |
( |
| 48 |
"postmark://", |
| 49 |
{ |
| 50 |
|
| 51 |
"instance": TypeError, |
| 52 |
}, |
| 53 |
), |
| 54 |
( |
| 55 |
"postmark://:@/", |
| 56 |
{ |
| 57 |
|
| 58 |
"instance": TypeError, |
| 59 |
}, |
| 60 |
), |
| 61 |
( |
| 62 |
"postmark://abcd", |
| 63 |
{ |
| 64 |
|
| 65 |
"instance": TypeError, |
| 66 |
}, |
| 67 |
), |
| 68 |
( |
| 69 |
"postmark://abcd@host", |
| 70 |
{ |
| 71 |
|
| 72 |
"instance": TypeError, |
| 73 |
}, |
| 74 |
), |
| 75 |
( |
| 76 |
"postmark://abcd:[email protected]", |
| 77 |
{ |
| 78 |
|
| 79 |
"instance": NotifyPostmark, |
| 80 |
}, |
| 81 |
), |
| 82 |
( |
| 83 |
"postmark://abcd:[email protected]?format=text", |
| 84 |
{ |
| 85 |
|
| 86 |
"instance": NotifyPostmark, |
| 87 |
}, |
| 88 |
), |
| 89 |
( |
| 90 |
"postmark://abcd:[email protected]/[email protected]", |
| 91 |
{ |
| 92 |
|
| 93 |
"instance": NotifyPostmark, |
| 94 |
"force_debug": True, |
| 95 |
}, |
| 96 |
), |
| 97 |
( |
| 98 |
"postmark://abcd:[email protected]/[email protected]?name=John", |
| 99 |
{ |
| 100 |
|
| 101 |
"instance": NotifyPostmark, |
| 102 |
"privacy_url": ( |
| 103 |
"postmark://a...d:[email protected]/[email protected]" |
| 104 |
), |
| 105 |
"url_matches": r"name=John", |
| 106 |
}, |
| 107 |
), |
| 108 |
( |
| 109 |
( |
| 110 |
"postmark://abcd:[email protected]/[email protected]" |
| 111 |
"[email protected]" |
| 112 |
), |
| 113 |
{ |
| 114 |
|
| 115 |
"instance": NotifyPostmark, |
| 116 |
"url_matches": r"reply=", |
| 117 |
}, |
| 118 |
), |
| 119 |
( |
| 120 |
( |
| 121 |
"postmark://abcd:[email protected]/[email protected]" |
| 122 |
"[email protected]" |
| 123 |
), |
| 124 |
{ |
| 125 |
|
| 126 |
"instance": NotifyPostmark, |
| 127 |
}, |
| 128 |
), |
| 129 |
( |
| 130 |
( |
| 131 |
"postmark://abcd:[email protected]/[email protected]" |
| 132 |
"[email protected]" |
| 133 |
), |
| 134 |
{ |
| 135 |
|
| 136 |
"instance": NotifyPostmark, |
| 137 |
}, |
| 138 |
), |
| 139 |
( |
| 140 |
( |
| 141 |
"postmark://abcd:[email protected]/[email protected]" |
| 142 |
"?cc=Chris<[email protected]>" |
| 143 |
), |
| 144 |
{ |
| 145 |
|
| 146 |
"instance": NotifyPostmark, |
| 147 |
}, |
| 148 |
), |
| 149 |
( |
| 150 |
( |
| 151 |
"postmark://abcd:[email protected]/[email protected]" |
| 152 |
"[email protected]" |
| 153 |
), |
| 154 |
{ |
| 155 |
|
| 156 |
"instance": NotifyPostmark, |
| 157 |
}, |
| 158 |
), |
| 159 |
( |
| 160 |
"postmark://abcd:[email protected]/bademailaddress", |
| 161 |
{ |
| 162 |
|
| 163 |
"instance": NotifyPostmark, |
| 164 |
"notify_response": False, |
| 165 |
}, |
| 166 |
), |
| 167 |
( |
| 168 |
"postmark://abcd:[email protected]/[email protected]", |
| 169 |
{ |
| 170 |
"instance": NotifyPostmark, |
| 171 |
|
| 172 |
"response": False, |
| 173 |
"requests_response_code": requests.codes.internal_server_error, |
| 174 |
}, |
| 175 |
), |
| 176 |
( |
| 177 |
"postmark://abcd:[email protected]/[email protected]", |
| 178 |
{ |
| 179 |
"instance": NotifyPostmark, |
| 180 |
|
| 181 |
"response": False, |
| 182 |
"requests_response_code": 999, |
| 183 |
}, |
| 184 |
), |
| 185 |
( |
| 186 |
"postmark://abcd:[email protected]/[email protected]", |
| 187 |
{ |
| 188 |
"instance": NotifyPostmark, |
| 189 |
|
| 190 |
"test_requests_exceptions": True, |
| 191 |
}, |
| 192 |
), |
| 193 |
) |
| 194 |
|
| 195 |
|
| 196 |
def test_plugin_postmark_urls(): |
| 197 |
"""NotifyPostmark() Apprise URLs.""" |
| 198 |
|
| 199 |
|
| 200 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 201 |
|
| 202 |
|
| 203 |
@mock.patch("requests.get") |
| 204 |
@mock.patch("requests.post") |
| 205 |
def test_plugin_postmark_edge_cases(mock_post, mock_get): |
| 206 |
"""NotifyPostmark() Edge Cases.""" |
| 207 |
|
| 208 |
|
| 209 |
with pytest.raises(TypeError): |
| 210 |
NotifyPostmark(apikey=None, from_email="[email protected]") |
| 211 |
|
| 212 |
|
| 213 |
with pytest.raises(TypeError): |
| 214 |
NotifyPostmark(apikey="abcd", from_email="!invalid") |
| 215 |
|
| 216 |
|
| 217 |
with pytest.raises(TypeError): |
| 218 |
NotifyPostmark(apikey="abcd", from_email=None) |
| 219 |
|
| 220 |
|
| 221 |
obj = NotifyPostmark( |
| 222 |
apikey="abcd", |
| 223 |
from_email="[email protected]", |
| 224 |
targets="!invalid", |
| 225 |
) |
| 226 |
|
| 227 |
assert isinstance(obj, NotifyPostmark) |
| 228 |
|
| 229 |
|
| 230 |
assert isinstance( |
| 231 |
NotifyPostmark( |
| 232 |
apikey="abcd", |
| 233 |
from_email="[email protected]", |
| 234 |
bcc=("[email protected]", "!invalid"), |
| 235 |
cc=("[email protected]", "!invalid"), |
| 236 |
), |
| 237 |
NotifyPostmark, |
| 238 |
) |
| 239 |
|
| 240 |
|
| 241 |
assert isinstance( |
| 242 |
NotifyPostmark( |
| 243 |
apikey="abcd", |
| 244 |
from_email="[email protected]", |
| 245 |
reply_to=("[email protected]", "!invalid"), |
| 246 |
), |
| 247 |
NotifyPostmark, |
| 248 |
) |
| 249 |
|
| 250 |
|
| 251 |
@mock.patch("requests.get") |
| 252 |
@mock.patch("requests.post") |
| 253 |
def test_plugin_postmark_send(mock_post, mock_get): |
| 254 |
"""NotifyPostmark() Send.""" |
| 255 |
|
| 256 |
def _mk_resp(code=requests.codes.ok): |
| 257 |
r = mock.Mock() |
| 258 |
r.status_code = code |
| 259 |
r.content = b"" |
| 260 |
return r |
| 261 |
|
| 262 |
mock_post.return_value = _mk_resp() |
| 263 |
mock_get.return_value = _mk_resp() |
| 264 |
|
| 265 |
|
| 266 |
obj = Apprise.instantiate("postmark://abcd:[email protected]") |
| 267 |
assert isinstance(obj, NotifyPostmark) |
| 268 |
assert obj.notify(body="body", title="title") is True |
| 269 |
|
| 270 |
|
| 271 |
assert mock_post.call_count == 1 |
| 272 |
call_kwargs = mock_post.call_args |
| 273 |
import json |
| 274 |
|
| 275 |
posted = json.loads(call_kwargs[1]["data"]) |
| 276 |
assert posted["To"] == "[email protected]" |
| 277 |
assert "X-Postmark-Server-Token" in call_kwargs[1]["headers"] |
| 278 |
|
| 279 |
mock_post.reset_mock() |
| 280 |
|
| 281 |
|
| 282 |
obj = Apprise.instantiate( |
| 283 |
"postmark://abcd:[email protected]/[email protected]" |
| 284 |
) |
| 285 |
assert obj.notify(body="body", title="title") is True |
| 286 |
posted = json.loads(mock_post.call_args[1]["data"]) |
| 287 |
assert posted["To"] == "[email protected]" |
| 288 |
mock_post.reset_mock() |
| 289 |
|
| 290 |
|
| 291 |
obj = NotifyPostmark( |
| 292 |
apikey="abcd", |
| 293 |
from_email="[email protected]", |
| 294 |
targets=["[email protected]"], |
| 295 |
cc=["[email protected]"], |
| 296 |
bcc=["[email protected]"], |
| 297 |
) |
| 298 |
assert obj.notify(body="body", title="title") is True |
| 299 |
posted = json.loads(mock_post.call_args[1]["data"]) |
| 300 |
assert "Cc" in posted |
| 301 |
assert "Bcc" in posted |
| 302 |
mock_post.reset_mock() |
| 303 |
|
| 304 |
|
| 305 |
obj = NotifyPostmark( |
| 306 |
apikey="abcd", |
| 307 |
from_email="[email protected]", |
| 308 |
reply_to=["[email protected]"], |
| 309 |
) |
| 310 |
assert obj.notify(body="body", title="title") is True |
| 311 |
posted = json.loads(mock_post.call_args[1]["data"]) |
| 312 |
assert "ReplyTo" in posted |
| 313 |
mock_post.reset_mock() |
| 314 |
|
| 315 |
|
| 316 |
obj = NotifyPostmark( |
| 317 |
apikey="abcd", |
| 318 |
from_email="[email protected]", |
| 319 |
) |
| 320 |
assert obj.notify(body="body", title="") is True |
| 321 |
posted = json.loads(mock_post.call_args[1]["data"]) |
| 322 |
assert posted["Subject"] == NotifyPostmark.default_empty_subject |
| 323 |
mock_post.reset_mock() |
| 324 |
|
| 325 |
|
| 326 |
obj = NotifyPostmark( |
| 327 |
apikey="abcd", |
| 328 |
from_email="[email protected]", |
| 329 |
) |
| 330 |
from apprise.common import NotifyFormat |
| 331 |
|
| 332 |
obj.notify_format = NotifyFormat.TEXT |
| 333 |
assert obj.notify(body="body", title="title") is True |
| 334 |
posted = json.loads(mock_post.call_args[1]["data"]) |
| 335 |
assert "TextBody" in posted |
| 336 |
assert "HtmlBody" not in posted |
| 337 |
mock_post.reset_mock() |
| 338 |
|
| 339 |
|
| 340 |
mock_post.return_value = _mk_resp(requests.codes.internal_server_error) |
| 341 |
obj = Apprise.instantiate( |
| 342 |
"postmark://abcd:[email protected]/[email protected]" |
| 343 |
) |
| 344 |
assert obj.notify(body="body", title="title") is False |
| 345 |
mock_post.reset_mock() |
| 346 |
|
| 347 |
|
| 348 |
mock_post.return_value = _mk_resp(999) |
| 349 |
assert obj.notify(body="body", title="title") is False |
| 350 |
mock_post.reset_mock() |
| 351 |
|
| 352 |
|
| 353 |
mock_post.side_effect = requests.RequestException("Connection failed") |
| 354 |
assert obj.notify(body="body", title="title") is False |
| 355 |
mock_post.side_effect = None |
| 356 |
mock_post.reset_mock() |
| 357 |
|
| 358 |
|
| 359 |
obj = NotifyPostmark( |
| 360 |
apikey="abcd", |
| 361 |
from_email="[email protected]", |
| 362 |
targets=["!invalid"], |
| 363 |
) |
| 364 |
mock_post.return_value = _mk_resp() |
| 365 |
assert obj.notify(body="body", title="title") is False |
| 366 |
assert mock_post.call_count == 0 |
| 367 |
|
| 368 |
|
| 369 |
mock_post.reset_mock() |
| 370 |
responses = [_mk_resp(), _mk_resp(requests.codes.bad_request)] |
| 371 |
mock_post.side_effect = responses |
| 372 |
obj = NotifyPostmark( |
| 373 |
apikey="abcd", |
| 374 |
from_email="[email protected]", |
| 375 |
targets=["[email protected]", "[email protected]"], |
| 376 |
) |
| 377 |
assert obj.notify(body="body", title="title") is False |
| 378 |
assert mock_post.call_count == 2 |
| 379 |
mock_post.side_effect = None |
| 380 |
|
| 381 |
|
| 382 |
@mock.patch("requests.post") |
| 383 |
def test_plugin_postmark_attachments(mock_post): |
| 384 |
"""NotifyPostmark() Attachments.""" |
| 385 |
|
| 386 |
def _mk_resp(code=requests.codes.ok): |
| 387 |
r = mock.Mock() |
| 388 |
r.status_code = code |
| 389 |
r.content = b"" |
| 390 |
return r |
| 391 |
|
| 392 |
mock_post.return_value = _mk_resp() |
| 393 |
|
| 394 |
|
| 395 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 396 |
attach = AppriseAttachment(path) |
| 397 |
obj = Apprise.instantiate("postmark://abcd:[email protected]") |
| 398 |
assert isinstance(obj, NotifyPostmark) |
| 399 |
assert ( |
| 400 |
obj.notify( |
| 401 |
body="body", |
| 402 |
title="title", |
| 403 |
notify_type=NotifyType.INFO, |
| 404 |
attach=attach, |
| 405 |
) |
| 406 |
is True |
| 407 |
) |
| 408 |
|
| 409 |
import json |
| 410 |
|
| 411 |
posted = json.loads(mock_post.call_args[1]["data"]) |
| 412 |
assert "Attachments" in posted |
| 413 |
assert len(posted["Attachments"]) == 1 |
| 414 |
assert posted["Attachments"][0]["Name"] == "apprise-test.gif" |
| 415 |
mock_post.reset_mock() |
| 416 |
|
| 417 |
|
| 418 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 419 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png")) |
| 420 |
assert ( |
| 421 |
obj.notify( |
| 422 |
body="body", |
| 423 |
title="title", |
| 424 |
notify_type=NotifyType.INFO, |
| 425 |
attach=attach, |
| 426 |
) |
| 427 |
is True |
| 428 |
) |
| 429 |
posted = json.loads(mock_post.call_args[1]["data"]) |
| 430 |
assert len(posted["Attachments"]) == 2 |
| 431 |
mock_post.reset_mock() |
| 432 |
|
| 433 |
|
| 434 |
with mock.patch("os.path.isfile", return_value=False): |
| 435 |
assert ( |
| 436 |
obj.notify( |
| 437 |
body="body", |
| 438 |
title="title", |
| 439 |
notify_type=NotifyType.INFO, |
| 440 |
attach=AppriseAttachment( |
| 441 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 442 |
), |
| 443 |
) |
| 444 |
is False |
| 445 |
) |
| 446 |
|
| 447 |
|
| 448 |
with mock.patch("builtins.open", side_effect=OSError): |
| 449 |
assert ( |
| 450 |
obj.notify( |
| 451 |
body="body", |
| 452 |
title="title", |
| 453 |
notify_type=NotifyType.INFO, |
| 454 |
attach=AppriseAttachment( |
| 455 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 456 |
), |
| 457 |
) |
| 458 |
is False |
| 459 |
) |
| 460 |
|
| 461 |
|
| 462 |
@mock.patch("requests.post") |
| 463 |
def test_plugin_postmark_url_parsing(mock_post): |
| 464 |
"""NotifyPostmark() URL parsing and round-trip.""" |
| 465 |
|
| 466 |
def _mk_resp(code=requests.codes.ok): |
| 467 |
r = mock.Mock() |
| 468 |
r.status_code = code |
| 469 |
r.content = b"" |
| 470 |
return r |
| 471 |
|
| 472 |
mock_post.return_value = _mk_resp() |
| 473 |
|
| 474 |
|
| 475 |
obj = NotifyPostmark( |
| 476 |
apikey="mytoken", |
| 477 |
from_email="[email protected]", |
| 478 |
) |
| 479 |
url = obj.url() |
| 480 |
result = NotifyPostmark.parse_url(url) |
| 481 |
assert result is not None |
| 482 |
obj2 = NotifyPostmark(**result) |
| 483 |
assert obj.url_identifier == obj2.url_identifier |
| 484 |
|
| 485 |
|
| 486 |
obj = NotifyPostmark( |
| 487 |
apikey="mytoken", |
| 488 |
from_email="[email protected]", |
| 489 |
targets=["[email protected]"], |
| 490 |
) |
| 491 |
url = obj.url() |
| 492 |
result = NotifyPostmark.parse_url(url) |
| 493 |
assert result is not None |
| 494 |
obj2 = NotifyPostmark(**result) |
| 495 |
assert len(obj.targets) == len(obj2.targets) |
| 496 |
|
| 497 |
|
| 498 |
obj = NotifyPostmark( |
| 499 |
apikey="mytoken", |
| 500 |
from_email="[email protected]", |
| 501 |
from_name="Alice", |
| 502 |
) |
| 503 |
url = obj.url() |
| 504 |
assert "name=Alice" in url |
| 505 |
result = NotifyPostmark.parse_url(url) |
| 506 |
assert result is not None |
| 507 |
obj2 = NotifyPostmark(**result) |
| 508 |
assert obj2.from_addr[0] == "Alice" |
| 509 |
|
| 510 |
|
| 511 |
obj = NotifyPostmark( |
| 512 |
apikey="mytoken", |
| 513 |
from_email="[email protected]", |
| 514 |
targets=["[email protected]"], |
| 515 |
cc=["[email protected]"], |
| 516 |
bcc=["[email protected]"], |
| 517 |
) |
| 518 |
url = obj.url() |
| 519 |
result = NotifyPostmark.parse_url(url) |
| 520 |
assert result is not None |
| 521 |
obj2 = NotifyPostmark(**result) |
| 522 |
assert "[email protected]" in obj2.cc |
| 523 |
assert "[email protected]" in obj2.bcc |
| 524 |
|
| 525 |
|
| 526 |
obj = NotifyPostmark( |
| 527 |
apikey="mytoken", |
| 528 |
from_email="[email protected]", |
| 529 |
reply_to=["[email protected]"], |
| 530 |
) |
| 531 |
url = obj.url() |
| 532 |
result = NotifyPostmark.parse_url(url) |
| 533 |
assert result is not None |
| 534 |
obj2 = NotifyPostmark(**result) |
| 535 |
assert "[email protected]" in obj2.reply_to |
| 536 |
|
| 537 |
|
| 538 |
assert NotifyPostmark.parse_url(None) is None |
| 539 |
|
| 540 |
|
| 541 |
result = NotifyPostmark.parse_url( |
| 542 |
"postmark://?apikey=overridekey&[email protected]" |
| 543 |
"&[email protected]" |
| 544 |
) |
| 545 |
assert result is not None |
| 546 |
assert result["apikey"] == "overridekey" |
| 547 |
|
| 548 |
|
| 549 |
result = NotifyPostmark.parse_url( |
| 550 |
"postmark://[email protected]/[email protected][email protected]" |
| 551 |
) |
| 552 |
assert result is not None |
| 553 |
assert result["from_email"] == "[email protected]" |
| 554 |
|
| 555 |
|
| 556 |
obj = NotifyPostmark( |
| 557 |
apikey="secrettoken", |
| 558 |
from_email="[email protected]", |
| 559 |
) |
| 560 |
privacy = obj.url(privacy=True) |
| 561 |
assert "secrettoken" not in privacy |
| 562 |
assert "s...n" in privacy or "..." in privacy |
| 563 |
|
| 564 |
|
| 565 |
obj = NotifyPostmark( |
| 566 |
apikey="mytoken", |
| 567 |
from_email="[email protected]", |
| 568 |
targets=["[email protected]", "[email protected]"], |
| 569 |
) |
| 570 |
assert len(obj) == 2 |
| 571 |
|
| 572 |
|
| 573 |
obj1 = NotifyPostmark( |
| 574 |
apikey="tok", |
| 575 |
from_email="[email protected]", |
| 576 |
) |
| 577 |
obj2 = NotifyPostmark( |
| 578 |
apikey="tok", |
| 579 |
from_email="[email protected]", |
| 580 |
targets=["[email protected]"], |
| 581 |
) |
| 582 |
assert obj1.url_identifier == obj2.url_identifier |