Debian Codegraph @

tests/test_plugin_sms_manager.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 requests
34
35 from apprise import Apprise, NotifyType
36 from apprise.plugins.smsmanager import NotifySMSManager
37
38 logging.disable(logging.CRITICAL)
39
40 # Our Testing URLs
41 apprise_url_tests = (
42 (
43 "smsmgr://",
44 {
45 # Instantiated but no auth, so no otification can happen
46 "instance": TypeError,
47 },
48 ),
49 (
50 "smsmgr://:@/",
51 {
52 # invalid auth
53 "instance": TypeError
54 },
55 ),
56 (
57 "smsmgr://{}@{}".format("b" * 10, "3" * 5),
58 {
59 # invalid nubmer provided
60 "instance": NotifySMSManager,
61 # Expected notify() response because we have no one to notify
62 "notify_response": False,
63 },
64 ),
65 (
66 "smsmgr://{}@123/{}/abcd/+{}".format("z" * 10, "3" * 11, "4" * 11),
67 {
68 # includes a few invalid bits of info
69 "instance": NotifySMSManager,
70 "privacy_url": "smsmgr://z...z@33333333333/+44444444444",
71 },
72 ),
73 (
74 "smsmgr://{}@{}?batch=y".format("b" * 5, "4" * 11),
75 {
76 "instance": NotifySMSManager,
77 # Our expected url(privacy=True) startswith() response:
78 "privacy_url": "smsmgr://b...b@44444444444",
79 },
80 ),
81 # Test gateway group
82 (
83 "smsmgr://{}@{}?gateway=low".format("a" * 10, "1" * 11),
84 {
85 "instance": NotifySMSManager,
86 },
87 ),
88 (
89 "smsmgr://{}@{}?gateway=invalid".format("a" * 10, "1" * 11),
90 {
91 # invalid gatewwway
92 "instance": TypeError,
93 },
94 ),
95 (
96 "smsmgr://{}?key={}&from=user".format("1" * 11, "a" * 10),
97 {
98 # use get args to acomplish the same thing
99 "instance": NotifySMSManager,
100 },
101 ),
102 (
103 "smsmgr://_?to={},{}&key={}&sender={}".format(
104 "1" * 11, "2" * 11, "b" * 10, "5" * 13
105 ),
106 {
107 # use get args to acomplish the same thing
108 "instance": NotifySMSManager,
109 },
110 ),
111 (
112 "smsmgr://{}@{}".format("a" * 10, "1" * 11),
113 {
114 "instance": NotifySMSManager,
115 # throw a bizarre code forcing us to fail to look it up
116 "response": False,
117 "requests_response_code": 999,
118 },
119 ),
120 (
121 "smsmgr://{}@{}".format("a" * 10, "1" * 11),
122 {
123 "instance": NotifySMSManager,
124 # Throws a series of i/o exceptions with this flag
125 # is set and tests that we gracefully handle them
126 "test_requests_exceptions": True,
127 },
128 ),
129 )
130
131
132 def test_plugin_smsmgr_urls():
133 """NotifyTemplate() Apprise URLs."""
134
135 # Run our general tests
136 AppriseURLTester(tests=apprise_url_tests).run_all()
137
138
139 @mock.patch("requests.get")
140 def test_plugin_smsmgr_edge_cases(mock_get):
141 """NotifySMSManager() Edge Cases."""
142
143 # Initialize some generic (but valid) tokens
144 apikey = "my-api-key"
145 targets = [
146 "+1(555) 123-1234",
147 "1555 5555555",
148 # A garbage entry
149 "12",
150 # NOw a valid one because a group was implicit
151 "@12",
152 ]
153
154 # Prepare our response
155 response = requests.Request()
156 response.status_code = requests.codes.ok
157
158 # Prepare Mock
159 mock_get.return_value = response
160
161 # Instantiate our object
162 obj = Apprise.instantiate(
163 "smsmgr://{}@{}?batch=n".format(apikey, "/".join(targets))
164 )
165
166 assert (
167 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
168 is True
169 )
170
171 # We know there are 2 (valid) targets
172 assert len(obj) == 2
173
174 # Test our call count
175 assert mock_get.call_count == 2
176
177 # Test
178 details = mock_get.call_args_list[0]
179 payload = details[1]["params"]
180 assert payload["apikey"] == apikey
181 assert payload["gateway"] == "high"
182 assert payload["number"] == "+15551231234"
183 assert payload["message"] == "title\r\nbody"
184
185 details = mock_get.call_args_list[1]
186 payload = details[1]["params"]
187 assert payload["apikey"] == apikey
188 assert payload["gateway"] == "high"
189 assert payload["number"] == "15555555555"
190 assert payload["message"] == "title\r\nbody"
191
192 # Verify our URL looks good
193 assert obj.url().startswith(
194 "smsmgr://{}@{}".format(
195 apikey, "/".join(["+15551231234", "15555555555"])
196 )
197 )
198
199 assert "batch=no" in obj.url()
200
201 # Reset our mock object
202 mock_get.reset_mock()
203
204 # With our batch in place, our calculations are different
205 obj = Apprise.instantiate(
206 "smsmgr://{}@{}?batch=y".format(apikey, "/".join(targets))
207 )
208
209 # 2 phones were loaded but counted as 1 due to batch flag
210 assert len(obj) == 1
211
212 assert (
213 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
214 is True
215 )
216
217 # Test our call count (batched into 1)
218 assert mock_get.call_count == 1
219
220 details = mock_get.call_args_list[0]
221 payload = details[1]["params"]
222 assert payload["apikey"] == apikey
223 assert payload["gateway"] == "high"
224 assert payload["number"] == "+15551231234;15555555555"
225 assert payload["message"] == "title\r\nbody"