Debian Codegraph @

tests/test_plugin_notifico.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 # Disable logging for a cleaner testing output
29 import logging
30 from unittest import mock
31 from urllib.parse import urlparse
32
33 from helpers import AppriseURLTester
34 import pytest
35 import requests
36
37 from apprise.common import NotifyType
38 from apprise.exception import AppriseImproperlyConfigured
39 from apprise.plugins.notifico import NotificoMode, NotifyNotifico
40
41 logging.disable(logging.CRITICAL)
42
43 # Our Testing URLs
44 apprise_url_tests = (
45 (
46 "notifico://",
47 {
48 "instance": AppriseImproperlyConfigured,
49 },
50 ),
51 (
52 "notifico://:@/",
53 {
54 "instance": AppriseImproperlyConfigured,
55 },
56 ),
57 (
58 "notifico://1234",
59 {
60 # Just a project id provided (no message token)
61 "instance": AppriseImproperlyConfigured,
62 },
63 ),
64 (
65 "notifico://abcd/ckhrjW8w672m6HG",
66 {
67 # an invalid project id provided (not all digits)
68 "instance": AppriseImproperlyConfigured,
69 },
70 ),
71 (
72 "notifico://1234/ckhrjW8w672m6HG",
73 {
74 # A project id and message hook provided (official mode)
75 "instance": NotifyNotifico,
76 },
77 ),
78 (
79 "notifico://1234/ckhrjW8w672m6HG?prefix=no",
80 {
81 # Disable our prefix
82 "instance": NotifyNotifico,
83 },
84 ),
85 (
86 "notifico://1234/ckhrjW8w672m6HG?color=yes",
87 {
88 "instance": NotifyNotifico,
89 "notify_type": "info",
90 },
91 ),
92 (
93 "notifico://1234/ckhrjW8w672m6HG?color=yes",
94 {
95 "instance": NotifyNotifico,
96 "notify_type": "success",
97 },
98 ),
99 (
100 "notifico://1234/ckhrjW8w672m6HG?color=yes",
101 {
102 "instance": NotifyNotifico,
103 "notify_type": "warning",
104 },
105 ),
106 (
107 "notifico://1234/ckhrjW8w672m6HG?color=yes",
108 {
109 "instance": NotifyNotifico,
110 "notify_type": "failure",
111 },
112 ),
113 (
114 "notifico://1234/ckhrjW8w672m6HG?color=yes",
115 {
116 "instance": NotifyNotifico,
117 "notify_type": "invalid",
118 },
119 ),
120 (
121 "notifico://1234/ckhrjW8w672m6HG?color=no",
122 {
123 # Test our color flag by having it set to off
124 "instance": NotifyNotifico,
125 # Our expected url(privacy=True) startswith() response:
126 "privacy_url": "notifico://1...4/c...G",
127 },
128 ),
129 # Native URL (official endpoint)
130 (
131 "https://n.tkte.ch/h/2144/uJmKaBW9WFk42miB146ci3Kj",
132 {
133 "instance": NotifyNotifico,
134 },
135 ),
136 # Self-hosted HTTP
137 (
138 "notifico://example.com/1234/ckhrjW8w672m6HG",
139 {
140 "instance": NotifyNotifico,
141 },
142 ),
143 # Self-hosted HTTPS
144 (
145 "notificos://example.com/1234/ckhrjW8w672m6HG",
146 {
147 "instance": NotifyNotifico,
148 "privacy_url": "notificos://example.com/1...4/c...G",
149 },
150 ),
151 # Self-hosted with port and color
152 (
153 "notifico://[email protected]:20/1234/ckhrjW8w672m6HG?color=yes",
154 {
155 "instance": NotifyNotifico,
156 "notify_type": "info",
157 },
158 ),
159 # Self-hosted HTTPS with auth
160 (
161 "notificos://user:[email protected]/1234/ckhrjW8w672m6HG?color=yes",
162 {
163 "instance": NotifyNotifico,
164 "notify_type": "success",
165 "privacy_url": "notificos://user:****@example.com/1...4/c...G",
166 },
167 ),
168 # Self-hosted: project and token supplied as query params
169 (
170 "notificos://user:[email protected]/"
171 "?project=1234&token=ckhrjW8w672m6HG&color=yes",
172 {
173 "instance": NotifyNotifico,
174 "notify_type": "success",
175 "privacy_url": "notificos://user:****@example.com/1...4/c...G",
176 },
177 ),
178 # Self-hosted: invalid project_id in path
179 (
180 "notifico://example.com/abcd/ckhrjW8w672m6HG",
181 {
182 "instance": AppriseImproperlyConfigured,
183 },
184 ),
185 (
186 "notifico://1234/ckhrjW8w672m6HG",
187 {
188 "instance": NotifyNotifico,
189 # don't include an image by default
190 "include_image": False,
191 },
192 ),
193 # Official: HTTP error responses
194 (
195 "notifico://1234/ckhrjW8w672m6HG",
196 {
197 "instance": NotifyNotifico,
198 # force a failure
199 "response": False,
200 "requests_response_code": requests.codes.internal_server_error,
201 },
202 ),
203 (
204 "notifico://1234/ckhrjW8w672m6HG",
205 {
206 "instance": NotifyNotifico,
207 # throw a bizarre code forcing us to fail to look it up
208 "response": False,
209 "requests_response_code": 999,
210 },
211 ),
212 (
213 "notifico://1234/ckhrjW8w672m6HG",
214 {
215 "instance": NotifyNotifico,
216 # Throws a series of i/o exceptions with this flag set
217 # and tests that we gracefully handle them
218 "test_requests_exceptions": True,
219 },
220 ),
221 # Self-hosted: HTTP error responses
222 (
223 "notifico://example.com/1234/ckhrjW8w672m6HG",
224 {
225 "instance": NotifyNotifico,
226 "response": False,
227 "requests_response_code": requests.codes.internal_server_error,
228 },
229 ),
230 (
231 "notifico://example.com/1234/ckhrjW8w672m6HG",
232 {
233 "instance": NotifyNotifico,
234 "response": False,
235 "requests_response_code": 999,
236 },
237 ),
238 (
239 "notifico://example.com/1234/ckhrjW8w672m6HG",
240 {
241 "instance": NotifyNotifico,
242 "test_requests_exceptions": True,
243 },
244 ),
245 )
246
247
248 def test_plugin_notifico_urls():
249 """NotifyNotifico() Apprise URLs."""
250
251 # Run our general tests
252 AppriseURLTester(tests=apprise_url_tests).run_all()
253
254
255 @mock.patch("requests.get")
256 def test_plugin_notifico_official_mode(mock_get):
257 """NotifyNotifico() official-mode init and round-trip."""
258
259 # Prepare a successful response
260 response = mock.Mock()
261 response.status_code = requests.codes.ok
262 mock_get.return_value = response
263
264 # Valid official-mode instance
265 obj = NotifyNotifico(
266 project_id="2144",
267 msghook="uJmKaBW9WFk42miB146ci3Kj",
268 )
269 assert obj.mode == NotificoMode.OFFICIAL
270 assert not obj.host
271
272 # url() round-trip preserves identity
273 url = obj.url()
274 assert "2144" in url
275 assert "uJmKaBW9WFk42miB146ci3Kj" in url
276
277 result = NotifyNotifico.parse_url(url)
278 assert result is not None
279 obj2 = NotifyNotifico(**result)
280 assert obj2.url_identifier == obj.url_identifier
281
282 # send() succeeds and hits the official n.tkte.ch endpoint
283 assert obj.send(body="test") is True
284 assert mock_get.call_count == 1
285 call_url = mock_get.call_args[0][0]
286 assert "n.tkte.ch" in call_url
287 assert "2144" in call_url
288 assert "uJmKaBW9WFk42miB146ci3Kj" in call_url
289
290
291 @mock.patch("requests.get")
292 def test_plugin_notifico_selfhosted_http(mock_get):
293 """NotifyNotifico() self-hosted HTTP mode."""
294
295 response = mock.Mock()
296 response.status_code = requests.codes.ok
297 mock_get.return_value = response
298
299 obj = NotifyNotifico(
300 project_id="1234",
301 msghook="ckhrjW8w672m6HG",
302 host="myhost.local",
303 )
304 assert obj.mode == NotificoMode.SELFHOSTED
305 assert obj.host == "myhost.local"
306 assert obj.secure is False
307
308 # url() encodes the hostname
309 url = obj.url()
310 assert url.startswith("notifico://myhost.local/1234/ckhrjW8w672m6HG")
311
312 # Round-trip preserves identity
313 result = NotifyNotifico.parse_url(url)
314 assert result is not None
315 obj2 = NotifyNotifico(**result)
316 assert obj2.url_identifier == obj.url_identifier
317
318 # send() uses HTTP against the self-hosted host
319 assert obj.send(body="test") is True
320 call_url = mock_get.call_args[0][0]
321 parsed = urlparse(call_url)
322 assert parsed.scheme == "http"
323 assert parsed.hostname == "myhost.local"
324 assert "1234" in call_url
325 assert "ckhrjW8w672m6HG" in call_url
326
327
328 @mock.patch("requests.get")
329 def test_plugin_notifico_selfhosted_https(mock_get):
330 """NotifyNotifico() self-hosted HTTPS mode with auth and port."""
331
332 response = mock.Mock()
333 response.status_code = requests.codes.ok
334 mock_get.return_value = response
335
336 # Parse a notificos:// URL with credentials and non-default port
337 result = NotifyNotifico.parse_url(
338 "notificos://user:[email protected]:8443/9999/AbCdEfGh"
339 )
340 assert result is not None
341 obj = NotifyNotifico(**result)
342
343 assert obj.mode == NotificoMode.SELFHOSTED
344 assert obj.secure is True
345 assert obj.host == "myhost.local"
346 assert obj.port == 8443
347 assert obj.user == "user"
348
349 # url() emits notificos:// with the non-default port
350 url = obj.url()
351 assert url.startswith("notificos://user:")
352 assert "myhost.local:8443" in url
353 assert "9999" in url
354
355 # Privacy URL hides the password
356 privacy = obj.url(privacy=True)
357 assert "****" in privacy
358
359 # send() uses HTTPS against the self-hosted host
360 assert obj.send(body="test") is True
361 call_url = mock_get.call_args[0][0]
362 parsed = urlparse(call_url)
363 assert parsed.scheme == "https"
364 assert parsed.hostname == "myhost.local"
365 assert parsed.port == 8443
366 assert "9999" in call_url
367 assert "AbCdEfGh" in call_url
368
369
370 @mock.patch("requests.get")
371 def test_plugin_notifico_invalid_params(mock_get):
372 """NotifyNotifico() rejects invalid initialization parameters."""
373
374 # Invalid project_id (not all digits)
375 with pytest.raises(AppriseImproperlyConfigured):
376 NotifyNotifico(project_id="abc", msghook="validhook")
377
378 # Invalid msghook (contains special characters)
379 with pytest.raises(AppriseImproperlyConfigured):
380 NotifyNotifico(project_id="1234", msghook="bad hook!")
381
382 # None project_id
383 with pytest.raises(AppriseImproperlyConfigured):
384 NotifyNotifico(project_id=None, msghook="validhook")
385
386 # None msghook
387 with pytest.raises(AppriseImproperlyConfigured):
388 NotifyNotifico(project_id="1234", msghook=None)
389
390 # No HTTP calls should have been made
391 assert mock_get.call_count == 0
392
393
394 @mock.patch("requests.get")
395 def test_plugin_notifico_parse_url(mock_get):
396 """NotifyNotifico() parse_url edge cases."""
397
398 # Official mode: numeric host is the project_id
399 result = NotifyNotifico.parse_url("notifico://1234/hookABC")
400 assert result is not None
401 assert result["project_id"] == "1234"
402 assert result["msghook"] == "hookABC"
403 assert not result["host"]
404
405 # Official mode must clear port/user/password even when present
406 # in the URL (e.g. notifico://user:pass@1234:8080/hook);
407 # these fields have no meaning for the official n.tkte.ch endpoint
408 result = NotifyNotifico.parse_url("notifico://user:pass@1234:8080/hookABC")
409 assert result is not None
410 assert result["project_id"] == "1234"
411 assert result["msghook"] == "hookABC"
412 assert not result["host"]
413 assert result["port"] is None
414 assert result["user"] is None
415 assert result["password"] is None
416
417 # Self-hosted mode: hostname in host position
418 result = NotifyNotifico.parse_url("notifico://myhost.example/5678/hookXYZ")
419 assert result is not None
420 assert result["project_id"] == "5678"
421 assert result["msghook"] == "hookXYZ"
422 assert result["host"] == "myhost.example"
423
424 # Query-param aliases override path-based values
425 result = NotifyNotifico.parse_url(
426 "notificos://example.com/?project=9999&token=hookQQQ"
427 )
428 assert result is not None
429 assert result["project_id"] == "9999"
430 assert result["msghook"] == "hookQQQ"
431
432 # Color and prefix default to True
433 result = NotifyNotifico.parse_url("notifico://1234/hook")
434 assert result["color"] is True
435 assert result["prefix"] is True
436
437 # Explicit color=no and prefix=no
438 result = NotifyNotifico.parse_url(
439 "notifico://1234/hook?color=no&prefix=no"
440 )
441 assert result["color"] is False
442 assert result["prefix"] is False
443
444 # parse_url returns None only when parse_url itself cannot parse the URL
445 assert NotifyNotifico.parse_url(None) is None
446
447 assert mock_get.call_count == 0
448
449
450 @mock.patch("requests.get")
451 def test_plugin_notifico_send_http_errors(mock_get):
452 """NotifyNotifico() handles HTTP error responses gracefully."""
453
454 obj = NotifyNotifico(project_id="1234", msghook="validHook")
455
456 # HTTP 500
457 response = mock.Mock()
458 response.status_code = requests.codes.internal_server_error
459 response.content = b""
460 mock_get.return_value = response
461 assert obj.send(body="test") is False
462
463 # Unrecognised HTTP status
464 response.status_code = 999
465 assert obj.send(body="test") is False
466
467 # Network exception
468 mock_get.side_effect = requests.RequestException("connection refused")
469 assert obj.send(body="test") is False
470
471
472 @mock.patch("requests.get")
473 def test_plugin_notifico_send_notify_types(mock_get):
474 """NotifyNotifico() sends all four notification types successfully."""
475
476 response = mock.Mock()
477 response.status_code = requests.codes.ok
478 mock_get.return_value = response
479
480 obj = NotifyNotifico(
481 project_id="1234",
482 msghook="validHook",
483 color=True,
484 prefix=True,
485 )
486
487 for notify_type in (
488 NotifyType.INFO,
489 NotifyType.SUCCESS,
490 NotifyType.WARNING,
491 NotifyType.FAILURE,
492 ):
493 mock_get.reset_mock()
494 assert obj.send(body="msg", notify_type=notify_type) is True
495 assert mock_get.call_count == 1
496
497
498 @mock.patch("requests.get")
499 def test_plugin_notifico_color_prefix_flags(mock_get):
500 """NotifyNotifico() color=False strips IRC codes; prefix=False omits it."""
501
502 response = mock.Mock()
503 response.status_code = requests.codes.ok
504 mock_get.return_value = response
505
506 # color=False, prefix=False -- payload should be the raw body
507 obj = NotifyNotifico(
508 project_id="1234",
509 msghook="validHook",
510 color=False,
511 prefix=False,
512 )
513 assert obj.send(body="plain text") is True
514 payload = mock_get.call_args[1]["params"]["payload"]
515 assert payload == "plain text"
516
517 # color=True, prefix=True -- payload contains the prefix bracket
518 mock_get.reset_mock()
519 obj2 = NotifyNotifico(
520 project_id="1234",
521 msghook="validHook",
522 color=True,
523 prefix=True,
524 )
525 assert obj2.send(body="colored text", notify_type=NotifyType.INFO) is True
526 payload2 = mock_get.call_args[1]["params"]["payload"]
527 assert "colored text" in payload2
528 assert "[" in payload2
529
530
531 @mock.patch("requests.get")
532 def test_plugin_notifico_native_url(mock_get):
533 """NotifyNotifico() parse_native_url() parses n.tkte.ch URLs."""
534
535 response = mock.Mock()
536 response.status_code = requests.codes.ok
537 mock_get.return_value = response
538
539 # Standard native URL
540 result = NotifyNotifico.parse_native_url(
541 "https://n.tkte.ch/h/2144/uJmKaBW9WFk42miB146ci3Kj"
542 )
543 assert result is not None
544 obj = NotifyNotifico(**result)
545 assert obj.mode == NotificoMode.OFFICIAL
546 assert obj.project_id == "2144"
547 assert obj.msghook == "uJmKaBW9WFk42miB146ci3Kj"
548 assert obj.send(body="test") is True
549
550 # Non-matching URL returns None
551 assert (
552 NotifyNotifico.parse_native_url("https://other.host/h/1/abc") is None
553 )