Debian Codegraph @

tests/test_plugin_serwersms.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 from json import loads
29
30 # Disable logging for a cleaner testing output
31 import logging
32 import os
33 from unittest import mock
34 from urllib.parse import urlparse
35
36 from helpers import AppriseURLTester
37 import pytest
38 import requests
39
40 from apprise import Apprise, AppriseAttachment, NotifyType
41 from apprise.plugins.serwersms import NotifySerwerSMS
42
43 # Attachment test fixtures directory
44 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
45
46 logging.disable(logging.CRITICAL)
47
48 # The API always returns 200 with a JSON body; use this as the mock response
49 # for successful cases so our success-field check passes.
50 SUCCESS_RESPONSE = {"success": True}
51 FAILURE_RESPONSE = {"success": False, "error": {"code": 101, "message": "err"}}
52 FAILURE_NO_ERROR = {"success": False}
53
54 # Our Testing URLs
55 apprise_url_tests = (
56 (
57 "serwersms://",
58 {
59 # Missing credentials
60 "instance": TypeError,
61 },
62 ),
63 (
64 "serwersms://:@/",
65 {
66 # Empty credentials
67 "instance": TypeError,
68 },
69 ),
70 (
71 "serwersms://user@SenderA/+48123456789",
72 {
73 # Missing password
74 "instance": TypeError,
75 },
76 ),
77 (
78 "serwersms://user:pass@/+48123456789",
79 {
80 # Missing sender (empty host)
81 "instance": TypeError,
82 },
83 ),
84 (
85 "serwersms://user:pass@!!invalid!!",
86 {
87 # Invalid sender name (fails regex)
88 "instance": TypeError,
89 },
90 ),
91 (
92 "serwersms://user:pass@SenderA",
93 {
94 # Valid credentials + sender but no targets
95 "instance": NotifySerwerSMS,
96 # No targets -- send() returns False
97 "notify_response": False,
98 },
99 ),
100 (
101 "serwersms://user:pass@SenderA/+48123456789",
102 {
103 # Single phone number target
104 "instance": NotifySerwerSMS,
105 "privacy_url": "serwersms://u...r:****@SenderA/+48123456789?",
106 "requests_response_text": SUCCESS_RESPONSE,
107 },
108 ),
109 (
110 "serwersms://user:pass@SenderA/%23456",
111 {
112 # Single group ID target (# encoded as %23)
113 "instance": NotifySerwerSMS,
114 "privacy_url": "serwersms://u...r:****@SenderA/%23456?",
115 "requests_response_text": SUCCESS_RESPONSE,
116 },
117 ),
118 (
119 "serwersms://user:pass@SenderA/+48123456789/%23789",
120 {
121 # Mixed: one phone + one group
122 "instance": NotifySerwerSMS,
123 "requests_response_text": SUCCESS_RESPONSE,
124 },
125 ),
126 (
127 "serwersms://user:pass@SenderA/+48111222333?to=%23999",
128 {
129 # Phone in path + group via ?to=
130 "instance": NotifySerwerSMS,
131 "requests_response_text": SUCCESS_RESPONSE,
132 },
133 ),
134 (
135 "serwersms://user:pass@SenderA/+48123456789",
136 {
137 # HTTP 500 error
138 "instance": NotifySerwerSMS,
139 "response": False,
140 "requests_response_code": requests.codes.internal_server_error,
141 },
142 ),
143 (
144 "serwersms://user:pass@SenderA/+48123456789",
145 {
146 # Unknown HTTP status code
147 "instance": NotifySerwerSMS,
148 "response": False,
149 "requests_response_code": 999,
150 },
151 ),
152 (
153 "serwersms://user:pass@SenderA/+48123456789",
154 {
155 # Connection exceptions
156 "instance": NotifySerwerSMS,
157 "test_requests_exceptions": True,
158 },
159 ),
160 (
161 "serwersms://user:pass@SenderA/%23123",
162 {
163 # Group target -- HTTP 500
164 "instance": NotifySerwerSMS,
165 "response": False,
166 "requests_response_code": requests.codes.internal_server_error,
167 },
168 ),
169 (
170 "serwersms://user:pass@SenderA/%23123",
171 {
172 # Group target -- connection exceptions
173 "instance": NotifySerwerSMS,
174 "test_requests_exceptions": True,
175 },
176 ),
177 )
178
179
180 def test_plugin_serwersms_urls():
181 """NotifySerwerSMS() Apprise URLs."""
182
183 # Run our general tests
184 AppriseURLTester(tests=apprise_url_tests).run_all()
185
186
187 @mock.patch("requests.post")
188 def test_plugin_serwersms_init(mock_post):
189 """NotifySerwerSMS() Initialisation tests."""
190
191 # Missing username
192 with pytest.raises(TypeError):
193 NotifySerwerSMS(sender="SenderA", targets=["+48123456789"])
194
195 # Missing password
196 with pytest.raises(TypeError):
197 NotifySerwerSMS(
198 user="user",
199 sender="SenderA",
200 targets=["+48123456789"],
201 )
202
203 # Missing sender
204 with pytest.raises(TypeError):
205 NotifySerwerSMS(
206 user="user",
207 password="pass",
208 targets=["+48123456789"],
209 )
210
211 # Invalid sender (too long - >11 chars)
212 with pytest.raises(TypeError):
213 NotifySerwerSMS(
214 user="user",
215 password="pass",
216 sender="A" * 12,
217 targets=["+48123456789"],
218 )
219
220 # Invalid sender (starts with non-alphanumeric)
221 with pytest.raises(TypeError):
222 NotifySerwerSMS(
223 user="user",
224 password="pass",
225 sender="!invalid",
226 targets=["+48123456789"],
227 )
228
229 # Valid instance with no targets -- loads OK; __len__ returns minimum 1
230 obj = NotifySerwerSMS(
231 user="user",
232 password="pass",
233 sender="SenderA",
234 )
235 assert obj is not None
236 assert len(obj) == 1
237
238 # Valid instance with a phone target
239 obj = NotifySerwerSMS(
240 user="user",
241 password="pass",
242 sender="SenderA",
243 targets=["+48123456789"],
244 )
245 assert len(obj.target_phones) == 1
246 assert len(obj.target_groups) == 0
247
248 # Valid instance with a group target (#123)
249 obj = NotifySerwerSMS(
250 user="user",
251 password="pass",
252 sender="SenderA",
253 targets=["#456"],
254 )
255 assert len(obj.target_phones) == 0
256 assert len(obj.target_groups) == 1
257 assert obj.target_groups[0] == "456"
258
259 # Group target with %23 prefix is also accepted
260 obj = NotifySerwerSMS(
261 user="user",
262 password="pass",
263 sender="SenderA",
264 targets=["%23789"],
265 )
266 assert len(obj.target_groups) == 1
267 assert obj.target_groups[0] == "789"
268
269 # Invalid targets are stored in invalid_targets
270 obj = NotifySerwerSMS(
271 user="user",
272 password="pass",
273 sender="SenderA",
274 targets=["notaphone", "+48123456789"],
275 )
276 assert len(obj.invalid_targets) == 1
277 assert len(obj.target_phones) == 1
278
279
280 @mock.patch("requests.post")
281 def test_plugin_serwersms_send_phone(mock_post):
282 """NotifySerwerSMS() send to phone numbers."""
283
284 # Prepare our success response
285 mock_post.return_value = mock.Mock()
286 mock_post.return_value.status_code = requests.codes.ok
287 mock_post.return_value.content = b'{"success": true}'
288
289 obj = NotifySerwerSMS(
290 user="myuser",
291 password="mypass",
292 sender="AppName",
293 targets=["+48111222333", "+48444555666"],
294 )
295
296 # Two phone targets means two POST calls
297 assert obj.send(body="Hello", title="Title") is True
298 assert mock_post.call_count == 2
299
300 # Verify request URL
301 call_url = mock_post.call_args_list[0][0][0]
302 assert urlparse(call_url).hostname == "api2.serwersms.pl"
303
304 # Verify payload contents for first call
305 payload = loads(mock_post.call_args_list[0][1]["data"])
306 assert payload["username"] == "myuser"
307 assert payload["password"] == "mypass"
308 assert payload["sender"] == "AppName"
309 assert payload["text"] == "Hello"
310 assert payload["phone"] == "+48111222333"
311 assert "group_id" not in payload
312
313 # Verify second call targets the second number
314 payload2 = loads(mock_post.call_args_list[1][1]["data"])
315 assert payload2["phone"] == "+48444555666"
316
317
318 @mock.patch("requests.post")
319 def test_plugin_serwersms_send_group(mock_post):
320 """NotifySerwerSMS() send to contact groups."""
321
322 # Prepare our success response
323 mock_post.return_value = mock.Mock()
324 mock_post.return_value.status_code = requests.codes.ok
325 mock_post.return_value.content = b'{"success": true}'
326
327 obj = NotifySerwerSMS(
328 user="myuser",
329 password="mypass",
330 sender="AppName",
331 targets=["#100", "#200"],
332 )
333
334 # Two group targets means two POST calls
335 assert obj.send(body="Hello") is True
336 assert mock_post.call_count == 2
337
338 # Verify payload for group send
339 payload = loads(mock_post.call_args_list[0][1]["data"])
340 assert payload["group_id"] == "100"
341 assert "phone" not in payload
342
343 payload2 = loads(mock_post.call_args_list[1][1]["data"])
344 assert payload2["group_id"] == "200"
345
346
347 @mock.patch("requests.post")
348 def test_plugin_serwersms_send_mixed(mock_post):
349 """NotifySerwerSMS() send to phones and groups together."""
350
351 mock_post.return_value = mock.Mock()
352 mock_post.return_value.status_code = requests.codes.ok
353 mock_post.return_value.content = b'{"success": true}'
354
355 obj = NotifySerwerSMS(
356 user="u",
357 password="p",
358 sender="Sender",
359 targets=["+48123456789", "#500"],
360 )
361
362 # One phone + one group = two calls
363 assert obj.send(body="msg") is True
364 assert mock_post.call_count == 2
365
366 # First call: phone
367 p1 = loads(mock_post.call_args_list[0][1]["data"])
368 assert "phone" in p1
369
370 # Second call: group
371 p2 = loads(mock_post.call_args_list[1][1]["data"])
372 assert "group_id" in p2
373
374
375 @mock.patch("requests.post")
376 def test_plugin_serwersms_no_targets(mock_post):
377 """NotifySerwerSMS() with no targets returns False without calling API."""
378
379 obj = NotifySerwerSMS(
380 user="u",
381 password="p",
382 sender="Sender",
383 )
384 assert obj.send(body="msg") is False
385 assert mock_post.call_count == 0
386
387
388 @mock.patch("requests.post")
389 def test_plugin_serwersms_http_error(mock_post):
390 """NotifySerwerSMS() handles HTTP errors gracefully."""
391
392 mock_post.return_value = mock.Mock()
393 mock_post.return_value.status_code = requests.codes.internal_server_error
394 mock_post.return_value.content = b""
395
396 obj = NotifySerwerSMS(
397 user="u",
398 password="p",
399 sender="Sender",
400 targets=["+48123456789", "#100"],
401 )
402
403 # Both calls fail due to HTTP 500
404 assert obj.send(body="msg") is False
405 assert mock_post.call_count == 2
406
407
408 @mock.patch("requests.post")
409 def test_plugin_serwersms_http_error_partial(mock_post):
410 """NotifySerwerSMS() returns False on any partial HTTP failure."""
411
412 # First call succeeds, second fails
413 good = mock.Mock()
414 good.status_code = requests.codes.ok
415 good.content = b'{"success": true}'
416
417 bad = mock.Mock()
418 bad.status_code = 999
419 bad.content = b""
420
421 mock_post.side_effect = [good, bad]
422
423 obj = NotifySerwerSMS(
424 user="u",
425 password="p",
426 sender="Sender",
427 targets=["+48111000000", "+48222000000"],
428 )
429
430 # One success, one failure -> overall False
431 assert obj.send(body="msg") is False
432 assert mock_post.call_count == 2
433
434
435 @mock.patch("requests.post")
436 def test_plugin_serwersms_request_exception(mock_post):
437 """NotifySerwerSMS() handles RequestException gracefully."""
438
439 mock_post.side_effect = requests.RequestException("Connection refused")
440
441 obj = NotifySerwerSMS(
442 user="u",
443 password="p",
444 sender="Sender",
445 targets=["+48123456789"],
446 )
447
448 assert obj.send(body="msg") is False
449
450
451 @mock.patch("requests.post")
452 def test_plugin_serwersms_request_exception_group(mock_post):
453 """NotifySerwerSMS() handles RequestException on group send."""
454
455 mock_post.side_effect = requests.RequestException("timeout")
456
457 obj = NotifySerwerSMS(
458 user="u",
459 password="p",
460 sender="Sender",
461 targets=["#100"],
462 )
463
464 assert obj.send(body="msg") is False
465
466
467 @mock.patch("requests.post")
468 def test_plugin_serwersms_api_failure_with_error(mock_post):
469 """NotifySerwerSMS() handles API success=false with error object."""
470
471 mock_post.return_value = mock.Mock()
472 mock_post.return_value.status_code = requests.codes.ok
473 mock_post.return_value.content = (
474 b'{"success": false, "error": {"code": 101, "message": "Auth fail"}}'
475 )
476
477 obj = NotifySerwerSMS(
478 user="u",
479 password="p",
480 sender="Sender",
481 targets=["+48123456789"],
482 )
483
484 assert obj.send(body="msg") is False
485
486
487 @mock.patch("requests.post")
488 def test_plugin_serwersms_api_failure_no_error(mock_post):
489 """NotifySerwerSMS() handles API success=false without error detail."""
490
491 mock_post.return_value = mock.Mock()
492 mock_post.return_value.status_code = requests.codes.ok
493 mock_post.return_value.content = b'{"success": false}'
494
495 obj = NotifySerwerSMS(
496 user="u",
497 password="p",
498 sender="Sender",
499 targets=["#200"],
500 )
501
502 assert obj.send(body="msg") is False
503
504
505 @mock.patch("requests.post")
506 def test_plugin_serwersms_api_bad_json(mock_post):
507 """NotifySerwerSMS() handles unparseable JSON response body."""
508
509 mock_post.return_value = mock.Mock()
510 mock_post.return_value.status_code = requests.codes.ok
511 mock_post.return_value.content = b"not-json"
512
513 obj = NotifySerwerSMS(
514 user="u",
515 password="p",
516 sender="Sender",
517 targets=["+48123456789"],
518 )
519
520 # Bad JSON treated as failure (success field absent -> falsy)
521 assert obj.send(body="msg") is False
522
523
524 @mock.patch("requests.post")
525 def test_plugin_serwersms_api_null_json(mock_post):
526 """NotifySerwerSMS() handles null JSON response body."""
527
528 mock_post.return_value = mock.Mock()
529 mock_post.return_value.status_code = requests.codes.ok
530 # JSON null parses to Python None; plugin normalises to {}
531 mock_post.return_value.content = b"null"
532
533 obj = NotifySerwerSMS(
534 user="u",
535 password="p",
536 sender="Sender",
537 targets=["+48123456789"],
538 )
539
540 assert obj.send(body="msg") is False
541
542
543 @mock.patch("requests.post")
544 def test_plugin_serwersms_url(mock_post):
545 """NotifySerwerSMS() url() and url_identifier() tests."""
546
547 obj = NotifySerwerSMS(
548 user="myuser",
549 password="mypass",
550 sender="AppName",
551 targets=["+48123456789", "#100"],
552 )
553
554 # url() produces a string
555 full_url = obj.url()
556 assert full_url.startswith("serwersms://")
557 assert "myuser" in full_url
558 assert "mypass" in full_url
559 assert "AppName" in full_url
560
561 # Privacy URL masks the password and abbreviates the user
562 priv = obj.url(privacy=True)
563 assert "mypass" not in priv
564 assert "myuser" not in priv
565 assert "AppName" in priv
566
567 # url_identifier excludes targets
568 ident = obj.url_identifier
569 assert "serwersms" in ident
570 assert "myuser" in ident
571 assert "AppName" in ident
572 assert "+48123456789" not in str(ident)
573
574
575 @mock.patch("requests.post")
576 def test_plugin_serwersms_url_roundtrip(mock_post):
577 """NotifySerwerSMS() round-trips through url() -> parse_url()."""
578
579 obj = NotifySerwerSMS(
580 user="myuser",
581 password="mypass",
582 sender="AppName",
583 targets=["+48123456789", "#100"],
584 )
585
586 # Parse the generated URL back into a new object
587 result = NotifySerwerSMS.parse_url(obj.url())
588 assert result is not None
589
590 obj2 = NotifySerwerSMS(**result)
591 assert obj.url_identifier == obj2.url_identifier
592 assert len(obj.target_phones) == len(obj2.target_phones)
593 assert len(obj.target_groups) == len(obj2.target_groups)
594
595
596 @mock.patch("requests.post")
597 def test_plugin_serwersms_url_invalid_targets(mock_post):
598 """NotifySerwerSMS() preserves invalid targets in url() round-trip."""
599
600 obj = NotifySerwerSMS(
601 user="u",
602 password="p",
603 sender="S",
604 targets=["+48123456789", "badtarget"],
605 )
606
607 assert len(obj.invalid_targets) == 1
608 assert len(obj.target_phones) == 1
609
610 # The generated URL must include the invalid target so it survives
611 # a round-trip without silent loss.
612 url = obj.url()
613 assert "badtarget" in url
614
615
616 def test_plugin_serwersms_parse_url():
617 """NotifySerwerSMS() parse_url() edge cases."""
618
619 # Standard URL with phone and group
620 result = NotifySerwerSMS.parse_url(
621 "serwersms://user:pass@SenderA/+48123456789/%23100"
622 )
623 assert result is not None
624 assert result["user"] == "user"
625 assert result["password"] == "pass"
626 assert result["sender"] == "SenderA"
627 # Path entries come back decoded; +48123456789 and #100
628 assert "+48123456789" in result["targets"]
629 assert "#100" in result["targets"]
630
631 # ?to= query param adds targets
632 result = NotifySerwerSMS.parse_url(
633 "serwersms://user:pass@SenderA?to=%2348123456789"
634 )
635 assert result is not None
636 assert len(result["targets"]) >= 1
637
638 # ?from= sets the sender
639 result = NotifySerwerSMS.parse_url(
640 "serwersms://user:pass@SenderA/+48111?from=NewSender"
641 )
642 assert result["sender"] == "NewSender"
643
644 # ?sender= sets the sender (takes priority over ?from=)
645 result = NotifySerwerSMS.parse_url(
646 "serwersms://user:pass@SenderA/+48111?from=Old&sender=New"
647 )
648 assert result["sender"] == "New"
649
650 # A URL with verify_host=False accepts empty host; verify None is returned
651 # for a genuinely unparseable URL (non-string)
652 assert NotifySerwerSMS.parse_url(None) is None
653
654
655 @mock.patch("requests.post")
656 def test_plugin_serwersms_apprise_integration(mock_post):
657 """NotifySerwerSMS() integration with Apprise object."""
658
659 mock_post.return_value = mock.Mock()
660 mock_post.return_value.status_code = requests.codes.ok
661 mock_post.return_value.content = b'{"success": true}'
662
663 aobj = Apprise()
664 assert aobj.add("serwersms://user:pass@AppName/+48123456789")
665
666 assert aobj.notify(
667 body="Integration test",
668 title="Title",
669 notify_type=NotifyType.INFO,
670 )
671 assert mock_post.call_count == 1
672
673
674 @mock.patch("requests.post")
675 def test_plugin_serwersms_mms_phone_success(mock_post):
676 """NotifySerwerSMS() MMS to phone -- success path."""
677
678 mock_post.return_value = mock.Mock()
679 mock_post.return_value.status_code = requests.codes.ok
680 mock_post.return_value.content = b'{"success": true}'
681
682 obj = NotifySerwerSMS(
683 user="myuser",
684 password="mypass",
685 sender="AppName",
686 targets=["+48123456789"],
687 )
688
689 attach = AppriseAttachment.instantiate(
690 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
691 )
692 assert obj.notify(body="MMS test", attach=attach) is True
693 assert mock_post.call_count == 1
694
695 # Confirm multipart files kwarg was set (MMS endpoint used)
696 call_kwargs = mock_post.call_args[1]
697 assert call_kwargs["files"] is not None
698
699 # Confirm form fields (not JSON body)
700 assert call_kwargs["data"]["phone"] == "+48123456789"
701 assert call_kwargs["data"]["sender"] == "AppName"
702 assert call_kwargs["data"]["text"] == "MMS test"
703
704 # Confirm MMS endpoint was used
705 call_url = mock_post.call_args[0][0]
706 assert "send_mms" in call_url
707
708 # Confirm Content-Type was not forced (requests sets multipart boundary)
709 assert "Content-Type" not in call_kwargs["headers"]
710
711
712 @mock.patch("requests.post")
713 def test_plugin_serwersms_mms_group_success(mock_post):
714 """NotifySerwerSMS() MMS to group -- success path."""
715
716 mock_post.return_value = mock.Mock()
717 mock_post.return_value.status_code = requests.codes.ok
718 mock_post.return_value.content = b'{"success": true}'
719
720 obj = NotifySerwerSMS(
721 user="u",
722 password="p",
723 sender="Sender",
724 targets=["#500"],
725 )
726
727 attach = AppriseAttachment.instantiate(
728 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
729 )
730 assert obj.notify(body="Group MMS", attach=attach) is True
731 assert mock_post.call_count == 1
732
733 # Confirm group_id was sent as a form field
734 call_kwargs = mock_post.call_args[1]
735 assert call_kwargs["data"]["group_id"] == "500"
736 assert "phone" not in call_kwargs["data"]
737
738
739 @mock.patch("requests.post")
740 def test_plugin_serwersms_mms_guard1_inaccessible(mock_post):
741 """NotifySerwerSMS() MMS -- guard 1 inaccessible attachment."""
742
743 mock_post.return_value = mock.Mock()
744 mock_post.return_value.status_code = requests.codes.ok
745 mock_post.return_value.content = b'{"success": true}'
746
747 obj = NotifySerwerSMS(
748 user="u",
749 password="p",
750 sender="Sender",
751 targets=["+48123456789"],
752 )
753
754 attach = AppriseAttachment.instantiate(
755 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
756 )
757
758 # Simulate file being inaccessible (bool(attachment) -> False)
759 with mock.patch("os.path.isfile", return_value=False):
760 assert obj.notify(body="Test", attach=attach) is False
761
762 # No HTTP call should have been made
763 assert mock_post.call_count == 0
764
765
766 @mock.patch("requests.post")
767 def test_plugin_serwersms_mms_guard2_oserror(mock_post):
768 """NotifySerwerSMS() MMS -- guard 2 OSError on open."""
769
770 mock_post.return_value = mock.Mock()
771 mock_post.return_value.status_code = requests.codes.ok
772 mock_post.return_value.content = b'{"success": true}'
773
774 obj = NotifySerwerSMS(
775 user="u",
776 password="p",
777 sender="Sender",
778 targets=["+48123456789"],
779 )
780
781 attach = AppriseAttachment.instantiate(
782 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
783 )
784
785 with mock.patch("builtins.open", side_effect=OSError("IO fail")):
786 assert obj.notify(body="Test", attach=attach) is False
787
788 # No HTTP call should have been made
789 assert mock_post.call_count == 0
790
791
792 @mock.patch("requests.post")
793 def test_plugin_serwersms_mms_http_error(mock_post):
794 """NotifySerwerSMS() MMS -- HTTP error response."""
795
796 mock_post.return_value = mock.Mock()
797 mock_post.return_value.status_code = requests.codes.internal_server_error
798 mock_post.return_value.content = b""
799
800 obj = NotifySerwerSMS(
801 user="u",
802 password="p",
803 sender="Sender",
804 targets=["+48123456789"],
805 )
806
807 attach = AppriseAttachment.instantiate(
808 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
809 )
810 assert obj.notify(body="Test", attach=attach) is False
811 assert mock_post.call_count == 1
812
813
814 @mock.patch("requests.post")
815 def test_plugin_serwersms_mms_api_failure_with_error(mock_post):
816 """NotifySerwerSMS() MMS -- API success=false with error object."""
817
818 mock_post.return_value = mock.Mock()
819 mock_post.return_value.status_code = requests.codes.ok
820 mock_post.return_value.content = (
821 b'{"success": false, "error": {"code": 201, "message": "MMS fail"}}'
822 )
823
824 obj = NotifySerwerSMS(
825 user="u",
826 password="p",
827 sender="Sender",
828 targets=["+48123456789"],
829 )
830
831 attach = AppriseAttachment.instantiate(
832 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
833 )
834 assert obj.notify(body="Test", attach=attach) is False
835
836
837 @mock.patch("requests.post")
838 def test_plugin_serwersms_mms_api_failure_no_error(mock_post):
839 """NotifySerwerSMS() MMS -- API success=false without error detail."""
840
841 mock_post.return_value = mock.Mock()
842 mock_post.return_value.status_code = requests.codes.ok
843 mock_post.return_value.content = b'{"success": false}'
844
845 obj = NotifySerwerSMS(
846 user="u",
847 password="p",
848 sender="Sender",
849 targets=["+48123456789"],
850 )
851
852 attach = AppriseAttachment.instantiate(
853 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
854 )
855 assert obj.notify(body="Test", attach=attach) is False
856
857
858 @mock.patch("requests.post")
859 def test_plugin_serwersms_mms_bad_json(mock_post):
860 """NotifySerwerSMS() MMS -- unparseable JSON response."""
861
862 mock_post.return_value = mock.Mock()
863 mock_post.return_value.status_code = requests.codes.ok
864 mock_post.return_value.content = b"not-json"
865
866 obj = NotifySerwerSMS(
867 user="u",
868 password="p",
869 sender="Sender",
870 targets=["+48123456789"],
871 )
872
873 attach = AppriseAttachment.instantiate(
874 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
875 )
876 # Bad JSON -> content = {} -> success absent -> False
877 assert obj.notify(body="Test", attach=attach) is False
878
879
880 @mock.patch("requests.post")
881 def test_plugin_serwersms_mms_null_json(mock_post):
882 """NotifySerwerSMS() MMS -- null JSON response body."""
883
884 mock_post.return_value = mock.Mock()
885 mock_post.return_value.status_code = requests.codes.ok
886 # JSON null parses to Python None; plugin normalises to {}
887 mock_post.return_value.content = b"null"
888
889 obj = NotifySerwerSMS(
890 user="u",
891 password="p",
892 sender="Sender",
893 targets=["+48123456789"],
894 )
895
896 attach = AppriseAttachment.instantiate(
897 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
898 )
899 assert obj.notify(body="Test", attach=attach) is False
900
901
902 @mock.patch("requests.post")
903 def test_plugin_serwersms_mms_request_exception(mock_post):
904 """NotifySerwerSMS() MMS -- RequestException guard."""
905
906 mock_post.side_effect = requests.RequestException("Connection refused")
907
908 obj = NotifySerwerSMS(
909 user="u",
910 password="p",
911 sender="Sender",
912 targets=["+48123456789"],
913 )
914
915 attach = AppriseAttachment.instantiate(
916 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
917 )
918 assert obj.notify(body="Test", attach=attach) is False