Debian Codegraph @

tests/test_plugin_custom_xml.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 import os
31 import re
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.custom_xml import NotifyXML
39
40 logging.disable(logging.CRITICAL)
41
42 # Attachment Directory
43 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
44
45
46 # Our Testing URLs
47 apprise_url_tests = (
48 (
49 "xml://:@/",
50 {
51 "instance": None,
52 },
53 ),
54 (
55 "xml://",
56 {
57 "instance": None,
58 },
59 ),
60 (
61 "xmls://",
62 {
63 "instance": None,
64 },
65 ),
66 (
67 "xml://localhost",
68 {
69 "instance": NotifyXML,
70 },
71 ),
72 (
73 "xml://user@localhost",
74 {
75 "instance": NotifyXML,
76 },
77 ),
78 (
79 "xml://user@localhost?method=invalid",
80 {
81 "instance": TypeError,
82 },
83 ),
84 (
85 "xml://user:pass@localhost",
86 {
87 "instance": NotifyXML,
88 # Our expected url(privacy=True) startswith() response:
89 "privacy_url": "xml://user:****@localhost",
90 },
91 ),
92 # Test method variations
93 (
94 "xml://user@localhost?method=put",
95 {
96 "instance": NotifyXML,
97 },
98 ),
99 (
100 "xml://user@localhost?method=get",
101 {
102 "instance": NotifyXML,
103 },
104 ),
105 (
106 "xml://user@localhost?method=post",
107 {
108 "instance": NotifyXML,
109 "force_debug": True,
110 },
111 ),
112 (
113 "xml://user@localhost?method=head",
114 {
115 "instance": NotifyXML,
116 },
117 ),
118 (
119 "xml://user@localhost?method=delete",
120 {
121 "instance": NotifyXML,
122 },
123 ),
124 (
125 "xml://user@localhost?method=patch",
126 {
127 "instance": NotifyXML,
128 },
129 ),
130 (
131 "xml://user@localhost?method=update",
132 {
133 "instance": NotifyXML,
134 },
135 ),
136 (
137 "xml://user@localhost?method=options",
138 {
139 "instance": NotifyXML,
140 },
141 ),
142 # Continue testing other cases
143 (
144 "xml://localhost:8080",
145 {
146 "instance": NotifyXML,
147 },
148 ),
149 (
150 "xml://user:pass@localhost:8080",
151 {
152 "instance": NotifyXML,
153 },
154 ),
155 (
156 "xmls://localhost",
157 {
158 "instance": NotifyXML,
159 },
160 ),
161 (
162 "xmls://user:pass@localhost",
163 {
164 "instance": NotifyXML,
165 },
166 ),
167 # Continue testing other cases
168 (
169 "xml://localhost:8080",
170 {
171 "instance": NotifyXML,
172 },
173 ),
174 (
175 "xml://user:pass@localhost:8080",
176 {
177 "instance": NotifyXML,
178 },
179 ),
180 (
181 "xml://localhost",
182 {
183 "instance": NotifyXML,
184 },
185 ),
186 (
187 "xmls://user:pass@localhost",
188 {
189 "instance": NotifyXML,
190 # Our expected url(privacy=True) startswith() response:
191 "privacy_url": "xmls://user:****@localhost",
192 },
193 ),
194 (
195 "xml://user@localhost:8080/path/",
196 {
197 "instance": NotifyXML,
198 "privacy_url": "xml://user@localhost:8080/path",
199 },
200 ),
201 (
202 "xmls://localhost:8080/path/",
203 {
204 "instance": NotifyXML,
205 # Our expected url(privacy=True) startswith() response:
206 "privacy_url": "xmls://localhost:8080/path/",
207 },
208 ),
209 (
210 "xmls://user:pass@localhost:8080",
211 {
212 "instance": NotifyXML,
213 },
214 ),
215 # Test our GET params
216 (
217 "xml://localhost:8080/path?-ParamA=Value",
218 {
219 "instance": NotifyXML,
220 },
221 ),
222 # Test our Headers
223 (
224 "xml://localhost:8080/path?+HeaderKey=HeaderValue",
225 {
226 "instance": NotifyXML,
227 },
228 ),
229 (
230 "xml://user:pass@localhost:8081",
231 {
232 "instance": NotifyXML,
233 # force a failure
234 "response": False,
235 "requests_response_code": requests.codes.internal_server_error,
236 },
237 ),
238 (
239 "xml://user:pass@localhost:8082",
240 {
241 "instance": NotifyXML,
242 # throw a bizarre code forcing us to fail to look it up
243 "response": False,
244 "requests_response_code": 999,
245 },
246 ),
247 (
248 "xml://user:pass@localhost:8083",
249 {
250 "instance": NotifyXML,
251 # Throws a series of i/o exceptions with this flag
252 # is set and tests that we gracefully handle them
253 "test_requests_exceptions": True,
254 },
255 ),
256 )
257
258
259 def test_plugin_custom_xml_urls():
260 """NotifyXML() Apprise URLs."""
261
262 # Run our general tests
263 AppriseURLTester(tests=apprise_url_tests).run_all()
264
265
266 @mock.patch("requests.request")
267 def test_notify_xml_plugin_attachments(mock_request):
268 """NotifyXML() Attachments."""
269
270 okay_response = requests.Request()
271 okay_response.status_code = requests.codes.ok
272 okay_response.content = ""
273
274 # Assign our mock object our return value
275 mock_request.return_value = okay_response
276
277 obj = Apprise.instantiate("xml://localhost.localdomain/")
278 assert isinstance(obj, NotifyXML)
279
280 # Test Valid Attachment
281 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
282 attach = AppriseAttachment(path)
283 assert (
284 obj.notify(
285 body="body",
286 title="title",
287 notify_type=NotifyType.INFO,
288 attach=attach,
289 )
290 is True
291 )
292
293 # Test invalid attachment
294 path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg")
295 assert (
296 obj.notify(
297 body="body",
298 title="title",
299 notify_type=NotifyType.INFO,
300 attach=path,
301 )
302 is False
303 )
304
305 # Test Valid Attachment (load 3)
306 path = (
307 os.path.join(TEST_VAR_DIR, "apprise-test.gif"),
308 os.path.join(TEST_VAR_DIR, "apprise-test.gif"),
309 os.path.join(TEST_VAR_DIR, "apprise-test.gif"),
310 )
311 attach = AppriseAttachment(path)
312
313 # Return our good configuration
314 mock_request.side_effect = None
315 mock_request.return_value = okay_response
316 with mock.patch("builtins.open", side_effect=OSError()):
317 # We can't send the message we can't open the attachment for reading
318 assert (
319 obj.notify(
320 body="body",
321 title="title",
322 notify_type=NotifyType.INFO,
323 attach=attach,
324 )
325 is False
326 )
327
328 # test the handling of our batch modes
329 obj = Apprise.instantiate("xml://[email protected]/")
330 assert isinstance(obj, NotifyXML)
331
332 # Now send an attachment normally without issues
333 mock_request.reset_mock()
334 assert (
335 obj.notify(
336 body="body",
337 title="title",
338 notify_type=NotifyType.INFO,
339 attach=attach,
340 )
341 is True
342 )
343 assert mock_request.call_count == 1
344
345
346 @mock.patch("requests.request")
347 def test_plugin_custom_xml_edge_cases(mock_request):
348 """NotifyXML() Edge Cases."""
349
350 # Prepare our response
351 response = requests.Request()
352 response.status_code = requests.codes.ok
353
354 # Prepare Mock
355 mock_request.return_value = response
356
357 results = NotifyXML.parse_url(
358 "xml://localhost:8080/command?:Message=Body&method=GET"
359 "&:Key=value&:,=invalid&:MessageType="
360 )
361
362 assert isinstance(results, dict)
363 assert results["user"] is None
364 assert results["password"] is None
365 assert results["port"] == 8080
366 assert results["host"] == "localhost"
367 assert results["fullpath"] == "/command"
368 assert results["path"] == "/"
369 assert results["query"] == "command"
370 assert results["schema"] == "xml"
371 assert results["url"] == "xml://localhost:8080/command"
372 assert isinstance(results["qsd:"], dict)
373 assert results["qsd:"]["Message"] == "Body"
374 assert results["qsd:"]["Key"] == "value"
375 assert results["qsd:"][","] == "invalid"
376
377 instance = NotifyXML(**results)
378 assert isinstance(instance, NotifyXML)
379
380 # XSD URL is disabled due to custom formatting
381 assert instance.xsd_url is None
382
383 response = instance.send(title="title", body="body")
384 assert response is True
385 assert mock_request.call_count == 1
386
387 details = mock_request.call_args_list[0]
388 assert "NotifyType." not in details[1]["data"]
389 assert details[0][0] == "GET"
390 assert details[0][1] == "http://localhost:8080/command"
391 assert instance.url(privacy=False).startswith(
392 "xml://localhost:8080/command?"
393 )
394
395 # Generate a new URL based on our last and verify key values are the same
396 new_results = NotifyXML.parse_url(instance.url(safe=False))
397 for k in (
398 "user",
399 "password",
400 "port",
401 "host",
402 "fullpath",
403 "path",
404 "query",
405 "schema",
406 "url",
407 "method",
408 ):
409 assert new_results[k] == results[k]
410
411 # Test our data set for our key/value pair
412 assert re.search(r"<Version>[1-9]+\.[0-9]+</Version>", details[1]["data"])
413 assert "<Subject>title</Subject>" in details[1]["data"]
414
415 assert "<Message>test</Message>" not in details[1]["data"]
416 assert "<Message>" not in details[1]["data"]
417 # MessageType was removed from the payload
418 assert "<MessageType>" not in details[1]["data"]
419 # However we can find our mapped Message to the new value Body
420 assert "<Body>body</Body>" in details[1]["data"]
421 # Custom entry
422 assert "<Key>value</Key>" in details[1]["data"]
423
424 mock_request.reset_mock()
425
426 results = NotifyXML.parse_url(
427 "xml://localhost:8081/command?method=POST&:New=Value"
428 )
429
430 assert isinstance(results, dict)
431 assert results["user"] is None
432 assert results["password"] is None
433 assert results["port"] == 8081
434 assert results["host"] == "localhost"
435 assert results["fullpath"] == "/command"
436 assert results["path"] == "/"
437 assert results["query"] == "command"
438 assert results["schema"] == "xml"
439 assert results["url"] == "xml://localhost:8081/command"
440 assert isinstance(results["qsd:"], dict)
441 assert results["qsd:"]["New"] == "Value"
442
443 instance = NotifyXML(**results)
444 assert isinstance(instance, NotifyXML)
445
446 # XSD URL is disabled due to custom formatting
447 assert instance.xsd_url is None
448
449 response = instance.send(title="title", body="body")
450 assert response is True
451 assert mock_request.call_count == 1
452
453 details = mock_request.call_args_list[0]
454 assert "NotifyType." not in details[1]["data"]
455 assert details[0][0] == "POST"
456 assert details[0][1] == "http://localhost:8081/command"
457 assert instance.url(privacy=False).startswith(
458 "xml://localhost:8081/command?"
459 )
460
461 # Generate a new URL based on our last and verify key values are the same
462 new_results = NotifyXML.parse_url(instance.url(safe=False))
463 for k in (
464 "user",
465 "password",
466 "port",
467 "host",
468 "fullpath",
469 "path",
470 "query",
471 "schema",
472 "url",
473 "method",
474 ):
475 assert new_results[k] == results[k]
476
477 # Test our data set for our key/value pair
478 assert re.search(r"<Version>[1-9]+\.[0-9]+</Version>", details[1]["data"])
479 assert re.search(
480 r"<MessageType>{}</MessageType>".format(NotifyType.INFO.value),
481 details[1]["data"],
482 )
483 assert r"<Subject>title</Subject>" in details[1]["data"]
484 # No over-ride
485 assert r"<Message>body</Message>" in details[1]["data"]
486
487 mock_request.reset_mock()
488
489 results = NotifyXML.parse_url(
490 "xmls://localhost?method=POST&:Message=Body&:Subject=Title&:Version"
491 )
492
493 assert isinstance(results, dict)
494 assert results["user"] is None
495 assert results["password"] is None
496 assert results["port"] is None
497 assert results["host"] == "localhost"
498 assert results["fullpath"] is None
499 assert results["path"] is None
500 assert results["query"] is None
501 assert results["schema"] == "xmls"
502 assert results["url"] == "xmls://localhost"
503 assert isinstance(results["qsd:"], dict)
504 assert results["qsd:"]["Version"] == ""
505 assert results["qsd:"]["Message"] == "Body"
506 assert results["qsd:"]["Subject"] == "Title"
507
508 instance = NotifyXML(**results)
509 assert isinstance(instance, NotifyXML)
510
511 # XSD URL is disabled due to custom formatting
512 assert instance.xsd_url is None
513
514 response = instance.send(title="title", body="body")
515 assert response is True
516 assert mock_request.call_count == 1
517
518 details = mock_request.call_args_list[0]
519 assert "NotifyType." not in details[1]["data"]
520 assert details[0][0] == "POST"
521 assert details[0][1] == "https://localhost"
522 assert instance.url(privacy=False).startswith("xmls://localhost")
523
524 # Generate a new URL based on our last and verify key values are the same
525 new_results = NotifyXML.parse_url(instance.url(safe=False))
526
527 # Test that the Version has been dropped
528 assert (
529 re.search(r"<Version>[1-9]+\.[0-9]+</Version>", details[1]["data"])
530 is None
531 )
532
533 # Test our data set for our key/value pair
534 assert re.search(
535 r"<MessageType>{}</MessageType>".format(NotifyType.INFO.value),
536 details[1]["data"],
537 )
538
539 # Subject is swapped for Title
540 assert r"<Subject>title</Subject>" not in details[1]["data"]
541 assert r"<Title>title</Title>" in details[1]["data"]
542
543 # Message is swapped for Body
544 assert r"<Message>body</Message>" not in details[1]["data"]
545 assert r"<Body>body</Body>" in details[1]["data"]
546
547 mock_request.reset_mock()
548
549 # Verify UPDATE method is forwarded correctly
550 results = NotifyXML.parse_url("xml://localhost?method=UPDATE")
551 instance = NotifyXML(**results)
552 assert instance.send(title="title", body="body") is True
553 assert mock_request.call_count == 1
554 details = mock_request.call_args_list[0]
555 assert details[0][0] == "UPDATE"
556 assert details[0][1] == "http://localhost"
557
558 mock_request.reset_mock()
559
560 # Verify OPTIONS method is forwarded correctly
561 results = NotifyXML.parse_url("xml://localhost?method=OPTIONS")
562 instance = NotifyXML(**results)
563 assert instance.send(title="title", body="body") is True
564 assert mock_request.call_count == 1
565 details = mock_request.call_args_list[0]
566 assert details[0][0] == "OPTIONS"
567 assert details[0][1] == "http://localhost"