| 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 bool(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 |
bool( |
| 461 |
dot.notify(title="test", body="test", attach=[FailedAttachment()]) |
| 462 |
) |
| 463 |
is False |
| 464 |
) |
| 465 |
|
| 466 |
|
| 467 |
def test_notify_dot_image_mode_attachment_none(): |
| 468 |
"""Image mode fails when attachment is None.""" |
| 469 |
dot = NotifyDot(apikey="token", device_id="device", mode="image") |
| 470 |
assert dot.send(body="test", title="test", attach=[None]) is False |
| 471 |
|
| 472 |
|
| 473 |
def test_notify_dot_image_mode_attachment_falsy(): |
| 474 |
"""Image mode fails when attachment is falsy.""" |
| 475 |
|
| 476 |
class FalsyAttachment: |
| 477 |
def __bool__(self): |
| 478 |
return False |
| 479 |
|
| 480 |
def base64(self): |
| 481 |
return "should_not_be_called" |
| 482 |
|
| 483 |
dot = NotifyDot(apikey="token", device_id="device", mode="image") |
| 484 |
assert ( |
| 485 |
dot.send(body="test", title="test", attach=[FalsyAttachment()]) |
| 486 |
is False |
| 487 |
) |
| 488 |
|
| 489 |
|
| 490 |
def test_notify_dot_image_mode_no_border(): |
| 491 |
"""Image mode omits border from payload when None.""" |
| 492 |
dot = NotifyDot( |
| 493 |
apikey="token", |
| 494 |
device_id="device", |
| 495 |
mode="image", |
| 496 |
image_data="base64img", |
| 497 |
) |
| 498 |
dot.border = None |
| 499 |
resp = mock.Mock() |
| 500 |
resp.status_code = 200 |
| 501 |
|
| 502 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 503 |
assert dot.send(body="test", title="test") |
| 504 |
|
| 505 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 506 |
assert "border" not in payload |
| 507 |
|
| 508 |
|
| 509 |
def test_notify_dot_image_mode_no_dither(): |
| 510 |
"""Image mode omits dither fields from payload when None.""" |
| 511 |
dot = NotifyDot( |
| 512 |
apikey="token", |
| 513 |
device_id="device", |
| 514 |
mode="image", |
| 515 |
image_data="base64img", |
| 516 |
) |
| 517 |
dot.dither_type = None |
| 518 |
dot.dither_kernel = None |
| 519 |
resp = mock.Mock() |
| 520 |
resp.status_code = 200 |
| 521 |
|
| 522 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 523 |
assert dot.send(body="test", title="test") |
| 524 |
|
| 525 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 526 |
assert "ditherType" not in payload |
| 527 |
assert "ditherKernel" not in payload |
| 528 |
|
| 529 |
|
| 530 |
def test_notify_dot_image_mode_title_body_ignored(): |
| 531 |
"""Image mode warns when title or body are provided.""" |
| 532 |
dot = NotifyDot( |
| 533 |
apikey="token", |
| 534 |
device_id="device", |
| 535 |
mode="image", |
| 536 |
image_data="base64img", |
| 537 |
) |
| 538 |
resp = mock.Mock() |
| 539 |
resp.status_code = 200 |
| 540 |
|
| 541 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 542 |
assert dot.send(title="ignored", body="also ignored") |
| 543 |
|
| 544 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 545 |
assert "title" not in payload |
| 546 |
assert "message" not in payload |
| 547 |
|
| 548 |
|
| 549 |
# --------------------------------------------------------------------------- |
| 550 |
# Explicit text mode |
| 551 |
# --------------------------------------------------------------------------- |
| 552 |
|
| 553 |
|
| 554 |
def test_notify_dot_text_mode_with_existing_icon(): |
| 555 |
"""Text mode: icon= param with Text API; attachment goes to Image API.""" |
| 556 |
dot = NotifyDot( |
| 557 |
apikey="token", |
| 558 |
device_id="device", |
| 559 |
mode="text", |
| 560 |
signature="footer", |
| 561 |
icon="aW5jb24=", |
| 562 |
) |
| 563 |
resp = mock.Mock() |
| 564 |
resp.status_code = 200 |
| 565 |
|
| 566 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 567 |
assert dot.send( |
| 568 |
title="hello", |
| 569 |
body="world", |
| 570 |
attach=[DummyAttachment("attachment_image")], |
| 571 |
) |
| 572 |
|
| 573 |
# Two POSTs: text API first, image API second |
| 574 |
assert mock_post.call_count == 2 |
| 575 |
text_payload = json.loads(mock_post.call_args_list[0][1]["data"]) |
| 576 |
image_payload = json.loads(mock_post.call_args_list[1][1]["data"]) |
| 577 |
assert text_payload["message"] == "world" |
| 578 |
assert text_payload["icon"] == "aW5jb24=" |
| 579 |
assert "image" not in text_payload |
| 580 |
assert image_payload["image"] == "attachment_image" |
| 581 |
|
| 582 |
|
| 583 |
def test_notify_dot_text_mode_uses_attachment_as_image(): |
| 584 |
"""Text mode sends attachment to Image API when no icon= is set.""" |
| 585 |
dot = NotifyDot( |
| 586 |
apikey="token", |
| 587 |
device_id="device", |
| 588 |
mode="text", |
| 589 |
signature="footer", |
| 590 |
) |
| 591 |
resp = mock.Mock() |
| 592 |
resp.status_code = 200 |
| 593 |
|
| 594 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 595 |
assert dot.send( |
| 596 |
title="hello", |
| 597 |
body="world", |
| 598 |
attach=[DummyAttachment("attachment_image_data")], |
| 599 |
) |
| 600 |
|
| 601 |
assert mock_post.call_count == 2 |
| 602 |
text_payload = json.loads(mock_post.call_args_list[0][1]["data"]) |
| 603 |
image_payload = json.loads(mock_post.call_args_list[1][1]["data"]) |
| 604 |
assert text_payload["message"] == "world" |
| 605 |
assert "icon" not in text_payload |
| 606 |
assert image_payload["image"] == "attachment_image_data" |
| 607 |
|
| 608 |
|
| 609 |
def test_notify_dot_text_mode_multiple_attachments_warning(): |
| 610 |
"""Text mode warns when multiple attachments are provided.""" |
| 611 |
dot = NotifyDot(apikey="token", device_id="device", mode="text") |
| 612 |
resp = mock.Mock() |
| 613 |
resp.status_code = 200 |
| 614 |
|
| 615 |
with ( |
| 616 |
mock.patch("requests.post", return_value=resp) as mock_post, |
| 617 |
mock.patch.object(dot.logger, "warning") as mock_warning, |
| 618 |
): |
| 619 |
assert dot.send( |
| 620 |
title="hello", |
| 621 |
body="world", |
| 622 |
attach=[ |
| 623 |
DummyAttachment("first"), |
| 624 |
DummyAttachment("second"), |
| 625 |
], |
| 626 |
) |
| 627 |
mock_warning.assert_called_once() |
| 628 |
assert "Multiple attachments" in str(mock_warning.call_args) |
| 629 |
|
| 630 |
# First attachment sent as image to Image API (second POST) |
| 631 |
assert mock_post.call_count == 2 |
| 632 |
image_payload = json.loads(mock_post.call_args_list[1][1]["data"]) |
| 633 |
assert image_payload["image"] == "first" |
| 634 |
|
| 635 |
|
| 636 |
def test_notify_dot_text_mode_no_optional_fields(): |
| 637 |
"""Text mode omits optional fields when not set.""" |
| 638 |
dot = NotifyDot(apikey="token", device_id="device", mode="text") |
| 639 |
resp = mock.Mock() |
| 640 |
resp.status_code = 200 |
| 641 |
|
| 642 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 643 |
assert dot.send(body="test body", title="test title") |
| 644 |
|
| 645 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 646 |
assert "signature" not in payload |
| 647 |
assert "icon" not in payload |
| 648 |
assert payload.get("link") is None or "link" not in payload |
| 649 |
|
| 650 |
|
| 651 |
def test_notify_dot_text_mode_with_title_and_body(): |
| 652 |
"""Text mode sends title and body correctly.""" |
| 653 |
dot = NotifyDot(apikey="token", device_id="device", mode="text") |
| 654 |
resp = mock.Mock() |
| 655 |
resp.status_code = 200 |
| 656 |
|
| 657 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 658 |
assert dot.send(body="test_body", title="test_title") |
| 659 |
|
| 660 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 661 |
assert payload["message"] == "test_body" |
| 662 |
assert payload["title"] == "test_title" |
| 663 |
|
| 664 |
|
| 665 |
def test_notify_dot_text_mode_without_title(): |
| 666 |
"""Text mode omits title from payload when empty.""" |
| 667 |
dot = NotifyDot(apikey="token", device_id="device", mode="text") |
| 668 |
resp = mock.Mock() |
| 669 |
resp.status_code = 200 |
| 670 |
|
| 671 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 672 |
assert dot.send(body="test message", title="") |
| 673 |
|
| 674 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 675 |
assert "title" not in payload |
| 676 |
assert payload["message"] == "test message" |
| 677 |
|
| 678 |
|
| 679 |
def test_notify_dot_text_mode_without_body(): |
| 680 |
"""Text mode omits message from payload when body is empty.""" |
| 681 |
dot = NotifyDot(apikey="token", device_id="device", mode="text") |
| 682 |
resp = mock.Mock() |
| 683 |
resp.status_code = 200 |
| 684 |
|
| 685 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 686 |
assert dot.send(body="", title="test_title") |
| 687 |
|
| 688 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 689 |
assert payload["title"] == "test_title" |
| 690 |
assert "message" not in payload |
| 691 |
|
| 692 |
|
| 693 |
def test_notify_dot_text_mode_attachment_exception(): |
| 694 |
"""Text mode warns on attachment error; text still sends without image.""" |
| 695 |
|
| 696 |
class ExceptionAttachment: |
| 697 |
def base64(self): |
| 698 |
raise Exception("Attachment base64 conversion fails") |
| 699 |
|
| 700 |
dot = NotifyDot(apikey="token", device_id="device", mode="text") |
| 701 |
resp = mock.Mock() |
| 702 |
resp.status_code = 200 |
| 703 |
|
| 704 |
with ( |
| 705 |
mock.patch("requests.post", return_value=resp) as mock_post, |
| 706 |
mock.patch.object(dot.logger, "warning") as mock_warning, |
| 707 |
): |
| 708 |
assert dot.send( |
| 709 |
title="hello", |
| 710 |
body="world", |
| 711 |
attach=[ExceptionAttachment()], |
| 712 |
) |
| 713 |
assert mock_warning.called |
| 714 |
assert any( |
| 715 |
"Failed to process attachment" in str(c) |
| 716 |
for c in mock_warning.call_args_list |
| 717 |
) |
| 718 |
|
| 719 |
# Only text API called (image_data resolved to None on exception) |
| 720 |
assert mock_post.call_count == 1 |
| 721 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 722 |
assert payload["message"] == "world" |
| 723 |
assert "icon" not in payload |
| 724 |
|
| 725 |
|
| 726 |
def test_notify_dot_text_mode_attachment_none(): |
| 727 |
"""Text mode sends only text when attachment is None.""" |
| 728 |
dot = NotifyDot(apikey="token", device_id="device", mode="text") |
| 729 |
resp = mock.Mock() |
| 730 |
resp.status_code = 200 |
| 731 |
|
| 732 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 733 |
assert dot.send(title="hello", body="world", attach=[None]) |
| 734 |
|
| 735 |
assert mock_post.call_count == 1 |
| 736 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 737 |
assert payload["message"] == "world" |
| 738 |
assert "icon" not in payload |
| 739 |
|
| 740 |
|
| 741 |
def test_notify_dot_text_mode_attachment_falsy(): |
| 742 |
"""Text mode sends only text when attachment is falsy.""" |
| 743 |
|
| 744 |
class FalsyAttachment: |
| 745 |
def __bool__(self): |
| 746 |
return False |
| 747 |
|
| 748 |
def base64(self): |
| 749 |
return "should_not_be_called" |
| 750 |
|
| 751 |
dot = NotifyDot(apikey="token", device_id="device", mode="text") |
| 752 |
resp = mock.Mock() |
| 753 |
resp.status_code = 200 |
| 754 |
|
| 755 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 756 |
assert dot.send( |
| 757 |
title="hello", |
| 758 |
body="world", |
| 759 |
attach=[FalsyAttachment()], |
| 760 |
) |
| 761 |
|
| 762 |
assert mock_post.call_count == 1 |
| 763 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 764 |
assert payload["message"] == "world" |
| 765 |
assert "icon" not in payload |
| 766 |
|
| 767 |
|
| 768 |
def test_notify_dot_title_handling(): |
| 769 |
"""Text mode omits title from payload when empty.""" |
| 770 |
dot = NotifyDot(apikey="token", device_id="device", mode="text") |
| 771 |
resp = mock.Mock() |
| 772 |
resp.status_code = 200 |
| 773 |
|
| 774 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 775 |
assert dot.send(body="test", title="provided_title") |
| 776 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 777 |
assert payload["title"] == "provided_title" |
| 778 |
|
| 779 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 780 |
assert dot.send(body="test", title="") |
| 781 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 782 |
assert "title" not in payload |
| 783 |
|
| 784 |
|
| 785 |
# --------------------------------------------------------------------------- |
| 786 |
# URL generation |
| 787 |
# --------------------------------------------------------------------------- |
| 788 |
|
| 789 |
|
| 790 |
def test_notify_dot_url_generation(): |
| 791 |
"""URL generation for text (default) and image modes.""" |
| 792 |
# text mode (default) — no mode param emitted |
| 793 |
text_dot = NotifyDot( |
| 794 |
apikey="token", |
| 795 |
device_id="device", |
| 796 |
signature="sig", |
| 797 |
icon="aW5jb24=", |
| 798 |
) |
| 799 |
text_url = text_dot.url() |
| 800 |
parsed = urlparse(text_url) |
| 801 |
assert parsed.path == "/" |
| 802 |
query = parse_qs(parsed.query) |
| 803 |
assert query["refresh"] == ["yes"] |
| 804 |
assert query["signature"] == ["sig"] |
| 805 |
assert "mode" not in query |
| 806 |
|
| 807 |
# Explicit image mode — mode=image in params |
| 808 |
image_dot = NotifyDot( |
| 809 |
apikey="token", |
| 810 |
device_id="device", |
| 811 |
mode="image", |
| 812 |
image_data="aW1hZ2U=", |
| 813 |
link="https://example.com", |
| 814 |
border=1, |
| 815 |
dither_type="ORDERED", |
| 816 |
dither_kernel="ATKINSON", |
| 817 |
) |
| 818 |
image_url = image_dot.url() |
| 819 |
image_query = parse_qs(urlparse(image_url).query) |
| 820 |
assert image_query["mode"] == ["image"] |
| 821 |
assert image_query["image"] == ["aW1hZ2U="] |
| 822 |
assert image_query["border"] == ["1"] |
| 823 |
|
| 824 |
|
| 825 |
def test_notify_dot_url_generation_defaults(): |
| 826 |
"""Default text mode omits mode param; image mode includes it.""" |
| 827 |
dot = NotifyDot(apikey="token", device_id="device") |
| 828 |
url = dot.url() |
| 829 |
assert "refresh=yes" in url |
| 830 |
assert "mode=" not in url |
| 831 |
|
| 832 |
dot_image = NotifyDot( |
| 833 |
apikey="token", |
| 834 |
device_id="device", |
| 835 |
mode="image", |
| 836 |
image_data="img", |
| 837 |
dither_type="ORDERED", |
| 838 |
dither_kernel="ATKINSON", |
| 839 |
) |
| 840 |
url_image = dot_image.url() |
| 841 |
assert "mode=image" in url_image |
| 842 |
assert "dither_type=ORDERED" in url_image |
| 843 |
assert "dither_kernel=ATKINSON" in url_image |
| 844 |
|
| 845 |
|
| 846 |
def test_notify_dot_url_generation_defaults_no_dither(): |
| 847 |
"""Default dither values are omitted from URL.""" |
| 848 |
dot = NotifyDot( |
| 849 |
apikey="token", |
| 850 |
device_id="device", |
| 851 |
mode="image", |
| 852 |
image_data="img", |
| 853 |
dither_type="DIFFUSION", |
| 854 |
dither_kernel="FLOYD_STEINBERG", |
| 855 |
) |
| 856 |
url = dot.url() |
| 857 |
assert "dither_type" not in url |
| 858 |
assert "dither_kernel" not in url |
| 859 |
|
| 860 |
|
| 861 |
def test_notify_dot_url_with_border(): |
| 862 |
"""URL includes border when set.""" |
| 863 |
dot = NotifyDot( |
| 864 |
apikey="token", |
| 865 |
device_id="device", |
| 866 |
mode="image", |
| 867 |
image_data="img", |
| 868 |
border=1, |
| 869 |
) |
| 870 |
assert "border=1" in dot.url() |
| 871 |
|
| 872 |
|
| 873 |
def test_notify_dot_url_with_link(): |
| 874 |
"""URL includes link when set.""" |
| 875 |
dot = NotifyDot( |
| 876 |
apikey="token", |
| 877 |
device_id="device", |
| 878 |
link="https://example.com", |
| 879 |
) |
| 880 |
assert "link=" in dot.url() |
| 881 |
|
| 882 |
|
| 883 |
def test_notify_dot_url_with_task_key(): |
| 884 |
"""URL includes task_key when set.""" |
| 885 |
dot = NotifyDot( |
| 886 |
apikey="token", |
| 887 |
device_id="device", |
| 888 |
task_key="url_task", |
| 889 |
) |
| 890 |
assert "task_key=url_task" in dot.url() |
| 891 |
|
| 892 |
|
| 893 |
# --------------------------------------------------------------------------- |
| 894 |
# parse_url |
| 895 |
# --------------------------------------------------------------------------- |
| 896 |
|
| 897 |
|
| 898 |
def test_notify_dot_parse_url_mode_and_image(): |
| 899 |
"""parse_url handles image mode via path and query param.""" |
| 900 |
# Backward-compat: /image/ path selects image mode |
| 901 |
result = NotifyDot.parse_url( |
| 902 |
"dot://token@device/image/?image=Zm9vYmFy&link=https://example.com" |
| 903 |
) |
| 904 |
assert result["mode"] == "image" |
| 905 |
assert result["image_data"] == "Zm9vYmFy" |
| 906 |
assert result["link"] == "https://example.com" |
| 907 |
|
| 908 |
# New-style: ?mode= query param |
| 909 |
result2 = NotifyDot.parse_url( |
| 910 |
"dot://token@device/?mode=image&image=Zm9vYmFy" |
| 911 |
) |
| 912 |
assert result2["mode"] == "image" |
| 913 |
assert result2["image_data"] == "Zm9vYmFy" |
| 914 |
|
| 915 |
# image= without mode= stays as default text mode (dual-send) |
| 916 |
result3 = NotifyDot.parse_url("dot://token@device/?image=Zm9vYmFy") |
| 917 |
assert result3["mode"] == "text" |
| 918 |
assert result3["image_data"] == "Zm9vYmFy" |
| 919 |
|
| 920 |
# Invalid ?mode= falls back to text (default) |
| 921 |
result4 = NotifyDot.parse_url("dot://token@device/?mode=invalid") |
| 922 |
assert result4["mode"] == "text" |
| 923 |
|
| 924 |
# Unknown path token falls back to text (default) |
| 925 |
fallback = NotifyDot.parse_url("dot://token@device/unknown/?refresh=no") |
| 926 |
assert fallback["mode"] == "text" |
| 927 |
assert fallback["refresh_now"] is False |
| 928 |
|
| 929 |
|
| 930 |
def test_notify_dot_parse_url_with_all_params(): |
| 931 |
"""parse_url handles all parameters.""" |
| 932 |
result = NotifyDot.parse_url( |
| 933 |
"dot://apikey@device/image/?refresh=yes&signature=sig&icon=icon_b64" |
| 934 |
"&link=https://example.com&border=1&dither_type=ORDERED" |
| 935 |
"&dither_kernel=ATKINSON&image=img_b64" |
| 936 |
) |
| 937 |
assert result["mode"] == "image" |
| 938 |
assert result["refresh_now"] is True |
| 939 |
assert result["signature"] == "sig" |
| 940 |
assert result["icon"] == "icon_b64" |
| 941 |
assert result["link"] == "https://example.com" |
| 942 |
assert result["border"] == 1 |
| 943 |
assert result["dither_type"] == "ORDERED" |
| 944 |
assert result["dither_kernel"] == "ATKINSON" |
| 945 |
assert result["image_data"] == "img_b64" |
| 946 |
|
| 947 |
|
| 948 |
def test_notify_dot_parse_url_no_host(): |
| 949 |
"""parse_url succeeds when host provides device_id.""" |
| 950 |
result = NotifyDot.parse_url("dot://apikey@device/text/") |
| 951 |
assert result is not None |
| 952 |
assert result.get("device_id") == "device" |
| 953 |
|
| 954 |
|
| 955 |
def test_notify_dot_parse_url_without_host(): |
| 956 |
"""parse_url returns None when host is empty.""" |
| 957 |
assert NotifyDot.parse_url("dot://apikey@/text/") is None |
| 958 |
|
| 959 |
|
| 960 |
def test_notify_dot_parse_url_with_empty_refresh(): |
| 961 |
"""parse_url omits refresh_now when not specified.""" |
| 962 |
result = NotifyDot.parse_url("dot://apikey@device/text/") |
| 963 |
assert result is not None |
| 964 |
assert result.get("refresh_now") is None |
| 965 |
|
| 966 |
|
| 967 |
def test_notify_dot_parse_url_task_key(): |
| 968 |
"""parse_url extracts task_key.""" |
| 969 |
result = NotifyDot.parse_url("dot://apikey@device_id/?task_key=parsed") |
| 970 |
assert result is not None |
| 971 |
assert result["task_key"] == "parsed" |
| 972 |
|
| 973 |
|
| 974 |
def test_notify_dot_parse_url_without_host_field(): |
| 975 |
"""parse_url with host=None sets no device_id.""" |
| 976 |
from apprise import NotifyBase |
| 977 |
|
| 978 |
with mock.patch.object(NotifyBase, "parse_url") as mock_parse: |
| 979 |
mock_parse.return_value = { |
| 980 |
"user": "apikey", |
| 981 |
"password": None, |
| 982 |
"port": None, |
| 983 |
"host": None, |
| 984 |
"fullpath": "/text/", |
| 985 |
"path": "", |
| 986 |
"query": None, |
| 987 |
"schema": "dot", |
| 988 |
"qsd": {"refresh": "yes"}, |
| 989 |
"secure": False, |
| 990 |
"verify": True, |
| 991 |
} |
| 992 |
result = NotifyDot.parse_url("dot://fake") |
| 993 |
assert result is not None |
| 994 |
assert result.get("mode") == "text" |
| 995 |
assert result.get("device_id") is None |
| 996 |
assert result.get("apikey") == "apikey" |
| 997 |
assert result.get("refresh_now") is True |
| 998 |
|
| 999 |
|
| 1000 |
# --------------------------------------------------------------------------- |
| 1001 |
# url_identifier |
| 1002 |
# --------------------------------------------------------------------------- |
| 1003 |
|
| 1004 |
|
| 1005 |
def test_notify_dot_url_identifier(): |
| 1006 |
"""url_identifier includes mode.""" |
| 1007 |
dot = NotifyDot(apikey="token", device_id="device", mode="image") |
| 1008 |
assert dot.url_identifier == ("dot", "token", "device", "image") |
| 1009 |
|
| 1010 |
dot_text = NotifyDot(apikey="token", device_id="device") |
| 1011 |
assert dot_text.url_identifier == ("dot", "token", "device", "text") |
| 1012 |
|
| 1013 |
|
| 1014 |
# --------------------------------------------------------------------------- |
| 1015 |
# URL round-trip |
| 1016 |
# --------------------------------------------------------------------------- |
| 1017 |
|
| 1018 |
|
| 1019 |
def test_notify_dot_url_roundtrip_with_task_key(): |
| 1020 |
"""parse_url -> NotifyDot -> url() preserves task_key and signature.""" |
| 1021 |
original = "dot://apikey@device123/text/?task_key=test&signature=sig" |
| 1022 |
result = NotifyDot.parse_url(original) |
| 1023 |
dot = NotifyDot(**result) |
| 1024 |
reconstructed = dot.url(privacy=False) |
| 1025 |
|
| 1026 |
assert "device123" in reconstructed |
| 1027 |
# text is the default mode; mode= is not emitted |
| 1028 |
assert "mode=" not in reconstructed |
| 1029 |
assert "task_key=test" in reconstructed |
| 1030 |
assert "signature=sig" in reconstructed |
| 1031 |
|
| 1032 |
|
| 1033 |
# --------------------------------------------------------------------------- |
| 1034 |
# Missing credentials |
| 1035 |
# --------------------------------------------------------------------------- |
| 1036 |
|
| 1037 |
|
| 1038 |
def test_notify_dot_no_device_id(): |
| 1039 |
"""send() returns False when device_id is missing.""" |
| 1040 |
dot = NotifyDot(apikey="token", device_id=None) |
| 1041 |
assert bool(dot.notify(title="test", body="test")) is False |
| 1042 |
assert len(dot) == 0 |
| 1043 |
|
| 1044 |
|
| 1045 |
# --------------------------------------------------------------------------- |
| 1046 |
# API v2 endpoint construction |
| 1047 |
# --------------------------------------------------------------------------- |
| 1048 |
|
| 1049 |
|
| 1050 |
def test_notify_dot_api_v2_endpoints(): |
| 1051 |
"""API v2 endpoints embed device_id in URL path.""" |
| 1052 |
dot = NotifyDot(apikey="test_key", device_id="DEVICE123", mode="text") |
| 1053 |
assert dot.text_api_url == ( |
| 1054 |
"https://dot.mindreset.tech/api/authV2/open/device/DEVICE123/text" |
| 1055 |
) |
| 1056 |
|
| 1057 |
dot_img = NotifyDot(apikey="test_key", device_id="DEVICE456", mode="image") |
| 1058 |
assert dot_img.image_api_url == ( |
| 1059 |
"https://dot.mindreset.tech/api/authV2/open/device/DEVICE456/image" |
| 1060 |
) |
| 1061 |
|
| 1062 |
|
| 1063 |
def test_notify_dot_deviceid_not_in_payload(): |
| 1064 |
"""deviceId is NOT in text or image payload (API v2).""" |
| 1065 |
dot = NotifyDot(apikey="test_key", device_id="12345", mode="text") |
| 1066 |
resp = mock.Mock() |
| 1067 |
resp.status_code = 200 |
| 1068 |
|
| 1069 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 1070 |
assert dot.send("test body", "test title") |
| 1071 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 1072 |
assert "deviceId" not in payload |
| 1073 |
assert "/device/12345/text" in mock_post.call_args[0][0] |
| 1074 |
|
| 1075 |
dot_img = NotifyDot( |
| 1076 |
apikey="test_key", |
| 1077 |
device_id="67890", |
| 1078 |
mode="image", |
| 1079 |
image_data="base64data", |
| 1080 |
) |
| 1081 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 1082 |
assert dot_img.send("", "") |
| 1083 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 1084 |
assert "deviceId" not in payload |
| 1085 |
assert "/device/67890/image" in mock_post.call_args[0][0] |
| 1086 |
|
| 1087 |
|
| 1088 |
# --------------------------------------------------------------------------- |
| 1089 |
# task_key |
| 1090 |
# --------------------------------------------------------------------------- |
| 1091 |
|
| 1092 |
|
| 1093 |
def test_notify_dot_task_key_in_text_payload(): |
| 1094 |
"""taskKey appears in text API payload.""" |
| 1095 |
dot = NotifyDot(apikey="k", device_id="d", mode="text", task_key="my_task") |
| 1096 |
resp = mock.Mock() |
| 1097 |
resp.status_code = 200 |
| 1098 |
|
| 1099 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 1100 |
assert dot.send("body", "title") |
| 1101 |
assert json.loads(mock_post.call_args[1]["data"])["taskKey"] == "my_task" |
| 1102 |
|
| 1103 |
|
| 1104 |
def test_notify_dot_task_key_in_image_payload(): |
| 1105 |
"""taskKey appears in image API payload.""" |
| 1106 |
dot = NotifyDot( |
| 1107 |
apikey="k", |
| 1108 |
device_id="d", |
| 1109 |
mode="image", |
| 1110 |
task_key="img_task", |
| 1111 |
image_data="base64", |
| 1112 |
) |
| 1113 |
resp = mock.Mock() |
| 1114 |
resp.status_code = 200 |
| 1115 |
|
| 1116 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 1117 |
assert dot.send("", "") |
| 1118 |
assert json.loads(mock_post.call_args[1]["data"])["taskKey"] == "img_task" |
| 1119 |
|
| 1120 |
|
| 1121 |
def test_notify_dot_task_key_none_not_in_payload(): |
| 1122 |
"""taskKey is absent from payload when None.""" |
| 1123 |
dot = NotifyDot(apikey="k", device_id="d", mode="text", task_key=None) |
| 1124 |
resp = mock.Mock() |
| 1125 |
resp.status_code = 200 |
| 1126 |
|
| 1127 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 1128 |
assert dot.send("test", "title") |
| 1129 |
assert "taskKey" not in json.loads(mock_post.call_args[1]["data"]) |
| 1130 |
|
| 1131 |
|
| 1132 |
# --------------------------------------------------------------------------- |
| 1133 |
# refresh_now |
| 1134 |
# --------------------------------------------------------------------------- |
| 1135 |
|
| 1136 |
|
| 1137 |
def test_notify_dot_refresh_now_defaults(): |
| 1138 |
"""refreshNow defaults to True and appears correctly in payload.""" |
| 1139 |
for refresh_now, expected in ((None, True), (True, True), (False, False)): |
| 1140 |
dot = NotifyDot( |
| 1141 |
apikey="k", |
| 1142 |
device_id="d", |
| 1143 |
mode="text", |
| 1144 |
refresh_now=refresh_now, |
| 1145 |
) |
| 1146 |
resp = mock.Mock() |
| 1147 |
resp.status_code = 200 |
| 1148 |
|
| 1149 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 1150 |
assert dot.send("test", "title") |
| 1151 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 1152 |
assert payload["refreshNow"] is expected |
| 1153 |
|
| 1154 |
|
| 1155 |
def test_notify_dot_refresh_now_in_url(): |
| 1156 |
"""refresh=yes appears in URL when refresh_now is None (defaults True).""" |
| 1157 |
dot = NotifyDot(apikey="key", device_id="id", refresh_now=None) |
| 1158 |
assert "refresh=yes" in dot.url() |
| 1159 |
|
| 1160 |
|
| 1161 |
# --------------------------------------------------------------------------- |
| 1162 |
# Authorization header |
| 1163 |
# --------------------------------------------------------------------------- |
| 1164 |
|
| 1165 |
|
| 1166 |
def test_notify_dot_authorization_header(): |
| 1167 |
"""Authorization header uses Bearer format.""" |
| 1168 |
dot = NotifyDot(apikey="MY_SECRET_KEY", device_id="12345", mode="text") |
| 1169 |
resp = mock.Mock() |
| 1170 |
resp.status_code = 200 |
| 1171 |
|
| 1172 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 1173 |
assert dot.send("test", "title") |
| 1174 |
headers = mock_post.call_args[1]["headers"] |
| 1175 |
assert headers["Authorization"] == "Bearer MY_SECRET_KEY" |
| 1176 |
|
| 1177 |
|
| 1178 |
# --------------------------------------------------------------------------- |
| 1179 |
# Special-character device_id |
| 1180 |
# --------------------------------------------------------------------------- |
| 1181 |
|
| 1182 |
|
| 1183 |
def test_notify_dot_device_id_encoding(): |
| 1184 |
"""Device IDs with special characters are URL-encoded.""" |
| 1185 |
cases = [ |
| 1186 |
("device/id", "device%2Fid"), |
| 1187 |
("device?id", "device%3Fid"), |
| 1188 |
("device#id", "device%23id"), |
| 1189 |
("device%id", "device%25id"), |
| 1190 |
("device id", "device%20id"), |
| 1191 |
("device@id", "device%40id"), |
| 1192 |
] |
| 1193 |
for device_id, expected in cases: |
| 1194 |
dot = NotifyDot(apikey="test_key", device_id=device_id) |
| 1195 |
assert expected in dot.url(), ( |
| 1196 |
f"device_id '{device_id}' should encode as '{expected}'" |
| 1197 |
) |
| 1198 |
|
| 1199 |
|
| 1200 |
# --------------------------------------------------------------------------- |
| 1201 |
# Combined parameter tests |
| 1202 |
# --------------------------------------------------------------------------- |
| 1203 |
|
| 1204 |
|
| 1205 |
def test_notify_dot_combined_text_params(): |
| 1206 |
"""Text mode sends all configured parameters correctly.""" |
| 1207 |
dot = NotifyDot( |
| 1208 |
apikey="key", |
| 1209 |
device_id="device", |
| 1210 |
mode="text", |
| 1211 |
task_key="combined_task", |
| 1212 |
refresh_now=False, |
| 1213 |
signature="test_sig", |
| 1214 |
link="https://example.com", |
| 1215 |
) |
| 1216 |
resp = mock.Mock() |
| 1217 |
resp.status_code = 200 |
| 1218 |
|
| 1219 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 1220 |
assert dot.send("body", "title") |
| 1221 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 1222 |
assert "deviceId" not in payload |
| 1223 |
assert payload["taskKey"] == "combined_task" |
| 1224 |
assert payload["refreshNow"] is False |
| 1225 |
assert payload["signature"] == "test_sig" |
| 1226 |
assert payload["link"] == "https://example.com" |
| 1227 |
assert payload["title"] == "title" |
| 1228 |
assert payload["message"] == "body" |
| 1229 |
|
| 1230 |
|
| 1231 |
def test_notify_dot_combined_image_params(): |
| 1232 |
"""Image mode sends all configured parameters correctly.""" |
| 1233 |
dot = NotifyDot( |
| 1234 |
apikey="key", |
| 1235 |
device_id="img_device", |
| 1236 |
mode="image", |
| 1237 |
image_data="base64image", |
| 1238 |
task_key="img_task", |
| 1239 |
refresh_now=True, |
| 1240 |
link="https://example.com", |
| 1241 |
border=1, |
| 1242 |
dither_type="ORDERED", |
| 1243 |
dither_kernel="ATKINSON", |
| 1244 |
) |
| 1245 |
resp = mock.Mock() |
| 1246 |
resp.status_code = 200 |
| 1247 |
|
| 1248 |
with mock.patch("requests.post", return_value=resp) as mock_post: |
| 1249 |
assert dot.send("", "") |
| 1250 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 1251 |
assert "deviceId" not in payload |
| 1252 |
assert payload["image"] == "base64image" |
| 1253 |
assert payload["taskKey"] == "img_task" |
| 1254 |
assert payload["refreshNow"] is True |
| 1255 |
assert payload["link"] == "https://example.com" |
| 1256 |
assert payload["border"] == 1 |
| 1257 |
assert payload["ditherType"] == "ORDERED" |
| 1258 |
assert payload["ditherKernel"] == "ATKINSON" |
| 1259 |
assert "/device/img_device/image" in mock_post.call_args[0][0] |
| 1260 |
|
| 1261 |
|
| 1262 |
def test_notify_dot_debug_logging_branch(): |
| 1263 |
"""Exercise the isEnabledFor(DEBUG) branch inside _post.""" |
| 1264 |
dot = NotifyDot(apikey="key", device_id="dev123", mode="text") |
| 1265 |
resp = mock.Mock() |
| 1266 |
resp.status_code = 200 |
| 1267 |
|
| 1268 |
with ( |
| 1269 |
mock.patch.object(dot.logger, "isEnabledFor", return_value=True), |
| 1270 |
mock.patch("requests.post", return_value=resp), |
| 1271 |
): |
| 1272 |
assert dot.send("body", "title") |