Debian Codegraph @

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