Debian Codegraph @

tests/test_plugin_dot.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 json
30 import logging
31 from unittest import mock
32 from urllib.parse import parse_qs, urlparse
33
34 from helpers import AppriseURLTester
35
36 from apprise.plugins.dot import NotifyDot
37
38
39 class DummyAttachment:
40 def __init__(self, payload="ZmFjZQ=="):
41 self._payload = payload
42
43 def base64(self):
44 return self._payload
45
46
47 logging.disable(logging.CRITICAL)
48
49 # Our Testing URLs
50 apprise_url_tests = (
51 (
52 "dot://",
53 {
54 # No API key or device ID
55 "instance": None,
56 },
57 ),
58 (
59 "dot://@",
60 {
61 # No device ID
62 "instance": None,
63 },
64 ),
65 (
66 "dot://apikey@",
67 {
68 # No device ID
69 "instance": None,
70 },
71 ),
72 (
73 "dot://@device_id",
74 {
75 # No API key — loads but notify() returns False
76 "instance": NotifyDot,
77 "notify_response": False,
78 },
79 ),
80 (
81 "dot://apikey@device_id/",
82 {
83 # Default text mode
84 "instance": NotifyDot,
85 "privacy_url": "dot://****@device_id/?",
86 },
87 ),
88 (
89 "dot://apikey@device_id/?refresh=no",
90 {
91 # Disable refresh now
92 "instance": NotifyDot,
93 },
94 ),
95 (
96 "dot://apikey@device_id/?signature=test_signature",
97 {
98 # With signature (text mode default)
99 "instance": NotifyDot,
100 },
101 ),
102 (
103 "dot://apikey@device_id/?link=https://example.com",
104 {
105 # With link (text mode default)
106 "instance": NotifyDot,
107 },
108 ),
109 (
110 "dot://apikey@device_id/?mode=text",
111 {
112 # Explicit text mode (same as default)
113 "instance": NotifyDot,
114 "privacy_url": "dot://****@device_id/?",
115 },
116 ),
117 (
118 # Backward-compat: /text/ path still parsed as text mode
119 "dot://apikey@device_id/text/",
120 {
121 "instance": NotifyDot,
122 "privacy_url": "dot://****@device_id/?",
123 },
124 ),
125 (
126 "dot://apikey@device_id/?mode=image&image=ZmFrZUJhc2U2NA==",
127 {
128 # Explicit image mode via ?mode= query param
129 "instance": NotifyDot,
130 "privacy_url": "dot://****@device_id/?",
131 },
132 ),
133 (
134 "dot://apikey@device_id/?image=ZmFrZUJhc2U2NA==",
135 {
136 # image= without mode= stays in default text mode (dual-send)
137 "instance": NotifyDot,
138 "privacy_url": "dot://****@device_id/?",
139 },
140 ),
141 (
142 "dot://apikey@device_id/image/?link=https://example.com"
143 "&border=1&dither_type=ORDERED&dither_kernel=ATKINSON",
144 {
145 # Backward-compat /image/ path; no image payload -> fail
146 "instance": NotifyDot,
147 "notify_response": False,
148 "attach_response": True,
149 },
150 ),
151 (
152 "dot://apikey@device_id/image/?image=ZmFrZUJhc2U2NA=="
153 "&link=https://example.com&border=1"
154 "&dither_type=DIFFUSION&dither_kernel=FLOYD_STEINBERG",
155 {
156 # Backward-compat /image/ path with image data
157 "instance": NotifyDot,
158 "privacy_url": "dot://****@device_id/?",
159 },
160 ),
161 (
162 "dot://apikey@device_id/",
163 {
164 "instance": NotifyDot,
165 # Throw a bizarre code forcing us to fail to look it up
166 "response": False,
167 "requests_response_code": 999,
168 },
169 ),
170 (
171 "dot://apikey@device_id/",
172 {
173 "instance": NotifyDot,
174 # Throws a series of i/o exceptions
175 "test_requests_exceptions": True,
176 },
177 ),
178 (
179 "dot://apikey@device_id/unknown/",
180 {
181 # Unknown path token defaults to text mode
182 "instance": NotifyDot,
183 "privacy_url": "dot://****@device_id/?",
184 },
185 ),
186 )
187
188
189 def test_plugin_dot_urls():
190 """NotifyDot() Apprise URLs."""
191 AppriseURLTester(tests=apprise_url_tests).run_all()
192
193
194 # ---------------------------------------------------------------------------
195 # Mode and initialization
196 # ---------------------------------------------------------------------------
197
198
199 def test_notify_dot_default_mode():
200 """Default mode is text."""
201 dot = NotifyDot(apikey="key", device_id="dev")
202 assert dot.mode == "text"
203
204
205 def test_notify_dot_invalid_mode():
206 """Invalid mode falls back to text."""
207 dot = NotifyDot(apikey="token", device_id="device", mode="invalid_mode")
208 assert dot.mode == "text"
209
210 dot = NotifyDot(apikey="token", device_id="device", mode=123)
211 assert dot.mode == "text"
212
213
214 def test_notify_dot_explicit_text_mode():
215 """Explicit text mode is honoured."""
216 dot = NotifyDot(apikey="key", device_id="dev", mode="text")
217 assert dot.mode == "text"
218
219
220 def test_notify_dot_explicit_image_mode():
221 """Explicit image mode is honoured."""
222 dot = NotifyDot(apikey="key", device_id="dev", mode="image")
223 assert dot.mode == "image"
224
225
226 def test_notify_dot_image_data_kept_in_text_mode():
227 """image_data is preserved in text mode for dual-send."""
228 dot = NotifyDot(
229 apikey="token",
230 device_id="device",
231 mode="text",
232 image_data="somebase64",
233 )
234 assert dot.image_data == "somebase64"
235
236
237 # ---------------------------------------------------------------------------
238 # Text mode (default) — send logic
239 # ---------------------------------------------------------------------------
240
241
242 def test_notify_dot_auto_text_only():
243 """Default text mode sends text only when no image is available."""
244 dot = NotifyDot(apikey="token", device_id="device")
245 resp = mock.Mock()
246 resp.status_code = 200
247
248 with mock.patch("requests.post", return_value=resp) as mock_post:
249 assert dot.send(body="hello", title="world")
250
251 assert mock_post.call_count == 1
252 url = mock_post.call_args[0][0]
253 assert "/text" in url
254 payload = json.loads(mock_post.call_args[1]["data"])
255 assert payload["message"] == "hello"
256 assert payload["title"] == "world"
257
258
259 def test_notify_dot_auto_image_only_from_image_data():
260 """Auto mode sends image only when image_data set but no body/title."""
261 dot = NotifyDot(
262 apikey="token",
263 device_id="device",
264 image_data="base64img",
265 )
266 resp = mock.Mock()
267 resp.status_code = 200
268
269 with mock.patch("requests.post", return_value=resp) as mock_post:
270 assert dot.send(body="", title="")
271
272 assert mock_post.call_count == 1
273 url = mock_post.call_args[0][0]
274 assert "/image" in url
275 payload = json.loads(mock_post.call_args[1]["data"])
276 assert payload["image"] == "base64img"
277
278
279 def test_notify_dot_auto_dual_send_text_then_image():
280 """Auto mode sends text first, then image, when both are present."""
281 dot = NotifyDot(
282 apikey="token",
283 device_id="device",
284 image_data="base64img",
285 )
286 resp = mock.Mock()
287 resp.status_code = 200
288
289 with mock.patch("requests.post", return_value=resp) as mock_post:
290 assert dot.send(body="hello", title="world")
291
292 assert mock_post.call_count == 2
293 first_url = mock_post.call_args_list[0][0][0]
294 second_url = mock_post.call_args_list[1][0][0]
295 assert "/text" in first_url
296 assert "/image" in second_url
297
298
299 def test_notify_dot_auto_dual_send_with_attachment():
300 """Auto mode sends text first then attachment as image."""
301 dot = NotifyDot(apikey="token", device_id="device")
302 resp = mock.Mock()
303 resp.status_code = 200
304
305 with mock.patch("requests.post", return_value=resp) as mock_post:
306 assert dot.send(
307 body="hello",
308 title="world",
309 attach=[DummyAttachment("imgdata")],
310 )
311
312 assert mock_post.call_count == 2
313 first_url = mock_post.call_args_list[0][0][0]
314 second_url = mock_post.call_args_list[1][0][0]
315 assert "/text" in first_url
316 assert "/image" in second_url
317 payload = json.loads(mock_post.call_args_list[1][1]["data"])
318 assert payload["image"] == "imgdata"
319
320
321 def test_notify_dot_auto_image_from_attachment_no_body():
322 """Auto mode sends image-only when attachment given but no body."""
323 dot = NotifyDot(apikey="token", device_id="device")
324 resp = mock.Mock()
325 resp.status_code = 200
326
327 with mock.patch("requests.post", return_value=resp) as mock_post:
328 assert dot.send(body="", title="", attach=[DummyAttachment("imgdata")])
329
330 assert mock_post.call_count == 1
331 assert "/image" in mock_post.call_args[0][0]
332
333
334 def test_notify_dot_auto_nothing_to_send():
335 """Auto mode returns False when nothing is available to send."""
336 dot = NotifyDot(apikey="token", device_id="device")
337 assert dot.send(body="", title="") is False
338
339
340 def test_notify_dot_auto_partial_failure():
341 """Auto mode returns False when text succeeds but image fails."""
342 dot = NotifyDot(
343 apikey="token",
344 device_id="device",
345 image_data="base64img",
346 )
347
348 def side_effect(*args, **kwargs):
349 resp = mock.Mock()
350 url = args[0]
351 resp.status_code = 200 if "/text" in url else 500
352 resp.content = b""
353 return resp
354
355 with mock.patch("requests.post", side_effect=side_effect):
356 assert dot.send(body="hello", title="") is False
357
358
359 def test_notify_dot_auto_icon_passthrough():
360 """Auto mode passes self.icon to the text API call."""
361 dot = NotifyDot(apikey="token", device_id="device", icon="aW1h")
362 resp = mock.Mock()
363 resp.status_code = 200
364
365 with mock.patch("requests.post", return_value=resp) as mock_post:
366 assert dot.send(body="hello", title="")
367
368 payload = json.loads(mock_post.call_args[1]["data"])
369 assert payload["icon"] == "aW1h"
370
371
372 # ---------------------------------------------------------------------------
373 # Explicit image mode
374 # ---------------------------------------------------------------------------
375
376
377 def test_notify_dot_image_mode_requires_image():
378 """Explicit image mode fails without image data."""
379 dot = NotifyDot(apikey="token", device_id="device", mode="image")
380 assert dot.notify(title="x", body="y") is False
381
382
383 def test_notify_dot_image_mode_with_attachment():
384 """Image mode uses first attachment when no image_data provided."""
385 dot = NotifyDot(
386 apikey="token",
387 device_id="device",
388 mode="image",
389 link="https://example.com",
390 border=1,
391 dither_type="ORDERED",
392 dither_kernel="ATKINSON",
393 )
394 resp = mock.Mock()
395 resp.status_code = 200
396
397 with mock.patch("requests.post", return_value=resp) as mock_post:
398 assert dot.send(
399 body="payload",
400 title="title",
401 attach=[DummyAttachment("YmFzZTY0")],
402 )
403
404 payload = json.loads(mock_post.call_args[1]["data"])
405 assert payload["image"] == "YmFzZTY0"
406 assert "deviceId" not in payload
407
408
409 def test_notify_dot_image_mode_with_existing_image_data():
410 """Image mode ignores attachment when image_data is already set."""
411 dot = NotifyDot(
412 apikey="token",
413 device_id="device",
414 mode="image",
415 image_data="existing_image_data",
416 )
417 resp = mock.Mock()
418 resp.status_code = 200
419
420 with mock.patch("requests.post", return_value=resp) as mock_post:
421 assert dot.send(
422 body="test",
423 title="test",
424 attach=[DummyAttachment("attachment_data")],
425 )
426
427 payload = json.loads(mock_post.call_args[1]["data"])
428 assert payload["image"] == "existing_image_data"
429
430
431 def test_notify_dot_image_mode_multiple_attachments():
432 """Image mode uses only the first attachment."""
433 dot = NotifyDot(apikey="token", device_id="device", mode="image")
434 resp = mock.Mock()
435 resp.status_code = 200
436
437 with mock.patch("requests.post", return_value=resp) as mock_post:
438 assert dot.send(
439 body="test",
440 title="test",
441 attach=[
442 DummyAttachment("first_attachment"),
443 DummyAttachment("second_attachment"),
444 ],
445 )
446
447 payload = json.loads(mock_post.call_args[1]["data"])
448 assert payload["image"] == "first_attachment"
449
450
451 def test_notify_dot_image_mode_with_failed_attachment():
452 """Image mode warns and fails when attachment.base64() raises."""
453
454 class FailedAttachment:
455 def base64(self):
456 raise Exception("Conversion failed")
457
458 dot = NotifyDot(apikey="token", device_id="device", mode="image")
459 assert (
460 dot.notify(title="test", body="test", attach=[FailedAttachment()])
461 is False
462 )
463
464
465 def test_notify_dot_image_mode_attachment_none():
466 """Image mode fails when attachment is None."""
467 dot = NotifyDot(apikey="token", device_id="device", mode="image")
468 assert dot.send(body="test", title="test", attach=[None]) is False
469
470
471 def test_notify_dot_image_mode_attachment_falsy():
472 """Image mode fails when attachment is falsy."""
473
474 class FalsyAttachment:
475 def __bool__(self):
476 return False
477
478 def base64(self):
479 return "should_not_be_called"
480
481 dot = NotifyDot(apikey="token", device_id="device", mode="image")
482 assert (
483 dot.send(body="test", title="test", attach=[FalsyAttachment()])
484 is False
485 )
486
487
488 def test_notify_dot_image_mode_no_border():
489 """Image mode omits border from payload when None."""
490 dot = NotifyDot(
491 apikey="token",
492 device_id="device",
493 mode="image",
494 image_data="base64img",
495 )
496 dot.border = None
497 resp = mock.Mock()
498 resp.status_code = 200
499
500 with mock.patch("requests.post", return_value=resp) as mock_post:
501 assert dot.send(body="test", title="test")
502
503 payload = json.loads(mock_post.call_args[1]["data"])
504 assert "border" not in payload
505
506
507 def test_notify_dot_image_mode_no_dither():
508 """Image mode omits dither fields from payload when None."""
509 dot = NotifyDot(
510 apikey="token",
511 device_id="device",
512 mode="image",
513 image_data="base64img",
514 )
515 dot.dither_type = None
516 dot.dither_kernel = None
517 resp = mock.Mock()
518 resp.status_code = 200
519
520 with mock.patch("requests.post", return_value=resp) as mock_post:
521 assert dot.send(body="test", title="test")
522
523 payload = json.loads(mock_post.call_args[1]["data"])
524 assert "ditherType" not in payload
525 assert "ditherKernel" not in payload
526
527
528 def test_notify_dot_image_mode_title_body_ignored():
529 """Image mode warns when title or body are provided."""
530 dot = NotifyDot(
531 apikey="token",
532 device_id="device",
533 mode="image",
534 image_data="base64img",
535 )
536 resp = mock.Mock()
537 resp.status_code = 200
538
539 with mock.patch("requests.post", return_value=resp) as mock_post:
540 assert dot.send(title="ignored", body="also ignored")
541
542 payload = json.loads(mock_post.call_args[1]["data"])
543 assert "title" not in payload
544 assert "message" not in payload
545
546
547 # ---------------------------------------------------------------------------
548 # Explicit text mode
549 # ---------------------------------------------------------------------------
550
551
552 def test_notify_dot_text_mode_with_existing_icon():
553 """Text mode: icon= param with Text API; attachment goes to Image API."""
554 dot = NotifyDot(
555 apikey="token",
556 device_id="device",
557 mode="text",
558 signature="footer",
559 icon="aW5jb24=",
560 )
561 resp = mock.Mock()
562 resp.status_code = 200
563
564 with mock.patch("requests.post", return_value=resp) as mock_post:
565 assert dot.send(
566 title="hello",
567 body="world",
568 attach=[DummyAttachment("attachment_image")],
569 )
570
571 # Two POSTs: text API first, image API second
572 assert mock_post.call_count == 2
573 text_payload = json.loads(mock_post.call_args_list[0][1]["data"])
574 image_payload = json.loads(mock_post.call_args_list[1][1]["data"])
575 assert text_payload["message"] == "world"
576 assert text_payload["icon"] == "aW5jb24="
577 assert "image" not in text_payload
578 assert image_payload["image"] == "attachment_image"
579
580
581 def test_notify_dot_text_mode_uses_attachment_as_image():
582 """Text mode sends attachment to Image API when no icon= is set."""
583 dot = NotifyDot(
584 apikey="token",
585 device_id="device",
586 mode="text",
587 signature="footer",
588 )
589 resp = mock.Mock()
590 resp.status_code = 200
591
592 with mock.patch("requests.post", return_value=resp) as mock_post:
593 assert dot.send(
594 title="hello",
595 body="world",
596 attach=[DummyAttachment("attachment_image_data")],
597 )
598
599 assert mock_post.call_count == 2
600 text_payload = json.loads(mock_post.call_args_list[0][1]["data"])
601 image_payload = json.loads(mock_post.call_args_list[1][1]["data"])
602 assert text_payload["message"] == "world"
603 assert "icon" not in text_payload
604 assert image_payload["image"] == "attachment_image_data"
605
606
607 def test_notify_dot_text_mode_multiple_attachments_warning():
608 """Text mode warns when multiple attachments are provided."""
609 dot = NotifyDot(apikey="token", device_id="device", mode="text")
610 resp = mock.Mock()
611 resp.status_code = 200
612
613 with (
614 mock.patch("requests.post", return_value=resp) as mock_post,
615 mock.patch.object(dot.logger, "warning") as mock_warning,
616 ):
617 assert dot.send(
618 title="hello",
619 body="world",
620 attach=[
621 DummyAttachment("first"),
622 DummyAttachment("second"),
623 ],
624 )
625 mock_warning.assert_called_once()
626 assert "Multiple attachments" in str(mock_warning.call_args)
627
628 # First attachment sent as image to Image API (second POST)
629 assert mock_post.call_count == 2
630 image_payload = json.loads(mock_post.call_args_list[1][1]["data"])
631 assert image_payload["image"] == "first"
632
633
634 def test_notify_dot_text_mode_no_optional_fields():
635 """Text mode omits optional fields when not set."""
636 dot = NotifyDot(apikey="token", device_id="device", mode="text")
637 resp = mock.Mock()
638 resp.status_code = 200
639
640 with mock.patch("requests.post", return_value=resp) as mock_post:
641 assert dot.send(body="test body", title="test title")
642
643 payload = json.loads(mock_post.call_args[1]["data"])
644 assert "signature" not in payload
645 assert "icon" not in payload
646 assert payload.get("link") is None or "link" not in payload
647
648
649 def test_notify_dot_text_mode_with_title_and_body():
650 """Text mode sends title and body correctly."""
651 dot = NotifyDot(apikey="token", device_id="device", mode="text")
652 resp = mock.Mock()
653 resp.status_code = 200
654
655 with mock.patch("requests.post", return_value=resp) as mock_post:
656 assert dot.send(body="test_body", title="test_title")
657
658 payload = json.loads(mock_post.call_args[1]["data"])
659 assert payload["message"] == "test_body"
660 assert payload["title"] == "test_title"
661
662
663 def test_notify_dot_text_mode_without_title():
664 """Text mode omits title from payload when empty."""
665 dot = NotifyDot(apikey="token", device_id="device", mode="text")
666 resp = mock.Mock()
667 resp.status_code = 200
668
669 with mock.patch("requests.post", return_value=resp) as mock_post:
670 assert dot.send(body="test message", title="")
671
672 payload = json.loads(mock_post.call_args[1]["data"])
673 assert "title" not in payload
674 assert payload["message"] == "test message"
675
676
677 def test_notify_dot_text_mode_without_body():
678 """Text mode omits message from payload when body is empty."""
679 dot = NotifyDot(apikey="token", device_id="device", mode="text")
680 resp = mock.Mock()
681 resp.status_code = 200
682
683 with mock.patch("requests.post", return_value=resp) as mock_post:
684 assert dot.send(body="", title="test_title")
685
686 payload = json.loads(mock_post.call_args[1]["data"])
687 assert payload["title"] == "test_title"
688 assert "message" not in payload
689
690
691 def test_notify_dot_text_mode_attachment_exception():
692 """Text mode warns on attachment error; text still sends without image."""
693
694 class ExceptionAttachment:
695 def base64(self):
696 raise Exception("Attachment base64 conversion fails")
697
698 dot = NotifyDot(apikey="token", device_id="device", mode="text")
699 resp = mock.Mock()
700 resp.status_code = 200
701
702 with (
703 mock.patch("requests.post", return_value=resp) as mock_post,
704 mock.patch.object(dot.logger, "warning") as mock_warning,
705 ):
706 assert dot.send(
707 title="hello",
708 body="world",
709 attach=[ExceptionAttachment()],
710 )
711 assert mock_warning.called
712 assert any(
713 "Failed to process attachment" in str(c)
714 for c in mock_warning.call_args_list
715 )
716
717 # Only text API called (image_data resolved to None on exception)
718 assert mock_post.call_count == 1
719 payload = json.loads(mock_post.call_args[1]["data"])
720 assert payload["message"] == "world"
721 assert "icon" not in payload
722
723
724 def test_notify_dot_text_mode_attachment_none():
725 """Text mode sends only text when attachment is None."""
726 dot = NotifyDot(apikey="token", device_id="device", mode="text")
727 resp = mock.Mock()
728 resp.status_code = 200
729
730 with mock.patch("requests.post", return_value=resp) as mock_post:
731 assert dot.send(title="hello", body="world", attach=[None])
732
733 assert mock_post.call_count == 1
734 payload = json.loads(mock_post.call_args[1]["data"])
735 assert payload["message"] == "world"
736 assert "icon" not in payload
737
738
739 def test_notify_dot_text_mode_attachment_falsy():
740 """Text mode sends only text when attachment is falsy."""
741
742 class FalsyAttachment:
743 def __bool__(self):
744 return False
745
746 def base64(self):
747 return "should_not_be_called"
748
749 dot = NotifyDot(apikey="token", device_id="device", mode="text")
750 resp = mock.Mock()
751 resp.status_code = 200
752
753 with mock.patch("requests.post", return_value=resp) as mock_post:
754 assert dot.send(
755 title="hello",
756 body="world",
757 attach=[FalsyAttachment()],
758 )
759
760 assert mock_post.call_count == 1
761 payload = json.loads(mock_post.call_args[1]["data"])
762 assert payload["message"] == "world"
763 assert "icon" not in payload
764
765
766 def test_notify_dot_title_handling():
767 """Text mode omits title from payload when empty."""
768 dot = NotifyDot(apikey="token", device_id="device", mode="text")
769 resp = mock.Mock()
770 resp.status_code = 200
771
772 with mock.patch("requests.post", return_value=resp) as mock_post:
773 assert dot.send(body="test", title="provided_title")
774 payload = json.loads(mock_post.call_args[1]["data"])
775 assert payload["title"] == "provided_title"
776
777 with mock.patch("requests.post", return_value=resp) as mock_post:
778 assert dot.send(body="test", title="")
779 payload = json.loads(mock_post.call_args[1]["data"])
780 assert "title" not in payload
781
782
783 # ---------------------------------------------------------------------------
784 # URL generation
785 # ---------------------------------------------------------------------------
786
787
788 def test_notify_dot_url_generation():
789 """URL generation for text (default) and image modes."""
790 # text mode (default) — no mode param emitted
791 text_dot = NotifyDot(
792 apikey="token",
793 device_id="device",
794 signature="sig",
795 icon="aW5jb24=",
796 )
797 text_url = text_dot.url()
798 parsed = urlparse(text_url)
799 assert parsed.path == "/"
800 query = parse_qs(parsed.query)
801 assert query["refresh"] == ["yes"]
802 assert query["signature"] == ["sig"]
803 assert "mode" not in query
804
805 # Explicit image mode — mode=image in params
806 image_dot = NotifyDot(
807 apikey="token",
808 device_id="device",
809 mode="image",
810 image_data="aW1hZ2U=",
811 link="https://example.com",
812 border=1,
813 dither_type="ORDERED",
814 dither_kernel="ATKINSON",
815 )
816 image_url = image_dot.url()
817 image_query = parse_qs(urlparse(image_url).query)
818 assert image_query["mode"] == ["image"]
819 assert image_query["image"] == ["aW1hZ2U="]
820 assert image_query["border"] == ["1"]
821
822
823 def test_notify_dot_url_generation_defaults():
824 """Default text mode omits mode param; image mode includes it."""
825 dot = NotifyDot(apikey="token", device_id="device")
826 url = dot.url()
827 assert "refresh=yes" in url
828 assert "mode=" not in url
829
830 dot_image = NotifyDot(
831 apikey="token",
832 device_id="device",
833 mode="image",
834 image_data="img",
835 dither_type="ORDERED",
836 dither_kernel="ATKINSON",
837 )
838 url_image = dot_image.url()
839 assert "mode=image" in url_image
840 assert "dither_type=ORDERED" in url_image
841 assert "dither_kernel=ATKINSON" in url_image
842
843
844 def test_notify_dot_url_generation_defaults_no_dither():
845 """Default dither values are omitted from URL."""
846 dot = NotifyDot(
847 apikey="token",
848 device_id="device",
849 mode="image",
850 image_data="img",
851 dither_type="DIFFUSION",
852 dither_kernel="FLOYD_STEINBERG",
853 )
854 url = dot.url()
855 assert "dither_type" not in url
856 assert "dither_kernel" not in url
857
858
859 def test_notify_dot_url_with_border():
860 """URL includes border when set."""
861 dot = NotifyDot(
862 apikey="token",
863 device_id="device",
864 mode="image",
865 image_data="img",
866 border=1,
867 )
868 assert "border=1" in dot.url()
869
870
871 def test_notify_dot_url_with_link():
872 """URL includes link when set."""
873 dot = NotifyDot(
874 apikey="token",
875 device_id="device",
876 link="https://example.com",
877 )
878 assert "link=" in dot.url()
879
880
881 def test_notify_dot_url_with_task_key():
882 """URL includes task_key when set."""
883 dot = NotifyDot(
884 apikey="token",
885 device_id="device",
886 task_key="url_task",
887 )
888 assert "task_key=url_task" in dot.url()
889
890
891 # ---------------------------------------------------------------------------
892 # parse_url
893 # ---------------------------------------------------------------------------
894
895
896 def test_notify_dot_parse_url_mode_and_image():
897 """parse_url handles image mode via path and query param."""
898 # Backward-compat: /image/ path selects image mode
899 result = NotifyDot.parse_url(
900 "dot://token@device/image/?image=Zm9vYmFy&link=https://example.com"
901 )
902 assert result["mode"] == "image"
903 assert result["image_data"] == "Zm9vYmFy"
904 assert result["link"] == "https://example.com"
905
906 # New-style: ?mode= query param
907 result2 = NotifyDot.parse_url(
908 "dot://token@device/?mode=image&image=Zm9vYmFy"
909 )
910 assert result2["mode"] == "image"
911 assert result2["image_data"] == "Zm9vYmFy"
912
913 # image= without mode= stays as default text mode (dual-send)
914 result3 = NotifyDot.parse_url("dot://token@device/?image=Zm9vYmFy")
915 assert result3["mode"] == "text"
916 assert result3["image_data"] == "Zm9vYmFy"
917
918 # Invalid ?mode= falls back to text (default)
919 result4 = NotifyDot.parse_url("dot://token@device/?mode=invalid")
920 assert result4["mode"] == "text"
921
922 # Unknown path token falls back to text (default)
923 fallback = NotifyDot.parse_url("dot://token@device/unknown/?refresh=no")
924 assert fallback["mode"] == "text"
925 assert fallback["refresh_now"] is False
926
927
928 def test_notify_dot_parse_url_with_all_params():
929 """parse_url handles all parameters."""
930 result = NotifyDot.parse_url(
931 "dot://apikey@device/image/?refresh=yes&signature=sig&icon=icon_b64"
932 "&link=https://example.com&border=1&dither_type=ORDERED"
933 "&dither_kernel=ATKINSON&image=img_b64"
934 )
935 assert result["mode"] == "image"
936 assert result["refresh_now"] is True
937 assert result["signature"] == "sig"
938 assert result["icon"] == "icon_b64"
939 assert result["link"] == "https://example.com"
940 assert result["border"] == 1
941 assert result["dither_type"] == "ORDERED"
942 assert result["dither_kernel"] == "ATKINSON"
943 assert result["image_data"] == "img_b64"
944
945
946 def test_notify_dot_parse_url_no_host():
947 """parse_url succeeds when host provides device_id."""
948 result = NotifyDot.parse_url("dot://apikey@device/text/")
949 assert result is not None
950 assert result.get("device_id") == "device"
951
952
953 def test_notify_dot_parse_url_without_host():
954 """parse_url returns None when host is empty."""
955 assert NotifyDot.parse_url("dot://apikey@/text/") is None
956
957
958 def test_notify_dot_parse_url_with_empty_refresh():
959 """parse_url omits refresh_now when not specified."""
960 result = NotifyDot.parse_url("dot://apikey@device/text/")
961 assert result is not None
962 assert result.get("refresh_now") is None
963
964
965 def test_notify_dot_parse_url_task_key():
966 """parse_url extracts task_key."""
967 result = NotifyDot.parse_url("dot://apikey@device_id/?task_key=parsed")
968 assert result is not None
969 assert result["task_key"] == "parsed"
970
971
972 def test_notify_dot_parse_url_without_host_field():
973 """parse_url with host=None sets no device_id."""
974 from apprise import NotifyBase
975
976 with mock.patch.object(NotifyBase, "parse_url") as mock_parse:
977 mock_parse.return_value = {
978 "user": "apikey",
979 "password": None,
980 "port": None,
981 "host": None,
982 "fullpath": "/text/",
983 "path": "",
984 "query": None,
985 "schema": "dot",
986 "qsd": {"refresh": "yes"},
987 "secure": False,
988 "verify": True,
989 }
990 result = NotifyDot.parse_url("dot://fake")
991 assert result is not None
992 assert result.get("mode") == "text"
993 assert result.get("device_id") is None
994 assert result.get("apikey") == "apikey"
995 assert result.get("refresh_now") is True
996
997
998 # ---------------------------------------------------------------------------
999 # url_identifier
1000 # ---------------------------------------------------------------------------
1001
1002
1003 def test_notify_dot_url_identifier():
1004 """url_identifier includes mode."""
1005 dot = NotifyDot(apikey="token", device_id="device", mode="image")
1006 assert dot.url_identifier == ("dot", "token", "device", "image")
1007
1008 dot_text = NotifyDot(apikey="token", device_id="device")
1009 assert dot_text.url_identifier == ("dot", "token", "device", "text")
1010
1011
1012 # ---------------------------------------------------------------------------
1013 # URL round-trip
1014 # ---------------------------------------------------------------------------
1015
1016
1017 def test_notify_dot_url_roundtrip_with_task_key():
1018 """parse_url -> NotifyDot -> url() preserves task_key and signature."""
1019 original = "dot://apikey@device123/text/?task_key=test&signature=sig"
1020 result = NotifyDot.parse_url(original)
1021 dot = NotifyDot(**result)
1022 reconstructed = dot.url(privacy=False)
1023
1024 assert "device123" in reconstructed
1025 # text is the default mode; mode= is not emitted
1026 assert "mode=" not in reconstructed
1027 assert "task_key=test" in reconstructed
1028 assert "signature=sig" in reconstructed
1029
1030
1031 # ---------------------------------------------------------------------------
1032 # Missing credentials
1033 # ---------------------------------------------------------------------------
1034
1035
1036 def test_notify_dot_no_device_id():
1037 """send() returns False when device_id is missing."""
1038 dot = NotifyDot(apikey="token", device_id=None)
1039 assert dot.notify(title="test", body="test") is False
1040 assert len(dot) == 0
1041
1042
1043 # ---------------------------------------------------------------------------
1044 # API v2 endpoint construction
1045 # ---------------------------------------------------------------------------
1046
1047
1048 def test_notify_dot_api_v2_endpoints():
1049 """API v2 endpoints embed device_id in URL path."""
1050 dot = NotifyDot(apikey="test_key", device_id="DEVICE123", mode="text")
1051 assert dot.text_api_url == (
1052 "https://dot.mindreset.tech/api/authV2/open/device/DEVICE123/text"
1053 )
1054
1055 dot_img = NotifyDot(apikey="test_key", device_id="DEVICE456", mode="image")
1056 assert dot_img.image_api_url == (
1057 "https://dot.mindreset.tech/api/authV2/open/device/DEVICE456/image"
1058 )
1059
1060
1061 def test_notify_dot_deviceid_not_in_payload():
1062 """deviceId is NOT in text or image payload (API v2)."""
1063 dot = NotifyDot(apikey="test_key", device_id="12345", mode="text")
1064 resp = mock.Mock()
1065 resp.status_code = 200
1066
1067 with mock.patch("requests.post", return_value=resp) as mock_post:
1068 assert dot.send("test body", "test title")
1069 payload = json.loads(mock_post.call_args[1]["data"])
1070 assert "deviceId" not in payload
1071 assert "/device/12345/text" in mock_post.call_args[0][0]
1072
1073 dot_img = NotifyDot(
1074 apikey="test_key",
1075 device_id="67890",
1076 mode="image",
1077 image_data="base64data",
1078 )
1079 with mock.patch("requests.post", return_value=resp) as mock_post:
1080 assert dot_img.send("", "")
1081 payload = json.loads(mock_post.call_args[1]["data"])
1082 assert "deviceId" not in payload
1083 assert "/device/67890/image" in mock_post.call_args[0][0]
1084
1085
1086 # ---------------------------------------------------------------------------
1087 # task_key
1088 # ---------------------------------------------------------------------------
1089
1090
1091 def test_notify_dot_task_key_in_text_payload():
1092 """taskKey appears in text API payload."""
1093 dot = NotifyDot(apikey="k", device_id="d", mode="text", task_key="my_task")
1094 resp = mock.Mock()
1095 resp.status_code = 200
1096
1097 with mock.patch("requests.post", return_value=resp) as mock_post:
1098 assert dot.send("body", "title")
1099 assert json.loads(mock_post.call_args[1]["data"])["taskKey"] == "my_task"
1100
1101
1102 def test_notify_dot_task_key_in_image_payload():
1103 """taskKey appears in image API payload."""
1104 dot = NotifyDot(
1105 apikey="k",
1106 device_id="d",
1107 mode="image",
1108 task_key="img_task",
1109 image_data="base64",
1110 )
1111 resp = mock.Mock()
1112 resp.status_code = 200
1113
1114 with mock.patch("requests.post", return_value=resp) as mock_post:
1115 assert dot.send("", "")
1116 assert json.loads(mock_post.call_args[1]["data"])["taskKey"] == "img_task"
1117
1118
1119 def test_notify_dot_task_key_none_not_in_payload():
1120 """taskKey is absent from payload when None."""
1121 dot = NotifyDot(apikey="k", device_id="d", mode="text", task_key=None)
1122 resp = mock.Mock()
1123 resp.status_code = 200
1124
1125 with mock.patch("requests.post", return_value=resp) as mock_post:
1126 assert dot.send("test", "title")
1127 assert "taskKey" not in json.loads(mock_post.call_args[1]["data"])
1128
1129
1130 # ---------------------------------------------------------------------------
1131 # refresh_now
1132 # ---------------------------------------------------------------------------
1133
1134
1135 def test_notify_dot_refresh_now_defaults():
1136 """refreshNow defaults to True and appears correctly in payload."""
1137 for refresh_now, expected in ((None, True), (True, True), (False, False)):
1138 dot = NotifyDot(
1139 apikey="k",
1140 device_id="d",
1141 mode="text",
1142 refresh_now=refresh_now,
1143 )
1144 resp = mock.Mock()
1145 resp.status_code = 200
1146
1147 with mock.patch("requests.post", return_value=resp) as mock_post:
1148 assert dot.send("test", "title")
1149 payload = json.loads(mock_post.call_args[1]["data"])
1150 assert payload["refreshNow"] is expected
1151
1152
1153 def test_notify_dot_refresh_now_in_url():
1154 """refresh=yes appears in URL when refresh_now is None (defaults True)."""
1155 dot = NotifyDot(apikey="key", device_id="id", refresh_now=None)
1156 assert "refresh=yes" in dot.url()
1157
1158
1159 # ---------------------------------------------------------------------------
1160 # Authorization header
1161 # ---------------------------------------------------------------------------
1162
1163
1164 def test_notify_dot_authorization_header():
1165 """Authorization header uses Bearer format."""
1166 dot = NotifyDot(apikey="MY_SECRET_KEY", device_id="12345", mode="text")
1167 resp = mock.Mock()
1168 resp.status_code = 200
1169
1170 with mock.patch("requests.post", return_value=resp) as mock_post:
1171 assert dot.send("test", "title")
1172 headers = mock_post.call_args[1]["headers"]
1173 assert headers["Authorization"] == "Bearer MY_SECRET_KEY"
1174
1175
1176 # ---------------------------------------------------------------------------
1177 # Special-character device_id
1178 # ---------------------------------------------------------------------------
1179
1180
1181 def test_notify_dot_device_id_encoding():
1182 """Device IDs with special characters are URL-encoded."""
1183 cases = [
1184 ("device/id", "device%2Fid"),
1185 ("device?id", "device%3Fid"),
1186 ("device#id", "device%23id"),
1187 ("device%id", "device%25id"),
1188 ("device id", "device%20id"),
1189 ("device@id", "device%40id"),
1190 ]
1191 for device_id, expected in cases:
1192 dot = NotifyDot(apikey="test_key", device_id=device_id)
1193 assert expected in dot.url(), (
1194 f"device_id '{device_id}' should encode as '{expected}'"
1195 )
1196
1197
1198 # ---------------------------------------------------------------------------
1199 # Combined parameter tests
1200 # ---------------------------------------------------------------------------
1201
1202
1203 def test_notify_dot_combined_text_params():
1204 """Text mode sends all configured parameters correctly."""
1205 dot = NotifyDot(
1206 apikey="key",
1207 device_id="device",
1208 mode="text",
1209 task_key="combined_task",
1210 refresh_now=False,
1211 signature="test_sig",
1212 link="https://example.com",
1213 )
1214 resp = mock.Mock()
1215 resp.status_code = 200
1216
1217 with mock.patch("requests.post", return_value=resp) as mock_post:
1218 assert dot.send("body", "title")
1219 payload = json.loads(mock_post.call_args[1]["data"])
1220 assert "deviceId" not in payload
1221 assert payload["taskKey"] == "combined_task"
1222 assert payload["refreshNow"] is False
1223 assert payload["signature"] == "test_sig"
1224 assert payload["link"] == "https://example.com"
1225 assert payload["title"] == "title"
1226 assert payload["message"] == "body"
1227
1228
1229 def test_notify_dot_combined_image_params():
1230 """Image mode sends all configured parameters correctly."""
1231 dot = NotifyDot(
1232 apikey="key",
1233 device_id="img_device",
1234 mode="image",
1235 image_data="base64image",
1236 task_key="img_task",
1237 refresh_now=True,
1238 link="https://example.com",
1239 border=1,
1240 dither_type="ORDERED",
1241 dither_kernel="ATKINSON",
1242 )
1243 resp = mock.Mock()
1244 resp.status_code = 200
1245
1246 with mock.patch("requests.post", return_value=resp) as mock_post:
1247 assert dot.send("", "")
1248 payload = json.loads(mock_post.call_args[1]["data"])
1249 assert "deviceId" not in payload
1250 assert payload["image"] == "base64image"
1251 assert payload["taskKey"] == "img_task"
1252 assert payload["refreshNow"] is True
1253 assert payload["link"] == "https://example.com"
1254 assert payload["border"] == 1
1255 assert payload["ditherType"] == "ORDERED"
1256 assert payload["ditherKernel"] == "ATKINSON"
1257 assert "/device/img_device/image" in mock_post.call_args[0][0]
1258
1259
1260 def test_notify_dot_debug_logging_branch():
1261 """Exercise the isEnabledFor(DEBUG) branch inside _post."""
1262 dot = NotifyDot(apikey="key", device_id="dev123", mode="text")
1263 resp = mock.Mock()
1264 resp.status_code = 200
1265
1266 with (
1267 mock.patch.object(dot.logger, "isEnabledFor", return_value=True),
1268 mock.patch("requests.post", return_value=resp),
1269 ):
1270 assert dot.send("body", "title")