Debian Codegraph @

tests/test_plugin_mailersend.py

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