| 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 |
from json import dumps |
| 29 |
|
| 30 |
# Disable logging for a cleaner testing output |
| 31 |
import logging |
| 32 |
import os |
| 33 |
from unittest import mock |
| 34 |
|
| 35 |
from helpers import AppriseURLTester |
| 36 |
import pytest |
| 37 |
import requests |
| 38 |
|
| 39 |
from apprise import Apprise, AppriseAttachment, NotifyType |
| 40 |
from apprise.exception import AppriseImproperlyConfigured |
| 41 |
from apprise.plugins.sparkpost import NotifySparkPost |
| 42 |
|
| 43 |
logging.disable(logging.CRITICAL) |
| 44 |
|
| 45 |
# Attachment Directory |
| 46 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 47 |
|
| 48 |
# Our Testing URLs |
| 49 |
apprise_url_tests = ( |
| 50 |
( |
| 51 |
"sparkpost://", |
| 52 |
{ |
| 53 |
"instance": AppriseImproperlyConfigured, |
| 54 |
}, |
| 55 |
), |
| 56 |
( |
| 57 |
"sparkpost://:@/", |
| 58 |
{ |
| 59 |
"instance": AppriseImproperlyConfigured, |
| 60 |
}, |
| 61 |
), |
| 62 |
# No Token specified |
| 63 |
( |
| 64 |
"sparkpost://[email protected]", |
| 65 |
{ |
| 66 |
"instance": AppriseImproperlyConfigured, |
| 67 |
}, |
| 68 |
), |
| 69 |
# Token is valid, but no user name specified |
| 70 |
( |
| 71 |
"sparkpost://localhost.localdomain/{}".format("a" * 32), |
| 72 |
{ |
| 73 |
"instance": AppriseImproperlyConfigured, |
| 74 |
}, |
| 75 |
), |
| 76 |
# Invalid from email address |
| 77 |
( |
| 78 |
'sparkpost://"@localhost.localdomain/{}'.format("b" * 32), |
| 79 |
{ |
| 80 |
"instance": AppriseImproperlyConfigured, |
| 81 |
}, |
| 82 |
), |
| 83 |
# No To email address, but everything else is valid |
| 84 |
( |
| 85 |
"sparkpost://[email protected]/{}".format("c" * 32), |
| 86 |
{ |
| 87 |
"instance": NotifySparkPost, |
| 88 |
"requests_response_text": { |
| 89 |
"results": { |
| 90 |
"total_rejected_recipients": 0, |
| 91 |
"total_accepted_recipients": 1, |
| 92 |
"id": "11668787484950529", |
| 93 |
} |
| 94 |
}, |
| 95 |
}, |
| 96 |
), |
| 97 |
( |
| 98 |
"sparkpost://[email protected]/{}?format=markdown".format( |
| 99 |
"d" * 32 |
| 100 |
), |
| 101 |
{ |
| 102 |
"instance": NotifySparkPost, |
| 103 |
"requests_response_text": { |
| 104 |
"results": { |
| 105 |
"total_rejected_recipients": 0, |
| 106 |
"total_accepted_recipients": 1, |
| 107 |
"id": "11668787484950529", |
| 108 |
} |
| 109 |
}, |
| 110 |
}, |
| 111 |
), |
| 112 |
( |
| 113 |
"sparkpost://[email protected]/{}?format=html".format( |
| 114 |
"d" * 32 |
| 115 |
), |
| 116 |
{ |
| 117 |
"instance": NotifySparkPost, |
| 118 |
"requests_response_text": { |
| 119 |
"results": { |
| 120 |
"total_rejected_recipients": 0, |
| 121 |
"total_accepted_recipients": 1, |
| 122 |
"id": "11668787484950529", |
| 123 |
} |
| 124 |
}, |
| 125 |
"force_debug": True, |
| 126 |
}, |
| 127 |
), |
| 128 |
( |
| 129 |
"sparkpost://[email protected]/{}?format=text".format( |
| 130 |
"d" * 32 |
| 131 |
), |
| 132 |
{ |
| 133 |
"instance": NotifySparkPost, |
| 134 |
"requests_response_text": { |
| 135 |
"results": { |
| 136 |
"total_rejected_recipients": 0, |
| 137 |
"total_accepted_recipients": 1, |
| 138 |
"id": "11668787484950529", |
| 139 |
} |
| 140 |
}, |
| 141 |
}, |
| 142 |
), |
| 143 |
# valid url with region specified (case insensitve) |
| 144 |
( |
| 145 |
"sparkpost://[email protected]/{}?region=uS".format("d" * 32), |
| 146 |
{ |
| 147 |
"instance": NotifySparkPost, |
| 148 |
"requests_response_text": { |
| 149 |
"results": { |
| 150 |
"total_rejected_recipients": 0, |
| 151 |
"total_accepted_recipients": 1, |
| 152 |
"id": "11668787484950529", |
| 153 |
} |
| 154 |
}, |
| 155 |
}, |
| 156 |
), |
| 157 |
# valid url with region specified (case insensitve) |
| 158 |
( |
| 159 |
"sparkpost://[email protected]/{}?region=EU".format("e" * 32), |
| 160 |
{ |
| 161 |
"instance": NotifySparkPost, |
| 162 |
"requests_response_text": { |
| 163 |
"results": { |
| 164 |
"total_rejected_recipients": 0, |
| 165 |
"total_accepted_recipients": 1, |
| 166 |
"id": "11668787484950529", |
| 167 |
} |
| 168 |
}, |
| 169 |
}, |
| 170 |
), |
| 171 |
# headers |
| 172 |
( |
| 173 |
"sparkpost://[email protected]/{}" |
| 174 |
"?+X-Customer-Campaign-ID=Apprise".format("f" * 32), |
| 175 |
{ |
| 176 |
"instance": NotifySparkPost, |
| 177 |
"requests_response_text": { |
| 178 |
"results": { |
| 179 |
"total_rejected_recipients": 0, |
| 180 |
"total_accepted_recipients": 1, |
| 181 |
"id": "11668787484950529", |
| 182 |
} |
| 183 |
}, |
| 184 |
}, |
| 185 |
), |
| 186 |
# template tokens |
| 187 |
( |
| 188 |
"sparkpost://[email protected]/{}" |
| 189 |
"?:name=Chris&:status=admin".format("g" * 32), |
| 190 |
{ |
| 191 |
"instance": NotifySparkPost, |
| 192 |
"requests_response_text": { |
| 193 |
"results": { |
| 194 |
"total_rejected_recipients": 0, |
| 195 |
"total_accepted_recipients": 1, |
| 196 |
"id": "11668787484950529", |
| 197 |
} |
| 198 |
}, |
| 199 |
}, |
| 200 |
), |
| 201 |
# bcc and cc |
| 202 |
( |
| 203 |
"sparkpost://[email protected]/{}" |
| 204 |
"[email protected]&[email protected]".format("h" * 32), |
| 205 |
{ |
| 206 |
"instance": NotifySparkPost, |
| 207 |
"requests_response_text": { |
| 208 |
"results": { |
| 209 |
"total_rejected_recipients": 0, |
| 210 |
"total_accepted_recipients": 1, |
| 211 |
"id": "11668787484950529", |
| 212 |
} |
| 213 |
}, |
| 214 |
}, |
| 215 |
), |
| 216 |
# invalid url with region specified (case insensitve) |
| 217 |
( |
| 218 |
"sparkpost://[email protected]/{}?region=invalid".format( |
| 219 |
"a" * 32 |
| 220 |
), |
| 221 |
{ |
| 222 |
"instance": AppriseImproperlyConfigured, |
| 223 |
}, |
| 224 |
), |
| 225 |
# One 'To' Email address |
| 226 |
( |
| 227 |
"sparkpost://[email protected]/{}/[email protected]".format( |
| 228 |
"a" * 32 |
| 229 |
), |
| 230 |
{ |
| 231 |
"instance": NotifySparkPost, |
| 232 |
"requests_response_text": { |
| 233 |
"results": { |
| 234 |
"total_rejected_recipients": 0, |
| 235 |
"total_accepted_recipients": 1, |
| 236 |
"id": "11668787484950529", |
| 237 |
} |
| 238 |
}, |
| 239 |
}, |
| 240 |
), |
| 241 |
# Invalid 'To' Email address |
| 242 |
( |
| 243 |
"sparkpost://[email protected]/{}/invalid".format("i" * 32), |
| 244 |
{ |
| 245 |
"instance": NotifySparkPost, |
| 246 |
# Expected notify() response |
| 247 |
"notify_response": False, |
| 248 |
}, |
| 249 |
), |
| 250 |
# Multiple 'To', 'Cc', and 'Bcc' addresses (with invalid ones) |
| 251 |
( |
| 252 |
"sparkpost://[email protected]/{}/{}?bcc={}&cc={}".format( |
| 253 |
"j" * 32, |
| 254 |
"/".join( |
| 255 |
("[email protected]", "invalid", "User2:[email protected]") |
| 256 |
), |
| 257 |
",".join(("[email protected]", "i@v", "User1:[email protected]")), |
| 258 |
",".join(("[email protected]", "g@r@b", "Da:[email protected]")), |
| 259 |
), |
| 260 |
{ |
| 261 |
"instance": NotifySparkPost, |
| 262 |
"requests_response_text": { |
| 263 |
"results": { |
| 264 |
"total_rejected_recipients": 0, |
| 265 |
"total_accepted_recipients": 1, |
| 266 |
"id": "11668787484950529", |
| 267 |
} |
| 268 |
}, |
| 269 |
}, |
| 270 |
), |
| 271 |
( |
| 272 |
"sparkpost://[email protected]/{}[email protected]".format( |
| 273 |
"k" * 32 |
| 274 |
), |
| 275 |
{ |
| 276 |
"instance": NotifySparkPost, |
| 277 |
"requests_response_text": { |
| 278 |
"results": { |
| 279 |
"total_rejected_recipients": 0, |
| 280 |
"total_accepted_recipients": 1, |
| 281 |
"id": "11668787484950529", |
| 282 |
} |
| 283 |
}, |
| 284 |
}, |
| 285 |
), |
| 286 |
# One To Email address, a from name specified too |
| 287 |
( |
| 288 |
"sparkpost://[email protected]/{}/" |
| 289 |
'[email protected]?name="Frodo"'.format("l" * 32), |
| 290 |
{ |
| 291 |
"instance": NotifySparkPost, |
| 292 |
"requests_response_text": { |
| 293 |
"results": { |
| 294 |
"total_rejected_recipients": 0, |
| 295 |
"total_accepted_recipients": 1, |
| 296 |
"id": "11668787484950529", |
| 297 |
} |
| 298 |
}, |
| 299 |
}, |
| 300 |
), |
| 301 |
# Test invalid JSON response |
| 302 |
( |
| 303 |
"sparkpost://[email protected]/{}".format("m" * 32), |
| 304 |
{ |
| 305 |
"instance": NotifySparkPost, |
| 306 |
"requests_response_text": "{", |
| 307 |
}, |
| 308 |
), |
| 309 |
( |
| 310 |
"sparkpost://[email protected]/{}".format("n" * 32), |
| 311 |
{ |
| 312 |
"instance": NotifySparkPost, |
| 313 |
# force a failure |
| 314 |
"response": False, |
| 315 |
"requests_response_code": requests.codes.internal_server_error, |
| 316 |
}, |
| 317 |
), |
| 318 |
( |
| 319 |
"sparkpost://[email protected]/{}".format("o" * 32), |
| 320 |
{ |
| 321 |
"instance": NotifySparkPost, |
| 322 |
# throw a bizarre code forcing us to fail to look it up |
| 323 |
"response": False, |
| 324 |
"requests_response_code": 999, |
| 325 |
}, |
| 326 |
), |
| 327 |
( |
| 328 |
"sparkpost://[email protected]/{}".format("p" * 32), |
| 329 |
{ |
| 330 |
"instance": NotifySparkPost, |
| 331 |
# Throws a series of i/o exceptions with this flag |
| 332 |
# is set and tests that we gracefully handle them |
| 333 |
"test_requests_exceptions": True, |
| 334 |
}, |
| 335 |
), |
| 336 |
) |
| 337 |
|
| 338 |
|
| 339 |
def test_plugin_sparkpost_urls(): |
| 340 |
"""NotifySparkPost() Apprise URLs.""" |
| 341 |
|
| 342 |
# Run our general tests |
| 343 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 344 |
|
| 345 |
|
| 346 |
@mock.patch("requests.post") |
| 347 |
def test_plugin_sparkpost_throttling(mock_post): |
| 348 |
"""NotifySparkPost() Throttling.""" |
| 349 |
|
| 350 |
NotifySparkPost.sparkpost_retry_wait_sec = 0.1 |
| 351 |
NotifySparkPost.sparkpost_retry_attempts = 3 |
| 352 |
|
| 353 |
# API Key |
| 354 |
apikey = "abc123" |
| 355 |
user = "user" |
| 356 |
host = "example.com" |
| 357 |
targets = f"{user}@{host}" |
| 358 |
|
| 359 |
# Exception should be thrown about the fact no user was specified |
| 360 |
with pytest.raises(AppriseImproperlyConfigured): |
| 361 |
NotifySparkPost(apikey=apikey, targets=targets, host=host) |
| 362 |
|
| 363 |
# Exception should be thrown about the fact no private key was specified |
| 364 |
with pytest.raises(AppriseImproperlyConfigured): |
| 365 |
NotifySparkPost(apikey=None, targets=targets, user=user, host=host) |
| 366 |
|
| 367 |
okay_response = requests.Request() |
| 368 |
okay_response.status_code = requests.codes.ok |
| 369 |
okay_response.content = dumps( |
| 370 |
{ |
| 371 |
"results": { |
| 372 |
"total_rejected_recipients": 0, |
| 373 |
"total_accepted_recipients": 1, |
| 374 |
"id": "11668787484950529", |
| 375 |
} |
| 376 |
} |
| 377 |
) |
| 378 |
|
| 379 |
retry_response = requests.Request() |
| 380 |
retry_response.status_code = requests.codes.too_many_requests |
| 381 |
retry_response.content = dumps( |
| 382 |
{ |
| 383 |
"errors": [ |
| 384 |
{ |
| 385 |
"description": ( |
| 386 |
"Unconfigured or unverified sending domain." |
| 387 |
), |
| 388 |
"code": "7001", |
| 389 |
"message": "Invalid domain", |
| 390 |
} |
| 391 |
] |
| 392 |
} |
| 393 |
) |
| 394 |
|
| 395 |
# Prepare Mock (force 2 retry responses and then one okay) |
| 396 |
mock_post.side_effect = (retry_response, retry_response, okay_response) |
| 397 |
|
| 398 |
obj = Apprise.instantiate( |
| 399 |
f"sparkpost://[email protected]/{apikey}" |
| 400 |
) |
| 401 |
assert isinstance(obj, NotifySparkPost) |
| 402 |
|
| 403 |
# We'll successfully perform the notification as we're within |
| 404 |
# our retry limit |
| 405 |
assert bool(obj.notify("test")) is True |
| 406 |
|
| 407 |
mock_post.reset_mock() |
| 408 |
mock_post.side_effect = (retry_response, retry_response, retry_response) |
| 409 |
|
| 410 |
# Now we are less than our expected limit check so we will fail |
| 411 |
assert bool(obj.notify("test")) is False |
| 412 |
|
| 413 |
|
| 414 |
@mock.patch("requests.post") |
| 415 |
def test_plugin_sparkpost_attachments(mock_post): |
| 416 |
"""NotifySparkPost() Attachments.""" |
| 417 |
NotifySparkPost.sparkpost_retry_wait_sec = 0.1 |
| 418 |
NotifySparkPost.sparkpost_retry_attempts = 3 |
| 419 |
|
| 420 |
okay_response = requests.Request() |
| 421 |
okay_response.status_code = requests.codes.ok |
| 422 |
okay_response.content = dumps( |
| 423 |
{ |
| 424 |
"results": { |
| 425 |
"total_rejected_recipients": 0, |
| 426 |
"total_accepted_recipients": 1, |
| 427 |
"id": "11668787484950529", |
| 428 |
} |
| 429 |
} |
| 430 |
) |
| 431 |
|
| 432 |
# Assign our mock object our return value |
| 433 |
mock_post.return_value = okay_response |
| 434 |
|
| 435 |
# API Key |
| 436 |
apikey = "abc123" |
| 437 |
|
| 438 |
obj = Apprise.instantiate( |
| 439 |
f"sparkpost://[email protected]/{apikey}" |
| 440 |
) |
| 441 |
assert isinstance(obj, NotifySparkPost) |
| 442 |
|
| 443 |
# Test Valid Attachment |
| 444 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 445 |
attach = AppriseAttachment(path) |
| 446 |
assert ( |
| 447 |
bool( |
| 448 |
obj.notify( |
| 449 |
body="body", |
| 450 |
title="title", |
| 451 |
notify_type=NotifyType.INFO, |
| 452 |
attach=attach, |
| 453 |
) |
| 454 |
) |
| 455 |
is True |
| 456 |
) |
| 457 |
|
| 458 |
# Test invalid attachment |
| 459 |
path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 460 |
assert ( |
| 461 |
bool( |
| 462 |
obj.notify( |
| 463 |
body="body", |
| 464 |
title="title", |
| 465 |
notify_type=NotifyType.INFO, |
| 466 |
attach=path, |
| 467 |
) |
| 468 |
) |
| 469 |
is False |
| 470 |
) |
| 471 |
|
| 472 |
with mock.patch("base64.b64encode", side_effect=OSError()): |
| 473 |
# We can't send the message if we fail to parse the data |
| 474 |
assert ( |
| 475 |
bool( |
| 476 |
obj.notify( |
| 477 |
body="body", |
| 478 |
title="title", |
| 479 |
notify_type=NotifyType.INFO, |
| 480 |
attach=attach, |
| 481 |
) |
| 482 |
) |
| 483 |
is False |
| 484 |
) |
| 485 |
|
| 486 |
obj = Apprise.instantiate( |
| 487 |
f"sparkpost://[email protected]/{apikey}/" |
| 488 |
"[email protected]/[email protected]?batch=yes" |
| 489 |
) |
| 490 |
assert isinstance(obj, NotifySparkPost) |
| 491 |
|
| 492 |
# As a batch mode, both emails can be lumped into 1 |
| 493 |
assert len(obj) == 1 |
| 494 |
|
| 495 |
# Force our batch to break into separate messages |
| 496 |
obj.default_batch_size = 1 |
| 497 |
|
| 498 |
# We'll send 2 messages no |
| 499 |
assert len(obj) == 2 |
| 500 |
mock_post.reset_mock() |
| 501 |
|
| 502 |
assert ( |
| 503 |
bool( |
| 504 |
obj.notify( |
| 505 |
body="body", |
| 506 |
title="title", |
| 507 |
notify_type=NotifyType.INFO, |
| 508 |
attach=attach, |
| 509 |
) |
| 510 |
) |
| 511 |
is True |
| 512 |
) |
| 513 |
assert mock_post.call_count == 2 |
| 514 |
|
| 515 |
# single batch |
| 516 |
mock_post.reset_mock() |
| 517 |
# We'll send 1 message |
| 518 |
obj.default_batch_size = 2 |
| 519 |
|
| 520 |
assert ( |
| 521 |
bool( |
| 522 |
obj.notify( |
| 523 |
body="body", |
| 524 |
title="title", |
| 525 |
notify_type=NotifyType.INFO, |
| 526 |
attach=attach, |
| 527 |
) |
| 528 |
) |
| 529 |
is True |
| 530 |
) |
| 531 |
assert mock_post.call_count == 1 |
| 532 |
|
| 533 |
|
| 534 |
def test_plugin_sparkpost_cc_bcc_invalid_branch(): |
| 535 |
"""NotifySparkPost() CC/BCC validation: invalid entries are dropped.""" |
| 536 |
|
| 537 |
# Build a NotifySparkPost with: |
| 538 |
# cc = one valid address + one that parse_emails passes through but |
| 539 |
# is_email rejects (hits the FALSE branch in the CC loop). |
| 540 |
# A list must be used -- parse_emails filters invalid tokens out |
| 541 |
# of comma-separated strings before is_email is ever called. |
| 542 |
# bcc = same, for the FALSE branch in the BCC loop |
| 543 |
obj = NotifySparkPost( |
| 544 |
apikey="abc123", |
| 545 |
targets=["[email protected]"], |
| 546 |
cc=["[email protected]", "notanemail"], |
| 547 |
bcc=["[email protected]", "alsonotanemail"], |
| 548 |
user="user", |
| 549 |
host="example.com", |
| 550 |
) |
| 551 |
|
| 552 |
# Valid CC entry was added; invalid one was silently dropped |
| 553 |
assert "[email protected]" in obj.cc |
| 554 |
assert len(obj.cc) == 1 |
| 555 |
|
| 556 |
# Valid BCC entry was added; invalid one was silently dropped |
| 557 |
assert "[email protected]" in obj.bcc |
| 558 |
assert len(obj.bcc) == 1 |
| 559 |
|
| 560 |
|
| 561 |
def test_plugin_sparkpost_cc_names(): |
| 562 |
"""NotifySparkPost() url() keeps the display names of CC addresses.""" |
| 563 |
obj = Apprise.instantiate( |
| 564 |
"sparkpost://[email protected]/apikey/[email protected]" |
| 565 |
"?cc=Chris<[email protected]>,[email protected]" |
| 566 |
) |
| 567 |
assert isinstance(obj, NotifySparkPost) |
| 568 |
assert obj.names["[email protected]"] == "Chris" |
| 569 |
assert obj.names["[email protected]"] is False |
| 570 |
|
| 571 |
url = obj.url() |
| 572 |
assert "Chris%3Al2g%40nuxref.com" in url |
| 573 |
|
| 574 |
# The names survive a round trip through url() |
| 575 |
obj2 = Apprise.instantiate(url) |
| 576 |
assert isinstance(obj2, NotifySparkPost) |
| 577 |
assert obj2.cc == obj.cc |
| 578 |
assert obj2.names["[email protected]"] == "Chris" |
| 579 |
assert obj2.names["[email protected]"] is False |