| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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.plugins.smtp2go import NotifySMTP2Go |
| 38 |
|
| 39 |
logging.disable(logging.CRITICAL) |
| 40 |
|
| 41 |
|
| 42 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 43 |
|
| 44 |
|
| 45 |
apprise_url_tests = ( |
| 46 |
( |
| 47 |
"smtp2go://", |
| 48 |
{ |
| 49 |
"instance": TypeError, |
| 50 |
}, |
| 51 |
), |
| 52 |
( |
| 53 |
"smtp2go://:@/", |
| 54 |
{ |
| 55 |
"instance": TypeError, |
| 56 |
}, |
| 57 |
), |
| 58 |
|
| 59 |
( |
| 60 |
"smtp2go://[email protected]", |
| 61 |
{ |
| 62 |
"instance": TypeError, |
| 63 |
}, |
| 64 |
), |
| 65 |
|
| 66 |
( |
| 67 |
"smtp2go://localhost.localdomain/{}-{}-{}".format( |
| 68 |
"a" * 32, "b" * 8, "c" * 8 |
| 69 |
), |
| 70 |
{ |
| 71 |
"instance": TypeError, |
| 72 |
}, |
| 73 |
), |
| 74 |
|
| 75 |
( |
| 76 |
'smtp2go://"@localhost.localdomain/{}-{}-{}'.format( |
| 77 |
"a" * 32, "b" * 8, "c" * 8 |
| 78 |
), |
| 79 |
{ |
| 80 |
"instance": TypeError, |
| 81 |
}, |
| 82 |
), |
| 83 |
|
| 84 |
( |
| 85 |
"smtp2go://[email protected]/{}-{}-{}".format( |
| 86 |
"a" * 32, "b" * 8, "c" * 8 |
| 87 |
), |
| 88 |
{ |
| 89 |
"instance": NotifySMTP2Go, |
| 90 |
}, |
| 91 |
), |
| 92 |
( |
| 93 |
"smtp2go://[email protected]/{}-{}-{}?format=markdown".format( |
| 94 |
"a" * 32, "b" * 8, "c" * 8 |
| 95 |
), |
| 96 |
{ |
| 97 |
"instance": NotifySMTP2Go, |
| 98 |
}, |
| 99 |
), |
| 100 |
( |
| 101 |
"smtp2go://[email protected]/{}-{}-{}?format=html".format( |
| 102 |
"a" * 32, "b" * 8, "c" * 8 |
| 103 |
), |
| 104 |
{ |
| 105 |
"instance": NotifySMTP2Go, |
| 106 |
}, |
| 107 |
), |
| 108 |
( |
| 109 |
"smtp2go://[email protected]/{}-{}-{}?format=text".format( |
| 110 |
"a" * 32, "b" * 8, "c" * 8 |
| 111 |
), |
| 112 |
{ |
| 113 |
"instance": NotifySMTP2Go, |
| 114 |
}, |
| 115 |
), |
| 116 |
|
| 117 |
( |
| 118 |
"smtp2go://[email protected]/{}-{}-{}" |
| 119 |
"?+X-Customer-Campaign-ID=Apprise".format("a" * 32, "b" * 8, "c" * 8), |
| 120 |
{ |
| 121 |
"instance": NotifySMTP2Go, |
| 122 |
"force_debug": True, |
| 123 |
}, |
| 124 |
), |
| 125 |
|
| 126 |
( |
| 127 |
"smtp2go://[email protected]/{}-{}-{}" |
| 128 |
"[email protected]&[email protected]".format( |
| 129 |
"a" * 32, "b" * 8, "c" * 8 |
| 130 |
), |
| 131 |
{ |
| 132 |
"instance": NotifySMTP2Go, |
| 133 |
}, |
| 134 |
), |
| 135 |
|
| 136 |
( |
| 137 |
"smtp2go://[email protected]/{}-{}-{}/[email protected]".format( |
| 138 |
"a" * 32, "b" * 8, "c" * 8 |
| 139 |
), |
| 140 |
{ |
| 141 |
"instance": NotifySMTP2Go, |
| 142 |
}, |
| 143 |
), |
| 144 |
( |
| 145 |
"smtp2go://[email protected]/" |
| 146 |
"{}-{}-{}[email protected]".format("a" * 32, "b" * 8, "c" * 8), |
| 147 |
{"instance": NotifySMTP2Go}, |
| 148 |
), |
| 149 |
|
| 150 |
( |
| 151 |
"smtp2go://[email protected]/{}-{}-{}/" |
| 152 |
'[email protected]?name="Frodo"'.format("a" * 32, "b" * 8, "c" * 8), |
| 153 |
{"instance": NotifySMTP2Go}, |
| 154 |
), |
| 155 |
|
| 156 |
( |
| 157 |
"smtp2go://[email protected]/{}-{}-{}/invalid".format( |
| 158 |
"a" * 32, "b" * 8, "c" * 8 |
| 159 |
), |
| 160 |
{ |
| 161 |
"instance": NotifySMTP2Go, |
| 162 |
|
| 163 |
"notify_response": False, |
| 164 |
}, |
| 165 |
), |
| 166 |
|
| 167 |
( |
| 168 |
"smtp2go://[email protected]/{}-{}-{}/{}?bcc={}&cc={}".format( |
| 169 |
"a" * 32, |
| 170 |
"b" * 8, |
| 171 |
"c" * 8, |
| 172 |
"/".join( |
| 173 |
("[email protected]", "invalid", "User2:[email protected]") |
| 174 |
), |
| 175 |
",".join(("[email protected]", "i@v", "User1:[email protected]")), |
| 176 |
",".join(("[email protected]", "g@r@b", "Da:[email protected]")), |
| 177 |
), |
| 178 |
{ |
| 179 |
"instance": NotifySMTP2Go, |
| 180 |
}, |
| 181 |
), |
| 182 |
( |
| 183 |
"smtp2go://[email protected]/{}-{}-{}".format( |
| 184 |
"a" * 32, "b" * 8, "c" * 8 |
| 185 |
), |
| 186 |
{ |
| 187 |
"instance": NotifySMTP2Go, |
| 188 |
|
| 189 |
"response": False, |
| 190 |
"requests_response_code": requests.codes.internal_server_error, |
| 191 |
}, |
| 192 |
), |
| 193 |
( |
| 194 |
"smtp2go://[email protected]/{}-{}-{}".format( |
| 195 |
"a" * 32, "b" * 8, "c" * 8 |
| 196 |
), |
| 197 |
{ |
| 198 |
"instance": NotifySMTP2Go, |
| 199 |
|
| 200 |
"response": False, |
| 201 |
"requests_response_code": 999, |
| 202 |
}, |
| 203 |
), |
| 204 |
( |
| 205 |
"smtp2go://[email protected]/{}-{}-{}".format( |
| 206 |
"a" * 32, "b" * 8, "c" * 8 |
| 207 |
), |
| 208 |
{ |
| 209 |
"instance": NotifySMTP2Go, |
| 210 |
|
| 211 |
|
| 212 |
"test_requests_exceptions": True, |
| 213 |
}, |
| 214 |
), |
| 215 |
) |
| 216 |
|
| 217 |
|
| 218 |
def test_plugin_smtp2go_urls(): |
| 219 |
"""NotifySMTP2Go() Apprise URLs.""" |
| 220 |
|
| 221 |
|
| 222 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 223 |
|
| 224 |
|
| 225 |
@mock.patch("requests.post") |
| 226 |
def test_plugin_smtp2go_attachments(mock_post): |
| 227 |
"""NotifySMTP2Go() Attachments.""" |
| 228 |
|
| 229 |
okay_response = requests.Request() |
| 230 |
okay_response.status_code = requests.codes.ok |
| 231 |
okay_response.content = "" |
| 232 |
|
| 233 |
|
| 234 |
mock_post.return_value = okay_response |
| 235 |
|
| 236 |
|
| 237 |
apikey = "abc123" |
| 238 |
|
| 239 |
obj = Apprise.instantiate(f"smtp2go://[email protected]/{apikey}") |
| 240 |
assert isinstance(obj, NotifySMTP2Go) |
| 241 |
|
| 242 |
|
| 243 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 244 |
attach = AppriseAttachment(path) |
| 245 |
assert ( |
| 246 |
obj.notify( |
| 247 |
body="body", |
| 248 |
title="title", |
| 249 |
notify_type=NotifyType.INFO, |
| 250 |
attach=attach, |
| 251 |
) |
| 252 |
is True |
| 253 |
) |
| 254 |
|
| 255 |
|
| 256 |
path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 257 |
assert ( |
| 258 |
obj.notify( |
| 259 |
body="body", |
| 260 |
title="title", |
| 261 |
notify_type=NotifyType.INFO, |
| 262 |
attach=path, |
| 263 |
) |
| 264 |
is False |
| 265 |
) |
| 266 |
|
| 267 |
mock_post.return_value = None |
| 268 |
mock_post.side_effect = OSError() |
| 269 |
|
| 270 |
assert ( |
| 271 |
obj.notify( |
| 272 |
body="body", |
| 273 |
title="title", |
| 274 |
notify_type=NotifyType.INFO, |
| 275 |
attach=attach, |
| 276 |
) |
| 277 |
is False |
| 278 |
) |
| 279 |
|
| 280 |
|
| 281 |
path = ( |
| 282 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 283 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 284 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 285 |
) |
| 286 |
attach = AppriseAttachment(path) |
| 287 |
|
| 288 |
|
| 289 |
mock_post.side_effect = None |
| 290 |
mock_post.return_value = okay_response |
| 291 |
with mock.patch("builtins.open", side_effect=OSError()): |
| 292 |
|
| 293 |
assert ( |
| 294 |
obj.notify( |
| 295 |
body="body", |
| 296 |
title="title", |
| 297 |
notify_type=NotifyType.INFO, |
| 298 |
attach=attach, |
| 299 |
) |
| 300 |
is False |
| 301 |
) |
| 302 |
|
| 303 |
|
| 304 |
obj = Apprise.instantiate( |
| 305 |
f"smtp2go://[email protected]/{apikey}/" |
| 306 |
"[email protected]/[email protected]?batch=yes" |
| 307 |
) |
| 308 |
assert isinstance(obj, NotifySMTP2Go) |
| 309 |
|
| 310 |
|
| 311 |
assert len(obj) == 1 |
| 312 |
|
| 313 |
|
| 314 |
obj.default_batch_size = 1 |
| 315 |
|
| 316 |
|
| 317 |
assert len(obj) == 2 |
| 318 |
|
| 319 |
mock_post.reset_mock() |
| 320 |
|
| 321 |
assert ( |
| 322 |
obj.notify( |
| 323 |
body="body", |
| 324 |
title="title", |
| 325 |
notify_type=NotifyType.INFO, |
| 326 |
attach=attach, |
| 327 |
) |
| 328 |
is True |
| 329 |
) |
| 330 |
assert mock_post.call_count == 2 |
| 331 |
|
| 332 |
|
| 333 |
mock_post.reset_mock() |
| 334 |
|
| 335 |
obj.default_batch_size = 2 |
| 336 |
|
| 337 |
assert ( |
| 338 |
obj.notify( |
| 339 |
body="body", |
| 340 |
title="title", |
| 341 |
notify_type=NotifyType.INFO, |
| 342 |
attach=attach, |
| 343 |
) |
| 344 |
is True |
| 345 |
) |
| 346 |
assert mock_post.call_count == 1 |
| 347 |
|
| 348 |
|
| 349 |
def test_plugin_smtp2go_cc_bcc_invalid_branch(): |
| 350 |
"""NotifySMTP2Go() CC/BCC validation: invalid entries are dropped.""" |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
obj = NotifySMTP2Go( |
| 359 |
apikey="abc123", |
| 360 |
targets=["[email protected]"], |
| 361 |
cc=["[email protected]", "notanemail"], |
| 362 |
bcc=["[email protected]", "alsonotanemail"], |
| 363 |
user="user", |
| 364 |
host="example.com", |
| 365 |
) |
| 366 |
|
| 367 |
|
| 368 |
assert "[email protected]" in obj.cc |
| 369 |
assert len(obj.cc) == 1 |
| 370 |
|
| 371 |
|
| 372 |
assert "[email protected]" in obj.bcc |
| 373 |
assert len(obj.bcc) == 1 |