Debian Codegraph @

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