Debian Codegraph @

tests/test_plugin_bulksms.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 loads
29
30 # Disable logging for a cleaner testing output
31 import logging
32 from unittest import mock
33
34 from helpers import AppriseURLTester
35 import requests
36
37 from apprise import Apprise, NotifyType
38 from apprise.plugins.bulksms import NotifyBulkSMS
39
40 logging.disable(logging.CRITICAL)
41
42 # Our Testing URLs
43 apprise_url_tests = (
44 (
45 "bulksms://",
46 {
47 # Instantiated but no auth, so no otification can happen
48 "instance": NotifyBulkSMS,
49 # Expected notify() response because we have no one to notify
50 "notify_response": False,
51 },
52 ),
53 (
54 "bulksms://:@/",
55 {
56 # invalid auth
57 "instance": NotifyBulkSMS,
58 # Expected notify() response because we have no one to notify
59 "notify_response": False,
60 },
61 ),
62 (
63 "bulksms://{}@12345678".format("a" * 10),
64 {
65 # Just user provided (no password)
66 "instance": NotifyBulkSMS,
67 # Expected notify() response because we have no one to notify
68 "notify_response": False,
69 },
70 ),
71 (
72 "bulksms://{}:{}@{}".format("a" * 10, "b" * 10, "3" * 5),
73 {
74 # invalid nubmer provided
75 "instance": NotifyBulkSMS,
76 # Expected notify() response because we have no one to notify
77 "notify_response": False,
78 },
79 ),
80 (
81 "bulksms://{}:{}@123/{}/abcd/".format("a" * 5, "b" * 10, "3" * 11),
82 {
83 # included group and phone, short number (123) dropped
84 "instance": NotifyBulkSMS,
85 "privacy_url": "bulksms://a...a:****@+33333333333/@abcd",
86 },
87 ),
88 (
89 "bulksms://{}:{}@{}?batch=y&unicode=n".format(
90 "b" * 5, "c" * 10, "4" * 11
91 ),
92 {
93 "instance": NotifyBulkSMS,
94 # Our expected url(privacy=True) startswith() response:
95 "privacy_url": "bulksms://b...b:****@+4444444444",
96 },
97 ),
98 (
99 "bulksms://{}:{}@123456/{}".format("a" * 10, "b" * 10, "4" * 11),
100 {
101 # using short-code (6 characters)
102 "instance": NotifyBulkSMS,
103 },
104 ),
105 (
106 "bulksms://{}:{}@{}".format("a" * 10, "b" * 10, "5" * 11),
107 {
108 # using phone no with no target - we text ourselves in
109 # this case
110 "instance": NotifyBulkSMS,
111 },
112 ),
113 # Test route group
114 (
115 "bulksms://{}:{}@admin?route=premium".format("a" * 10, "b" * 10),
116 {
117 "instance": NotifyBulkSMS,
118 },
119 ),
120 (
121 "bulksms://{}:{}@admin?route=invalid".format("a" * 10, "b" * 10),
122 {
123 # invalid route
124 "instance": TypeError,
125 },
126 ),
127 (
128 "bulksms://_?user={}&password={}&from={}".format(
129 "a" * 10, "b" * 10, "5" * 11
130 ),
131 {
132 # use get args to acomplish the same thing
133 "instance": NotifyBulkSMS,
134 },
135 ),
136 (
137 "bulksms://_?user={}&password={}&from={}".format(
138 "a" * 10, "b" * 10, "5" * 3
139 ),
140 {
141 # use get args to acomplish the same thing
142 "instance": TypeError,
143 },
144 ),
145 (
146 "bulksms://_?user={}&password={}&from={}&to={}".format(
147 "a" * 10, "b" * 10, "5" * 11, "7" * 13
148 ),
149 {
150 # use to=
151 "instance": NotifyBulkSMS,
152 },
153 ),
154 (
155 "bulksms://{}:{}@{}".format("a" * 10, "b" * 10, "a" * 3),
156 {
157 "instance": NotifyBulkSMS,
158 # throw a bizarre code forcing us to fail to look it up
159 "response": False,
160 "requests_response_code": 999,
161 },
162 ),
163 (
164 "bulksms://{}:{}@{}".format("a" * 10, "b" * 10, "6" * 11),
165 {
166 "instance": NotifyBulkSMS,
167 # Throws a series of i/o exceptions with this flag
168 # is set and tests that we gracefully handle them
169 "test_requests_exceptions": True,
170 },
171 ),
172 )
173
174
175 def test_plugin_bulksms_urls():
176 """NotifyTemplate() Apprise URLs."""
177
178 # Run our general tests
179 AppriseURLTester(tests=apprise_url_tests).run_all()
180
181
182 @mock.patch("requests.post")
183 def test_plugin_bulksms_edge_cases(mock_post):
184 """NotifyBulkSMS() Edge Cases."""
185
186 # Initialize some generic (but valid) tokens
187 user = "abcd"
188 pwd = "mypass123"
189 targets = [
190 "+1(555) 123-1234",
191 "1555 5555555",
192 "group",
193 # A garbage entry
194 "12",
195 # NOw a valid one because a group was implicit
196 "@12",
197 ]
198
199 # Prepare our response
200 response = requests.Request()
201 response.status_code = requests.codes.ok
202
203 # Prepare Mock
204 mock_post.return_value = response
205
206 # Instantiate our object
207 obj = Apprise.instantiate(
208 "bulksms://{}:{}@{}?batch=n".format(user, pwd, "/".join(targets))
209 )
210
211 assert (
212 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
213 is True
214 )
215
216 # We know there are 4 targets
217 assert len(obj) == 4
218
219 # Test our call count
220 assert mock_post.call_count == 4
221
222 # Test
223 details = mock_post.call_args_list[0]
224 payload = loads(details[1]["data"])
225 assert payload["to"] == "+15551231234"
226 assert payload["body"] == "title\r\nbody"
227
228 details = mock_post.call_args_list[1]
229 payload = loads(details[1]["data"])
230 assert payload["to"] == "+15555555555"
231 assert payload["body"] == "title\r\nbody"
232
233 details = mock_post.call_args_list[2]
234 payload = loads(details[1]["data"])
235 assert isinstance(payload["to"], dict)
236 assert payload["to"]["name"] == "group"
237 assert payload["body"] == "title\r\nbody"
238
239 details = mock_post.call_args_list[3]
240 payload = loads(details[1]["data"])
241 assert isinstance(payload["to"], dict)
242 assert payload["to"]["name"] == "12"
243 assert payload["body"] == "title\r\nbody"
244
245 # Verify our URL looks good
246 assert obj.url().startswith(
247 "bulksms://{}:{}@{}".format(
248 user,
249 pwd,
250 "/".join(["+15551231234", "+15555555555", "@group", "@12"]),
251 )
252 )
253
254 assert "batch=no" in obj.url()
255
256 # With our batch in place, our calculations are different
257 obj = Apprise.instantiate(
258 "bulksms://{}:{}@{}?batch=y".format(user, pwd, "/".join(targets))
259 )
260 # 2 groups and 2 phones are lumped together
261 assert len(obj) == 3