| 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, loads |
| 29 |
|
| 30 |
# Disable logging for a cleaner testing output |
| 31 |
import logging |
| 32 |
from unittest import mock |
| 33 |
|
| 34 |
from helpers import AppriseURLTester |
| 35 |
import pytest |
| 36 |
import requests |
| 37 |
|
| 38 |
from apprise import Apprise, NotifyType |
| 39 |
from apprise.exception import AppriseImproperlyConfigured |
| 40 |
from apprise.plugins.pingram import NotifyPingram |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
PINGRAM_GOOD_RESPONSE = dumps({"trackingId": "abc123"}) |
| 45 |
|
| 46 |
PINGRAM_BAD_RESPONSE = "{" |
| 47 |
|
| 48 |
# Our Testing URLs |
| 49 |
apprise_url_tests = ( |
| 50 |
( |
| 51 |
"pingram://", |
| 52 |
{ |
| 53 |
# No API Key at all |
| 54 |
"instance": AppriseImproperlyConfigured, |
| 55 |
}, |
| 56 |
), |
| 57 |
( |
| 58 |
"pingram://:@/", |
| 59 |
{ |
| 60 |
"instance": AppriseImproperlyConfigured, |
| 61 |
}, |
| 62 |
), |
| 63 |
( |
| 64 |
"pingram://abcd", |
| 65 |
{ |
| 66 |
# Doesn't match the pingram_(sk|pk)_ prefix |
| 67 |
"instance": AppriseImproperlyConfigured, |
| 68 |
}, |
| 69 |
), |
| 70 |
( |
| 71 |
"pingram://pingram_sk_key/+15551235553/?mode=invalid", |
| 72 |
{ |
| 73 |
# Invalid mode |
| 74 |
"instance": AppriseImproperlyConfigured, |
| 75 |
}, |
| 76 |
), |
| 77 |
( |
| 78 |
"pingram://pingram_sk_key/+15551235553/?region=invalid", |
| 79 |
{ |
| 80 |
# Invalid region |
| 81 |
"instance": AppriseImproperlyConfigured, |
| 82 |
}, |
| 83 |
), |
| 84 |
( |
| 85 |
"pingram://pingram_sk_key/+15551235553/?type=*(", |
| 86 |
{ |
| 87 |
# Invalid type |
| 88 |
"instance": AppriseImproperlyConfigured, |
| 89 |
}, |
| 90 |
), |
| 91 |
( |
| 92 |
"pingram://pingram_sk_key/+15551235553/?channels=bad", |
| 93 |
{ |
| 94 |
# Invalid channel |
| 95 |
"instance": AppriseImproperlyConfigured, |
| 96 |
}, |
| 97 |
), |
| 98 |
( |
| 99 |
"pingram://pingram_sk_abc123/g@rb@ge/+15551235553/", |
| 100 |
{ |
| 101 |
# g@rb@ge entry ignored (invalid target, dropped with a |
| 102 |
# warning, preserved for round-trip only) |
| 103 |
"instance": NotifyPingram, |
| 104 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 105 |
}, |
| 106 |
), |
| 107 |
( |
| 108 |
"pingram://pingram_sk_abc123/g@rb@ge/", |
| 109 |
{ |
| 110 |
# Only an invalid target specified; nothing to notify |
| 111 |
"instance": NotifyPingram, |
| 112 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 113 |
"notify_response": False, |
| 114 |
}, |
| 115 |
), |
| 116 |
( |
| 117 |
"pingram://pingram_sk_abc123/[email protected]/[email protected]", |
| 118 |
{ |
| 119 |
# Two emails back to back with no id between them each |
| 120 |
# become their own recipient |
| 121 |
"instance": NotifyPingram, |
| 122 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 123 |
}, |
| 124 |
), |
| 125 |
( |
| 126 |
"pingram://pingram_sk_abc123/+15551235553", |
| 127 |
{ |
| 128 |
# A bare phone number with no id is valid |
| 129 |
"instance": NotifyPingram, |
| 130 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 131 |
}, |
| 132 |
), |
| 133 |
( |
| 134 |
"pingram://pingram_pk_abc123/[email protected]", |
| 135 |
{ |
| 136 |
# A bare email with no id is valid; public key prefix works |
| 137 |
"instance": NotifyPingram, |
| 138 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 139 |
}, |
| 140 |
), |
| 141 |
( |
| 142 |
"pingram://pingram_sk_aaaa.bbbb.cccc/+15551235553", |
| 143 |
{ |
| 144 |
# Newer keys embed a JWT after the prefix, so the key itself |
| 145 |
# contains periods |
| 146 |
"instance": NotifyPingram, |
| 147 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 148 |
}, |
| 149 |
), |
| 150 |
( |
| 151 |
"pingram://pingram_pk_aaaa.bbbb.cccc-dd_ee/[email protected]", |
| 152 |
{ |
| 153 |
# JWTs are base64url encoded, so - and _ show up alongside the |
| 154 |
# period delimiters |
| 155 |
"instance": NotifyPingram, |
| 156 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 157 |
}, |
| 158 |
), |
| 159 |
( |
| 160 |
"pingram://pingram_sk_abc123/+15551235553/+15551235554", |
| 161 |
{ |
| 162 |
# Multiple id-less phone numbers become separate targets |
| 163 |
"instance": NotifyPingram, |
| 164 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 165 |
}, |
| 166 |
), |
| 167 |
( |
| 168 |
"pingram://pingram_sk_abc123/id/+15551235553", |
| 169 |
{ |
| 170 |
# An id can still optionally be supplied |
| 171 |
"instance": NotifyPingram, |
| 172 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 173 |
}, |
| 174 |
), |
| 175 |
( |
| 176 |
"pingram://pingram_sk_abc123/id/id2/[email protected]", |
| 177 |
{ |
| 178 |
# two ids in a row; first is dropped in favor of the second |
| 179 |
"instance": NotifyPingram, |
| 180 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 181 |
}, |
| 182 |
), |
| 183 |
( |
| 184 |
( |
| 185 |
"pingram://type@pingram_sk_abc123/id10/[email protected]/" |
| 186 |
"id5/+15551235555/id8/+15551235534" |
| 187 |
"?reply=Chris<[email protected]>" |
| 188 |
), |
| 189 |
{ |
| 190 |
"instance": NotifyPingram, |
| 191 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 192 |
}, |
| 193 |
), |
| 194 |
( |
| 195 |
( |
| 196 |
"pingram://type@pingram_sk_abc123/abc1/[email protected]/" |
| 197 |
"id5/+15551235555/?from=Chris&reply=Christopher" |
| 198 |
), |
| 199 |
{ |
| 200 |
"instance": NotifyPingram, |
| 201 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 202 |
}, |
| 203 |
), |
| 204 |
( |
| 205 |
( |
| 206 |
"pingram://type@pingram_sk_abc123/[email protected]/" |
| 207 |
"[email protected]&[email protected]" |
| 208 |
), |
| 209 |
{ |
| 210 |
# Set from/source |
| 211 |
"instance": NotifyPingram, |
| 212 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 213 |
}, |
| 214 |
), |
| 215 |
( |
| 216 |
( |
| 217 |
"pingram://type@pingram_sk_abc123/[email protected]/" |
| 218 |
"[email protected]&[email protected]&[email protected]" |
| 219 |
), |
| 220 |
{ |
| 221 |
"instance": NotifyPingram, |
| 222 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 223 |
# Our expected url(privacy=True) startswith() response: |
| 224 |
"privacy_url": "pingram://[email protected]/", |
| 225 |
}, |
| 226 |
), |
| 227 |
( |
| 228 |
"pingram://?apikey=pingram_sk_abc123&to=id,[email protected]" |
| 229 |
"&type=typec", |
| 230 |
{ |
| 231 |
# use just kwargs |
| 232 |
"instance": NotifyPingram, |
| 233 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 234 |
"privacy_url": "pingram://[email protected]/", |
| 235 |
}, |
| 236 |
), |
| 237 |
( |
| 238 |
"pingram://pingram_sk_abc123?to=id,[email protected]&type=typeb", |
| 239 |
{ |
| 240 |
# apikey is pulled from the host |
| 241 |
"instance": NotifyPingram, |
| 242 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 243 |
"privacy_url": "pingram://[email protected]/", |
| 244 |
}, |
| 245 |
), |
| 246 |
( |
| 247 |
"pingram://?apikey=pingram_sk_abc123&type=test-type®ion=eu", |
| 248 |
{ |
| 249 |
# No targets specified |
| 250 |
"instance": NotifyPingram, |
| 251 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 252 |
"notify_response": False, |
| 253 |
}, |
| 254 |
), |
| 255 |
( |
| 256 |
"pingram://?apikey=pingram_sk_abc123&to=id,[email protected]" |
| 257 |
"&type=typec", |
| 258 |
{ |
| 259 |
# bad response |
| 260 |
"instance": NotifyPingram, |
| 261 |
"requests_response_text": PINGRAM_BAD_RESPONSE, |
| 262 |
"notify_response": False, |
| 263 |
}, |
| 264 |
), |
| 265 |
( |
| 266 |
"pingram://pingram_sk_abc123/id/[email protected]?bcc=invalid", |
| 267 |
{ |
| 268 |
# A good email with a bad Blind Carbon Copy |
| 269 |
"instance": NotifyPingram, |
| 270 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 271 |
}, |
| 272 |
), |
| 273 |
( |
| 274 |
"pingram://pingram_sk_abc123/id/[email protected][email protected]", |
| 275 |
{ |
| 276 |
# A good email with Carbon Copy |
| 277 |
"instance": NotifyPingram, |
| 278 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 279 |
}, |
| 280 |
), |
| 281 |
( |
| 282 |
"pingram://pingram_sk_abc123/id/[email protected]" |
| 283 |
"?channels=email,sms,slack,mobile_push,web_push,inapp,call", |
| 284 |
{ |
| 285 |
# All channels forced at once |
| 286 |
"instance": NotifyPingram, |
| 287 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 288 |
}, |
| 289 |
), |
| 290 |
( |
| 291 |
"pingram://pingram_sk_abc123/id/[email protected]" |
| 292 |
"?cc=Chris<[email protected]>", |
| 293 |
{ |
| 294 |
"instance": NotifyPingram, |
| 295 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 296 |
}, |
| 297 |
), |
| 298 |
( |
| 299 |
"pingram://pingram_sk_abc123/id/[email protected]?cc=invalid", |
| 300 |
{ |
| 301 |
"instance": NotifyPingram, |
| 302 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 303 |
}, |
| 304 |
), |
| 305 |
( |
| 306 |
"pingram://pingram_sk_abc123/id/[email protected]?to=invalid", |
| 307 |
{ |
| 308 |
# an invalid to email |
| 309 |
"instance": NotifyPingram, |
| 310 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 311 |
}, |
| 312 |
), |
| 313 |
( |
| 314 |
"pingram://pingram_sk_abc123/id1/[email protected]" |
| 315 |
"?to=id,Chris<[email protected]>", |
| 316 |
{ |
| 317 |
"instance": NotifyPingram, |
| 318 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 319 |
}, |
| 320 |
), |
| 321 |
( |
| 322 |
"pingram://pingram_sk_abc123/id2/[email protected]/" |
| 323 |
"id/[email protected]/id/[email protected]/id/+15552341234" |
| 324 |
"?:token=value", |
| 325 |
{ |
| 326 |
# Several targets to notify |
| 327 |
"instance": NotifyPingram, |
| 328 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 329 |
}, |
| 330 |
), |
| 331 |
( |
| 332 |
"pingram://pingram_sk_abc123/id/[email protected]" |
| 333 |
"?bcc=Chris<[email protected]>", |
| 334 |
{ |
| 335 |
"instance": NotifyPingram, |
| 336 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 337 |
}, |
| 338 |
), |
| 339 |
( |
| 340 |
"pingram://pingram_sk_abc123/id/[email protected]" |
| 341 |
"?:sub=value&:sub2=value2", |
| 342 |
{ |
| 343 |
# A good email with template substitutions |
| 344 |
"instance": NotifyPingram, |
| 345 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 346 |
"privacy_url": "pingram://p...3/", |
| 347 |
}, |
| 348 |
), |
| 349 |
( |
| 350 |
"pingram://pingram_sk_abc123/id/[email protected]", |
| 351 |
{ |
| 352 |
"instance": NotifyPingram, |
| 353 |
# force a failure |
| 354 |
"response": False, |
| 355 |
"requests_response_code": requests.codes.internal_server_error, |
| 356 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 357 |
}, |
| 358 |
), |
| 359 |
( |
| 360 |
"pingram://pingram_sk_abc123/id/[email protected]", |
| 361 |
{ |
| 362 |
"instance": NotifyPingram, |
| 363 |
# throw a bizarre code forcing us to fail to look it up |
| 364 |
"response": False, |
| 365 |
"requests_response_code": 999, |
| 366 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 367 |
}, |
| 368 |
), |
| 369 |
( |
| 370 |
"pingram://pingram_sk_abc123/id/[email protected]", |
| 371 |
{ |
| 372 |
"instance": NotifyPingram, |
| 373 |
# Throws connection and transfer exceptions when this |
| 374 |
# flag is set and tests that we gracefully handle them |
| 375 |
"test_requests_exceptions": True, |
| 376 |
"requests_response_text": PINGRAM_GOOD_RESPONSE, |
| 377 |
}, |
| 378 |
), |
| 379 |
) |
| 380 |
|
| 381 |
|
| 382 |
def test_plugin_pingram_urls(): |
| 383 |
"""NotifyPingram() Apprise URLs.""" |
| 384 |
|
| 385 |
# Run our general tests |
| 386 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 387 |
|
| 388 |
|
| 389 |
@mock.patch("requests.post") |
| 390 |
def test_plugin_pingram_template_sms_payloads(mock_post): |
| 391 |
"""NotifyPingram() Testing Template SMS Payloads.""" |
| 392 |
|
| 393 |
okay_response = requests.Request() |
| 394 |
okay_response.status_code = requests.codes.ok |
| 395 |
okay_response.content = PINGRAM_GOOD_RESPONSE |
| 396 |
|
| 397 |
# Assign our mock object our return value |
| 398 |
mock_post.return_value = okay_response |
| 399 |
|
| 400 |
# Details |
| 401 |
apikey = "pingram_sk_my_key" |
| 402 |
message_type = "apprise-post" |
| 403 |
targets = "userid/+1-555-123-4567" |
| 404 |
|
| 405 |
obj = Apprise.instantiate( |
| 406 |
f"pingram://{message_type}@{apikey}/{targets}?mode=template" |
| 407 |
) |
| 408 |
assert isinstance(obj, NotifyPingram) |
| 409 |
assert isinstance(obj.url(), str) |
| 410 |
|
| 411 |
# No calls made yet |
| 412 |
assert mock_post.call_count == 0 |
| 413 |
|
| 414 |
# Send our notification |
| 415 |
assert ( |
| 416 |
bool( |
| 417 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 418 |
) |
| 419 |
is True |
| 420 |
) |
| 421 |
|
| 422 |
# delivery of message |
| 423 |
assert mock_post.call_count == 1 |
| 424 |
assert mock_post.call_args_list[0][0][0] == "https://api.pingram.io/send" |
| 425 |
|
| 426 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 427 |
assert payload == { |
| 428 |
"type": "apprise-post", |
| 429 |
"to": { |
| 430 |
"id": "userid", |
| 431 |
"number": "+15551234567", |
| 432 |
}, |
| 433 |
"parameters": { |
| 434 |
"appBody": "body", |
| 435 |
"appTitle": "title", |
| 436 |
"appType": "info", |
| 437 |
"appId": "Apprise", |
| 438 |
"appDescription": "Apprise Notifications", |
| 439 |
"appColor": "#3AA3E3", |
| 440 |
"appImageUrl": ( |
| 441 |
"https://github.com/caronc/apprise/raw/master/apprise" |
| 442 |
"/assets/themes/default/apprise-info-72x72.png" |
| 443 |
), |
| 444 |
"appUrl": "https://github.com/caronc/apprise", |
| 445 |
}, |
| 446 |
} |
| 447 |
headers = mock_post.call_args_list[0][1]["headers"] |
| 448 |
assert headers == { |
| 449 |
"User-Agent": "Apprise", |
| 450 |
"Content-Type": "application/json", |
| 451 |
"Authorization": f"Bearer {apikey}", |
| 452 |
} |
| 453 |
|
| 454 |
# Reset our mock object |
| 455 |
mock_post.reset_mock() |
| 456 |
|
| 457 |
|
| 458 |
@mock.patch("requests.post") |
| 459 |
def test_plugin_pingram_template_email_payloads(mock_post): |
| 460 |
"""NotifyPingram() Testing Template Email Payloads.""" |
| 461 |
|
| 462 |
okay_response = requests.Request() |
| 463 |
okay_response.status_code = requests.codes.ok |
| 464 |
okay_response.content = PINGRAM_GOOD_RESPONSE |
| 465 |
|
| 466 |
# Assign our mock object our return value |
| 467 |
mock_post.return_value = okay_response |
| 468 |
|
| 469 |
# Details |
| 470 |
apikey = "pingram_sk_my_key_abc" |
| 471 |
message_type = "apprise-post" |
| 472 |
targets = "userid/[email protected]" |
| 473 |
|
| 474 |
obj = Apprise.instantiate( |
| 475 |
f"pingram://{message_type}@{apikey}/{targets}" |
| 476 |
f"?from=Chris<[email protected]>&[email protected]&" |
| 477 |
f"[email protected]&:customToken=customValue&mode=template" |
| 478 |
) |
| 479 |
assert isinstance(obj, NotifyPingram) |
| 480 |
assert isinstance(obj.url(), str) |
| 481 |
|
| 482 |
# No calls made yet |
| 483 |
assert mock_post.call_count == 0 |
| 484 |
|
| 485 |
# Send our notification |
| 486 |
assert ( |
| 487 |
bool( |
| 488 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 489 |
) |
| 490 |
is True |
| 491 |
) |
| 492 |
|
| 493 |
# delivery of message |
| 494 |
assert mock_post.call_count == 1 |
| 495 |
assert mock_post.call_args_list[0][0][0] == "https://api.pingram.io/send" |
| 496 |
|
| 497 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 498 |
assert payload == { |
| 499 |
"type": "apprise-post", |
| 500 |
"to": { |
| 501 |
"id": "userid", |
| 502 |
"email": "[email protected]", |
| 503 |
}, |
| 504 |
"options": { |
| 505 |
"email": { |
| 506 |
"fromAddress": "[email protected]", |
| 507 |
"fromName": "Chris", |
| 508 |
"ccAddresses": ["[email protected]"], |
| 509 |
"bccAddresses": ["[email protected]"], |
| 510 |
} |
| 511 |
}, |
| 512 |
"parameters": { |
| 513 |
"customToken": "customValue", |
| 514 |
"appBody": "body", |
| 515 |
"appTitle": "title", |
| 516 |
"appType": "info", |
| 517 |
"appId": "Apprise", |
| 518 |
"appDescription": "Apprise Notifications", |
| 519 |
"appColor": "#3AA3E3", |
| 520 |
"appImageUrl": ( |
| 521 |
"https://github.com/caronc/apprise/raw/master/apprise/" |
| 522 |
"assets/themes/default/apprise-info-72x72.png" |
| 523 |
), |
| 524 |
"appUrl": "https://github.com/caronc/apprise", |
| 525 |
}, |
| 526 |
} |
| 527 |
headers = mock_post.call_args_list[0][1]["headers"] |
| 528 |
assert headers == { |
| 529 |
"User-Agent": "Apprise", |
| 530 |
"Content-Type": "application/json", |
| 531 |
"Authorization": f"Bearer {apikey}", |
| 532 |
} |
| 533 |
|
| 534 |
# Reset our mock object |
| 535 |
mock_post.reset_mock() |
| 536 |
|
| 537 |
|
| 538 |
@mock.patch("requests.post") |
| 539 |
def test_plugin_pingram_message_payloads(mock_post): |
| 540 |
"""NotifyPingram() Testing Message Payloads.""" |
| 541 |
|
| 542 |
okay_response = requests.Request() |
| 543 |
okay_response.status_code = requests.codes.ok |
| 544 |
okay_response.content = PINGRAM_GOOD_RESPONSE |
| 545 |
|
| 546 |
# Assign our mock object our return value |
| 547 |
mock_post.return_value = okay_response |
| 548 |
|
| 549 |
# Details |
| 550 |
apikey = "pingram_sk_my_key_abc" |
| 551 |
message_type = "apprise-post" |
| 552 |
targets = "userid/[email protected]/+15551239876" |
| 553 |
|
| 554 |
obj = Apprise.instantiate( |
| 555 |
f"pingram://{message_type}@{apikey}/{targets}" |
| 556 |
f"?from=Chris<[email protected]>&[email protected]" |
| 557 |
f"&mode=message" |
| 558 |
) |
| 559 |
assert isinstance(obj, NotifyPingram) |
| 560 |
assert isinstance(obj.url(), str) |
| 561 |
|
| 562 |
# Send our notification |
| 563 |
assert ( |
| 564 |
bool( |
| 565 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 566 |
) |
| 567 |
is True |
| 568 |
) |
| 569 |
|
| 570 |
# delivery of message |
| 571 |
assert mock_post.call_count == 1 |
| 572 |
assert mock_post.call_args_list[0][0][0] == "https://api.pingram.io/send" |
| 573 |
|
| 574 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 575 |
assert payload == { |
| 576 |
"type": "apprise-post", |
| 577 |
"to": { |
| 578 |
"id": "userid", |
| 579 |
"email": "[email protected]", |
| 580 |
"number": "+15551239876", |
| 581 |
}, |
| 582 |
"email": { |
| 583 |
"subject": "title", |
| 584 |
"html": "body", |
| 585 |
"senderName": "Chris", |
| 586 |
"senderEmail": "[email protected]", |
| 587 |
}, |
| 588 |
"options": { |
| 589 |
"email": { |
| 590 |
"fromAddress": "[email protected]", |
| 591 |
"fromName": "Chris", |
| 592 |
"bccAddresses": ["[email protected]"], |
| 593 |
}, |
| 594 |
}, |
| 595 |
} |
| 596 |
headers = mock_post.call_args_list[0][1]["headers"] |
| 597 |
assert headers == { |
| 598 |
"User-Agent": "Apprise", |
| 599 |
"Content-Type": "application/json", |
| 600 |
"Authorization": f"Bearer {apikey}", |
| 601 |
} |
| 602 |
|
| 603 |
# Reset our mock object |
| 604 |
mock_post.reset_mock() |
| 605 |
|
| 606 |
# Reversing the sms with email causes auto-detection channel to |
| 607 |
# be sms instead of email |
| 608 |
targets = "userid/+15551239876/[email protected]" |
| 609 |
|
| 610 |
obj = Apprise.instantiate( |
| 611 |
f"pingram://{apikey}/{targets}" |
| 612 |
f"?from=Chris<[email protected]>&[email protected]" |
| 613 |
) |
| 614 |
assert isinstance(obj, NotifyPingram) |
| 615 |
assert isinstance(obj.url(), str) |
| 616 |
|
| 617 |
# Send our notification |
| 618 |
assert ( |
| 619 |
bool( |
| 620 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 621 |
) |
| 622 |
is True |
| 623 |
) |
| 624 |
|
| 625 |
# delivery of message |
| 626 |
assert mock_post.call_count == 1 |
| 627 |
assert mock_post.call_args_list[0][0][0] == "https://api.pingram.io/send" |
| 628 |
|
| 629 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 630 |
assert payload == { |
| 631 |
"type": "apprise", |
| 632 |
"to": { |
| 633 |
"id": "userid", |
| 634 |
"number": "+15551239876", |
| 635 |
"email": "[email protected]", |
| 636 |
}, |
| 637 |
"sms": {"message": "title\nbody"}, |
| 638 |
"options": { |
| 639 |
"email": { |
| 640 |
"fromAddress": "[email protected]", |
| 641 |
"fromName": "Chris", |
| 642 |
"bccAddresses": ["[email protected]"], |
| 643 |
}, |
| 644 |
}, |
| 645 |
} |
| 646 |
|
| 647 |
headers = mock_post.call_args_list[0][1]["headers"] |
| 648 |
assert headers == { |
| 649 |
"User-Agent": "Apprise", |
| 650 |
"Content-Type": "application/json", |
| 651 |
"Authorization": f"Bearer {apikey}", |
| 652 |
} |
| 653 |
|
| 654 |
# Reset our mock object |
| 655 |
mock_post.reset_mock() |
| 656 |
|
| 657 |
# Experiment with fixed channels (including the call/TTS channel) |
| 658 |
obj = Apprise.instantiate( |
| 659 |
f"pingram://{message_type}@{apikey}/{targets}" |
| 660 |
f"?from=Chris<[email protected]>&[email protected]" |
| 661 |
f"&mode=message&channels=sms,slack,call" |
| 662 |
) |
| 663 |
assert isinstance(obj, NotifyPingram) |
| 664 |
assert isinstance(obj.url(), str) |
| 665 |
|
| 666 |
# Send our notification |
| 667 |
assert ( |
| 668 |
bool( |
| 669 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 670 |
) |
| 671 |
is True |
| 672 |
) |
| 673 |
|
| 674 |
# delivery of message |
| 675 |
assert mock_post.call_count == 1 |
| 676 |
assert mock_post.call_args_list[0][0][0] == "https://api.pingram.io/send" |
| 677 |
|
| 678 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 679 |
assert payload == { |
| 680 |
"type": "apprise-post", |
| 681 |
"to": { |
| 682 |
"id": "userid", |
| 683 |
"email": "[email protected]", |
| 684 |
"number": "+15551239876", |
| 685 |
}, |
| 686 |
"slack": {"text": "title\nbody"}, |
| 687 |
"sms": {"message": "title\nbody"}, |
| 688 |
"call": {"message": "title\nbody"}, |
| 689 |
"options": { |
| 690 |
"email": { |
| 691 |
"fromAddress": "[email protected]", |
| 692 |
"fromName": "Chris", |
| 693 |
"bccAddresses": ["[email protected]"], |
| 694 |
}, |
| 695 |
}, |
| 696 |
} |
| 697 |
|
| 698 |
headers = mock_post.call_args_list[0][1]["headers"] |
| 699 |
assert headers == { |
| 700 |
"User-Agent": "Apprise", |
| 701 |
"Content-Type": "application/json", |
| 702 |
"Authorization": f"Bearer {apikey}", |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
@mock.patch("requests.post") |
| 707 |
def test_plugin_pingram_targets(mock_post): |
| 708 |
"""NotifyPingram() Testing Target Parsing.""" |
| 709 |
|
| 710 |
okay_response = requests.Request() |
| 711 |
okay_response.status_code = requests.codes.ok |
| 712 |
okay_response.content = PINGRAM_GOOD_RESPONSE |
| 713 |
|
| 714 |
# Assign our mock object our return value |
| 715 |
mock_post.return_value = okay_response |
| 716 |
|
| 717 |
apikey = "pingram_sk_abc123" |
| 718 |
|
| 719 |
# A bare phone number with no id is valid |
| 720 |
obj = Apprise.instantiate(f"pingram://{apikey}/+15551234567") |
| 721 |
assert isinstance(obj, NotifyPingram) |
| 722 |
assert isinstance(obj.url(), str) |
| 723 |
|
| 724 |
assert ( |
| 725 |
bool( |
| 726 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 727 |
) |
| 728 |
is True |
| 729 |
) |
| 730 |
|
| 731 |
assert mock_post.call_count == 1 |
| 732 |
assert mock_post.call_args_list[0][0][0] == "https://api.pingram.io/send" |
| 733 |
|
| 734 |
# No "id" present since none was supplied in the URL |
| 735 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 736 |
assert payload == { |
| 737 |
"type": "apprise", |
| 738 |
"to": { |
| 739 |
"number": "+15551234567", |
| 740 |
}, |
| 741 |
"sms": {"message": "title\nbody"}, |
| 742 |
} |
| 743 |
|
| 744 |
headers = mock_post.call_args_list[0][1]["headers"] |
| 745 |
assert headers == { |
| 746 |
"User-Agent": "Apprise", |
| 747 |
"Content-Type": "application/json", |
| 748 |
"Authorization": f"Bearer {apikey}", |
| 749 |
} |
| 750 |
|
| 751 |
mock_post.reset_mock() |
| 752 |
|
| 753 |
# Multiple id-less phone targets each become their own recipient |
| 754 |
obj = Apprise.instantiate(f"pingram://{apikey}/+15551234567/+15551234568") |
| 755 |
assert isinstance(obj, NotifyPingram) |
| 756 |
assert len(obj.targets) == 2 |
| 757 |
|
| 758 |
assert ( |
| 759 |
bool( |
| 760 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 761 |
) |
| 762 |
is True |
| 763 |
) |
| 764 |
assert mock_post.call_count == 2 |
| 765 |
|
| 766 |
mock_post.reset_mock() |
| 767 |
|
| 768 |
# Region support carries over to the Pingram endpoints too |
| 769 |
obj = Apprise.instantiate(f"pingram://{apikey}/+15551234567?region=eu") |
| 770 |
assert isinstance(obj, NotifyPingram) |
| 771 |
assert ( |
| 772 |
bool( |
| 773 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 774 |
) |
| 775 |
is True |
| 776 |
) |
| 777 |
assert mock_post.call_count == 1 |
| 778 |
assert ( |
| 779 |
mock_post.call_args_list[0][0][0] == "https://api.eu.pingram.io/send" |
| 780 |
) |
| 781 |
|
| 782 |
mock_post.reset_mock() |
| 783 |
|
| 784 |
# An id can still optionally be supplied alongside the number |
| 785 |
obj = Apprise.instantiate(f"pingram://{apikey}/myid/+15551234567") |
| 786 |
assert isinstance(obj, NotifyPingram) |
| 787 |
assert ( |
| 788 |
bool( |
| 789 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 790 |
) |
| 791 |
is True |
| 792 |
) |
| 793 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 794 |
assert payload["to"] == {"id": "myid", "number": "+15551234567"} |
| 795 |
|
| 796 |
|
| 797 |
def test_plugin_pingram_edge_cases(): |
| 798 |
"""NotifyPingram() Edge Cases.""" |
| 799 |
|
| 800 |
# An API key is required. |
| 801 |
with pytest.raises(AppriseImproperlyConfigured): |
| 802 |
NotifyPingram(apikey=None, targets=["+15551239876"]) |
| 803 |
|
| 804 |
# An API key with the wrong prefix is invalid. |
| 805 |
with pytest.raises(AppriseImproperlyConfigured): |
| 806 |
NotifyPingram(apikey="not-a-pingram-key", targets=["+15551239876"]) |
| 807 |
|
| 808 |
# Tests case where tokens is == None |
| 809 |
obj = NotifyPingram(apikey="pingram_sk_my_key", targets=["+15551239876"]) |
| 810 |
assert isinstance(obj, NotifyPingram) |
| 811 |
assert isinstance(obj.url(), str) |
| 812 |
assert obj.tokens == {} |