Debian Codegraph @

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