| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
# Disable logging for a cleaner testing output |
| 29 |
import logging |
| 30 |
import os |
| 31 |
from unittest import mock |
| 32 |
|
| 33 |
from helpers import AppriseURLTester |
| 34 |
import requests |
| 35 |
|
| 36 |
from apprise import Apprise, AppriseAttachment, NotifyType |
| 37 |
from apprise.exception import AppriseImproperlyConfigured |
| 38 |
from apprise.plugins.smtp2go import NotifySMTP2Go |
| 39 |
|
| 40 |
logging.disable(logging.CRITICAL) |
| 41 |
|
| 42 |
# Attachment Directory |
| 43 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 44 |
|
| 45 |
# Our Testing URLs |
| 46 |
apprise_url_tests = ( |
| 47 |
( |
| 48 |
"smtp2go://", |
| 49 |
{ |
| 50 |
"instance": AppriseImproperlyConfigured, |
| 51 |
}, |
| 52 |
), |
| 53 |
( |
| 54 |
"smtp2go://:@/", |
| 55 |
{ |
| 56 |
"instance": AppriseImproperlyConfigured, |
| 57 |
}, |
| 58 |
), |
| 59 |
# No Token specified |
| 60 |
( |
| 61 |
"smtp2go://[email protected]", |
| 62 |
{ |
| 63 |
"instance": AppriseImproperlyConfigured, |
| 64 |
}, |
| 65 |
), |
| 66 |
# Token is valid, but no user name specified |
| 67 |
( |
| 68 |
"smtp2go://localhost.localdomain/{}-{}-{}".format( |
| 69 |
"a" * 32, "b" * 8, "c" * 8 |
| 70 |
), |
| 71 |
{ |
| 72 |
"instance": AppriseImproperlyConfigured, |
| 73 |
}, |
| 74 |
), |
| 75 |
# Invalid from email address |
| 76 |
( |
| 77 |
'smtp2go://"@localhost.localdomain/{}-{}-{}'.format( |
| 78 |
"a" * 32, "b" * 8, "c" * 8 |
| 79 |
), |
| 80 |
{ |
| 81 |
"instance": AppriseImproperlyConfigured, |
| 82 |
}, |
| 83 |
), |
| 84 |
# No To email address, but everything else is valid |
| 85 |
( |
| 86 |
"smtp2go://[email protected]/{}-{}-{}".format( |
| 87 |
"a" * 32, "b" * 8, "c" * 8 |
| 88 |
), |
| 89 |
{ |
| 90 |
"instance": NotifySMTP2Go, |
| 91 |
}, |
| 92 |
), |
| 93 |
( |
| 94 |
"smtp2go://[email protected]/{}-{}-{}?format=markdown".format( |
| 95 |
"a" * 32, "b" * 8, "c" * 8 |
| 96 |
), |
| 97 |
{ |
| 98 |
"instance": NotifySMTP2Go, |
| 99 |
}, |
| 100 |
), |
| 101 |
( |
| 102 |
"smtp2go://[email protected]/{}-{}-{}?format=html".format( |
| 103 |
"a" * 32, "b" * 8, "c" * 8 |
| 104 |
), |
| 105 |
{ |
| 106 |
"instance": NotifySMTP2Go, |
| 107 |
}, |
| 108 |
), |
| 109 |
( |
| 110 |
"smtp2go://[email protected]/{}-{}-{}?format=text".format( |
| 111 |
"a" * 32, "b" * 8, "c" * 8 |
| 112 |
), |
| 113 |
{ |
| 114 |
"instance": NotifySMTP2Go, |
| 115 |
}, |
| 116 |
), |
| 117 |
# headers |
| 118 |
( |
| 119 |
"smtp2go://[email protected]/{}-{}-{}" |
| 120 |
"?+X-Customer-Campaign-ID=Apprise".format("a" * 32, "b" * 8, "c" * 8), |
| 121 |
{ |
| 122 |
"instance": NotifySMTP2Go, |
| 123 |
"force_debug": True, |
| 124 |
}, |
| 125 |
), |
| 126 |
# bcc and cc |
| 127 |
( |
| 128 |
"smtp2go://[email protected]/{}-{}-{}" |
| 129 |
"[email protected]&[email protected]".format( |
| 130 |
"a" * 32, "b" * 8, "c" * 8 |
| 131 |
), |
| 132 |
{ |
| 133 |
"instance": NotifySMTP2Go, |
| 134 |
}, |
| 135 |
), |
| 136 |
# One To Email address |
| 137 |
( |
| 138 |
"smtp2go://[email protected]/{}-{}-{}/[email protected]".format( |
| 139 |
"a" * 32, "b" * 8, "c" * 8 |
| 140 |
), |
| 141 |
{ |
| 142 |
"instance": NotifySMTP2Go, |
| 143 |
}, |
| 144 |
), |
| 145 |
( |
| 146 |
"smtp2go://[email protected]/" |
| 147 |
"{}-{}-{}[email protected]".format("a" * 32, "b" * 8, "c" * 8), |
| 148 |
{"instance": NotifySMTP2Go}, |
| 149 |
), |
| 150 |
# One To Email address, a from name specified too |
| 151 |
( |
| 152 |
"smtp2go://[email protected]/{}-{}-{}/" |
| 153 |
'[email protected]?name="Frodo"'.format("a" * 32, "b" * 8, "c" * 8), |
| 154 |
{"instance": NotifySMTP2Go}, |
| 155 |
), |
| 156 |
# Invalid 'To' Email address |
| 157 |
( |
| 158 |
"smtp2go://[email protected]/{}-{}-{}/invalid".format( |
| 159 |
"a" * 32, "b" * 8, "c" * 8 |
| 160 |
), |
| 161 |
{ |
| 162 |
"instance": NotifySMTP2Go, |
| 163 |
# Expected notify() response |
| 164 |
"notify_response": False, |
| 165 |
}, |
| 166 |
), |
| 167 |
# Multiple 'To', 'Cc', and 'Bcc' addresses (with invalid ones) |
| 168 |
( |
| 169 |
"smtp2go://[email protected]/{}-{}-{}/{}?bcc={}&cc={}".format( |
| 170 |
"a" * 32, |
| 171 |
"b" * 8, |
| 172 |
"c" * 8, |
| 173 |
"/".join( |
| 174 |
("[email protected]", "invalid", "User2:[email protected]") |
| 175 |
), |
| 176 |
",".join(("[email protected]", "i@v", "User1:[email protected]")), |
| 177 |
",".join(("[email protected]", "g@r@b", "Da:[email protected]")), |
| 178 |
), |
| 179 |
{ |
| 180 |
"instance": NotifySMTP2Go, |
| 181 |
}, |
| 182 |
), |
| 183 |
( |
| 184 |
"smtp2go://[email protected]/{}-{}-{}".format( |
| 185 |
"a" * 32, "b" * 8, "c" * 8 |
| 186 |
), |
| 187 |
{ |
| 188 |
"instance": NotifySMTP2Go, |
| 189 |
# force a failure |
| 190 |
"response": False, |
| 191 |
"requests_response_code": requests.codes.internal_server_error, |
| 192 |
}, |
| 193 |
), |
| 194 |
( |
| 195 |
"smtp2go://[email protected]/{}-{}-{}".format( |
| 196 |
"a" * 32, "b" * 8, "c" * 8 |
| 197 |
), |
| 198 |
{ |
| 199 |
"instance": NotifySMTP2Go, |
| 200 |
# throw a bizarre code forcing us to fail to look it up |
| 201 |
"response": False, |
| 202 |
"requests_response_code": 999, |
| 203 |
}, |
| 204 |
), |
| 205 |
( |
| 206 |
"smtp2go://[email protected]/{}-{}-{}".format( |
| 207 |
"a" * 32, "b" * 8, "c" * 8 |
| 208 |
), |
| 209 |
{ |
| 210 |
"instance": NotifySMTP2Go, |
| 211 |
# Throws a series of i/o exceptions with this flag |
| 212 |
# is set and tests that we gracefully handle them |
| 213 |
"test_requests_exceptions": True, |
| 214 |
}, |
| 215 |
), |
| 216 |
) |
| 217 |
|
| 218 |
|
| 219 |
def test_plugin_smtp2go_urls(): |
| 220 |
"""NotifySMTP2Go() Apprise URLs.""" |
| 221 |
|
| 222 |
# Run our general tests |
| 223 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 224 |
|
| 225 |
|
| 226 |
@mock.patch("requests.post") |
| 227 |
def test_plugin_smtp2go_attachments(mock_post): |
| 228 |
"""NotifySMTP2Go() Attachments.""" |
| 229 |
|
| 230 |
okay_response = requests.Request() |
| 231 |
okay_response.status_code = requests.codes.ok |
| 232 |
okay_response.content = "" |
| 233 |
|
| 234 |
# Assign our mock object our return value |
| 235 |
mock_post.return_value = okay_response |
| 236 |
|
| 237 |
# API Key |
| 238 |
apikey = "abc123" |
| 239 |
|
| 240 |
obj = Apprise.instantiate(f"smtp2go://[email protected]/{apikey}") |
| 241 |
assert isinstance(obj, NotifySMTP2Go) |
| 242 |
|
| 243 |
# Test Valid Attachment |
| 244 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 245 |
attach = AppriseAttachment(path) |
| 246 |
assert ( |
| 247 |
bool( |
| 248 |
obj.notify( |
| 249 |
body="body", |
| 250 |
title="title", |
| 251 |
notify_type=NotifyType.INFO, |
| 252 |
attach=attach, |
| 253 |
) |
| 254 |
) |
| 255 |
is True |
| 256 |
) |
| 257 |
|
| 258 |
# Test invalid attachment |
| 259 |
path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 260 |
assert ( |
| 261 |
bool( |
| 262 |
obj.notify( |
| 263 |
body="body", |
| 264 |
title="title", |
| 265 |
notify_type=NotifyType.INFO, |
| 266 |
attach=path, |
| 267 |
) |
| 268 |
) |
| 269 |
is False |
| 270 |
) |
| 271 |
|
| 272 |
mock_post.return_value = None |
| 273 |
mock_post.side_effect = OSError() |
| 274 |
# We can't send the message if we can't read the attachment |
| 275 |
assert ( |
| 276 |
bool( |
| 277 |
obj.notify( |
| 278 |
body="body", |
| 279 |
title="title", |
| 280 |
notify_type=NotifyType.INFO, |
| 281 |
attach=attach, |
| 282 |
) |
| 283 |
) |
| 284 |
is False |
| 285 |
) |
| 286 |
|
| 287 |
# Test Valid Attachment (load 3) |
| 288 |
path = ( |
| 289 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 290 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 291 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 292 |
) |
| 293 |
attach = AppriseAttachment(path) |
| 294 |
|
| 295 |
# Return our good configuration |
| 296 |
mock_post.side_effect = None |
| 297 |
mock_post.return_value = okay_response |
| 298 |
with mock.patch("builtins.open", side_effect=OSError()): |
| 299 |
# We can't send the message we can't open the attachment for reading |
| 300 |
assert ( |
| 301 |
bool( |
| 302 |
obj.notify( |
| 303 |
body="body", |
| 304 |
title="title", |
| 305 |
notify_type=NotifyType.INFO, |
| 306 |
attach=attach, |
| 307 |
) |
| 308 |
) |
| 309 |
is False |
| 310 |
) |
| 311 |
|
| 312 |
# test the handling of our batch modes |
| 313 |
obj = Apprise.instantiate( |
| 314 |
f"smtp2go://[email protected]/{apikey}/" |
| 315 |
"[email protected]/[email protected]?batch=yes" |
| 316 |
) |
| 317 |
assert isinstance(obj, NotifySMTP2Go) |
| 318 |
|
| 319 |
# objects will be combined into a single post in batch mode |
| 320 |
assert len(obj) == 1 |
| 321 |
|
| 322 |
# Force our batch to break into separate messages |
| 323 |
obj.default_batch_size = 1 |
| 324 |
|
| 325 |
# We'll send 2 messages now |
| 326 |
assert len(obj) == 2 |
| 327 |
|
| 328 |
mock_post.reset_mock() |
| 329 |
|
| 330 |
assert ( |
| 331 |
bool( |
| 332 |
obj.notify( |
| 333 |
body="body", |
| 334 |
title="title", |
| 335 |
notify_type=NotifyType.INFO, |
| 336 |
attach=attach, |
| 337 |
) |
| 338 |
) |
| 339 |
is True |
| 340 |
) |
| 341 |
assert mock_post.call_count == 2 |
| 342 |
|
| 343 |
# single batch |
| 344 |
mock_post.reset_mock() |
| 345 |
# We'll send 1 message |
| 346 |
obj.default_batch_size = 2 |
| 347 |
|
| 348 |
assert ( |
| 349 |
bool( |
| 350 |
obj.notify( |
| 351 |
body="body", |
| 352 |
title="title", |
| 353 |
notify_type=NotifyType.INFO, |
| 354 |
attach=attach, |
| 355 |
) |
| 356 |
) |
| 357 |
is True |
| 358 |
) |
| 359 |
assert mock_post.call_count == 1 |
| 360 |
|
| 361 |
|
| 362 |
def test_plugin_smtp2go_cc_bcc_invalid_branch(): |
| 363 |
"""NotifySMTP2Go() CC/BCC validation: invalid entries are dropped.""" |
| 364 |
|
| 365 |
# Build a NotifySMTP2Go with: |
| 366 |
# cc = one valid address + one that parse_emails passes through but |
| 367 |
# is_email rejects (hits the FALSE branch in the CC loop). |
| 368 |
# A list must be used -- parse_emails filters invalid tokens out |
| 369 |
# of comma-separated strings before is_email is ever called. |
| 370 |
# bcc = same, for the FALSE branch in the BCC loop |
| 371 |
obj = NotifySMTP2Go( |
| 372 |
apikey="abc123", |
| 373 |
targets=["[email protected]"], |
| 374 |
cc=["[email protected]", "notanemail"], |
| 375 |
bcc=["[email protected]", "alsonotanemail"], |
| 376 |
user="user", |
| 377 |
host="example.com", |
| 378 |
) |
| 379 |
|
| 380 |
# Valid CC entry was added; invalid one was silently dropped |
| 381 |
assert "[email protected]" in obj.cc |
| 382 |
assert len(obj.cc) == 1 |
| 383 |
|
| 384 |
# Valid BCC entry was added; invalid one was silently dropped |
| 385 |
assert "[email protected]" in obj.bcc |
| 386 |
assert len(obj.bcc) == 1 |
| 387 |
|
| 388 |
|
| 389 |
def test_plugin_smtp2go_cc_names(): |
| 390 |
"""NotifySMTP2Go() url() keeps the display names of CC addresses.""" |
| 391 |
obj = Apprise.instantiate( |
| 392 |
"smtp2go://[email protected]/apikey/[email protected]" |
| 393 |
"?cc=Chris<[email protected]>,[email protected]" |
| 394 |
) |
| 395 |
assert isinstance(obj, NotifySMTP2Go) |
| 396 |
assert obj.names["[email protected]"] == "Chris" |
| 397 |
assert obj.names["[email protected]"] is False |
| 398 |
|
| 399 |
url = obj.url() |
| 400 |
assert "Chris%3Al2g%40nuxref.com" in url |
| 401 |
|
| 402 |
# The names survive a round trip through url() |
| 403 |
obj2 = Apprise.instantiate(url) |
| 404 |
assert isinstance(obj2, NotifySMTP2Go) |
| 405 |
assert obj2.cc == obj.cc |
| 406 |
assert obj2.names["[email protected]"] == "Chris" |
| 407 |
assert obj2.names["[email protected]"] is False |