Debian Codegraph @

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