| 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 |
# Disable logging for a cleaner testing output |
| 29 |
import logging |
| 30 |
import os |
| 31 |
import sys |
| 32 |
from unittest import mock |
| 33 |
|
| 34 |
from helpers import AppriseURLTester |
| 35 |
import pytest |
| 36 |
import requests |
| 37 |
|
| 38 |
from apprise import Apprise, AppriseAttachment |
| 39 |
from apprise.exception import AppriseImproperlyConfigured |
| 40 |
from apprise.plugins.ses import NotifySES |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
if hasattr(sys, "pypy_version_info"): |
| 45 |
raise pytest.skip( |
| 46 |
reason="Skipping test cases which stall on PyPy", |
| 47 |
allow_module_level=True, |
| 48 |
) |
| 49 |
|
| 50 |
# Attachment Directory |
| 51 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 52 |
|
| 53 |
AWS_SES_GOOD_RESPONSE = """ |
| 54 |
<SendRawEmailResponse |
| 55 |
xmlns="http://ses.amazonaws.com/doc/2010-12-01/"> |
| 56 |
<SendRawEmailResult> |
| 57 |
<MessageId> |
| 58 |
010f017d87656ee2-a2ea291f-79ea- |
| 59 |
44f3-9d25-00d041de3007-000000</MessageId> |
| 60 |
</SendRawEmailResult> |
| 61 |
<ResponseMetadata> |
| 62 |
<RequestId>7abb454e-904b-4e46-a23c-2f4d2fc127a6</RequestId> |
| 63 |
</ResponseMetadata> |
| 64 |
</SendRawEmailResponse> |
| 65 |
""" |
| 66 |
|
| 67 |
TEST_ACCESS_KEY_ID = "AHIAJGNT76XIMXDBIJYA" |
| 68 |
TEST_ACCESS_KEY_SECRET = "bu1dHSdO22pfaaVy/wmNsdljF4C07D3bndi9PQJ9" |
| 69 |
TEST_REGION = "us-east-2" |
| 70 |
TEST_SESSION_TOKEN = "FwoGZXIvYXdzSESSIONTOKENABCDEFGHIJKLMNOPQRS" |
| 71 |
|
| 72 |
# Our Testing URLs |
| 73 |
apprise_url_tests = ( |
| 74 |
( |
| 75 |
"ses://", |
| 76 |
{ |
| 77 |
"instance": AppriseImproperlyConfigured, |
| 78 |
}, |
| 79 |
), |
| 80 |
( |
| 81 |
"ses://:@/", |
| 82 |
{ |
| 83 |
"instance": AppriseImproperlyConfigured, |
| 84 |
}, |
| 85 |
), |
| 86 |
( |
| 87 |
"ses://[email protected]/T1JJ3T3L2", |
| 88 |
{ |
| 89 |
# Just Token 1 provided |
| 90 |
"instance": AppriseImproperlyConfigured, |
| 91 |
}, |
| 92 |
), |
| 93 |
( |
| 94 |
"ses://[email protected]/T1JJ3TD4JD/TIiajkdnlazk7FQ/", |
| 95 |
{ |
| 96 |
# Missing a region |
| 97 |
"instance": AppriseImproperlyConfigured, |
| 98 |
}, |
| 99 |
), |
| 100 |
( |
| 101 |
"ses://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcevi7FQ/us-west-2", |
| 102 |
{ |
| 103 |
# No email |
| 104 |
"instance": AppriseImproperlyConfigured, |
| 105 |
}, |
| 106 |
), |
| 107 |
( |
| 108 |
"ses://[email protected]/T1JJ3TD4JD/TIiajkdnlazk7FQ/[email protected]", |
| 109 |
{ |
| 110 |
# Missing a region (but has email) |
| 111 |
"instance": AppriseImproperlyConfigured, |
| 112 |
}, |
| 113 |
), |
| 114 |
( |
| 115 |
( |
| 116 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcevi7FQ/" |
| 117 |
"us-west-2?reply=invalid-email" |
| 118 |
), |
| 119 |
{ |
| 120 |
# An invalid reply-to address |
| 121 |
"instance": AppriseImproperlyConfigured, |
| 122 |
# Our response expected server response |
| 123 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 124 |
}, |
| 125 |
), |
| 126 |
( |
| 127 |
( |
| 128 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcevi7FQ/" |
| 129 |
"us-west-2" |
| 130 |
), |
| 131 |
{ |
| 132 |
# we have a valid URL and we'll use our own email as a target |
| 133 |
"instance": NotifySES, |
| 134 |
# Our response expected server response |
| 135 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 136 |
}, |
| 137 |
), |
| 138 |
( |
| 139 |
( |
| 140 |
"ses://[email protected]/T1JJ3TD4JD/TIiajkdnlazk7FQ/us-west-2/" |
| 141 |
"[email protected]/[email protected]" |
| 142 |
), |
| 143 |
{ |
| 144 |
# Multi Email Suppport |
| 145 |
"instance": NotifySES, |
| 146 |
# Our response expected server response |
| 147 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 148 |
# Our expected url(privacy=True) startswith() response: |
| 149 |
"privacy_url": "ses://[email protected]/T...D/****/us-west-2", |
| 150 |
}, |
| 151 |
), |
| 152 |
( |
| 153 |
( |
| 154 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiajkdnlaevi7FQ/us-east-1" |
| 155 |
"[email protected]" |
| 156 |
), |
| 157 |
{ |
| 158 |
# leveraging to: keyword |
| 159 |
"instance": NotifySES, |
| 160 |
# Our response expected server response |
| 161 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 162 |
}, |
| 163 |
), |
| 164 |
( |
| 165 |
( |
| 166 |
"ses://[email protected]®ion=us-west-2&access=T1JJ3T3L2" |
| 167 |
"&secret=A1BRTD4JD/TIiajkdnlaevi7FQ" |
| 168 |
"&reply=No One <[email protected]>" |
| 169 |
"&[email protected],[email protected],invalid-email" |
| 170 |
"&[email protected],[email protected],invalid-email" |
| 171 |
"&[email protected]" |
| 172 |
), |
| 173 |
{ |
| 174 |
# leveraging a ton of our keywords |
| 175 |
# We also test invlid emails specified on the bcc and cc list |
| 176 |
"instance": NotifySES, |
| 177 |
# Our response expected server response |
| 178 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 179 |
}, |
| 180 |
), |
| 181 |
( |
| 182 |
( |
| 183 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiacevi7FQ/us-west-2/" |
| 184 |
"?name=From%20Name&[email protected],invalid-email" |
| 185 |
), |
| 186 |
{ |
| 187 |
# leveraging a ton of our keywords |
| 188 |
"instance": NotifySES, |
| 189 |
# Our response expected server response |
| 190 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 191 |
}, |
| 192 |
), |
| 193 |
( |
| 194 |
( |
| 195 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiacevi7FQ/us-west-2/" |
| 196 |
"?format=text" |
| 197 |
), |
| 198 |
{ |
| 199 |
# Send email as a text (instead of HTML) |
| 200 |
"instance": NotifySES, |
| 201 |
# Our response expected server response |
| 202 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 203 |
}, |
| 204 |
), |
| 205 |
( |
| 206 |
( |
| 207 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiacevi7FQ/us-west-2/" |
| 208 |
"?to=invalid-email" |
| 209 |
), |
| 210 |
{ |
| 211 |
# An invalid email will get dropped during the initialization |
| 212 |
# we'll have no targets to notify afterwards |
| 213 |
"instance": NotifySES, |
| 214 |
# Our response expected server response |
| 215 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 216 |
# As a result, we won't be able to notify anyone |
| 217 |
"notify_response": False, |
| 218 |
}, |
| 219 |
), |
| 220 |
( |
| 221 |
( |
| 222 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiacevi7FQ/us-west-2/" |
| 223 |
"[email protected]" |
| 224 |
), |
| 225 |
{ |
| 226 |
"instance": NotifySES, |
| 227 |
# Our response expected server response |
| 228 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 229 |
# throw a bizarre code forcing us to fail to look it up |
| 230 |
"response": False, |
| 231 |
"requests_response_code": 999, |
| 232 |
}, |
| 233 |
), |
| 234 |
( |
| 235 |
( |
| 236 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiajkdnlavi7FQ/us-west-2/" |
| 237 |
"[email protected]" |
| 238 |
), |
| 239 |
{ |
| 240 |
"instance": NotifySES, |
| 241 |
# Our response expected server response |
| 242 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 243 |
# Throws a series of connection and transfer exceptions when this |
| 244 |
# flag is set and tests that we gracefully handle them |
| 245 |
"test_requests_exceptions": True, |
| 246 |
}, |
| 247 |
), |
| 248 |
( |
| 249 |
# Session token via ?token= query parameter |
| 250 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiacevi7FQ/us-west-2/" |
| 251 |
"[email protected]?token=SESSIONTOKEN", |
| 252 |
{ |
| 253 |
"instance": NotifySES, |
| 254 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 255 |
}, |
| 256 |
), |
| 257 |
( |
| 258 |
# Access key via ?key= alias |
| 259 |
"ses://[email protected]/?key=T1JJ3T3L2" |
| 260 |
"&secret=A1BRTD4JD/TIiacevi7FQ®ion=us-west-2" |
| 261 |
"&[email protected]", |
| 262 |
{ |
| 263 |
"instance": NotifySES, |
| 264 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 265 |
}, |
| 266 |
), |
| 267 |
( |
| 268 |
# Session token + HTTP error |
| 269 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiacevi7FQ/us-west-2/" |
| 270 |
"[email protected]?token=SESSIONTOKEN", |
| 271 |
{ |
| 272 |
"instance": NotifySES, |
| 273 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 274 |
"response": False, |
| 275 |
"requests_response_code": 999, |
| 276 |
}, |
| 277 |
), |
| 278 |
( |
| 279 |
# Session token + request exceptions |
| 280 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiacevi7FQ/us-west-2/" |
| 281 |
"[email protected]?token=SESSIONTOKEN", |
| 282 |
{ |
| 283 |
"instance": NotifySES, |
| 284 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 285 |
"test_requests_exceptions": True, |
| 286 |
}, |
| 287 |
), |
| 288 |
( |
| 289 |
# Session token in the URL password field |
| 290 |
"ses://user:[email protected]/T1JJ3T3L2/A1BRTD4JD/" |
| 291 |
"TIiacevi7FQ/us-west-2/[email protected]", |
| 292 |
{ |
| 293 |
"instance": NotifySES, |
| 294 |
"requests_response_text": AWS_SES_GOOD_RESPONSE, |
| 295 |
}, |
| 296 |
), |
| 297 |
) |
| 298 |
|
| 299 |
|
| 300 |
def test_plugin_ses_urls(): |
| 301 |
"""NotifySES() Apprise URLs.""" |
| 302 |
|
| 303 |
# Run our general tests |
| 304 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 305 |
|
| 306 |
|
| 307 |
# We initialize a post object just incase a test fails below |
| 308 |
# we don't want it sending any notifications upstream |
| 309 |
@mock.patch("requests.post") |
| 310 |
def test_plugin_ses_edge_cases(mock_post): |
| 311 |
"""NotifySES() Edge Cases.""" |
| 312 |
|
| 313 |
# Initializes the plugin with a valid access, but invalid access key |
| 314 |
with pytest.raises(AppriseImproperlyConfigured): |
| 315 |
# No access_key_id specified |
| 316 |
NotifySES( |
| 317 |
from_addr="[email protected]", |
| 318 |
access_key_id=None, |
| 319 |
secret_access_key=TEST_ACCESS_KEY_SECRET, |
| 320 |
region_name=TEST_REGION, |
| 321 |
targets="[email protected]", |
| 322 |
) |
| 323 |
|
| 324 |
with pytest.raises(AppriseImproperlyConfigured): |
| 325 |
# No secret_access_key specified |
| 326 |
NotifySES( |
| 327 |
from_addr="[email protected]", |
| 328 |
access_key_id=TEST_ACCESS_KEY_ID, |
| 329 |
secret_access_key=None, |
| 330 |
region_name=TEST_REGION, |
| 331 |
targets="[email protected]", |
| 332 |
) |
| 333 |
|
| 334 |
with pytest.raises(AppriseImproperlyConfigured): |
| 335 |
# No region_name specified |
| 336 |
NotifySES( |
| 337 |
from_addr="[email protected]", |
| 338 |
access_key_id=TEST_ACCESS_KEY_ID, |
| 339 |
secret_access_key=TEST_ACCESS_KEY_SECRET, |
| 340 |
region_name=None, |
| 341 |
targets="[email protected]", |
| 342 |
) |
| 343 |
|
| 344 |
# No recipients |
| 345 |
obj = NotifySES( |
| 346 |
from_addr="[email protected]", |
| 347 |
access_key_id=TEST_ACCESS_KEY_ID, |
| 348 |
secret_access_key=TEST_ACCESS_KEY_SECRET, |
| 349 |
region_name=TEST_REGION, |
| 350 |
targets=None, |
| 351 |
) |
| 352 |
|
| 353 |
# The object initializes properly but would not be able to send anything |
| 354 |
assert bool(obj.notify(body="test", title="test")) is False |
| 355 |
|
| 356 |
# The phone number is invalid, and without it, there is nothing |
| 357 |
# to notify; we |
| 358 |
obj = NotifySES( |
| 359 |
from_addr="[email protected]", |
| 360 |
access_key_id=TEST_ACCESS_KEY_ID, |
| 361 |
secret_access_key=TEST_ACCESS_KEY_SECRET, |
| 362 |
region_name=TEST_REGION, |
| 363 |
targets="invalid-email", |
| 364 |
) |
| 365 |
|
| 366 |
# The object initializes properly but would not be able to send anything |
| 367 |
assert bool(obj.notify(body="test", title="test")) is False |
| 368 |
|
| 369 |
|
| 370 |
def test_plugin_ses_url_parsing(): |
| 371 |
"""NotifySES() URL Parsing.""" |
| 372 |
|
| 373 |
# No recipients |
| 374 |
results = NotifySES.parse_url( |
| 375 |
"ses://{}/{}/{}/{}/".format( |
| 376 |
"[email protected]", |
| 377 |
TEST_ACCESS_KEY_ID, |
| 378 |
TEST_ACCESS_KEY_SECRET, |
| 379 |
TEST_REGION, |
| 380 |
) |
| 381 |
) |
| 382 |
|
| 383 |
# Confirm that there were no recipients found |
| 384 |
assert len(results["targets"]) == 0 |
| 385 |
assert "region_name" in results |
| 386 |
assert results["region_name"] == TEST_REGION |
| 387 |
assert "access_key_id" in results |
| 388 |
assert results["access_key_id"] == TEST_ACCESS_KEY_ID |
| 389 |
assert "secret_access_key" in results |
| 390 |
assert results["secret_access_key"] == TEST_ACCESS_KEY_SECRET |
| 391 |
|
| 392 |
# Detect recipients |
| 393 |
results = NotifySES.parse_url( |
| 394 |
"ses://{}/{}/{}/{}/{}/{}/".format( |
| 395 |
"[email protected]", |
| 396 |
TEST_ACCESS_KEY_ID, |
| 397 |
TEST_ACCESS_KEY_SECRET, |
| 398 |
# Uppercase Region won't break anything |
| 399 |
TEST_REGION.upper(), |
| 400 |
"[email protected]", |
| 401 |
"[email protected]", |
| 402 |
) |
| 403 |
) |
| 404 |
|
| 405 |
# Confirm that our recipients were found |
| 406 |
assert len(results["targets"]) == 2 |
| 407 |
assert "[email protected]" in results["targets"] |
| 408 |
assert "[email protected]" in results["targets"] |
| 409 |
assert "region_name" in results |
| 410 |
assert results["region_name"] == TEST_REGION |
| 411 |
assert "access_key_id" in results |
| 412 |
assert results["access_key_id"] == TEST_ACCESS_KEY_ID |
| 413 |
assert "secret_access_key" in results |
| 414 |
assert results["secret_access_key"] == TEST_ACCESS_KEY_SECRET |
| 415 |
|
| 416 |
|
| 417 |
def test_plugin_ses_aws_response_handling(): |
| 418 |
"""NotifySES() AWS Response Handling.""" |
| 419 |
# Not a string |
| 420 |
response = NotifySES.aws_response_to_dict(None) |
| 421 |
assert response["type"] is None |
| 422 |
assert response["request_id"] is None |
| 423 |
|
| 424 |
# Invalid XML |
| 425 |
response = NotifySES.aws_response_to_dict( |
| 426 |
'<Bad Response xmlns="http://ses.amazonaws.com/doc/2010-03-31/">' |
| 427 |
) |
| 428 |
assert response["type"] is None |
| 429 |
assert response["request_id"] is None |
| 430 |
|
| 431 |
# Single Element in XML |
| 432 |
response = NotifySES.aws_response_to_dict( |
| 433 |
"<SingleElement></SingleElement>" |
| 434 |
) |
| 435 |
assert response["type"] == "SingleElement" |
| 436 |
assert response["request_id"] is None |
| 437 |
|
| 438 |
# Empty String |
| 439 |
response = NotifySES.aws_response_to_dict("") |
| 440 |
assert response["type"] is None |
| 441 |
assert response["request_id"] is None |
| 442 |
|
| 443 |
response = NotifySES.aws_response_to_dict(""" |
| 444 |
<SendRawEmailResponse |
| 445 |
xmlns="http://ses.amazonaws.com/doc/2010-12-01/"> |
| 446 |
<SendRawEmailResult> |
| 447 |
<MessageId> |
| 448 |
010f017d87656ee2-a2ea291f-79ea-44f3-9d25-00d041de307</MessageId> |
| 449 |
</SendRawEmailResult> |
| 450 |
<ResponseMetadata> |
| 451 |
<RequestId>7abb454e-904b-4e46-a23c-2f4d2fc127a6</RequestId> |
| 452 |
</ResponseMetadata> |
| 453 |
</SendRawEmailResponse> |
| 454 |
""") |
| 455 |
assert response["type"] == "SendRawEmailResponse" |
| 456 |
assert response["request_id"] == "7abb454e-904b-4e46-a23c-2f4d2fc127a6" |
| 457 |
assert ( |
| 458 |
response["message_id"] |
| 459 |
== "010f017d87656ee2-a2ea291f-79ea-44f3-9d25-00d041de307" |
| 460 |
) |
| 461 |
|
| 462 |
response = NotifySES.aws_response_to_dict(""" |
| 463 |
<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-03-31/"> |
| 464 |
<Error> |
| 465 |
<Type>Sender</Type> |
| 466 |
<Code>InvalidParameter</Code> |
| 467 |
<Message>Invalid parameter</Message> |
| 468 |
</Error> |
| 469 |
<RequestId>b5614883-babe-56ca-93b2-1c592ba6191e</RequestId> |
| 470 |
</ErrorResponse> |
| 471 |
""") |
| 472 |
assert response["type"] == "ErrorResponse" |
| 473 |
assert response["request_id"] == "b5614883-babe-56ca-93b2-1c592ba6191e" |
| 474 |
assert response["error_type"] == "Sender" |
| 475 |
assert response["error_code"] == "InvalidParameter" |
| 476 |
assert response["error_message"] == "Invalid parameter" |
| 477 |
|
| 478 |
# An empty known element (no text at all) doesn't blow up; it's |
| 479 |
# just treated as blank |
| 480 |
response = NotifySES.aws_response_to_dict(""" |
| 481 |
<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-03-31/"> |
| 482 |
<Error> |
| 483 |
<Type>Sender</Type> |
| 484 |
<Code>InvalidParameter</Code> |
| 485 |
<Message/> |
| 486 |
</Error> |
| 487 |
<RequestId>b5614883-babe-56ca-93b2-1c592ba6191e</RequestId> |
| 488 |
</ErrorResponse> |
| 489 |
""") |
| 490 |
assert response["type"] == "ErrorResponse" |
| 491 |
assert response["error_message"] == "" |
| 492 |
|
| 493 |
# A response nested far deeper than Python's recursion limit still |
| 494 |
# parses instead of raising a RecursionError |
| 495 |
depth = sys.getrecursionlimit() + 1000 |
| 496 |
response = NotifySES.aws_response_to_dict( |
| 497 |
("<RequestId>" * depth) |
| 498 |
+ "b5614883-babe-56ca-93b2-1c592ba6191e" |
| 499 |
+ ("</RequestId>" * depth) |
| 500 |
) |
| 501 |
assert response["type"] == "RequestId" |
| 502 |
assert response["request_id"] == "b5614883-babe-56ca-93b2-1c592ba6191e" |
| 503 |
|
| 504 |
|
| 505 |
@mock.patch("requests.post") |
| 506 |
def test_plugin_ses_attachments(mock_post): |
| 507 |
"""NotifySES() Attachment Checks.""" |
| 508 |
|
| 509 |
# Prepare Mock return object |
| 510 |
response = mock.Mock() |
| 511 |
response.content = AWS_SES_GOOD_RESPONSE |
| 512 |
response.status_code = requests.codes.ok |
| 513 |
mock_post.return_value = response |
| 514 |
|
| 515 |
# prepare our attachment |
| 516 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 517 |
|
| 518 |
# Test our markdown |
| 519 |
obj = Apprise.instantiate( |
| 520 |
"ses://{}/{}/{}/{}/".format( |
| 521 |
"[email protected]", |
| 522 |
TEST_ACCESS_KEY_ID, |
| 523 |
TEST_ACCESS_KEY_SECRET, |
| 524 |
TEST_REGION, |
| 525 |
) |
| 526 |
) |
| 527 |
|
| 528 |
# Send a good attachment |
| 529 |
assert bool(obj.notify(body="test", attach=attach)) is True |
| 530 |
|
| 531 |
# Reset our mock object |
| 532 |
mock_post.reset_mock() |
| 533 |
|
| 534 |
# Add another attachment so we drop into the area of the PushBullet code |
| 535 |
# that sends remaining attachments (if more detected) |
| 536 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 537 |
|
| 538 |
# Send our attachments |
| 539 |
assert bool(obj.notify(body="test", attach=attach)) is True |
| 540 |
|
| 541 |
# Test our call count |
| 542 |
assert mock_post.call_count == 1 |
| 543 |
|
| 544 |
# Reset our mock object |
| 545 |
mock_post.reset_mock() |
| 546 |
|
| 547 |
# An invalid attachment will cause a failure |
| 548 |
path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 549 |
attach = AppriseAttachment(path) |
| 550 |
assert bool(obj.notify(body="test", attach=attach)) is False |
| 551 |
|
| 552 |
|
| 553 |
@mock.patch("requests.post") |
| 554 |
def test_plugin_ses_session_token(mock_post): |
| 555 |
"""NotifySES() session token for temporary/IAM credentials.""" |
| 556 |
|
| 557 |
response = mock.MagicMock() |
| 558 |
response.status_code = requests.codes.ok |
| 559 |
response.content = AWS_SES_GOOD_RESPONSE |
| 560 |
mock_post.return_value = response |
| 561 |
|
| 562 |
# Init with session token |
| 563 |
obj = NotifySES( |
| 564 |
access_key_id=TEST_ACCESS_KEY_ID, |
| 565 |
secret_access_key=TEST_ACCESS_KEY_SECRET, |
| 566 |
region_name=TEST_REGION, |
| 567 |
from_addr="[email protected]", |
| 568 |
targets=["[email protected]"], |
| 569 |
session_token=TEST_SESSION_TOKEN, |
| 570 |
) |
| 571 |
assert obj.aws_session_token == TEST_SESSION_TOKEN |
| 572 |
|
| 573 |
# send() must include X-Amz-Security-Token in request headers |
| 574 |
assert bool(obj.notify(body="test")) is True |
| 575 |
call_kwargs = mock_post.call_args[1] |
| 576 |
assert "X-Amz-Security-Token" in call_kwargs["headers"] |
| 577 |
assert call_kwargs["headers"]["X-Amz-Security-Token"] == TEST_SESSION_TOKEN |
| 578 |
|
| 579 |
# x-amz-security-token must appear in the Authorization SignedHeaders |
| 580 |
auth = call_kwargs["headers"]["Authorization"] |
| 581 |
assert "x-amz-security-token" in auth |
| 582 |
|
| 583 |
# URL round-trip: token appears in the password field, not ?token= |
| 584 |
url = obj.url() |
| 585 |
assert f":{TEST_SESSION_TOKEN}@" in url |
| 586 |
assert "token=" not in url |
| 587 |
|
| 588 |
results = NotifySES.parse_url(url) |
| 589 |
assert results["session_token"] == TEST_SESSION_TOKEN |
| 590 |
assert results["access_key_id"] == TEST_ACCESS_KEY_ID |
| 591 |
|
| 592 |
obj2 = NotifySES(**results) |
| 593 |
assert obj2.aws_session_token == TEST_SESSION_TOKEN |
| 594 |
assert obj2.url_identifier == obj.url_identifier |
| 595 |
|
| 596 |
# Privacy URL must mask the token value (in the password position) |
| 597 |
priv_url = obj.url(privacy=True) |
| 598 |
assert TEST_SESSION_TOKEN not in priv_url |
| 599 |
assert "token=" not in priv_url |
| 600 |
|
| 601 |
# Without a session token no X-Amz-Security-Token header is sent |
| 602 |
mock_post.reset_mock() |
| 603 |
obj_plain = NotifySES( |
| 604 |
access_key_id=TEST_ACCESS_KEY_ID, |
| 605 |
secret_access_key=TEST_ACCESS_KEY_SECRET, |
| 606 |
region_name=TEST_REGION, |
| 607 |
from_addr="[email protected]", |
| 608 |
targets=["[email protected]"], |
| 609 |
) |
| 610 |
assert obj_plain.aws_session_token is None |
| 611 |
assert bool(obj_plain.notify(body="test")) is True |
| 612 |
call_kwargs = mock_post.call_args[1] |
| 613 |
assert "X-Amz-Security-Token" not in call_kwargs["headers"] |
| 614 |
|
| 615 |
# url() without token has no token= param |
| 616 |
plain_url = obj_plain.url() |
| 617 |
assert "token=" not in plain_url |
| 618 |
|
| 619 |
|
| 620 |
def test_plugin_ses_session_token_via_kwarg(): |
| 621 |
"""NotifySES() session token parsed from ?token= kwarg.""" |
| 622 |
|
| 623 |
url = ( |
| 624 |
f"ses://[email protected]/{TEST_ACCESS_KEY_ID}" |
| 625 |
f"/{TEST_ACCESS_KEY_SECRET}/{TEST_REGION}" |
| 626 |
f"/[email protected]?token={TEST_SESSION_TOKEN}" |
| 627 |
) |
| 628 |
results = NotifySES.parse_url(url) |
| 629 |
assert results["session_token"] == TEST_SESSION_TOKEN |
| 630 |
assert results["access_key_id"] == TEST_ACCESS_KEY_ID |
| 631 |
|
| 632 |
obj = NotifySES(**results) |
| 633 |
assert obj.aws_session_token == TEST_SESSION_TOKEN |
| 634 |
|
| 635 |
|
| 636 |
def test_plugin_ses_key_alias(): |
| 637 |
"""NotifySES() ?key= alias for access_key_id.""" |
| 638 |
|
| 639 |
url = ( |
| 640 |
"ses://[email protected]/" |
| 641 |
f"?key={TEST_ACCESS_KEY_ID}" |
| 642 |
f"&secret={TEST_ACCESS_KEY_SECRET}" |
| 643 |
f"®ion={TEST_REGION}" |
| 644 |
"&[email protected]" |
| 645 |
) |
| 646 |
results = NotifySES.parse_url(url) |
| 647 |
assert results["access_key_id"] == TEST_ACCESS_KEY_ID |
| 648 |
|
| 649 |
obj = NotifySES(**results) |
| 650 |
assert obj.aws_access_key_id == TEST_ACCESS_KEY_ID |
| 651 |
|
| 652 |
# ?key= takes priority over ?access= when both appear |
| 653 |
url_both = ( |
| 654 |
"ses://[email protected]/" |
| 655 |
f"?key={TEST_ACCESS_KEY_ID}&access=OTHERID" |
| 656 |
f"&secret={TEST_ACCESS_KEY_SECRET}" |
| 657 |
f"®ion={TEST_REGION}" |
| 658 |
"&[email protected]" |
| 659 |
) |
| 660 |
results2 = NotifySES.parse_url(url_both) |
| 661 |
assert results2["access_key_id"] == TEST_ACCESS_KEY_ID |
| 662 |
|
| 663 |
|
| 664 |
def test_plugin_ses_session_token_via_password_field(): |
| 665 |
"""NotifySES() session token parsed from URL password position.""" |
| 666 |
|
| 667 |
# Token in the password field: |
| 668 |
# ses://sender:{token}@example.com/{access_key_id}/... |
| 669 |
url = ( |
| 670 |
f"ses://sender:{TEST_SESSION_TOKEN}@example.com" |
| 671 |
f"/{TEST_ACCESS_KEY_ID}/{TEST_ACCESS_KEY_SECRET}" |
| 672 |
f"/{TEST_REGION}/[email protected]" |
| 673 |
) |
| 674 |
results = NotifySES.parse_url(url) |
| 675 |
assert results["session_token"] == TEST_SESSION_TOKEN |
| 676 |
assert results["access_key_id"] == TEST_ACCESS_KEY_ID |
| 677 |
|
| 678 |
obj = NotifySES(**results) |
| 679 |
assert obj.aws_session_token == TEST_SESSION_TOKEN |
| 680 |
assert obj.from_addr == "[email protected]" |
| 681 |
|
| 682 |
# Round-trip: url() emits password-field form, which re-parses correctly |
| 683 |
regenerated = obj.url() |
| 684 |
assert f":{TEST_SESSION_TOKEN}@" in regenerated |
| 685 |
assert "token=" not in regenerated |
| 686 |
|
| 687 |
results2 = NotifySES.parse_url(regenerated) |
| 688 |
assert results2["session_token"] == TEST_SESSION_TOKEN |
| 689 |
|
| 690 |
obj2 = NotifySES(**results2) |
| 691 |
assert obj2.aws_session_token == TEST_SESSION_TOKEN |
| 692 |
assert obj2.from_addr == obj.from_addr |
| 693 |
|
| 694 |
# ?token= takes priority over the password field when both are present |
| 695 |
url_both = ( |
| 696 |
f"ses://sender:[email protected]" |
| 697 |
f"/{TEST_ACCESS_KEY_ID}/{TEST_ACCESS_KEY_SECRET}" |
| 698 |
f"/{TEST_REGION}/[email protected]" |
| 699 |
f"?token={TEST_SESSION_TOKEN}" |
| 700 |
) |
| 701 |
results3 = NotifySES.parse_url(url_both) |
| 702 |
assert results3["session_token"] == TEST_SESSION_TOKEN |
| 703 |
|
| 704 |
obj3 = NotifySES(**results3) |
| 705 |
assert obj3.aws_session_token == TEST_SESSION_TOKEN |
| 706 |
|
| 707 |
|
| 708 |
def test_plugin_ses_cc_bcc_invalid(): |
| 709 |
"""NotifySES() drops invalid CC and BCC entries.""" |
| 710 |
# A list is used because parse_emails() already filters invalid |
| 711 |
# entries out of a comma-separated string before they are checked. |
| 712 |
obj = NotifySES( |
| 713 |
access_key_id="T1JJ3T3L2", |
| 714 |
secret_access_key="A1BRTD4JD/TIiajkdnlazkcevi7FQ", |
| 715 |
region_name="us-west-2", |
| 716 |
from_addr="[email protected]", |
| 717 |
targets=["[email protected]"], |
| 718 |
cc=["[email protected]", "notanemail"], |
| 719 |
bcc=["[email protected]", "alsonotanemail"], |
| 720 |
) |
| 721 |
|
| 722 |
# Only the valid CC and BCC entries were kept |
| 723 |
assert obj.cc == {"[email protected]"} |
| 724 |
assert obj.bcc == {"[email protected]"} |
| 725 |
|
| 726 |
|
| 727 |
def test_plugin_ses_cc_names(): |
| 728 |
"""NotifySES() url() keeps the display names of CC addresses.""" |
| 729 |
obj = Apprise.instantiate( |
| 730 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcevi7FQ/" |
| 731 |
"us-west-2/[email protected]" |
| 732 |
"?cc=Chris<[email protected]>,[email protected]" |
| 733 |
) |
| 734 |
assert isinstance(obj, NotifySES) |
| 735 |
assert obj.names["[email protected]"] == "Chris" |
| 736 |
assert obj.names["[email protected]"] is False |
| 737 |
|
| 738 |
url = obj.url() |
| 739 |
assert "Chris%3Al2g%40nuxref.com" in url |
| 740 |
|
| 741 |
# The names survive a round trip through url() |
| 742 |
obj2 = Apprise.instantiate(url) |
| 743 |
assert isinstance(obj2, NotifySES) |
| 744 |
assert obj2.cc == obj.cc |
| 745 |
assert obj2.names["[email protected]"] == "Chris" |
| 746 |
assert obj2.names["[email protected]"] is False |
| 747 |
|
| 748 |
|
| 749 |
def test_plugin_ses_cc_names_with_spaces(): |
| 750 |
"""NotifySES() url() keeps CC display names that contain spaces.""" |
| 751 |
obj = NotifySES( |
| 752 |
access_key_id="T1JJ3T3L2", |
| 753 |
secret_access_key="A1BRTD4JD/TIiajkdnlazkcevi7FQ", |
| 754 |
region_name="us-west-2", |
| 755 |
from_addr="[email protected]", |
| 756 |
targets=["[email protected]"], |
| 757 |
cc=["Chris Smith <[email protected]>"], |
| 758 |
bcc=["Jane Doe <[email protected]>"], |
| 759 |
) |
| 760 |
assert obj.names["[email protected]"] == "Chris Smith" |
| 761 |
|
| 762 |
obj2 = Apprise.instantiate(obj.url()) |
| 763 |
assert isinstance(obj2, NotifySES) |
| 764 |
assert obj2.cc == {"[email protected]"} |
| 765 |
assert obj2.bcc == {"[email protected]"} |
| 766 |
assert obj2.names["[email protected]"] == "Chris Smith" |
| 767 |
|
| 768 |
# A BCC name with a space is kept whole when read from a URL |
| 769 |
obj3 = Apprise.instantiate( |
| 770 |
"ses://[email protected]/T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcevi7FQ/" |
| 771 |
"us-west-2/[email protected]?bcc=Jane%20Doe<[email protected]>" |
| 772 |
) |
| 773 |
assert isinstance(obj3, NotifySES) |
| 774 |
assert obj3.bcc == {"[email protected]"} |
| 775 |
assert obj3.names["[email protected]"] == "Jane Doe" |