Debian Codegraph @

tests/test_plugin_sendpulse.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 from json import dumps, loads
29
30 # Disable logging for a cleaner testing output
31 import logging
32 import os
33 from unittest import mock
34
35 from helpers import AppriseURLTester
36 import pytest
37 import requests
38
39 from apprise import Apprise, AppriseAttachment, NotifyType
40 from apprise.plugins.sendpulse import NotifySendPulse
41
42 logging.disable(logging.CRITICAL)
43
44 SENDPULSE_GOOD_RESPONSE = dumps(
45 {
46 "access_token": "abc123",
47 "expires_in": 3600,
48 }
49 )
50
51 SENDPULSE_BAD_RESPONSE = "{"
52
53 # Attachment Directory
54 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
55
56 # Our Testing URLs
57 apprise_url_tests = (
58 (
59 "sendpulse://",
60 {
61 "instance": TypeError,
62 },
63 ),
64 (
65 "sendpulse://:@/",
66 {
67 "instance": TypeError,
68 },
69 ),
70 (
71 "sendpulse://abcd",
72 {
73 # invalid from email
74 "instance": TypeError,
75 },
76 ),
77 (
78 "sendpulse://[email protected]",
79 {
80 # Just an Email specified, no client_id or client_secret
81 "instance": TypeError,
82 },
83 ),
84 (
85 "sendpulse://[email protected]/client_id/cs/?template=invalid",
86 {
87 # Invalid template
88 "instance": TypeError,
89 },
90 ),
91 (
92 "sendpulse://[email protected]/client_id/cs1/?template=123",
93 {
94 "instance": NotifySendPulse,
95 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
96 },
97 ),
98 (
99 "sendpulse://[email protected]/client_id/cs1/",
100 {
101 "instance": NotifySendPulse,
102 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
103 },
104 ),
105 (
106 "sendpulse://[email protected]/client_id/cs1/?format=text",
107 {
108 "instance": NotifySendPulse,
109 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
110 },
111 ),
112 (
113 "sendpulse://[email protected]/client_id/cs1/?format=html",
114 {
115 "instance": NotifySendPulse,
116 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
117 },
118 ),
119 (
120 "sendpulse://[email protected]/client_id/cs1/?from=Chris",
121 {
122 # Set name only
123 "instance": NotifySendPulse,
124 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
125 "force_debug": True,
126 },
127 ),
128 (
129 "sendpulse://?id=ci&secret=cs&[email protected]",
130 {
131 # Set login through user= only
132 "instance": NotifySendPulse,
133 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
134 },
135 ),
136 (
137 "sendpulse://?id=ci&secret=cs&user=Chris<[email protected]>",
138 {
139 # Set login through user= only
140 "instance": NotifySendPulse,
141 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
142 },
143 ),
144 (
145 "sendpulse://?id=ci&secret=cs&user=chris",
146 {
147 # Set login through user= only - invaild email
148 "instance": TypeError,
149 },
150 ),
151 (
152 "sendpulse://example.com/client_id/cs1/?user=chris",
153 {
154 # Set user as a name only
155 "instance": NotifySendPulse,
156 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
157 },
158 ),
159 (
160 "sendpulse://client_id/cs1/[email protected]",
161 {
162 # Set user as email
163 "instance": NotifySendPulse,
164 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
165 },
166 ),
167 (
168 "sendpulse://client_id/cs1/?from=Chris<[email protected]>",
169 {
170 # set full email with name
171 "instance": NotifySendPulse,
172 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
173 },
174 ),
175 (
176 "sendpulse://?from=Chris<[email protected]>&id=ci&secret=cs",
177 {
178 # leverage all get params from URL
179 "instance": NotifySendPulse,
180 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
181 },
182 ),
183 (
184 "sendpulse://[email protected]/client_id/cs1a/",
185 {
186 "instance": NotifySendPulse,
187 "requests_response_text": SENDPULSE_BAD_RESPONSE,
188 # Notify will fail because auth failed
189 "response": False,
190 },
191 ),
192 (
193 "sendpulse://[email protected]/client_id/cs2/[email protected]",
194 {
195 # A good email with Blind Carbon Copy
196 "instance": NotifySendPulse,
197 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
198 },
199 ),
200 (
201 "sendpulse://[email protected]/client_id/cs2/?bcc=invalid",
202 {
203 # A good email with Blind Carbon Copy
204 "instance": NotifySendPulse,
205 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
206 },
207 ),
208 (
209 "sendpulse://[email protected]/client_id/cs3/[email protected]",
210 {
211 # A good email with Carbon Copy
212 "instance": NotifySendPulse,
213 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
214 },
215 ),
216 (
217 "sendpulse://[email protected]/client_id/cs4/?cc=Chris<[email protected]>",
218 {
219 # A good email with Carbon Copy
220 "instance": NotifySendPulse,
221 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
222 },
223 ),
224 (
225 "sendpulse://[email protected]/client_id/cs5/?cc=invalid",
226 {
227 # A good email with Carbon Copy
228 "instance": NotifySendPulse,
229 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
230 },
231 ),
232 (
233 "sendpulse://[email protected]/client_id/cs6/?to=invalid",
234 {
235 # an invalid to email
236 "instance": NotifySendPulse,
237 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
238 },
239 ),
240 (
241 "sendpulse://[email protected]/client_id/cs7/[email protected]",
242 {
243 # An email with a designated to email
244 "instance": NotifySendPulse,
245 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
246 },
247 ),
248 (
249 "sendpulse://[email protected]/client_id/cs8/"
250 "?to=Chris<[email protected]>",
251 {
252 # An email with a full name in in To field
253 "instance": NotifySendPulse,
254 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
255 },
256 ),
257 (
258 "sendpulse://[email protected]/client_id/cs9/"
259 "[email protected]/[email protected]/Test<[email protected]>",
260 {
261 # Several emails to notify
262 "instance": NotifySendPulse,
263 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
264 },
265 ),
266 (
267 "sendpulse://[email protected]/client_id/cs10/"
268 "?cc=Chris<[email protected]>",
269 {
270 # An email with a full name in cc
271 "instance": NotifySendPulse,
272 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
273 },
274 ),
275 (
276 "sendpulse://[email protected]/client_id/cs11/[email protected]",
277 {
278 # An email with a full name in cc
279 "instance": NotifySendPulse,
280 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
281 },
282 ),
283 (
284 "sendpulse://[email protected]/client_id/cs12/"
285 "?bcc=Chris<[email protected]>",
286 {
287 # An email with a full name in bcc
288 "instance": NotifySendPulse,
289 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
290 },
291 ),
292 (
293 "sendpulse://[email protected]/client_id/cs13/[email protected]",
294 {
295 # An email with a full name in bcc
296 "instance": NotifySendPulse,
297 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
298 },
299 ),
300 (
301 "sendpulse://[email protected]/client_id/cs14/"
302 "?to=Chris<[email protected]>",
303 {
304 # An email with a full name in bcc
305 "instance": NotifySendPulse,
306 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
307 },
308 ),
309 (
310 "sendpulse://[email protected]/client_id/cs15/[email protected]",
311 {
312 # An email with a full name in bcc
313 "instance": NotifySendPulse,
314 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
315 },
316 ),
317 (
318 "sendpulse://[email protected]/client_id/cs16/"
319 "?template=1234&+sub=value&+sub2=value2",
320 {
321 # A good email with a template + substitutions
322 "instance": NotifySendPulse,
323 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
324 # Our expected url(privacy=True) startswith() response:
325 "privacy_url": "sendpulse://[email protected]/c...d/c...6/",
326 },
327 ),
328 (
329 "sendpulse://[email protected]/client_id/cs17/",
330 {
331 "instance": NotifySendPulse,
332 # force a failure
333 "response": False,
334 "requests_response_code": requests.codes.internal_server_error,
335 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
336 },
337 ),
338 (
339 "sendpulse://[email protected]/client_id/cs18/",
340 {
341 "instance": NotifySendPulse,
342 # throw a bizarre code forcing us to fail to look it up
343 "response": False,
344 "requests_response_code": 999,
345 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
346 },
347 ),
348 (
349 "sendpulse://[email protected]/client_id/cs19/",
350 {
351 "instance": NotifySendPulse,
352 # Throws connection and transfer exceptions when this
353 # flag is set and tests that we gracefully handle them
354 "test_requests_exceptions": True,
355 "requests_response_text": SENDPULSE_GOOD_RESPONSE,
356 },
357 ),
358 )
359
360
361 def test_plugin_sendpulse_urls():
362 """
363 NotifySendPulse() Apprise URLs
364
365 """
366
367 # Run our general tests
368 AppriseURLTester(tests=apprise_url_tests).run_all()
369
370
371 @mock.patch("requests.post")
372 def test_plugin_sendpulse_edge_cases(mock_post):
373 """
374 NotifySendPulse() Edge Cases
375 """
376 request = mock.Mock()
377 request.status_code = requests.codes.ok
378 request.content = SENDPULSE_GOOD_RESPONSE
379
380 # Prepare Mock
381 mock_post.return_value = request
382
383 obj = Apprise.instantiate(
384 "sendpulse://[email protected]/ci/cs/Test<[email protected]>"
385 )
386
387 assert (
388 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
389 is True
390 )
391
392 # Test our call count
393 assert mock_post.call_count == 2
394
395 # Authentication
396 assert (
397 mock_post.call_args_list[0][0][0]
398 == "https://api.sendpulse.com/oauth/access_token"
399 )
400
401 payload = loads(mock_post.call_args_list[0][1]["data"])
402 assert payload == {
403 "grant_type": "client_credentials",
404 "client_id": "ci",
405 "client_secret": "cs",
406 }
407
408 assert (
409 mock_post.call_args_list[1][0][0]
410 == "https://api.sendpulse.com/smtp/emails"
411 )
412 payload = loads(mock_post.call_args_list[1][1]["data"])
413
414 assert payload == {
415 "email": {
416 "from": {"email": "[email protected]", "name": "Apprise"},
417 "to": [{"email": "[email protected]", "name": "Test"}],
418 "subject": "title",
419 "text": "body",
420 "html": "Ym9keQ==",
421 }
422 }
423
424 mock_post.reset_mock()
425
426 obj = Apprise.instantiate("sendpulse://[email protected]/ci/cs/?from=John")
427
428 assert (
429 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
430 is True
431 )
432
433 # Test our call count
434 assert mock_post.call_count == 2
435
436 # Authentication
437 assert (
438 mock_post.call_args_list[0][0][0]
439 == "https://api.sendpulse.com/oauth/access_token"
440 )
441
442 payload = loads(mock_post.call_args_list[0][1]["data"])
443 assert payload == {
444 "grant_type": "client_credentials",
445 "client_id": "ci",
446 "client_secret": "cs",
447 }
448
449 assert (
450 mock_post.call_args_list[1][0][0]
451 == "https://api.sendpulse.com/smtp/emails"
452 )
453 payload = loads(mock_post.call_args_list[1][1]["data"])
454
455 assert payload == {
456 "email": {
457 "from": {"email": "[email protected]", "name": "John"},
458 "to": [{"email": "[email protected]", "name": "John"}],
459 "subject": "title",
460 "text": "body",
461 "html": "Ym9keQ==",
462 }
463 }
464
465 mock_post.reset_mock()
466
467 # Second call no longer needs to authenticate
468 assert (
469 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
470 is True
471 )
472
473 assert mock_post.call_count == 1
474
475 assert (
476 mock_post.call_args_list[0][0][0]
477 == "https://api.sendpulse.com/smtp/emails"
478 )
479
480 # force an exception
481 mock_post.side_effect = requests.RequestException
482 assert (
483 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
484 is False
485 )
486
487 # Set an invalid return code
488 mock_post.side_effect = None
489 request.status_code = 403
490 assert (
491 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
492 is False
493 )
494
495 # Test re-authentication
496 mock_post.reset_mock()
497 request = mock.Mock()
498 obj = Apprise.instantiate("sendpulse://[email protected]/ci/cs/?from=Retry")
499
500 class sendpulse:
501 def __init__(self):
502 # 200 login okay
503 # 401 on retrival
504 # recursive re-attempt to login returns 200
505 # fetch after works
506 self._side_effect = iter(
507 [
508 requests.codes.ok,
509 requests.codes.unauthorized,
510 requests.codes.ok,
511 requests.codes.ok,
512 ]
513 )
514
515 @property
516 def status_code(self):
517 return next(self._side_effect)
518
519 @property
520 def content(self):
521 return SENDPULSE_GOOD_RESPONSE
522
523 mock_post.return_value = sendpulse()
524
525 assert (
526 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
527 is True
528 )
529
530 assert mock_post.call_count == 4
531 # Authentication
532 assert (
533 mock_post.call_args_list[0][0][0]
534 == "https://api.sendpulse.com/oauth/access_token"
535 )
536 # 401 received
537 assert (
538 mock_post.call_args_list[1][0][0]
539 == "https://api.sendpulse.com/smtp/emails"
540 )
541 # Re-authenticate
542 assert (
543 mock_post.call_args_list[2][0][0]
544 == "https://api.sendpulse.com/oauth/access_token"
545 )
546 # Try again
547 assert (
548 mock_post.call_args_list[3][0][0]
549 == "https://api.sendpulse.com/smtp/emails"
550 )
551
552 # Test re-authentication (no recursive loops)
553 mock_post.reset_mock()
554 request = mock.Mock()
555 obj = Apprise.instantiate("sendpulse://[email protected]/ci/cs/?from=Retry")
556
557 class sendpulse:
558 def __init__(self):
559 # oauth always returns okay but notify returns 401
560 # recursive re-attempt only once
561 self._side_effect = iter(
562 [
563 requests.codes.ok,
564 requests.codes.unauthorized,
565 requests.codes.ok,
566 requests.codes.unauthorized,
567 requests.codes.ok,
568 requests.codes.unauthorized,
569 requests.codes.ok,
570 requests.codes.unauthorized,
571 requests.codes.ok,
572 requests.codes.unauthorized,
573 requests.codes.ok,
574 requests.codes.unauthorized,
575 requests.codes.ok,
576 requests.codes.unauthorized,
577 requests.codes.ok,
578 requests.codes.unauthorized,
579 ]
580 )
581
582 @property
583 def status_code(self):
584 return next(self._side_effect)
585
586 @property
587 def content(self):
588 return SENDPULSE_GOOD_RESPONSE
589
590 mock_post.return_value = sendpulse()
591
592 assert (
593 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
594 is False
595 )
596
597 assert mock_post.call_count == 4
598 # Authentication
599 assert (
600 mock_post.call_args_list[0][0][0]
601 == "https://api.sendpulse.com/oauth/access_token"
602 )
603 # 401 received
604 assert (
605 mock_post.call_args_list[1][0][0]
606 == "https://api.sendpulse.com/smtp/emails"
607 )
608 # Re-authenticate
609 assert (
610 mock_post.call_args_list[2][0][0]
611 == "https://api.sendpulse.com/oauth/access_token"
612 )
613 # Last failed attempt
614 assert (
615 mock_post.call_args_list[3][0][0]
616 == "https://api.sendpulse.com/smtp/emails"
617 )
618
619 mock_post.side_effect = None
620 request = mock.Mock()
621 request.status_code = requests.codes.ok
622 request.content = SENDPULSE_GOOD_RESPONSE
623 mock_post.return_value = request
624 for expires_in in (None, -1, "garbage", 3600, 300000):
625 request.content = dumps(
626 {
627 "access_token": "abc123",
628 "expires_in": expires_in,
629 }
630 )
631
632 # Instantiate our object
633 obj = Apprise.instantiate("sendpulse://[email protected]/ci/cs/")
634
635 # Test variations of responses
636 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
637
638 # expires_in is missing
639 request.content = dumps(
640 {
641 "access_token": "abc123",
642 }
643 )
644
645 # Instantiate our object
646 obj = Apprise.instantiate("sendpulse://[email protected]/ci/cs/")
647 assert (
648 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
649 is True
650 )
651
652
653 def test_plugin_sendpulse_fail_cases():
654 """
655 NotifySendPulse() Fail Cases
656
657 """
658
659 # no client_id
660 with pytest.raises(TypeError):
661 NotifySendPulse(
662 client_id="abcd", client_secret=None, from_addr="[email protected]"
663 )
664
665 with pytest.raises(TypeError):
666 NotifySendPulse(
667 client_id=None,
668 client_secret="abcd123",
669 from_addr="[email protected]",
670 )
671
672 # invalid from email
673 with pytest.raises(TypeError):
674 NotifySendPulse(
675 client_id="abcd", client_secret="abcd456", from_addr="!invalid"
676 )
677
678 # no email
679 with pytest.raises(TypeError):
680 NotifySendPulse(
681 client_id="abcd", client_secret="abcd789", from_addr=None
682 )
683
684 # Invalid To email address
685 NotifySendPulse(
686 client_id="abcd",
687 client_secret="abcd321",
688 from_addr="[email protected]",
689 targets="!invalid",
690 )
691
692 # Test invalid bcc/cc entries mixed with good ones
693 assert isinstance(
694 NotifySendPulse(
695 client_id="abcd",
696 client_secret="abcd654",
697 from_addr="[email protected]",
698 bcc=("[email protected]", "!invalid"),
699 cc=("[email protected]", "!invalid"),
700 ),
701 NotifySendPulse,
702 )
703
704
705 @mock.patch("requests.post")
706 def test_plugin_sendpulse_attachments(mock_post):
707 """
708 NotifySendPulse() Attachments
709
710 """
711
712 request = mock.Mock()
713 request.status_code = requests.codes.ok
714 request.content = SENDPULSE_GOOD_RESPONSE
715
716 # Prepare Mock
717 mock_post.return_value = request
718
719 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
720 attach = AppriseAttachment(path)
721 obj = Apprise.instantiate("sendpulse://[email protected]/aaaa/bbbb")
722 assert isinstance(obj, NotifySendPulse)
723 assert (
724 obj.notify(
725 body="body",
726 title="title",
727 notify_type=NotifyType.INFO,
728 attach=attach,
729 )
730 is True
731 )
732
733 mock_post.reset_mock()
734
735 # Try again in a use case where we can't access the file
736 with mock.patch("os.path.isfile", return_value=False):
737 assert (
738 obj.notify(
739 body="body",
740 title="title",
741 notify_type=NotifyType.INFO,
742 attach=attach,
743 )
744 is False
745 )
746
747 # Try again in a use case where we can't access the file
748 with mock.patch("builtins.open", side_effect=OSError):
749 assert (
750 obj.notify(
751 body="body",
752 title="title",
753 notify_type=NotifyType.INFO,
754 attach=attach,
755 )
756 is False
757 )