Debian Codegraph @

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