Debian Codegraph @

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