Debian Codegraph @

tests/test_plugin_exotel.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 import Apprise, NotifyType
37 from apprise.exception import AppriseImproperlyConfigured
38 from apprise.plugins.exotel import NotifyExotel
39
40 logging.disable(logging.CRITICAL)
41
42 # Our Testing URLs
43 apprise_url_tests = (
44 (
45 "exotel://",
46 {
47 # No Account SID specified
48 "instance": AppriseImproperlyConfigured,
49 },
50 ),
51 (
52 "exotel://:@/",
53 {
54 # invalid Auth token
55 "instance": AppriseImproperlyConfigured,
56 },
57 ),
58 (
59 "exotel://{}@12345678".format("a" * 32),
60 {
61 # Just sid provided
62 "instance": AppriseImproperlyConfigured,
63 },
64 ),
65 (
66 "exotel://{}:{}@_".format("a" * 32, "b" * 32),
67 {
68 # sid and token provided but invalid from
69 "instance": AppriseImproperlyConfigured,
70 },
71 ),
72 (
73 "exotel://{}:{}@/%20".format("a" * 32, "b" * 32),
74 {
75 # sid and token provided but no from
76 "instance": AppriseImproperlyConfigured,
77 },
78 ),
79 (
80 "exotel://{}:{}@{}".format("a" * 32, "b" * 32, "3" * 8),
81 {
82 # sid and token provided and from but invalid from no
83 "instance": AppriseImproperlyConfigured,
84 },
85 ),
86 (
87 "exotel://{}:{}@{}".format("a" * 32, "b" * 32, "3" * 9),
88 {
89 # sid and token provided and from
90 "instance": NotifyExotel,
91 },
92 ),
93 (
94 "exotel://{}:{}@EXOTEL/{}".format("a" * 32, "b" * 32, "3" * 9),
95 {
96 # sid and token provided with a sender ID source
97 "instance": NotifyExotel,
98 },
99 ),
100 (
101 "exotel://{}:{}@600123/{}".format("a" * 32, "b" * 32, "3" * 9),
102 {
103 # sid and token provided with a numeric sender ID source
104 "instance": NotifyExotel,
105 },
106 ),
107 (
108 "exotel://{}:{}@{}/123/{}/abcd/".format(
109 "a" * 32, "b" * 32, "3" * 11, "9" * 15
110 ),
111 {
112 # valid everything but target numbers
113 "instance": NotifyExotel,
114 # Since the targets are invalid, we'll fail to send()
115 "notify_response": False,
116 },
117 ),
118 (
119 "exotel://{}:{}@12345/{}".format("a" * 32, "b" * 32, "4" * 11),
120 {
121 # using short-code (5 characters) is not supported
122 "instance": AppriseImproperlyConfigured,
123 },
124 ),
125 (
126 "exotel://{}:{}@{}".format("a" * 32, "b" * 32, "5" * 11),
127 {
128 # using phone no with no target - we text ourselves in
129 # this case
130 "instance": NotifyExotel,
131 # Our expected url(privacy=True) startswith() response:
132 "privacy_url": "exotel://****:b...b@55555555555/",
133 },
134 ),
135 (
136 "exotel://{}:{}@{}?batch=yes".format("a" * 32, "b" * 32, "5" * 11),
137 {
138 # Test batch flag
139 "instance": NotifyExotel,
140 },
141 ),
142 (
143 "exotel://{}:{}@{}?batch=no".format("a" * 32, "b" * 32, "5" * 11),
144 {
145 # Test batch flag
146 "instance": NotifyExotel,
147 },
148 ),
149 (
150 "exotel://_?sid={}&token={}&from={}".format(
151 "a" * 32, "b" * 32, "5" * 11
152 ),
153 {
154 # use get args to accomplish the same thing
155 "instance": NotifyExotel,
156 },
157 ),
158 (
159 "exotel://_?sid={}&token={}&from={}&unicode=Yes".format(
160 "a" * 32, "b" * 32, "5" * 11
161 ),
162 {
163 # Test unicode flag
164 "instance": NotifyExotel,
165 },
166 ),
167 (
168 "exotel://_?sid={}&token={}&from={}&unicode=no".format(
169 "a" * 32, "b" * 32, "5" * 11
170 ),
171 {
172 # Test unicode flag
173 "instance": NotifyExotel,
174 },
175 ),
176 (
177 "exotel://_?sid={}&token={}&from={}&region=us".format(
178 "a" * 32, "b" * 32, "5" * 11
179 ),
180 {
181 # Test region flag (Us)
182 "instance": NotifyExotel,
183 },
184 ),
185 (
186 "exotel://_?sid={}&token={}&from={}&region=in".format(
187 "a" * 32, "b" * 32, "5" * 11
188 ),
189 {
190 # Test region flag (India)
191 "instance": NotifyExotel,
192 },
193 ),
194 (
195 "exotel://_?sid={}&token={}&from={}&region=invalid".format(
196 "a" * 32, "b" * 32, "5" * 11
197 ),
198 {
199 # Test region flag Invalid
200 "instance": AppriseImproperlyConfigured,
201 },
202 ),
203 (
204 "exotel://_?sid={}&token={}&from={}&priority=normal".format(
205 "a" * 32, "b" * 32, "5" * 11
206 ),
207 {
208 # Test region flag (Us)
209 "instance": NotifyExotel,
210 },
211 ),
212 (
213 "exotel://_?sid={}&token={}&from={}&priority=high".format(
214 "a" * 32, "b" * 32, "5" * 11
215 ),
216 {
217 # Test region flag (India)
218 "instance": NotifyExotel,
219 },
220 ),
221 (
222 "exotel://_?sid={}&token={}&from={}&priority=invalid".format(
223 "a" * 32, "b" * 32, "5" * 11
224 ),
225 {
226 # Test region flag Invalid
227 "instance": AppriseImproperlyConfigured,
228 },
229 ),
230 (
231 "exotel://_?sid={}&token={}&source={}".format(
232 "a" * 32, "b" * 32, "5" * 11
233 ),
234 {
235 # use get args to accomplish the same thing (use source
236 # instead of from)
237 "instance": NotifyExotel,
238 },
239 ),
240 (
241 "exotel://_?sid={}&token={}&from={}&to={}".format(
242 "a" * 32, "b" * 32, "5" * 11, "7" * 13
243 ),
244 {
245 # use to=
246 "instance": NotifyExotel,
247 },
248 ),
249 (
250 "exotel://{}:{}@{}".format("a" * 32, "b" * 32, "6" * 11),
251 {
252 "instance": NotifyExotel,
253 # throw a bizarre code forcing us to fail to look it up
254 "response": False,
255 "requests_response_code": 999,
256 },
257 ),
258 (
259 "exotel://{}:{}@{}".format("a" * 32, "b" * 32, "6" * 11),
260 {
261 "instance": NotifyExotel,
262 # Throws a series of connection and transfer exceptions when
263 # this flag is set and tests that we gracefully handle them
264 "test_requests_exceptions": True,
265 },
266 ),
267 )
268
269
270 def test_plugin_exotel_urls():
271 """NotifyExotel() Apprise URLs."""
272
273 assert NotifyExotel.setup_url == "https://appriseit.com/services/exotel/"
274
275 # Run our general tests
276 AppriseURLTester(tests=apprise_url_tests).run_all()
277
278
279 @mock.patch("requests.post")
280 def test_plugin_exotel_edge_cases(mock_post):
281 """NotifyExotel() Edge Cases."""
282
283 sid = "a" * 32
284 apikey = "api-key"
285 token = "b" * 32
286 source = "LM-EXOTEL"
287 targets = ("6" * 11, "7" * 11)
288
289 response = requests.Request()
290 response.status_code = requests.codes.ok
291
292 mock_post.return_value = response
293
294 with pytest.raises(AppriseImproperlyConfigured):
295 NotifyExotel(sid=sid, token=token, source=" ")
296
297 with pytest.raises(AppriseImproperlyConfigured):
298 NotifyExotel(sid=sid, token=token, source=source, apikey=" ")
299
300 with pytest.raises(AppriseImproperlyConfigured):
301 NotifyExotel(sid=sid, token=token, source=source, region_name=" ")
302
303 with pytest.raises(AppriseImproperlyConfigured):
304 NotifyExotel(sid=sid, token=token, source=source, priority=" ")
305
306 # Programmatic priority shorthand is supported as an Apprise convenience.
307 obj = NotifyExotel(sid=sid, token=token, source="5" * 11, priority="+")
308 assert obj.priority == "high"
309 assert obj.targets == ["5" * 11]
310
311 mock_post.reset_mock()
312 assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
313 assert mock_post.call_count == 1
314 payload = mock_post.call_args_list[0][1]["data"]
315 assert payload["From"] == "5" * 11
316 assert payload["To"] == "5" * 11
317 assert payload["Priority"] == "high"
318
319 # When a Sender ID is provided without targets, pass it upstream as the
320 # implicit recipient and let Exotel decide whether it is deliverable.
321 obj = NotifyExotel(sid=sid, token=token, source=source)
322 assert obj.targets == [source]
323 assert len(obj) == 1
324
325 mock_post.reset_mock()
326 assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
327 assert mock_post.call_count == 1
328 payload = mock_post.call_args_list[0][1]["data"]
329 assert payload["From"] == source
330 assert payload["To"] == source
331
332 obj = Apprise.instantiate(
333 "exotel://{}:{}@{}/{}?apikey={}&region=in&unicode=no"
334 "&priority=high".format(sid, token, source, "/".join(targets), apikey)
335 )
336
337 assert isinstance(obj, NotifyExotel)
338 assert len(obj) == len(targets)
339 assert set(obj.targets) == set(targets)
340
341 mock_post.reset_mock()
342 assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
343
344 assert mock_post.call_count == len(targets)
345
346 first_call = mock_post.call_args_list[0]
347 assert first_call[0][0] == (
348 "https://api.in.exotel.com/v1/Accounts/{}/Sms/send".format(sid)
349 )
350 assert first_call[1]["auth"] == (apikey, token)
351
352 first_payload = first_call[1]["data"]
353 assert first_payload["From"] == source
354 assert first_payload["To"] == obj.targets[0]
355 assert first_payload["Body"] == "title\r\nbody"
356 assert first_payload["EncodingType"] == "plain"
357 assert first_payload["Priority"] == "high"
358 assert "StatusCallback" not in first_payload
359
360 second_call = mock_post.call_args_list[1]
361 assert second_call[0][0] == (
362 "https://api.in.exotel.com/v1/Accounts/{}/Sms/send".format(sid)
363 )
364 assert second_call[1]["auth"] == (apikey, token)
365
366 second_payload = second_call[1]["data"]
367 assert second_payload["From"] == source
368 assert second_payload["To"] == obj.targets[1]
369 assert second_payload["Body"] == "title\r\nbody"
370 assert second_payload["EncodingType"] == "plain"
371 assert second_payload["Priority"] == "high"
372 assert "StatusCallback" not in second_payload
373
374 obj = Apprise.instantiate(
375 "exotel://{}:{}@{}/{}?apikey={}&region=in&unicode=no"
376 "&priority=high&batch=yes".format(
377 sid, token, source, "/".join(targets), apikey
378 )
379 )
380 assert isinstance(obj, NotifyExotel)
381 assert len(obj) == 1
382 assert set(obj.targets) == set(targets)
383
384 mock_post.reset_mock()
385 assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
386 assert mock_post.call_count == 1
387
388 first_call = mock_post.call_args_list[0]
389 assert first_call[0][0] == (
390 "https://api.in.exotel.com/v1/Accounts/{}/Sms/send".format(sid)
391 )
392 assert first_call[1]["auth"] == (apikey, token)
393
394 payload = first_call[1]["data"]
395 assert payload["From"] == source
396 assert payload["To"] == obj.targets
397 assert payload["Body"] == "title\r\nbody"
398 assert payload["EncodingType"] == "plain"
399 assert payload["Priority"] == "high"
400 assert "StatusCallback" not in payload
401 assert "batch=yes" in obj.url()
402
403 batch_targets = tuple("6{:010d}".format(i) for i in range(101))
404 batch_obj = NotifyExotel(
405 sid=sid,
406 token=token,
407 source=source,
408 targets=batch_targets,
409 batch=True,
410 )
411 assert len(batch_obj) == 2
412
413 mock_post.reset_mock()
414 assert batch_obj.notify(
415 body="body", title="title", notify_type=NotifyType.INFO
416 )
417 assert mock_post.call_count == 2
418
419 first_payload = mock_post.call_args_list[0][1]["data"]
420 assert first_payload["To"] == list(batch_targets[:100])
421
422 second_payload = mock_post.call_args_list[1][1]["data"]
423 assert second_payload["To"] == list(batch_targets[100:])
424
425 # Targets must not be part of the URL identifier.
426 obj_cmp = Apprise.instantiate(
427 "exotel://{}:{}@{}/{}?region=in".format(sid, token, source, "8" * 11)
428 )
429 assert obj.url_id() != obj_cmp.url_id()
430
431 obj_cmp = Apprise.instantiate(
432 "exotel://{}:{}@{}/{}?apikey={}&region=in".format(
433 sid, token, source, "8" * 11, apikey
434 )
435 )
436 assert obj.url_id() == obj_cmp.url_id()
437
438 obj_cmp = Apprise.instantiate(
439 "exotel://{}:{}@{}/{}?key={}&region=in".format(
440 sid, token, source, "8" * 11, apikey
441 )
442 )
443 assert obj.url_id() == obj_cmp.url_id()
444
445 # Region changes the upstream API host and should alter the identifier.
446 obj_cmp = Apprise.instantiate(
447 "exotel://{}:{}@{}/{}?apikey={}&region=us".format(
448 sid, token, source, "8" * 11, apikey
449 )
450 )
451 assert obj.url_id() != obj_cmp.url_id()
452
453 # Even if all provided targets are invalid, Apprise treats the notifier
454 # as one logical request target for length purposes.
455 obj = Apprise.instantiate(f"exotel://{sid}:{token}@{source}/abcd")
456 assert isinstance(obj, NotifyExotel)
457 assert len(obj) == 1