Debian Codegraph @

tests/test_plugin_youlmk.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 loads
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 import Apprise, AppriseAsset, NotifyType
39 from apprise.exception import AppriseImproperlyConfigured
40 from apprise.plugins.youlmk import NotifyYouLMK, youlmk_priority
41
42 logging.disable(logging.CRITICAL)
43
44 # The handoff's placeholder key and a token of the same shape; neither is real
45 TOKEN = "ylk_" + "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6"
46 KEY = "k_7hq2nk3f9qd8w4"
47
48 # Our Testing URLs
49 apprise_url_tests = (
50 (
51 "youlmk://",
52 {
53 # No token specified
54 "instance": AppriseImproperlyConfigured,
55 },
56 ),
57 (
58 "youlmk://invalid",
59 {
60 # Neither a ylk_ token nor a k_ key
61 "instance": AppriseImproperlyConfigured,
62 },
63 ),
64 (
65 "youlmk://ylk_tooshort",
66 {
67 # The token's shape is ylk_ and 32 characters
68 "instance": AppriseImproperlyConfigured,
69 },
70 ),
71 (
72 f"youlmk://{TOKEN}",
73 {
74 # A valid bearer token
75 "instance": NotifyYouLMK,
76 # Our expected url(privacy=True) startswith() response:
77 "privacy_url": "youlmk://y...6/",
78 },
79 ),
80 (
81 f"youlmk://{KEY}",
82 {
83 # A valid URL key
84 "instance": NotifyYouLMK,
85 "privacy_url": "youlmk://k...4/",
86 },
87 ),
88 (
89 f"youlmk://?token={TOKEN}",
90 {
91 # The token provided as a query argument
92 "instance": NotifyYouLMK,
93 "privacy_url": "youlmk://y...6/",
94 },
95 ),
96 (
97 f"youlmk://{TOKEN}?priority=critical",
98 {
99 # A forced priority
100 "instance": NotifyYouLMK,
101 },
102 ),
103 (
104 f"youlmk://{TOKEN}?priority=bogus",
105 {
106 # An unknown priority
107 "instance": AppriseImproperlyConfigured,
108 },
109 ),
110 (
111 f"youlmk://{TOKEN}?failure=critical&info=low",
112 {
113 # Per-type priorities
114 "instance": NotifyYouLMK,
115 },
116 ),
117 (
118 f"youlmk://{TOKEN}?warning=bogus",
119 {
120 # An unknown per-type priority
121 "instance": AppriseImproperlyConfigured,
122 },
123 ),
124 (
125 f"youlmk://{TOKEN}?url=https://example.com/runs/1&group=deploys",
126 {
127 # A link and a group
128 "instance": NotifyYouLMK,
129 },
130 ),
131 (
132 f"youlmk://{TOKEN}?url=https://example.com&url_label=View%20run",
133 {
134 # A link with its own wording
135 "instance": NotifyYouLMK,
136 },
137 ),
138 (
139 f"youlmk://{TOKEN}?image=yes",
140 {
141 # The notification-type image rides along
142 "instance": NotifyYouLMK,
143 },
144 ),
145 (
146 f"https://youlmk.com/k/{KEY}",
147 {
148 # The key door pasted in as-is
149 "instance": NotifyYouLMK,
150 "privacy_url": "youlmk://k...4/",
151 },
152 ),
153 (
154 f"youlmk://{TOKEN}",
155 {
156 "instance": NotifyYouLMK,
157 # Throw an exception on the send
158 "test_requests_exceptions": True,
159 },
160 ),
161 (
162 f"youlmk://{TOKEN}",
163 {
164 "instance": NotifyYouLMK,
165 # An unknown key
166 "response": False,
167 "requests_response_code": 401,
168 },
169 ),
170 (
171 f"youlmk://{TOKEN}",
172 {
173 "instance": NotifyYouLMK,
174 # The trial's notifications are used
175 "response": False,
176 "requests_response_code": 402,
177 },
178 ),
179 (
180 f"youlmk://{TOKEN}",
181 {
182 "instance": NotifyYouLMK,
183 # A code the map does not name
184 "response": False,
185 "requests_response_code": 999,
186 },
187 ),
188 )
189
190
191 def test_plugin_youlmk_urls():
192 """NotifyYouLMK() Apprise URLs."""
193
194 # Run our general tests
195 AppriseURLTester(tests=apprise_url_tests).run_all()
196
197
198 def test_plugin_youlmk_edge_cases():
199 """NotifyYouLMK() Edge Cases."""
200
201 # No token
202 with pytest.raises(AppriseImproperlyConfigured):
203 NotifyYouLMK(token=None)
204
205 # An empty token
206 with pytest.raises(AppriseImproperlyConfigured):
207 NotifyYouLMK(token=" ")
208
209 # The wrong shape
210 with pytest.raises(AppriseImproperlyConfigured):
211 NotifyYouLMK(token="ylk_" + "x" * 31)
212
213 # A bad forced priority
214 with pytest.raises(AppriseImproperlyConfigured):
215 NotifyYouLMK(token=TOKEN, priority="bogus")
216
217 # A bad per-type priority
218 with pytest.raises(AppriseImproperlyConfigured):
219 NotifyYouLMK(token=TOKEN, failure="bogus")
220
221 # Both shapes are accepted, and the case of the token is kept
222 obj = NotifyYouLMK(token=TOKEN.upper())
223 assert obj.token == TOKEN.upper()
224 assert obj.is_token is True
225
226 obj = NotifyYouLMK(token=KEY)
227 assert obj.is_token is False
228
229
230 @mock.patch("requests.post")
231 def test_plugin_youlmk_send_token(mock_post):
232 """NotifyYouLMK() Send with the bearer token."""
233
234 response = mock.Mock()
235 response.status_code = requests.codes.ok
236 mock_post.return_value = response
237
238 obj = Apprise.instantiate(f"youlmk://{TOKEN}")
239 assert isinstance(obj, NotifyYouLMK)
240
241 assert obj.notify(title="backup finished", body="4.2 GB") is True
242 assert mock_post.call_count == 1
243
244 # The bearer door, with the token in the header
245 assert mock_post.call_args[0][0] == "https://youlmk.com/v1/notify"
246 headers = mock_post.call_args[1]["headers"]
247 assert headers["Authorization"] == f"Bearer {TOKEN}"
248 assert headers["Content-Type"] == "application/json; charset=utf-8"
249
250 payload = loads(mock_post.call_args[1]["data"])
251 assert payload["title"] == "backup finished"
252 assert payload["body"] == "4.2 GB"
253 # INFO maps to normal by default
254 assert payload["priority"] == "normal"
255 assert "url" not in payload
256 assert "group" not in payload
257
258
259 @mock.patch("requests.post")
260 def test_plugin_youlmk_send_key(mock_post):
261 """NotifyYouLMK() Send with the URL key."""
262
263 response = mock.Mock()
264 response.status_code = requests.codes.ok
265 mock_post.return_value = response
266
267 obj = Apprise.instantiate(f"youlmk://{KEY}")
268 assert isinstance(obj, NotifyYouLMK)
269
270 assert obj.notify(title="deployed", body="v2.4.1") is True
271 assert mock_post.call_count == 1
272
273 # The key door, with the key in the address and no bearer header
274 assert mock_post.call_args[0][0] == f"https://youlmk.com/k/{KEY}"
275 assert "Authorization" not in mock_post.call_args[1]["headers"]
276
277 payload = loads(mock_post.call_args[1]["data"])
278 assert payload["title"] == "deployed"
279 assert payload["priority"] == "normal"
280
281
282 @mock.patch("requests.post")
283 def test_plugin_youlmk_notify_type_mapping(mock_post):
284 """NotifyYouLMK() Priority derived from the notification type."""
285
286 response = mock.Mock()
287 response.status_code = requests.codes.ok
288 mock_post.return_value = response
289
290 obj = Apprise.instantiate(f"youlmk://{TOKEN}")
291
292 expected = {
293 NotifyType.INFO: "normal",
294 NotifyType.SUCCESS: "normal",
295 NotifyType.WARNING: "high",
296 NotifyType.FAILURE: "high",
297 }
298 for notify_type, priority in expected.items():
299 mock_post.reset_mock()
300 assert obj.notify(title="t", body="b", notify_type=notify_type) is True
301 payload = loads(mock_post.call_args[1]["data"])
302 assert payload["priority"] == priority
303
304
305 @mock.patch("requests.post")
306 def test_plugin_youlmk_priority_override(mock_post):
307 """NotifyYouLMK() A forced priority wins over the type."""
308
309 response = mock.Mock()
310 response.status_code = requests.codes.ok
311 mock_post.return_value = response
312
313 obj = Apprise.instantiate(f"youlmk://{TOKEN}?priority=crit")
314 assert obj.priority == "critical"
315
316 assert obj.notify(title="t", body="b", notify_type=NotifyType.INFO) is True
317 payload = loads(mock_post.call_args[1]["data"])
318 assert payload["priority"] == "critical"
319
320
321 @mock.patch("requests.post")
322 def test_plugin_youlmk_per_type_priorities(mock_post):
323 """NotifyYouLMK() Per-type priorities from the URL."""
324
325 response = mock.Mock()
326 response.status_code = requests.codes.ok
327 mock_post.return_value = response
328
329 obj = Apprise.instantiate(
330 f"youlmk://{TOKEN}?info=low&success=l&warning=n&failure=critical"
331 )
332 assert obj.priority is None
333 assert obj.priority_map[NotifyType.INFO] == "low"
334 assert obj.priority_map[NotifyType.SUCCESS] == "low"
335 assert obj.priority_map[NotifyType.WARNING] == "normal"
336 assert obj.priority_map[NotifyType.FAILURE] == "critical"
337
338 assert (
339 obj.notify(title="t", body="b", notify_type=NotifyType.FAILURE) is True
340 )
341 payload = loads(mock_post.call_args[1]["data"])
342 assert payload["priority"] == "critical"
343
344 # Every changed mapping is carried by the URL
345 url = obj.url()
346 assert "info=low" in url
347 assert "success=low" in url
348 assert "warning=normal" in url
349 assert "failure=critical" in url
350
351
352 @mock.patch("requests.post")
353 def test_plugin_youlmk_link_and_group(mock_post):
354 """NotifyYouLMK() A link and a group travel with the payload."""
355
356 response = mock.Mock()
357 response.status_code = requests.codes.ok
358 mock_post.return_value = response
359
360 obj = Apprise.instantiate(
361 f"youlmk://{TOKEN}?url=https://example.com/runs/1&group=deploys"
362 )
363 assert obj.link == "https://example.com/runs/1"
364 assert obj.group == "deploys"
365
366 assert obj.notify(title="deployed", body="v2.4.1") is True
367 payload = loads(mock_post.call_args[1]["data"])
368 assert payload["url"] == "https://example.com/runs/1"
369 assert payload["group"] == "deploys"
370
371 # And come back out of the URL
372 url = obj.url()
373 assert "url=https%3A%2F%2Fexample.com%2Fruns%2F1" in url
374 assert "group=deploys" in url
375
376
377 @mock.patch("requests.post")
378 def test_plugin_youlmk_empty_title(mock_post):
379 """NotifyYouLMK() A title is always sent."""
380
381 response = mock.Mock()
382 response.status_code = requests.codes.ok
383 mock_post.return_value = response
384
385 # The application description stands in for a missing title
386 asset = AppriseAsset(app_desc="My App")
387 obj = Apprise.instantiate(f"youlmk://{TOKEN}", asset=asset)
388 assert obj.notify(body="b", title="") is True
389 payload = loads(mock_post.call_args[1]["data"])
390 assert payload["title"] == "My App"
391
392 # And the application id when there is no description either
393 mock_post.reset_mock()
394 asset = AppriseAsset(app_desc="", app_id="apprise")
395 obj = Apprise.instantiate(f"youlmk://{TOKEN}", asset=asset)
396 assert obj.notify(body="b", title="") is True
397 payload = loads(mock_post.call_args[1]["data"])
398 assert payload["title"] == "apprise"
399
400
401 @mock.patch("requests.post")
402 def test_plugin_youlmk_failures(mock_post):
403 """NotifyYouLMK() The door's own codes read as failures."""
404
405 for code in (400, 401, 402, 410, 413, 422, 429, 503):
406 response = mock.Mock()
407 response.status_code = code
408 response.content = b'{"error":"code"}'
409 mock_post.return_value = response
410
411 obj = Apprise.instantiate(f"youlmk://{TOKEN}")
412 assert obj.notify(title="t", body="b") is False
413
414 # A connection error
415 mock_post.side_effect = requests.RequestException("down")
416 obj = Apprise.instantiate(f"youlmk://{TOKEN}")
417 assert obj.notify(title="t", body="b") is False
418
419
420 @mock.patch("requests.post")
421 def test_plugin_youlmk_url_roundtrip(mock_post):
422 """NotifyYouLMK() A URL survives a round trip."""
423
424 response = mock.Mock()
425 response.status_code = requests.codes.ok
426 mock_post.return_value = response
427
428 source = f"youlmk://{TOKEN}?priority=high&failure=critical&group=g"
429 obj = Apprise.instantiate(source)
430 url = obj.url()
431 assert url.startswith(f"youlmk://{TOKEN}/?")
432 assert "priority=high" in url
433 assert "failure=critical" in url
434 assert "group=g" in url
435
436 again = Apprise.instantiate(url)
437 assert isinstance(again, NotifyYouLMK)
438 assert again.token == TOKEN
439 assert again.priority == "high"
440 assert again.priority_map[NotifyType.FAILURE] == "critical"
441 assert again.group == "g"
442 assert again.url_identifier == obj.url_identifier
443
444 # Two credentials are two identifiers
445 other = Apprise.instantiate(f"youlmk://{KEY}")
446 assert other.url_identifier != obj.url_identifier
447
448
449 def test_plugin_youlmk_parse_url():
450 """NotifyYouLMK() parse_url()."""
451
452 # An unparseable (non-string) URL returns None
453 assert NotifyYouLMK.parse_url(None) is None
454
455 results = NotifyYouLMK.parse_url(f"youlmk://{TOKEN}")
456 assert results["token"] == TOKEN
457 assert "priority" not in results
458 assert "link" not in results
459
460 results = NotifyYouLMK.parse_url(
461 f"youlmk://ignored?token={TOKEN}&priority=low&info=high"
462 "&url=https://example.com&group=g"
463 )
464 assert results["token"] == TOKEN
465 assert results["priority"] == "low"
466 assert results["info"] == "high"
467 assert results["link"] == "https://example.com"
468 assert results["group"] == "g"
469
470
471 @mock.patch("requests.post")
472 def test_plugin_youlmk_url_label(mock_post):
473 """NotifyYouLMK() The primary button carries its own wording."""
474
475 response = mock.Mock()
476 response.status_code = requests.codes.ok
477 mock_post.return_value = response
478
479 obj = Apprise.instantiate(
480 f"youlmk://{TOKEN}?url=https://example.com&url_label=View%20run"
481 )
482 assert obj.url_label == "View run"
483
484 assert obj.notify(title="t", body="b") is True
485 payload = loads(mock_post.call_args[1]["data"])
486 assert payload["url_label"] == "View run"
487
488 # And it comes back out of the URL
489 assert "url_label=View%20run" in obj.url()
490
491 # Anything over the payload's limit is trimmed
492 obj = NotifyYouLMK(
493 token=TOKEN, link="https://example.com", url_label="x" * 40
494 )
495 assert obj.url_label == "x" * 24
496
497 # A label with no address to sit on is left out of the payload
498 mock_post.reset_mock()
499 obj = NotifyYouLMK(token=TOKEN, url_label="View run")
500 assert obj.notify(title="t", body="b") is True
501 payload = loads(mock_post.call_args[1]["data"])
502 assert "url" not in payload
503 assert "url_label" not in payload
504
505
506 @mock.patch("requests.post")
507 def test_plugin_youlmk_image(mock_post):
508 """NotifyYouLMK() The notification-type image is opt-in."""
509
510 response = mock.Mock()
511 response.status_code = requests.codes.ok
512 mock_post.return_value = response
513
514 # Off by default
515 obj = Apprise.instantiate(f"youlmk://{TOKEN}")
516 assert obj.include_image is False
517 assert obj.notify(title="t", body="b") is True
518 assert "image" not in loads(mock_post.call_args[1]["data"])
519 assert "image=no" in obj.url()
520
521 # On when asked for
522 mock_post.reset_mock()
523 obj = Apprise.instantiate(f"youlmk://{TOKEN}?image=yes")
524 assert obj.include_image is True
525 assert obj.notify(title="t", body="b") is True
526 assert loads(mock_post.call_args[1]["data"])["image"].startswith("http")
527 assert "image=yes" in obj.url()
528
529 # An asset with no imagery leaves the field out
530 mock_post.reset_mock()
531 asset = AppriseAsset(image_url_mask=None, image_path_mask=None)
532 obj = Apprise.instantiate(f"youlmk://{TOKEN}?image=yes", asset=asset)
533 assert obj.notify(title="t", body="b") is True
534 assert "image" not in loads(mock_post.call_args[1]["data"])
535
536
537 def test_plugin_youlmk_parse_native_url():
538 """NotifyYouLMK() parse_native_url()."""
539
540 # The key door pasted straight from the source's Key screen
541 results = NotifyYouLMK.parse_native_url(f"https://youlmk.com/k/{KEY}")
542 assert results["token"] == KEY
543
544 # A www. prefix, a trailing slash and query arguments all travel
545 results = NotifyYouLMK.parse_native_url(
546 f"https://www.youlmk.com/k/{KEY}/?priority=low"
547 )
548 assert results["token"] == KEY
549 assert results["priority"] == "low"
550
551 # The bearer door is not a key door
552 assert (
553 NotifyYouLMK.parse_native_url("https://youlmk.com/v1/notify") is None
554 )
555
556
557 def test_plugin_youlmk_priority_resolution():
558 """youlmk_priority() resolves short forms and refuses the rest."""
559
560 assert youlmk_priority("low") == "low"
561 assert youlmk_priority("l") == "low"
562 assert youlmk_priority("NORMAL") == "normal"
563 assert youlmk_priority("hi") == "high"
564 assert youlmk_priority("crit") == "critical"
565 assert youlmk_priority("") is None
566 assert youlmk_priority(" ") is None
567 assert youlmk_priority("bogus") is None