| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
import base64 |
| 30 |
import json |
| 31 |
import logging |
| 32 |
import re |
| 33 |
from unittest import mock |
| 34 |
|
| 35 |
from helpers import AppriseURLTester |
| 36 |
import pytest |
| 37 |
import requests |
| 38 |
|
| 39 |
from apprise import Apprise, NotifyFormat |
| 40 |
import apprise.plugins.bark as bark_module |
| 41 |
from apprise.plugins.bark import NotifyBark |
| 42 |
|
| 43 |
logging.disable(logging.CRITICAL) |
| 44 |
|
| 45 |
|
| 46 |
apprise_url_tests = ( |
| 47 |
( |
| 48 |
"bark://", |
| 49 |
{ |
| 50 |
|
| 51 |
"instance": None, |
| 52 |
}, |
| 53 |
), |
| 54 |
( |
| 55 |
"bark://:@/", |
| 56 |
{ |
| 57 |
|
| 58 |
"instance": None, |
| 59 |
}, |
| 60 |
), |
| 61 |
( |
| 62 |
"bark://localhost", |
| 63 |
{ |
| 64 |
|
| 65 |
"instance": NotifyBark, |
| 66 |
|
| 67 |
|
| 68 |
"notify_response": False, |
| 69 |
}, |
| 70 |
), |
| 71 |
( |
| 72 |
"bark://192.168.0.6:8081/device_key", |
| 73 |
{ |
| 74 |
|
| 75 |
"instance": NotifyBark, |
| 76 |
|
| 77 |
"privacy_url": "bark://192.168.0.6:8081/", |
| 78 |
}, |
| 79 |
), |
| 80 |
( |
| 81 |
"bark://[email protected]:8081/device_key", |
| 82 |
{ |
| 83 |
|
| 84 |
"instance": NotifyBark, |
| 85 |
|
| 86 |
"privacy_url": "bark://[email protected]:8081/", |
| 87 |
}, |
| 88 |
), |
| 89 |
( |
| 90 |
"bark://192.168.0.6:8081/device_key/?sound=invalid", |
| 91 |
{ |
| 92 |
|
| 93 |
"instance": NotifyBark, |
| 94 |
}, |
| 95 |
), |
| 96 |
( |
| 97 |
"bark://192.168.0.6:8081/device_key/?sound=alarm", |
| 98 |
{ |
| 99 |
|
| 100 |
"instance": NotifyBark, |
| 101 |
}, |
| 102 |
), |
| 103 |
( |
| 104 |
"bark://192.168.0.6:8081/device_key/?sound=NOiR.cAf", |
| 105 |
{ |
| 106 |
|
| 107 |
"instance": NotifyBark, |
| 108 |
}, |
| 109 |
), |
| 110 |
( |
| 111 |
"bark://192.168.0.6:8081/device_key/?badge=100", |
| 112 |
{ |
| 113 |
|
| 114 |
"instance": NotifyBark, |
| 115 |
}, |
| 116 |
), |
| 117 |
( |
| 118 |
"barks://192.168.0.6:8081/device_key/?badge=invalid", |
| 119 |
{ |
| 120 |
|
| 121 |
"instance": NotifyBark, |
| 122 |
}, |
| 123 |
), |
| 124 |
( |
| 125 |
"barks://192.168.0.6:8081/device_key/?badge=-12", |
| 126 |
{ |
| 127 |
|
| 128 |
"instance": NotifyBark, |
| 129 |
}, |
| 130 |
), |
| 131 |
( |
| 132 |
"bark://192.168.0.6:8081/device_key/?category=apprise", |
| 133 |
{ |
| 134 |
|
| 135 |
"instance": NotifyBark, |
| 136 |
}, |
| 137 |
), |
| 138 |
( |
| 139 |
"bark://192.168.0.6:8081/device_key/?image=no", |
| 140 |
{ |
| 141 |
|
| 142 |
"instance": NotifyBark, |
| 143 |
}, |
| 144 |
), |
| 145 |
( |
| 146 |
"bark://192.168.0.6:8081/device_key/?group=apprise", |
| 147 |
{ |
| 148 |
|
| 149 |
"instance": NotifyBark, |
| 150 |
}, |
| 151 |
), |
| 152 |
( |
| 153 |
"bark://192.168.0.6:8081/device_key/?level=invalid", |
| 154 |
{ |
| 155 |
|
| 156 |
"instance": NotifyBark, |
| 157 |
}, |
| 158 |
), |
| 159 |
( |
| 160 |
"bark://192.168.0.6:8081/?to=device_key", |
| 161 |
{ |
| 162 |
|
| 163 |
"instance": NotifyBark, |
| 164 |
}, |
| 165 |
), |
| 166 |
( |
| 167 |
"bark://192.168.0.6:8081/device_key/?click=http://localhost", |
| 168 |
{ |
| 169 |
|
| 170 |
"instance": NotifyBark, |
| 171 |
}, |
| 172 |
), |
| 173 |
( |
| 174 |
"bark://192.168.0.6:8081/device_key/?level=active", |
| 175 |
{ |
| 176 |
|
| 177 |
"instance": NotifyBark, |
| 178 |
}, |
| 179 |
), |
| 180 |
( |
| 181 |
"bark://192.168.0.6:8081/device_key/?level=critical", |
| 182 |
{ |
| 183 |
|
| 184 |
"instance": NotifyBark, |
| 185 |
}, |
| 186 |
), |
| 187 |
( |
| 188 |
"bark://192.168.0.6:8081/device_key/?level=critical&volume=10", |
| 189 |
{ |
| 190 |
|
| 191 |
"instance": NotifyBark, |
| 192 |
}, |
| 193 |
), |
| 194 |
( |
| 195 |
"bark://192.168.0.6:8081/device_key/?level=critical&volume=invalid", |
| 196 |
{ |
| 197 |
|
| 198 |
"instance": NotifyBark, |
| 199 |
}, |
| 200 |
), |
| 201 |
( |
| 202 |
"bark://192.168.0.6:8081/device_key/?level=critical&volume=11", |
| 203 |
{ |
| 204 |
|
| 205 |
"instance": NotifyBark, |
| 206 |
}, |
| 207 |
), |
| 208 |
( |
| 209 |
"bark://192.168.0.6:8081/device_key/?level=critical&volume=-1", |
| 210 |
{ |
| 211 |
|
| 212 |
"instance": NotifyBark, |
| 213 |
}, |
| 214 |
), |
| 215 |
( |
| 216 |
"bark://192.168.0.6:8081/device_key/?level=critical&volume=", |
| 217 |
{ |
| 218 |
|
| 219 |
"instance": NotifyBark, |
| 220 |
}, |
| 221 |
), |
| 222 |
( |
| 223 |
"bark://user:[email protected]:8086/device_key/device_key2/", |
| 224 |
{ |
| 225 |
|
| 226 |
"instance": NotifyBark, |
| 227 |
|
| 228 |
"privacy_url": "bark://user:****@192.168.0.5:8086/", |
| 229 |
}, |
| 230 |
), |
| 231 |
( |
| 232 |
"barks://192.168.0.7/device_key/", |
| 233 |
{ |
| 234 |
"instance": NotifyBark, |
| 235 |
|
| 236 |
"response": False, |
| 237 |
"requests_response_code": 999, |
| 238 |
|
| 239 |
"privacy_url": "barks://192.168.0.7/****", |
| 240 |
}, |
| 241 |
), |
| 242 |
( |
| 243 |
"bark://192.168.0.7/device_key", |
| 244 |
{ |
| 245 |
"instance": NotifyBark, |
| 246 |
|
| 247 |
|
| 248 |
"test_requests_exceptions": True, |
| 249 |
}, |
| 250 |
), |
| 251 |
( |
| 252 |
"bark://192.168.0.6:8081/device_key/?icon=https://example.com/icon.png", |
| 253 |
{ |
| 254 |
|
| 255 |
"instance": NotifyBark, |
| 256 |
}, |
| 257 |
), |
| 258 |
( |
| 259 |
"bark://192.168.0.6:8081/device_key/?icon=https://example.com/icon.png&image=no", |
| 260 |
{ |
| 261 |
|
| 262 |
"instance": NotifyBark, |
| 263 |
}, |
| 264 |
), |
| 265 |
( |
| 266 |
"bark://192.168.0.6:8081/device_key/?call=1", |
| 267 |
{ |
| 268 |
|
| 269 |
"instance": NotifyBark, |
| 270 |
}, |
| 271 |
), |
| 272 |
( |
| 273 |
"bark://192.168.0.6:8081/device_key/?call=1&sound=alarm&level=critical", |
| 274 |
{ |
| 275 |
|
| 276 |
"instance": NotifyBark, |
| 277 |
}, |
| 278 |
), |
| 279 |
( |
| 280 |
"bark://192.168.0.6:8081/device_key/?format=markdown", |
| 281 |
{ |
| 282 |
|
| 283 |
"instance": NotifyBark, |
| 284 |
}, |
| 285 |
), |
| 286 |
( |
| 287 |
"bark://192.168.0.6:8081/device_key/?format=text", |
| 288 |
{ |
| 289 |
|
| 290 |
"instance": NotifyBark, |
| 291 |
}, |
| 292 |
), |
| 293 |
) |
| 294 |
|
| 295 |
|
| 296 |
def test_plugin_bark_urls(): |
| 297 |
"""NotifyBark() Apprise URLs.""" |
| 298 |
|
| 299 |
|
| 300 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 301 |
|
| 302 |
|
| 303 |
@mock.patch("requests.post") |
| 304 |
def test_plugin_bark_html_to_markdown_format(mock_post): |
| 305 |
"""NotifyBark(): HTML body is converted to Markdown.""" |
| 306 |
|
| 307 |
|
| 308 |
mock_post.return_value = requests.Request() |
| 309 |
mock_post.return_value.status_code = requests.codes.ok |
| 310 |
|
| 311 |
|
| 312 |
aobj = Apprise() |
| 313 |
assert aobj.add("bark://192.168.0.6:8081/device_key/?format=markdown") |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
assert ( |
| 318 |
aobj.notify( |
| 319 |
body="<b>hello</b> <i>world</i>", |
| 320 |
body_format=NotifyFormat.HTML, |
| 321 |
) |
| 322 |
is True |
| 323 |
) |
| 324 |
assert mock_post.call_count == 1 |
| 325 |
|
| 326 |
|
| 327 |
payload = json.loads(mock_post.call_args_list[0][1]["data"]) |
| 328 |
assert payload["markdown"] == "**hello** *world*" |
| 329 |
|
| 330 |
|
| 331 |
@pytest.mark.parametrize( |
| 332 |
("key", "expected_ciphertext"), |
| 333 |
( |
| 334 |
( |
| 335 |
"k" * 16, |
| 336 |
"M31imDGJqdEEszZWcWfgiMTooJniyhmvnRIIrUSrWDVi3FxcD9cl1Dyre2wQ8a5I2bhFyBrGPeMEwp1+Uw==", |
| 337 |
), |
| 338 |
( |
| 339 |
"k" * 24, |
| 340 |
"QyuvztP2Yktq2khUWXw6hYrjLnhuc+kC44lyGOaVYBRJC4vsYaB6RewayvX+4ZKjUUIifRWDtIiVFq/KgA==", |
| 341 |
), |
| 342 |
( |
| 343 |
"k" * 32, |
| 344 |
"a9CkBbe9d0qkskyAzKyb4pV/WjH6D1J/JLm0EOUTMcoTpJEs10gqANkkLKaOPqVXL0yo0AaxElYjmLmynw==", |
| 345 |
), |
| 346 |
), |
| 347 |
) |
| 348 |
def test_plugin_bark_aesgcm_wire_format( |
| 349 |
key, |
| 350 |
expected_ciphertext, |
| 351 |
monkeypatch, |
| 352 |
): |
| 353 |
"""NotifyBark() matches Bark's AES-GCM combined wire format.""" |
| 354 |
pytest.importorskip("cryptography") |
| 355 |
monkeypatch.setattr( |
| 356 |
bark_module.secrets, |
| 357 |
"token_urlsafe", |
| 358 |
lambda length: "fixed-iv-123", |
| 359 |
) |
| 360 |
instance = NotifyBark( |
| 361 |
host="localhost", |
| 362 |
targets=("device-key",), |
| 363 |
secure=True, |
| 364 |
encryption_key=key, |
| 365 |
) |
| 366 |
|
| 367 |
ciphertext, iv = instance._encrypt_payload( |
| 368 |
{"body": "Encrypted weather", "level": "active"} |
| 369 |
) |
| 370 |
|
| 371 |
assert iv == "fixed-iv-123" |
| 372 |
assert ciphertext == expected_ciphertext |
| 373 |
|
| 374 |
|
| 375 |
@pytest.mark.parametrize( |
| 376 |
"key", |
| 377 |
( |
| 378 |
pytest.param("short", id="too-short"), |
| 379 |
pytest.param("k" * 17, id="unsupported-length"), |
| 380 |
pytest.param("🔒" * 16, id="non-ascii-unicode-probe"), |
| 381 |
pytest.param(b"k" * 16, id="non-string"), |
| 382 |
), |
| 383 |
) |
| 384 |
def test_plugin_bark_rejects_invalid_encryption_keys(key): |
| 385 |
"""NotifyBark() rejects keys Bark cannot use as raw AES keys.""" |
| 386 |
with pytest.raises(TypeError, match="Bark encryption key"): |
| 387 |
NotifyBark( |
| 388 |
host="localhost", |
| 389 |
targets=("device-key",), |
| 390 |
secure=True, |
| 391 |
encryption_key=key, |
| 392 |
) |
| 393 |
|
| 394 |
|
| 395 |
def test_plugin_bark_encryption_fails_closed_without_cryptography(monkeypatch): |
| 396 |
"""NotifyBark() never downgrades requested encryption to plaintext.""" |
| 397 |
monkeypatch.setattr(bark_module, "BARK_AESGCM_SUPPORT", False) |
| 398 |
|
| 399 |
with pytest.raises(TypeError, match="requires the 'cryptography' package"): |
| 400 |
NotifyBark( |
| 401 |
host="localhost", |
| 402 |
targets=("device-key",), |
| 403 |
secure=True, |
| 404 |
encryption_key="k" * 32, |
| 405 |
) |
| 406 |
|
| 407 |
instance = NotifyBark( |
| 408 |
host="localhost", |
| 409 |
targets=("device-key",), |
| 410 |
secure=True, |
| 411 |
) |
| 412 |
assert instance.encryption_key is None |
| 413 |
|
| 414 |
|
| 415 |
def test_plugin_bark_encryption_url_roundtrip_and_privacy(): |
| 416 |
"""NotifyBark() preserves encryption config without exposing secrets.""" |
| 417 |
pytest.importorskip("cryptography") |
| 418 |
device_key = "private-device-key" |
| 419 |
encryption_key = "k" * 32 |
| 420 |
instance = NotifyBark( |
| 421 |
host="localhost", |
| 422 |
targets=(device_key,), |
| 423 |
secure=True, |
| 424 |
encryption_key=encryption_key, |
| 425 |
) |
| 426 |
|
| 427 |
generated_url = instance.url(privacy=False) |
| 428 |
assert device_key in generated_url |
| 429 |
assert encryption_key in generated_url |
| 430 |
|
| 431 |
private_url = instance.url(privacy=True) |
| 432 |
assert device_key not in private_url |
| 433 |
assert encryption_key not in private_url |
| 434 |
assert private_url.startswith("barks://localhost/****?") |
| 435 |
assert "key=" in private_url |
| 436 |
|
| 437 |
parsed = NotifyBark.parse_url(generated_url) |
| 438 |
rebuilt = NotifyBark(**parsed) |
| 439 |
assert rebuilt.encryption_key == encryption_key |
| 440 |
assert rebuilt.targets == [device_key] |
| 441 |
assert rebuilt.url_identifier == instance.url_identifier |
| 442 |
|
| 443 |
different_key = NotifyBark( |
| 444 |
host="localhost", |
| 445 |
targets=(device_key,), |
| 446 |
secure=True, |
| 447 |
encryption_key="x" * 32, |
| 448 |
) |
| 449 |
assert different_key.url_identifier != instance.url_identifier |
| 450 |
|
| 451 |
|
| 452 |
@mock.patch("requests.post") |
| 453 |
def test_plugin_bark_encrypts_complete_payload_with_fresh_iv_per_target( |
| 454 |
mock_post, |
| 455 |
monkeypatch, |
| 456 |
): |
| 457 |
"""NotifyBark() encrypts all parameters and rotates IVs per request.""" |
| 458 |
aesgcm = pytest.importorskip( |
| 459 |
"cryptography.hazmat.primitives.ciphers.aead" |
| 460 |
).AESGCM |
| 461 |
generated_ivs = iter(("abcdefghijkl", "mnopqrstuvwx")) |
| 462 |
token_urlsafe = mock.Mock(side_effect=lambda length: next(generated_ivs)) |
| 463 |
monkeypatch.setattr( |
| 464 |
bark_module.secrets, |
| 465 |
"token_urlsafe", |
| 466 |
token_urlsafe, |
| 467 |
) |
| 468 |
response = mock.Mock() |
| 469 |
response.status_code = requests.codes.ok |
| 470 |
mock_post.return_value = response |
| 471 |
key = "k" * 32 |
| 472 |
private_body = "Private encrypted body" |
| 473 |
private_title = "Private encrypted title" |
| 474 |
targets = ("private-device-one", "private-device-two") |
| 475 |
instance = NotifyBark( |
| 476 |
host="localhost", |
| 477 |
targets=targets, |
| 478 |
secure=True, |
| 479 |
encryption_key=key, |
| 480 |
include_image=False, |
| 481 |
sound="alarm", |
| 482 |
category="private-category", |
| 483 |
group="private-group", |
| 484 |
level="active", |
| 485 |
click="https://private.example.invalid", |
| 486 |
badge=3, |
| 487 |
volume=4, |
| 488 |
icon="https://private.example.invalid/icon.png", |
| 489 |
call=True, |
| 490 |
) |
| 491 |
instance.logger = mock.Mock() |
| 492 |
|
| 493 |
assert instance.send(body=private_body, title=private_title) is True |
| 494 |
assert mock_post.call_count == 2 |
| 495 |
|
| 496 |
request_payloads = [ |
| 497 |
json.loads(call.kwargs["data"]) for call in mock_post.call_args_list |
| 498 |
] |
| 499 |
assert {payload["device_key"] for payload in request_payloads} == set( |
| 500 |
targets |
| 501 |
) |
| 502 |
assert [payload["iv"] for payload in request_payloads] == [ |
| 503 |
"abcdefghijkl", |
| 504 |
"mnopqrstuvwx", |
| 505 |
] |
| 506 |
assert token_urlsafe.call_args_list == [ |
| 507 |
mock.call(bark_module.BARK_GCM_IV_RANDOM_BYTES), |
| 508 |
mock.call(bark_module.BARK_GCM_IV_RANDOM_BYTES), |
| 509 |
] |
| 510 |
assert all( |
| 511 |
re.fullmatch(r"[A-Za-z0-9_-]{12}", payload["iv"]) |
| 512 |
for payload in request_payloads |
| 513 |
) |
| 514 |
assert all( |
| 515 |
set(payload) == {"device_key", "ciphertext", "iv"} |
| 516 |
for payload in request_payloads |
| 517 |
) |
| 518 |
|
| 519 |
expected_parameters = { |
| 520 |
"title": private_title, |
| 521 |
"body": private_body, |
| 522 |
"sound": "alarm.caf", |
| 523 |
"url": "https://private.example.invalid", |
| 524 |
"badge": 3, |
| 525 |
"level": "active", |
| 526 |
"category": "private-category", |
| 527 |
"group": "private-group", |
| 528 |
"volume": 4, |
| 529 |
"icon": "https://private.example.invalid/icon.png", |
| 530 |
"call": 1, |
| 531 |
} |
| 532 |
for request_payload in request_payloads: |
| 533 |
plaintext = aesgcm(key.encode("ascii")).decrypt( |
| 534 |
request_payload["iv"].encode("ascii"), |
| 535 |
base64.b64decode(request_payload["ciphertext"]), |
| 536 |
None, |
| 537 |
) |
| 538 |
assert json.loads(plaintext) == expected_parameters |
| 539 |
|
| 540 |
logged = repr(instance.logger.method_calls) |
| 541 |
for private_value in ( |
| 542 |
key, |
| 543 |
private_body, |
| 544 |
private_title, |
| 545 |
*targets, |
| 546 |
"private-category", |
| 547 |
"private-group", |
| 548 |
"private.example.invalid", |
| 549 |
): |
| 550 |
assert private_value not in logged |
| 551 |
|
| 552 |
|
| 553 |
@mock.patch("requests.post") |
| 554 |
def test_plugin_bark_encrypts_markdown_payload(mock_post, monkeypatch): |
| 555 |
"""NotifyBark() encrypts Markdown and does not add a plaintext body.""" |
| 556 |
aesgcm = pytest.importorskip( |
| 557 |
"cryptography.hazmat.primitives.ciphers.aead" |
| 558 |
).AESGCM |
| 559 |
monkeypatch.setattr( |
| 560 |
bark_module.secrets, |
| 561 |
"token_urlsafe", |
| 562 |
lambda length: "fixed-iv-123", |
| 563 |
) |
| 564 |
response = mock.Mock() |
| 565 |
response.status_code = requests.codes.ok |
| 566 |
mock_post.return_value = response |
| 567 |
key = "k" * 32 |
| 568 |
instance = NotifyBark( |
| 569 |
host="localhost", |
| 570 |
targets=("device-key",), |
| 571 |
secure=True, |
| 572 |
encryption_key=key, |
| 573 |
include_image=False, |
| 574 |
format=NotifyFormat.MARKDOWN, |
| 575 |
) |
| 576 |
|
| 577 |
assert instance.send(body="**encrypted**", title="Title") is True |
| 578 |
|
| 579 |
request_payload = json.loads(mock_post.call_args.kwargs["data"]) |
| 580 |
plaintext = aesgcm(key.encode("ascii")).decrypt( |
| 581 |
request_payload["iv"].encode("ascii"), |
| 582 |
base64.b64decode(request_payload["ciphertext"]), |
| 583 |
None, |
| 584 |
) |
| 585 |
assert json.loads(plaintext) == { |
| 586 |
"title": "Title", |
| 587 |
"markdown": "**encrypted**", |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
@mock.patch("requests.post") |
| 592 |
def test_plugin_bark_rotates_iv_across_repeated_sends( |
| 593 |
mock_post, |
| 594 |
monkeypatch, |
| 595 |
): |
| 596 |
"""NotifyBark() generates a fresh IV for every repeated request.""" |
| 597 |
pytest.importorskip("cryptography") |
| 598 |
generated_ivs = iter(("abcdefghijkl", "mnopqrstuvwx")) |
| 599 |
monkeypatch.setattr( |
| 600 |
bark_module.secrets, |
| 601 |
"token_urlsafe", |
| 602 |
lambda length: next(generated_ivs), |
| 603 |
) |
| 604 |
response = mock.Mock() |
| 605 |
response.status_code = requests.codes.ok |
| 606 |
mock_post.return_value = response |
| 607 |
instance = NotifyBark( |
| 608 |
host="localhost", |
| 609 |
targets=("device-key",), |
| 610 |
secure=True, |
| 611 |
encryption_key="k" * 32, |
| 612 |
include_image=False, |
| 613 |
) |
| 614 |
|
| 615 |
assert instance.send(body="same body", title="same title") is True |
| 616 |
assert instance.send(body="same body", title="same title") is True |
| 617 |
|
| 618 |
payloads = [ |
| 619 |
json.loads(call.kwargs["data"]) for call in mock_post.call_args_list |
| 620 |
] |
| 621 |
assert [payload["iv"] for payload in payloads] == [ |
| 622 |
"abcdefghijkl", |
| 623 |
"mnopqrstuvwx", |
| 624 |
] |
| 625 |
assert payloads[0]["ciphertext"] != payloads[1]["ciphertext"] |
| 626 |
|
| 627 |
|
| 628 |
def test_plugin_bark_rejects_incompatible_generated_iv(monkeypatch): |
| 629 |
"""NotifyBark() rejects RNG output Bark cannot decrypt.""" |
| 630 |
pytest.importorskip("cryptography") |
| 631 |
monkeypatch.setattr( |
| 632 |
bark_module.secrets, |
| 633 |
"token_urlsafe", |
| 634 |
lambda length: "too-short", |
| 635 |
) |
| 636 |
instance = NotifyBark( |
| 637 |
host="localhost", |
| 638 |
targets=("device-key",), |
| 639 |
secure=True, |
| 640 |
encryption_key="k" * 32, |
| 641 |
) |
| 642 |
|
| 643 |
with pytest.raises(ValueError, match="incompatible AES-GCM IV"): |
| 644 |
instance._encrypt_payload({"body": "private body"}) |
| 645 |
|
| 646 |
|
| 647 |
@mock.patch("requests.post") |
| 648 |
def test_plugin_bark_encryption_failure_prevents_network_io(mock_post): |
| 649 |
"""NotifyBark() fails closed when payload encryption fails.""" |
| 650 |
pytest.importorskip("cryptography") |
| 651 |
instance = NotifyBark( |
| 652 |
host="localhost", |
| 653 |
targets=("private-device",), |
| 654 |
secure=True, |
| 655 |
encryption_key="k" * 32, |
| 656 |
include_image=False, |
| 657 |
) |
| 658 |
instance.logger = mock.Mock() |
| 659 |
instance._encrypt_payload = mock.Mock( |
| 660 |
side_effect=ValueError("private encryption detail") |
| 661 |
) |
| 662 |
|
| 663 |
assert instance.send(body="private body", title="private title") is False |
| 664 |
mock_post.assert_not_called() |
| 665 |
logged = repr(instance.logger.method_calls) |
| 666 |
assert "private encryption detail" not in logged |
| 667 |
assert "private body" not in logged |
| 668 |
assert "private title" not in logged |
| 669 |
assert "private-device" not in logged |
| 670 |
|
| 671 |
|
| 672 |
@mock.patch("requests.post") |
| 673 |
def test_plugin_bark_encrypted_send_http_error(mock_post, monkeypatch): |
| 674 |
"""NotifyBark() suppresses response details when encrypted.""" |
| 675 |
pytest.importorskip("cryptography") |
| 676 |
monkeypatch.setattr( |
| 677 |
bark_module.secrets, |
| 678 |
"token_urlsafe", |
| 679 |
lambda length: "fixed-iv-123", |
| 680 |
) |
| 681 |
response = mock.Mock() |
| 682 |
response.status_code = requests.codes.internal_server_error |
| 683 |
response.content = b"private response body" |
| 684 |
mock_post.return_value = response |
| 685 |
instance = NotifyBark( |
| 686 |
host="localhost", |
| 687 |
targets=("private-device",), |
| 688 |
secure=True, |
| 689 |
encryption_key="k" * 32, |
| 690 |
include_image=False, |
| 691 |
) |
| 692 |
instance.logger = mock.Mock() |
| 693 |
|
| 694 |
assert instance.send(body="private body", title="private title") is False |
| 695 |
|
| 696 |
|
| 697 |
assert mock_post.call_count == 1 |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
instance.logger.warning.assert_called_once() |
| 702 |
logged = repr(instance.logger.method_calls) |
| 703 |
assert "private response body" not in logged |
| 704 |
assert "private body" not in logged |
| 705 |
assert "private title" not in logged |
| 706 |
assert "private-device" not in logged |
| 707 |
|
| 708 |
|
| 709 |
@mock.patch("requests.post") |
| 710 |
def test_plugin_bark_encrypted_send_connection_error(mock_post, monkeypatch): |
| 711 |
"""NotifyBark() suppresses socket exception details when encrypted.""" |
| 712 |
pytest.importorskip("cryptography") |
| 713 |
monkeypatch.setattr( |
| 714 |
bark_module.secrets, |
| 715 |
"token_urlsafe", |
| 716 |
lambda length: "fixed-iv-123", |
| 717 |
) |
| 718 |
mock_post.side_effect = requests.RequestException("private socket detail") |
| 719 |
instance = NotifyBark( |
| 720 |
host="localhost", |
| 721 |
targets=("private-device",), |
| 722 |
secure=True, |
| 723 |
encryption_key="k" * 32, |
| 724 |
include_image=False, |
| 725 |
) |
| 726 |
instance.logger = mock.Mock() |
| 727 |
|
| 728 |
assert instance.send(body="private body", title="private title") is False |
| 729 |
assert mock_post.call_count == 1 |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
instance.logger.warning.assert_called_once() |
| 734 |
logged = repr(instance.logger.method_calls) |
| 735 |
assert "private socket detail" not in logged |
| 736 |
assert "private body" not in logged |
| 737 |
assert "private title" not in logged |
| 738 |
assert "private-device" not in logged |
| 739 |
|
| 740 |
|
| 741 |
def test_plugin_bark_runtime_dependencies(): |
| 742 |
"""NotifyBark() declares its optional cryptography dependency.""" |
| 743 |
assert NotifyBark.runtime_deps() == ("cryptography",) |