Debian Codegraph @

tests/test_plugin_apprise_api.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 from json import loads
30 import logging
31 import os
32 from unittest import mock
33
34 from helpers import AppriseURLTester
35 import requests
36
37 from apprise import Apprise, AppriseAttachment, NotifyType
38 from apprise.plugins.apprise_api import NotifyAppriseAPI
39
40 logging.disable(logging.CRITICAL)
41
42 # Attachment Directory
43 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
44
45 # Our Testing URLs
46 apprise_url_tests = (
47 (
48 "apprise://",
49 {
50 # invalid url (not complete)
51 "instance": None,
52 },
53 ),
54 # A a bad url
55 (
56 "apprise://:@/",
57 {
58 "instance": None,
59 },
60 ),
61 # No token specified
62 (
63 "apprise://localhost",
64 {
65 "instance": TypeError,
66 },
67 ),
68 # invalid token
69 (
70 "apprise://localhost/!",
71 {
72 "instance": TypeError,
73 },
74 ),
75 # No token specified (whitespace is trimmed)
76 (
77 "apprise://localhost/%%20",
78 {
79 "instance": TypeError,
80 },
81 ),
82 # A valid URL with Token
83 (
84 "apprise://localhost/%s" % ("a" * 32),
85 {
86 "instance": NotifyAppriseAPI,
87 # Our expected url(privacy=True) startswith() response:
88 "privacy_url": "apprise://localhost/a...a/",
89 "force_debug": True,
90 },
91 ),
92 # A valid URL with long Token
93 (
94 "apprise://localhost/%s" % ("a" * 128),
95 {
96 "instance": NotifyAppriseAPI,
97 # Our expected url(privacy=True) startswith() response:
98 "privacy_url": "apprise://localhost/a...a/",
99 },
100 ),
101 # A valid URL with Token (using port)
102 (
103 "apprise://localhost:8080/%s" % ("b" * 32),
104 {
105 "instance": NotifyAppriseAPI,
106 # Our expected url(privacy=True) startswith() response:
107 "privacy_url": "apprise://localhost:8080/b...b/",
108 },
109 ),
110 # A secure (https://) reference
111 (
112 "apprises://localhost/%s" % ("c" * 32),
113 {
114 "instance": NotifyAppriseAPI,
115 # Our expected url(privacy=True) startswith() response:
116 "privacy_url": "apprises://localhost/c...c/",
117 },
118 ),
119 # Native URL suport (https)
120 (
121 "https://example.com/path/notify/%s" % ("d" * 32),
122 {
123 "instance": NotifyAppriseAPI,
124 # Our expected url(privacy=True) startswith() response:
125 "privacy_url": "apprises://example.com/path/d...d/",
126 },
127 ),
128 # Native URL suport (http)
129 (
130 "http://example.com/notify/%s" % ("d" * 32),
131 {
132 "instance": NotifyAppriseAPI,
133 # Our expected url(privacy=True) startswith() response:
134 "privacy_url": "apprise://example.com/d...d/",
135 },
136 ),
137 # support to= keyword
138 (
139 "apprises://localhost/?to=%s" % ("e" * 32),
140 {
141 "instance": NotifyAppriseAPI,
142 "privacy_url": "apprises://localhost/e...e/",
143 },
144 ),
145 # support token= keyword (even when passed with to=, token over-rides)
146 (
147 "apprise://localhost/?token={}&to={}".format("f" * 32, "abcd"),
148 {
149 "instance": NotifyAppriseAPI,
150 "privacy_url": "apprise://localhost/f...f/",
151 },
152 ),
153 # Test tags
154 (
155 "apprise://localhost/?token={}&tags=admin,team".format("abcd"),
156 {
157 "instance": NotifyAppriseAPI,
158 "privacy_url": "apprise://localhost/a...d/",
159 },
160 ),
161 # Test Format string
162 (
163 "apprise://user@localhost/mytoken0/?format=markdown",
164 {
165 "instance": NotifyAppriseAPI,
166 "privacy_url": "apprise://user@localhost/m...0/",
167 },
168 ),
169 (
170 "apprise://user@localhost/mytoken1/",
171 {
172 "instance": NotifyAppriseAPI,
173 "privacy_url": "apprise://user@localhost/m...1/",
174 },
175 ),
176 (
177 "apprise://localhost:8080/mytoken/",
178 {
179 "instance": NotifyAppriseAPI,
180 },
181 ),
182 (
183 "apprise://user:pass@localhost:8080/mytoken2/",
184 {
185 "instance": NotifyAppriseAPI,
186 "privacy_url": "apprise://user:****@localhost:8080/m...2/",
187 },
188 ),
189 (
190 "apprises://localhost/mytoken/",
191 {
192 "instance": NotifyAppriseAPI,
193 },
194 ),
195 (
196 "apprises://user:pass@localhost/mytoken3/",
197 {
198 "instance": NotifyAppriseAPI,
199 # Our expected url(privacy=True) startswith() response:
200 "privacy_url": "apprises://user:****@localhost/m...3/",
201 },
202 ),
203 (
204 "apprises://localhost:8080/mytoken4/",
205 {
206 "instance": NotifyAppriseAPI,
207 # Our expected url(privacy=True) startswith() response:
208 "privacy_url": "apprises://localhost:8080/m...4/",
209 },
210 ),
211 (
212 "apprises://localhost:8080/abc123/?method=json",
213 {
214 "instance": NotifyAppriseAPI,
215 # Our expected url(privacy=True) startswith() response:
216 "privacy_url": "apprises://localhost:8080/a...3/",
217 },
218 ),
219 (
220 "apprises://localhost:8080/abc123/?method=form",
221 {
222 "instance": NotifyAppriseAPI,
223 # Our expected url(privacy=True) startswith() response:
224 "privacy_url": "apprises://localhost:8080/a...3/",
225 },
226 ),
227 # Invalid method specified
228 (
229 "apprises://localhost:8080/abc123/?method=invalid",
230 {
231 "instance": TypeError,
232 },
233 ),
234 (
235 "apprises://user:password@localhost:8080/mytoken5/",
236 {
237 "instance": NotifyAppriseAPI,
238 # Our expected url(privacy=True) startswith() response:
239 "privacy_url": "apprises://user:****@localhost:8080/m...5/",
240 },
241 ),
242 (
243 "apprises://localhost:8080/path?+HeaderKey=HeaderValue",
244 {
245 "instance": NotifyAppriseAPI,
246 },
247 ),
248 (
249 "apprise://localhost/%s" % ("a" * 32),
250 {
251 "instance": NotifyAppriseAPI,
252 # force a failure
253 "response": False,
254 "requests_response_code": requests.codes.internal_server_error,
255 },
256 ),
257 (
258 "apprise://localhost/%s" % ("a" * 32),
259 {
260 "instance": NotifyAppriseAPI,
261 # throw a bizarre code forcing us to fail to look it up
262 "response": False,
263 "requests_response_code": 999,
264 },
265 ),
266 (
267 "apprise://localhost/%s" % ("a" * 32),
268 {
269 "instance": NotifyAppriseAPI,
270 # Throws a series of i/o exceptions with this flag
271 # is set and tests that we gracefully handle them
272 "test_requests_exceptions": True,
273 },
274 ),
275 )
276
277
278 def test_plugin_apprise_api_urls():
279 """NotifyAppriseAPI() General Checks."""
280
281 # Run our general tests
282 AppriseURLTester(tests=apprise_url_tests).run_all()
283
284
285 @mock.patch("requests.post")
286 def test_notify_apprise_api_payload_check(mock_post):
287 """NotifyAppriseAPI() payload checks"""
288
289 okay_response = requests.Request()
290
291 okay_response.status_code = requests.codes.ok
292 okay_response.content = ""
293
294 # Assign our mock object our return value
295 mock_post.return_value = okay_response
296
297 obj = Apprise.instantiate("apprise://user@localhost/mytoken1/?method=form")
298 assert isinstance(obj, NotifyAppriseAPI)
299
300 # Test URL with Attachment
301 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
302 attach = AppriseAttachment(path)
303 assert (
304 obj.notify(
305 body="body",
306 title="title",
307 notify_type=NotifyType.INFO,
308 attach=attach,
309 )
310 is True
311 )
312
313 details = mock_post.call_args_list[0]
314 assert details[0][0] == "http://localhost/notify/mytoken1"
315 assert details[1]["data"] == {
316 "title": "title",
317 "body": "body",
318 "type": "info",
319 "format": "text",
320 }
321 assert "X-Apprise-ID" in details[1]["headers"]
322 assert details[1]["headers"].get("User-Agent") == "Apprise"
323 assert details[1]["headers"].get("Accept") == "application/json"
324 assert details[1]["headers"].get("X-Apprise-Recursion-Count") == "1"
325
326 mock_post.reset_mock()
327
328 obj = Apprise.instantiate("apprise://user@localhost/mytoken1/?method=json")
329 assert isinstance(obj, NotifyAppriseAPI)
330
331 assert (
332 obj.notify(
333 body="body",
334 title="title",
335 notify_type=NotifyType.INFO,
336 attach=attach,
337 )
338 is True
339 )
340
341 details = mock_post.call_args_list[0]
342 assert details[0][0] == "http://localhost/notify/mytoken1"
343 data = loads(details[1]["data"])
344 assert "attachments" in data
345 assert isinstance(data["attachments"], list)
346 assert len(data["attachments"]) == 1
347
348 # Remove our attachment to make the next assert easier
349 del data["attachments"]
350 assert data == {
351 "title": "title",
352 "body": "body",
353 "type": "info",
354 "format": "text",
355 }
356 assert "X-Apprise-ID" in details[1]["headers"]
357 assert details[1]["headers"].get("User-Agent") == "Apprise"
358 assert details[1]["headers"].get("Accept") == "application/json"
359 assert details[1]["headers"].get("X-Apprise-Recursion-Count") == "1"
360
361
362 @mock.patch("requests.post")
363 def test_notify_apprise_api_attachments(mock_post):
364 """NotifyAppriseAPI() Attachments."""
365
366 okay_response = requests.Request()
367
368 for method in ("json", "form"):
369 okay_response.status_code = requests.codes.ok
370 okay_response.content = ""
371
372 # Assign our mock object our return value
373 mock_post.return_value = okay_response
374
375 obj = Apprise.instantiate(
376 f"apprise://user@localhost/mytoken1/?method={method}"
377 )
378 assert isinstance(obj, NotifyAppriseAPI)
379
380 # Test Valid Attachment
381 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
382 attach = AppriseAttachment(path)
383 assert (
384 obj.notify(
385 body="body",
386 title="title",
387 notify_type=NotifyType.INFO,
388 attach=attach,
389 )
390 is True
391 )
392
393 # Test invalid attachment
394 path = os.path.join(
395 TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg"
396 )
397 assert (
398 obj.notify(
399 body="body",
400 title="title",
401 notify_type=NotifyType.INFO,
402 attach=path,
403 )
404 is False
405 )
406
407 # Test Valid Attachment (load 3)
408 path = (
409 os.path.join(TEST_VAR_DIR, "apprise-test.gif"),
410 os.path.join(TEST_VAR_DIR, "apprise-test.gif"),
411 os.path.join(TEST_VAR_DIR, "apprise-test.gif"),
412 )
413 attach = AppriseAttachment(path)
414
415 # Return our good configuration
416 mock_post.side_effect = None
417 mock_post.return_value = okay_response
418 with mock.patch("builtins.open", side_effect=OSError()):
419 # We can't send the message we can't open the attachment for
420 # reading
421 assert (
422 obj.notify(
423 body="body",
424 title="title",
425 notify_type=NotifyType.INFO,
426 attach=attach,
427 )
428 is False
429 )
430
431 with mock.patch("requests.post", side_effect=OSError()):
432 # Attachment issue
433 assert (
434 obj.notify(
435 body="body",
436 title="title",
437 notify_type=NotifyType.INFO,
438 attach=attach,
439 )
440 is False
441 )
442
443 # test the handling of our batch modes
444 obj = Apprise.instantiate("apprise://user@localhost/mytoken1/")
445 assert isinstance(obj, NotifyAppriseAPI)
446
447 # Now send an attachment normally without issues
448 mock_post.reset_mock()
449
450 assert (
451 obj.notify(
452 body="body",
453 title="title",
454 notify_type=NotifyType.INFO,
455 attach=attach,
456 )
457 is True
458 )
459 assert mock_post.call_count == 1
460
461 details = mock_post.call_args_list[0]
462 assert details[0][0] == "http://localhost/notify/mytoken1"
463 assert obj.url(privacy=False).startswith(
464 "apprise://user@localhost/mytoken1/"
465 )
466
467 mock_post.reset_mock()