Debian Codegraph @

tests/test_plugin_vonage.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.plugins.vonage import NotifyVonage
39
40 logging.disable(logging.CRITICAL)
41
42 # Our Testing URLs
43 apprise_url_tests = (
44 (
45 "vonage://",
46 {
47 # No API Key specified
48 "instance": TypeError,
49 },
50 ),
51 (
52 "vonage://:@/",
53 {
54 # invalid Auth key
55 "instance": TypeError,
56 },
57 ),
58 (
59 "vonage://AC{}@12345678".format("a" * 8),
60 {
61 # Just a key provided
62 "instance": TypeError,
63 },
64 ),
65 (
66 "vonage://AC{}:{}@{}".format("a" * 8, "b" * 16, "3" * 9),
67 {
68 # key and secret provided and from but invalid from no
69 "instance": TypeError,
70 },
71 ),
72 (
73 "vonage://AC{}:{}@{}/?ttl=0".format("b" * 8, "c" * 16, "3" * 11),
74 {
75 # Invalid ttl defined
76 "instance": TypeError,
77 },
78 ),
79 (
80 "vonage://AC{}:{}@{}".format("d" * 8, "e" * 16, "a" * 11),
81 {
82 # Invalid source number
83 "instance": TypeError,
84 },
85 ),
86 (
87 "vonage://AC{}:{}@{}/123/{}/abcd/".format(
88 "f" * 8, "g" * 16, "3" * 11, "9" * 15
89 ),
90 {
91 # valid everything but target numbers
92 "instance": NotifyVonage,
93 # Our expected url(privacy=True) startswith() response:
94 "privacy_url": "vonage://A...f:****@",
95 },
96 ),
97 (
98 "vonage://AC{}:{}@{}".format("h" * 8, "i" * 16, "5" * 11),
99 {
100 # using phone no with no target - we text ourselves in
101 # this case
102 "instance": NotifyVonage,
103 },
104 ),
105 (
106 "vonage://_?key=AC{}&secret={}&from={}".format(
107 "a" * 8, "b" * 16, "5" * 11
108 ),
109 {
110 # use get args to acomplish the same thing
111 "instance": NotifyVonage,
112 },
113 ),
114 (
115 "vonage://_?key=AC{}&secret={}&source={}".format(
116 "a" * 8, "b" * 16, "5" * 11
117 ),
118 {
119 # use get args to acomplish the same thing (use source instead
120 # of from)
121 "instance": NotifyVonage,
122 },
123 ),
124 (
125 "vonage://_?key=AC{}&secret={}&from={}&to={}".format(
126 "a" * 8, "b" * 16, "5" * 11, "7" * 13
127 ),
128 {
129 # use to=
130 "instance": NotifyVonage,
131 },
132 ),
133 (
134 "vonage://AC{}:{}@{}".format("a" * 8, "b" * 16, "6" * 11),
135 {
136 "instance": NotifyVonage,
137 # throw a bizarre code forcing us to fail to look it up
138 "response": False,
139 "requests_response_code": 999,
140 },
141 ),
142 (
143 "vonage://AC{}:{}@{}".format("a" * 8, "b" * 16, "6" * 11),
144 {
145 "instance": NotifyVonage,
146 # Throws a series of i/o exceptions with this flag
147 # is set and tests that we gracefully handle them
148 "test_requests_exceptions": True,
149 },
150 ),
151 # Nexmo Backwards Support
152 (
153 "nexmo://",
154 {
155 # No API Key specified
156 "instance": TypeError,
157 },
158 ),
159 (
160 "nexmo://:@/",
161 {
162 # invalid Auth key
163 "instance": TypeError,
164 },
165 ),
166 (
167 "nexmo://AC{}@12345678".format("a" * 8),
168 {
169 # Just a key provided
170 "instance": TypeError,
171 },
172 ),
173 (
174 "nexmo://AC{}:{}@{}".format("a" * 8, "b" * 16, "3" * 9),
175 {
176 # key and secret provided and from but invalid from no
177 "instance": TypeError,
178 },
179 ),
180 (
181 "nexmo://AC{}:{}@{}/?ttl=0".format("b" * 8, "c" * 16, "3" * 11),
182 {
183 # Invalid ttl defined
184 "instance": TypeError,
185 },
186 ),
187 (
188 "nexmo://AC{}:{}@{}".format("d" * 8, "e" * 16, "a" * 11),
189 {
190 # Invalid source number
191 "instance": TypeError,
192 },
193 ),
194 (
195 "nexmo://AC{}:{}@{}/123/{}/abcd/".format(
196 "f" * 8, "g" * 16, "3" * 11, "9" * 15
197 ),
198 {
199 # valid everything but target numbers
200 "instance": NotifyVonage,
201 # Our expected url(privacy=True) startswith() response:
202 "privacy_url": "vonage://A...f:****@",
203 },
204 ),
205 (
206 "nexmo://AC{}:{}@{}".format("h" * 8, "i" * 16, "5" * 11),
207 {
208 # using phone no with no target - we text ourselves in
209 # this case
210 "instance": NotifyVonage,
211 },
212 ),
213 (
214 "nexmo://_?key=AC{}&secret={}&from={}".format(
215 "a" * 8, "b" * 16, "5" * 11
216 ),
217 {
218 # use get args to acomplish the same thing
219 "instance": NotifyVonage,
220 },
221 ),
222 (
223 "nexmo://_?key=AC{}&secret={}&source={}".format(
224 "a" * 8, "b" * 16, "5" * 11
225 ),
226 {
227 # use get args to acomplish the same thing (use source instead of
228 # from)
229 "instance": NotifyVonage,
230 },
231 ),
232 (
233 "nexmo://_?key=AC{}&secret={}&from={}&to={}".format(
234 "a" * 8, "b" * 16, "5" * 11, "7" * 13
235 ),
236 {
237 # use to=
238 "instance": NotifyVonage,
239 },
240 ),
241 (
242 "nexmo://AC{}:{}@{}".format("a" * 8, "b" * 16, "6" * 11),
243 {
244 "instance": NotifyVonage,
245 # throw a bizarre code forcing us to fail to look it up
246 "response": False,
247 "requests_response_code": 999,
248 },
249 ),
250 (
251 "nexmo://AC{}:{}@{}".format("a" * 8, "b" * 16, "6" * 11),
252 {
253 "instance": NotifyVonage,
254 # Throws a series of i/o exceptions with this flag
255 # is set and tests that we gracefully handle them
256 "test_requests_exceptions": True,
257 },
258 ),
259 )
260
261
262 def test_plugin_vonage_urls():
263 """NotifyVonage() Apprise URLs."""
264
265 # Run our general tests
266 AppriseURLTester(tests=apprise_url_tests).run_all()
267
268
269 @mock.patch("requests.post")
270 def test_plugin_vonage_edge_cases(mock_post):
271 """NotifyVonage() Edge Cases."""
272
273 # Prepare our response
274 response = requests.Request()
275 response.status_code = requests.codes.ok
276
277 # Prepare Mock
278 mock_post.return_value = response
279
280 # Initialize some generic (but valid) tokens
281 apikey = "AC{}".format("b" * 8)
282 secret = "{}".format("b" * 16)
283 source = "+1 (555) 123-3456"
284
285 # No apikey specified
286 with pytest.raises(TypeError):
287 NotifyVonage(apikey=None, secret=secret, source=source)
288
289 with pytest.raises(TypeError):
290 NotifyVonage(apikey=" ", secret=secret, source=source)
291
292 # No secret specified
293 with pytest.raises(TypeError):
294 NotifyVonage(apikey=apikey, secret=None, source=source)
295
296 with pytest.raises(TypeError):
297 NotifyVonage(apikey=apikey, secret=" ", source=source)
298
299 # a error response
300 response.status_code = 400
301 response.content = dumps(
302 {
303 "code": 21211,
304 "message": "The 'To' number +1234567 is not a valid phone number.",
305 }
306 )
307 mock_post.return_value = response
308
309 # Initialize our object
310 obj = NotifyVonage(apikey=apikey, secret=secret, source=source)
311
312 # We will fail with the above error code
313 assert obj.notify("title", "body", "info") is False