Debian Codegraph @

tests/test_plugin_messagebird.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 from unittest import mock
31
32 from helpers import AppriseURLTester
33 import pytest
34 import requests
35
36 from apprise.exception import AppriseImproperlyConfigured
37 from apprise.plugins.messagebird import NotifyMessageBird
38
39 logging.disable(logging.CRITICAL)
40
41 # Our Testing URLs
42 apprise_url_tests = (
43 (
44 "msgbird://",
45 {
46 # No hostname/apikey specified
47 "instance": AppriseImproperlyConfigured,
48 },
49 ),
50 (
51 "msgbird://{}/abcd".format("a" * 25),
52 {
53 # invalid characters in source phone number
54 "instance": AppriseImproperlyConfigured,
55 },
56 ),
57 (
58 "msgbird://{}/123".format("a" * 25),
59 {
60 # invalid source phone number
61 "instance": AppriseImproperlyConfigured,
62 },
63 ),
64 (
65 "msgbird://{}/15551232000".format("a" * 25),
66 {
67 # target phone number becomes who we text too; all is good
68 "instance": NotifyMessageBird,
69 # Our expected url(privacy=True) startswith() response:
70 "privacy_url": "msgbird://a...a/15551232000",
71 },
72 ),
73 (
74 "msgbird://{}/15551232000/abcd".format("a" * 25),
75 {
76 # valid credentials
77 "instance": NotifyMessageBird,
78 # Since there are no targets specified we expect a False return on
79 # send()
80 "notify_response": False,
81 },
82 ),
83 (
84 "msgbird://{}/15551232000/123".format("a" * 25),
85 {
86 # valid credentials
87 "instance": NotifyMessageBird,
88 # Since there are no targets specified we expect a False return on
89 # send()
90 "notify_response": False,
91 },
92 ),
93 (
94 "msgbird://{}/?from=15551233000&to=15551232000".format("a" * 25),
95 {
96 # reference to to= and from=
97 "instance": NotifyMessageBird,
98 },
99 ),
100 (
101 "msgbird://{}/15551232000".format("a" * 25),
102 {
103 "instance": NotifyMessageBird,
104 # force a failure
105 "response": False,
106 "requests_response_code": requests.codes.internal_server_error,
107 },
108 ),
109 (
110 "msgbird://{}/15551232000".format("a" * 25),
111 {
112 "instance": NotifyMessageBird,
113 # throw a bizarre code forcing us to fail to look it up
114 "response": False,
115 "requests_response_code": 999,
116 },
117 ),
118 (
119 "msgbird://{}/15551232000".format("a" * 25),
120 {
121 "instance": NotifyMessageBird,
122 # Throws a series of i/o exceptions with this flag
123 # is set and tests that we gracefully handle them
124 "test_requests_exceptions": True,
125 },
126 ),
127 )
128
129
130 def test_plugin_messagebird_urls():
131 """NotifyTemplate() Apprise URLs."""
132
133 # Run our general tests
134 AppriseURLTester(tests=apprise_url_tests).run_all()
135
136
137 @mock.patch("requests.post")
138 def test_plugin_messagebird_edge_cases(mock_post):
139 """NotifyMessageBird() Edge Cases."""
140
141 # Prepare our response
142 response = requests.Request()
143 response.status_code = requests.codes.ok
144
145 # Prepare Mock
146 mock_post.return_value = response
147
148 # Initialize some generic (but valid) tokens
149 # authkey = '{}'.format('a' * 24)
150 source = "+1 (555) 123-3456"
151
152 # No apikey specified
153 with pytest.raises(AppriseImproperlyConfigured):
154 NotifyMessageBird(apikey=None, source=source)
155 with pytest.raises(AppriseImproperlyConfigured):
156 NotifyMessageBird(apikey=" ", source=source)