| 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 datetime import datetime |
| 29 |
from email.header import decode_header |
| 30 |
from inspect import cleandoc |
| 31 |
import logging |
| 32 |
import os |
| 33 |
import re |
| 34 |
import shutil |
| 35 |
import smtplib |
| 36 |
import ssl |
| 37 |
import sys |
| 38 |
from unittest import mock |
| 39 |
|
| 40 |
import pytest |
| 41 |
|
| 42 |
from apprise import ( |
| 43 |
Apprise, |
| 44 |
AppriseAsset, |
| 45 |
AppriseAttachment, |
| 46 |
AttachBase, |
| 47 |
NotifyBase, |
| 48 |
NotifyFormat, |
| 49 |
NotifyType, |
| 50 |
PersistentStoreMode, |
| 51 |
utils, |
| 52 |
) |
| 53 |
from apprise.attachment.memory import AttachMemory |
| 54 |
from apprise.config import ConfigBase |
| 55 |
from apprise.exception import AppriseException, AppriseImproperlyConfigured |
| 56 |
from apprise.plugins import email |
| 57 |
|
| 58 |
try: |
| 59 |
import pgpy |
| 60 |
except ImportError: |
| 61 |
pgpy = None |
| 62 |
|
| 63 |
# Disable logging for a cleaner testing output |
| 64 |
logging.disable(logging.CRITICAL) |
| 65 |
|
| 66 |
# Attachment Directory |
| 67 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 68 |
|
| 69 |
TEST_URLS = ( |
| 70 |
################################## |
| 71 |
# NotifyEmail |
| 72 |
################################## |
| 73 |
( |
| 74 |
"mailto://", |
| 75 |
{ |
| 76 |
"instance": AppriseImproperlyConfigured, |
| 77 |
}, |
| 78 |
), |
| 79 |
( |
| 80 |
"mailtos://", |
| 81 |
{ |
| 82 |
"instance": AppriseImproperlyConfigured, |
| 83 |
}, |
| 84 |
), |
| 85 |
( |
| 86 |
"mailto://:@/", |
| 87 |
{ |
| 88 |
"instance": AppriseImproperlyConfigured, |
| 89 |
}, |
| 90 |
), |
| 91 |
# No Username |
| 92 |
( |
| 93 |
"mailtos://:[email protected]:567", |
| 94 |
{ |
| 95 |
# Can't prepare a To address using this expression |
| 96 |
"instance": AppriseImproperlyConfigured, |
| 97 |
}, |
| 98 |
), |
| 99 |
( |
| 100 |
# invalid Timezone |
| 101 |
"mailto://user:[email protected]?tz=invalid", |
| 102 |
{ |
| 103 |
# An error is thrown for this |
| 104 |
"instance": AppriseImproperlyConfigured, |
| 105 |
}, |
| 106 |
), |
| 107 |
# Pre-Configured Email Services |
| 108 |
( |
| 109 |
"mailto://user:[email protected]", |
| 110 |
{ |
| 111 |
"instance": email.NotifyEmail, |
| 112 |
}, |
| 113 |
), |
| 114 |
( |
| 115 |
"mailto://user:[email protected]", |
| 116 |
{ |
| 117 |
"instance": email.NotifyEmail, |
| 118 |
}, |
| 119 |
), |
| 120 |
( |
| 121 |
"mailto://user:[email protected]", |
| 122 |
{ |
| 123 |
"instance": email.NotifyEmail, |
| 124 |
}, |
| 125 |
), |
| 126 |
( |
| 127 |
"mailto://user:[email protected]", |
| 128 |
{ |
| 129 |
"instance": email.NotifyEmail, |
| 130 |
}, |
| 131 |
), |
| 132 |
( |
| 133 |
"mailto://user:[email protected]", |
| 134 |
{ |
| 135 |
"instance": email.NotifyEmail, |
| 136 |
}, |
| 137 |
), |
| 138 |
( |
| 139 |
"mailto://user:[email protected]", |
| 140 |
{ |
| 141 |
"instance": email.NotifyEmail, |
| 142 |
}, |
| 143 |
), |
| 144 |
( |
| 145 |
"mailto://user:[email protected]?tz=UTC", |
| 146 |
{ |
| 147 |
"instance": email.NotifyEmail, |
| 148 |
}, |
| 149 |
), |
| 150 |
( |
| 151 |
"mailto://user:[email protected]", |
| 152 |
{ |
| 153 |
"instance": email.NotifyEmail, |
| 154 |
}, |
| 155 |
), |
| 156 |
# GMX Support |
| 157 |
( |
| 158 |
"mailto://user:[email protected]", |
| 159 |
{ |
| 160 |
"instance": email.NotifyEmail, |
| 161 |
"privacy_url": "mailtos://user:****@gmx.net", |
| 162 |
}, |
| 163 |
), |
| 164 |
( |
| 165 |
"mailto://user:[email protected]", |
| 166 |
{ |
| 167 |
"instance": email.NotifyEmail, |
| 168 |
"privacy_url": "mailtos://user:****@gmx.com", |
| 169 |
}, |
| 170 |
), |
| 171 |
( |
| 172 |
"mailto://user:[email protected]", |
| 173 |
{ |
| 174 |
"instance": email.NotifyEmail, |
| 175 |
"privacy_url": "mailtos://user:****@gmx.de", |
| 176 |
}, |
| 177 |
), |
| 178 |
( |
| 179 |
# STARTTLS flag checking (and privacy URL expectation) |
| 180 |
"mailtos://user:[email protected]?mode=starttls", |
| 181 |
{ |
| 182 |
"instance": email.NotifyEmail, |
| 183 |
"privacy_url": "mailtos://user:****@gmx.net", |
| 184 |
}, |
| 185 |
), |
| 186 |
# Yandex |
| 187 |
( |
| 188 |
"mailto://user:[email protected]", |
| 189 |
{ |
| 190 |
"instance": email.NotifyEmail, |
| 191 |
}, |
| 192 |
), |
| 193 |
( |
| 194 |
"mailto://user:[email protected]", |
| 195 |
{ |
| 196 |
"instance": email.NotifyEmail, |
| 197 |
}, |
| 198 |
), |
| 199 |
( |
| 200 |
"mailto://user:[email protected]", |
| 201 |
{ |
| 202 |
"instance": email.NotifyEmail, |
| 203 |
}, |
| 204 |
), |
| 205 |
# Custom Emails |
| 206 |
( |
| 207 |
"mailtos://user:[email protected]:567", |
| 208 |
{ |
| 209 |
"instance": email.NotifyEmail, |
| 210 |
}, |
| 211 |
), |
| 212 |
( |
| 213 |
"mailto://user:[email protected]?mode=ssl", |
| 214 |
{ |
| 215 |
# mailto:// with mode=ssl causes us to convert to ssl |
| 216 |
"instance": email.NotifyEmail, |
| 217 |
# Our expected url(privacy=True) startswith() response: |
| 218 |
"privacy_url": "mailtos://user:****@nuxref.com", |
| 219 |
}, |
| 220 |
), |
| 221 |
( |
| 222 |
"mailto://user:[email protected]:567?format=html", |
| 223 |
{ |
| 224 |
"instance": email.NotifyEmail, |
| 225 |
}, |
| 226 |
), |
| 227 |
( |
| 228 |
"mailtos://user:[email protected]:[email protected]", |
| 229 |
{ |
| 230 |
"instance": email.NotifyEmail, |
| 231 |
}, |
| 232 |
), |
| 233 |
( |
| 234 |
"mailtos://user:[email protected][email protected]", |
| 235 |
{ |
| 236 |
"instance": email.NotifyEmail, |
| 237 |
}, |
| 238 |
), |
| 239 |
( |
| 240 |
"mailtos://%[email protected][email protected]", |
| 241 |
{ |
| 242 |
"instance": email.NotifyEmail, |
| 243 |
}, |
| 244 |
), |
| 245 |
( |
| 246 |
"mailtos://%[email protected][email protected]&pgp=yes", |
| 247 |
{ |
| 248 |
# The legacy yes/no boolean spelling of pgp= is no longer |
| 249 |
# supported because it is now an unrecognized mode string. |
| 250 |
"instance": AppriseImproperlyConfigured, |
| 251 |
}, |
| 252 |
), |
| 253 |
( |
| 254 |
"mailtos://%[email protected][email protected]&pgp=encrypt", |
| 255 |
{ |
| 256 |
# Test canonical pgp=encrypt mode string; notify() fails because |
| 257 |
# no public key is available to encrypt with. |
| 258 |
"instance": email.NotifyEmail, |
| 259 |
"response": False, |
| 260 |
}, |
| 261 |
), |
| 262 |
( |
| 263 |
"mailtos://%[email protected][email protected]&pgp=e", |
| 264 |
{ |
| 265 |
# Test pgp= prefix shorthand ('e' -> 'encrypt'); notify() fails |
| 266 |
# because no public key is available to encrypt with. |
| 267 |
"instance": email.NotifyEmail, |
| 268 |
"response": False, |
| 269 |
}, |
| 270 |
), |
| 271 |
( |
| 272 |
"mailtos://%[email protected][email protected]&pgp=sign", |
| 273 |
{ |
| 274 |
# Test pgp=sign mode string; notify() fails because no private key |
| 275 |
# is provided and signing requires one. |
| 276 |
"instance": email.NotifyEmail, |
| 277 |
"response": False, |
| 278 |
}, |
| 279 |
), |
| 280 |
( |
| 281 |
"mailtos://%[email protected][email protected]&pgp=s", |
| 282 |
{ |
| 283 |
# Test pgp= prefix shorthand ('s' -> 'sign'); notify() fails |
| 284 |
# because no private key is provided. |
| 285 |
"instance": email.NotifyEmail, |
| 286 |
"response": False, |
| 287 |
}, |
| 288 |
), |
| 289 |
( |
| 290 |
"mailtos://%[email protected][email protected]&wkd=yes", |
| 291 |
{ |
| 292 |
# wkd=yes implies pgp=encrypt; notify() fails because no public |
| 293 |
# key can be discovered (no WKD server behind this test address). |
| 294 |
"instance": email.NotifyEmail, |
| 295 |
"response": False, |
| 296 |
}, |
| 297 |
), |
| 298 |
( |
| 299 |
"mailtos://%[email protected][email protected]" |
| 300 |
"&pgp=encrypt&pgppub=%2Fpath%2Fto%2Fpub.asc", |
| 301 |
{ |
| 302 |
# Encryption fails when pgppub= cannot be found. |
| 303 |
"instance": email.NotifyEmail, |
| 304 |
"response": False, |
| 305 |
}, |
| 306 |
), |
| 307 |
( |
| 308 |
"mailtos://user:[email protected]:567/[email protected]", |
| 309 |
{ |
| 310 |
"instance": email.NotifyEmail, |
| 311 |
}, |
| 312 |
), |
| 313 |
( |
| 314 |
( |
| 315 |
"mailto://user:[email protected]:[email protected]" |
| 316 |
"&pass=l2g@apprise!is!Awesome" |
| 317 |
), |
| 318 |
{ |
| 319 |
"instance": email.NotifyEmail, |
| 320 |
}, |
| 321 |
), |
| 322 |
( |
| 323 |
( |
| 324 |
"mailto://user:[email protected]:[email protected]" |
| 325 |
"&pass=l2g@apprise!is!Awesome&format=text" |
| 326 |
), |
| 327 |
{ |
| 328 |
"instance": email.NotifyEmail, |
| 329 |
}, |
| 330 |
), |
| 331 |
( |
| 332 |
# Test Carbon Copy |
| 333 |
( |
| 334 |
"mailtos://user:[email protected]?smtp=smtp.example.com" |
| 335 |
"&name=l2g&[email protected],[email protected]" |
| 336 |
), |
| 337 |
{ |
| 338 |
"instance": email.NotifyEmail, |
| 339 |
}, |
| 340 |
), |
| 341 |
( |
| 342 |
# Test Blind Carbon Copy |
| 343 |
( |
| 344 |
"mailtos://user:[email protected]?smtp=smtp.example.com" |
| 345 |
"&name=l2g&[email protected],[email protected]" |
| 346 |
), |
| 347 |
{ |
| 348 |
"instance": email.NotifyEmail, |
| 349 |
}, |
| 350 |
), |
| 351 |
( |
| 352 |
# Test Carbon Copy with bad email |
| 353 |
( |
| 354 |
"mailtos://user:[email protected]?smtp=smtp.example.com" |
| 355 |
"&name=l2g&[email protected],@" |
| 356 |
), |
| 357 |
{ |
| 358 |
"instance": email.NotifyEmail, |
| 359 |
}, |
| 360 |
), |
| 361 |
( |
| 362 |
# Test Blind Carbon Copy with bad email |
| 363 |
( |
| 364 |
"mailtos://user:[email protected]?smtp=smtp.example.com" |
| 365 |
"&name=l2g&[email protected],@" |
| 366 |
), |
| 367 |
{ |
| 368 |
"instance": email.NotifyEmail, |
| 369 |
}, |
| 370 |
), |
| 371 |
( |
| 372 |
# Test Reply To |
| 373 |
( |
| 374 |
"mailtos://user:[email protected]?smtp=smtp.example.com" |
| 375 |
"&name=l2g&[email protected],[email protected]" |
| 376 |
), |
| 377 |
{ |
| 378 |
"instance": email.NotifyEmail, |
| 379 |
}, |
| 380 |
), |
| 381 |
( |
| 382 |
# Test Reply To with bad email |
| 383 |
( |
| 384 |
"mailtos://user:[email protected]?smtp=smtp.example.com" |
| 385 |
"&name=l2g&[email protected],@" |
| 386 |
), |
| 387 |
{ |
| 388 |
"instance": email.NotifyEmail, |
| 389 |
}, |
| 390 |
), |
| 391 |
# headers |
| 392 |
( |
| 393 |
( |
| 394 |
"mailto://user:[email protected]" |
| 395 |
"?+X-Customer-Campaign-ID=Apprise" |
| 396 |
), |
| 397 |
{ |
| 398 |
"instance": email.NotifyEmail, |
| 399 |
}, |
| 400 |
), |
| 401 |
# No Password |
| 402 |
( |
| 403 |
"mailtos://user:@nuxref.com", |
| 404 |
{ |
| 405 |
"instance": email.NotifyEmail, |
| 406 |
}, |
| 407 |
), |
| 408 |
# Invalid From Address; but just gets put as the from name instead |
| 409 |
# Hence the below generats From: "@ <[email protected]>" |
| 410 |
( |
| 411 |
"mailtos://user:[email protected]?from=@", |
| 412 |
{ |
| 413 |
"instance": email.NotifyEmail, |
| 414 |
}, |
| 415 |
), |
| 416 |
# Invalid From Address |
| 417 |
( |
| 418 |
"mailtos://nuxref.com?user=&pass=.", |
| 419 |
{ |
| 420 |
"instance": AppriseImproperlyConfigured, |
| 421 |
}, |
| 422 |
), |
| 423 |
# Invalid To Address is accepted, but we won't be able to properly email |
| 424 |
# using the notify() call |
| 425 |
( |
| 426 |
"mailtos://user:[email protected]?to=@", |
| 427 |
{ |
| 428 |
"instance": email.NotifyEmail, |
| 429 |
"response": False, |
| 430 |
}, |
| 431 |
), |
| 432 |
# Valid URL, but can't structure a proper email |
| 433 |
( |
| 434 |
'mailtos://nuxref.com?user=%20"&pass=.', |
| 435 |
{ |
| 436 |
"instance": AppriseImproperlyConfigured, |
| 437 |
}, |
| 438 |
), |
| 439 |
# Invalid From (and To) Address |
| 440 |
( |
| 441 |
"mailtos://nuxref.com?to=test", |
| 442 |
{ |
| 443 |
"instance": AppriseImproperlyConfigured, |
| 444 |
}, |
| 445 |
), |
| 446 |
# Invalid Secure Mode |
| 447 |
( |
| 448 |
"mailtos://user:[email protected]?mode=notamode", |
| 449 |
{ |
| 450 |
"instance": AppriseImproperlyConfigured, |
| 451 |
}, |
| 452 |
), |
| 453 |
# STARTTLS flag checking |
| 454 |
( |
| 455 |
"mailtos://user:[email protected]?mode=starttls", |
| 456 |
{ |
| 457 |
"instance": email.NotifyEmail, |
| 458 |
# Our expected url(privacy=True) startswith() response: |
| 459 |
"privacy_url": "mailtos://user:****@gmail.com", |
| 460 |
}, |
| 461 |
), |
| 462 |
# SSL flag checking |
| 463 |
( |
| 464 |
"mailtos://user:[email protected]?mode=ssl", |
| 465 |
{ |
| 466 |
"instance": email.NotifyEmail, |
| 467 |
}, |
| 468 |
), |
| 469 |
# Can make a To address using what we have ([email protected]) |
| 470 |
( |
| 471 |
"mailtos://nuxref.com?user=l2g&pass=.", |
| 472 |
{ |
| 473 |
"instance": email.NotifyEmail, |
| 474 |
# Our expected url(privacy=True) startswith() response: |
| 475 |
"privacy_url": "mailtos://l2g:****@nuxref.com", |
| 476 |
}, |
| 477 |
), |
| 478 |
( |
| 479 |
"mailto://user:pass@localhost:2525", |
| 480 |
{ |
| 481 |
"instance": email.NotifyEmail, |
| 482 |
# Throws a series of i/o exceptions with this flag |
| 483 |
# is set and tests that we gracefully handle them |
| 484 |
"test_smtplib_exceptions": True, |
| 485 |
}, |
| 486 |
), |
| 487 |
# Use of both 'name' and 'from' together; these are synonymous |
| 488 |
( |
| 489 |
( |
| 490 |
"mailtos://user:[email protected]?" |
| 491 |
"[email protected]&name=Jason<[email protected]>" |
| 492 |
), |
| 493 |
{"instance": email.NotifyEmail}, |
| 494 |
), |
| 495 |
# Test no auth at all |
| 496 |
( |
| 497 |
"mailto://[email protected]&[email protected]", |
| 498 |
{ |
| 499 |
"instance": email.NotifyEmail, |
| 500 |
"privacy_url": "mailto://localhost", |
| 501 |
}, |
| 502 |
), |
| 503 |
# Test multi-emails where some are bad |
| 504 |
( |
| 505 |
"mailto://user:pass@localhost/[email protected]/test2@/$@!/", |
| 506 |
{ |
| 507 |
"instance": email.NotifyEmail, |
| 508 |
"privacy_url": "mailto://user:****@localhost/", |
| 509 |
}, |
| 510 |
), |
| 511 |
( |
| 512 |
"mailto://user:pass@localhost/?bcc=test2@,$@!/", |
| 513 |
{ |
| 514 |
"instance": email.NotifyEmail, |
| 515 |
}, |
| 516 |
), |
| 517 |
( |
| 518 |
"mailto://user:pass@localhost/?cc=test2@,$@!/", |
| 519 |
{ |
| 520 |
"instance": email.NotifyEmail, |
| 521 |
}, |
| 522 |
), |
| 523 |
( |
| 524 |
"mailto://user:pass@localhost/?reply=test2@,$@!/", |
| 525 |
{ |
| 526 |
"instance": email.NotifyEmail, |
| 527 |
}, |
| 528 |
), |
| 529 |
) |
| 530 |
|
| 531 |
|
| 532 |
@mock.patch("smtplib.SMTP") |
| 533 |
@mock.patch("smtplib.SMTP_SSL") |
| 534 |
def test_plugin_email(mock_smtp, mock_smtpssl): |
| 535 |
"""NotifyEmail() General Checks.""" |
| 536 |
|
| 537 |
# iterate over our dictionary and test it out |
| 538 |
for url, meta in TEST_URLS: |
| 539 |
# Our expected instance |
| 540 |
instance = meta.get("instance", None) |
| 541 |
|
| 542 |
# Our expected server objects |
| 543 |
self = meta.get("self", None) |
| 544 |
|
| 545 |
# Our expected Query response (True, False, or exception type) |
| 546 |
response = meta.get("response", True) |
| 547 |
|
| 548 |
# Our expected privacy url |
| 549 |
# Leave this unset when its value is not needed. |
| 550 |
privacy_url = meta.get("privacy_url") |
| 551 |
|
| 552 |
test_smtplib_exceptions = meta.get("test_smtplib_exceptions", False) |
| 553 |
|
| 554 |
# Our mock of our socket action |
| 555 |
mock_socket = mock.Mock() |
| 556 |
mock_socket.starttls.return_value = True |
| 557 |
mock_socket.login.return_value = True |
| 558 |
# An empty refusal map means SMTP accepted every recipient. |
| 559 |
mock_socket.sendmail.return_value = {} |
| 560 |
|
| 561 |
# Create a mock SMTP Object |
| 562 |
mock_smtp.return_value = mock_socket |
| 563 |
mock_smtpssl.return_value = mock_socket |
| 564 |
|
| 565 |
if test_smtplib_exceptions: |
| 566 |
# Handle exception testing; first we turn the boolean flag ito |
| 567 |
# a list of exceptions |
| 568 |
test_smtplib_exceptions = ( |
| 569 |
smtplib.SMTPHeloError( |
| 570 |
0, "smtplib.SMTPHeloError() not handled" |
| 571 |
), |
| 572 |
smtplib.SMTPException( |
| 573 |
0, "smtplib.SMTPException() not handled" |
| 574 |
), |
| 575 |
smtplib.SMTPRecipientsRefused( |
| 576 |
"smtplib.SMTPRecipientsRefused() not handled" |
| 577 |
), |
| 578 |
smtplib.SMTPSenderRefused( |
| 579 |
0, |
| 580 |
"smtplib.SMTPSenderRefused() not handled", |
| 581 |
"[email protected]", |
| 582 |
), |
| 583 |
smtplib.SMTPDataError( |
| 584 |
0, "smtplib.SMTPDataError() not handled" |
| 585 |
), |
| 586 |
smtplib.SMTPServerDisconnected( |
| 587 |
"smtplib.SMTPServerDisconnected() not handled" |
| 588 |
), |
| 589 |
) |
| 590 |
|
| 591 |
try: |
| 592 |
obj = Apprise.instantiate(url, suppress_exceptions=False) |
| 593 |
|
| 594 |
if obj is None: |
| 595 |
# We're done (assuming this is what we were expecting) |
| 596 |
assert instance is None |
| 597 |
continue |
| 598 |
|
| 599 |
if instance is None: |
| 600 |
# Expected None but didn't get it |
| 601 |
raise AssertionError() |
| 602 |
|
| 603 |
assert isinstance(obj, instance) |
| 604 |
|
| 605 |
if isinstance(obj, NotifyBase): |
| 606 |
# We loaded okay; now lets make sure we can reverse this url |
| 607 |
assert isinstance(obj.url(), str) |
| 608 |
|
| 609 |
# Get our URL Identifier |
| 610 |
assert isinstance(obj.url_id(), str) |
| 611 |
|
| 612 |
# Verify we can acquire a target count as an integer |
| 613 |
assert isinstance(len(obj), int) |
| 614 |
|
| 615 |
# Test url() with privacy=True |
| 616 |
assert isinstance(obj.url(privacy=True), str) |
| 617 |
|
| 618 |
# Some Simple Invalid Instance Testing |
| 619 |
assert instance.parse_url(None) is None |
| 620 |
assert instance.parse_url(object) is None |
| 621 |
assert instance.parse_url(42) is None |
| 622 |
|
| 623 |
if privacy_url and not obj.url(privacy=True).startswith( |
| 624 |
# Assess that our privacy url is as expected |
| 625 |
privacy_url |
| 626 |
): |
| 627 |
raise AssertionError( |
| 628 |
f"URL: {url} Privacy URL:" |
| 629 |
f" '{obj.url(privacy=True)[: len(privacy_url)]}' !=" |
| 630 |
f" expected '{privacy_url}'" |
| 631 |
) |
| 632 |
|
| 633 |
# Instantiate the exact same object again using the URL from |
| 634 |
# the one that was already created properly |
| 635 |
obj_cmp = Apprise.instantiate(obj.url()) |
| 636 |
|
| 637 |
# Our object should be the same instance as what we had |
| 638 |
# originally expected above. |
| 639 |
if not isinstance(obj_cmp, NotifyBase): |
| 640 |
# Assert messages are hard to trace back with the way |
| 641 |
# these tests work. Just printing before throwing our |
| 642 |
# assertion failure makes things easier to debug later on |
| 643 |
raise AssertionError() |
| 644 |
|
| 645 |
# Verify there is no change from the old and the new |
| 646 |
assert len(obj) == len(obj_cmp), ( |
| 647 |
f"{len(obj)} targets found in " |
| 648 |
f"{obj.url(privacy=True)}, " |
| 649 |
f"But {len(obj_cmp)} targets found in " |
| 650 |
f"{obj_cmp.url(privacy=True)}" |
| 651 |
) |
| 652 |
if self: |
| 653 |
# Iterate over our expected entries inside of our object |
| 654 |
for key, val in self.items(): |
| 655 |
# Test that our object has the desired key |
| 656 |
assert hasattr(key, obj) |
| 657 |
assert getattr(key, obj) == val |
| 658 |
|
| 659 |
try: |
| 660 |
if test_smtplib_exceptions is False: |
| 661 |
# Verify we can acquire a target count as an integer |
| 662 |
targets = len(obj) |
| 663 |
|
| 664 |
# check that we're as expected |
| 665 |
assert ( |
| 666 |
obj.notify( |
| 667 |
title="test", |
| 668 |
body="body", |
| 669 |
notify_type=NotifyType.INFO, |
| 670 |
) |
| 671 |
== response |
| 672 |
) |
| 673 |
|
| 674 |
if response: |
| 675 |
# If we successfully got a response, there must have |
| 676 |
# been at least 1 target present |
| 677 |
assert targets > 0 |
| 678 |
|
| 679 |
else: |
| 680 |
for exception in test_smtplib_exceptions: |
| 681 |
mock_socket.sendmail.side_effect = exception |
| 682 |
try: |
| 683 |
assert ( |
| 684 |
obj.notify( |
| 685 |
title="test", |
| 686 |
body="body", |
| 687 |
notify_type=NotifyType.INFO, |
| 688 |
) |
| 689 |
is False |
| 690 |
) |
| 691 |
|
| 692 |
except AssertionError: |
| 693 |
# Don't mess with these entries |
| 694 |
raise |
| 695 |
|
| 696 |
except Exception: |
| 697 |
# We can't handle this exception type |
| 698 |
raise |
| 699 |
|
| 700 |
except AssertionError: |
| 701 |
# Don't mess with these entries |
| 702 |
raise |
| 703 |
|
| 704 |
except Exception as e: |
| 705 |
# Check that we were expecting this exception to happen |
| 706 |
if not isinstance(e, response): |
| 707 |
raise |
| 708 |
|
| 709 |
except AssertionError: |
| 710 |
# Don't mess with these entries |
| 711 |
raise |
| 712 |
|
| 713 |
except Exception as e: |
| 714 |
# Handle our exception |
| 715 |
if instance is None: |
| 716 |
raise |
| 717 |
|
| 718 |
if not isinstance(e, instance): |
| 719 |
raise |
| 720 |
|
| 721 |
|
| 722 |
@mock.patch("smtplib.SMTP") |
| 723 |
@mock.patch("smtplib.SMTP_SSL") |
| 724 |
def test_plugin_email_webbase_lookup(mock_smtp, mock_smtpssl): |
| 725 |
"""NotifyEmail() Web Based Lookup Tests.""" |
| 726 |
|
| 727 |
# Insert a test email at the head of our table |
| 728 |
email.templates.EMAIL_TEMPLATES = ( |
| 729 |
*( |
| 730 |
( |
| 731 |
"Testing Lookup", |
| 732 |
re.compile(r"^(?P<id>[^@]+)@(?P<domain>l2g\.com)$", re.I), |
| 733 |
{ |
| 734 |
"port": 123, |
| 735 |
"smtp_host": "smtp.l2g.com", |
| 736 |
"secure": True, |
| 737 |
"login_type": (email.WebBaseLogin.USERID,), |
| 738 |
}, |
| 739 |
), |
| 740 |
), |
| 741 |
*email.templates.EMAIL_TEMPLATES, |
| 742 |
) |
| 743 |
|
| 744 |
obj = Apprise.instantiate( |
| 745 |
"mailto://user:[email protected]", |
| 746 |
suppress_exceptions=True, |
| 747 |
) |
| 748 |
|
| 749 |
assert isinstance(obj, email.NotifyEmail) |
| 750 |
assert len(obj.targets) == 1 |
| 751 |
assert (False, "[email protected]") in obj.targets |
| 752 |
assert obj.from_addr[0] == obj.app_id |
| 753 |
assert obj.from_addr[1] == "[email protected]" |
| 754 |
assert obj.password == "pass" |
| 755 |
assert obj.user == "user" |
| 756 |
assert obj.secure is True |
| 757 |
assert obj.port == 123 |
| 758 |
assert obj.smtp_host == "smtp.l2g.com" |
| 759 |
|
| 760 |
# We get the same results if an email is identified as the username |
| 761 |
# because the USERID variable forces that we can't use an email |
| 762 |
obj = Apprise.instantiate( |
| 763 |
"mailto://_:[email protected][email protected]", suppress_exceptions=True |
| 764 |
) |
| 765 |
assert obj.user == "user" |
| 766 |
|
| 767 |
|
| 768 |
@mock.patch("smtplib.SMTP") |
| 769 |
def test_plugin_email_smtplib_init_fail(mock_smtplib): |
| 770 |
"""NotifyEmail() Test exception handling when calling smtplib.SMTP()""" |
| 771 |
|
| 772 |
obj = Apprise.instantiate( |
| 773 |
"mailto://user:[email protected]", suppress_exceptions=False |
| 774 |
) |
| 775 |
assert isinstance(obj, email.NotifyEmail) |
| 776 |
|
| 777 |
# Support Exception handling of smtplib.SMTP |
| 778 |
mock_smtplib.side_effect = OSError("Test") |
| 779 |
|
| 780 |
assert ( |
| 781 |
bool( |
| 782 |
obj.notify(body="body", title="test", notify_type=NotifyType.INFO) |
| 783 |
) |
| 784 |
is False |
| 785 |
) |
| 786 |
|
| 787 |
# A handled and expected exception |
| 788 |
mock_smtplib.side_effect = smtplib.SMTPException("Test") |
| 789 |
assert ( |
| 790 |
bool( |
| 791 |
obj.notify(body="body", title="test", notify_type=NotifyType.INFO) |
| 792 |
) |
| 793 |
is False |
| 794 |
) |
| 795 |
|
| 796 |
# AppriseSMTPController re-raises RuntimeError (part of its own |
| 797 |
# SMTP_EXCEPTIONS catch-all in __enter__) after cleanup, so send() |
| 798 |
# must still catch it here rather than let it escape |
| 799 |
mock_smtplib.side_effect = RuntimeError("Test") |
| 800 |
assert ( |
| 801 |
bool( |
| 802 |
obj.notify(body="body", title="test", notify_type=NotifyType.INFO) |
| 803 |
) |
| 804 |
is False |
| 805 |
) |
| 806 |
|
| 807 |
|
| 808 |
@mock.patch("smtplib.SMTP") |
| 809 |
def test_plugin_email_smtplib_send_okay(mock_smtplib): |
| 810 |
"""NotifyEmail() Test a successfully sent email.""" |
| 811 |
|
| 812 |
# Defaults to HTML |
| 813 |
obj = Apprise.instantiate( |
| 814 |
"mailto://user:[email protected]", suppress_exceptions=False |
| 815 |
) |
| 816 |
assert isinstance(obj, email.NotifyEmail) |
| 817 |
|
| 818 |
# Support an email simulation where we can correctly quit |
| 819 |
mock_smtplib.return_value.starttls.return_value = True |
| 820 |
mock_smtplib.return_value.login.return_value = True |
| 821 |
mock_smtplib.return_value.sendmail.return_value = {} |
| 822 |
mock_smtplib.return_value.quit.return_value = True |
| 823 |
|
| 824 |
assert ( |
| 825 |
bool( |
| 826 |
obj.notify(body="body", title="test", notify_type=NotifyType.INFO) |
| 827 |
) |
| 828 |
is True |
| 829 |
) |
| 830 |
|
| 831 |
# Set Text |
| 832 |
obj = Apprise.instantiate( |
| 833 |
"mailto://user:[email protected]?format=text", suppress_exceptions=False |
| 834 |
) |
| 835 |
assert isinstance(obj, email.NotifyEmail) |
| 836 |
|
| 837 |
assert ( |
| 838 |
bool( |
| 839 |
obj.notify(body="body", title="test", notify_type=NotifyType.INFO) |
| 840 |
) |
| 841 |
is True |
| 842 |
) |
| 843 |
|
| 844 |
# Create an apprise object to work with as well |
| 845 |
a = Apprise() |
| 846 |
assert a.add("mailto://user:[email protected]?format=text") |
| 847 |
assert a.add("mailto://other:[email protected]?format=text") |
| 848 |
|
| 849 |
# Send Attachment with success |
| 850 |
attach = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 851 |
assert ( |
| 852 |
bool( |
| 853 |
obj.notify( |
| 854 |
body="body", |
| 855 |
title="test", |
| 856 |
notify_type=NotifyType.INFO, |
| 857 |
attach=attach, |
| 858 |
) |
| 859 |
) |
| 860 |
is True |
| 861 |
) |
| 862 |
|
| 863 |
# same results happen from our Apprise object |
| 864 |
assert bool(a.notify(body="body", title="test", attach=attach)) is True |
| 865 |
|
| 866 |
# test using an Apprise Attachment object |
| 867 |
assert ( |
| 868 |
bool( |
| 869 |
obj.notify( |
| 870 |
body="body", |
| 871 |
title="test", |
| 872 |
notify_type=NotifyType.INFO, |
| 873 |
attach=AppriseAttachment(attach), |
| 874 |
) |
| 875 |
) |
| 876 |
is True |
| 877 |
) |
| 878 |
|
| 879 |
# same results happen from our Apprise object |
| 880 |
assert ( |
| 881 |
bool( |
| 882 |
a.notify( |
| 883 |
body="body", title="test", attach=AppriseAttachment(attach) |
| 884 |
) |
| 885 |
) |
| 886 |
is True |
| 887 |
) |
| 888 |
|
| 889 |
# test using an Apprise in memory attachement object |
| 890 |
attachment = AppriseAttachment() |
| 891 |
attachment.add( |
| 892 |
AttachMemory( |
| 893 |
content="attachment", |
| 894 |
name="attached.txt", |
| 895 |
mimetype="text/plain", |
| 896 |
) |
| 897 |
) |
| 898 |
assert ( |
| 899 |
obj.notify( |
| 900 |
body="body", |
| 901 |
title="test", |
| 902 |
notify_type=NotifyType.INFO, |
| 903 |
attach=attachment, |
| 904 |
) |
| 905 |
is True |
| 906 |
) |
| 907 |
|
| 908 |
# same results happen from our Apprise object |
| 909 |
result = a.notify(body="body", title="test", attach=attachment) |
| 910 |
assert bool(result) is True |
| 911 |
assert len(result) == 2 |
| 912 |
|
| 913 |
max_file_size = AttachBase.max_file_size |
| 914 |
# Now do a case where the file can't be sent |
| 915 |
|
| 916 |
AttachBase.max_file_size = 1 |
| 917 |
assert ( |
| 918 |
bool( |
| 919 |
obj.notify( |
| 920 |
body="body", |
| 921 |
title="test", |
| 922 |
notify_type=NotifyType.INFO, |
| 923 |
attach=attach, |
| 924 |
) |
| 925 |
) |
| 926 |
is False |
| 927 |
) |
| 928 |
|
| 929 |
# same results happen from our Apprise object |
| 930 |
assert bool(a.notify(body="body", title="test", attach=attach)) is False |
| 931 |
|
| 932 |
# Restore value |
| 933 |
AttachBase.max_file_size = max_file_size |
| 934 |
|
| 935 |
|
| 936 |
@mock.patch("smtplib.SMTP") |
| 937 |
def test_plugin_email_smtplib_send_multiple_recipients(mock_smtplib): |
| 938 |
"""Verify that NotifyEmail() will use a single SMTP session for submitting |
| 939 |
multiple emails.""" |
| 940 |
|
| 941 |
# Defaults to HTML |
| 942 |
obj = Apprise.instantiate( |
| 943 |
"mailto://user:[email protected]?" |
| 944 |
"[email protected],[email protected]&" |
| 945 |
"[email protected]&[email protected]", |
| 946 |
suppress_exceptions=False, |
| 947 |
) |
| 948 |
assert isinstance(obj, email.NotifyEmail) |
| 949 |
mock_smtplib.return_value.sendmail.return_value = {} |
| 950 |
|
| 951 |
assert ( |
| 952 |
bool( |
| 953 |
obj.notify(body="body", title="test", notify_type=NotifyType.INFO) |
| 954 |
) |
| 955 |
is True |
| 956 |
) |
| 957 |
|
| 958 |
assert mock_smtplib.mock_calls == [ |
| 959 |
mock.call("mail.example.org", 25, None, timeout=15), |
| 960 |
mock.call().login("user", "pass"), |
| 961 |
mock.call().sendmail( |
| 962 |
"[email protected]", |
| 963 |
["[email protected]", "[email protected]", "[email protected]"], |
| 964 |
mock.ANY, |
| 965 |
), |
| 966 |
mock.call().sendmail( |
| 967 |
"[email protected]", |
| 968 |
["[email protected]", "[email protected]", "[email protected]"], |
| 969 |
mock.ANY, |
| 970 |
), |
| 971 |
mock.call().quit(), |
| 972 |
] |
| 973 |
|
| 974 |
# No from= used in the above |
| 975 |
assert re.match(r".*from=.*", obj.url()) is None |
| 976 |
# No mode= as this isn't a secure connection |
| 977 |
assert re.match(r".*mode=.*", obj.url()) is None |
| 978 |
# No smtp= as the SMTP server is the same as the hostname in this case |
| 979 |
assert re.match(r".*smtp=.*", obj.url()) is None |
| 980 |
# URL is assembled based on provided user |
| 981 |
assert ( |
| 982 |
re.match(r"^mailto://user:pass\@mail.example.org/.*", obj.url()) |
| 983 |
is not None |
| 984 |
) |
| 985 |
|
| 986 |
# Verify our added emails are still part of the URL |
| 987 |
assert re.match(r".*/foo%40example.net[/?].*", obj.url()) is not None |
| 988 |
assert re.match(r".*/bar%40example.com[/?].*", obj.url()) is not None |
| 989 |
|
| 990 |
assert re.match(r".*bcc=qux%40example.org.*", obj.url()) is not None |
| 991 |
assert re.match(r".*cc=baz%40example.org.*", obj.url()) is not None |
| 992 |
|
| 993 |
|
| 994 |
@mock.patch("smtplib.SMTP") |
| 995 |
def test_plugin_email_timezone(mock_smtp): |
| 996 |
"""NotifyEmail() Timezone Handling""" |
| 997 |
|
| 998 |
response = mock.Mock() |
| 999 |
# An empty refusal map means SMTP accepted every recipient. |
| 1000 |
response.sendmail.return_value = {} |
| 1001 |
mock_smtp.return_value = response |
| 1002 |
|
| 1003 |
# Loads America/Toronto |
| 1004 |
results = email.NotifyEmail.parse_url( |
| 1005 |
"mailtos://user:[email protected]:123?tz=Toronto" |
| 1006 |
) |
| 1007 |
assert isinstance(results, dict) |
| 1008 |
# timezone is detected |
| 1009 |
assert "tz" in results |
| 1010 |
|
| 1011 |
# Instantiate the object |
| 1012 |
obj = email.NotifyEmail(**results) |
| 1013 |
assert isinstance(obj, email.NotifyEmail) |
| 1014 |
assert obj.tzinfo.key == "America/Toronto" |
| 1015 |
# Verify our URL has defined our timezone |
| 1016 |
# %2F = escaped '/' |
| 1017 |
assert "tz=America%2FToronto" in obj.url() |
| 1018 |
|
| 1019 |
# No Timezone setup/default |
| 1020 |
results = email.NotifyEmail.parse_url( |
| 1021 |
"mailtos://user:[email protected]:1235" |
| 1022 |
) |
| 1023 |
assert "tz" not in results |
| 1024 |
|
| 1025 |
# Instantiate the object |
| 1026 |
obj = email.NotifyEmail(**results) |
| 1027 |
assert isinstance(obj, email.NotifyEmail) |
| 1028 |
# Defaults to our system |
| 1029 |
assert obj.tzinfo == datetime.now().astimezone().tzinfo |
| 1030 |
assert "tz=" not in obj.url() |
| 1031 |
|
| 1032 |
# Now we'll work with an Asset to identify how it can hold |
| 1033 |
# our default global variable (initialization proves case |
| 1034 |
# insensitive initialization is supported) |
| 1035 |
asset = AppriseAsset(timezone="aMErica/vanCOUver") |
| 1036 |
|
| 1037 |
# Instatiate our object once again using the same variable set |
| 1038 |
# as above |
| 1039 |
obj = email.NotifyEmail(**results, asset=asset) |
| 1040 |
# Defaults to our system |
| 1041 |
# lower() is required since Mac and Window are not case sensitive and will |
| 1042 |
# See output as it was passed in and not corrected per IANA |
| 1043 |
assert obj.tzinfo.key.lower() == "america/vancouver" |
| 1044 |
assert "tz=" not in obj.url() |
| 1045 |
|
| 1046 |
# Having ourselves a default variable also does not prevent |
| 1047 |
# A caller may still provide its own override: |
| 1048 |
|
| 1049 |
# Loads America/Montreal |
| 1050 |
results = email.NotifyEmail.parse_url( |
| 1051 |
"mailtos://user:[email protected]:321?tz=Montreal" |
| 1052 |
) |
| 1053 |
assert isinstance(results, dict) |
| 1054 |
# timezone is detected |
| 1055 |
assert "tz" in results |
| 1056 |
|
| 1057 |
# Instantiate the object |
| 1058 |
obj = email.NotifyEmail(**results) |
| 1059 |
assert isinstance(obj, email.NotifyEmail) |
| 1060 |
assert obj.tzinfo.key == "America/Montreal" |
| 1061 |
# Verify our URL has defined our timezone |
| 1062 |
# %2F = escaped '/' |
| 1063 |
assert "tz=America%2FMontreal" in obj.url() |
| 1064 |
|
| 1065 |
|
| 1066 |
@mock.patch("smtplib.SMTP") |
| 1067 |
def test_plugin_email_smtplib_internationalization(mock_smtp): |
| 1068 |
"""NotifyEmail() Internationalization Handling.""" |
| 1069 |
|
| 1070 |
# i18n test |
| 1071 |
email_url = "".join( |
| 1072 |
[ |
| 1073 |
"mailto://user:[email protected]?", |
| 1074 |
"name=Например%20так", # noqa: RUF001 |
| 1075 |
] |
| 1076 |
) |
| 1077 |
|
| 1078 |
obj = Apprise.instantiate( |
| 1079 |
email_url, |
| 1080 |
suppress_exceptions=False, |
| 1081 |
) |
| 1082 |
|
| 1083 |
assert isinstance(obj, email.NotifyEmail) |
| 1084 |
|
| 1085 |
class SMTPMock: |
| 1086 |
def sendmail(self, *args, **kwargs): |
| 1087 |
"""Capture sendmail calls to check international formatting.""" |
| 1088 |
|
| 1089 |
match_subject = re.search( |
| 1090 |
r"\n?(?P<line>Subject: (?P<subject>(.+?)))\n(?:[a-z0-9-]+:)", |
| 1091 |
args[2], |
| 1092 |
re.I | re.M | re.S, |
| 1093 |
) |
| 1094 |
assert match_subject is not None |
| 1095 |
|
| 1096 |
match_from = re.search( |
| 1097 |
r"^(?P<line>From: (?P<name>.+) <(?P<email>[^>]+)>)$", |
| 1098 |
args[2], |
| 1099 |
re.I | re.M, |
| 1100 |
) |
| 1101 |
assert match_from is not None |
| 1102 |
|
| 1103 |
# Verify our output was correctly stored |
| 1104 |
assert match_from.group("email") == "[email protected]" |
| 1105 |
|
| 1106 |
assert ( |
| 1107 |
decode_header(match_from.group("name"))[0][0].decode("utf-8") |
| 1108 |
== "Например так" |
| 1109 |
) |
| 1110 |
|
| 1111 |
assert ( |
| 1112 |
decode_header(match_subject.group("subject"))[0][0].decode( |
| 1113 |
"utf-8" |
| 1114 |
) |
| 1115 |
== "دعونا نجعل العالم مكانا أفضل." |
| 1116 |
) |
| 1117 |
|
| 1118 |
# Dummy Function |
| 1119 |
def quit(self, *args, **kwargs): |
| 1120 |
return True |
| 1121 |
|
| 1122 |
# Dummy Function |
| 1123 |
def starttls(self, *args, **kwargs): |
| 1124 |
return True |
| 1125 |
|
| 1126 |
# Dummy Function |
| 1127 |
def login(self, *args, **kwargs): |
| 1128 |
return True |
| 1129 |
|
| 1130 |
# Prepare our object we will test our generated email against |
| 1131 |
mock_smtp.return_value = SMTPMock() |
| 1132 |
|
| 1133 |
# Further test encoding through the message content as well |
| 1134 |
assert ( |
| 1135 |
bool( |
| 1136 |
obj.notify( |
| 1137 |
# Google Translated to Arabic: |
| 1138 |
# "Let's make the world a better place." |
| 1139 |
title="دعونا نجعل العالم مكانا أفضل.", |
| 1140 |
# Google Translated to Hungarian: "One line of code at a time.' |
| 1141 |
body="Egy sor kódot egyszerre.", |
| 1142 |
notify_type=NotifyType.INFO, |
| 1143 |
) |
| 1144 |
) |
| 1145 |
is True |
| 1146 |
) |
| 1147 |
|
| 1148 |
|
| 1149 |
def test_plugin_email_url_escaping(): |
| 1150 |
"""NotifyEmail() Test that user/passwords are properly escaped from URL.""" |
| 1151 |
# quote(' %20') |
| 1152 |
passwd = "%20%2520" |
| 1153 |
|
| 1154 |
# Basically we want to check that ' ' equates to %20 and % equates to %25 |
| 1155 |
# So the above translates to ' %20' (a space in front of %20). We want |
| 1156 |
# to verify the handling of the password escaping and when it happens. |
| 1157 |
# a very bad response would be ' ' (double space) |
| 1158 |
obj = email.NotifyEmail.parse_url( |
| 1159 |
f"mailto://user:{passwd}@gmail.com?format=text" |
| 1160 |
) |
| 1161 |
|
| 1162 |
assert isinstance(obj, dict) |
| 1163 |
assert "password" in obj |
| 1164 |
|
| 1165 |
# Escaping doesn't happen at this stage because we want to leave this to |
| 1166 |
# the plugins discretion |
| 1167 |
assert obj.get("password") == "%20%2520" |
| 1168 |
|
| 1169 |
obj = Apprise.instantiate( |
| 1170 |
f"mailto://user:{passwd}@gmail.com?format=text", |
| 1171 |
suppress_exceptions=False, |
| 1172 |
) |
| 1173 |
assert isinstance(obj, email.NotifyEmail) |
| 1174 |
|
| 1175 |
# The password is escaped only 'once' |
| 1176 |
assert obj.password == " %20" |
| 1177 |
|
| 1178 |
|
| 1179 |
def test_plugin_email_url_variations(): |
| 1180 |
"""NotifyEmail() Test URL variations to ensure parsing is correct.""" |
| 1181 |
# Test variations of username required to be an email address |
| 1182 |
# [email protected] |
| 1183 |
obj = Apprise.instantiate( |
| 1184 |
"mailto://{user}:{passwd}@example.com?smtp=example.com".format( |
| 1185 |
user="apprise%40example21.ca", passwd="abcd123" |
| 1186 |
), |
| 1187 |
suppress_exceptions=False, |
| 1188 |
) |
| 1189 |
assert isinstance(obj, email.NotifyEmail) |
| 1190 |
|
| 1191 |
assert obj.password == "abcd123" |
| 1192 |
assert obj.user == "[email protected]" |
| 1193 |
|
| 1194 |
# No from= used in the above |
| 1195 |
assert re.match(r".*from=.*", obj.url()) is None |
| 1196 |
# No mode= as this isn't a secure connection |
| 1197 |
assert re.match(r".*mode=.*", obj.url()) is None |
| 1198 |
# No smtp= as the SMTP server is the same as the hostname in this case |
| 1199 |
# even though it was explicitly specified |
| 1200 |
assert re.match(r".*smtp=.*", obj.url()) is None |
| 1201 |
# URL is assembled based on provided user |
| 1202 |
assert ( |
| 1203 |
re.match(r"^mailto://apprise:abcd123\@example.com/.*", obj.url()) |
| 1204 |
is not None |
| 1205 |
) |
| 1206 |
|
| 1207 |
# test username specified in the url body (as an argument) |
| 1208 |
# this always over-rides the entry at the front of the url |
| 1209 |
obj = Apprise.instantiate( |
| 1210 |
"mailto://_:{passwd}@example.com?user={user}".format( |
| 1211 |
user="apprise%40example21.ca", passwd="abcd123" |
| 1212 |
), |
| 1213 |
suppress_exceptions=False, |
| 1214 |
) |
| 1215 |
assert isinstance(obj, email.NotifyEmail) |
| 1216 |
|
| 1217 |
assert obj.password == "abcd123" |
| 1218 |
assert obj.user == "[email protected]" |
| 1219 |
|
| 1220 |
# No from= used in the above |
| 1221 |
assert re.match(r".*from=.*", obj.url()) is None |
| 1222 |
# No mode= as this isn't a secure connection |
| 1223 |
assert re.match(r".*mode=.*", obj.url()) is None |
| 1224 |
# No smtp= as the SMTP server is the same as the hostname in this case |
| 1225 |
assert re.match(r".*smtp=.*", obj.url()) is None |
| 1226 |
# URL is assembled based on provided user |
| 1227 |
assert ( |
| 1228 |
re.match(r"^mailto://apprise:abcd123\@example.com/.*", obj.url()) |
| 1229 |
is not None |
| 1230 |
) |
| 1231 |
|
| 1232 |
# test user and password specified in the url body (as an argument) |
| 1233 |
# this always over-rides the entries at the front of the url |
| 1234 |
obj = Apprise.instantiate( |
| 1235 |
"mailtos://_:[email protected]?user={user}&pass={passwd}".format( |
| 1236 |
user="apprise%40example21.ca", passwd="abcd123" |
| 1237 |
), |
| 1238 |
suppress_exceptions=False, |
| 1239 |
) |
| 1240 |
assert isinstance(obj, email.NotifyEmail) |
| 1241 |
|
| 1242 |
assert obj.password == "abcd123" |
| 1243 |
assert obj.user == "[email protected]" |
| 1244 |
assert len(obj.targets) == 1 |
| 1245 |
assert (False, "[email protected]") in obj.targets |
| 1246 |
assert obj.from_addr[0] == obj.app_id |
| 1247 |
assert obj.from_addr[1] == "[email protected]" |
| 1248 |
assert obj.targets[0][0] is False |
| 1249 |
assert obj.targets[0][1] == obj.from_addr[1] |
| 1250 |
|
| 1251 |
# No from= used in the above |
| 1252 |
assert re.match(r".*from=.*", obj.url()) is None |
| 1253 |
# Default mode is starttls |
| 1254 |
assert re.match(r".*mode=starttls.*", obj.url()) is not None |
| 1255 |
# No smtp= as the SMTP server is the same as the hostname in this case |
| 1256 |
assert re.match(r".*smtp=.*", obj.url()) is None |
| 1257 |
# URL is assembled based on provided user |
| 1258 |
assert ( |
| 1259 |
re.match(r"^mailtos://apprise:abcd123\@example.com/.*", obj.url()) |
| 1260 |
is not None |
| 1261 |
) |
| 1262 |
|
| 1263 |
# test user and password specified in the url body (as an argument) |
| 1264 |
# this always over-rides the entries at the front of the url |
| 1265 |
# this is similar to the previous test except we're only specifying |
| 1266 |
# this information in the kwargs |
| 1267 |
obj = Apprise.instantiate( |
| 1268 |
"mailto://example.com?user={user}&pass={passwd}".format( |
| 1269 |
user="apprise%40example21.ca", passwd="abcd123" |
| 1270 |
), |
| 1271 |
suppress_exceptions=False, |
| 1272 |
) |
| 1273 |
assert isinstance(obj, email.NotifyEmail) |
| 1274 |
|
| 1275 |
assert obj.password == "abcd123" |
| 1276 |
assert obj.user == "[email protected]" |
| 1277 |
assert len(obj.targets) == 1 |
| 1278 |
assert (False, "[email protected]") in obj.targets |
| 1279 |
assert obj.from_addr[0] == obj.app_id |
| 1280 |
assert obj.from_addr[1] == "[email protected]" |
| 1281 |
assert obj.targets[0][0] is False |
| 1282 |
assert obj.targets[0][1] == obj.from_addr[1] |
| 1283 |
assert obj.smtp_host == "example.com" |
| 1284 |
|
| 1285 |
# No from= used in the above |
| 1286 |
assert re.match(r".*from=.*", obj.url()) is None |
| 1287 |
# No mode= as this isn't a secure connection |
| 1288 |
assert re.match(r".*mode=.*", obj.url()) is None |
| 1289 |
# No smtp= as the SMTP server is the same as the hostname in this case |
| 1290 |
assert re.match(r".*smtp=.*", obj.url()) is None |
| 1291 |
# URL is assembled based on provided user |
| 1292 |
assert ( |
| 1293 |
re.match(r"^mailto://apprise:abcd123\@example.com/.*", obj.url()) |
| 1294 |
is not None |
| 1295 |
) |
| 1296 |
|
| 1297 |
# test a complicated example |
| 1298 |
obj = Apprise.instantiate( |
| 1299 |
"mailtos://{user}:{passwd}@{host}:{port}" |
| 1300 |
"?smtp={smtp_host}&format=text&from=Charles<{this}>&to={that}".format( |
| 1301 |
user="apprise%40example21.ca", |
| 1302 |
passwd="abcd123", |
| 1303 |
host="example.com", |
| 1304 |
port=1234, |
| 1305 |
this="[email protected]", |
| 1306 |
that="[email protected]", |
| 1307 |
smtp_host="smtp.example.edu", |
| 1308 |
), |
| 1309 |
suppress_exceptions=False, |
| 1310 |
) |
| 1311 |
assert isinstance(obj, email.NotifyEmail) |
| 1312 |
|
| 1313 |
assert obj.password == "abcd123" |
| 1314 |
assert obj.user == "[email protected]" |
| 1315 |
assert obj.host == "example.com" |
| 1316 |
assert obj.port == 1234 |
| 1317 |
assert obj.smtp_host == "smtp.example.edu" |
| 1318 |
assert len(obj.targets) == 1 |
| 1319 |
assert (False, "[email protected]") in obj.targets |
| 1320 |
assert obj.from_addr[0] == "Charles" |
| 1321 |
assert obj.from_addr[1] == "[email protected]" |
| 1322 |
assert ( |
| 1323 |
re.match(r".*from=Charles%20%3Cfrom%40example.jp%3E.*", obj.url()) |
| 1324 |
is not None |
| 1325 |
) |
| 1326 |
|
| 1327 |
# Test Tagging under various urll encodings |
| 1328 |
for toaddr in ( |
| 1329 |
"/[email protected]", |
| 1330 |
"[email protected]", |
| 1331 |
"/john.smith%[email protected]", |
| 1332 |
"?to=john.smith%[email protected]", |
| 1333 |
): |
| 1334 |
obj = Apprise.instantiate(f"mailto://user:[email protected]{toaddr}") |
| 1335 |
assert isinstance(obj, email.NotifyEmail) |
| 1336 |
assert obj.password == "pass" |
| 1337 |
assert obj.user == "user" |
| 1338 |
assert obj.host == "domain.com" |
| 1339 |
assert obj.from_addr[0] == obj.app_id |
| 1340 |
assert obj.from_addr[1] == "[email protected]" |
| 1341 |
assert len(obj.targets) == 1 |
| 1342 |
assert obj.targets[0][0] is False |
| 1343 |
assert obj.targets[0][1] == "[email protected]" |
| 1344 |
|
| 1345 |
|
| 1346 |
def test_plugin_email_dict_variations(): |
| 1347 |
"""NotifyEmail() Test email dictionary variations to ensure parsing is |
| 1348 |
correct.""" |
| 1349 |
# Test variations of username required to be an email address |
| 1350 |
# [email protected] |
| 1351 |
obj = Apprise.instantiate( |
| 1352 |
{ |
| 1353 |
"schema": "mailto", |
| 1354 |
"user": "[email protected]", |
| 1355 |
"password": "abd123", |
| 1356 |
"host": "example.com", |
| 1357 |
}, |
| 1358 |
suppress_exceptions=False, |
| 1359 |
) |
| 1360 |
assert isinstance(obj, email.NotifyEmail) |
| 1361 |
|
| 1362 |
|
| 1363 |
@mock.patch("smtplib.SMTP_SSL") |
| 1364 |
@mock.patch("smtplib.SMTP") |
| 1365 |
def test_plugin_email_url_parsing(mock_smtp, mock_smtp_ssl): |
| 1366 |
"""NotifyEmail() Test email url parsing.""" |
| 1367 |
|
| 1368 |
response = mock.Mock() |
| 1369 |
# An empty refusal map means SMTP accepted every recipient. |
| 1370 |
response.sendmail.return_value = {} |
| 1371 |
mock_smtp_ssl.return_value = response |
| 1372 |
mock_smtp.return_value = response |
| 1373 |
|
| 1374 |
# Accept a full email username and a custom template port. |
| 1375 |
results = email.NotifyEmail.parse_url( |
| 1376 |
"mailtos://user:[email protected]:444" |
| 1377 |
"[email protected]&name=test%20name" |
| 1378 |
) |
| 1379 |
assert isinstance(results, dict) |
| 1380 |
assert results["from_addr"] == "test name" |
| 1381 |
assert results["user"] == "user" |
| 1382 |
assert results["port"] == 444 |
| 1383 |
assert results["host"] == "hotmail.com" |
| 1384 |
assert results["password"] == "pass123" |
| 1385 |
assert "[email protected]" in results["targets"] |
| 1386 |
|
| 1387 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1388 |
assert isinstance(obj, email.NotifyEmail) |
| 1389 |
|
| 1390 |
assert mock_smtp.call_count == 0 |
| 1391 |
assert mock_smtp_ssl.call_count == 0 |
| 1392 |
assert bool(obj.notify("test")) is True |
| 1393 |
assert mock_smtp.call_count == 1 |
| 1394 |
assert mock_smtp_ssl.call_count == 0 |
| 1395 |
assert response.starttls.call_count == 1 |
| 1396 |
assert response.login.call_count == 1 |
| 1397 |
assert response.sendmail.call_count == 1 |
| 1398 |
# Store our Sent Arguments |
| 1399 |
# Syntax is: |
| 1400 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 1401 |
# [0] [1] [2] |
| 1402 |
from_ = response.sendmail.call_args[0][0] |
| 1403 |
to = response.sendmail.call_args[0][1] |
| 1404 |
msg = response.sendmail.call_args[0][2] |
| 1405 |
assert from_ == "[email protected]" |
| 1406 |
assert isinstance(to, list) |
| 1407 |
assert len(to) == 1 |
| 1408 |
assert to[0] == "[email protected]" |
| 1409 |
assert msg.split("\n")[-3] == "test" |
| 1410 |
|
| 1411 |
# Our URL port was over-ridden (on template) to use 444 |
| 1412 |
# We can verify that this was correctly saved |
| 1413 |
assert obj.url().startswith( |
| 1414 |
"mailtos://user:[email protected]:444/user2%40yahoo.com" |
| 1415 |
) |
| 1416 |
assert "mode=starttls" in obj.url() |
| 1417 |
assert "smtp=smtp-mail.outlook.com" in obj.url() |
| 1418 |
|
| 1419 |
mock_smtp.reset_mock() |
| 1420 |
mock_smtp_ssl.reset_mock() |
| 1421 |
response.reset_mock() |
| 1422 |
|
| 1423 |
# The below switches the `name` with the `to` to verify the results |
| 1424 |
# are the same; it also verfies that the mode gets changed to SSL |
| 1425 |
# instead of STARTTLS |
| 1426 |
results = email.NotifyEmail.parse_url( |
| 1427 |
"mailtos://user:[email protected]?smtp=override.com" |
| 1428 |
"&name=test%20name&[email protected]&mode=ssl" |
| 1429 |
) |
| 1430 |
assert isinstance(results, dict) |
| 1431 |
assert results["from_addr"] == "test name" |
| 1432 |
assert results["user"] == "user" |
| 1433 |
assert results["host"] == "hotmail.com" |
| 1434 |
assert results["password"] == "pass123" |
| 1435 |
assert "[email protected]" in results["targets"] |
| 1436 |
assert results["secure_mode"] == "ssl" |
| 1437 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1438 |
assert isinstance(obj, email.NotifyEmail) |
| 1439 |
|
| 1440 |
assert mock_smtp.call_count == 0 |
| 1441 |
assert mock_smtp_ssl.call_count == 0 |
| 1442 |
assert bool(obj.notify("test")) is True |
| 1443 |
assert mock_smtp.call_count == 0 |
| 1444 |
assert mock_smtp_ssl.call_count == 1 |
| 1445 |
assert response.starttls.call_count == 0 |
| 1446 |
assert response.login.call_count == 1 |
| 1447 |
assert response.sendmail.call_count == 1 |
| 1448 |
# Store our Sent Arguments |
| 1449 |
# Syntax is: |
| 1450 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 1451 |
# [0] [1] [2] |
| 1452 |
from_ = response.sendmail.call_args[0][0] |
| 1453 |
to = response.sendmail.call_args[0][1] |
| 1454 |
msg = response.sendmail.call_args[0][2] |
| 1455 |
assert from_ == "[email protected]" |
| 1456 |
assert isinstance(to, list) |
| 1457 |
assert len(to) == 1 |
| 1458 |
assert to[0] == "[email protected]" |
| 1459 |
assert msg.split("\n")[-3] == "test" |
| 1460 |
|
| 1461 |
user, pw = response.login.call_args[0] |
| 1462 |
# the SMTP Server was ovr |
| 1463 |
assert pw == "pass123" |
| 1464 |
assert user == "user" |
| 1465 |
|
| 1466 |
assert obj.url().startswith( |
| 1467 |
"mailtos://user:[email protected]/user2%40yahoo.com" |
| 1468 |
) |
| 1469 |
# Confirm the template override. |
| 1470 |
assert "mode=ssl" in obj.url() |
| 1471 |
assert "smtp=override.com" in obj.url() |
| 1472 |
# No reply address specified |
| 1473 |
assert "reply=" not in obj.url() |
| 1474 |
|
| 1475 |
mock_smtp.reset_mock() |
| 1476 |
mock_smtp_ssl.reset_mock() |
| 1477 |
response.reset_mock() |
| 1478 |
|
| 1479 |
# |
| 1480 |
# Test outlook/hotmail lookups |
| 1481 |
# |
| 1482 |
results = email.NotifyEmail.parse_url("mailtos://user:[email protected]") |
| 1483 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1484 |
assert isinstance(obj, email.NotifyEmail) |
| 1485 |
assert obj.smtp_host == "smtp-mail.outlook.com" |
| 1486 |
# No entries in the reply_to |
| 1487 |
assert not obj.reply_to |
| 1488 |
|
| 1489 |
assert mock_smtp.call_count == 0 |
| 1490 |
assert mock_smtp_ssl.call_count == 0 |
| 1491 |
assert response.starttls.call_count == 0 |
| 1492 |
assert bool(obj.notify("test")) is True |
| 1493 |
assert mock_smtp.call_count == 1 |
| 1494 |
assert mock_smtp_ssl.call_count == 0 |
| 1495 |
assert response.starttls.call_count == 1 |
| 1496 |
assert response.login.call_count == 1 |
| 1497 |
assert response.sendmail.call_count == 1 |
| 1498 |
|
| 1499 |
user, pw = response.login.call_args[0] |
| 1500 |
assert pw == "pass123" |
| 1501 |
assert user == "[email protected]" |
| 1502 |
|
| 1503 |
mock_smtp.reset_mock() |
| 1504 |
mock_smtp_ssl.reset_mock() |
| 1505 |
response.reset_mock() |
| 1506 |
|
| 1507 |
results = email.NotifyEmail.parse_url("mailtos://user:[email protected]") |
| 1508 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1509 |
assert isinstance(obj, email.NotifyEmail) |
| 1510 |
assert obj.smtp_host == "smtp.outlook.com" |
| 1511 |
# No entries in the reply_to |
| 1512 |
assert not obj.reply_to |
| 1513 |
|
| 1514 |
assert mock_smtp.call_count == 0 |
| 1515 |
assert mock_smtp_ssl.call_count == 0 |
| 1516 |
assert response.starttls.call_count == 0 |
| 1517 |
assert bool(obj.notify("test")) is True |
| 1518 |
assert mock_smtp.call_count == 1 |
| 1519 |
assert mock_smtp_ssl.call_count == 0 |
| 1520 |
assert response.starttls.call_count == 1 |
| 1521 |
assert response.login.call_count == 1 |
| 1522 |
assert response.sendmail.call_count == 1 |
| 1523 |
|
| 1524 |
user, pw = response.login.call_args[0] |
| 1525 |
assert pw == "pass123" |
| 1526 |
assert user == "[email protected]" |
| 1527 |
|
| 1528 |
mock_smtp.reset_mock() |
| 1529 |
mock_smtp_ssl.reset_mock() |
| 1530 |
response.reset_mock() |
| 1531 |
|
| 1532 |
results = email.NotifyEmail.parse_url( |
| 1533 |
"mailtos://user:[email protected]" |
| 1534 |
) |
| 1535 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1536 |
assert isinstance(obj, email.NotifyEmail) |
| 1537 |
assert obj.smtp_host == "smtp.outlook.com" |
| 1538 |
# No entries in the reply_to |
| 1539 |
assert not obj.reply_to |
| 1540 |
|
| 1541 |
assert mock_smtp.call_count == 0 |
| 1542 |
assert mock_smtp_ssl.call_count == 0 |
| 1543 |
assert response.starttls.call_count == 0 |
| 1544 |
assert bool(obj.notify("test")) is True |
| 1545 |
assert mock_smtp.call_count == 1 |
| 1546 |
assert mock_smtp_ssl.call_count == 0 |
| 1547 |
assert response.starttls.call_count == 1 |
| 1548 |
assert response.login.call_count == 1 |
| 1549 |
assert response.sendmail.call_count == 1 |
| 1550 |
|
| 1551 |
user, pw = response.login.call_args[0] |
| 1552 |
assert pw == "pass123" |
| 1553 |
assert user == "[email protected]" |
| 1554 |
|
| 1555 |
mock_smtp.reset_mock() |
| 1556 |
mock_smtp_ssl.reset_mock() |
| 1557 |
response.reset_mock() |
| 1558 |
|
| 1559 |
# Consisitency Checks |
| 1560 |
results = email.NotifyEmail.parse_url( |
| 1561 |
"mailtos://outlook.com?smtp=smtp.outlook.com" |
| 1562 |
"&[email protected]&pass=app.pw" |
| 1563 |
) |
| 1564 |
obj1 = Apprise.instantiate(results, suppress_exceptions=False) |
| 1565 |
assert isinstance(obj1, email.NotifyEmail) |
| 1566 |
assert obj1.smtp_host == "smtp.outlook.com" |
| 1567 |
assert obj1.user == "[email protected]" |
| 1568 |
assert obj1.password == "app.pw" |
| 1569 |
assert obj1.secure_mode == "starttls" |
| 1570 |
assert obj1.port == 587 |
| 1571 |
|
| 1572 |
assert mock_smtp.call_count == 0 |
| 1573 |
assert mock_smtp_ssl.call_count == 0 |
| 1574 |
assert response.starttls.call_count == 0 |
| 1575 |
assert bool(obj1.notify("test")) is True |
| 1576 |
assert mock_smtp.call_count == 1 |
| 1577 |
assert mock_smtp_ssl.call_count == 0 |
| 1578 |
assert response.starttls.call_count == 1 |
| 1579 |
assert response.login.call_count == 1 |
| 1580 |
assert response.sendmail.call_count == 1 |
| 1581 |
|
| 1582 |
user, pw = response.login.call_args[0] |
| 1583 |
assert pw == "app.pw" |
| 1584 |
assert user == "[email protected]" |
| 1585 |
|
| 1586 |
mock_smtp.reset_mock() |
| 1587 |
mock_smtp_ssl.reset_mock() |
| 1588 |
response.reset_mock() |
| 1589 |
|
| 1590 |
results = email.NotifyEmail.parse_url("mailtos://user:[email protected]") |
| 1591 |
obj2 = Apprise.instantiate(results, suppress_exceptions=False) |
| 1592 |
assert isinstance(obj2, email.NotifyEmail) |
| 1593 |
assert obj2.smtp_host == obj1.smtp_host |
| 1594 |
assert obj2.user == obj1.user |
| 1595 |
assert obj2.password == obj1.password |
| 1596 |
assert obj2.secure_mode == obj1.secure_mode |
| 1597 |
assert obj2.port == obj1.port |
| 1598 |
|
| 1599 |
assert mock_smtp.call_count == 0 |
| 1600 |
assert mock_smtp_ssl.call_count == 0 |
| 1601 |
assert response.starttls.call_count == 0 |
| 1602 |
assert bool(obj2.notify("test")) is True |
| 1603 |
assert mock_smtp.call_count == 1 |
| 1604 |
assert mock_smtp_ssl.call_count == 0 |
| 1605 |
assert response.starttls.call_count == 1 |
| 1606 |
assert response.login.call_count == 1 |
| 1607 |
assert response.sendmail.call_count == 1 |
| 1608 |
|
| 1609 |
user, pw = response.login.call_args[0] |
| 1610 |
assert pw == "app.pw" |
| 1611 |
assert user == "[email protected]" |
| 1612 |
|
| 1613 |
mock_smtp.reset_mock() |
| 1614 |
mock_smtp_ssl.reset_mock() |
| 1615 |
response.reset_mock() |
| 1616 |
|
| 1617 |
results = email.NotifyEmail.parse_url("mailto://user:[email protected]") |
| 1618 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1619 |
assert isinstance(obj, email.NotifyEmail) |
| 1620 |
assert obj.smtp_host == "smtp.comcast.net" |
| 1621 |
assert obj.user == "[email protected]" |
| 1622 |
assert obj.password == "pass" |
| 1623 |
assert obj.secure_mode == "ssl" |
| 1624 |
assert obj.port == 465 |
| 1625 |
|
| 1626 |
assert mock_smtp.call_count == 0 |
| 1627 |
assert mock_smtp_ssl.call_count == 0 |
| 1628 |
assert response.starttls.call_count == 0 |
| 1629 |
assert bool(obj.notify("test")) is True |
| 1630 |
assert mock_smtp.call_count == 0 |
| 1631 |
assert mock_smtp_ssl.call_count == 1 |
| 1632 |
assert response.starttls.call_count == 0 |
| 1633 |
assert response.login.call_count == 1 |
| 1634 |
assert response.sendmail.call_count == 1 |
| 1635 |
|
| 1636 |
user, pw = response.login.call_args[0] |
| 1637 |
assert pw == "pass" |
| 1638 |
assert user == "[email protected]" |
| 1639 |
|
| 1640 |
mock_smtp.reset_mock() |
| 1641 |
mock_smtp_ssl.reset_mock() |
| 1642 |
response.reset_mock() |
| 1643 |
|
| 1644 |
results = email.NotifyEmail.parse_url("mailtos://user:[email protected]") |
| 1645 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1646 |
assert isinstance(obj, email.NotifyEmail) |
| 1647 |
# No entries in the reply_to |
| 1648 |
assert not obj.reply_to |
| 1649 |
|
| 1650 |
assert mock_smtp.call_count == 0 |
| 1651 |
assert mock_smtp_ssl.call_count == 0 |
| 1652 |
assert response.starttls.call_count == 0 |
| 1653 |
assert bool(obj.notify("test")) is True |
| 1654 |
assert mock_smtp.call_count == 1 |
| 1655 |
assert mock_smtp_ssl.call_count == 0 |
| 1656 |
assert response.starttls.call_count == 1 |
| 1657 |
assert response.login.call_count == 1 |
| 1658 |
assert response.sendmail.call_count == 1 |
| 1659 |
|
| 1660 |
user, pw = response.login.call_args[0] |
| 1661 |
assert pw == "pass123" |
| 1662 |
assert user == "[email protected]" |
| 1663 |
|
| 1664 |
mock_smtp.reset_mock() |
| 1665 |
mock_smtp_ssl.reset_mock() |
| 1666 |
response.reset_mock() |
| 1667 |
|
| 1668 |
results = email.NotifyEmail.parse_url("mailtos://user:[email protected]") |
| 1669 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1670 |
assert isinstance(obj, email.NotifyEmail) |
| 1671 |
# No entries in the reply_to |
| 1672 |
assert not obj.reply_to |
| 1673 |
|
| 1674 |
assert mock_smtp.call_count == 0 |
| 1675 |
assert mock_smtp_ssl.call_count == 0 |
| 1676 |
assert response.starttls.call_count == 0 |
| 1677 |
assert bool(obj.notify("test")) is True |
| 1678 |
assert mock_smtp.call_count == 1 |
| 1679 |
assert mock_smtp_ssl.call_count == 0 |
| 1680 |
assert response.starttls.call_count == 1 |
| 1681 |
assert response.login.call_count == 1 |
| 1682 |
assert response.sendmail.call_count == 1 |
| 1683 |
|
| 1684 |
user, pw = response.login.call_args[0] |
| 1685 |
assert pw == "pass123" |
| 1686 |
assert user == "[email protected]" |
| 1687 |
|
| 1688 |
mock_smtp.reset_mock() |
| 1689 |
mock_smtp_ssl.reset_mock() |
| 1690 |
response.reset_mock() |
| 1691 |
|
| 1692 |
# |
| 1693 |
# Test Port Over-Riding |
| 1694 |
# |
| 1695 |
results = email.NotifyEmail.parse_url( |
| 1696 |
"mailtos://abc:[email protected]:465?smtp=smtp.exmail.qq.com&mode=ssl" |
| 1697 |
) |
| 1698 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1699 |
assert isinstance(obj, email.NotifyEmail) |
| 1700 |
|
| 1701 |
# Verify our over-rides are in place |
| 1702 |
assert obj.smtp_host == "smtp.exmail.qq.com" |
| 1703 |
assert obj.port == 465 |
| 1704 |
assert obj.from_addr[0] == obj.app_id |
| 1705 |
assert obj.from_addr[1] == "[email protected]" |
| 1706 |
assert obj.secure_mode == "ssl" |
| 1707 |
# No entries in the reply_to |
| 1708 |
assert not obj.reply_to |
| 1709 |
|
| 1710 |
# No from= used in the above |
| 1711 |
assert re.match(r".*from=.*", obj.url()) is None |
| 1712 |
# No Our secure connection is SSL |
| 1713 |
assert re.match(r".*mode=ssl.*", obj.url()) is not None |
| 1714 |
# No smtp= as the SMTP server is the same as the hostname in this case |
| 1715 |
assert re.match(r".*smtp=smtp.exmail.qq.com.*", obj.url()) is not None |
| 1716 |
# URL is assembled based on provided user (:465 is dropped because it |
| 1717 |
# is a default port when using xyz.cn) |
| 1718 |
assert ( |
| 1719 |
re.match(r"^mailtos://abc:[email protected]/.*", obj.url()) is not None |
| 1720 |
) |
| 1721 |
|
| 1722 |
assert mock_smtp.call_count == 0 |
| 1723 |
assert mock_smtp_ssl.call_count == 0 |
| 1724 |
assert response.starttls.call_count == 0 |
| 1725 |
assert bool(obj.notify("test")) is True |
| 1726 |
assert mock_smtp.call_count == 0 |
| 1727 |
assert mock_smtp_ssl.call_count == 1 |
| 1728 |
assert response.starttls.call_count == 0 |
| 1729 |
assert response.login.call_count == 1 |
| 1730 |
assert response.sendmail.call_count == 1 |
| 1731 |
|
| 1732 |
user, pw = response.login.call_args[0] |
| 1733 |
assert pw == "password" |
| 1734 |
assert user == "abc" |
| 1735 |
|
| 1736 |
mock_smtp.reset_mock() |
| 1737 |
mock_smtp_ssl.reset_mock() |
| 1738 |
response.reset_mock() |
| 1739 |
|
| 1740 |
results = email.NotifyEmail.parse_url( |
| 1741 |
"mailtos://abc:[email protected]?" |
| 1742 |
"smtp=smtp.exmail.qq.com&mode=ssl&port=465" |
| 1743 |
) |
| 1744 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1745 |
assert isinstance(obj, email.NotifyEmail) |
| 1746 |
|
| 1747 |
# Verify our over-rides are in place |
| 1748 |
assert obj.smtp_host == "smtp.exmail.qq.com" |
| 1749 |
assert obj.port == 465 |
| 1750 |
assert obj.from_addr[0] == obj.app_id |
| 1751 |
assert obj.from_addr[1] == "[email protected]" |
| 1752 |
assert obj.secure_mode == "ssl" |
| 1753 |
# No entries in the reply_to |
| 1754 |
assert not obj.reply_to |
| 1755 |
|
| 1756 |
assert mock_smtp.call_count == 0 |
| 1757 |
assert mock_smtp_ssl.call_count == 0 |
| 1758 |
assert response.starttls.call_count == 0 |
| 1759 |
assert bool(obj.notify("test")) is True |
| 1760 |
assert mock_smtp.call_count == 0 |
| 1761 |
assert mock_smtp_ssl.call_count == 1 |
| 1762 |
assert response.starttls.call_count == 0 |
| 1763 |
assert response.login.call_count == 1 |
| 1764 |
assert response.sendmail.call_count == 1 |
| 1765 |
|
| 1766 |
user, pw = response.login.call_args[0] |
| 1767 |
assert pw == "password" |
| 1768 |
assert user == "abc" |
| 1769 |
|
| 1770 |
mock_smtp.reset_mock() |
| 1771 |
mock_smtp_ssl.reset_mock() |
| 1772 |
response.reset_mock() |
| 1773 |
|
| 1774 |
# |
| 1775 |
# Test Reply-To Email |
| 1776 |
# |
| 1777 |
results = email.NotifyEmail.parse_url( |
| 1778 |
"mailtos://user:[email protected][email protected]" |
| 1779 |
) |
| 1780 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1781 |
assert isinstance(obj, email.NotifyEmail) |
| 1782 |
# Verify our over-rides are in place |
| 1783 |
assert obj.smtp_host == "example.com" |
| 1784 |
assert obj.from_addr[0] == obj.app_id |
| 1785 |
assert obj.from_addr[1] == "[email protected]" |
| 1786 |
assert obj.secure_mode == "starttls" |
| 1787 |
assert obj.url().startswith("mailtos://user:[email protected]") |
| 1788 |
# Confirm the template override. |
| 1789 |
assert "reply=noreply%40example.com" in obj.url() |
| 1790 |
|
| 1791 |
assert mock_smtp.call_count == 0 |
| 1792 |
assert mock_smtp_ssl.call_count == 0 |
| 1793 |
assert response.starttls.call_count == 0 |
| 1794 |
assert bool(obj.notify("test")) is True |
| 1795 |
assert mock_smtp.call_count == 1 |
| 1796 |
assert mock_smtp_ssl.call_count == 0 |
| 1797 |
assert response.starttls.call_count == 1 |
| 1798 |
assert response.login.call_count == 1 |
| 1799 |
assert response.sendmail.call_count == 1 |
| 1800 |
|
| 1801 |
user, pw = response.login.call_args[0] |
| 1802 |
assert pw == "pass" |
| 1803 |
assert user == "user" |
| 1804 |
|
| 1805 |
mock_smtp.reset_mock() |
| 1806 |
mock_smtp_ssl.reset_mock() |
| 1807 |
response.reset_mock() |
| 1808 |
|
| 1809 |
# |
| 1810 |
# Test Reply-To Email with Name Inline |
| 1811 |
# |
| 1812 |
results = email.NotifyEmail.parse_url( |
| 1813 |
"mailtos://user:[email protected]?reply=Chris<[email protected]>" |
| 1814 |
) |
| 1815 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1816 |
assert isinstance(obj, email.NotifyEmail) |
| 1817 |
# Verify our over-rides are in place |
| 1818 |
assert obj.smtp_host == "example.com" |
| 1819 |
assert obj.from_addr[0] == obj.app_id |
| 1820 |
assert obj.from_addr[1] == "[email protected]" |
| 1821 |
assert obj.secure_mode == "starttls" |
| 1822 |
assert obj.url().startswith("mailtos://user:[email protected]") |
| 1823 |
# Confirm the template override. |
| 1824 |
assert "reply=Chris%20%3Cnoreply%40example.ca%3E" in obj.url() |
| 1825 |
|
| 1826 |
assert mock_smtp.call_count == 0 |
| 1827 |
assert mock_smtp_ssl.call_count == 0 |
| 1828 |
assert response.starttls.call_count == 0 |
| 1829 |
assert bool(obj.notify("test")) is True |
| 1830 |
assert mock_smtp.call_count == 1 |
| 1831 |
assert mock_smtp_ssl.call_count == 0 |
| 1832 |
assert response.starttls.call_count == 1 |
| 1833 |
assert response.login.call_count == 1 |
| 1834 |
assert response.sendmail.call_count == 1 |
| 1835 |
|
| 1836 |
user, pw = response.login.call_args[0] |
| 1837 |
assert pw == "pass" |
| 1838 |
assert user == "user" |
| 1839 |
|
| 1840 |
mock_smtp.reset_mock() |
| 1841 |
mock_smtp_ssl.reset_mock() |
| 1842 |
response.reset_mock() |
| 1843 |
|
| 1844 |
# Fast Mail Handling |
| 1845 |
|
| 1846 |
# Accept a full email username and a custom template port. |
| 1847 |
results = email.NotifyEmail.parse_url( |
| 1848 |
"mailto://fastmail.com/[email protected]" |
| 1849 |
"&[email protected]&pass=abc123" |
| 1850 |
"&from=Concordium Explorer Bot<[email protected]>" |
| 1851 |
) |
| 1852 |
assert isinstance(results, dict) |
| 1853 |
assert ( |
| 1854 |
results["from_addr"] |
| 1855 |
== "Concordium Explorer Bot<[email protected]>" |
| 1856 |
) |
| 1857 |
assert results["user"] == "[email protected]" |
| 1858 |
assert results["port"] is None |
| 1859 |
assert results["host"] == "fastmail.com" |
| 1860 |
assert results["password"] == "abc123" |
| 1861 |
assert "[email protected]" in results["targets"] |
| 1862 |
|
| 1863 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1864 |
assert isinstance(obj, email.NotifyEmail) |
| 1865 |
|
| 1866 |
assert mock_smtp.call_count == 0 |
| 1867 |
assert mock_smtp_ssl.call_count == 0 |
| 1868 |
assert bool(obj.notify("test")) is True |
| 1869 |
assert mock_smtp.call_count == 0 |
| 1870 |
assert mock_smtp_ssl.call_count == 1 |
| 1871 |
assert response.starttls.call_count == 0 |
| 1872 |
assert response.login.call_count == 1 |
| 1873 |
assert response.sendmail.call_count == 1 |
| 1874 |
# Store our Sent Arguments |
| 1875 |
# Syntax is: |
| 1876 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 1877 |
# [0] [1] [2] |
| 1878 |
from_ = response.sendmail.call_args[0][0] |
| 1879 |
to = response.sendmail.call_args[0][1] |
| 1880 |
msg = response.sendmail.call_args[0][2] |
| 1881 |
assert from_ == "[email protected]" |
| 1882 |
assert isinstance(to, list) |
| 1883 |
assert len(to) == 1 |
| 1884 |
assert to[0] == "[email protected]" |
| 1885 |
assert msg.split("\n")[-3] == "test" |
| 1886 |
|
| 1887 |
user, pw = response.login.call_args[0] |
| 1888 |
assert pw == "abc123" |
| 1889 |
assert user == "[email protected]" |
| 1890 |
|
| 1891 |
mock_smtp.reset_mock() |
| 1892 |
mock_smtp_ssl.reset_mock() |
| 1893 |
response.reset_mock() |
| 1894 |
|
| 1895 |
# Omitting to= sends to the authenticated account itself: |
| 1896 |
# mailto://[email protected] \ |
| 1897 |
# &[email protected]&pass=password123 |
| 1898 |
# mailto://[email protected]&pass=password123 |
| 1899 |
# See https://github.com/caronc/apprise/issues/1040 |
| 1900 |
results = email.NotifyEmail.parse_url( |
| 1901 |
"mailto://[email protected]&pass=password123" |
| 1902 |
) |
| 1903 |
assert isinstance(results, dict) |
| 1904 |
assert results["user"] == "[email protected]" |
| 1905 |
assert results["from_addr"] == "" |
| 1906 |
assert results["port"] is None |
| 1907 |
assert results["host"] == "fastmail.com" |
| 1908 |
assert results["password"] == "password123" |
| 1909 |
assert results["smtp_host"] == "" |
| 1910 |
|
| 1911 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1912 |
assert isinstance(obj, email.NotifyEmail) |
| 1913 |
# During instantiation, our variables get detected |
| 1914 |
assert obj.smtp_host == "smtp.fastmail.com" |
| 1915 |
assert obj.from_addr == ["Apprise", "[email protected]"] |
| 1916 |
assert obj.host == "customdomain.com" |
| 1917 |
# detected from |
| 1918 |
assert (False, "[email protected]") in obj.targets |
| 1919 |
|
| 1920 |
assert mock_smtp.call_count == 0 |
| 1921 |
assert mock_smtp_ssl.call_count == 0 |
| 1922 |
assert bool(obj.notify("test")) is True |
| 1923 |
assert mock_smtp.call_count == 0 |
| 1924 |
assert mock_smtp_ssl.call_count == 1 |
| 1925 |
assert response.starttls.call_count == 0 |
| 1926 |
assert response.login.call_count == 1 |
| 1927 |
assert response.sendmail.call_count == 1 |
| 1928 |
# Store our Sent Arguments |
| 1929 |
# Syntax is: |
| 1930 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 1931 |
# [0] [1] [2] |
| 1932 |
from_ = response.sendmail.call_args[0][0] |
| 1933 |
to = response.sendmail.call_args[0][1] |
| 1934 |
msg = response.sendmail.call_args[0][2] |
| 1935 |
assert from_ == "[email protected]" |
| 1936 |
assert isinstance(to, list) |
| 1937 |
assert len(to) == 1 |
| 1938 |
assert to[0] == "[email protected]" |
| 1939 |
assert msg.split("\n")[-3] == "test" |
| 1940 |
|
| 1941 |
user, pw = response.login.call_args[0] |
| 1942 |
assert pw == "password123" |
| 1943 |
assert user == "[email protected]" |
| 1944 |
|
| 1945 |
mock_smtp.reset_mock() |
| 1946 |
mock_smtp_ssl.reset_mock() |
| 1947 |
response.reset_mock() |
| 1948 |
|
| 1949 |
# Confirm that From= can be overridden. |
| 1950 |
# with these custom URLs as well and not require a full email |
| 1951 |
results = email.NotifyEmail.parse_url( |
| 1952 |
"mailto://[email protected]" |
| 1953 |
"&pass=password123&from=Custom" |
| 1954 |
) |
| 1955 |
assert isinstance(results, dict) |
| 1956 |
assert results["user"] == "[email protected]" |
| 1957 |
assert results["from_addr"] == "Custom" |
| 1958 |
assert results["port"] is None |
| 1959 |
assert results["host"] == "fastmail.com" |
| 1960 |
assert results["password"] == "password123" |
| 1961 |
assert results["smtp_host"] == "" |
| 1962 |
|
| 1963 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 1964 |
assert isinstance(obj, email.NotifyEmail) |
| 1965 |
# During instantiation, our variables get detected |
| 1966 |
assert obj.smtp_host == "smtp.fastmail.com" |
| 1967 |
assert obj.from_addr == ["Custom", "[email protected]"] |
| 1968 |
assert obj.host == "customdomain.com" |
| 1969 |
# detected from |
| 1970 |
assert (False, "[email protected]") in obj.targets |
| 1971 |
|
| 1972 |
assert mock_smtp.call_count == 0 |
| 1973 |
assert mock_smtp_ssl.call_count == 0 |
| 1974 |
assert bool(obj.notify("test")) is True |
| 1975 |
assert mock_smtp.call_count == 0 |
| 1976 |
assert mock_smtp_ssl.call_count == 1 |
| 1977 |
assert response.starttls.call_count == 0 |
| 1978 |
assert response.login.call_count == 1 |
| 1979 |
assert response.sendmail.call_count == 1 |
| 1980 |
# Store our Sent Arguments |
| 1981 |
# Syntax is: |
| 1982 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 1983 |
# [0] [1] [2] |
| 1984 |
from_ = response.sendmail.call_args[0][0] |
| 1985 |
to = response.sendmail.call_args[0][1] |
| 1986 |
msg = response.sendmail.call_args[0][2] |
| 1987 |
assert from_ == "[email protected]" |
| 1988 |
assert isinstance(to, list) |
| 1989 |
assert len(to) == 1 |
| 1990 |
assert to[0] == "[email protected]" |
| 1991 |
assert msg.split("\n")[-3] == "test" |
| 1992 |
|
| 1993 |
user, pw = response.login.call_args[0] |
| 1994 |
assert pw == "password123" |
| 1995 |
assert user == "[email protected]" |
| 1996 |
|
| 1997 |
mock_smtp.reset_mock() |
| 1998 |
mock_smtp_ssl.reset_mock() |
| 1999 |
response.reset_mock() |
| 2000 |
|
| 2001 |
# Issue github.com/caronc/apprise/issue/941 |
| 2002 |
|
| 2003 |
# mail domain = mail-domain.com |
| 2004 |
# host domain = domain.subdomain.com |
| 2005 |
# PASSWORD needs to be fetched since a user= was provided |
| 2006 |
# - this is an edge case that is tested here |
| 2007 |
results = email.NotifyEmail.parse_url( |
| 2008 |
"mailtos://[email protected]:587?" |
| 2009 |
"[email protected]&[email protected]" |
| 2010 |
) |
| 2011 |
assert isinstance(results, dict) |
| 2012 |
# From_Addr could not be detected at this stage, but will be |
| 2013 |
# handled during instantiation |
| 2014 |
assert results["from_addr"] == "" |
| 2015 |
assert results["user"] == "[email protected]" |
| 2016 |
assert results["port"] == 587 |
| 2017 |
assert results["host"] == "domain.subdomain.com" |
| 2018 |
assert results["password"] == "PASSWORD" |
| 2019 |
assert "[email protected]" in results["targets"] |
| 2020 |
|
| 2021 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 2022 |
assert isinstance(obj, email.NotifyEmail) |
| 2023 |
|
| 2024 |
# Not that our from_address takes on '[email protected]' |
| 2025 |
assert obj.from_addr == ["Apprise", "[email protected]"] |
| 2026 |
|
| 2027 |
assert mock_smtp.call_count == 0 |
| 2028 |
assert mock_smtp_ssl.call_count == 0 |
| 2029 |
assert response.starttls.call_count == 0 |
| 2030 |
assert bool(obj.notify("test")) is True |
| 2031 |
assert mock_smtp.call_count == 1 |
| 2032 |
assert response.starttls.call_count == 1 |
| 2033 |
assert mock_smtp_ssl.call_count == 0 |
| 2034 |
assert response.login.call_count == 1 |
| 2035 |
assert response.sendmail.call_count == 1 |
| 2036 |
# Store our Sent Arguments |
| 2037 |
# Syntax is: |
| 2038 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 2039 |
# [0] [1] [2] |
| 2040 |
from_ = response.sendmail.call_args[0][0] |
| 2041 |
to = response.sendmail.call_args[0][1] |
| 2042 |
msg = response.sendmail.call_args[0][2] |
| 2043 |
assert from_ == "[email protected]" |
| 2044 |
assert isinstance(to, list) |
| 2045 |
assert len(to) == 1 |
| 2046 |
assert to[0] == "[email protected]" |
| 2047 |
assert msg.split("\n")[-3] == "test" |
| 2048 |
|
| 2049 |
user, pw = response.login.call_args[0] |
| 2050 |
assert user == "[email protected]" |
| 2051 |
assert pw == "PASSWORD" |
| 2052 |
|
| 2053 |
|
| 2054 |
@mock.patch("smtplib.SMTP_SSL") |
| 2055 |
@mock.patch("smtplib.SMTP") |
| 2056 |
def test_plugin_email_plus_in_toemail(mock_smtp, mock_smtp_ssl): |
| 2057 |
"""NotifyEmail() support + in To Email address.""" |
| 2058 |
|
| 2059 |
response = mock.Mock() |
| 2060 |
# An empty refusal map means SMTP accepted every recipient. |
| 2061 |
response.sendmail.return_value = {} |
| 2062 |
mock_smtp_ssl.return_value = response |
| 2063 |
mock_smtp.return_value = response |
| 2064 |
|
| 2065 |
# We want to test the case where a + is found in the To address; we want to |
| 2066 |
# ensure that it is supported |
| 2067 |
results = email.NotifyEmail.parse_url( |
| 2068 |
"mailtos://user:[email protected]" |
| 2069 |
"?to=Plus Support<[email protected]>" |
| 2070 |
) |
| 2071 |
assert isinstance(results, dict) |
| 2072 |
assert results["user"] == "user" |
| 2073 |
assert results["host"] == "gmail.com" |
| 2074 |
assert results["password"] == "pass123" |
| 2075 |
assert results["port"] is None |
| 2076 |
assert "Plus Support<[email protected]>" in results["targets"] |
| 2077 |
|
| 2078 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 2079 |
assert isinstance(obj, email.NotifyEmail) |
| 2080 |
|
| 2081 |
assert len(obj.targets) == 1 |
| 2082 |
assert ("Plus Support", "[email protected]") in obj.targets |
| 2083 |
assert obj.smtp_host == "smtp.gmail.com" |
| 2084 |
assert obj.from_addr == ["Apprise", "[email protected]"] |
| 2085 |
assert obj.host == "gmail.com" |
| 2086 |
|
| 2087 |
assert mock_smtp.call_count == 0 |
| 2088 |
assert mock_smtp_ssl.call_count == 0 |
| 2089 |
assert bool(obj.notify("test")) is True |
| 2090 |
assert mock_smtp.call_count == 1 |
| 2091 |
assert mock_smtp_ssl.call_count == 0 |
| 2092 |
assert response.starttls.call_count == 1 |
| 2093 |
assert response.login.call_count == 1 |
| 2094 |
assert response.sendmail.call_count == 1 |
| 2095 |
# Store our Sent Arguments |
| 2096 |
# Syntax is: |
| 2097 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 2098 |
# [0] [1] [2] |
| 2099 |
from_ = response.sendmail.call_args[0][0] |
| 2100 |
to = response.sendmail.call_args[0][1] |
| 2101 |
msg = response.sendmail.call_args[0][2] |
| 2102 |
assert from_ == "[email protected]" |
| 2103 |
assert isinstance(to, list) |
| 2104 |
assert len(to) == 1 |
| 2105 |
assert to[0] == "[email protected]" |
| 2106 |
assert msg.split("\n")[-3] == "test" |
| 2107 |
|
| 2108 |
mock_smtp.reset_mock() |
| 2109 |
mock_smtp_ssl.reset_mock() |
| 2110 |
response.reset_mock() |
| 2111 |
|
| 2112 |
# |
| 2113 |
# Perform the same test where the To field jsut contains the + in the |
| 2114 |
# address |
| 2115 |
# |
| 2116 |
results = email.NotifyEmail.parse_url( |
| 2117 |
"mailtos://user:[email protected][email protected]" |
| 2118 |
) |
| 2119 |
assert isinstance(results, dict) |
| 2120 |
assert results["user"] == "user" |
| 2121 |
assert results["host"] == "gmail.com" |
| 2122 |
assert results["password"] == "pass123" |
| 2123 |
assert results["port"] is None |
| 2124 |
assert "[email protected]" in results["targets"] |
| 2125 |
|
| 2126 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 2127 |
assert isinstance(obj, email.NotifyEmail) |
| 2128 |
|
| 2129 |
assert len(obj.targets) == 1 |
| 2130 |
assert (False, "[email protected]") in obj.targets |
| 2131 |
|
| 2132 |
assert mock_smtp.call_count == 0 |
| 2133 |
assert mock_smtp_ssl.call_count == 0 |
| 2134 |
assert bool(obj.notify("test")) is True |
| 2135 |
assert mock_smtp.call_count == 1 |
| 2136 |
assert mock_smtp_ssl.call_count == 0 |
| 2137 |
assert response.starttls.call_count == 1 |
| 2138 |
assert response.login.call_count == 1 |
| 2139 |
assert response.sendmail.call_count == 1 |
| 2140 |
# Store our Sent Arguments |
| 2141 |
# Syntax is: |
| 2142 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 2143 |
# [0] [1] [2] |
| 2144 |
from_ = response.sendmail.call_args[0][0] |
| 2145 |
to = response.sendmail.call_args[0][1] |
| 2146 |
msg = response.sendmail.call_args[0][2] |
| 2147 |
assert from_ == "[email protected]" |
| 2148 |
assert isinstance(to, list) |
| 2149 |
assert len(to) == 1 |
| 2150 |
assert to[0] == "[email protected]" |
| 2151 |
assert msg.split("\n")[-3] == "test" |
| 2152 |
|
| 2153 |
mock_smtp.reset_mock() |
| 2154 |
mock_smtp_ssl.reset_mock() |
| 2155 |
response.reset_mock() |
| 2156 |
|
| 2157 |
# |
| 2158 |
# Perform the same test where the To field is in the URL itself |
| 2159 |
# |
| 2160 |
results = email.NotifyEmail.parse_url( |
| 2161 |
"mailtos://user:[email protected]/[email protected]" |
| 2162 |
) |
| 2163 |
assert isinstance(results, dict) |
| 2164 |
assert results["user"] == "user" |
| 2165 |
assert results["host"] == "gmail.com" |
| 2166 |
assert results["password"] == "pass123" |
| 2167 |
assert results["port"] is None |
| 2168 |
assert "[email protected]" in results["targets"] |
| 2169 |
|
| 2170 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 2171 |
assert isinstance(obj, email.NotifyEmail) |
| 2172 |
|
| 2173 |
assert len(obj.targets) == 1 |
| 2174 |
assert (False, "[email protected]") in obj.targets |
| 2175 |
|
| 2176 |
assert mock_smtp.call_count == 0 |
| 2177 |
assert mock_smtp_ssl.call_count == 0 |
| 2178 |
assert bool(obj.notify("test")) is True |
| 2179 |
assert mock_smtp.call_count == 1 |
| 2180 |
assert mock_smtp_ssl.call_count == 0 |
| 2181 |
assert response.starttls.call_count == 1 |
| 2182 |
assert response.login.call_count == 1 |
| 2183 |
assert response.sendmail.call_count == 1 |
| 2184 |
# Store our Sent Arguments |
| 2185 |
# Syntax is: |
| 2186 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 2187 |
# [0] [1] [2] |
| 2188 |
from_ = response.sendmail.call_args[0][0] |
| 2189 |
to = response.sendmail.call_args[0][1] |
| 2190 |
msg = response.sendmail.call_args[0][2] |
| 2191 |
assert from_ == "[email protected]" |
| 2192 |
assert isinstance(to, list) |
| 2193 |
assert len(to) == 1 |
| 2194 |
assert to[0] == "[email protected]" |
| 2195 |
assert msg.split("\n")[-3] == "test" |
| 2196 |
|
| 2197 |
|
| 2198 |
@mock.patch("smtplib.SMTP_SSL") |
| 2199 |
@mock.patch("smtplib.SMTP") |
| 2200 |
def test_plugin_email_formatting_990(mock_smtp, mock_smtp_ssl): |
| 2201 |
""" |
| 2202 |
NotifyEmail() GitHub Issue 990 |
| 2203 |
https://github.com/caronc/apprise/issues/990 |
| 2204 |
Email formatting not working correctly |
| 2205 |
|
| 2206 |
""" |
| 2207 |
|
| 2208 |
response = mock.Mock() |
| 2209 |
# An empty refusal map means SMTP accepted every recipient. |
| 2210 |
response.sendmail.return_value = {} |
| 2211 |
mock_smtp_ssl.return_value = response |
| 2212 |
mock_smtp.return_value = response |
| 2213 |
|
| 2214 |
results = email.NotifyEmail.parse_url( |
| 2215 |
"mailtos://mydomain.com?smtp=mail.local.mydomain.com" |
| 2216 |
"&[email protected]&pass=mypassword" |
| 2217 |
"&[email protected]&[email protected]&mode=ssl&port=465" |
| 2218 |
) |
| 2219 |
|
| 2220 |
assert isinstance(results, dict) |
| 2221 |
assert results["user"] == "[email protected]" |
| 2222 |
assert results["host"] == "mydomain.com" |
| 2223 |
assert results["smtp_host"] == "mail.local.mydomain.com" |
| 2224 |
assert results["password"] == "mypassword" |
| 2225 |
assert results["secure_mode"] == "ssl" |
| 2226 |
assert results["port"] == "465" |
| 2227 |
assert "[email protected]" in results["targets"] |
| 2228 |
|
| 2229 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 2230 |
assert isinstance(obj, email.NotifyEmail) |
| 2231 |
|
| 2232 |
assert len(obj.targets) == 1 |
| 2233 |
assert (False, "[email protected]") in obj.targets |
| 2234 |
|
| 2235 |
|
| 2236 |
def test_plugin_email_variables_1087(): |
| 2237 |
""" |
| 2238 |
NotifyEmail() GitHub Issue 1087 |
| 2239 |
https://github.com/caronc/apprise/issues/1087 |
| 2240 |
Email variables reported not working correctly |
| 2241 |
|
| 2242 |
""" |
| 2243 |
|
| 2244 |
# Valid Configuration |
| 2245 |
result, _ = ConfigBase.config_parse( |
| 2246 |
cleandoc(""" |
| 2247 |
# |
| 2248 |
# Test Email Parsing |
| 2249 |
# |
| 2250 |
urls: |
| 2251 |
- mailtos://alt.lan/: |
| 2252 |
- user: [email protected] |
| 2253 |
pass: xxxxXXXxxx |
| 2254 |
smtp: smtp.alt.lan |
| 2255 |
to: [email protected] |
| 2256 |
"""), |
| 2257 |
asset=AppriseAsset(), |
| 2258 |
) |
| 2259 |
|
| 2260 |
assert isinstance(result, list) |
| 2261 |
assert len(result) == 1 |
| 2262 |
|
| 2263 |
email_ = result[0] |
| 2264 |
assert email_.from_addr == ["Apprise", "[email protected]"] |
| 2265 |
assert email_.user == "[email protected]" |
| 2266 |
assert email_.smtp_host == "smtp.alt.lan" |
| 2267 |
assert email_.targets == [(False, "[email protected]")] |
| 2268 |
assert email_.password == "xxxxXXXxxx" |
| 2269 |
|
| 2270 |
# Valid Configuration |
| 2271 |
result, _ = ConfigBase.config_parse( |
| 2272 |
cleandoc(""" |
| 2273 |
# |
| 2274 |
# YAML tokens override query-string values. |
| 2275 |
# |
| 2276 |
urls: |
| 2277 |
- mailtos://alt.lan/?pass=abcd&[email protected]: |
| 2278 |
- user: [email protected] |
| 2279 |
pass: xxxxXXXxxx |
| 2280 |
smtp: smtp.alt.lan |
| 2281 |
to: [email protected] |
| 2282 |
"""), |
| 2283 |
asset=AppriseAsset(), |
| 2284 |
) |
| 2285 |
|
| 2286 |
assert isinstance(result, list) |
| 2287 |
assert len(result) == 1 |
| 2288 |
|
| 2289 |
# YAML child values override URL query values. |
| 2290 |
email_ = result[0] |
| 2291 |
assert email_.from_addr == ["Apprise", "[email protected]"] |
| 2292 |
assert email_.user == "[email protected]" |
| 2293 |
assert email_.smtp_host == "smtp.alt.lan" |
| 2294 |
assert email_.targets == [(False, "[email protected]")] |
| 2295 |
assert email_.password == "xxxxXXXxxx" |
| 2296 |
|
| 2297 |
|
| 2298 |
@mock.patch("smtplib.SMTP_SSL") |
| 2299 |
@mock.patch("smtplib.SMTP") |
| 2300 |
def test_plugin_email_to_handling_1356(mock_smtp, mock_smtp_ssl): |
| 2301 |
""" |
| 2302 |
NotifyEmail() GitHub Issue 1356 |
| 2303 |
https://github.com/caronc/apprise/issues/1356 |
| 2304 |
Email not correctly preparing the `to:` |
| 2305 |
|
| 2306 |
""" |
| 2307 |
|
| 2308 |
response = mock.Mock() |
| 2309 |
# An empty refusal map means SMTP accepted every recipient. |
| 2310 |
response.sendmail.return_value = {} |
| 2311 |
mock_smtp_ssl.return_value = response |
| 2312 |
mock_smtp.return_value = response |
| 2313 |
|
| 2314 |
results = email.NotifyEmail.parse_url( |
| 2315 |
"mailtos://smtp-relay.gmail.com?" |
| 2316 |
"[email protected]&[email protected]" |
| 2317 |
) |
| 2318 |
|
| 2319 |
assert isinstance(results, dict) |
| 2320 |
assert results["user"] is None |
| 2321 |
assert results["password"] is None |
| 2322 |
assert results["host"] == "smtp-relay.gmail.com" |
| 2323 |
assert results["port"] is None |
| 2324 |
assert results["from_addr"] == "[email protected]" |
| 2325 |
assert results["smtp_host"] == "" |
| 2326 |
assert "[email protected]" in results["targets"] |
| 2327 |
|
| 2328 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 2329 |
assert isinstance(obj, email.NotifyEmail) |
| 2330 |
|
| 2331 |
assert len(obj.targets) == 1 |
| 2332 |
assert (False, "[email protected]") in obj.targets |
| 2333 |
|
| 2334 |
assert obj.smtp_host == "smtp-relay.gmail.com" |
| 2335 |
assert obj.from_addr == (False, "[email protected]") |
| 2336 |
|
| 2337 |
assert mock_smtp.call_count == 0 |
| 2338 |
assert mock_smtp_ssl.call_count == 0 |
| 2339 |
assert bool(obj.notify("body", "title")) is True |
| 2340 |
|
| 2341 |
assert mock_smtp.call_count == 1 |
| 2342 |
assert mock_smtp_ssl.call_count == 0 |
| 2343 |
assert response.starttls.call_count == 1 |
| 2344 |
# No login occurs as no user/pass was provided |
| 2345 |
assert response.login.call_count == 0 |
| 2346 |
assert response.sendmail.call_count == 1 |
| 2347 |
|
| 2348 |
# Store our Sent Arguments |
| 2349 |
# Syntax is: |
| 2350 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 2351 |
# [0] [1] [2] |
| 2352 |
from_ = response.sendmail.call_args[0][0] |
| 2353 |
to = response.sendmail.call_args[0][1] |
| 2354 |
msg = response.sendmail.call_args[0][2] |
| 2355 |
assert from_ == "[email protected]" |
| 2356 |
assert isinstance(to, list) |
| 2357 |
assert len(to) == 1 |
| 2358 |
assert to[0] == "[email protected]" |
| 2359 |
assert msg.split("\n")[-3] == "body" |
| 2360 |
|
| 2361 |
|
| 2362 |
@mock.patch("smtplib.SMTP_SSL") |
| 2363 |
@mock.patch("smtplib.SMTP") |
| 2364 |
def test_plugin_email_variables_1334(mock_smtp, mock_smtp_ssl): |
| 2365 |
""" |
| 2366 |
NotifyEmail() GitHub Issue 1334 |
| 2367 |
https://github.com/caronc/apprise/issues/1334 |
| 2368 |
Localhost & Local Domain default user |
| 2369 |
|
| 2370 |
""" |
| 2371 |
|
| 2372 |
response = mock.Mock() |
| 2373 |
# An empty refusal map means SMTP accepted every recipient. |
| 2374 |
response.sendmail.return_value = {} |
| 2375 |
mock_smtp_ssl.return_value = response |
| 2376 |
mock_smtp.return_value = response |
| 2377 |
|
| 2378 |
results = email.NotifyEmail.parse_url("mailto://localhost") |
| 2379 |
|
| 2380 |
assert isinstance(results, dict) |
| 2381 |
assert results["user"] is None |
| 2382 |
assert results["password"] is None |
| 2383 |
assert results["host"] == "localhost" |
| 2384 |
assert results["port"] is None |
| 2385 |
assert results["from_addr"] == "" |
| 2386 |
assert results["smtp_host"] == "" |
| 2387 |
assert results["targets"] == [] |
| 2388 |
|
| 2389 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 2390 |
assert isinstance(obj, email.NotifyEmail) |
| 2391 |
|
| 2392 |
assert len(obj.targets) == 1 |
| 2393 |
assert (False, "root@localhost") in obj.targets |
| 2394 |
|
| 2395 |
assert obj.smtp_host == "localhost" |
| 2396 |
assert obj.secure is False |
| 2397 |
assert obj.from_addr == [False, "root@localhost"] |
| 2398 |
|
| 2399 |
assert mock_smtp.call_count == 0 |
| 2400 |
assert mock_smtp_ssl.call_count == 0 |
| 2401 |
assert bool(obj.notify("body", "title")) is True |
| 2402 |
|
| 2403 |
assert mock_smtp.call_count == 1 |
| 2404 |
assert mock_smtp_ssl.call_count == 0 |
| 2405 |
assert response.starttls.call_count == 0 |
| 2406 |
# No login occurs as no user/pass was provided |
| 2407 |
assert response.login.call_count == 0 |
| 2408 |
assert response.sendmail.call_count == 1 |
| 2409 |
|
| 2410 |
# |
| 2411 |
# Again, but a different variation of the localhost domain |
| 2412 |
# |
| 2413 |
mock_smtp.reset_mock() |
| 2414 |
mock_smtp_ssl.reset_mock() |
| 2415 |
response.reset_mock() |
| 2416 |
|
| 2417 |
results = email.NotifyEmail.parse_url( |
| 2418 |
"mailtos://[email protected]" |
| 2419 |
) |
| 2420 |
|
| 2421 |
assert isinstance(results, dict) |
| 2422 |
assert results["user"] == "user" |
| 2423 |
assert results["password"] is None |
| 2424 |
assert results["host"] == "localhost.localdomain" |
| 2425 |
assert results["port"] is None |
| 2426 |
assert results["from_addr"] == "" |
| 2427 |
assert results["smtp_host"] == "" |
| 2428 |
assert results["targets"] == [] |
| 2429 |
|
| 2430 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 2431 |
assert isinstance(obj, email.NotifyEmail) |
| 2432 |
|
| 2433 |
assert len(obj.targets) == 1 |
| 2434 |
assert (False, "[email protected]") in obj.targets |
| 2435 |
|
| 2436 |
assert obj.smtp_host == "localhost.localdomain" |
| 2437 |
assert obj.secure is True |
| 2438 |
assert obj.from_addr == ["Apprise", "[email protected]"] |
| 2439 |
|
| 2440 |
assert mock_smtp.call_count == 0 |
| 2441 |
assert mock_smtp_ssl.call_count == 0 |
| 2442 |
assert bool(obj.notify("body", "title")) is True |
| 2443 |
|
| 2444 |
assert mock_smtp.call_count == 1 |
| 2445 |
assert mock_smtp_ssl.call_count == 0 |
| 2446 |
assert response.starttls.call_count == 1 |
| 2447 |
# No login occurs as no user/pass was provided |
| 2448 |
assert response.login.call_count == 0 |
| 2449 |
assert response.sendmail.call_count == 1 |
| 2450 |
|
| 2451 |
|
| 2452 |
@mock.patch("smtplib.SMTP_SSL") |
| 2453 |
@mock.patch("smtplib.SMTP") |
| 2454 |
def test_plugin_host_detection_from_source_email(mock_smtp, mock_smtp_ssl): |
| 2455 |
"""NotifyEmail() Discord Issue reporting that the following did not work: |
| 2456 |
|
| 2457 |
mailtos://?smtp=mobile.charter.net&pass=password&[email protected] |
| 2458 |
""" |
| 2459 |
|
| 2460 |
response = mock.Mock() |
| 2461 |
# An empty refusal map means SMTP accepted every recipient. |
| 2462 |
response.sendmail.return_value = {} |
| 2463 |
mock_smtp_ssl.return_value = response |
| 2464 |
mock_smtp.return_value = response |
| 2465 |
|
| 2466 |
results = email.NotifyEmail.parse_url( |
| 2467 |
"mailtos://spectrum.net?smtp=mobile.charter.net" |
| 2468 |
"&pass=password&[email protected]" |
| 2469 |
) |
| 2470 |
|
| 2471 |
assert isinstance(results, dict) |
| 2472 |
assert results["user"] == "[email protected]" |
| 2473 |
assert results["host"] == "spectrum.net" |
| 2474 |
assert results["smtp_host"] == "mobile.charter.net" |
| 2475 |
assert results["password"] == "password" |
| 2476 |
|
| 2477 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 2478 |
assert isinstance(obj, email.NotifyEmail) is True |
| 2479 |
|
| 2480 |
assert len(obj.targets) == 1 |
| 2481 |
assert (False, "[email protected]") in obj.targets |
| 2482 |
assert obj.from_addr[0] == obj.app_id |
| 2483 |
assert obj.from_addr[1] == "[email protected]" |
| 2484 |
assert obj.password == "password" |
| 2485 |
assert obj.user == "[email protected]" |
| 2486 |
assert obj.secure is True |
| 2487 |
assert obj.port == 587 |
| 2488 |
assert obj.smtp_host == "mobile.charter.net" |
| 2489 |
|
| 2490 |
assert mock_smtp.call_count == 0 |
| 2491 |
assert mock_smtp_ssl.call_count == 0 |
| 2492 |
assert bool(obj.notify("body", "title")) is True |
| 2493 |
|
| 2494 |
assert mock_smtp.call_count == 1 |
| 2495 |
assert mock_smtp_ssl.call_count == 0 |
| 2496 |
assert response.starttls.call_count == 1 |
| 2497 |
assert response.login.call_count == 1 |
| 2498 |
assert response.sendmail.call_count == 1 |
| 2499 |
# Store our Sent Arguments |
| 2500 |
# Syntax is: |
| 2501 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 2502 |
# [0] [1] [2] |
| 2503 |
from_ = response.sendmail.call_args[0][0] |
| 2504 |
to = response.sendmail.call_args[0][1] |
| 2505 |
msg = response.sendmail.call_args[0][2] |
| 2506 |
assert from_ == "[email protected]" |
| 2507 |
assert isinstance(to, list) |
| 2508 |
assert len(to) == 1 |
| 2509 |
assert to[0] == "[email protected]" |
| 2510 |
assert msg.split("\n")[-3] == "body" |
| 2511 |
|
| 2512 |
# |
| 2513 |
# Now let's do a shortened version of the same URL where the host isn't |
| 2514 |
# specified but is parseable from he user login |
| 2515 |
# |
| 2516 |
mock_smtp.reset_mock() |
| 2517 |
mock_smtp_ssl.reset_mock() |
| 2518 |
response.reset_mock() |
| 2519 |
|
| 2520 |
results = email.NotifyEmail.parse_url( |
| 2521 |
"mailtos://?smtp=mobile.charter.net" |
| 2522 |
"&pass=password&[email protected]" |
| 2523 |
) |
| 2524 |
|
| 2525 |
assert isinstance(results, dict) |
| 2526 |
assert results["user"] == "[email protected]" |
| 2527 |
assert results["host"] == "" # No hostname; it is detected later. |
| 2528 |
assert results["smtp_host"] == "mobile.charter.net" |
| 2529 |
assert results["password"] == "password" |
| 2530 |
|
| 2531 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 2532 |
assert isinstance(obj, email.NotifyEmail) is True |
| 2533 |
|
| 2534 |
assert len(obj.targets) == 1 |
| 2535 |
assert (False, "[email protected]") in obj.targets |
| 2536 |
assert obj.from_addr[0] == obj.app_id |
| 2537 |
assert obj.from_addr[1] == "[email protected]" |
| 2538 |
assert obj.password == "password" |
| 2539 |
assert obj.user == "[email protected]" |
| 2540 |
assert obj.secure is True |
| 2541 |
assert obj.port == 587 |
| 2542 |
assert obj.smtp_host == "mobile.charter.net" |
| 2543 |
|
| 2544 |
assert mock_smtp.call_count == 0 |
| 2545 |
assert mock_smtp_ssl.call_count == 0 |
| 2546 |
assert bool(obj.notify("body", "title")) is True |
| 2547 |
|
| 2548 |
assert mock_smtp.call_count == 1 |
| 2549 |
assert mock_smtp_ssl.call_count == 0 |
| 2550 |
assert response.starttls.call_count == 1 |
| 2551 |
assert response.login.call_count == 1 |
| 2552 |
assert response.sendmail.call_count == 1 |
| 2553 |
# Store our Sent Arguments |
| 2554 |
# Syntax is: |
| 2555 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 2556 |
# [0] [1] [2] |
| 2557 |
from_ = response.sendmail.call_args[0][0] |
| 2558 |
to = response.sendmail.call_args[0][1] |
| 2559 |
msg = response.sendmail.call_args[0][2] |
| 2560 |
assert from_ == "[email protected]" |
| 2561 |
assert isinstance(to, list) |
| 2562 |
assert len(to) == 1 |
| 2563 |
assert to[0] == "[email protected]" |
| 2564 |
assert msg.split("\n")[-3] == "body" |
| 2565 |
|
| 2566 |
# |
| 2567 |
# Now let's do a shortened version of the same URL where the host isn't |
| 2568 |
# specified but is parseable from he user login |
| 2569 |
# |
| 2570 |
mock_smtp.reset_mock() |
| 2571 |
mock_smtp_ssl.reset_mock() |
| 2572 |
response.reset_mock() |
| 2573 |
|
| 2574 |
results = email.NotifyEmail.parse_url( |
| 2575 |
"mailtos://?smtp=mobile.charter.net" |
| 2576 |
"&pass=password&user=userid-without-domain" |
| 2577 |
) |
| 2578 |
|
| 2579 |
assert isinstance(results, dict) |
| 2580 |
assert results["user"] == "userid-without-domain" |
| 2581 |
assert results["host"] == "" # No hostname defined |
| 2582 |
assert results["smtp_host"] == "mobile.charter.net" |
| 2583 |
assert results["password"] == "password" |
| 2584 |
|
| 2585 |
with pytest.raises(AppriseImproperlyConfigured): |
| 2586 |
# We will fail |
| 2587 |
Apprise.instantiate(results, suppress_exceptions=False) |
| 2588 |
|
| 2589 |
# |
| 2590 |
# Now support target emails in place of the hostname |
| 2591 |
# |
| 2592 |
|
| 2593 |
mock_smtp.reset_mock() |
| 2594 |
mock_smtp_ssl.reset_mock() |
| 2595 |
response.reset_mock() |
| 2596 |
|
| 2597 |
results = email.NotifyEmail.parse_url( |
| 2598 |
"mailtos://John Doe<john%40yahoo.ca>?smtp=mobile.charter.net" |
| 2599 |
"&pass=password&[email protected]" |
| 2600 |
) |
| 2601 |
|
| 2602 |
assert isinstance(results, dict) |
| 2603 |
assert results["user"] == "[email protected]" |
| 2604 |
assert results["host"] == "" # No hostname; it is detected later. |
| 2605 |
assert results["smtp_host"] == "mobile.charter.net" |
| 2606 |
assert results["password"] == "password" |
| 2607 |
|
| 2608 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 2609 |
assert isinstance(obj, email.NotifyEmail) is True |
| 2610 |
|
| 2611 |
assert len(obj.targets) == 1 |
| 2612 |
assert ("John Doe", "[email protected]") in obj.targets |
| 2613 |
assert obj.from_addr[0] == obj.app_id |
| 2614 |
assert obj.from_addr[1] == "[email protected]" |
| 2615 |
assert obj.password == "password" |
| 2616 |
assert obj.user == "[email protected]" |
| 2617 |
assert obj.secure is True |
| 2618 |
assert obj.port == 587 |
| 2619 |
assert obj.smtp_host == "mobile.charter.net" |
| 2620 |
|
| 2621 |
assert mock_smtp.call_count == 0 |
| 2622 |
assert mock_smtp_ssl.call_count == 0 |
| 2623 |
assert bool(obj.notify("body", "title")) is True |
| 2624 |
|
| 2625 |
assert mock_smtp.call_count == 1 |
| 2626 |
assert mock_smtp_ssl.call_count == 0 |
| 2627 |
assert response.starttls.call_count == 1 |
| 2628 |
assert response.login.call_count == 1 |
| 2629 |
assert response.sendmail.call_count == 1 |
| 2630 |
# Store our Sent Arguments |
| 2631 |
# Syntax is: |
| 2632 |
# sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) |
| 2633 |
# [0] [1] [2] |
| 2634 |
from_ = response.sendmail.call_args[0][0] |
| 2635 |
to = response.sendmail.call_args[0][1] |
| 2636 |
msg = response.sendmail.call_args[0][2] |
| 2637 |
assert from_ == "[email protected]" |
| 2638 |
assert isinstance(to, list) |
| 2639 |
assert len(to) == 1 |
| 2640 |
assert to[0] == "[email protected]" |
| 2641 |
assert msg.split("\n")[-3] == "body" |
| 2642 |
|
| 2643 |
|
| 2644 |
@mock.patch("smtplib.SMTP_SSL") |
| 2645 |
@mock.patch("smtplib.SMTP") |
| 2646 |
def test_plugin_email_by_ipaddr_1113(mock_smtp, mock_smtp_ssl): |
| 2647 |
""" |
| 2648 |
NotifyEmail() GitHub Issue 1113 |
| 2649 |
https://github.com/caronc/apprise/issues/1113 |
| 2650 |
Email with ip addresses not working |
| 2651 |
|
| 2652 |
""" |
| 2653 |
|
| 2654 |
response = mock.Mock() |
| 2655 |
# An empty refusal map means SMTP accepted every recipient. |
| 2656 |
response.sendmail.return_value = {} |
| 2657 |
mock_smtp_ssl.return_value = response |
| 2658 |
mock_smtp.return_value = response |
| 2659 |
|
| 2660 |
results = email.NotifyEmail.parse_url( |
| 2661 |
"mailto://10.0.0.195:25/[email protected]&[email protected]" |
| 2662 |
) |
| 2663 |
|
| 2664 |
assert isinstance(results, dict) |
| 2665 |
assert results["user"] is None |
| 2666 |
assert results["password"] is None |
| 2667 |
assert results["host"] == "10.0.0.195" |
| 2668 |
assert results["from_addr"] == "[email protected]" |
| 2669 |
assert isinstance(results["targets"], list) |
| 2670 |
assert len(results["targets"]) == 1 |
| 2671 |
assert results["targets"][0] == "[email protected]" |
| 2672 |
assert results["port"] == 25 |
| 2673 |
|
| 2674 |
email_ = Apprise.instantiate(results, suppress_exceptions=False) |
| 2675 |
assert isinstance(email_, email.NotifyEmail) is True |
| 2676 |
|
| 2677 |
assert len(email_.targets) == 1 |
| 2678 |
assert (False, "[email protected]") in email_.targets |
| 2679 |
|
| 2680 |
assert email_.from_addr == (False, "[email protected]") |
| 2681 |
assert email_.user is None |
| 2682 |
assert email_.password is None |
| 2683 |
assert email_.smtp_host == "10.0.0.195" |
| 2684 |
assert email_.port == 25 |
| 2685 |
assert email_.targets == [(False, "[email protected]")] |
| 2686 |
|
| 2687 |
|
| 2688 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 2689 |
@mock.patch("smtplib.SMTP_SSL") |
| 2690 |
@mock.patch("smtplib.SMTP") |
| 2691 |
def test_plugin_email_pgp(mock_smtp, mock_smtpssl, tmpdir): |
| 2692 |
"""NotifyEmail() PGP Tests.""" |
| 2693 |
# Our mock of our socket action |
| 2694 |
mock_socket = mock.Mock() |
| 2695 |
mock_socket.starttls.return_value = True |
| 2696 |
mock_socket.login.return_value = True |
| 2697 |
# An empty refusal map means SMTP accepted every recipient. |
| 2698 |
mock_socket.sendmail.return_value = {} |
| 2699 |
|
| 2700 |
# Create a mock SMTP Object |
| 2701 |
mock_smtp.return_value = mock_socket |
| 2702 |
mock_smtpssl.return_value = mock_socket |
| 2703 |
|
| 2704 |
assert utils.pgp.PGP_SUPPORT is True |
| 2705 |
utils.pgp.PGP_SUPPORT = False |
| 2706 |
# Forces to run through section of code that produces a warning there is |
| 2707 |
# no PGP |
| 2708 |
obj = Apprise.instantiate("mailto://user:[email protected]?pgp=encrypt") |
| 2709 |
# No PGP Support and set enabled |
| 2710 |
assert bool(obj.notify("test body")) is False |
| 2711 |
|
| 2712 |
# Return the PGP status for remaining checks |
| 2713 |
utils.pgp.PGP_SUPPORT = True |
| 2714 |
|
| 2715 |
# Initialize our email (no from name) |
| 2716 |
obj = Apprise.instantiate("mailto://user2:[email protected]?pgp=encrypt") |
| 2717 |
|
| 2718 |
# Nothing to lookup |
| 2719 |
assert obj.pgp.public_keyfile() is None |
| 2720 |
assert obj.pgp.public_key() is None |
| 2721 |
assert obj.pgp.encrypt("message") is False |
| 2722 |
# Keys cannot be generated in memory mode. |
| 2723 |
assert obj.pgp.keygen() is False |
| 2724 |
|
| 2725 |
# The reason... no location to store data |
| 2726 |
assert obj.store.mode == PersistentStoreMode.MEMORY |
| 2727 |
|
| 2728 |
tmpdir0 = tmpdir.mkdir("tmp00") |
| 2729 |
asset = AppriseAsset( |
| 2730 |
storage_mode=PersistentStoreMode.FLUSH, |
| 2731 |
storage_path=str(tmpdir0), |
| 2732 |
) |
| 2733 |
|
| 2734 |
# Prepare PGP |
| 2735 |
obj = Apprise.instantiate( |
| 2736 |
"mailto://pgp:[email protected]?pgp=encrypt", asset=asset |
| 2737 |
) |
| 2738 |
assert obj.store.mode == PersistentStoreMode.FLUSH |
| 2739 |
|
| 2740 |
# Still no public key |
| 2741 |
assert obj.pgp.public_key(autogen=False) is None |
| 2742 |
assert obj.pgp.keygen() is True |
| 2743 |
# Now we'll have a public key |
| 2744 |
assert isinstance(obj.pgp.public_keyfile(), str) |
| 2745 |
|
| 2746 |
# Generate warning by second call |
| 2747 |
assert obj.pgp.keygen() is True |
| 2748 |
|
| 2749 |
# Remove newly generated files |
| 2750 |
os.unlink(os.path.join(obj.store.path, "pgp-pub.asc")) |
| 2751 |
os.unlink(os.path.join(obj.store.path, "pgp-prv.asc")) |
| 2752 |
obj = Apprise.instantiate( |
| 2753 |
"mailto://pgp:[email protected]?pgp=encrypt", asset=asset |
| 2754 |
) |
| 2755 |
assert obj.store.mode == PersistentStoreMode.FLUSH |
| 2756 |
assert obj.pgp.keygen() is True |
| 2757 |
|
| 2758 |
# Prepare PGP while providing it a key |
| 2759 |
obj = Apprise.instantiate( |
| 2760 |
"mailto://pgp:[email protected]?pgp=encrypt&" |
| 2761 |
f"pgppub={obj.pgp.public_keyfile()}", |
| 2762 |
asset=asset, |
| 2763 |
) |
| 2764 |
|
| 2765 |
# keyfile Defined |
| 2766 |
assert obj.pgp.pub_keyfile is not None |
| 2767 |
|
| 2768 |
# Get our key |
| 2769 |
key = obj.pgp.public_key() |
| 2770 |
|
| 2771 |
# A replacement key cannot be generated in this case. |
| 2772 |
# is immutable |
| 2773 |
assert obj.pgp.keygen() is False |
| 2774 |
|
| 2775 |
# Our key is the same |
| 2776 |
assert key is obj.pgp.public_key() |
| 2777 |
|
| 2778 |
tmpdir0 = tmpdir.mkdir("tmp00a") |
| 2779 |
asset0 = AppriseAsset( |
| 2780 |
storage_mode=PersistentStoreMode.FLUSH, |
| 2781 |
storage_path=str(tmpdir0), |
| 2782 |
) |
| 2783 |
|
| 2784 |
# Prepare Invalid PGP Key |
| 2785 |
obj = Apprise.instantiate( |
| 2786 |
"mailto://pgpX:[email protected]?pgp=encrypt", asset=asset0 |
| 2787 |
) |
| 2788 |
|
| 2789 |
# No keyfiles |
| 2790 |
assert obj.pgp.pub_keyfile is None |
| 2791 |
|
| 2792 |
# Generate our keys |
| 2793 |
assert obj.pgp.keygen() is True |
| 2794 |
|
| 2795 |
# Second call uses cache |
| 2796 |
assert obj.pgp.keygen() is True |
| 2797 |
|
| 2798 |
# We will find our key |
| 2799 |
key = obj.pgp.public_key() |
| 2800 |
assert key is not None |
| 2801 |
|
| 2802 |
# Utilize force parameter |
| 2803 |
assert obj.pgp.keygen(force=True) is True |
| 2804 |
|
| 2805 |
# Our key is new |
| 2806 |
assert key != obj.pgp.public_key() |
| 2807 |
assert obj.pgp.public_key() is not None |
| 2808 |
|
| 2809 |
# Prepare Invalid PGP Key |
| 2810 |
obj = Apprise.instantiate( |
| 2811 |
"mailto://pgp:[email protected]?pgp=encrypt&pgppub=invalid", asset=asset |
| 2812 |
) |
| 2813 |
|
| 2814 |
# Returns false |
| 2815 |
assert obj.pgp.pub_keyfile is False |
| 2816 |
assert obj.pgp.public_keyfile() is False |
| 2817 |
|
| 2818 |
tmpdir2 = tmpdir.mkdir("tmp02") |
| 2819 |
asset = AppriseAsset( |
| 2820 |
storage_mode=PersistentStoreMode.FLUSH, |
| 2821 |
storage_path=str(tmpdir2), |
| 2822 |
) |
| 2823 |
obj = Apprise.instantiate( |
| 2824 |
"mailto://chris:[email protected]?pgp=encrypt", asset=asset |
| 2825 |
) |
| 2826 |
|
| 2827 |
assert obj.store.mode == PersistentStoreMode.FLUSH |
| 2828 |
assert obj.pgp.keygen() is True |
| 2829 |
|
| 2830 |
# Second call uses cache |
| 2831 |
assert obj.pgp.keygen() is True |
| 2832 |
|
| 2833 |
# We will find our key |
| 2834 |
assert obj.pgp.public_key() is not None |
| 2835 |
|
| 2836 |
# Direct lookups without an address still use the sender's own key. |
| 2837 |
tmpdir3 = tmpdir.mkdir("tmp03") |
| 2838 |
asset = AppriseAsset( |
| 2839 |
storage_mode=PersistentStoreMode.FLUSH, |
| 2840 |
storage_path=str(tmpdir3), |
| 2841 |
) |
| 2842 |
obj = Apprise.instantiate( |
| 2843 |
"mailto://chris:[email protected]/[email protected]?pgp=encrypt", |
| 2844 |
asset=asset, |
| 2845 |
) |
| 2846 |
|
| 2847 |
assert obj.store.mode == PersistentStoreMode.FLUSH |
| 2848 |
|
| 2849 |
# We'll have a public key object to encrypt with |
| 2850 |
assert obj.pgp.public_key() is not None |
| 2851 |
|
| 2852 |
encrypted = obj.pgp.encrypt("hello world") |
| 2853 |
assert encrypted.startswith("-----BEGIN PGP MESSAGE-----") |
| 2854 |
assert encrypted.rstrip().endswith("-----END PGP MESSAGE-----") |
| 2855 |
|
| 2856 |
dir_content = os.listdir(obj.store.path) |
| 2857 |
assert "chris-pub.asc" in dir_content |
| 2858 |
assert "chris-prv.asc" in dir_content |
| 2859 |
|
| 2860 |
assert obj.pgp.public_keyfile().endswith("chris-pub.asc") |
| 2861 |
|
| 2862 |
# A real send fails rather than generating an unknown recipient's key. |
| 2863 |
assert bool(obj.notify("test body")) is False |
| 2864 |
|
| 2865 |
# The private key is not needed for sending the encrypted messages |
| 2866 |
os.unlink(os.path.join(obj.store.path, "chris-prv.asc")) |
| 2867 |
os.rename( |
| 2868 |
os.path.join(obj.store.path, "chris-pub.asc"), |
| 2869 |
os.path.join(obj.store.path, "[email protected]"), |
| 2870 |
) |
| 2871 |
|
| 2872 |
assert obj.pgp.public_keyfile() is None |
| 2873 |
assert obj.pgp.public_keyfile("[email protected]") is None |
| 2874 |
assert obj.pgp.public_keyfile("[email protected]").endswith( |
| 2875 |
"[email protected]" |
| 2876 |
) |
| 2877 |
|
| 2878 |
assert obj.pgp.public_keyfile("[email protected]").endswith( |
| 2879 |
"[email protected]" |
| 2880 |
) |
| 2881 |
assert obj.pgp.public_keyfile("[email protected]").endswith( |
| 2882 |
"[email protected]" |
| 2883 |
) |
| 2884 |
assert obj.pgp.public_keyfile("unknown") is None |
| 2885 |
|
| 2886 |
shutil.copyfile( |
| 2887 |
os.path.join(obj.store.path, "[email protected]"), |
| 2888 |
os.path.join(obj.store.path, "user-pub.asc"), |
| 2889 |
) |
| 2890 |
|
| 2891 |
assert obj.pgp.public_keyfile("[email protected]").endswith( |
| 2892 |
"[email protected]" |
| 2893 |
) |
| 2894 |
assert obj.pgp.public_keyfile("[email protected]").endswith( |
| 2895 |
"[email protected]" |
| 2896 |
) |
| 2897 |
|
| 2898 |
# Remove file |
| 2899 |
os.unlink(os.path.join(obj.store.path, "[email protected]")) |
| 2900 |
assert obj.pgp.public_keyfile("[email protected]").endswith("user-pub.asc") |
| 2901 |
shutil.copyfile( |
| 2902 |
os.path.join(obj.store.path, "user-pub.asc"), |
| 2903 |
os.path.join(obj.store.path, "chris-pub.asc"), |
| 2904 |
) |
| 2905 |
# user-pub.asc still trumps still trumps |
| 2906 |
assert obj.pgp.public_keyfile("[email protected]").endswith("user-pub.asc") |
| 2907 |
shutil.copyfile( |
| 2908 |
os.path.join(obj.store.path, "chris-pub.asc"), |
| 2909 |
os.path.join(obj.store.path, "[email protected]"), |
| 2910 |
) |
| 2911 |
# user-pub still trumps |
| 2912 |
assert obj.pgp.public_keyfile("[email protected]").endswith("user-pub.asc") |
| 2913 |
# Never substitute the sender's key for a missing recipient key. |
| 2914 |
assert obj.pgp.public_keyfile("[email protected]") is None |
| 2915 |
|
| 2916 |
# remove this file |
| 2917 |
os.unlink(os.path.join(obj.store.path, "user-pub.asc")) |
| 2918 |
|
| 2919 |
# Removing the recipient key must not expose sender keys as fallbacks. |
| 2920 |
assert obj.pgp.public_keyfile("[email protected]") is None |
| 2921 |
os.unlink(os.path.join(obj.store.path, "[email protected]")) |
| 2922 |
assert obj.pgp.public_keyfile("[email protected]") is None |
| 2923 |
|
| 2924 |
# Testing again |
| 2925 |
tmpdir4 = tmpdir.mkdir("tmp04") |
| 2926 |
asset = AppriseAsset( |
| 2927 |
storage_mode=PersistentStoreMode.FLUSH, |
| 2928 |
storage_path=str(tmpdir4), |
| 2929 |
) |
| 2930 |
obj = Apprise.instantiate( |
| 2931 |
"mailto://chris:[email protected]/[email protected]?pgp=encrypt", |
| 2932 |
asset=asset, |
| 2933 |
) |
| 2934 |
|
| 2935 |
with mock.patch("builtins.open", side_effect=FileNotFoundError): |
| 2936 |
# can't open key |
| 2937 |
assert obj.pgp.public_key() is None |
| 2938 |
|
| 2939 |
with mock.patch("builtins.open", side_effect=OSError): |
| 2940 |
# can't open key |
| 2941 |
assert obj.pgp.public_key() is None |
| 2942 |
# Test unlink |
| 2943 |
with mock.patch("os.unlink", side_effect=OSError): |
| 2944 |
assert obj.pgp.public_key() is None |
| 2945 |
|
| 2946 |
# Key Generation will fail |
| 2947 |
assert obj.pgp.keygen() is False |
| 2948 |
|
| 2949 |
with mock.patch("pgpy.PGPKey.new", side_effect=NameError): |
| 2950 |
# Can't Generate keys |
| 2951 |
assert obj.pgp.keygen() is False |
| 2952 |
# can't open key |
| 2953 |
assert obj.pgp.public_key() is None |
| 2954 |
|
| 2955 |
with mock.patch("pgpy.PGPKey.from_blob", side_effect=FileNotFoundError): |
| 2956 |
# can't open key |
| 2957 |
assert obj.pgp.public_key() is None |
| 2958 |
|
| 2959 |
with mock.patch("pgpy.PGPKey.from_blob", side_effect=OSError): |
| 2960 |
# can't open key |
| 2961 |
assert obj.pgp.public_key() is None |
| 2962 |
|
| 2963 |
# Can't encrypt key |
| 2964 |
with mock.patch("pgpy.PGPKey.from_blob", side_effect=NameError): |
| 2965 |
assert obj.pgp.public_key() is None |
| 2966 |
|
| 2967 |
with mock.patch("pgpy.PGPMessage.new", side_effect=NameError): |
| 2968 |
assert obj.pgp.encrypt("message") is None |
| 2969 |
# Attempts to encrypt a message |
| 2970 |
assert bool(obj.notify("test-encrypt")) is False |
| 2971 |
|
| 2972 |
# Create new keys |
| 2973 |
assert obj.pgp.keygen() is True |
| 2974 |
with ( |
| 2975 |
mock.patch("os.path.isfile", return_value=False), |
| 2976 |
mock.patch("builtins.open", side_effect=OSError), |
| 2977 |
mock.patch("os.unlink", return_value=None), |
| 2978 |
): |
| 2979 |
assert obj.pgp.keygen() is False |
| 2980 |
|
| 2981 |
# Testing again |
| 2982 |
tmpdir5 = tmpdir.mkdir("tmp05") |
| 2983 |
asset = AppriseAsset( |
| 2984 |
storage_mode=PersistentStoreMode.FLUSH, |
| 2985 |
storage_path=str(tmpdir5), |
| 2986 |
) |
| 2987 |
obj = Apprise.instantiate( |
| 2988 |
"mailto://chris:[email protected]/[email protected]?pgp=encrypt", |
| 2989 |
asset=asset, |
| 2990 |
) |
| 2991 |
|
| 2992 |
# Catch edge case where we just can't generate the the key |
| 2993 |
with ( |
| 2994 |
mock.patch( |
| 2995 |
"os.path.isfile", |
| 2996 |
side_effect=( |
| 2997 |
# 5x False to skip through pgp.public_keyfile() |
| 2998 |
False, |
| 2999 |
False, |
| 3000 |
False, |
| 3001 |
False, |
| 3002 |
False, |
| 3003 |
False, |
| 3004 |
# 1x True to pass pgp.keygen() |
| 3005 |
True, |
| 3006 |
# 5x False to skip through pgp.public_keyfile() second call |
| 3007 |
False, |
| 3008 |
False, |
| 3009 |
False, |
| 3010 |
False, |
| 3011 |
False, |
| 3012 |
False, |
| 3013 |
), |
| 3014 |
), |
| 3015 |
mock.patch("pgpy.PGPKey.from_blob", side_effect=FileNotFoundError), |
| 3016 |
): |
| 3017 |
assert obj.pgp.public_key() is None |
| 3018 |
|
| 3019 |
# Corrupt Data |
| 3020 |
tmpdir6 = tmpdir.mkdir("tmp06") |
| 3021 |
asset = AppriseAsset( |
| 3022 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3023 |
storage_path=str(tmpdir6), |
| 3024 |
) |
| 3025 |
obj = Apprise.instantiate( |
| 3026 |
"mailto://chris:[email protected]/[email protected]?pgp=encrypt", |
| 3027 |
asset=asset, |
| 3028 |
) |
| 3029 |
|
| 3030 |
# Use the recipient's filename to exercise the corrupt-key path. |
| 3031 |
shutil.copyfile( |
| 3032 |
os.path.join(TEST_VAR_DIR, "pgp", "corrupt-pub.asc"), |
| 3033 |
os.path.join(obj.store.path, "[email protected]"), |
| 3034 |
) |
| 3035 |
|
| 3036 |
# Key is corrupted |
| 3037 |
assert bool(obj.notify("test")) is False |
| 3038 |
|
| 3039 |
shutil.copyfile( |
| 3040 |
os.path.join(TEST_VAR_DIR, "apprise-test.jpeg"), |
| 3041 |
os.path.join(obj.store.path, "[email protected]"), |
| 3042 |
) |
| 3043 |
|
| 3044 |
# Key is a binary image; definitely not a valid key |
| 3045 |
assert bool(obj.notify("test")) is False |
| 3046 |
|
| 3047 |
|
| 3048 |
@mock.patch("smtplib.SMTP_SSL") |
| 3049 |
@mock.patch("smtplib.SMTP") |
| 3050 |
def test_plugin_email_pgp_modes(mock_smtp, mock_smtpssl): |
| 3051 |
"""NotifyEmail() handles supported and unknown PGP modes.""" |
| 3052 |
|
| 3053 |
mock_socket = mock.Mock() |
| 3054 |
mock_socket.starttls.return_value = True |
| 3055 |
mock_socket.login.return_value = True |
| 3056 |
# An empty refusal map means SMTP accepted every recipient. |
| 3057 |
mock_socket.sendmail.return_value = {} |
| 3058 |
mock_smtp.return_value = mock_socket |
| 3059 |
mock_smtpssl.return_value = mock_socket |
| 3060 |
|
| 3061 |
# pgp= absent -> pgp_mode defaults to 'no' |
| 3062 |
obj = Apprise.instantiate("mailto://user:[email protected]") |
| 3063 |
assert obj is not None |
| 3064 |
assert obj.pgp_mode == "no" |
| 3065 |
assert obj.use_wkd is False |
| 3066 |
|
| 3067 |
# pgp=encrypt -> pgp_mode = 'encrypt' |
| 3068 |
obj = Apprise.instantiate("mailto://user:[email protected]?pgp=encrypt") |
| 3069 |
assert obj.pgp_mode == "encrypt" |
| 3070 |
|
| 3071 |
# pgp=e (prefix shorthand) -> pgp_mode = 'encrypt' |
| 3072 |
obj = Apprise.instantiate("mailto://user:[email protected]?pgp=e") |
| 3073 |
assert obj.pgp_mode == "encrypt" |
| 3074 |
|
| 3075 |
# "none" is an explicit synonym for "no". |
| 3076 |
obj = Apprise.instantiate("mailto://user:[email protected]?pgp=none") |
| 3077 |
assert obj.pgp_mode == "no" |
| 3078 |
|
| 3079 |
# pgp=n -> prefix matches 'no' |
| 3080 |
obj = Apprise.instantiate("mailto://user:[email protected]?pgp=n") |
| 3081 |
assert obj.pgp_mode == "no" |
| 3082 |
|
| 3083 |
# pgp=no -> direct mode match; canonical URL form |
| 3084 |
obj = Apprise.instantiate("mailto://user:[email protected]?pgp=no") |
| 3085 |
assert obj.pgp_mode == "no" |
| 3086 |
|
| 3087 |
# Removed boolean spellings are rejected; canonical "no" remains valid. |
| 3088 |
for legacy in ("yes", "true", "false"): |
| 3089 |
with pytest.raises(AppriseImproperlyConfigured): |
| 3090 |
Apprise.instantiate( |
| 3091 |
f"mailto://user:[email protected]?pgp={legacy}", |
| 3092 |
suppress_exceptions=False, |
| 3093 |
) |
| 3094 |
|
| 3095 |
# url() emits 'pgp=encrypt' when mode is encrypt, and nothing when no |
| 3096 |
obj = Apprise.instantiate("mailto://user:[email protected]?pgp=encrypt") |
| 3097 |
assert "pgp=encrypt" in obj.url() |
| 3098 |
assert "pgp=no" not in obj.url() |
| 3099 |
|
| 3100 |
obj = Apprise.instantiate("mailto://user:[email protected]") |
| 3101 |
assert "pgp=" not in obj.url() |
| 3102 |
|
| 3103 |
# The constructor also accepts pgp_mode directly. |
| 3104 |
obj = email.NotifyEmail( |
| 3105 |
user="user", |
| 3106 |
password="pass", |
| 3107 |
host="nuxref.com", |
| 3108 |
pgp_mode="encrypt", |
| 3109 |
) |
| 3110 |
assert obj.pgp_mode == "encrypt" |
| 3111 |
|
| 3112 |
# pgppub= path is masked when privacy=True |
| 3113 |
obj = Apprise.instantiate( |
| 3114 |
"mailto://user:[email protected]?pgp=encrypt&pgppub=/path/to/my-pub.asc" |
| 3115 |
) |
| 3116 |
assert obj is not None |
| 3117 |
assert obj.pgp_key == "/path/to/my-pub.asc" |
| 3118 |
# Full URL includes pgppub= (path may be percent-encoded in the query |
| 3119 |
# string); privacy URL must replace the value with literal **** |
| 3120 |
assert "pgppub=" in obj.url(privacy=False) |
| 3121 |
assert "pgppub=****" in obj.url(privacy=True) |
| 3122 |
assert "my-pub.asc" not in obj.url(privacy=True) |
| 3123 |
|
| 3124 |
# The removed pgpkey alias is ignored and is no longer advertised. |
| 3125 |
assert "pgpkey" not in email.NotifyEmail.template_args |
| 3126 |
obj = Apprise.instantiate( |
| 3127 |
"mailto://user:[email protected]?pgpkey=/old/public-key.asc" |
| 3128 |
) |
| 3129 |
assert obj is not None |
| 3130 |
assert obj.pgp_key is None |
| 3131 |
|
| 3132 |
# An unknown old alias cannot replace the supported parameter. |
| 3133 |
obj = Apprise.instantiate( |
| 3134 |
"mailto://user:[email protected]" |
| 3135 |
"?pgppub=/current/public-key.asc&pgpkey=/old/public-key.asc" |
| 3136 |
) |
| 3137 |
assert obj is not None |
| 3138 |
assert obj.pgp_key == "/current/public-key.asc" |
| 3139 |
|
| 3140 |
# Reject typos instead of silently disabling encryption. |
| 3141 |
with pytest.raises(AppriseImproperlyConfigured): |
| 3142 |
email.NotifyEmail( |
| 3143 |
user="user", |
| 3144 |
password="pass", |
| 3145 |
host="nuxref.com", |
| 3146 |
pgp_mode="encrpyt", |
| 3147 |
) |
| 3148 |
|
| 3149 |
# Same applies when the value arrives via the URL |
| 3150 |
with pytest.raises(AppriseImproperlyConfigured): |
| 3151 |
Apprise.instantiate( |
| 3152 |
"mailto://user:[email protected]?pgp=encrpyt", |
| 3153 |
suppress_exceptions=False, |
| 3154 |
) |
| 3155 |
|
| 3156 |
|
| 3157 |
@mock.patch("smtplib.SMTP_SSL") |
| 3158 |
@mock.patch("smtplib.SMTP") |
| 3159 |
def test_plugin_email_wkd_param(mock_smtp, mock_smtpssl): |
| 3160 |
"""NotifyEmail() wkd= URL parameter and url() round-trip.""" |
| 3161 |
|
| 3162 |
mock_socket = mock.Mock() |
| 3163 |
mock_socket.starttls.return_value = True |
| 3164 |
mock_socket.login.return_value = True |
| 3165 |
# An empty refusal map means SMTP accepted every recipient. |
| 3166 |
mock_socket.sendmail.return_value = {} |
| 3167 |
mock_smtp.return_value = mock_socket |
| 3168 |
mock_smtpssl.return_value = mock_socket |
| 3169 |
|
| 3170 |
# wkd=yes -> use_wkd=True; a WKD controller is wired into pgp |
| 3171 |
obj = Apprise.instantiate("mailto://user:[email protected]?wkd=yes") |
| 3172 |
assert obj.use_wkd is True |
| 3173 |
assert obj.pgp.wkd is not None |
| 3174 |
|
| 3175 |
# wkd=yes without pgp= implies pgp=encrypt |
| 3176 |
assert obj.pgp_mode == "encrypt" |
| 3177 |
|
| 3178 |
# wkd=no -> use_wkd=False |
| 3179 |
obj = Apprise.instantiate("mailto://user:[email protected]?wkd=no") |
| 3180 |
assert obj.use_wkd is False |
| 3181 |
assert obj.pgp.wkd is None |
| 3182 |
|
| 3183 |
# url() includes wkd=yes when enabled; pgp=encrypt is implied |
| 3184 |
obj = Apprise.instantiate("mailto://user:[email protected]?wkd=yes") |
| 3185 |
generated = obj.url() |
| 3186 |
assert "wkd=yes" in generated |
| 3187 |
assert "pgp=encrypt" in generated |
| 3188 |
|
| 3189 |
# wkd=yes alongside explicit pgp=no keeps no (explicit wins) |
| 3190 |
obj = Apprise.instantiate("mailto://user:[email protected]?pgp=no&wkd=yes") |
| 3191 |
assert obj.pgp_mode == "no" |
| 3192 |
|
| 3193 |
# url() must emit pgp=no when wkd=yes and pgp=no is explicit so that |
| 3194 |
# the round-trip URL does not re-apply the wkd=yes->encrypt implication |
| 3195 |
generated = obj.url() |
| 3196 |
assert "pgp=no" in generated |
| 3197 |
assert "wkd=yes" in generated |
| 3198 |
|
| 3199 |
# Parsing the round-trip URL must preserve pgp=no (not re-imply encrypt) |
| 3200 |
obj2 = Apprise.instantiate(generated) |
| 3201 |
assert obj2 is not None |
| 3202 |
assert obj2.pgp_mode == "no" |
| 3203 |
assert obj2.use_wkd is True |
| 3204 |
|
| 3205 |
# use_wkd as a string 'no' must disable WKD (bool('no') would be True) |
| 3206 |
obj = email.NotifyEmail( |
| 3207 |
user="user", password="pass", host="nuxref.com", use_wkd="no" |
| 3208 |
) |
| 3209 |
assert obj.use_wkd is False |
| 3210 |
|
| 3211 |
# Direct __init__ call with use_wkd=True and no pgp_mode |
| 3212 |
obj = email.NotifyEmail( |
| 3213 |
user="user", password="pass", host="nuxref.com", use_wkd=True |
| 3214 |
) |
| 3215 |
assert obj.use_wkd is True |
| 3216 |
assert obj.pgp_mode == "encrypt" |
| 3217 |
|
| 3218 |
# url() omits wkd= when disabled (it is the default) |
| 3219 |
obj = Apprise.instantiate("mailto://user:[email protected]") |
| 3220 |
assert "wkd=" not in obj.url() |
| 3221 |
|
| 3222 |
|
| 3223 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3224 |
@mock.patch("smtplib.SMTP_SSL") |
| 3225 |
@mock.patch("smtplib.SMTP") |
| 3226 |
def test_plugin_email_wkd_key_discovery(mock_smtp, mock_smtpssl, tmpdir): |
| 3227 |
"""Email send uses the WKD-fetched key when no local key exists.""" |
| 3228 |
import pgpy |
| 3229 |
|
| 3230 |
mock_socket = mock.Mock() |
| 3231 |
mock_socket.starttls.return_value = True |
| 3232 |
mock_socket.login.return_value = True |
| 3233 |
# An empty refusal map means SMTP accepted every recipient. |
| 3234 |
mock_socket.sendmail.return_value = {} |
| 3235 |
mock_smtp.return_value = mock_socket |
| 3236 |
mock_smtpssl.return_value = mock_socket |
| 3237 |
|
| 3238 |
# Generate a real key pair to serve as the WKD response |
| 3239 |
key = pgpy.PGPKey.new( |
| 3240 |
pgpy.constants.PubKeyAlgorithm.RSAEncryptOrSign, 2048 |
| 3241 |
) |
| 3242 |
uid = pgpy.PGPUID.new("Chris", email="[email protected]") |
| 3243 |
key.add_uid( |
| 3244 |
uid, |
| 3245 |
usage={ |
| 3246 |
pgpy.constants.KeyFlags.Sign, |
| 3247 |
pgpy.constants.KeyFlags.EncryptCommunications, |
| 3248 |
}, |
| 3249 |
hashes=[pgpy.constants.HashAlgorithm.SHA256], |
| 3250 |
ciphers=[pgpy.constants.SymmetricKeyAlgorithm.AES256], |
| 3251 |
compression=[pgpy.constants.CompressionAlgorithm.ZLIB], |
| 3252 |
) |
| 3253 |
pub_bytes = str(key.pubkey).encode() |
| 3254 |
|
| 3255 |
# Keep WKD as the only key source in this test. |
| 3256 |
asset = AppriseAsset( |
| 3257 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3258 |
storage_path=str(tmpdir), |
| 3259 |
pgp_autogen=False, |
| 3260 |
) |
| 3261 |
|
| 3262 |
obj = Apprise.instantiate( |
| 3263 |
"mailto://chris:[email protected]?pgp=encrypt&wkd=yes", |
| 3264 |
asset=asset, |
| 3265 |
) |
| 3266 |
assert obj.use_wkd is True |
| 3267 |
|
| 3268 |
# Patch WKD fetch to return our generated public key |
| 3269 |
with mock.patch.object(obj.pgp.wkd, "fetch", return_value=pub_bytes): |
| 3270 |
assert bool(obj.notify("test body")) is True |
| 3271 |
|
| 3272 |
# Clear the cache so the next fetch uses only its mock result. |
| 3273 |
obj.pgp._ApprisePGPController__key_lookup.clear() |
| 3274 |
|
| 3275 |
# WKD returning None with autogen disabled falls through gracefully |
| 3276 |
with mock.patch.object(obj.pgp.wkd, "fetch", return_value=None): |
| 3277 |
assert bool(obj.notify("test body")) is False |
| 3278 |
|
| 3279 |
|
| 3280 |
@mock.patch("smtplib.SMTP_SSL") |
| 3281 |
@mock.patch("smtplib.SMTP") |
| 3282 |
def test_plugin_email_pgp_sign_mode_param(mock_smtp, mock_smtpssl): |
| 3283 |
"""NotifyEmail() pgp=sign mode round-trip and attribute checks.""" |
| 3284 |
|
| 3285 |
mock_socket = mock.Mock() |
| 3286 |
mock_socket.starttls.return_value = True |
| 3287 |
mock_socket.login.return_value = True |
| 3288 |
# An empty refusal map means SMTP accepted every recipient. |
| 3289 |
mock_socket.sendmail.return_value = {} |
| 3290 |
mock_smtp.return_value = mock_socket |
| 3291 |
mock_smtpssl.return_value = mock_socket |
| 3292 |
|
| 3293 |
# pgp=sign sets pgp_mode to 'sign' |
| 3294 |
obj = Apprise.instantiate("mailto://user:[email protected]?pgp=sign") |
| 3295 |
assert obj is not None |
| 3296 |
assert obj.pgp_mode == "sign" |
| 3297 |
|
| 3298 |
# pgp=s (prefix shorthand) also resolves to 'sign' |
| 3299 |
obj = Apprise.instantiate("mailto://user:[email protected]?pgp=s") |
| 3300 |
assert obj is not None |
| 3301 |
assert obj.pgp_mode == "sign" |
| 3302 |
|
| 3303 |
# url() round-trip preserves pgp=sign |
| 3304 |
obj = Apprise.instantiate("mailto://user:[email protected]?pgp=sign") |
| 3305 |
generated = obj.url() |
| 3306 |
assert "pgp=sign" in generated |
| 3307 |
obj2 = Apprise.instantiate(generated) |
| 3308 |
assert obj2 is not None |
| 3309 |
assert obj2.pgp_mode == "sign" |
| 3310 |
|
| 3311 |
# pgp=sign + wkd=yes: sign mode is respected; wkd implication does NOT |
| 3312 |
# override an explicit pgp=sign to pgp=encrypt |
| 3313 |
obj = Apprise.instantiate("mailto://user:[email protected]?pgp=sign&wkd=yes") |
| 3314 |
assert obj.pgp_mode == "sign" |
| 3315 |
assert obj.use_wkd is True |
| 3316 |
|
| 3317 |
|
| 3318 |
@mock.patch("smtplib.SMTP_SSL") |
| 3319 |
@mock.patch("smtplib.SMTP") |
| 3320 |
def test_plugin_email_pgp_privkey_param(mock_smtp, mock_smtpssl, tmpdir): |
| 3321 |
"""NotifyEmail() pgpprv= URL parameter round-trip.""" |
| 3322 |
|
| 3323 |
mock_socket = mock.Mock() |
| 3324 |
mock_socket.starttls.return_value = True |
| 3325 |
mock_socket.login.return_value = True |
| 3326 |
# An empty refusal map means SMTP accepted every recipient. |
| 3327 |
mock_socket.sendmail.return_value = {} |
| 3328 |
mock_smtp.return_value = mock_socket |
| 3329 |
mock_smtpssl.return_value = mock_socket |
| 3330 |
|
| 3331 |
prv_path = os.path.join( |
| 3332 |
os.path.dirname(__file__), "var", "pgp", "valid-prv.asc" |
| 3333 |
) |
| 3334 |
|
| 3335 |
# pgpprv= is stored on the instance |
| 3336 |
obj = Apprise.instantiate( |
| 3337 |
f"mailto://user:[email protected]?pgp=sign&pgpprv={prv_path}" |
| 3338 |
) |
| 3339 |
assert obj is not None |
| 3340 |
assert obj.pgp_privkey == prv_path |
| 3341 |
|
| 3342 |
# url() includes pgpprv= in plain mode, masks it in privacy mode |
| 3343 |
plain = obj.url(privacy=False) |
| 3344 |
priv = obj.url(privacy=True) |
| 3345 |
assert "pgpprv=" in plain |
| 3346 |
assert prv_path in plain or "valid-prv.asc" in plain |
| 3347 |
assert "pgpprv=****" in priv |
| 3348 |
|
| 3349 |
# Round-trip: parse the generated URL and verify pgp_privkey is preserved |
| 3350 |
parsed = email.NotifyEmail.parse_url(plain) |
| 3351 |
assert parsed is not None |
| 3352 |
assert "pgp_privkey" in parsed |
| 3353 |
assert "valid-prv.asc" in parsed["pgp_privkey"] |
| 3354 |
|
| 3355 |
|
| 3356 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3357 |
@mock.patch("smtplib.SMTP_SSL") |
| 3358 |
@mock.patch("smtplib.SMTP") |
| 3359 |
def test_plugin_email_pgp_sign_send(mock_smtp, mock_smtpssl, tmpdir): |
| 3360 |
"""pgp=sign mode produces a multipart/signed email on send.""" |
| 3361 |
|
| 3362 |
mock_socket = mock.Mock() |
| 3363 |
mock_socket.starttls.return_value = True |
| 3364 |
mock_socket.login.return_value = True |
| 3365 |
# An empty refusal map means SMTP accepted every recipient. |
| 3366 |
mock_socket.sendmail.return_value = {} |
| 3367 |
mock_smtp.return_value = mock_socket |
| 3368 |
mock_smtpssl.return_value = mock_socket |
| 3369 |
|
| 3370 |
prv_path = os.path.join( |
| 3371 |
os.path.dirname(__file__), "var", "pgp", "valid-prv.asc" |
| 3372 |
) |
| 3373 |
asset = AppriseAsset( |
| 3374 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3375 |
storage_path=str(tmpdir), |
| 3376 |
) |
| 3377 |
obj = Apprise.instantiate( |
| 3378 |
f"mailto://user:[email protected]?pgp=sign&pgpprv={prv_path}", |
| 3379 |
asset=asset, |
| 3380 |
) |
| 3381 |
assert obj is not None |
| 3382 |
|
| 3383 |
# Notification should succeed and sendmail should be called once |
| 3384 |
assert bool(obj.notify("test body")) is True |
| 3385 |
assert mock_socket.sendmail.call_count == 1 |
| 3386 |
|
| 3387 |
# The emitted email must contain the multipart/signed structure |
| 3388 |
raw = mock_socket.sendmail.call_args[0][2] |
| 3389 |
assert "multipart/signed" in raw |
| 3390 |
assert "application/pgp-signature" in raw |
| 3391 |
|
| 3392 |
|
| 3393 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3394 |
@mock.patch("smtplib.SMTP_SSL") |
| 3395 |
@mock.patch("smtplib.SMTP") |
| 3396 |
def test_plugin_email_pgp_encrypt_send_with_autocrypt( |
| 3397 |
mock_smtp, mock_smtpssl, tmpdir |
| 3398 |
): |
| 3399 |
"""Encrypt with a recipient key and advertise the sender key.""" |
| 3400 |
|
| 3401 |
mock_socket = mock.Mock() |
| 3402 |
mock_socket.starttls.return_value = True |
| 3403 |
mock_socket.login.return_value = True |
| 3404 |
mock_socket.sendmail.return_value = {} |
| 3405 |
mock_smtp.return_value = mock_socket |
| 3406 |
mock_smtpssl.return_value = mock_socket |
| 3407 |
|
| 3408 |
pub_path = os.path.join( |
| 3409 |
os.path.dirname(__file__), "var", "pgp", "valid-pub.asc" |
| 3410 |
) |
| 3411 |
|
| 3412 |
# Generate a sender key that includes an encryption subkey. |
| 3413 |
keygen_ctrl = utils.pgp.ApprisePGPController( |
| 3414 |
path=str(tmpdir.mkdir("keygen")), email="[email protected]" |
| 3415 |
) |
| 3416 |
assert keygen_ctrl.keygen() is True |
| 3417 |
own_prv_path = keygen_ctrl.private_keyfile() |
| 3418 |
|
| 3419 |
asset = AppriseAsset( |
| 3420 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3421 |
storage_path=str(tmpdir.mkdir("send")), |
| 3422 |
) |
| 3423 |
obj = Apprise.instantiate( |
| 3424 |
"mailto://user:[email protected]/[email protected]" |
| 3425 |
f"?pgp=encrypt&pgppub={pub_path}&pgpprv={own_prv_path}", |
| 3426 |
asset=asset, |
| 3427 |
) |
| 3428 |
assert obj is not None |
| 3429 |
assert bool(obj.notify("test body")) is True |
| 3430 |
|
| 3431 |
raw = mock_socket.sendmail.call_args[0][2] |
| 3432 |
assert "multipart/encrypted" in raw |
| 3433 |
assert raw.count("Autocrypt:") == 1 |
| 3434 |
|
| 3435 |
|
| 3436 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3437 |
@mock.patch("smtplib.SMTP_SSL") |
| 3438 |
@mock.patch("smtplib.SMTP") |
| 3439 |
def test_plugin_email_pgp_encrypt_self_send_autogens_key( |
| 3440 |
mock_smtp, mock_smtpssl, tmpdir |
| 3441 |
): |
| 3442 |
"""Encrypted self-sends may generate the sender's key.""" |
| 3443 |
|
| 3444 |
mock_socket = mock.Mock() |
| 3445 |
mock_socket.starttls.return_value = True |
| 3446 |
mock_socket.login.return_value = True |
| 3447 |
mock_socket.sendmail.return_value = {} |
| 3448 |
mock_smtp.return_value = mock_socket |
| 3449 |
mock_smtpssl.return_value = mock_socket |
| 3450 |
|
| 3451 |
asset = AppriseAsset( |
| 3452 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3453 |
storage_path=str(tmpdir), |
| 3454 |
) |
| 3455 |
obj = Apprise.instantiate( |
| 3456 |
"mailto://user:[email protected]?pgp=encrypt", asset=asset |
| 3457 |
) |
| 3458 |
assert obj is not None |
| 3459 |
assert obj.pgp.private_key() is None |
| 3460 |
|
| 3461 |
assert bool(obj.notify("test body")) is True |
| 3462 |
|
| 3463 |
raw = mock_socket.sendmail.call_args[0][2] |
| 3464 |
assert "multipart/encrypted" in raw |
| 3465 |
assert obj.pgp.private_key() is not None |
| 3466 |
|
| 3467 |
|
| 3468 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3469 |
@mock.patch("smtplib.SMTP_SSL") |
| 3470 |
@mock.patch("smtplib.SMTP") |
| 3471 |
def test_plugin_email_pgp_encrypt_external_recipient_no_autogen( |
| 3472 |
mock_smtp, mock_smtpssl, tmpdir |
| 3473 |
): |
| 3474 |
"""Encrypted external sends never generate the recipient's key.""" |
| 3475 |
|
| 3476 |
mock_socket = mock.Mock() |
| 3477 |
mock_socket.starttls.return_value = True |
| 3478 |
mock_socket.login.return_value = True |
| 3479 |
mock_socket.sendmail.return_value = {} |
| 3480 |
mock_smtp.return_value = mock_socket |
| 3481 |
mock_smtpssl.return_value = mock_socket |
| 3482 |
|
| 3483 |
asset = AppriseAsset( |
| 3484 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3485 |
storage_path=str(tmpdir), |
| 3486 |
) |
| 3487 |
obj = Apprise.instantiate( |
| 3488 |
"mailto://user:[email protected]/[email protected]?pgp=encrypt", |
| 3489 |
asset=asset, |
| 3490 |
) |
| 3491 |
assert obj is not None |
| 3492 |
|
| 3493 |
assert bool(obj.notify("test body")) is False |
| 3494 |
assert obj.pgp.public_keyfile("[email protected]") is None |
| 3495 |
|
| 3496 |
|
| 3497 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3498 |
@mock.patch("smtplib.SMTP_SSL") |
| 3499 |
@mock.patch("smtplib.SMTP") |
| 3500 |
def test_plugin_email_send_without_autocrypt(mock_smtp, mock_smtpssl, tmpdir): |
| 3501 |
"""Protected sends omit Autocrypt when no sender key is available.""" |
| 3502 |
|
| 3503 |
mock_socket = mock.Mock() |
| 3504 |
mock_socket.starttls.return_value = True |
| 3505 |
mock_socket.login.return_value = True |
| 3506 |
# An empty refusal map means SMTP accepted every recipient. |
| 3507 |
mock_socket.sendmail.return_value = {} |
| 3508 |
mock_smtp.return_value = mock_socket |
| 3509 |
mock_smtpssl.return_value = mock_socket |
| 3510 |
|
| 3511 |
prv_path = os.path.join( |
| 3512 |
os.path.dirname(__file__), "var", "pgp", "valid-prv.asc" |
| 3513 |
) |
| 3514 |
pub_path = os.path.join( |
| 3515 |
os.path.dirname(__file__), "var", "pgp", "valid-pub.asc" |
| 3516 |
) |
| 3517 |
|
| 3518 |
with mock.patch.object( |
| 3519 |
utils.pgp.ApprisePGPController, |
| 3520 |
"autocrypt_header", |
| 3521 |
return_value=None, |
| 3522 |
): |
| 3523 |
# Sign mode |
| 3524 |
asset = AppriseAsset( |
| 3525 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3526 |
storage_path=str(tmpdir.mkdir("sign")), |
| 3527 |
) |
| 3528 |
obj = Apprise.instantiate( |
| 3529 |
f"mailto://user:[email protected]?pgp=sign&pgpprv={prv_path}", |
| 3530 |
asset=asset, |
| 3531 |
) |
| 3532 |
assert bool(obj.notify("test body")) is True |
| 3533 |
raw = mock_socket.sendmail.call_args[0][2] |
| 3534 |
assert "Autocrypt:" not in raw |
| 3535 |
|
| 3536 |
mock_socket.reset_mock() |
| 3537 |
|
| 3538 |
# Encrypt mode |
| 3539 |
asset = AppriseAsset( |
| 3540 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3541 |
storage_path=str(tmpdir.mkdir("encrypt")), |
| 3542 |
) |
| 3543 |
obj = Apprise.instantiate( |
| 3544 |
f"mailto://user:[email protected]?pgp=encrypt&pgppub={pub_path}", |
| 3545 |
asset=asset, |
| 3546 |
) |
| 3547 |
assert bool(obj.notify("test body")) is True |
| 3548 |
raw = mock_socket.sendmail.call_args[0][2] |
| 3549 |
assert "Autocrypt:" not in raw |
| 3550 |
|
| 3551 |
|
| 3552 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3553 |
@mock.patch("smtplib.SMTP_SSL") |
| 3554 |
@mock.patch("smtplib.SMTP") |
| 3555 |
def test_plugin_email_autocrypt_deduplication(mock_smtp, mock_smtpssl, tmpdir): |
| 3556 |
"""Generated Autocrypt headers replace custom case variants.""" |
| 3557 |
|
| 3558 |
mock_socket = mock.Mock() |
| 3559 |
mock_socket.starttls.return_value = True |
| 3560 |
mock_socket.login.return_value = True |
| 3561 |
# An empty refusal map means SMTP accepted every recipient. |
| 3562 |
mock_socket.sendmail.return_value = {} |
| 3563 |
mock_smtp.return_value = mock_socket |
| 3564 |
mock_smtpssl.return_value = mock_socket |
| 3565 |
|
| 3566 |
# Generate a sender key that Autocrypt can advertise. |
| 3567 |
keygen_ctrl = utils.pgp.ApprisePGPController( |
| 3568 |
path=str(tmpdir.mkdir("keygen")), email="[email protected]" |
| 3569 |
) |
| 3570 |
assert keygen_ctrl.keygen() is True |
| 3571 |
prv_path = keygen_ctrl.private_keyfile() |
| 3572 |
|
| 3573 |
# Keep iterations isolated, even on case-insensitive filesystems. |
| 3574 |
for index, custom_key in enumerate( |
| 3575 |
("Autocrypt", "autocrypt", "AUTOCRYPT") |
| 3576 |
): |
| 3577 |
mock_socket.reset_mock() |
| 3578 |
asset = AppriseAsset( |
| 3579 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3580 |
storage_path=str(tmpdir.mkdir(f"sign-{index}")), |
| 3581 |
) |
| 3582 |
spoofed_value = "addr%3Dattacker%40example.com%3B%20keydata%3Dbogus" |
| 3583 |
obj = Apprise.instantiate( |
| 3584 |
f"mailto://user:[email protected]?pgp=sign&pgpprv={prv_path}" |
| 3585 |
f"&+{custom_key}={spoofed_value}", |
| 3586 |
asset=asset, |
| 3587 |
) |
| 3588 |
assert bool(obj.notify("test body")) is True |
| 3589 |
raw = mock_socket.sendmail.call_args[0][2] |
| 3590 |
|
| 3591 |
# Keep only the controller-generated Autocrypt value. |
| 3592 |
assert raw.lower().count("autocrypt:") == 1 |
| 3593 |
assert "[email protected]" not in raw |
| 3594 |
assert "keydata=bogus" not in raw |
| 3595 |
|
| 3596 |
|
| 3597 |
@mock.patch("smtplib.SMTP_SSL") |
| 3598 |
@mock.patch("smtplib.SMTP") |
| 3599 |
def test_plugin_email_autocrypt_custom_only_deduplication( |
| 3600 |
mock_smtp, mock_smtpssl |
| 3601 |
): |
| 3602 |
"""Custom Autocrypt header names are deduplicated case-insensitively.""" |
| 3603 |
|
| 3604 |
mock_socket = mock.Mock() |
| 3605 |
mock_socket.starttls.return_value = True |
| 3606 |
mock_socket.login.return_value = True |
| 3607 |
mock_socket.sendmail.return_value = {} |
| 3608 |
mock_smtp.return_value = mock_socket |
| 3609 |
mock_smtpssl.return_value = mock_socket |
| 3610 |
|
| 3611 |
# pgp=no prevents a controller-generated Autocrypt header. |
| 3612 |
obj = Apprise.instantiate( |
| 3613 |
"mailto://user:[email protected]/[email protected]" |
| 3614 |
"?+Autocrypt=first&+autocrypt=second" |
| 3615 |
) |
| 3616 |
assert bool(obj.notify("test body")) is True |
| 3617 |
|
| 3618 |
raw = mock_socket.sendmail.call_args[0][2] |
| 3619 |
assert raw.lower().count("autocrypt:") == 1 |
| 3620 |
assert "Autocrypt: first" in raw |
| 3621 |
assert "second" not in raw |
| 3622 |
|
| 3623 |
|
| 3624 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3625 |
def test_plugin_email_pgp_sign_crlf_roundtrip(tmpdir): |
| 3626 |
"""PGP signature must verify against CRLF content, not bare LF. |
| 3627 |
|
| 3628 |
This test verifies we are following RFC 3156's requirement |
| 3629 |
""" |
| 3630 |
import pgpy as _pgpy |
| 3631 |
|
| 3632 |
prv_path = os.path.join(TEST_VAR_DIR, "pgp", "valid-prv.asc") |
| 3633 |
|
| 3634 |
# Build a controller that uses the test fixture private key |
| 3635 |
ctrl = utils.pgp.ApprisePGPController( |
| 3636 |
path=str(tmpdir), prv_keyfile=prv_path |
| 3637 |
) |
| 3638 |
|
| 3639 |
# Obtain the corresponding public key so we can verify independently; |
| 3640 |
# the private key object exposes its own public sub-key via .pubkey |
| 3641 |
prv_key = ctrl.private_key() |
| 3642 |
assert prv_key is not None |
| 3643 |
pub_key = prv_key.pubkey |
| 3644 |
|
| 3645 |
# Simulate a MIME body with bare LF endings (as Python produces on Linux) |
| 3646 |
raw_body = "Content-Type: text/plain\r\n\r\nHello RFC 3156\n" |
| 3647 |
|
| 3648 |
# Build the CRLF form — exactly what the fixed code passes to sign() |
| 3649 |
crlf_body = re.sub(r"(?:\r\n|\n|\r(?!\n))", "\r\n", raw_body) |
| 3650 |
|
| 3651 |
# Build the bare-LF form — what the unfixed code would have passed |
| 3652 |
lf_body = re.sub(r"\r\n", "\n", crlf_body) |
| 3653 |
|
| 3654 |
# Sign the CRLF form (as the production code does) |
| 3655 |
result = ctrl.sign(crlf_body) |
| 3656 |
assert result is not None, "sign() must succeed with a valid private key" |
| 3657 |
sig_str, micalg = result |
| 3658 |
assert micalg.startswith("pgp-") |
| 3659 |
|
| 3660 |
# Parse the returned armored signature back to a PGPSignature object |
| 3661 |
sig = _pgpy.PGPSignature.from_blob(sig_str) |
| 3662 |
|
| 3663 |
# Verify the CRLF form received by the recipient. |
| 3664 |
# PGPy reports verification through the result's truth value. |
| 3665 |
crlf_ok = bool(pub_key.verify(crlf_body, sig)) |
| 3666 |
assert crlf_ok, ( |
| 3667 |
"Signature did not verify against CRLF content -- " |
| 3668 |
"the signed body must use CRLF line endings (RFC 3156)" |
| 3669 |
) |
| 3670 |
|
| 3671 |
# The bare-LF form must not verify after CRLF normalization. |
| 3672 |
lf_ok = bool(pub_key.verify(lf_body, sig)) |
| 3673 |
assert not lf_ok, ( |
| 3674 |
"Signature verified against bare-LF content -- CRLF normalisation " |
| 3675 |
"before pgp.sign() may have been removed (regression)" |
| 3676 |
) |
| 3677 |
|
| 3678 |
|
| 3679 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3680 |
@mock.patch("smtplib.SMTP_SSL") |
| 3681 |
@mock.patch("smtplib.SMTP") |
| 3682 |
def test_plugin_email_pgp_sign_wire_content_crlf( |
| 3683 |
mock_smtp, mock_smtpssl, tmpdir |
| 3684 |
): |
| 3685 |
"""Content passed to pgp.sign() must have no bare LF line endings. |
| 3686 |
|
| 3687 |
Further verification ensureing RFC 3156 requirements are met |
| 3688 |
""" |
| 3689 |
mock_socket = mock.Mock() |
| 3690 |
mock_socket.starttls.return_value = True |
| 3691 |
mock_socket.login.return_value = True |
| 3692 |
# An empty refusal map means SMTP accepted every recipient. |
| 3693 |
mock_socket.sendmail.return_value = {} |
| 3694 |
mock_smtp.return_value = mock_socket |
| 3695 |
mock_smtpssl.return_value = mock_socket |
| 3696 |
|
| 3697 |
prv_path = os.path.join(TEST_VAR_DIR, "pgp", "valid-prv.asc") |
| 3698 |
|
| 3699 |
# Capture the exact content forwarded to pgp.sign() during send |
| 3700 |
signed_content = {} |
| 3701 |
original_sign = utils.pgp.ApprisePGPController.sign |
| 3702 |
|
| 3703 |
def capturing_sign(self, message): |
| 3704 |
# Record the message that base.py passes to the signer |
| 3705 |
signed_content["message"] = message |
| 3706 |
return original_sign(self, message) |
| 3707 |
|
| 3708 |
asset = AppriseAsset( |
| 3709 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3710 |
storage_path=str(tmpdir), |
| 3711 |
) |
| 3712 |
obj = Apprise.instantiate( |
| 3713 |
f"mailto://user:[email protected]?pgp=sign&pgpprv={prv_path}", |
| 3714 |
asset=asset, |
| 3715 |
) |
| 3716 |
assert obj is not None |
| 3717 |
|
| 3718 |
# Send while intercepting sign() to record the input content |
| 3719 |
with mock.patch.object( |
| 3720 |
utils.pgp.ApprisePGPController, "sign", capturing_sign |
| 3721 |
): |
| 3722 |
assert bool(obj.notify("test body")) is True |
| 3723 |
|
| 3724 |
# Confirm send actually happened and our interceptor was triggered |
| 3725 |
assert mock_socket.sendmail.call_count == 1 |
| 3726 |
assert "message" in signed_content, ( |
| 3727 |
"pgp.sign() was not called during the send — " |
| 3728 |
"the interception wrapper was not reached" |
| 3729 |
) |
| 3730 |
|
| 3731 |
# Verify every line ending in the signed content must be CRLF |
| 3732 |
assert not re.search(r"(?<!\r)\n", signed_content["message"]), ( |
| 3733 |
"pgp.sign() received content with bare LF line endings — " |
| 3734 |
"CRLF normalisation (RFC 3156 compliance) may have been removed " |
| 3735 |
"from base.py" |
| 3736 |
) |
| 3737 |
|
| 3738 |
|
| 3739 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3740 |
@mock.patch("smtplib.SMTP_SSL") |
| 3741 |
@mock.patch("smtplib.SMTP") |
| 3742 |
def test_plugin_email_pgp_sign_no_privkey_fails( |
| 3743 |
mock_smtp, mock_smtpssl, tmpdir |
| 3744 |
): |
| 3745 |
"""pgp=sign with no accessible private key causes send to fail.""" |
| 3746 |
|
| 3747 |
mock_socket = mock.Mock() |
| 3748 |
mock_socket.starttls.return_value = True |
| 3749 |
mock_socket.login.return_value = True |
| 3750 |
# An empty refusal map means SMTP accepted every recipient. |
| 3751 |
mock_socket.sendmail.return_value = {} |
| 3752 |
mock_smtp.return_value = mock_socket |
| 3753 |
mock_smtpssl.return_value = mock_socket |
| 3754 |
|
| 3755 |
asset = AppriseAsset( |
| 3756 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3757 |
storage_path=str(tmpdir), |
| 3758 |
) |
| 3759 |
# Sign mode requested but no private key in tmpdir and none specified |
| 3760 |
obj = Apprise.instantiate( |
| 3761 |
"mailto://user:[email protected]?pgp=sign", |
| 3762 |
asset=asset, |
| 3763 |
) |
| 3764 |
assert obj is not None |
| 3765 |
assert bool(obj.notify("test body")) is False |
| 3766 |
|
| 3767 |
|
| 3768 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3769 |
@mock.patch("smtplib.SMTP_SSL") |
| 3770 |
@mock.patch("smtplib.SMTP") |
| 3771 |
def test_plugin_email_pgp_sign_opportunistic_encrypt( |
| 3772 |
mock_smtp, mock_smtpssl, tmpdir |
| 3773 |
): |
| 3774 |
"""pgp=sign with WKD: signs always, encrypts when WKD key found.""" |
| 3775 |
|
| 3776 |
mock_socket = mock.Mock() |
| 3777 |
mock_socket.starttls.return_value = True |
| 3778 |
mock_socket.login.return_value = True |
| 3779 |
# An empty refusal map means SMTP accepted every recipient. |
| 3780 |
mock_socket.sendmail.return_value = {} |
| 3781 |
mock_smtp.return_value = mock_socket |
| 3782 |
mock_smtpssl.return_value = mock_socket |
| 3783 |
|
| 3784 |
prv_path = os.path.join( |
| 3785 |
os.path.dirname(__file__), "var", "pgp", "valid-prv.asc" |
| 3786 |
) |
| 3787 |
|
| 3788 |
# Generate a public key to serve as the WKD payload |
| 3789 |
key = pgpy.PGPKey.new( |
| 3790 |
pgpy.constants.PubKeyAlgorithm.RSAEncryptOrSign, 2048 |
| 3791 |
) |
| 3792 |
uid = pgpy.PGPUID.new("User", email="[email protected]") |
| 3793 |
key.add_uid( |
| 3794 |
uid, |
| 3795 |
usage={ |
| 3796 |
pgpy.constants.KeyFlags.Sign, |
| 3797 |
pgpy.constants.KeyFlags.EncryptCommunications, |
| 3798 |
}, |
| 3799 |
hashes=[pgpy.constants.HashAlgorithm.SHA256], |
| 3800 |
ciphers=[pgpy.constants.SymmetricKeyAlgorithm.AES256], |
| 3801 |
compression=[pgpy.constants.CompressionAlgorithm.ZLIB], |
| 3802 |
) |
| 3803 |
pub_bytes = str(key.pubkey).encode() |
| 3804 |
|
| 3805 |
asset = AppriseAsset( |
| 3806 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3807 |
storage_path=str(tmpdir), |
| 3808 |
) |
| 3809 |
obj = Apprise.instantiate( |
| 3810 |
f"mailto://user:[email protected]?pgp=sign&wkd=yes&pgpprv={prv_path}", |
| 3811 |
asset=asset, |
| 3812 |
) |
| 3813 |
assert obj is not None |
| 3814 |
assert obj.pgp_mode == "sign" |
| 3815 |
|
| 3816 |
# With WKD returning a public key: sign + encrypt |
| 3817 |
with mock.patch.object(obj.pgp.wkd, "fetch", return_value=pub_bytes): |
| 3818 |
assert bool(obj.notify("test body")) is True |
| 3819 |
|
| 3820 |
raw = mock_socket.sendmail.call_args[0][2] |
| 3821 |
# The outer structure is multipart/encrypted (the signed body is inside) |
| 3822 |
assert "multipart/encrypted" in raw |
| 3823 |
mock_socket.sendmail.reset_mock() |
| 3824 |
|
| 3825 |
# Clear the cache so the next call re-queries WKD |
| 3826 |
obj.pgp._ApprisePGPController__key_lookup.clear() |
| 3827 |
|
| 3828 |
# With WKD returning nothing: sign only, no failure |
| 3829 |
with mock.patch.object(obj.pgp.wkd, "fetch", return_value=None): |
| 3830 |
assert bool(obj.notify("test body")) is True |
| 3831 |
|
| 3832 |
raw = mock_socket.sendmail.call_args[0][2] |
| 3833 |
# No encryption this time; just a signed message |
| 3834 |
assert "multipart/signed" in raw |
| 3835 |
assert "multipart/encrypted" not in raw |
| 3836 |
|
| 3837 |
|
| 3838 |
@mock.patch("smtplib.SMTP_SSL") |
| 3839 |
@mock.patch("smtplib.SMTP") |
| 3840 |
def test_plugin_email_pgp_sign_no_pgp_support(mock_smtp, mock_smtpssl): |
| 3841 |
"""pgp=sign fails gracefully when PGPy is not installed.""" |
| 3842 |
|
| 3843 |
mock_socket = mock.Mock() |
| 3844 |
mock_socket.starttls.return_value = True |
| 3845 |
mock_socket.login.return_value = True |
| 3846 |
# An empty refusal map means SMTP accepted every recipient. |
| 3847 |
mock_socket.sendmail.return_value = {} |
| 3848 |
mock_smtp.return_value = mock_socket |
| 3849 |
mock_smtpssl.return_value = mock_socket |
| 3850 |
|
| 3851 |
prv_path = os.path.join( |
| 3852 |
os.path.dirname(__file__), "var", "pgp", "valid-prv.asc" |
| 3853 |
) |
| 3854 |
obj = Apprise.instantiate( |
| 3855 |
f"mailto://user:[email protected]?pgp=sign&pgpprv={prv_path}" |
| 3856 |
) |
| 3857 |
assert obj is not None |
| 3858 |
|
| 3859 |
# Simulate PGPy not installed |
| 3860 |
import apprise.utils.pgp as pgp_utils |
| 3861 |
|
| 3862 |
real_support = pgp_utils.PGP_SUPPORT |
| 3863 |
pgp_utils.PGP_SUPPORT = False |
| 3864 |
try: |
| 3865 |
assert bool(obj.notify("test body")) is False |
| 3866 |
finally: |
| 3867 |
pgp_utils.PGP_SUPPORT = real_support |
| 3868 |
|
| 3869 |
|
| 3870 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3871 |
@mock.patch("smtplib.SMTP_SSL") |
| 3872 |
@mock.patch("smtplib.SMTP") |
| 3873 |
def test_plugin_email_pgp_sign_encrypt_none_mode( |
| 3874 |
mock_smtp, mock_smtpssl, tmpdir |
| 3875 |
): |
| 3876 |
"""prepare_emails() skips PGP entirely when pgp_mode is PGPMode.NONE.""" |
| 3877 |
|
| 3878 |
mock_socket = mock.Mock() |
| 3879 |
mock_socket.starttls.return_value = True |
| 3880 |
mock_socket.login.return_value = True |
| 3881 |
# An empty refusal map means SMTP accepted every recipient. |
| 3882 |
mock_socket.sendmail.return_value = {} |
| 3883 |
mock_smtp.return_value = mock_socket |
| 3884 |
mock_smtpssl.return_value = mock_socket |
| 3885 |
|
| 3886 |
prv_path = os.path.join( |
| 3887 |
os.path.dirname(__file__), "var", "pgp", "valid-prv.asc" |
| 3888 |
) |
| 3889 |
asset = AppriseAsset( |
| 3890 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3891 |
storage_path=str(tmpdir), |
| 3892 |
) |
| 3893 |
|
| 3894 |
# pgp=no disables all PGP even when a private key is provided |
| 3895 |
obj = Apprise.instantiate( |
| 3896 |
f"mailto://user:[email protected]?pgp=no&pgpprv={prv_path}", |
| 3897 |
asset=asset, |
| 3898 |
) |
| 3899 |
assert obj is not None |
| 3900 |
assert bool(obj.notify("test body")) is True |
| 3901 |
|
| 3902 |
# The email must not contain any PGP markers |
| 3903 |
raw = mock_socket.sendmail.call_args[0][2] |
| 3904 |
assert "pgp-signature" not in raw.lower() |
| 3905 |
assert "pgp-encrypted" not in raw.lower() |
| 3906 |
|
| 3907 |
|
| 3908 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 3909 |
@mock.patch("smtplib.SMTP_SSL") |
| 3910 |
@mock.patch("smtplib.SMTP") |
| 3911 |
def test_plugin_email_pgp_sign_keygen_auto(mock_smtp, mock_smtpssl, tmpdir): |
| 3912 |
"""pgp=sign auto-discovers a private key generated by keygen().""" |
| 3913 |
|
| 3914 |
mock_socket = mock.Mock() |
| 3915 |
mock_socket.starttls.return_value = True |
| 3916 |
mock_socket.login.return_value = True |
| 3917 |
# An empty refusal map means SMTP accepted every recipient. |
| 3918 |
mock_socket.sendmail.return_value = {} |
| 3919 |
mock_smtp.return_value = mock_socket |
| 3920 |
mock_smtpssl.return_value = mock_socket |
| 3921 |
|
| 3922 |
asset = AppriseAsset( |
| 3923 |
storage_mode=PersistentStoreMode.FLUSH, |
| 3924 |
storage_path=str(tmpdir), |
| 3925 |
) |
| 3926 |
obj = Apprise.instantiate( |
| 3927 |
"mailto://user:[email protected]?pgp=sign", |
| 3928 |
asset=asset, |
| 3929 |
) |
| 3930 |
assert obj is not None |
| 3931 |
|
| 3932 |
# Generate the key pair into the storage directory |
| 3933 |
assert obj.pgp.keygen() is True |
| 3934 |
|
| 3935 |
# Sending must discover the generated private key. |
| 3936 |
assert bool(obj.notify("test body")) is True |
| 3937 |
|
| 3938 |
|
| 3939 |
@mock.patch("smtplib.SMTP") |
| 3940 |
def test_plugin_email_multi_format(mock_smtp): |
| 3941 |
"""NotifyEmail() declares HTML+TEXT and resolves per-call.""" |
| 3942 |
|
| 3943 |
assert email.NotifyEmail.notify_format == ( |
| 3944 |
NotifyFormat.HTML, |
| 3945 |
NotifyFormat.TEXT, |
| 3946 |
) |
| 3947 |
|
| 3948 |
instance = mock_smtp.return_value |
| 3949 |
instance.sendmail.return_value = {} |
| 3950 |
|
| 3951 |
aobj = Apprise() |
| 3952 |
assert aobj.add("mailto://user:[email protected]") is True |
| 3953 |
|
| 3954 |
# Default (no body_format): still builds a multipart/alternative |
| 3955 |
# message with both an HTML and a derived plain-text part. |
| 3956 |
assert bool(aobj.notify(body="<b>Bold HTML</b> body")) is True |
| 3957 |
msg = instance.sendmail.call_args_list[-1][0][2] |
| 3958 |
assert "multipart" in msg |
| 3959 |
assert "text/plain" in msg |
| 3960 |
assert "text/html" in msg |
| 3961 |
|
| 3962 |
# body_format=TEXT aligns directly to TEXT mode (no HTML round |
| 3963 |
# trip); the exact original text is sent, unescaped and unchanged. |
| 3964 |
assert ( |
| 3965 |
bool( |
| 3966 |
aobj.notify( |
| 3967 |
body="Plain text with <literal> angle brackets.", |
| 3968 |
body_format=NotifyFormat.TEXT, |
| 3969 |
) |
| 3970 |
) |
| 3971 |
is True |
| 3972 |
) |
| 3973 |
msg = instance.sendmail.call_args_list[-1][0][2] |
| 3974 |
assert "multipart" not in msg |
| 3975 |
assert "text/plain" in msg |
| 3976 |
assert "text/html" not in msg |
| 3977 |
assert "Plain text with <literal> angle brackets." in msg |
| 3978 |
|
| 3979 |
|
| 3980 |
def test_plugin_email_prepare(): |
| 3981 |
"""NotifyEmail() prepare_emails static function.""" |
| 3982 |
with pytest.raises(AppriseException): |
| 3983 |
# No To: provided |
| 3984 |
for _e in email.NotifyEmail.prepare_emails( |
| 3985 |
subject="Email Subject", |
| 3986 |
body="Email Body", |
| 3987 |
from_addr=(None, "[email protected]"), |
| 3988 |
to=[], |
| 3989 |
): |
| 3990 |
pass |
| 3991 |
|
| 3992 |
# Most basic call (a lot of defaults are used) |
| 3993 |
iterator = email.NotifyEmail.prepare_emails( |
| 3994 |
subject="Email Subject", |
| 3995 |
body="Email Body", |
| 3996 |
from_addr=(None, "[email protected]"), |
| 3997 |
to=[ |
| 3998 |
("Apprise User", "[email protected]"), |
| 3999 |
], |
| 4000 |
) |
| 4001 |
entries = list(iterator) |
| 4002 |
assert len(entries) == 1 |
| 4003 |
|
| 4004 |
|
| 4005 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 4006 |
def test_plugin_pgp(tmpdir): |
| 4007 |
"""Pretty Good Privacy Testing.""" |
| 4008 |
|
| 4009 |
p_obj = utils.pgp.ApprisePGPController(path=None) |
| 4010 |
# No Path |
| 4011 |
assert p_obj.keygen() is False |
| 4012 |
assert p_obj.public_keyfile() is None |
| 4013 |
|
| 4014 |
p_obj = utils.pgp.ApprisePGPController(path=None, email="[email protected]") |
| 4015 |
# No Path |
| 4016 |
assert p_obj.keygen() is False |
| 4017 |
|
| 4018 |
tmpdir0 = tmpdir.mkdir("tmp00") |
| 4019 |
p_obj = utils.pgp.ApprisePGPController( |
| 4020 |
path=str(tmpdir0), email="[email protected]" |
| 4021 |
) |
| 4022 |
|
| 4023 |
# A key can be generated with a path defined |
| 4024 |
assert p_obj.keygen() is True |
| 4025 |
assert p_obj.public_keyfile() is not None |
| 4026 |
# A key can be generated with a path defined |
| 4027 |
assert p_obj.keygen(name="Apprise", force=True) is True |
| 4028 |
assert ( |
| 4029 |
p_obj.keygen(email="[email protected]", name="Apprise", force=True) is True |
| 4030 |
) |
| 4031 |
|
| 4032 |
assert utils.pgp.PGP_SUPPORT is True |
| 4033 |
utils.pgp.PGP_SUPPORT = False |
| 4034 |
|
| 4035 |
with pytest.raises(AppriseException): |
| 4036 |
assert p_obj.public_keyfile() |
| 4037 |
|
| 4038 |
# Return the PGP status for remaining checks |
| 4039 |
utils.pgp.PGP_SUPPORT = True |
| 4040 |
|
| 4041 |
tmpdir1 = tmpdir.mkdir("tmp01") |
| 4042 |
p_obj = utils.pgp.ApprisePGPController( |
| 4043 |
path=str(tmpdir1), pub_keyfile="bad-file" |
| 4044 |
) |
| 4045 |
assert p_obj.public_keyfile() is False |
| 4046 |
|
| 4047 |
|
| 4048 |
@pytest.mark.skipif("pgpy" not in sys.modules, reason="Requires PGPy") |
| 4049 |
def test_pgp_public_keyfile_skips_self_email(tmpdir): |
| 4050 |
"""Test pgp.public_keyfile() when self.email is None and skipped.""" |
| 4051 |
|
| 4052 |
key_dir = tmpdir.mkdir("pgpkeytest2") |
| 4053 |
key_path = os.path.join(str(key_dir), "externaluser-pub.asc") |
| 4054 |
|
| 4055 |
# Create a fake matching keyfile to trigger discovery |
| 4056 |
with open(key_path, "w") as f: |
| 4057 |
f.write("-----BEGIN PGP PUBLIC KEY BLOCK-----\n") |
| 4058 |
|
| 4059 |
# Controller without setting self.email |
| 4060 |
pgp = utils.pgp.ApprisePGPController(path=str(key_dir), email=None) |
| 4061 |
|
| 4062 |
# Should skip over `if self.email:` logic entirely |
| 4063 |
result = pgp.public_keyfile("[email protected]") |
| 4064 |
|
| 4065 |
assert result.endswith("externaluser-pub.asc") |
| 4066 |
|
| 4067 |
|
| 4068 |
@mock.patch("smtplib.SMTP") |
| 4069 |
def test_plugin_email_gmx_template_lookup(mock_smtp): |
| 4070 |
"""NotifyEmail() GMX template lookup tests.""" |
| 4071 |
|
| 4072 |
response = mock.Mock() |
| 4073 |
# An empty refusal map means SMTP accepted every recipient. |
| 4074 |
response.sendmail.return_value = {} |
| 4075 |
mock_smtp.return_value = response |
| 4076 |
|
| 4077 |
for domain in ( |
| 4078 |
"gmx.net", |
| 4079 |
"gmx.com", |
| 4080 |
"gmx.de", |
| 4081 |
"gmx.at", |
| 4082 |
"gmx.ch", |
| 4083 |
"gmx.fr", |
| 4084 |
): |
| 4085 |
results = email.NotifyEmail.parse_url( |
| 4086 |
f"mailtos://user:pass123@{domain}" |
| 4087 |
) |
| 4088 |
obj = Apprise.instantiate(results, suppress_exceptions=False) |
| 4089 |
assert isinstance(obj, email.NotifyEmail) |
| 4090 |
|
| 4091 |
# Template-driven defaults |
| 4092 |
assert obj.smtp_host == "mail.gmx.com" |
| 4093 |
assert obj.secure_mode == "starttls" |
| 4094 |
assert obj.port == 587 |
| 4095 |
assert obj.secure is True |
| 4096 |
|
| 4097 |
# Send once to trigger SMTP/login behaviour |
| 4098 |
assert bool(obj.notify("body", "title")) is True |
| 4099 |
|
| 4100 |
# STARTTLS used |
| 4101 |
assert response.starttls.call_count == 1 |
| 4102 |
assert response.login.call_count == 1 |
| 4103 |
|
| 4104 |
login_user, login_pass = response.login.call_args[0] |
| 4105 |
mock_smtp.reset_mock() |
| 4106 |
|
| 4107 |
assert login_pass == "pass123" |
| 4108 |
|
| 4109 |
# GMX should authenticate with full email, not just the local part |
| 4110 |
assert login_user == f"user@{domain}" |
| 4111 |
|
| 4112 |
|
| 4113 |
@mock.patch("smtplib.SMTP") |
| 4114 |
@mock.patch("smtplib.SMTP_SSL") |
| 4115 |
def test_plugin_email_tls_certificate_verification(mock_smtpssl, mock_smtp): |
| 4116 |
"""Verify that SMTPS and STARTTLS honour the verify_certificate setting by |
| 4117 |
passing a validating SSL context to smtplib.""" |
| 4118 |
|
| 4119 |
response = mock.Mock() |
| 4120 |
# An empty refusal map means SMTP accepted every recipient. |
| 4121 |
response.sendmail.return_value = {} |
| 4122 |
response.starttls.return_value = True |
| 4123 |
response.login.return_value = True |
| 4124 |
response.sendmail.return_value = {} |
| 4125 |
response.quit.return_value = True |
| 4126 |
mock_smtp.return_value = response |
| 4127 |
mock_smtpssl.return_value = response |
| 4128 |
|
| 4129 |
# 1. SMTPS (mode=ssl) with the default verify_certificate=True should |
| 4130 |
# pass a context that validates the certificate and hostname. |
| 4131 |
obj = Apprise.instantiate( |
| 4132 |
"mailtos://user:[email protected]?mode=ssl", |
| 4133 |
suppress_exceptions=False, |
| 4134 |
) |
| 4135 |
assert isinstance(obj, email.NotifyEmail) |
| 4136 |
assert obj.verify_certificate is True |
| 4137 |
assert bool(obj.notify(body="body", title="title")) is True |
| 4138 |
|
| 4139 |
assert mock_smtpssl.call_count == 1 |
| 4140 |
context = mock_smtpssl.call_args.kwargs.get("context") |
| 4141 |
assert isinstance(context, ssl.SSLContext) |
| 4142 |
assert context.verify_mode == ssl.CERT_REQUIRED |
| 4143 |
assert context.check_hostname is True |
| 4144 |
|
| 4145 |
mock_smtp.reset_mock() |
| 4146 |
mock_smtpssl.reset_mock() |
| 4147 |
|
| 4148 |
# 2. SMTPS (mode=ssl) with verify=no should pass a context that does not |
| 4149 |
# validate the certificate. |
| 4150 |
obj = Apprise.instantiate( |
| 4151 |
"mailtos://user:[email protected]?mode=ssl&verify=no", |
| 4152 |
suppress_exceptions=False, |
| 4153 |
) |
| 4154 |
assert obj.verify_certificate is False |
| 4155 |
assert bool(obj.notify(body="body", title="title")) is True |
| 4156 |
|
| 4157 |
assert mock_smtpssl.call_count == 1 |
| 4158 |
context = mock_smtpssl.call_args.kwargs.get("context") |
| 4159 |
assert isinstance(context, ssl.SSLContext) |
| 4160 |
assert context.verify_mode == ssl.CERT_NONE |
| 4161 |
assert context.check_hostname is False |
| 4162 |
|
| 4163 |
mock_smtp.reset_mock() |
| 4164 |
mock_smtpssl.reset_mock() |
| 4165 |
|
| 4166 |
# 3. STARTTLS with the default verify_certificate=True should pass a |
| 4167 |
# validating context to starttls(). |
| 4168 |
obj = Apprise.instantiate( |
| 4169 |
"mailtos://user:[email protected]?mode=starttls", |
| 4170 |
suppress_exceptions=False, |
| 4171 |
) |
| 4172 |
assert obj.verify_certificate is True |
| 4173 |
assert bool(obj.notify(body="body", title="title")) is True |
| 4174 |
|
| 4175 |
assert response.starttls.call_count == 1 |
| 4176 |
context = response.starttls.call_args.kwargs.get("context") |
| 4177 |
assert isinstance(context, ssl.SSLContext) |
| 4178 |
assert context.verify_mode == ssl.CERT_REQUIRED |
| 4179 |
assert context.check_hostname is True |
| 4180 |
|
| 4181 |
response.starttls.reset_mock() |
| 4182 |
|
| 4183 |
# 4. STARTTLS with verify=no should pass a non-validating context. |
| 4184 |
obj = Apprise.instantiate( |
| 4185 |
"mailtos://user:[email protected]?mode=starttls&verify=no", |
| 4186 |
suppress_exceptions=False, |
| 4187 |
) |
| 4188 |
assert obj.verify_certificate is False |
| 4189 |
assert bool(obj.notify(body="body", title="title")) is True |
| 4190 |
|
| 4191 |
assert response.starttls.call_count == 1 |
| 4192 |
context = response.starttls.call_args.kwargs.get("context") |
| 4193 |
assert isinstance(context, ssl.SSLContext) |
| 4194 |
assert context.verify_mode == ssl.CERT_NONE |
| 4195 |
assert context.check_hostname is False |
| 4196 |
|
| 4197 |
|
| 4198 |
@mock.patch("smtplib.SMTP") |
| 4199 |
def test_plugin_email_starttls_certificate_failure_handling(mock_smtp): |
| 4200 |
"""A rejected STARTTLS certificate must be a normal send failure.""" |
| 4201 |
response = mock.Mock() |
| 4202 |
# An empty refusal map means SMTP accepted every recipient. |
| 4203 |
response.sendmail.return_value = {} |
| 4204 |
response.starttls.side_effect = ssl.SSLCertVerificationError( |
| 4205 |
"certificate verify failed" |
| 4206 |
) |
| 4207 |
# A real socket is no longer connected after the failed TLS handshake. |
| 4208 |
response.quit.side_effect = smtplib.SMTPServerDisconnected( |
| 4209 |
"Server not connected" |
| 4210 |
) |
| 4211 |
mock_smtp.return_value = response |
| 4212 |
|
| 4213 |
obj = Apprise.instantiate( |
| 4214 |
"mailtos://user:[email protected]?mode=starttls", |
| 4215 |
suppress_exceptions=False, |
| 4216 |
) |
| 4217 |
assert isinstance(obj, email.NotifyEmail) |
| 4218 |
|
| 4219 |
assert bool(obj.notify(body="body", title="title")) is False |
| 4220 |
response.login.assert_not_called() |
| 4221 |
response.sendmail.assert_not_called() |
| 4222 |
|
| 4223 |
|
| 4224 |
@mock.patch("smtplib.SMTP") |
| 4225 |
def test_plugin_email_inline_attachments(mock_smtplib): |
| 4226 |
"""Test ?inline=yes RFC 2387 inline attachment handling.""" |
| 4227 |
|
| 4228 |
# Capture raw email bodies sent via SMTP |
| 4229 |
sent_messages = [] |
| 4230 |
|
| 4231 |
class SMTPMock: |
| 4232 |
def __init__(self, *a, **kw): |
| 4233 |
pass |
| 4234 |
|
| 4235 |
def sendmail(self, from_addr, to_addrs, message): |
| 4236 |
sent_messages.append(message) |
| 4237 |
|
| 4238 |
def quit(self, *a, **kw): |
| 4239 |
pass |
| 4240 |
|
| 4241 |
def starttls(self, *a, **kw): |
| 4242 |
pass |
| 4243 |
|
| 4244 |
def login(self, *a, **kw): |
| 4245 |
pass |
| 4246 |
|
| 4247 |
mock_smtplib.return_value = SMTPMock() |
| 4248 |
|
| 4249 |
img = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 4250 |
img_name = "apprise-test.gif" |
| 4251 |
img2 = os.path.join(TEST_VAR_DIR, "apprise-test.png") |
| 4252 |
img2_name = "apprise-test.png" |
| 4253 |
|
| 4254 |
# inline=False (default): multipart/mixed, attachment |
| 4255 |
obj = Apprise.instantiate( |
| 4256 |
"mailto://user:[email protected]", suppress_exceptions=False |
| 4257 |
) |
| 4258 |
assert isinstance(obj, email.NotifyEmail) |
| 4259 |
assert obj.inline is False |
| 4260 |
|
| 4261 |
sent_messages.clear() |
| 4262 |
assert ( |
| 4263 |
bool( |
| 4264 |
obj.notify( |
| 4265 |
body="<p>hello</p>", |
| 4266 |
title="test", |
| 4267 |
notify_type=NotifyType.INFO, |
| 4268 |
attach=img, |
| 4269 |
) |
| 4270 |
) |
| 4271 |
is True |
| 4272 |
) |
| 4273 |
assert len(sent_messages) == 1 |
| 4274 |
msg = sent_messages[0] |
| 4275 |
|
| 4276 |
# Standard mode: mixed wrapper, attachment disposition, no Content-ID |
| 4277 |
assert "multipart/mixed" in msg |
| 4278 |
assert "Content-Disposition: attachment" in msg |
| 4279 |
assert "Content-ID" not in msg |
| 4280 |
|
| 4281 |
obj = Apprise.instantiate( |
| 4282 |
"mailto://user:[email protected]?inline=yes", suppress_exceptions=False |
| 4283 |
) |
| 4284 |
assert isinstance(obj, email.NotifyEmail) |
| 4285 |
assert obj.inline is True |
| 4286 |
|
| 4287 |
# inline=True, HTML, image with no pre-existing cid: ref: |
| 4288 |
# a <br/><img src="cid:name"> anchor is appended to the body and |
| 4289 |
# the attachment is embedded inline |
| 4290 |
sent_messages.clear() |
| 4291 |
assert ( |
| 4292 |
bool( |
| 4293 |
obj.notify( |
| 4294 |
body="<p>no cid ref here</p>", |
| 4295 |
title="test", |
| 4296 |
notify_type=NotifyType.INFO, |
| 4297 |
attach=img, |
| 4298 |
) |
| 4299 |
) |
| 4300 |
is True |
| 4301 |
) |
| 4302 |
assert len(sent_messages) == 1 |
| 4303 |
msg = sent_messages[0] |
| 4304 |
|
| 4305 |
# Unreferenced images receive an automatic inline anchor. |
| 4306 |
assert "multipart/related" in msg |
| 4307 |
assert "Content-Disposition: inline" in msg |
| 4308 |
assert "Content-ID:" in msg |
| 4309 |
assert img_name in msg |
| 4310 |
|
| 4311 |
# inline=True, HTML, image WITH pre-existing cid: ref: |
| 4312 |
# no duplicate anchor is added; attachment still inlined |
| 4313 |
html_with_ref = f'<img src="cid:{img_name}">' |
| 4314 |
|
| 4315 |
sent_messages.clear() |
| 4316 |
assert ( |
| 4317 |
bool( |
| 4318 |
obj.notify( |
| 4319 |
body=html_with_ref, |
| 4320 |
title="test", |
| 4321 |
notify_type=NotifyType.INFO, |
| 4322 |
attach=img, |
| 4323 |
) |
| 4324 |
) |
| 4325 |
is True |
| 4326 |
) |
| 4327 |
assert len(sent_messages) == 1 |
| 4328 |
msg = sent_messages[0] |
| 4329 |
|
| 4330 |
# Still related + inline; body should not have a second <br/><img> |
| 4331 |
assert "multipart/related" in msg |
| 4332 |
assert "Content-Disposition: inline" in msg |
| 4333 |
assert "Content-ID:" in msg |
| 4334 |
# Only one cid: anchor expected in the body part |
| 4335 |
assert msg.count(f"cid:{img_name}") == 1 |
| 4336 |
|
| 4337 |
# inline=True, HTML, two images: both inlined, body gets an |
| 4338 |
# anchor appended for the one not already referenced |
| 4339 |
html_one_ref = f'<img src="cid:{img_name}">' |
| 4340 |
|
| 4341 |
sent_messages.clear() |
| 4342 |
assert ( |
| 4343 |
bool( |
| 4344 |
obj.notify( |
| 4345 |
body=html_one_ref, |
| 4346 |
title="test", |
| 4347 |
notify_type=NotifyType.INFO, |
| 4348 |
attach=AppriseAttachment([img, img2]), |
| 4349 |
) |
| 4350 |
) |
| 4351 |
is True |
| 4352 |
) |
| 4353 |
assert len(sent_messages) == 1 |
| 4354 |
msg = sent_messages[0] |
| 4355 |
|
| 4356 |
# Both images are inline; second one had its anchor appended |
| 4357 |
assert "multipart/related" in msg |
| 4358 |
assert msg.count("Content-Disposition: inline") == 2 |
| 4359 |
assert msg.count("Content-ID:") == 2 |
| 4360 |
assert img_name in msg |
| 4361 |
assert img2_name in msg |
| 4362 |
|
| 4363 |
# inline=True, HTML, non-image attachment: stays as regular |
| 4364 |
# download regardless of inline flag |
| 4365 |
img_jpeg = os.path.join(TEST_VAR_DIR, "apprise-test.jpeg") |
| 4366 |
|
| 4367 |
from apprise.attachment.file import AttachFile |
| 4368 |
|
| 4369 |
pdf_attach = AttachFile(img_jpeg, mimetype="application/pdf") |
| 4370 |
aa = AppriseAttachment() |
| 4371 |
aa.add(pdf_attach) |
| 4372 |
|
| 4373 |
sent_messages.clear() |
| 4374 |
assert ( |
| 4375 |
bool( |
| 4376 |
obj.notify( |
| 4377 |
body="<p>body</p>", |
| 4378 |
title="test", |
| 4379 |
notify_type=NotifyType.INFO, |
| 4380 |
attach=aa, |
| 4381 |
) |
| 4382 |
) |
| 4383 |
is True |
| 4384 |
) |
| 4385 |
assert len(sent_messages) == 1 |
| 4386 |
msg = sent_messages[0] |
| 4387 |
|
| 4388 |
# No image -> no related wrapper, no inline, no Content-ID |
| 4389 |
assert "multipart/mixed" in msg |
| 4390 |
assert "Content-Disposition: attachment" in msg |
| 4391 |
assert "Content-ID" not in msg |
| 4392 |
|
| 4393 |
# inline=True, TEXT format: [Image: name] is appended to the |
| 4394 |
# text body; attachment stays as a regular download |
| 4395 |
obj_txt = Apprise.instantiate( |
| 4396 |
"mailto://user:[email protected]?inline=yes&format=text", |
| 4397 |
suppress_exceptions=False, |
| 4398 |
) |
| 4399 |
assert isinstance(obj_txt, email.NotifyEmail) |
| 4400 |
assert obj_txt.inline is True |
| 4401 |
|
| 4402 |
sent_messages.clear() |
| 4403 |
assert ( |
| 4404 |
bool( |
| 4405 |
obj_txt.notify( |
| 4406 |
body="plain text body", |
| 4407 |
title="test", |
| 4408 |
notify_type=NotifyType.INFO, |
| 4409 |
attach=img, |
| 4410 |
) |
| 4411 |
) |
| 4412 |
is True |
| 4413 |
) |
| 4414 |
assert len(sent_messages) == 1 |
| 4415 |
msg = sent_messages[0] |
| 4416 |
|
| 4417 |
# Text mode: mixed wrapper, attachment disposition, placeholder in body |
| 4418 |
assert "multipart/mixed" in msg |
| 4419 |
assert "Content-Disposition: attachment" in msg |
| 4420 |
assert "Content-ID" not in msg |
| 4421 |
assert f"[Image: {img_name}]" in msg |
| 4422 |
|
| 4423 |
# inline=True, TEXT format, non-image attachment |
| 4424 |
sent_messages.clear() |
| 4425 |
assert ( |
| 4426 |
bool( |
| 4427 |
obj_txt.notify( |
| 4428 |
body="plain text body", |
| 4429 |
title="test", |
| 4430 |
notify_type=NotifyType.INFO, |
| 4431 |
attach=aa, # application/pdf is not an image |
| 4432 |
) |
| 4433 |
) |
| 4434 |
is True |
| 4435 |
) |
| 4436 |
assert len(sent_messages) == 1 |
| 4437 |
msg = sent_messages[0] |
| 4438 |
|
| 4439 |
# Non-image in text inline mode: body unchanged, no [Image:] annotation |
| 4440 |
assert "Content-Disposition: attachment" in msg |
| 4441 |
assert "[Image:" not in msg |
| 4442 |
|
| 4443 |
# inline=True, HTML, image attachment with uppercase mimetype |
| 4444 |
upper_attach = AttachFile(img) |
| 4445 |
upper_attach.detected_mimetype = "Image/GIF" |
| 4446 |
aa_upper = AppriseAttachment() |
| 4447 |
aa_upper.add(upper_attach) |
| 4448 |
|
| 4449 |
sent_messages.clear() |
| 4450 |
assert ( |
| 4451 |
bool( |
| 4452 |
obj.notify( |
| 4453 |
body="<p>no cid</p>", |
| 4454 |
title="test", |
| 4455 |
notify_type=NotifyType.INFO, |
| 4456 |
attach=aa_upper, |
| 4457 |
) |
| 4458 |
) |
| 4459 |
is True |
| 4460 |
) |
| 4461 |
assert len(sent_messages) == 1 |
| 4462 |
msg = sent_messages[0] |
| 4463 |
|
| 4464 |
# Uppercase mimetype must still be treated as an image |
| 4465 |
assert "multipart/related" in msg |
| 4466 |
assert "Content-Disposition: inline" in msg |
| 4467 |
|
| 4468 |
# inline=True, TEXT format, uppercase mimetype: [Image: name] marker must |
| 4469 |
# also appear, proving the case-insensitive check works in the text path |
| 4470 |
sent_messages.clear() |
| 4471 |
assert ( |
| 4472 |
bool( |
| 4473 |
obj_txt.notify( |
| 4474 |
body="plain body", |
| 4475 |
title="test", |
| 4476 |
notify_type=NotifyType.INFO, |
| 4477 |
attach=aa_upper, |
| 4478 |
) |
| 4479 |
) |
| 4480 |
is True |
| 4481 |
) |
| 4482 |
assert len(sent_messages) == 1 |
| 4483 |
msg = sent_messages[0] |
| 4484 |
assert f"[Image: {img_name}]" in msg |
| 4485 |
|
| 4486 |
# inline=True, HTML, cid: ref in body with no matching attachment: |
| 4487 |
# warning is logged so the user can debug the mismatch |
| 4488 |
with mock.patch("apprise.plugins.email.base.logger") as mock_logger: |
| 4489 |
assert ( |
| 4490 |
bool( |
| 4491 |
obj.notify( |
| 4492 |
body='<img src="cid:missing-file.jpg">', |
| 4493 |
title="test", |
| 4494 |
notify_type=NotifyType.INFO, |
| 4495 |
attach=img, |
| 4496 |
) |
| 4497 |
) |
| 4498 |
is True |
| 4499 |
) |
| 4500 |
|
| 4501 |
# Warning fired for the unmatched ref |
| 4502 |
warning_texts = " ".join( |
| 4503 |
str(c) for c in mock_logger.warning.call_args_list |
| 4504 |
) |
| 4505 |
assert "missing-file.jpg" in warning_texts |
| 4506 |
|
| 4507 |
# An unmatched cid: with only a PDF keeps the mixed wrapper. |
| 4508 |
sent_messages.clear() |
| 4509 |
with mock.patch("apprise.plugins.email.base.logger"): |
| 4510 |
assert ( |
| 4511 |
bool( |
| 4512 |
obj.notify( |
| 4513 |
body='<p>body <img src="cid:ghost.jpg"></p>', |
| 4514 |
title="test", |
| 4515 |
notify_type=NotifyType.INFO, |
| 4516 |
# application/pdf is not an image or ghost.jpg |
| 4517 |
attach=aa, |
| 4518 |
) |
| 4519 |
) |
| 4520 |
is True |
| 4521 |
) |
| 4522 |
assert len(sent_messages) == 1 |
| 4523 |
msg = sent_messages[0] |
| 4524 |
|
| 4525 |
# No inline parts exist: wrapper must be mixed, not related |
| 4526 |
assert "multipart/mixed" in msg |
| 4527 |
assert "multipart/related" not in msg |
| 4528 |
assert "Content-ID" not in msg |
| 4529 |
|
| 4530 |
# A matching explicit cid: reference also inlines a PDF. |
| 4531 |
sent_messages.clear() |
| 4532 |
with mock.patch("apprise.plugins.email.base.logger") as mock_logger: |
| 4533 |
assert ( |
| 4534 |
bool( |
| 4535 |
obj.notify( |
| 4536 |
body=f'<p>body <embed src="cid:{pdf_attach.name}"></p>', |
| 4537 |
title="test", |
| 4538 |
notify_type=NotifyType.INFO, |
| 4539 |
# same PDF attachment as earlier tests |
| 4540 |
attach=aa, |
| 4541 |
) |
| 4542 |
) |
| 4543 |
is True |
| 4544 |
) |
| 4545 |
# The body ref matched by name, so no "no attachment matches" warning |
| 4546 |
for call in mock_logger.warning.call_args_list: |
| 4547 |
assert "apprise-test" not in str(call) |
| 4548 |
assert len(sent_messages) == 1 |
| 4549 |
msg = sent_messages[0] |
| 4550 |
|
| 4551 |
# Explicit cid: ref to PDF must be honored: related wrapper, inline |
| 4552 |
assert "multipart/related" in msg |
| 4553 |
assert "Content-Disposition: inline" in msg |
| 4554 |
assert "Content-ID" in msg |
| 4555 |
|
| 4556 |
# inline=True, HTML, cid: ref encoded with %20 for a file whose actual |
| 4557 |
# name contains a space: the decoded ref matches the attachment so no |
| 4558 |
# spurious warning is fired and the image is inlined exactly once |
| 4559 |
spaced_attach = AttachFile(img, name="my photo.gif") |
| 4560 |
aa_spaced = AppriseAttachment() |
| 4561 |
aa_spaced.add(spaced_attach) |
| 4562 |
|
| 4563 |
sent_messages.clear() |
| 4564 |
with mock.patch("apprise.plugins.email.base.logger") as mock_logger: |
| 4565 |
assert ( |
| 4566 |
bool( |
| 4567 |
obj.notify( |
| 4568 |
body='<img src="cid:my%20photo.gif">', |
| 4569 |
title="test", |
| 4570 |
notify_type=NotifyType.INFO, |
| 4571 |
attach=aa_spaced, |
| 4572 |
) |
| 4573 |
) |
| 4574 |
is True |
| 4575 |
) |
| 4576 |
|
| 4577 |
# Percent-decoding lets the cid: reference match the spaced filename. |
| 4578 |
for call in mock_logger.warning.call_args_list: |
| 4579 |
assert "my" not in str(call), ( |
| 4580 |
"Spurious warning fired for a cid: ref that should have " |
| 4581 |
"matched after %20 normalisation" |
| 4582 |
) |
| 4583 |
assert len(sent_messages) == 1 |
| 4584 |
msg = sent_messages[0] |
| 4585 |
|
| 4586 |
# Image is inlined exactly once (no duplicate anchor) |
| 4587 |
assert msg.count("cid:my%20photo.gif") == 1 |
| 4588 |
assert "Content-Disposition: inline" in msg |
| 4589 |
assert "Content-ID" in msg |
| 4590 |
|
| 4591 |
# inline=True, HTML, inaccessible attachment |
| 4592 |
bad_attach = AttachFile("/nonexistent/path/photo.jpg") |
| 4593 |
aa_bad = AppriseAttachment() |
| 4594 |
aa_bad.add(bad_attach) |
| 4595 |
|
| 4596 |
sent_messages.clear() |
| 4597 |
assert ( |
| 4598 |
bool( |
| 4599 |
obj.notify( |
| 4600 |
body="<p>body</p>", |
| 4601 |
title="test", |
| 4602 |
notify_type=NotifyType.INFO, |
| 4603 |
attach=aa_bad, |
| 4604 |
) |
| 4605 |
) |
| 4606 |
is False # bad attachment detected by the main loop |
| 4607 |
) |
| 4608 |
# An inaccessible attachment fails before SMTP delivery. |
| 4609 |
assert len(sent_messages) == 0 |
| 4610 |
|
| 4611 |
# inline=True, TEXT format, inaccessible attachment |
| 4612 |
sent_messages.clear() |
| 4613 |
assert ( |
| 4614 |
bool( |
| 4615 |
obj_txt.notify( |
| 4616 |
body="plain body", |
| 4617 |
title="test", |
| 4618 |
notify_type=NotifyType.INFO, |
| 4619 |
attach=aa_bad, |
| 4620 |
) |
| 4621 |
) |
| 4622 |
is False |
| 4623 |
) |
| 4624 |
assert len(sent_messages) == 0 |
| 4625 |
|
| 4626 |
# URL round-trip: inline=yes survives url() -> parse_url() |
| 4627 |
assert "inline=yes" in obj.url() |
| 4628 |
parsed = email.NotifyEmail.parse_url(obj.url()) |
| 4629 |
assert parsed is not None |
| 4630 |
assert parsed.get("inline") is True |
| 4631 |
|
| 4632 |
# inline=no parses as False |
| 4633 |
obj2 = Apprise.instantiate( |
| 4634 |
"mailto://user:[email protected]?inline=no", suppress_exceptions=False |
| 4635 |
) |
| 4636 |
assert isinstance(obj2, email.NotifyEmail) |
| 4637 |
assert obj2.inline is False |
| 4638 |
assert "inline" not in obj2.url() |