Debian Codegraph @

tests/test_plugin_voipms.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 dumps
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 import Apprise
39 from apprise.plugins.voipms import NotifyVoipms
40
41 logging.disable(logging.CRITICAL)
42
43 # Our Testing URLs
44 apprise_url_tests = (
45 (
46 "voipms://",
47 {
48 # No email/password specified
49 "instance": TypeError,
50 },
51 ),
52 (
53 "voipms://@:",
54 {
55 # Invalid url
56 "instance": TypeError,
57 },
58 ),
59 (
60 "voipms://{}/{}".format("[email protected]", "1" * 11),
61 {
62 # No password specified
63 "instance": TypeError,
64 },
65 ),
66 (
67 "voipms://:{}".format("password"),
68 {
69 # No email specified
70 "instance": TypeError,
71 },
72 ),
73 (
74 "voipms://{}:{}/{}".format("user@", "pass", "1" * 11),
75 {
76 # Check valid email
77 "instance": TypeError,
78 },
79 ),
80 (
81 "voipms://{password}:{email}".format(
82 email="[email protected]", password="password"
83 ),
84 {
85 # No from_phone specified
86 "instance": TypeError,
87 },
88 ),
89 # Invalid phone number test
90 (
91 "voipms://{password}:{email}/1613".format(
92 email="[email protected]", password="password"
93 ),
94 {
95 # Invalid phone number
96 "instance": TypeError,
97 },
98 ),
99 # Invalid country code phone number test
100 (
101 "voipms://{password}:{email}/01133122446688".format(
102 email="[email protected]", password="password"
103 ),
104 {
105 # Non North American phone number
106 "instance": TypeError,
107 },
108 ),
109 (
110 "voipms://{password}:{email}/{from_phone}/{targets}/".format(
111 email="[email protected]",
112 password="password",
113 from_phone="16134448888",
114 targets="/".join(["26134442222"]),
115 ),
116 {
117 # Invalid target phone number
118 "instance": NotifyVoipms,
119 "response": False,
120 "requests_response_code": 999,
121 },
122 ),
123 (
124 "voipms://{password}:{email}/{from_phone}".format(
125 email="[email protected]",
126 password="password",
127 from_phone="16138884444",
128 ),
129 {
130 "instance": NotifyVoipms,
131 # No targets specified
132 "response": False,
133 "requests_response_code": 999,
134 },
135 ),
136 (
137 "voipms://{password}:{email}/?from={from_phone}".format(
138 email="[email protected]",
139 password="password",
140 from_phone="16138884444",
141 ),
142 {
143 "instance": NotifyVoipms,
144 # No targets specified
145 "response": False,
146 "requests_response_code": 999,
147 },
148 ),
149 (
150 "voipms://{password}:{email}/{from_phone}/{targets}/".format(
151 email="[email protected]",
152 password="password",
153 from_phone="16138884444",
154 targets="/".join(["16134442222"]),
155 ),
156 {
157 # Valid
158 "instance": NotifyVoipms,
159 "response": True,
160 "privacy_url": "voipms://p...d:[email protected]/16...4",
161 },
162 ),
163 (
164 "voipms://{password}:{email}/{from_phone}/{targets}/".format(
165 email="[email protected]",
166 password="password",
167 from_phone="16138884444",
168 targets="/".join(["16134442222", "16134443333"]),
169 ),
170 {
171 # Valid multiple targets
172 "instance": NotifyVoipms,
173 "response": True,
174 "privacy_url": "voipms://p...d:[email protected]/16...4",
175 },
176 ),
177 (
178 "voipms://{password}:{email}/?from={from_phone}&to={targets}".format(
179 email="[email protected]",
180 password="password",
181 from_phone="16138884444",
182 targets="16134448888",
183 ),
184 {
185 # Valid
186 "instance": NotifyVoipms,
187 },
188 ),
189 (
190 "voipms://{password}:{email}/{from_phone}/{targets}/".format(
191 email="[email protected]",
192 password="password",
193 from_phone="16138884444",
194 targets="16134442222",
195 ),
196 {
197 "instance": NotifyVoipms,
198 # Throws a series of errors
199 "test_requests_exceptions": True,
200 },
201 ),
202 )
203
204
205 def test_plugin_voipms():
206 """NotifyVoipms() Apprise URLs."""
207
208 # Run our general tests
209 AppriseURLTester(tests=apprise_url_tests).run_all()
210
211
212 @mock.patch("requests.get")
213 def test_plugin_voipms_edge_cases(mock_get):
214 """NotifyVoipms() Edge Cases."""
215
216 # Prepare our response
217 response = requests.Request()
218 response.status_code = requests.codes.ok
219
220 # Prepare Mock
221 mock_get.return_value = response
222
223 # Initialize some generic (but valid) tokens
224 email = "[email protected]"
225 password = "password"
226 source = "+1 (555) 123-3456"
227 targets = "+1 (555) 123-9876"
228
229 # No email specified
230 with pytest.raises(TypeError):
231 NotifyVoipms(email=None, source=source)
232
233 # a error response is returned
234 response.status_code = 400
235 response.content = dumps(
236 {
237 "code": 21211,
238 "message": "Unable to process your request.",
239 }
240 )
241 mock_get.return_value = response
242
243 # Initialize our object
244 obj = Apprise.instantiate(
245 f"voipms://{password}:{email}/{source}/{targets}"
246 )
247
248 assert isinstance(obj, NotifyVoipms)
249
250 # We will fail with the above error code
251 assert obj.notify("title", "body", "info") is False
252
253
254 @mock.patch("requests.get")
255 def test_plugin_voipms_non_success_status(mock_get):
256 """NotifyVoipms() Non Success Status."""
257
258 # Prepare our response
259 response = requests.Request()
260 response.status_code = requests.codes.ok
261
262 # Prepare Mock
263 mock_get.return_value = response
264
265 # A 200 response is returned but non-success message
266 response.status_code = 200
267 response.content = dumps(
268 {
269 "status": "invalid_credentials",
270 "message": "Username or Password is incorrect",
271 }
272 )
273
274 obj = Apprise.instantiate(
275 "voipms://{password}:{email}/{source}/{targets}".format(
276 email="[email protected]",
277 password="badpassword",
278 source="16134448888",
279 targets="16134442222",
280 )
281 )
282
283 assert isinstance(obj, NotifyVoipms)
284
285 # We will fail with the above error code
286 assert obj.notify("title", "body", "info") is False
287
288 response.content = "{"
289 assert obj.send("title", "body") is False