Debian Codegraph @

tests/test_plugin_mailersend.py

raw
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 pytest
35 import requests
36
37 from apprise import Apprise, AppriseAttachment, NotifyType
38 from apprise.exception import AppriseImproperlyConfigured
39 from apprise.plugins.mailersend import NotifyMailerSend
40
41 logging.disable(logging.CRITICAL)
42
43 # Attachment Directory
44 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
45
46 # A representative MailerSend API key
47 APIKEY = "mlsn.abc123def456abc123def456abc123def456"
48
49 # Our Testing URLs
50 apprise_url_tests = (
51 (
52 "mailersend://",
53 {
54 "instance": None,
55 },
56 ),
57 (
58 "mailersend://:@/",
59 {
60 "instance": None,
61 },
62 ),
63 (
64 # API key only, no from email
65 "mailersend://abcd",
66 {
67 "instance": None,
68 },
69 ),
70 (
71 # host only, no user (API key)
72 "mailersend://abcd@host",
73 {
74 "instance": None,
75 },
76 ),
77 (
78 # invalid API key (contains disallowed characters)
79 "mailersend://bad+key*!:[email protected]",
80 {
81 "instance": AppriseImproperlyConfigured,
82 },
83 ),
84 (
85 # No To/Target Address; use From address as default
86 "mailersend://abcd:[email protected]",
87 {
88 "instance": NotifyMailerSend,
89 },
90 ),
91 (
92 # Single explicit To address
93 "mailersend://abcd:[email protected]/[email protected]",
94 {
95 "instance": NotifyMailerSend,
96 "privacy_url": (
97 "mailersend://a...d:[email protected]/[email protected]"
98 ),
99 },
100 ),
101 (
102 # Multiple To addresses in the URL path
103 ("mailersend://abcd:[email protected]/[email protected]/[email protected]"),
104 {
105 "instance": NotifyMailerSend,
106 },
107 ),
108 (
109 # Extra To addresses via ?to=
110 (
111 "mailersend://abcd:[email protected]"
112 "/[email protected][email protected]"
113 ),
114 {
115 "instance": NotifyMailerSend,
116 },
117 ),
118 (
119 # Carbon Copy via ?cc=
120 (
121 "mailersend://abcd:[email protected]"
122 "/[email protected][email protected]"
123 ),
124 {
125 "instance": NotifyMailerSend,
126 },
127 ),
128 (
129 # Blind Carbon Copy via ?bcc=
130 (
131 "mailersend://abcd:[email protected]"
132 "/[email protected][email protected]"
133 ),
134 {
135 "instance": NotifyMailerSend,
136 },
137 ),
138 (
139 # Reply-To via ?reply=
140 ("mailersend://abcd:[email protected][email protected]"),
141 {
142 "instance": NotifyMailerSend,
143 "url_matches": r"reply=reply%40example.com",
144 },
145 ),
146 (
147 # Invalid Reply-To address
148 ("mailersend://abcd:[email protected]/[email protected]?reply=%20!"),
149 {
150 "instance": AppriseImproperlyConfigured,
151 },
152 ),
153 (
154 # Invalid To address is dropped but plugin still loads
155 "mailersend://abcd:[email protected]/bademailaddress",
156 {
157 "instance": NotifyMailerSend,
158 "notify_response": False,
159 },
160 ),
161 (
162 # Invalid CC and BCC (mixed with good ones)
163 (
164 "mailersend://abcd:[email protected]"
165 "/[email protected]"
166 "[email protected],!bad"
167 "&[email protected],!bad"
168 ),
169 {
170 "instance": NotifyMailerSend,
171 },
172 ),
173 (
174 # plain-text format
175 "mailersend://abcd:[email protected]?format=text",
176 {
177 "instance": NotifyMailerSend,
178 },
179 ),
180 (
181 # Force debug output
182 ("mailersend://abcd:[email protected]/[email protected]"),
183 {
184 "instance": NotifyMailerSend,
185 "force_debug": True,
186 },
187 ),
188 (
189 # HTTP 500 from server
190 "mailersend://abcd:[email protected]/[email protected]",
191 {
192 "instance": NotifyMailerSend,
193 "response": False,
194 "requests_response_code": (requests.codes.internal_server_error),
195 },
196 ),
197 (
198 # Unknown HTTP response code
199 "mailersend://abcd:[email protected]/[email protected]",
200 {
201 "instance": NotifyMailerSend,
202 "response": False,
203 "requests_response_code": 999,
204 },
205 ),
206 (
207 # RequestException
208 "mailersend://abcd:[email protected]/[email protected]",
209 {
210 "instance": NotifyMailerSend,
211 "test_requests_exceptions": True,
212 },
213 ),
214 )
215
216
217 def test_plugin_mailersend_urls():
218 """NotifyMailerSend() Apprise URLs."""
219
220 # Run our general tests
221 AppriseURLTester(tests=apprise_url_tests).run_all()
222
223
224 @mock.patch("requests.get")
225 @mock.patch("requests.post")
226 def test_plugin_mailersend_edge_cases(mock_post, mock_get):
227 """NotifyMailerSend() Edge Cases."""
228
229 # No API key
230 with pytest.raises(AppriseImproperlyConfigured):
231 NotifyMailerSend(apikey=None, from_email="[email protected]")
232
233 # Invalid API key
234 with pytest.raises(AppriseImproperlyConfigured):
235 NotifyMailerSend(apikey="bad+key*!", from_email="[email protected]")
236
237 # Invalid From email
238 with pytest.raises(AppriseImproperlyConfigured):
239 NotifyMailerSend(apikey="abcd", from_email="!invalid")
240
241 # No From email
242 with pytest.raises(AppriseImproperlyConfigured):
243 NotifyMailerSend(apikey="abcd", from_email=None)
244
245 # An invalid Reply-To address is rejected.
246 with pytest.raises(AppriseImproperlyConfigured):
247 NotifyMailerSend(
248 apikey="abcd",
249 from_email="[email protected]",
250 reply_to="!invalid",
251 )
252
253 # Invalid To email is dropped; object still created
254 obj = NotifyMailerSend(
255 apikey="abcd",
256 from_email="[email protected]",
257 targets="!invalid",
258 )
259 # When all To addresses are invalid the targets list is empty, so
260 # send() will return False
261 assert isinstance(obj, NotifyMailerSend)
262
263 # Mixed good and bad cc/bcc entries
264 obj = NotifyMailerSend(
265 apikey="abcd",
266 from_email="[email protected]",
267 bcc=("[email protected]", "!invalid"),
268 cc=("[email protected]", "!invalid"),
269 )
270 assert isinstance(obj, NotifyMailerSend)
271
272
273 @mock.patch("requests.get")
274 @mock.patch("requests.post")
275 def test_plugin_mailersend_send(mock_post, mock_get):
276 """NotifyMailerSend() send logic."""
277
278 def _mk_resp(code=requests.codes.accepted):
279 r = mock.Mock()
280 r.status_code = code
281 r.content = b""
282 return r
283
284 mock_post.return_value = _mk_resp()
285 mock_get.return_value = _mk_resp()
286
287 # Single target -- success
288 obj = Apprise.instantiate(
289 "mailersend://abcd:[email protected]/[email protected]"
290 )
291 assert isinstance(obj, NotifyMailerSend)
292 assert bool(obj.notify(body="body", title="title")) is True
293 assert mock_post.call_count == 1
294
295 mock_post.reset_mock()
296
297 # Multiple targets -- all succeed
298 obj = NotifyMailerSend(
299 apikey="abcd",
300 from_email="[email protected]",
301 targets=["[email protected]", "[email protected]"],
302 )
303 assert bool(obj.notify(body="body")) is True
304 assert mock_post.call_count == 2
305
306 mock_post.reset_mock()
307
308 # No targets -- send returns False without any HTTP call
309 obj = NotifyMailerSend(
310 apikey="abcd",
311 from_email="[email protected]",
312 targets=["!invalid"],
313 )
314 assert bool(obj.notify(body="body")) is False
315 assert mock_post.call_count == 0
316
317 mock_post.reset_mock()
318
319 # HTTP error on one of two targets -- partial failure
320 mock_post.side_effect = [
321 _mk_resp(requests.codes.accepted),
322 _mk_resp(requests.codes.internal_server_error),
323 ]
324 obj = NotifyMailerSend(
325 apikey="abcd",
326 from_email="[email protected]",
327 targets=["[email protected]", "[email protected]"],
328 )
329 assert bool(obj.notify(body="body")) is False
330
331 mock_post.reset_mock()
332 mock_post.side_effect = None
333
334 # RequestException -- marks failure
335 mock_post.side_effect = requests.RequestException("boom")
336 obj = Apprise.instantiate(
337 "mailersend://abcd:[email protected]/[email protected]"
338 )
339 assert bool(obj.notify(body="body")) is False
340
341 mock_post.side_effect = None
342
343 # CC and BCC are excluded from the To list de-duplication
344 mock_post.return_value = _mk_resp()
345 obj = NotifyMailerSend(
346 apikey="abcd",
347 from_email="[email protected]",
348 targets=["[email protected]"],
349 cc=["[email protected]", "[email protected]"],
350 bcc=["[email protected]", "[email protected]"],
351 reply_to="[email protected]",
352 )
353 assert bool(obj.notify(body="body")) is True
354 call_payload = mock_post.call_args
355 assert call_payload is not None
356
357 # text format triggers html conversion
358 mock_post.reset_mock()
359 mock_post.return_value = _mk_resp()
360 obj = Apprise.instantiate(
361 "mailersend://abcd:[email protected]/[email protected]?format=text"
362 )
363 assert bool(obj.notify(body="<p>body</p>")) is True
364
365 # unknown response code (not 202) is treated as failure
366 mock_post.reset_mock()
367 mock_post.return_value = _mk_resp(999)
368 obj = Apprise.instantiate(
369 "mailersend://abcd:[email protected]/[email protected]"
370 )
371 assert bool(obj.notify(body="body")) is False
372
373
374 @mock.patch("requests.post")
375 def test_plugin_mailersend_attachments(mock_post):
376 """NotifyMailerSend() Attachment handling."""
377
378 def _mk_resp(code=requests.codes.accepted):
379 r = mock.Mock()
380 r.status_code = code
381 r.content = b""
382 return r
383
384 mock_post.return_value = _mk_resp()
385
386 # Success -- valid attachment
387 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
388 attach = AppriseAttachment(path)
389 obj = Apprise.instantiate(
390 "mailersend://abcd:[email protected]/[email protected]"
391 )
392 assert isinstance(obj, NotifyMailerSend)
393 assert (
394 bool(
395 obj.notify(
396 body="body",
397 title="title",
398 notify_type=NotifyType.INFO,
399 attach=attach,
400 )
401 )
402 is True
403 )
404
405 mock_post.reset_mock()
406
407 # Inaccessible file -- os.path.isfile returns False
408 with mock.patch("os.path.isfile", return_value=False):
409 assert (
410 bool(
411 obj.notify(
412 body="body",
413 title="title",
414 notify_type=NotifyType.INFO,
415 attach=attach,
416 )
417 )
418 is False
419 )
420
421 mock_post.reset_mock()
422
423 # OSError on open
424 with mock.patch("builtins.open", side_effect=OSError):
425 assert (
426 bool(
427 obj.notify(
428 body="body",
429 title="title",
430 notify_type=NotifyType.INFO,
431 attach=attach,
432 )
433 )
434 is False
435 )
436
437 mock_post.reset_mock()
438
439 # HTTP error after successful open
440 mock_post.return_value = _mk_resp(requests.codes.internal_server_error)
441 assert (
442 bool(
443 obj.notify(
444 body="body",
445 title="title",
446 notify_type=NotifyType.INFO,
447 attach=attach,
448 )
449 )
450 is False
451 )
452
453 mock_post.reset_mock()
454
455 # RequestException after successful open
456 mock_post.side_effect = requests.RequestException("boom")
457 assert (
458 bool(
459 obj.notify(
460 body="body",
461 title="title",
462 notify_type=NotifyType.INFO,
463 attach=attach,
464 )
465 )
466 is False
467 )
468
469 mock_post.side_effect = None
470
471 # Multiple attachments -- all succeed
472 mock_post.return_value = _mk_resp()
473 paths = [
474 os.path.join(TEST_VAR_DIR, "apprise-test.gif"),
475 os.path.join(TEST_VAR_DIR, "apprise-test.png"),
476 ]
477 multi_attach = AppriseAttachment(paths)
478 assert (
479 bool(
480 obj.notify(
481 body="body",
482 title="title",
483 notify_type=NotifyType.INFO,
484 attach=multi_attach,
485 )
486 )
487 is True
488 )
489
490
491 @mock.patch("requests.get")
492 @mock.patch("requests.post")
493 def test_plugin_mailersend_url_parsing(mock_post, mock_get):
494 """NotifyMailerSend() URL parsing."""
495
496 # Empty URL
497 assert NotifyMailerSend.parse_url("") is None
498
499 # Missing API key (no user segment)
500 result = NotifyMailerSend.parse_url(
501 "mailersend://[email protected]/[email protected]"
502 )
503 assert result is None
504
505 # Valid round-trip
506 obj = NotifyMailerSend(
507 apikey="mlsn.abc123",
508 from_email="[email protected]",
509 targets=["[email protected]"],
510 cc=["[email protected]"],
511 bcc=["[email protected]"],
512 reply_to="[email protected]",
513 )
514 url = obj.url()
515 result = NotifyMailerSend.parse_url(url)
516 assert result is not None
517 obj2 = NotifyMailerSend(**result)
518 assert obj.url_identifier == obj2.url_identifier
519 assert len(obj.targets) == len(obj2.targets)
520
521 # url() with privacy=True masks the API key
522 priv = obj.url(privacy=True)
523 assert "mlsn.abc123" not in priv
524
525 # len() returns number of targets (at least 1)
526 obj_self = NotifyMailerSend(
527 apikey="abcd",
528 from_email="[email protected]",
529 )
530 assert len(obj_self) >= 1
531
532 # CC/BCC round-trip preserved
533 assert "cc=" in url
534 assert "bcc=" in url
535 assert "reply=" in url
536
537 # ?to= alias
538 result = NotifyMailerSend.parse_url(
539 "mailersend://abcd:[email protected][email protected]"
540 )
541 assert result is not None
542 assert "[email protected]" in result["targets"]
543
544
545 @mock.patch("requests.get")
546 @mock.patch("requests.post")
547 def test_plugin_mailersend_apprise_integration(mock_post, mock_get):
548 """NotifyMailerSend() Apprise integration test."""
549
550 def _mk_resp(code=requests.codes.accepted):
551 r = mock.Mock()
552 r.status_code = code
553 r.content = b""
554 return r
555
556 mock_post.return_value = _mk_resp()
557 mock_get.return_value = _mk_resp()
558
559 a = Apprise()
560 assert a.add("mailersend://abcd:[email protected]/[email protected]")
561 assert bool(a.notify(body="body", title="title")) is True