Debian Codegraph @

tests/test_plugin_sfr.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 import json
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 pytest
36 import requests
37
38 from apprise.plugins.sfr import NotifySFR
39
40 logging.disable(logging.CRITICAL)
41
42 SFR_GOOD_RESPONSE = json.dumps(
43 {
44 "success": True,
45 "reponse": 8888888,
46 }
47 )
48
49 SFR_BAD_RESPONSE = json.dumps(
50 {
51 "success": False,
52 "errorCode": "THIS_IS_AN_ERROR",
53 "errorDetail": "Appel api en erreur",
54 "fatal": True,
55 "invalidParams": True,
56 }
57 )
58
59 # Our Testing URLs
60 apprise_url_tests = (
61 (
62 "sfr://",
63 {
64 # No host specified
65 "instance": TypeError,
66 },
67 ),
68 (
69 "sfr://:@/",
70 {
71 # Invalid host
72 "instance": TypeError,
73 },
74 ),
75 (
76 "sfr://:service_password",
77 {
78 # No user specified
79 "instance": TypeError,
80 },
81 ),
82 (
83 "sfr://testing:serv@ice_password",
84 {
85 # Invalid Password
86 "instance": TypeError,
87 },
88 ),
89 (
90 "sfr://testing:service_password@/5555555555",
91 {
92 # No spaceId provided
93 "instance": TypeError,
94 },
95 ),
96 (
97 "sfr://testing:service_password@12345/",
98 {
99 # No target provided
100 "instance": TypeError,
101 },
102 ),
103 (
104 f"sfr://:service_password@12345/{3 * 13}",
105 {
106 # No host but everything else provided
107 "instance": TypeError,
108 },
109 ),
110 (
111 "sfr://:service_password@space_id/targets?media=TEST",
112 {
113 "instance": TypeError,
114 },
115 ),
116 (
117 "sfr://service_id:",
118 {
119 "instance": TypeError,
120 },
121 ),
122 (
123 "sfr://service_id:@",
124 {
125 "instance": TypeError,
126 },
127 ),
128 (
129 "sfr://service_id:@{}".format("0" * 3),
130 {
131 "instance": TypeError,
132 },
133 ),
134 (
135 "sfr://service_id:@{}/".format("0" * 3),
136 {
137 "instance": TypeError,
138 },
139 ),
140 (
141 "sfr://service_id:@{}/targets".format("0" * 3),
142 {
143 "instance": TypeError,
144 },
145 ),
146 (
147 "sfr://service_id:@{}/targets?media=TEST".format("0" * 3),
148 {
149 "instance": TypeError,
150 },
151 ),
152 (
153 "sfr://service_id:service_password@{}/{}?from=MyApp&timeout=30".format(
154 "0" * 3, "0" * 10
155 ),
156 {
157 # a valid group
158 "instance": NotifySFR,
159 # Our expected url(privacy=True) startswith() response:
160 "privacy_url": (
161 "sfr://service_id:****@0...0/0000000000?"
162 "from=MyApp&timeout=30&voice=claire08s&"
163 "lang=fr_FR&media=SMSUnicode"
164 ),
165 # Our response expected server response
166 "requests_response_text": SFR_GOOD_RESPONSE,
167 },
168 ),
169 (
170 "sfr://service_id:service_password@{}/{}?voice=laura8k&lang=en_US".format(
171 "0" * 3, "0" * 10
172 ),
173 {
174 # a valid group
175 "instance": NotifySFR,
176 # Our expected url(privacy=True) startswith() response:
177 "privacy_url": (
178 "sfr://service_id:****@0...0/0000000000?"
179 "from=&timeout=2880&voice=laura8k&"
180 "lang=en_US&media=SMSUnicode"
181 ),
182 # Our response expected server response
183 "requests_response_text": SFR_GOOD_RESPONSE,
184 },
185 ),
186 (
187 "sfr://service_id:service_password@{}/{}?media=SMS".format(
188 "0" * 3, "0" * 10
189 ),
190 {
191 # a valid group
192 "instance": NotifySFR,
193 # Our expected url(privacy=True) startswith() response:
194 "privacy_url": (
195 "sfr://service_id:****@0...0/0000000000?"
196 "from=&timeout=2880&voice=claire08s&"
197 "lang=fr_FR&media=SMS"
198 ),
199 # Our response expected server response
200 "requests_response_text": SFR_GOOD_RESPONSE,
201 },
202 ),
203 (
204 "sfr://service_id:service_password@{}/{}".format("0" * 3, "0" * 10),
205 {
206 # Test case where we get a bad response
207 "instance": NotifySFR,
208 # Our expected url(privacy=True) startswith() response:
209 "privacy_url": (
210 "sfr://service_id:****@0...0/0000000000?"
211 "from=&timeout=2880&voice=claire08s&"
212 "lang=fr_FR&media=SMSUnicode"
213 ),
214 # Our failed notification expected server response
215 "requests_response_text": SFR_BAD_RESPONSE,
216 "requests_response_code": requests.codes.ok,
217 # as a result, we expect a failed notification
218 "response": False,
219 },
220 ),
221 )
222
223
224 def test_plugin_sfr_urls():
225 """NotifySFR() Apprise URLs."""
226 # Run our general tests
227 AppriseURLTester(tests=apprise_url_tests).run_all()
228
229
230 @mock.patch("requests.post")
231 def test_plugin_sfr_notification_ok(mock_post):
232 """NotifySFR() Notifications Ok response."""
233 # Prepare Mock
234 # Create a mock response object
235 response = mock.Mock()
236 response.status_code = requests.codes.ok
237 response.content = SFR_GOOD_RESPONSE
238 mock_post.return_value = response
239
240 # Test our URL parsing
241 results = NotifySFR.parse_url(
242 "sfr://srv:pwd@{}/{}?media=SMSLong".format("1" * 8, "0" * 10)
243 )
244
245 assert isinstance(results, dict)
246 assert results["user"] == "srv"
247 assert results["password"] == "pwd"
248 assert results["space_id"] == "11111111"
249 assert results["targets"] == ["0000000000"]
250 assert results["media"] == "SMSLong"
251 assert results["timeout"] == ""
252 assert results["voice"] == ""
253 assert results["lang"] == ""
254 assert results["sender"] == ""
255
256 instance = NotifySFR(**results)
257 assert isinstance(instance, NotifySFR)
258 assert len(instance) == 1
259 assert instance.lang == "fr_FR"
260 assert instance.lang == "fr_FR"
261 assert instance.sender == ""
262 assert isinstance(instance.targets, list)
263 assert isinstance(instance.timeout, int)
264 assert isinstance(instance.voice, str)
265 assert isinstance(instance.space_id, str)
266
267 response = instance.send(body="test")
268 assert response is True
269 assert mock_post.call_count == 1
270
271
272 @mock.patch("requests.post")
273 def test_plugin_sfr_notification_multiple_targets_ok(mock_post):
274 """NotifySFR() Notifications ko response."""
275 # Reset our object
276 mock_post.reset_mock()
277 # Prepare Mock
278 # Create a mock response object
279 response = mock.Mock()
280 response.status_code = requests.codes.ok
281 response.content = SFR_GOOD_RESPONSE
282 mock_post.return_value = response
283
284 # Test "real" parameters
285 results = NotifySFR.parse_url(
286 "sfr://{}:other_fjv&8password@{}/?to={},{}&from=MyCustomUser".format(
287 "4" * 6, "1" * 8, "6" * 10, "8" * 10
288 )
289 )
290
291 assert isinstance(results, dict)
292 assert results["user"] == "444444"
293 assert results["password"] == "other_fjv&8password"
294 assert results["space_id"] == "11111111"
295 assert results["targets"] == ["6666666666", "8888888888"]
296 assert results["media"] == ""
297 assert results["timeout"] == ""
298 assert results["voice"] == ""
299 assert results["lang"] == ""
300 assert results["sender"] == "MyCustomUser"
301
302 instance = NotifySFR(**results)
303 assert isinstance(instance, NotifySFR)
304 assert len(instance) == 2
305 assert instance.lang == "fr_FR"
306 assert instance.sender == "MyCustomUser"
307 assert instance.media == "SMSUnicode"
308 assert isinstance(instance.targets, list)
309 assert instance.timeout == 2880
310 assert instance.voice == "claire08s"
311 assert isinstance(instance.space_id, str)
312
313 response = instance.send(body="test")
314 assert response is True
315 assert mock_post.call_count == 2
316
317
318 @mock.patch("requests.post")
319 def test_plugin_sfr_notification_ko(mock_post):
320 """NotifySFR() Notifications ko response."""
321 # Reset our object
322 mock_post.reset_mock()
323 # Prepare Mock
324 # Create a mock response object
325 response = mock.Mock()
326 response.status_code = requests.codes.ok
327 response.content = SFR_BAD_RESPONSE
328 mock_post.return_value = response
329
330 # Test "real" parameters
331 results = NotifySFR.parse_url(
332 "sfr://{}:other_fjv&8password@{}/{}?timeout=30&media=SMS".format(
333 "4" * 6, "1" * 8, "2" * 10
334 )
335 )
336
337 assert isinstance(results, dict)
338 assert results["user"] == "444444"
339 assert results["password"] == "other_fjv&8password"
340 assert results["space_id"] == "11111111"
341 assert results["media"] == "SMS"
342 assert results["targets"] == ["2222222222"]
343 assert results["timeout"] == "30"
344 assert results["voice"] == ""
345 assert results["lang"] == ""
346 assert results["sender"] == ""
347
348 instance = NotifySFR(**results)
349 assert isinstance(instance, NotifySFR)
350 assert len(instance) == 1
351 assert instance.lang == "fr_FR"
352 assert instance.sender == ""
353 assert instance.media == "SMS"
354 assert isinstance(instance.targets, list)
355 assert instance.timeout == 30
356 assert instance.voice == "claire08s"
357 assert isinstance(instance.space_id, str)
358
359 response = instance.send(body="test")
360 assert response is False
361 assert mock_post.call_count == 1
362
363
364 @mock.patch("requests.post")
365 def test_plugin_sfr_notification_multiple_targets_all_ko(mock_post):
366 """NotifySFR() Notifications ko response."""
367 # Reset our object
368 mock_post.reset_mock()
369 # Prepare Mock
370 # Create a mock response object
371 response = mock.Mock()
372 response.status_code = requests.codes.ok
373 response.content = SFR_BAD_RESPONSE
374 mock_post.return_value = response
375
376 # Test "real" parameters
377 results = NotifySFR.parse_url(
378 "sfr://{}:other_fjv&8password@{}/?to={},{}&voice=laura8k".format(
379 "4" * 6, "1" * 8, "6" * 4, "8" * 4
380 )
381 )
382
383 assert isinstance(results, dict)
384 assert results["user"] == "444444"
385 assert results["password"] == "other_fjv&8password"
386 assert results["space_id"] == "11111111"
387 assert results["targets"] == ["6666", "8888"]
388 assert results["voice"] == "laura8k"
389 assert results["media"] == ""
390 assert results["timeout"] == ""
391 assert results["lang"] == ""
392 assert results["sender"] == ""
393
394 # No valid phone number provided
395 with pytest.raises(TypeError):
396 NotifySFR(**results)
397
398
399 @mock.patch("requests.post")
400 def test_plugin_sfr_notification_multiple_targets_one_ko(mock_post):
401 """NotifySFR() Notifications ko response."""
402 # Reset our object
403 mock_post.reset_mock()
404 # Prepare Mock
405 # Create a mock response object
406 response = mock.Mock()
407 response.status_code = requests.codes.ok
408 response.content = SFR_BAD_RESPONSE
409 mock_post.return_value = response
410
411 # Test "real" parameters
412 results = NotifySFR.parse_url(
413 "sfr://{}:&pass@{}/?to={},{}&media=SMSUnicodeLong&lang=en_US".format(
414 "4" * 6, "1" * 8, "6" * 10, "8" * 4
415 )
416 )
417
418 assert isinstance(results, dict)
419 assert results["user"] == "444444"
420 assert results["password"] == "&pass"
421 assert results["space_id"] == "11111111"
422 assert results["targets"] == ["6666666666", "8888"]
423 assert results["voice"] == ""
424 assert results["media"] == "SMSUnicodeLong"
425 assert results["timeout"] == ""
426 assert results["lang"] == "en_US"
427 assert results["sender"] == ""
428
429 instance = NotifySFR(**results)
430 assert isinstance(instance, NotifySFR)
431 assert len(instance) == 1
432 assert instance.lang == "en_US"
433 assert instance.sender == ""
434 assert instance.media == "SMSUnicodeLong"
435 assert isinstance(instance.targets, list)
436 assert instance.timeout == 2880
437 assert instance.voice == "claire08s"
438 assert isinstance(instance.space_id, str)
439
440 # One phone number failed to be parsed, therefore notify fails
441 response = instance.send(body="test")
442 assert response is False
443 assert mock_post.call_count == 1
444
445
446 @mock.patch("requests.post")
447 def test_plugin_sfr_notification_exceptions(mock_post):
448 """NotifySFR() Notifications exceptions."""
449 mock_post.reset_mock()
450 # Prepare Mock
451 # Create a mock response object
452 response = mock.Mock()
453 response.status_code = requests.codes.internal_server_error
454 response.content = SFR_GOOD_RESPONSE
455 mock_post.return_value = response
456
457 # Test "real" parameters
458 results = NotifySFR.parse_url(
459 "sfr://{}:str0*fn_ppw0rd@{}/{}".format(
460 "404ghwo89144", "9993384", "0959290404"
461 )
462 )
463
464 assert isinstance(results, dict)
465 assert results["user"] == "404ghwo89144"
466 assert results["password"] == "str0*fn_ppw0rd"
467 assert results["space_id"] == "9993384"
468 assert results["targets"] == ["0959290404"]
469 assert results["media"] == ""
470 assert results["timeout"] == ""
471 assert results["lang"] == ""
472 assert results["sender"] == ""
473
474 instance = NotifySFR(**results)
475 assert isinstance(instance, NotifySFR)
476 assert len(instance) == 1
477 assert instance.lang == "fr_FR"
478 assert instance.sender == ""
479 assert instance.media == "SMSUnicode"
480 assert isinstance(instance.targets, list)
481 assert instance.timeout == 2880
482 assert instance.voice == "claire08s"
483 assert isinstance(instance.space_id, str)
484
485 response = instance.send(body="test")
486 # Must return False
487 assert response is False
488 assert mock_post.call_count == 1
489
490 # Test invalid content returned by requests
491 mock_post.reset_mock()
492 response = mock.Mock()
493 response.status_code = requests.codes.ok
494 response.content = b"Invalid JSON Content"
495 mock_post.return_value = response
496
497 # Test "real" parameters
498 results = NotifySFR.parse_url(
499 "sfr://{}:str0*fn_ppw0rd@{}/{}".format(
500 "404ghwo89144", "9993384", "0959290404"
501 )
502 )
503
504 assert isinstance(results, dict)
505 assert results["user"] == "404ghwo89144"
506 assert results["password"] == "str0*fn_ppw0rd"
507 assert results["space_id"] == "9993384"
508 assert results["targets"] == ["0959290404"]
509 assert results["media"] == ""
510 assert results["timeout"] == ""
511 assert results["lang"] == ""
512 assert results["sender"] == ""
513
514 instance = NotifySFR(**results)
515 assert isinstance(instance, NotifySFR)
516 assert len(instance) == 1
517 assert instance.lang == "fr_FR"
518 assert instance.sender == ""
519 assert instance.media == "SMSUnicode"
520 assert isinstance(instance.targets, list)
521 assert instance.timeout == 2880
522 assert instance.voice == "claire08s"
523 assert isinstance(instance.space_id, str)
524
525 response = instance.send(body="test")
526 # Must return False
527 assert response is False
528 assert mock_post.call_count == 1
529
530
531 @mock.patch(
532 "requests.post",
533 side_effect=requests.RequestException("Connection error"),
534 )
535 def test_plugin_sfr_notification_exceptions_requests(mock_post):
536 """NotifySFR() Notifications requests exceptions."""
537 # Test requests socket error return
538 mock_post.reset_mock()
539 # Prepare Mock
540 # Create a mock response object
541 response = mock.Mock()
542 response.status_code = requests.codes.internal_server_error
543 response.content = b"Invalid content"
544 mock_post.return_value = response
545
546 # Test "real" parameters
547 results = NotifySFR.parse_url(
548 "sfr://{}:str0*fn_ppw0rd@{}/{}".format(
549 "404ghwo89144", "9993384", "0959290404"
550 )
551 )
552
553 assert isinstance(results, dict)
554 assert results["user"] == "404ghwo89144"
555 assert results["password"] == "str0*fn_ppw0rd"
556 assert results["space_id"] == "9993384"
557 assert results["targets"] == ["0959290404"]
558 assert results["media"] == ""
559 assert results["timeout"] == ""
560 assert results["lang"] == ""
561 assert results["sender"] == ""
562
563 instance = NotifySFR(**results)
564 assert isinstance(instance, NotifySFR)
565 assert len(instance) == 1
566 assert instance.lang == "fr_FR"
567 assert instance.sender == ""
568 assert instance.media == "SMSUnicode"
569 assert isinstance(instance.targets, list)
570 assert instance.timeout == 2880
571 assert instance.voice == "claire08s"
572 assert isinstance(instance.space_id, str)
573
574 response = instance.send(body="test")
575 # Must return False do to requests error
576 assert response is False
577 assert mock_post.call_count == 1
578
579
580 @mock.patch("requests.post")
581 def test_plugin_sfr_failure(mock_post):
582 """NotifySFR() Failure Cases."""
583 mock_post.reset_mock()
584 # Prepare Mock
585 # Create a mock response object
586 response = mock.Mock()
587 response.status_code = requests.codes.no_content
588 mock_post.return_value = response
589
590 # Invalid service_id
591 with pytest.raises(TypeError):
592 NotifySFR(
593 user=None,
594 password="service_password",
595 space_id=int("8" * 10),
596 targets=int("8" * 10),
597 )
598
599 # Invalid service_password
600 with pytest.raises(TypeError):
601 NotifySFR(
602 user="service_id",
603 password=None,
604 space_id=int("8" * 10),
605 targets=int("8" * 10),
606 )
607
608 # Invalid space_id
609 with pytest.raises(TypeError):
610 NotifySFR(
611 user="service_id",
612 password="service_password",
613 space_id=None,
614 targets=int("8" * 10),
615 )
616
617 # Invalid targets
618 with pytest.raises(TypeError):
619 NotifySFR(
620 user="service_id",
621 password="service_password",
622 space_id=int("8" * 10),
623 targets=None,
624 )