| 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 |
import base64 |
| 29 |
import json |
| 30 |
|
| 31 |
# Disable logging for a cleaner testing output |
| 32 |
import logging |
| 33 |
import os |
| 34 |
import stat |
| 35 |
import sys |
| 36 |
from unittest import mock |
| 37 |
|
| 38 |
from helpers import AppriseURLTester |
| 39 |
import pytest |
| 40 |
import requests |
| 41 |
|
| 42 |
from apprise import Apprise, asset, exception, url |
| 43 |
from apprise.common import PersistentStoreMode |
| 44 |
from apprise.exception import AppriseImproperlyConfigured |
| 45 |
from apprise.plugins.vapid import VAPID_API_LOOKUP, NotifyVapid |
| 46 |
from apprise.plugins.vapid.subscription import ( |
| 47 |
WebPushSubscription, |
| 48 |
WebPushSubscriptionManager, |
| 49 |
webpush_origin, |
| 50 |
) |
| 51 |
from apprise.utils.cwe312 import cwe312_loggable |
| 52 |
from apprise.utils.pem import ApprisePEMController |
| 53 |
|
| 54 |
logging.disable(logging.CRITICAL) |
| 55 |
|
| 56 |
# Attachment Directory |
| 57 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 58 |
|
| 59 |
# a test UUID we can use |
| 60 |
SUBSCRIBER = "[email protected]" |
| 61 |
|
| 62 |
PLUGIN_ID = "vapid" |
| 63 |
|
| 64 |
# Windows has no POSIX file modes, so tests that check them are skipped |
| 65 |
# there. |
| 66 |
IS_WINDOWS = sys.platform == "win32" |
| 67 |
|
| 68 |
# Root ignores file permissions entirely, so a read-only file still tests |
| 69 |
# as writable. RPM builds run their test suite as root. |
| 70 |
IS_ROOT = hasattr(os, "geteuid") and os.geteuid() == 0 |
| 71 |
|
| 72 |
# Our Testing URLs |
| 73 |
apprise_url_tests = ( |
| 74 |
( |
| 75 |
"vapid://", |
| 76 |
{ |
| 77 |
"instance": AppriseImproperlyConfigured, |
| 78 |
}, |
| 79 |
), |
| 80 |
( |
| 81 |
"vapid://:@/", |
| 82 |
{ |
| 83 |
"instance": AppriseImproperlyConfigured, |
| 84 |
}, |
| 85 |
), |
| 86 |
( |
| 87 |
"vapid://invalid-subscriber", |
| 88 |
{ |
| 89 |
# An invalid Subscriber |
| 90 |
"instance": AppriseImproperlyConfigured, |
| 91 |
}, |
| 92 |
), |
| 93 |
( |
| 94 |
"vapid://[email protected]", |
| 95 |
{ |
| 96 |
# bare bone requirements met, but we don't have our subscription |
| 97 |
# file or our private key (pem) |
| 98 |
"instance": NotifyVapid, |
| 99 |
# We'll fail to respond because we would not have found any |
| 100 |
# configuration to load |
| 101 |
"notify_response": False, |
| 102 |
}, |
| 103 |
), |
| 104 |
( |
| 105 |
"vapid://[email protected]?keyfile=invalid&subfile=invalid", |
| 106 |
{ |
| 107 |
# Test passing keyfile and subfile on our path (even if invalid) |
| 108 |
"instance": NotifyVapid, |
| 109 |
# We'll fail to respond because we would not have found any |
| 110 |
# configuration to load |
| 111 |
"notify_response": False, |
| 112 |
}, |
| 113 |
), |
| 114 |
( |
| 115 |
"vapid://[email protected]/[email protected]", |
| 116 |
{ |
| 117 |
# we don't have our subscription file or private key |
| 118 |
"instance": NotifyVapid, |
| 119 |
"notify_response": False, |
| 120 |
}, |
| 121 |
), |
| 122 |
( |
| 123 |
"vapid://[email protected]/[email protected]", |
| 124 |
{ |
| 125 |
"instance": NotifyVapid, |
| 126 |
# force a failure |
| 127 |
"response": False, |
| 128 |
"requests_response_code": requests.codes.internal_server_error, |
| 129 |
}, |
| 130 |
), |
| 131 |
( |
| 132 |
"vapid://[email protected]/[email protected]", |
| 133 |
{ |
| 134 |
"instance": NotifyVapid, |
| 135 |
# throw a bizarre code forcing us to fail to look it up |
| 136 |
"response": False, |
| 137 |
"requests_response_code": 999, |
| 138 |
}, |
| 139 |
), |
| 140 |
( |
| 141 |
"vapid://[email protected]/[email protected]", |
| 142 |
{ |
| 143 |
"instance": NotifyVapid, |
| 144 |
# Throws a series of i/o exceptions with this flag |
| 145 |
# is set and tests that we gracefully handle them |
| 146 |
"test_requests_exceptions": True, |
| 147 |
}, |
| 148 |
), |
| 149 |
) |
| 150 |
|
| 151 |
|
| 152 |
@pytest.fixture |
| 153 |
def patch_persistent_store_namespace(tmpdir): |
| 154 |
"""Force an easy to test environment.""" |
| 155 |
with ( |
| 156 |
mock.patch.object(url.URLBase, "url_id", return_value=PLUGIN_ID), |
| 157 |
mock.patch.object( |
| 158 |
asset.AppriseAsset, "storage_mode", PersistentStoreMode.AUTO |
| 159 |
), |
| 160 |
mock.patch.object(asset.AppriseAsset, "storage_path", str(tmpdir)), |
| 161 |
): |
| 162 |
tmp_dir = tmpdir.mkdir(PLUGIN_ID) |
| 163 |
# Return the directory name |
| 164 |
yield str(tmp_dir) |
| 165 |
|
| 166 |
|
| 167 |
@pytest.fixture |
| 168 |
def subscription_reference(): |
| 169 |
return { |
| 170 |
"[email protected]": { |
| 171 |
"endpoint": "https://fcm.googleapis.com/fcm/send/default", |
| 172 |
"keys": { |
| 173 |
"p256dh": ( |
| 174 |
"BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO" |
| 175 |
"5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0" |
| 176 |
), |
| 177 |
"auth": "k9Xzm43nBGo=", |
| 178 |
}, |
| 179 |
}, |
| 180 |
"user1": { |
| 181 |
"endpoint": "https://fcm.googleapis.com/fcm/send/abc123", |
| 182 |
"keys": { |
| 183 |
"p256dh": ( |
| 184 |
"BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO" |
| 185 |
"5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0" |
| 186 |
), |
| 187 |
"auth": "k9Xzm43nBGo=", |
| 188 |
}, |
| 189 |
}, |
| 190 |
"user2": { |
| 191 |
"endpoint": "https://fcm.googleapis.com/fcm/send/def456", |
| 192 |
"keys": { |
| 193 |
"p256dh": ( |
| 194 |
"BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO" |
| 195 |
"5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0" |
| 196 |
), |
| 197 |
"auth": "k9Xzm43nBGo=", |
| 198 |
}, |
| 199 |
}, |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
@pytest.mark.skipif( |
| 204 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 205 |
) |
| 206 |
def test_plugin_vapid_urls(): |
| 207 |
""" |
| 208 |
NotifyVapid() Apprise URLs - No Config |
| 209 |
|
| 210 |
""" |
| 211 |
|
| 212 |
# Run our general tests |
| 213 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 214 |
|
| 215 |
|
| 216 |
@pytest.mark.skipif( |
| 217 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 218 |
) |
| 219 |
def test_plugin_vapid_urls_with_required_assets( |
| 220 |
patch_persistent_store_namespace, subscription_reference |
| 221 |
): |
| 222 |
"""NotifyVapid() Apprise URLs With Config.""" |
| 223 |
|
| 224 |
# Determine our store |
| 225 |
pc = ApprisePEMController(path=patch_persistent_store_namespace) |
| 226 |
assert pc.keygen() is True |
| 227 |
|
| 228 |
# Write our subscriptions file to disk |
| 229 |
subscription_file = os.path.join( |
| 230 |
patch_persistent_store_namespace, |
| 231 |
WebPushSubscriptionManager.default_filename, |
| 232 |
) |
| 233 |
|
| 234 |
with open(subscription_file, "w") as f: |
| 235 |
f.write(json.dumps(subscription_reference)) |
| 236 |
|
| 237 |
tests = ( |
| 238 |
( |
| 239 |
"vapid://[email protected]", |
| 240 |
{ |
| 241 |
# [email protected] loaded (also used as subscriber id) |
| 242 |
"instance": NotifyVapid, |
| 243 |
}, |
| 244 |
), |
| 245 |
( |
| 246 |
"vapid://[email protected]/[email protected]", |
| 247 |
{ |
| 248 |
# no [email protected] key entry |
| 249 |
"instance": NotifyVapid, |
| 250 |
"notify_response": False, |
| 251 |
}, |
| 252 |
), |
| 253 |
( |
| 254 |
"vapid://[email protected]/user1?to=user2", |
| 255 |
{ |
| 256 |
# We'll succesfully notify 2 users |
| 257 |
"instance": NotifyVapid, |
| 258 |
}, |
| 259 |
), |
| 260 |
( |
| 261 |
"vapid://user1?to=user2&[email protected]", |
| 262 |
{ |
| 263 |
# We'll succesfully notify 2 users |
| 264 |
"instance": NotifyVapid, |
| 265 |
}, |
| 266 |
), |
| 267 |
( |
| 268 |
"vapid://?to=user2&[email protected]", |
| 269 |
{ |
| 270 |
# No host provided |
| 271 |
"instance": NotifyVapid, |
| 272 |
}, |
| 273 |
), |
| 274 |
( |
| 275 |
"vapid://[email protected]?to=user2&[email protected]", |
| 276 |
{ |
| 277 |
# We'll succesfully notify 2 users |
| 278 |
"instance": NotifyVapid, |
| 279 |
}, |
| 280 |
), |
| 281 |
( |
| 282 |
"vapid://[email protected]/user1?to=user2&ttl=15", |
| 283 |
{ |
| 284 |
# test ttl |
| 285 |
"instance": NotifyVapid, |
| 286 |
}, |
| 287 |
), |
| 288 |
( |
| 289 |
"vapid://[email protected]/user1?to=user2&ttl=", |
| 290 |
{ |
| 291 |
# test ttl |
| 292 |
"instance": NotifyVapid, |
| 293 |
}, |
| 294 |
), |
| 295 |
( |
| 296 |
"vapid://[email protected]/user1?to=user2&ttl=invalid", |
| 297 |
{ |
| 298 |
# test ttl |
| 299 |
"instance": NotifyVapid, |
| 300 |
}, |
| 301 |
), |
| 302 |
( |
| 303 |
"vapid://[email protected]/user1?to=user2&ttl=-4000", |
| 304 |
{ |
| 305 |
# bad ttl |
| 306 |
"instance": AppriseImproperlyConfigured, |
| 307 |
}, |
| 308 |
), |
| 309 |
( |
| 310 |
"vapid://[email protected]/user1?to=user2&mode=edge", |
| 311 |
{ |
| 312 |
# test mode |
| 313 |
"instance": NotifyVapid, |
| 314 |
}, |
| 315 |
), |
| 316 |
( |
| 317 |
"vapid://[email protected]/user1?to=user2&mode=", |
| 318 |
{ |
| 319 |
# test mode |
| 320 |
"instance": AppriseImproperlyConfigured, |
| 321 |
}, |
| 322 |
), |
| 323 |
( |
| 324 |
"vapid://[email protected]/user1?to=user2&mode=invalid", |
| 325 |
{ |
| 326 |
# test mode more |
| 327 |
"instance": AppriseImproperlyConfigured, |
| 328 |
}, |
| 329 |
), |
| 330 |
( |
| 331 |
"vapid://[email protected]/user1", |
| 332 |
{ |
| 333 |
"instance": NotifyVapid, |
| 334 |
# force a failure |
| 335 |
"response": False, |
| 336 |
"requests_response_code": requests.codes.internal_server_error, |
| 337 |
}, |
| 338 |
), |
| 339 |
( |
| 340 |
"vapid://[email protected]/user1", |
| 341 |
{ |
| 342 |
"instance": NotifyVapid, |
| 343 |
# throw a bizarre code forcing us to fail to look it up |
| 344 |
"response": False, |
| 345 |
"requests_response_code": 999, |
| 346 |
}, |
| 347 |
), |
| 348 |
( |
| 349 |
"vapid://[email protected]/user1", |
| 350 |
{ |
| 351 |
"instance": NotifyVapid, |
| 352 |
# Throws a series of connection and transfer exceptions |
| 353 |
# when this flag is set and tests that we gracefully handle |
| 354 |
# them |
| 355 |
"test_requests_exceptions": True, |
| 356 |
}, |
| 357 |
), |
| 358 |
) |
| 359 |
|
| 360 |
AppriseURLTester(tests=tests).run_all() |
| 361 |
|
| 362 |
|
| 363 |
@pytest.mark.skipif( |
| 364 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 365 |
) |
| 366 |
def test_plugin_vapid_subscriptions(tmpdir): |
| 367 |
"""NotifyVapid() Subscriptions.""" |
| 368 |
|
| 369 |
# Temporary directory |
| 370 |
tmpdir0 = tmpdir.mkdir("tmp00") |
| 371 |
|
| 372 |
with pytest.raises(exception.AppriseInvalidData): |
| 373 |
# Integer not supported |
| 374 |
WebPushSubscription(42) |
| 375 |
|
| 376 |
with pytest.raises(exception.AppriseInvalidData): |
| 377 |
# Not the correct format |
| 378 |
WebPushSubscription("bad-content") |
| 379 |
|
| 380 |
with pytest.raises(exception.AppriseInvalidData): |
| 381 |
# Invalid JSON |
| 382 |
WebPushSubscription("{") |
| 383 |
|
| 384 |
with pytest.raises(exception.AppriseInvalidData): |
| 385 |
# Empty Dictionary |
| 386 |
WebPushSubscription({}) |
| 387 |
|
| 388 |
with pytest.raises(exception.AppriseInvalidData): |
| 389 |
WebPushSubscription( |
| 390 |
{ |
| 391 |
"endpoint": "https://fcm.googleapis.com/fcm/send/abc123", |
| 392 |
"keys": { |
| 393 |
"p256dh": "BNcW4oA7zq5H9TKIrA3XfKclN2fX9P_7NR=", |
| 394 |
"auth": 42, |
| 395 |
}, |
| 396 |
} |
| 397 |
) |
| 398 |
|
| 399 |
with pytest.raises(exception.AppriseInvalidData): |
| 400 |
WebPushSubscription( |
| 401 |
{ |
| 402 |
"endpoint": "https://fcm.googleapis.com/fcm/send/abc123", |
| 403 |
"keys": { |
| 404 |
"p256dh": 42, |
| 405 |
"auth": "k9Xzm43nBGo=", |
| 406 |
}, |
| 407 |
} |
| 408 |
) |
| 409 |
|
| 410 |
with pytest.raises(exception.AppriseInvalidData): |
| 411 |
WebPushSubscription( |
| 412 |
{ |
| 413 |
"endpoint": "https://fcm.googleapis.com/fcm/send/abc123", |
| 414 |
} |
| 415 |
) |
| 416 |
|
| 417 |
with pytest.raises(exception.AppriseInvalidData): |
| 418 |
WebPushSubscription( |
| 419 |
{ |
| 420 |
"endpoint": "https://fcm.googleapis.com/fcm/send/abc123", |
| 421 |
"keys": {}, |
| 422 |
} |
| 423 |
) |
| 424 |
|
| 425 |
with pytest.raises(exception.AppriseInvalidData): |
| 426 |
# Invalid p256dh public key provided |
| 427 |
wps = WebPushSubscription( |
| 428 |
{ |
| 429 |
"endpoint": "https://fcm.googleapis.com/fcm/send/abc123", |
| 430 |
"keys": { |
| 431 |
"p256dh": "BNcW4oA7zq5H9TKIrA3XfKclN2fX9P_7NR=", |
| 432 |
"auth": "k9Xzm43nBGo=", |
| 433 |
}, |
| 434 |
} |
| 435 |
) |
| 436 |
|
| 437 |
# An empty object |
| 438 |
wps = WebPushSubscription() |
| 439 |
assert bool(wps) is False |
| 440 |
assert isinstance(wps.json(), str) |
| 441 |
assert json.loads(wps.json()) |
| 442 |
assert str(wps) == "" |
| 443 |
assert wps.auth is None |
| 444 |
assert wps.endpoint is None |
| 445 |
assert wps.p256dh is None |
| 446 |
assert wps.public_key is None |
| 447 |
# We can't write anything as there is nothing loaded |
| 448 |
assert wps.write(os.path.join(str(tmpdir0), "subscriptions.json")) is False |
| 449 |
|
| 450 |
# A valid key |
| 451 |
wps = WebPushSubscription( |
| 452 |
{ |
| 453 |
"endpoint": "https://fcm.googleapis.com/fcm/send/abc123", |
| 454 |
"keys": { |
| 455 |
"p256dh": ( |
| 456 |
"BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO" |
| 457 |
"5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0" |
| 458 |
), |
| 459 |
"auth": "k9Xzm43nBGo=", |
| 460 |
}, |
| 461 |
} |
| 462 |
) |
| 463 |
|
| 464 |
assert bool(wps) is True |
| 465 |
assert isinstance(wps.json(), str) |
| 466 |
assert json.loads(wps.json()) |
| 467 |
assert str(wps) == "abc123" |
| 468 |
assert wps.auth == "k9Xzm43nBGo=" |
| 469 |
assert wps.endpoint == "https://fcm.googleapis.com/fcm/send/abc123" |
| 470 |
assert ( |
| 471 |
wps.p256dh == "BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO" |
| 472 |
"5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0" |
| 473 |
) |
| 474 |
assert wps.public_key is not None |
| 475 |
|
| 476 |
# Currently no files here |
| 477 |
assert os.listdir(str(tmpdir0)) == [] |
| 478 |
|
| 479 |
# Bad content |
| 480 |
assert wps.write(object) is False |
| 481 |
assert wps.write(None) is False |
| 482 |
# Can't write to a name already taken by as a directory |
| 483 |
assert wps.write(str(tmpdir0)) is False |
| 484 |
# Can't write to a name already taken by as a directory |
| 485 |
assert wps.write(os.path.join(str(tmpdir0), "subscriptions.json")) is True |
| 486 |
assert os.listdir(str(tmpdir0)) == ["subscriptions.json"] |
| 487 |
|
| 488 |
|
| 489 |
@pytest.mark.skipif( |
| 490 |
"cryptography" in sys.modules, |
| 491 |
reason="Requires that cryptography NOT be installed", |
| 492 |
) |
| 493 |
def test_plugin_vapid_subscriptions_without_c(): |
| 494 |
"""NotifyVapid() Subscriptions (no Cryptography)""" |
| 495 |
with pytest.raises(exception.AppriseInvalidData): |
| 496 |
# A valid key that can't be loaded because crytography is missing |
| 497 |
WebPushSubscription( |
| 498 |
{ |
| 499 |
"endpoint": "https://fcm.googleapis.com/fcm/send/abc123", |
| 500 |
"keys": { |
| 501 |
"p256dh": ( |
| 502 |
"BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO" |
| 503 |
"5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0" |
| 504 |
), |
| 505 |
"auth": "k9Xzm43nBGo=", |
| 506 |
}, |
| 507 |
} |
| 508 |
) |
| 509 |
|
| 510 |
|
| 511 |
@pytest.mark.skipif( |
| 512 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 513 |
) |
| 514 |
def test_plugin_vapid_subscription_manager(tmpdir): |
| 515 |
"""NotifyVapid() Subscription Manager.""" |
| 516 |
|
| 517 |
# Temporary directory |
| 518 |
tmpdir0 = tmpdir.mkdir("tmp00") |
| 519 |
|
| 520 |
with pytest.raises(exception.AppriseInvalidData): |
| 521 |
# An invalid object |
| 522 |
smgr = WebPushSubscriptionManager() |
| 523 |
smgr["abc"] = "invalid" |
| 524 |
|
| 525 |
with pytest.raises(exception.AppriseInvalidData): |
| 526 |
# An invalid object |
| 527 |
smgr = WebPushSubscriptionManager() |
| 528 |
smgr += "invalid" |
| 529 |
|
| 530 |
smgr = WebPushSubscriptionManager() |
| 531 |
|
| 532 |
assert bool(smgr) is False |
| 533 |
assert len(smgr) == 0 |
| 534 |
|
| 535 |
sub = { |
| 536 |
"endpoint": "https://fcm.googleapis.com/fcm/send/abc123", |
| 537 |
"keys": { |
| 538 |
"p256dh": ( |
| 539 |
"BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO" |
| 540 |
"5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0" |
| 541 |
), |
| 542 |
"auth": "k9Xzm43nBGo=", |
| 543 |
}, |
| 544 |
} |
| 545 |
|
| 546 |
assert smgr.add(sub) is True |
| 547 |
assert bool(smgr) is True |
| 548 |
assert len(smgr) == 1 |
| 549 |
|
| 550 |
# Same sub (overwrites same slot) |
| 551 |
smgr += sub |
| 552 |
assert bool(smgr) is True |
| 553 |
assert len(smgr) == 1 |
| 554 |
|
| 555 |
# This makes a copy |
| 556 |
smgr["abc"] = smgr["abc123"] |
| 557 |
assert bool(smgr) is True |
| 558 |
assert len(smgr) == 2 |
| 559 |
|
| 560 |
assert isinstance(smgr["abc123"], WebPushSubscription) |
| 561 |
|
| 562 |
# Currently no files here |
| 563 |
assert os.listdir(str(tmpdir0)) == [] |
| 564 |
|
| 565 |
# Write our content |
| 566 |
assert smgr.write(os.path.join(str(tmpdir0), "subscriptions.json")) is True |
| 567 |
|
| 568 |
assert os.listdir(str(tmpdir0)) == ["subscriptions.json"] |
| 569 |
|
| 570 |
# Reset our object |
| 571 |
smgr.clear() |
| 572 |
assert bool(smgr) is False |
| 573 |
assert len(smgr) == 0 |
| 574 |
|
| 575 |
# Load our content back |
| 576 |
assert smgr.load(os.path.join(str(tmpdir0), "subscriptions.json")) is True |
| 577 |
assert bool(smgr) is True |
| 578 |
assert len(smgr) == 2 |
| 579 |
|
| 580 |
# Write over our file using the standard Subscription format |
| 581 |
assert ( |
| 582 |
smgr["abc123"].write(os.path.join(str(tmpdir0), "subscriptions.json")) |
| 583 |
is True |
| 584 |
) |
| 585 |
|
| 586 |
# We can still open this type as well |
| 587 |
assert smgr.load(os.path.join(str(tmpdir0), "subscriptions.json")) is True |
| 588 |
assert bool(smgr) is True |
| 589 |
assert len(smgr) == 1 |
| 590 |
|
| 591 |
smgr.clear() |
| 592 |
bad_entry = { |
| 593 |
"endpoint": "https://fcm.googleapis.com/fcm/send/abc123", |
| 594 |
"keys": { |
| 595 |
"p256dh": "invalid", |
| 596 |
"auth": "garbage", |
| 597 |
}, |
| 598 |
} |
| 599 |
|
| 600 |
subscriptions = os.path.join(str(tmpdir0), "subscriptions.json") |
| 601 |
with open(subscriptions, "w", encoding="utf-8") as f: |
| 602 |
# A bad JSON file |
| 603 |
f.write("{") |
| 604 |
assert smgr.load(subscriptions) is False |
| 605 |
|
| 606 |
with open(subscriptions, "w", encoding="utf-8") as f: |
| 607 |
# not expected dictionary |
| 608 |
f.write("null") |
| 609 |
assert smgr.load(subscriptions) is False |
| 610 |
|
| 611 |
subscriptions = os.path.join(str(tmpdir0), "subscriptions.json") |
| 612 |
with open(subscriptions, "w", encoding="utf-8") as f: |
| 613 |
json.dump(bad_entry, f) |
| 614 |
assert smgr.load(subscriptions) is False |
| 615 |
|
| 616 |
# Create bad data |
| 617 |
bad_data = { |
| 618 |
"bad1": bad_entry, |
| 619 |
"bad2": bad_entry, |
| 620 |
"bad3": bad_entry, |
| 621 |
"bad4": bad_entry, |
| 622 |
} |
| 623 |
subscriptions = os.path.join(str(tmpdir0), "subscriptions.json") |
| 624 |
with open(subscriptions, "w", encoding="utf-8") as f: |
| 625 |
json.dump(bad_data, f) |
| 626 |
assert smgr.load(subscriptions) is False |
| 627 |
assert smgr.load("invalid-file") is False |
| 628 |
|
| 629 |
|
| 630 |
@pytest.mark.skipif( |
| 631 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 632 |
) |
| 633 |
@mock.patch("requests.post") |
| 634 |
def test_plugin_vapid_initializations(mock_post, tmpdir): |
| 635 |
"""NotifyVapid() Initializations.""" |
| 636 |
|
| 637 |
# Assign our mock object our return value |
| 638 |
okay_response = requests.Request() |
| 639 |
okay_response.status_code = requests.codes.ok |
| 640 |
okay_response.content = "" |
| 641 |
mock_post.return_value = okay_response |
| 642 |
|
| 643 |
# Temporary directory |
| 644 |
tmpdir0 = tmpdir.mkdir("tmp00") |
| 645 |
|
| 646 |
# Write our subfile |
| 647 |
smgr = WebPushSubscriptionManager() |
| 648 |
sub = { |
| 649 |
"endpoint": "https://fcm.googleapis.com/fcm/send/abc123", |
| 650 |
"keys": { |
| 651 |
"p256dh": ( |
| 652 |
"BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO" |
| 653 |
"5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0" |
| 654 |
), |
| 655 |
"auth": "k9Xzm43nBGo=", |
| 656 |
}, |
| 657 |
} |
| 658 |
subfile = os.path.join(str(tmpdir0), "subscriptions.json") |
| 659 |
assert smgr.add(sub) is True |
| 660 |
assert smgr.add(smgr["abc123"]) is True |
| 661 |
assert os.listdir(str(tmpdir0)) == [] |
| 662 |
|
| 663 |
with mock.patch("json.dump", side_effect=OSError): |
| 664 |
# We will fial to write |
| 665 |
assert smgr.write(subfile) is False |
| 666 |
|
| 667 |
assert smgr.write(subfile) is True |
| 668 |
assert os.listdir(str(tmpdir0)) == ["subscriptions.json"] |
| 669 |
assert isinstance(smgr.json(), str) |
| 670 |
|
| 671 |
asset_ = asset.AppriseAsset( |
| 672 |
storage_mode=PersistentStoreMode.FLUSH, |
| 673 |
storage_path=str(tmpdir0), |
| 674 |
# Auto-gen our private/public key pair |
| 675 |
pem_autogen=True, |
| 676 |
) |
| 677 |
|
| 678 |
# Auto-Key Generation |
| 679 |
obj = NotifyVapid( |
| 680 |
"[email protected]", |
| 681 |
targets=[ |
| 682 |
"abc123", |
| 683 |
], |
| 684 |
subfile=subfile, |
| 685 |
asset=asset_, |
| 686 |
) |
| 687 |
assert isinstance(obj, NotifyVapid) |
| 688 |
# Our subscription directory + our |
| 689 |
# persistent store where our keys were generated |
| 690 |
assert len(os.listdir(str(tmpdir0))) == 2 |
| 691 |
|
| 692 |
# Second call re-references keys previously generated |
| 693 |
obj = NotifyVapid( |
| 694 |
"[email protected]", |
| 695 |
targets=[ |
| 696 |
"abc123", |
| 697 |
], |
| 698 |
subfile=subfile, |
| 699 |
asset=asset_, |
| 700 |
) |
| 701 |
assert isinstance(obj, NotifyVapid) |
| 702 |
assert isinstance(obj.url(), str) |
| 703 |
assert obj.send("test") is True |
| 704 |
# A second message makes no difference; what is loaded into memory is used |
| 705 |
assert obj.send("test") is True |
| 706 |
|
| 707 |
obj = NotifyVapid( |
| 708 |
"[email protected]", |
| 709 |
targets=[ |
| 710 |
"abc123", |
| 711 |
], |
| 712 |
subfile="/a/bad/path", |
| 713 |
asset=asset_, |
| 714 |
) |
| 715 |
assert isinstance(obj, NotifyVapid) |
| 716 |
assert isinstance(obj.url(), str) |
| 717 |
assert obj.send("test") is False |
| 718 |
# A second message makes no difference; what is loaded into memory is used |
| 719 |
assert obj.send("test") is False |
| 720 |
|
| 721 |
# Detect our keyfile |
| 722 |
cache_dir = next( |
| 723 |
x |
| 724 |
for x in os.listdir(str(tmpdir0)) |
| 725 |
if not x.endswith("subscriptions.json") |
| 726 |
) |
| 727 |
|
| 728 |
# Test fixed assignment to our keyfile |
| 729 |
keyfile = os.path.join(str(tmpdir0), cache_dir, "private_key.pem") |
| 730 |
assert os.path.exists(keyfile) |
| 731 |
obj = NotifyVapid( |
| 732 |
"[email protected]", |
| 733 |
targets=[ |
| 734 |
"abc123", |
| 735 |
], |
| 736 |
keyfile=keyfile, |
| 737 |
subfile=subfile, |
| 738 |
asset=asset_, |
| 739 |
) |
| 740 |
assert isinstance(obj, NotifyVapid) |
| 741 |
assert isinstance(obj.url(), str) |
| 742 |
assert obj.send("test") is True |
| 743 |
# A second message makes no difference; what is loaded into memory is used |
| 744 |
assert obj.send("test") is True |
| 745 |
|
| 746 |
# Invalid Keyfile |
| 747 |
obj = NotifyVapid( |
| 748 |
"[email protected]", |
| 749 |
targets=[ |
| 750 |
"abc123", |
| 751 |
], |
| 752 |
keyfile=subfile, |
| 753 |
subfile=subfile, |
| 754 |
asset=asset_, |
| 755 |
) |
| 756 |
assert isinstance(obj, NotifyVapid) |
| 757 |
assert isinstance(obj.url(), str) |
| 758 |
assert obj.send("test") is False |
| 759 |
# A second message makes no difference; what is loaded into memory is used |
| 760 |
assert obj.send("test") is False |
| 761 |
|
| 762 |
# AutoGen Temporary directory |
| 763 |
tmpdir1 = tmpdir.mkdir("tmp01") |
| 764 |
asset2 = asset.AppriseAsset( |
| 765 |
storage_mode=PersistentStoreMode.FLUSH, |
| 766 |
storage_path=str(tmpdir1), |
| 767 |
# Auto-gen our private/public key pair |
| 768 |
pem_autogen=True, |
| 769 |
) |
| 770 |
|
| 771 |
assert os.listdir(str(tmpdir1)) == [] |
| 772 |
obj = NotifyVapid( |
| 773 |
"[email protected]", |
| 774 |
targets=[ |
| 775 |
"abc123", |
| 776 |
], |
| 777 |
keyfile=keyfile, |
| 778 |
asset=asset2, |
| 779 |
) |
| 780 |
assert isinstance(obj, NotifyVapid) |
| 781 |
assert isinstance(obj.url(), str) |
| 782 |
# We have a temporary subscription file we can use |
| 783 |
assert os.listdir(str(tmpdir1)) == ["00088ad3"] |
| 784 |
# We will have a dud configuration file, but at least it's something |
| 785 |
# to help the user with |
| 786 |
assert obj.send("test") is False |
| 787 |
# Second instance fails as well |
| 788 |
assert obj.send("test") is False |
| 789 |
|
| 790 |
# AutoGen Temporary directory |
| 791 |
tmpdir2 = tmpdir.mkdir("tmp02") |
| 792 |
asset3 = asset.AppriseAsset( |
| 793 |
storage_mode=PersistentStoreMode.FLUSH, |
| 794 |
storage_path=str(tmpdir2), |
| 795 |
# Auto-gen our private/public key pair |
| 796 |
pem_autogen=True, |
| 797 |
) |
| 798 |
|
| 799 |
# Test invalid keyfile |
| 800 |
assert os.path.exists(keyfile) |
| 801 |
obj = NotifyVapid( |
| 802 |
"[email protected]", |
| 803 |
targets=[ |
| 804 |
"abc123", |
| 805 |
], |
| 806 |
keyfile="invalid-file", |
| 807 |
subfile=subfile, |
| 808 |
asset=asset3, |
| 809 |
) |
| 810 |
assert isinstance(obj, NotifyVapid) |
| 811 |
assert isinstance(obj.url(), str) |
| 812 |
assert obj.send("test") is False |
| 813 |
# A second message makes no difference; what is loaded into memory is used |
| 814 |
assert obj.send("test") is False |
| 815 |
|
| 816 |
|
| 817 |
@pytest.mark.skipif( |
| 818 |
"cryptography" in sys.modules, |
| 819 |
reason="Requires that cryptography NOT be installed", |
| 820 |
) |
| 821 |
def test_plugin_vapid_initializations_without_c(tmpdir): |
| 822 |
"""NotifyVapid() Initializations without cryptography.""" |
| 823 |
# Temporary directory |
| 824 |
tmpdir0 = tmpdir.mkdir("tmp00") |
| 825 |
|
| 826 |
# Write our subfile |
| 827 |
smgr = WebPushSubscriptionManager() |
| 828 |
sub = { |
| 829 |
"endpoint": "https://fcm.googleapis.com/fcm/send/abc123", |
| 830 |
"keys": { |
| 831 |
"p256dh": ( |
| 832 |
"BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO" |
| 833 |
"5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0" |
| 834 |
), |
| 835 |
"auth": "k9Xzm43nBGo=", |
| 836 |
}, |
| 837 |
} |
| 838 |
subfile = os.path.join(str(tmpdir0), "subscriptions.json") |
| 839 |
assert smgr.add(sub) is False |
| 840 |
asset_ = asset.AppriseAsset( |
| 841 |
storage_mode=PersistentStoreMode.FLUSH, |
| 842 |
storage_path=str(tmpdir0), |
| 843 |
# Auto-gen our private/public key pair |
| 844 |
pem_autogen=True, |
| 845 |
) |
| 846 |
|
| 847 |
# Auto-Key Generation |
| 848 |
obj = NotifyVapid( |
| 849 |
"[email protected]", |
| 850 |
targets=[ |
| 851 |
"abc123", |
| 852 |
], |
| 853 |
subfile=subfile, |
| 854 |
asset=asset_, |
| 855 |
) |
| 856 |
assert isinstance(obj, NotifyVapid) |
| 857 |
|
| 858 |
|
| 859 |
def _jwt_audience(authorization): |
| 860 |
"""Returns the VAPID JWT audience from an Authorization header.""" |
| 861 |
|
| 862 |
token = authorization.split("t=", 1)[1].split(",", 1)[0] |
| 863 |
payload = token.split(".")[1] |
| 864 |
# JWT segments use base64url without padding. |
| 865 |
payload += "=" * (-len(payload) % 4) |
| 866 |
return json.loads(base64.urlsafe_b64decode(payload))["aud"] |
| 867 |
|
| 868 |
|
| 869 |
# Valid P-256 public key shared by the test subscriptions. |
| 870 |
TEST_P256DH = ( |
| 871 |
"BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO" |
| 872 |
"5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0" |
| 873 |
) |
| 874 |
|
| 875 |
# Valid authentication secret shared by the test subscriptions. |
| 876 |
TEST_AUTH = "k9Xzm43nBGo=" |
| 877 |
|
| 878 |
|
| 879 |
def _mk_resp(code=requests.codes.ok): |
| 880 |
"""Builds a mocked push service response carrying the code given.""" |
| 881 |
|
| 882 |
response = requests.Request() |
| 883 |
response.status_code = code |
| 884 |
response.content = "" |
| 885 |
return response |
| 886 |
|
| 887 |
|
| 888 |
def _subscription(endpoint): |
| 889 |
"""Builds a single subscription dictionary for the endpoint given.""" |
| 890 |
|
| 891 |
return { |
| 892 |
"endpoint": endpoint, |
| 893 |
"keys": {"p256dh": TEST_P256DH, "auth": TEST_AUTH}, |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
def _write_subscriptions(path, endpoints): |
| 898 |
"""Builds a subscriptions.json containing the endpoints provided. |
| 899 |
|
| 900 |
``endpoints`` accepts: |
| 901 |
- a list of endpoints, each named after its own last path segment |
| 902 |
- a dictionary of name to endpoint pairs |
| 903 |
""" |
| 904 |
|
| 905 |
if isinstance(endpoints, dict): |
| 906 |
with open(path, "w") as f: |
| 907 |
json.dump( |
| 908 |
{ |
| 909 |
name: _subscription(endpoint) |
| 910 |
for name, endpoint in endpoints.items() |
| 911 |
}, |
| 912 |
f, |
| 913 |
) |
| 914 |
return |
| 915 |
|
| 916 |
smgr = WebPushSubscriptionManager() |
| 917 |
for endpoint in endpoints: |
| 918 |
assert smgr.add(_subscription(endpoint)) is True |
| 919 |
|
| 920 |
assert smgr.write(path) is True |
| 921 |
|
| 922 |
|
| 923 |
def _mk_notifier(tmpdir, endpoints, targets, name="vapid"): |
| 924 |
"""Builds a NotifyVapid() backed by a subscriptions file on disk. |
| 925 |
|
| 926 |
Returns the notifier along with the path of the file that was written. |
| 927 |
""" |
| 928 |
|
| 929 |
tmpdir0 = tmpdir.mkdir(name) |
| 930 |
subfile = os.path.join(str(tmpdir0), "subscriptions.json") |
| 931 |
_write_subscriptions(subfile, endpoints) |
| 932 |
|
| 933 |
obj = NotifyVapid( |
| 934 |
"[email protected]", |
| 935 |
targets=targets, |
| 936 |
subfile=subfile, |
| 937 |
asset=asset.AppriseAsset( |
| 938 |
storage_mode=PersistentStoreMode.FLUSH, |
| 939 |
storage_path=str(tmpdir0), |
| 940 |
pem_autogen=True, |
| 941 |
), |
| 942 |
) |
| 943 |
return obj, subfile |
| 944 |
|
| 945 |
|
| 946 |
@pytest.mark.skipif( |
| 947 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 948 |
) |
| 949 |
@mock.patch("requests.post") |
| 950 |
def test_plugin_vapid_subscription_endpoint(mock_post, tmpdir): |
| 951 |
"""NotifyVapid() delivers to the endpoint of the subscription itself.""" |
| 952 |
|
| 953 |
mock_post.return_value = _mk_resp() |
| 954 |
|
| 955 |
endpoint = "https://fcm.googleapis.com/fcm/send/abc123" |
| 956 |
obj, _ = _mk_notifier(tmpdir, [endpoint], ["abc123"], name="tmp10") |
| 957 |
assert obj.send("test") is True |
| 958 |
|
| 959 |
# Use the browser-issued endpoint instead of the mode's base URL. |
| 960 |
assert mock_post.call_args[0][0] == endpoint |
| 961 |
assert mock_post.call_args[0][0] != VAPID_API_LOOKUP[obj.mode] |
| 962 |
|
| 963 |
# Use the endpoint's origin as the JWT audience. |
| 964 |
headers = mock_post.call_args[1]["headers"] |
| 965 |
assert _jwt_audience(headers["Authorization"]) == ( |
| 966 |
"https://fcm.googleapis.com" |
| 967 |
) |
| 968 |
|
| 969 |
|
| 970 |
@pytest.mark.skipif( |
| 971 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 972 |
) |
| 973 |
@mock.patch("requests.post") |
| 974 |
def test_plugin_vapid_multiple_endpoints(mock_post, tmpdir): |
| 975 |
"""NotifyVapid() handles subscriptions hosted by different services.""" |
| 976 |
|
| 977 |
mock_post.return_value = _mk_resp() |
| 978 |
|
| 979 |
endpoints = { |
| 980 |
"abc123": "https://fcm.googleapis.com/fcm/send/abc123", |
| 981 |
"xyz789": "https://web.push.apple.com/xyz789", |
| 982 |
} |
| 983 |
obj, _ = _mk_notifier( |
| 984 |
tmpdir, list(endpoints.values()), list(endpoints), name="tmp11" |
| 985 |
) |
| 986 |
assert obj.send("test") is True |
| 987 |
assert mock_post.call_count == len(endpoints) |
| 988 |
|
| 989 |
# Give each service its own endpoint and JWT audience. |
| 990 |
delivered = {} |
| 991 |
for call in mock_post.call_args_list: |
| 992 |
url = call[0][0] |
| 993 |
delivered[url] = _jwt_audience(call[1]["headers"]["Authorization"]) |
| 994 |
|
| 995 |
assert set(delivered) == set(endpoints.values()) |
| 996 |
assert delivered["https://fcm.googleapis.com/fcm/send/abc123"] == ( |
| 997 |
"https://fcm.googleapis.com" |
| 998 |
) |
| 999 |
assert delivered["https://web.push.apple.com/xyz789"] == ( |
| 1000 |
"https://web.push.apple.com" |
| 1001 |
) |
| 1002 |
|
| 1003 |
|
| 1004 |
@pytest.mark.skipif( |
| 1005 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1006 |
) |
| 1007 |
@pytest.mark.parametrize( |
| 1008 |
"status_code", |
| 1009 |
( |
| 1010 |
requests.codes.ok, |
| 1011 |
requests.codes.created, |
| 1012 |
requests.codes.accepted, |
| 1013 |
requests.codes.no_content, |
| 1014 |
), |
| 1015 |
) |
| 1016 |
@mock.patch("requests.post") |
| 1017 |
def test_plugin_vapid_success_codes(mock_post, tmpdir, status_code): |
| 1018 |
"""NotifyVapid() accepts its supported success responses.""" |
| 1019 |
|
| 1020 |
mock_post.return_value = _mk_resp(status_code) |
| 1021 |
|
| 1022 |
obj, _ = _mk_notifier( |
| 1023 |
tmpdir, |
| 1024 |
["https://fcm.googleapis.com/fcm/send/abc123"], |
| 1025 |
["abc123"], |
| 1026 |
name=f"ok{status_code}", |
| 1027 |
) |
| 1028 |
|
| 1029 |
# Supported services may acknowledge queued messages with 201 or 202. |
| 1030 |
assert obj.send("test") is True |
| 1031 |
|
| 1032 |
|
| 1033 |
@pytest.mark.skipif( |
| 1034 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1035 |
) |
| 1036 |
@mock.patch("requests.post") |
| 1037 |
def test_plugin_vapid_prunes_expired_subscriptions(mock_post, tmpdir): |
| 1038 |
"""A 404 or a 410 from the push service removes that subscription.""" |
| 1039 |
|
| 1040 |
def respond(url, *args, **kwargs): |
| 1041 |
# Endpoint names select the expired response returned by this mock. |
| 1042 |
return _mk_resp( |
| 1043 |
requests.codes.gone |
| 1044 |
if url.endswith("GONE") |
| 1045 |
else ( |
| 1046 |
requests.codes.not_found |
| 1047 |
if url.endswith("MISSING") |
| 1048 |
else requests.codes.created |
| 1049 |
) |
| 1050 |
) |
| 1051 |
|
| 1052 |
mock_post.side_effect = respond |
| 1053 |
|
| 1054 |
obj, subfile = _mk_notifier( |
| 1055 |
tmpdir, |
| 1056 |
{ |
| 1057 |
"live": "https://web.push.apple.com/LIVE", |
| 1058 |
"gone": "https://web.push.apple.com/GONE", |
| 1059 |
"missing": "https://web.push.apple.com/MISSING", |
| 1060 |
}, |
| 1061 |
["live", "gone", "missing"], |
| 1062 |
name="prune", |
| 1063 |
) |
| 1064 |
|
| 1065 |
# An expired endpoint is reported honestly as a failure, even though |
| 1066 |
# another target was accepted. |
| 1067 |
assert obj.send("test") is False |
| 1068 |
|
| 1069 |
# Remove only the expired subscriptions from disk. |
| 1070 |
with open(subfile) as f: |
| 1071 |
remaining = json.load(f) |
| 1072 |
|
| 1073 |
assert set(remaining) == {"live"} |
| 1074 |
assert "gone" not in obj.subscriptions |
| 1075 |
assert "missing" not in obj.subscriptions |
| 1076 |
|
| 1077 |
|
| 1078 |
@pytest.mark.skipif( |
| 1079 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1080 |
) |
| 1081 |
@mock.patch("requests.post") |
| 1082 |
def test_plugin_vapid_prune_write_failure(mock_post, tmpdir): |
| 1083 |
"""A subscription file that cannot be updated does not break sending.""" |
| 1084 |
|
| 1085 |
mock_post.return_value = _mk_resp(requests.codes.gone) |
| 1086 |
|
| 1087 |
obj, subfile = _mk_notifier( |
| 1088 |
tmpdir, |
| 1089 |
["https://web.push.apple.com/DEAD"], |
| 1090 |
["dead"], |
| 1091 |
name="writefail", |
| 1092 |
) |
| 1093 |
|
| 1094 |
with mock.patch.object( |
| 1095 |
WebPushSubscriptionManager, "prune", return_value=False |
| 1096 |
): |
| 1097 |
assert obj.send("test") is False |
| 1098 |
|
| 1099 |
# Remove the expired subscription from memory despite the write failure. |
| 1100 |
assert "dead" not in obj.subscriptions |
| 1101 |
|
| 1102 |
# The unchanged file still contains the expired subscription. |
| 1103 |
with open(subfile) as f: |
| 1104 |
assert "dead" in json.load(f) |
| 1105 |
|
| 1106 |
|
| 1107 |
@pytest.mark.skipif( |
| 1108 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1109 |
) |
| 1110 |
@mock.patch("requests.post") |
| 1111 |
def test_plugin_vapid_expired_after_subfile_clear(mock_post, tmpdir): |
| 1112 |
"""An expired subscription is dropped after clearing ``subfile``.""" |
| 1113 |
|
| 1114 |
mock_post.return_value = _mk_resp(requests.codes.gone) |
| 1115 |
|
| 1116 |
obj, subfile = _mk_notifier( |
| 1117 |
tmpdir, |
| 1118 |
["https://web.push.apple.com/DEAD"], |
| 1119 |
["dead"], |
| 1120 |
name="nosubfile", |
| 1121 |
) |
| 1122 |
|
| 1123 |
# Load subscriptions, then clear the notifier's file reference |
| 1124 |
assert obj.subscriptions.load(subfile) is True |
| 1125 |
obj.subfile = None |
| 1126 |
|
| 1127 |
assert obj.send("test") is False |
| 1128 |
|
| 1129 |
# The expired subscription is still dropped from memory. |
| 1130 |
assert "dead" not in obj.subscriptions |
| 1131 |
|
| 1132 |
|
| 1133 |
@pytest.mark.skipif( |
| 1134 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1135 |
) |
| 1136 |
def test_plugin_vapid_subscription_remove(): |
| 1137 |
"""WebPushSubscriptionManager() reports whether it removed anything.""" |
| 1138 |
|
| 1139 |
smgr = WebPushSubscriptionManager() |
| 1140 |
|
| 1141 |
# Nothing can be removed from an empty manager. |
| 1142 |
assert smgr.remove("missing") is False |
| 1143 |
|
| 1144 |
assert smgr.add(_subscription("https://web.push.apple.com/abc123")) is True |
| 1145 |
assert len(smgr) == 1 |
| 1146 |
|
| 1147 |
# Match subscription names without regard to case. |
| 1148 |
assert smgr.remove("ABC123") is True |
| 1149 |
assert len(smgr) == 0 |
| 1150 |
assert smgr.remove("abc123") is False |
| 1151 |
|
| 1152 |
|
| 1153 |
@pytest.mark.skipif( |
| 1154 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1155 |
) |
| 1156 |
@pytest.mark.parametrize( |
| 1157 |
("endpoint", "origin"), |
| 1158 |
( |
| 1159 |
# A standard endpoint keeps its origin. |
| 1160 |
( |
| 1161 |
"https://fcm.googleapis.com/fcm/send/abc123", |
| 1162 |
"https://fcm.googleapis.com", |
| 1163 |
), |
| 1164 |
# Lowercase the scheme and host, and omit port 443. |
| 1165 |
( |
| 1166 |
"HTTPS://FCM.GoogleAPIs.COM:443/fcm/send/abc123", |
| 1167 |
"https://fcm.googleapis.com", |
| 1168 |
), |
| 1169 |
# Ignore surrounding whitespace. |
| 1170 |
( |
| 1171 |
" https://fcm.googleapis.com/fcm/send/abc123 ", |
| 1172 |
"https://fcm.googleapis.com", |
| 1173 |
), |
| 1174 |
# Keep a non-default port in the origin. |
| 1175 |
( |
| 1176 |
"https://push.example.com:8443/wpush/v1/abc123", |
| 1177 |
"https://push.example.com:8443", |
| 1178 |
), |
| 1179 |
# Keep brackets around an IPv6 host. |
| 1180 |
("https://[2001:db8::1]/wpush/v1/abc", "https://[2001:db8::1]"), |
| 1181 |
( |
| 1182 |
"https://[2001:db8::1]:8443/wpush/v1/abc", |
| 1183 |
"https://[2001:db8::1]:8443", |
| 1184 |
), |
| 1185 |
# Allow a self-hosted service on a private address. |
| 1186 |
("https://192.168.0.10:8443/push/abc", "https://192.168.0.10:8443"), |
| 1187 |
), |
| 1188 |
) |
| 1189 |
def test_plugin_vapid_endpoint_origins(endpoint, origin): |
| 1190 |
"""A valid endpoint is accepted and reduced to its origin.""" |
| 1191 |
|
| 1192 |
assert webpush_origin(endpoint) == origin |
| 1193 |
|
| 1194 |
sub = WebPushSubscription(_subscription(endpoint)) |
| 1195 |
assert sub.origin == origin |
| 1196 |
assert sub.endpoint == endpoint.strip() |
| 1197 |
|
| 1198 |
|
| 1199 |
@pytest.mark.skipif( |
| 1200 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1201 |
) |
| 1202 |
@pytest.mark.parametrize( |
| 1203 |
"endpoint", |
| 1204 |
( |
| 1205 |
# Web Push requires HTTPS. |
| 1206 |
"http://fcm.googleapis.com/fcm/send/abc123", |
| 1207 |
"ftp://fcm.googleapis.com/fcm/send/abc123", |
| 1208 |
"fcm.googleapis.com/fcm/send/abc123", |
| 1209 |
# Reject embedded credentials, even empty ones. |
| 1210 |
"https://[email protected]/fcm/send/abc123", |
| 1211 |
"https://user:[email protected]/fcm/send/abc123", |
| 1212 |
"https://@fcm.googleapis.com/fcm/send/abc123", |
| 1213 |
"https://:@fcm.googleapis.com/fcm/send/abc123", |
| 1214 |
"https://:[email protected]/fcm/send/abc123", |
| 1215 |
# Reject endpoints without a host. |
| 1216 |
"https://", |
| 1217 |
"https:///fcm/send/abc123", |
| 1218 |
"://", |
| 1219 |
"", |
| 1220 |
"not a url", |
| 1221 |
# Reject malformed IPv6 without raising. |
| 1222 |
"https://[2001:db8::1/fcm/send/abc123", |
| 1223 |
# Reject an out-of-range port without raising. |
| 1224 |
"https://fcm.googleapis.com:99999/fcm/send/abc123", |
| 1225 |
# Reject a host holding characters that are not legal in one. |
| 1226 |
"https://fcm google apis.com/fcm/send/abc123", |
| 1227 |
"https://fcm%20googleapis.com/fcm/send/abc123", |
| 1228 |
"https://-fcm.googleapis.com/fcm/send/abc123", |
| 1229 |
"https://.fcm.googleapis.com/fcm/send/abc123", |
| 1230 |
f"https://{'a' * 254}.com/fcm/send/abc123", |
| 1231 |
# Reject an IPv6 literal that is not an address at all. |
| 1232 |
"https://[2001:db8::1::2]/fcm/send/abc123", |
| 1233 |
"https://[not:an:address]/fcm/send/abc123", |
| 1234 |
# Reject non-string values. |
| 1235 |
None, |
| 1236 |
42, |
| 1237 |
["https://fcm.googleapis.com/fcm/send/abc123"], |
| 1238 |
), |
| 1239 |
) |
| 1240 |
def test_plugin_vapid_bad_endpoints(endpoint): |
| 1241 |
"""An endpoint that cannot be used safely is rejected.""" |
| 1242 |
|
| 1243 |
assert webpush_origin(endpoint) is None |
| 1244 |
|
| 1245 |
# A subscription containing an invalid endpoint cannot be loaded. |
| 1246 |
with pytest.raises(exception.AppriseInvalidData): |
| 1247 |
WebPushSubscription(_subscription(endpoint)) |
| 1248 |
|
| 1249 |
sub = WebPushSubscription() |
| 1250 |
assert sub.load(_subscription(endpoint)) is False |
| 1251 |
assert sub.origin is None |
| 1252 |
assert sub.endpoint is None |
| 1253 |
assert bool(sub) is False |
| 1254 |
|
| 1255 |
# The manager also refuses the subscription. |
| 1256 |
smgr = WebPushSubscriptionManager() |
| 1257 |
assert smgr.add(_subscription(endpoint)) is False |
| 1258 |
assert len(smgr) == 0 |
| 1259 |
|
| 1260 |
|
| 1261 |
@pytest.mark.skipif( |
| 1262 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1263 |
) |
| 1264 |
def test_plugin_vapid_subscription_atomic_write(tmpdir): |
| 1265 |
"""A failed write leaves whatever was already on disk alone.""" |
| 1266 |
|
| 1267 |
tmpdir0 = tmpdir.mkdir("atomic") |
| 1268 |
path = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1269 |
|
| 1270 |
smgr = WebPushSubscriptionManager() |
| 1271 |
assert smgr.add(_subscription("https://web.push.apple.com/abc123")) is True |
| 1272 |
assert smgr.write(path) is True |
| 1273 |
|
| 1274 |
with open(path) as f: |
| 1275 |
original = json.load(f) |
| 1276 |
|
| 1277 |
assert set(original) == {"abc123"} |
| 1278 |
|
| 1279 |
# A partial write must not replace the existing file. |
| 1280 |
smgr.clear() |
| 1281 |
assert smgr.add(_subscription("https://web.push.apple.com/xyz789")) is True |
| 1282 |
with mock.patch("json.dump", side_effect=OSError): |
| 1283 |
assert smgr.write(path) is False |
| 1284 |
|
| 1285 |
with open(path) as f: |
| 1286 |
assert json.load(f) == original |
| 1287 |
|
| 1288 |
# Do not leave temporary files behind. |
| 1289 |
assert os.listdir(str(tmpdir0)) == ["subscriptions.json"] |
| 1290 |
|
| 1291 |
# Treat a missing destination directory as a failed write. |
| 1292 |
assert smgr.write(os.path.join(str(tmpdir0), "missing", "s.json")) is False |
| 1293 |
|
| 1294 |
|
| 1295 |
@pytest.mark.skipif( |
| 1296 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1297 |
) |
| 1298 |
@pytest.mark.skipif(IS_WINDOWS, reason="Requires POSIX file modes") |
| 1299 |
def test_plugin_vapid_write_keeps_permissions(tmpdir): |
| 1300 |
"""An existing subscription file keeps the permissions it had.""" |
| 1301 |
|
| 1302 |
tmpdir0 = tmpdir.mkdir("perms") |
| 1303 |
path = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1304 |
|
| 1305 |
smgr = WebPushSubscriptionManager() |
| 1306 |
assert smgr.add(_subscription("https://web.push.apple.com/abc123")) is True |
| 1307 |
assert smgr.write(path) is True |
| 1308 |
|
| 1309 |
# A brand new file is kept to ourselves; it lists where we deliver |
| 1310 |
assert stat.S_IMODE(os.stat(path).st_mode) == 0o600 |
| 1311 |
|
| 1312 |
# Hand the file a mode of its own and rewrite it |
| 1313 |
os.chmod(path, 0o640) |
| 1314 |
assert smgr.add(_subscription("https://web.push.apple.com/xyz789")) is True |
| 1315 |
assert smgr.write(path) is True |
| 1316 |
|
| 1317 |
assert stat.S_IMODE(os.stat(path).st_mode) == 0o640 |
| 1318 |
|
| 1319 |
# Confirm the new entry was written. |
| 1320 |
with open(path) as f: |
| 1321 |
assert set(json.load(f)) == {"abc123", "xyz789"} |
| 1322 |
|
| 1323 |
|
| 1324 |
@pytest.mark.skipif( |
| 1325 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1326 |
) |
| 1327 |
def test_plugin_vapid_write_follows_symlink(tmpdir): |
| 1328 |
"""A symlinked subscription file is written through, not replaced.""" |
| 1329 |
|
| 1330 |
tmpdir0 = tmpdir.mkdir("symlink") |
| 1331 |
target = os.path.join(str(tmpdir0), "real.json") |
| 1332 |
link = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1333 |
|
| 1334 |
smgr = WebPushSubscriptionManager() |
| 1335 |
assert smgr.add(_subscription("https://web.push.apple.com/abc123")) is True |
| 1336 |
assert smgr.write(target) is True |
| 1337 |
os.symlink(target, link) |
| 1338 |
|
| 1339 |
# Write by way of the link |
| 1340 |
assert smgr.add(_subscription("https://web.push.apple.com/xyz789")) is True |
| 1341 |
assert smgr.write(link) is True |
| 1342 |
|
| 1343 |
# Our link survived and the file it points at was the one updated |
| 1344 |
assert os.path.islink(link) is True |
| 1345 |
assert os.path.realpath(link) == target |
| 1346 |
|
| 1347 |
with open(target) as f: |
| 1348 |
assert set(json.load(f)) == {"abc123", "xyz789"} |
| 1349 |
|
| 1350 |
# No temporary files are left beside either path |
| 1351 |
assert sorted(os.listdir(str(tmpdir0))) == [ |
| 1352 |
"real.json", |
| 1353 |
"subscriptions.json", |
| 1354 |
] |
| 1355 |
|
| 1356 |
|
| 1357 |
@pytest.mark.skipif( |
| 1358 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1359 |
) |
| 1360 |
def test_plugin_vapid_bad_subfile_source(tmpdir): |
| 1361 |
"""An unavailable or invalid subscription source is refused.""" |
| 1362 |
|
| 1363 |
smgr = WebPushSubscriptionManager() |
| 1364 |
|
| 1365 |
# A remote source that cannot be fetched. The download is mocked so the |
| 1366 |
# test does not depend on anything outside this machine. |
| 1367 |
with mock.patch("requests.get") as mock_get: |
| 1368 |
mock_get.return_value.status_code = ( |
| 1369 |
requests.codes.internal_server_error |
| 1370 |
) |
| 1371 |
mock_get.return_value.headers = {} |
| 1372 |
mock_get.return_value.raise_for_status.side_effect = ( |
| 1373 |
requests.RequestException |
| 1374 |
) |
| 1375 |
mock_get.return_value.__enter__ = lambda s: s |
| 1376 |
mock_get.return_value.__exit__ = lambda s, *a, **kw: None |
| 1377 |
|
| 1378 |
assert smgr.load("https://example.com/subscriptions.json") is False |
| 1379 |
|
| 1380 |
assert len(smgr) == 0 |
| 1381 |
|
| 1382 |
# Reject an empty path as well. |
| 1383 |
assert smgr.load("") is False |
| 1384 |
assert len(smgr) == 0 |
| 1385 |
|
| 1386 |
# A valid local file still loads normally. |
| 1387 |
tmpdir0 = tmpdir.mkdir("local") |
| 1388 |
path = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1389 |
_write_subscriptions(path, ["https://web.push.apple.com/abc123"]) |
| 1390 |
|
| 1391 |
assert smgr.load(path) is True |
| 1392 |
assert len(smgr) == 1 |
| 1393 |
|
| 1394 |
|
| 1395 |
@pytest.mark.skipif( |
| 1396 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1397 |
) |
| 1398 |
def test_plugin_vapid_subfile_not_in_url(tmpdir): |
| 1399 |
"""A subscription path we worked out ourselves stays out of the URL.""" |
| 1400 |
|
| 1401 |
tmpdir0 = tmpdir.mkdir("portable") |
| 1402 |
asset_ = asset.AppriseAsset( |
| 1403 |
storage_mode=PersistentStoreMode.FLUSH, |
| 1404 |
storage_path=str(tmpdir0), |
| 1405 |
pem_autogen=True, |
| 1406 |
) |
| 1407 |
|
| 1408 |
obj = Apprise.instantiate("vapid://[email protected]/abc123", asset=asset_) |
| 1409 |
|
| 1410 |
# The file lives in this URL's own storage directory |
| 1411 |
assert obj.subfile.startswith(obj.store.path) |
| 1412 |
assert obj.subfile_specified is False |
| 1413 |
|
| 1414 |
# The URL omits local details so it can move to another installation. |
| 1415 |
assert "subfile" not in obj.url() |
| 1416 |
assert str(tmpdir0) not in obj.url() |
| 1417 |
|
| 1418 |
|
| 1419 |
@pytest.mark.skipif( |
| 1420 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1421 |
) |
| 1422 |
def test_plugin_vapid_subfile_in_url_when_asked(tmpdir): |
| 1423 |
"""A subscription path given to us is kept in the URL.""" |
| 1424 |
|
| 1425 |
tmpdir0 = tmpdir.mkdir("explicit") |
| 1426 |
subfile = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1427 |
_write_subscriptions(subfile, ["https://web.push.apple.com/abc123"]) |
| 1428 |
|
| 1429 |
obj = Apprise.instantiate( |
| 1430 |
f"vapid://[email protected]/abc123?subfile={subfile}", |
| 1431 |
asset=asset.AppriseAsset( |
| 1432 |
storage_mode=PersistentStoreMode.FLUSH, |
| 1433 |
storage_path=str(tmpdir0), |
| 1434 |
pem_autogen=True, |
| 1435 |
), |
| 1436 |
) |
| 1437 |
|
| 1438 |
assert obj.subfile == subfile |
| 1439 |
assert obj.subfile_specified is True |
| 1440 |
|
| 1441 |
# It round-trips, so a path someone chose is not lost |
| 1442 |
assert "subfile" in obj.url() |
| 1443 |
|
| 1444 |
reloaded = Apprise.instantiate(obj.url()) |
| 1445 |
assert reloaded.subfile == subfile |
| 1446 |
|
| 1447 |
|
| 1448 |
@pytest.mark.skipif( |
| 1449 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1450 |
) |
| 1451 |
def test_plugin_vapid_url_moves_between_servers(tmpdir): |
| 1452 |
"""The same URL resolves to each machine's own storage directory.""" |
| 1453 |
|
| 1454 |
rootA = tmpdir.mkdir("serverA") |
| 1455 |
rootB = tmpdir.mkdir("serverB") |
| 1456 |
|
| 1457 |
def _server(root): |
| 1458 |
return Apprise.instantiate( |
| 1459 |
"vapid://[email protected]/abc123", |
| 1460 |
asset=asset.AppriseAsset( |
| 1461 |
storage_mode=PersistentStoreMode.FLUSH, |
| 1462 |
storage_path=str(root), |
| 1463 |
pem_autogen=True, |
| 1464 |
), |
| 1465 |
) |
| 1466 |
|
| 1467 |
objA = _server(rootA) |
| 1468 |
objB = Apprise.instantiate( |
| 1469 |
objA.url(), |
| 1470 |
asset=asset.AppriseAsset( |
| 1471 |
storage_mode=PersistentStoreMode.FLUSH, |
| 1472 |
storage_path=str(rootB), |
| 1473 |
pem_autogen=True, |
| 1474 |
), |
| 1475 |
) |
| 1476 |
|
| 1477 |
# Both land on the same storage id, since it is worked out from the URL |
| 1478 |
# and not from anything local to one machine |
| 1479 |
assert objA.url_id() == objB.url_id() |
| 1480 |
|
| 1481 |
# Each instance reads from its own storage directory. |
| 1482 |
assert objA.subfile.startswith(str(rootA)) |
| 1483 |
assert objB.subfile.startswith(str(rootB)) |
| 1484 |
|
| 1485 |
|
| 1486 |
@pytest.mark.skipif( |
| 1487 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1488 |
) |
| 1489 |
def test_plugin_vapid_prune_keeps_other_entries(tmpdir): |
| 1490 |
"""Pruning removes only what it was asked to.""" |
| 1491 |
|
| 1492 |
tmpdir0 = tmpdir.mkdir("keep") |
| 1493 |
path = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1494 |
|
| 1495 |
# Mix valid entries with a typo and an entry missing its keys. |
| 1496 |
original = { |
| 1497 |
"good": _subscription("https://fcm.googleapis.com/fcm/send/GOOD"), |
| 1498 |
"dead": _subscription("https://fcm.googleapis.com/fcm/send/DEAD"), |
| 1499 |
"httponly": _subscription("http://legacy.example.com/push/OLD"), |
| 1500 |
"typo": _subscription("https://fcm.googleapis.com /fcm/send/X"), |
| 1501 |
"handedit": {"endpoint": "https://fcm.googleapis.com/fcm/send/Y"}, |
| 1502 |
} |
| 1503 |
with open(path, "w") as f: |
| 1504 |
json.dump(original, f) |
| 1505 |
|
| 1506 |
smgr = WebPushSubscriptionManager() |
| 1507 |
assert smgr.load(path) is True |
| 1508 |
|
| 1509 |
# Only the two usable entries were loaded |
| 1510 |
assert set(smgr.dict) == {"good", "dead"} |
| 1511 |
assert smgr.writable is True |
| 1512 |
|
| 1513 |
assert smgr.prune(["dead"]) is True |
| 1514 |
|
| 1515 |
with open(path) as f: |
| 1516 |
remaining = json.load(f) |
| 1517 |
|
| 1518 |
# The expired entry is gone and nothing else was touched, so somebody's |
| 1519 |
# typo is still theirs to fix rather than quietly deleted |
| 1520 |
assert set(remaining) == {"good", "httponly", "typo", "handedit"} |
| 1521 |
assert remaining["typo"] == original["typo"] |
| 1522 |
assert remaining["handedit"] == original["handedit"] |
| 1523 |
|
| 1524 |
|
| 1525 |
@pytest.mark.skipif( |
| 1526 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1527 |
) |
| 1528 |
def test_plugin_vapid_prune_reloads_file(tmpdir): |
| 1529 |
"""Each prune uses the latest file instead of stale loaded state.""" |
| 1530 |
|
| 1531 |
tmpdir0 = tmpdir.mkdir("concurrent") |
| 1532 |
path = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1533 |
_write_subscriptions( |
| 1534 |
path, |
| 1535 |
{ |
| 1536 |
n: f"https://fcm.googleapis.com/fcm/send/{n}" |
| 1537 |
for n in ("a", "b", "c", "d") |
| 1538 |
}, |
| 1539 |
) |
| 1540 |
|
| 1541 |
# Both instances begin with the same snapshot. |
| 1542 |
first = WebPushSubscriptionManager() |
| 1543 |
second = WebPushSubscriptionManager() |
| 1544 |
assert first.load(path) is True |
| 1545 |
assert second.load(path) is True |
| 1546 |
|
| 1547 |
assert first.prune(["a"]) is True |
| 1548 |
assert second.prune(["c"]) is True |
| 1549 |
|
| 1550 |
with open(path) as f: |
| 1551 |
remaining = json.load(f) |
| 1552 |
|
| 1553 |
# The second prune reloads the first one's change before writing. |
| 1554 |
assert set(remaining) == {"b", "d"} |
| 1555 |
|
| 1556 |
|
| 1557 |
@pytest.mark.skipif( |
| 1558 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1559 |
) |
| 1560 |
@pytest.mark.skipif(IS_ROOT, reason="Root can write read-only files") |
| 1561 |
def test_plugin_vapid_readonly_file_respected(tmpdir): |
| 1562 |
"""A read-only subscription file is left alone.""" |
| 1563 |
|
| 1564 |
tmpdir0 = tmpdir.mkdir("readonly") |
| 1565 |
path = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1566 |
_write_subscriptions(path, ["https://fcm.googleapis.com/fcm/send/X"]) |
| 1567 |
os.chmod(path, 0o444) |
| 1568 |
|
| 1569 |
smgr = WebPushSubscriptionManager() |
| 1570 |
assert smgr.load(path) is True |
| 1571 |
assert smgr.local_file == path |
| 1572 |
|
| 1573 |
# We can read it, but it is not ours to change |
| 1574 |
assert smgr.writable is False |
| 1575 |
|
| 1576 |
# Swapping a file into place only needs permission on the directory, so |
| 1577 |
# prune() has to refuse on its own rather than rely on the caller |
| 1578 |
assert smgr.prune(["x"]) is False |
| 1579 |
|
| 1580 |
with open(path) as f: |
| 1581 |
assert set(json.load(f)) == {"x"} |
| 1582 |
|
| 1583 |
|
| 1584 |
@pytest.mark.skipif( |
| 1585 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1586 |
) |
| 1587 |
@pytest.mark.skipif(IS_ROOT, reason="Root can write read-only files") |
| 1588 |
@mock.patch("requests.post") |
| 1589 |
def test_plugin_vapid_retires_when_not_writable(mock_post, tmpdir): |
| 1590 |
"""An expired endpoint is tracked when its file cannot be updated.""" |
| 1591 |
|
| 1592 |
mock_post.return_value = _mk_resp(requests.codes.gone) |
| 1593 |
|
| 1594 |
tmpdir0 = tmpdir.mkdir("retire") |
| 1595 |
subfile = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1596 |
_write_subscriptions(subfile, ["https://web.push.apple.com/DEAD"]) |
| 1597 |
os.chmod(subfile, 0o444) |
| 1598 |
|
| 1599 |
asset_ = asset.AppriseAsset( |
| 1600 |
storage_mode=PersistentStoreMode.FLUSH, |
| 1601 |
storage_path=str(tmpdir0), |
| 1602 |
pem_autogen=True, |
| 1603 |
) |
| 1604 |
|
| 1605 |
obj = NotifyVapid( |
| 1606 |
"[email protected]", |
| 1607 |
targets=["dead"], |
| 1608 |
subfile=subfile, |
| 1609 |
asset=asset_, |
| 1610 |
) |
| 1611 |
assert obj.send("test") is False |
| 1612 |
|
| 1613 |
# The file still holds it, because it was not ours to rewrite |
| 1614 |
with open(subfile) as f: |
| 1615 |
assert set(json.load(f)) == {"dead"} |
| 1616 |
|
| 1617 |
# Remember the endpoint in persistent storage instead. |
| 1618 |
assert obj.store.get(obj.vapid_retired_key) == [ |
| 1619 |
"https://web.push.apple.com/DEAD" |
| 1620 |
] |
| 1621 |
|
| 1622 |
# A fresh instance skips it and does not report a false success. |
| 1623 |
mock_post.reset_mock() |
| 1624 |
obj2 = NotifyVapid( |
| 1625 |
"[email protected]", |
| 1626 |
targets=["dead"], |
| 1627 |
subfile=subfile, |
| 1628 |
asset=asset_, |
| 1629 |
) |
| 1630 |
assert obj2.send("test") is False |
| 1631 |
assert mock_post.call_count == 0 |
| 1632 |
|
| 1633 |
|
| 1634 |
@pytest.mark.skipif( |
| 1635 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1636 |
) |
| 1637 |
def test_plugin_vapid_prune_edge_cases(tmpdir): |
| 1638 |
"""Pruning declines anything it cannot safely rewrite.""" |
| 1639 |
|
| 1640 |
tmpdir0 = tmpdir.mkdir("edges") |
| 1641 |
|
| 1642 |
# Nothing was loaded, so there is no file to update |
| 1643 |
smgr = WebPushSubscriptionManager() |
| 1644 |
assert smgr.local_file is None |
| 1645 |
assert smgr.writable is False |
| 1646 |
assert smgr.prune(["anything"]) is False |
| 1647 |
|
| 1648 |
# A single entry file carries no names, so there is nothing to take out |
| 1649 |
single = os.path.join(str(tmpdir0), "single.json") |
| 1650 |
with open(single, "w") as f: |
| 1651 |
json.dump( |
| 1652 |
_subscription("https://fcm.googleapis.com/fcm/send/abc123"), f |
| 1653 |
) |
| 1654 |
|
| 1655 |
smgr = WebPushSubscriptionManager() |
| 1656 |
assert smgr.load(single) is True |
| 1657 |
assert smgr.prune(["abc123"]) is False |
| 1658 |
|
| 1659 |
# A name we do not hold leaves the file exactly as it was |
| 1660 |
many = os.path.join(str(tmpdir0), "many.json") |
| 1661 |
_write_subscriptions(many, ["https://fcm.googleapis.com/fcm/send/abc123"]) |
| 1662 |
|
| 1663 |
smgr = WebPushSubscriptionManager() |
| 1664 |
assert smgr.load(many) is True |
| 1665 |
assert smgr.prune(["nothing-like-this"]) is True |
| 1666 |
|
| 1667 |
with open(many) as f: |
| 1668 |
assert set(json.load(f)) == {"abc123"} |
| 1669 |
|
| 1670 |
# Content that is no longer valid JSON is left for its owner to fix |
| 1671 |
with open(many, "w") as f: |
| 1672 |
f.write("{ this is not json") |
| 1673 |
|
| 1674 |
assert smgr.prune(["abc123"]) is False |
| 1675 |
|
| 1676 |
with open(many) as f: |
| 1677 |
assert f.read() == "{ this is not json" |
| 1678 |
|
| 1679 |
# Also reject a file that does not contain an object. |
| 1680 |
with open(many, "w") as f: |
| 1681 |
json.dump(["not", "an", "object"], f) |
| 1682 |
|
| 1683 |
assert smgr.prune(["abc123"]) is False |
| 1684 |
|
| 1685 |
# A write that fails part way leaves the file as it was |
| 1686 |
_write_subscriptions(many, ["https://fcm.googleapis.com/fcm/send/abc123"]) |
| 1687 |
assert smgr.load(many) is True |
| 1688 |
|
| 1689 |
with mock.patch("json.dump", side_effect=OSError): |
| 1690 |
assert smgr.prune(["abc123"]) is False |
| 1691 |
|
| 1692 |
with open(many) as f: |
| 1693 |
assert set(json.load(f)) == {"abc123"} |
| 1694 |
|
| 1695 |
|
| 1696 |
@pytest.mark.skipif( |
| 1697 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1698 |
) |
| 1699 |
def test_plugin_vapid_remote_subfile(tmpdir): |
| 1700 |
"""A remotely hosted subscription file loads as read-only.""" |
| 1701 |
|
| 1702 |
tmpdir0 = tmpdir.mkdir("remote") |
| 1703 |
local = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1704 |
_write_subscriptions(local, ["https://fcm.googleapis.com/fcm/send/abc123"]) |
| 1705 |
|
| 1706 |
with open(local, "rb") as f: |
| 1707 |
payload = f.read() |
| 1708 |
|
| 1709 |
smgr = WebPushSubscriptionManager() |
| 1710 |
|
| 1711 |
# Stand in for the download an AppriseAttachment would perform |
| 1712 |
with mock.patch("requests.get") as mock_get: |
| 1713 |
mock_get.return_value.status_code = requests.codes.ok |
| 1714 |
mock_get.return_value.headers = {"Content-Length": len(payload)} |
| 1715 |
mock_get.return_value.iter_content.return_value = iter([payload]) |
| 1716 |
mock_get.return_value.raise_for_status.return_value = None |
| 1717 |
mock_get.return_value.__enter__ = lambda s: s |
| 1718 |
mock_get.return_value.__exit__ = lambda s, *a, **kw: None |
| 1719 |
|
| 1720 |
assert smgr.load("https://example.com/subscriptions.json") is True |
| 1721 |
|
| 1722 |
# The subscription came through |
| 1723 |
assert set(smgr.dict) == {"abc123"} |
| 1724 |
|
| 1725 |
# With no local source file, it cannot be rewritten. |
| 1726 |
assert smgr.local_file is None |
| 1727 |
assert smgr.writable is False |
| 1728 |
assert smgr.prune(["abc123"]) is False |
| 1729 |
|
| 1730 |
|
| 1731 |
@pytest.mark.skipif( |
| 1732 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1733 |
) |
| 1734 |
@mock.patch("requests.post") |
| 1735 |
def test_plugin_vapid_retires_when_prune_fails(mock_post, tmpdir): |
| 1736 |
"""A pruning failure still stops the endpoint being tried again.""" |
| 1737 |
|
| 1738 |
mock_post.return_value = _mk_resp(requests.codes.gone) |
| 1739 |
|
| 1740 |
tmpdir0 = tmpdir.mkdir("prunefail") |
| 1741 |
subfile = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1742 |
_write_subscriptions(subfile, ["https://web.push.apple.com/DEAD"]) |
| 1743 |
|
| 1744 |
asset_ = asset.AppriseAsset( |
| 1745 |
storage_mode=PersistentStoreMode.FLUSH, |
| 1746 |
storage_path=str(tmpdir0), |
| 1747 |
pem_autogen=True, |
| 1748 |
) |
| 1749 |
|
| 1750 |
obj = NotifyVapid( |
| 1751 |
"[email protected]", |
| 1752 |
targets=["dead"], |
| 1753 |
subfile=subfile, |
| 1754 |
asset=asset_, |
| 1755 |
) |
| 1756 |
|
| 1757 |
# The file is writable, but rewriting it does not work out |
| 1758 |
with mock.patch.object( |
| 1759 |
WebPushSubscriptionManager, "prune", return_value=False |
| 1760 |
): |
| 1761 |
assert obj.send("test") is False |
| 1762 |
|
| 1763 |
# The file still lists it, since the rewrite failed |
| 1764 |
with open(subfile) as f: |
| 1765 |
assert set(json.load(f)) == {"dead"} |
| 1766 |
|
| 1767 |
# ...so the endpoint was remembered instead and is not asked for again |
| 1768 |
assert obj.store.get(obj.vapid_retired_key) == [ |
| 1769 |
"https://web.push.apple.com/DEAD" |
| 1770 |
] |
| 1771 |
|
| 1772 |
mock_post.reset_mock() |
| 1773 |
obj2 = NotifyVapid( |
| 1774 |
"[email protected]", |
| 1775 |
targets=["dead"], |
| 1776 |
subfile=subfile, |
| 1777 |
asset=asset_, |
| 1778 |
) |
| 1779 |
assert obj2.send("test") is False |
| 1780 |
assert mock_post.call_count == 0 |
| 1781 |
|
| 1782 |
|
| 1783 |
@pytest.mark.skipif( |
| 1784 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1785 |
) |
| 1786 |
def test_plugin_vapid_load_forgets_previous_path(tmpdir): |
| 1787 |
"""A later load does not leave an earlier file behind to be pruned.""" |
| 1788 |
|
| 1789 |
tmpdir0 = tmpdir.mkdir("stale") |
| 1790 |
first = os.path.join(str(tmpdir0), "first.json") |
| 1791 |
_write_subscriptions(first, ["https://fcm.googleapis.com/fcm/send/abc123"]) |
| 1792 |
|
| 1793 |
smgr = WebPushSubscriptionManager() |
| 1794 |
assert smgr.load(first) is True |
| 1795 |
assert smgr.local_file == first |
| 1796 |
|
| 1797 |
# A load that fails must not leave the previous file remembered, |
| 1798 |
# otherwise a prune would rewrite a file we are no longer using |
| 1799 |
assert smgr.load(os.path.join(str(tmpdir0), "missing.json")) is False |
| 1800 |
assert smgr.local_file is None |
| 1801 |
assert smgr.writable is False |
| 1802 |
assert smgr.prune(["abc123"]) is False |
| 1803 |
|
| 1804 |
# The first file is untouched |
| 1805 |
with open(first) as f: |
| 1806 |
assert set(json.load(f)) == {"abc123"} |
| 1807 |
|
| 1808 |
|
| 1809 |
@pytest.mark.skipif( |
| 1810 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1811 |
) |
| 1812 |
def test_plugin_vapid_logging_masks_secrets(tmpdir): |
| 1813 |
"""Endpoints and remote credentials do not reach the logs intact.""" |
| 1814 |
|
| 1815 |
secret = "cH1s-Is-A-SeCr3t-RegIstratIon-Id" |
| 1816 |
|
| 1817 |
# An endpoint token is sensitive and must not appear in logs. |
| 1818 |
masked = cwe312_loggable(f"https://fcm.googleapis.com/fcm/send/{secret}") |
| 1819 |
assert secret not in masked |
| 1820 |
|
| 1821 |
# A missing value is reported rather than blowing up |
| 1822 |
assert cwe312_loggable(None) == "(none)" |
| 1823 |
assert cwe312_loggable("") == "(none)" |
| 1824 |
|
| 1825 |
# Keep local paths readable for troubleshooting. |
| 1826 |
assert ( |
| 1827 |
cwe312_loggable("/etc/apprise/subscriptions.json") |
| 1828 |
== "/etc/apprise/subscriptions.json" |
| 1829 |
) |
| 1830 |
|
| 1831 |
smgr = WebPushSubscriptionManager() |
| 1832 |
|
| 1833 |
# A remote subscription file can carry a password |
| 1834 |
loggable = smgr.loggable_path( |
| 1835 |
"https://user:[email protected]/subscriptions.json" |
| 1836 |
) |
| 1837 |
assert "pass123" not in loggable |
| 1838 |
|
| 1839 |
assert smgr.loggable_path(None) == "(none)" |
| 1840 |
|
| 1841 |
# Somebody who turns masking off gets the value through untouched |
| 1842 |
smgr_plain = WebPushSubscriptionManager( |
| 1843 |
asset=asset.AppriseAsset(secure_logging=False) |
| 1844 |
) |
| 1845 |
url = "https://user:[email protected]/subscriptions.json" |
| 1846 |
assert smgr_plain.loggable_path(url) == url |
| 1847 |
|
| 1848 |
|
| 1849 |
@pytest.mark.skipif( |
| 1850 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1851 |
) |
| 1852 |
def test_plugin_vapid_load_read_errors(tmpdir): |
| 1853 |
"""Loading tells a missing file, bad JSON and a disk problem apart.""" |
| 1854 |
|
| 1855 |
tmpdir0 = tmpdir.mkdir("loaderrs") |
| 1856 |
path = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1857 |
_write_subscriptions(path, ["https://fcm.googleapis.com/fcm/send/abc123"]) |
| 1858 |
|
| 1859 |
smgr = WebPushSubscriptionManager() |
| 1860 |
|
| 1861 |
# The file goes away between being resolved and being opened |
| 1862 |
with mock.patch("builtins.open", side_effect=FileNotFoundError): |
| 1863 |
assert smgr.load(path) is False |
| 1864 |
|
| 1865 |
# It is there, but it does not hold JSON |
| 1866 |
with mock.patch( |
| 1867 |
"json.load", side_effect=json.decoder.JSONDecodeError("bad", "", 0) |
| 1868 |
): |
| 1869 |
assert smgr.load(path) is False |
| 1870 |
|
| 1871 |
# It is there and we are not allowed to read it |
| 1872 |
with mock.patch("builtins.open", side_effect=OSError): |
| 1873 |
assert smgr.load(path) is False |
| 1874 |
|
| 1875 |
# ...and it still loads when nothing is wrong |
| 1876 |
assert smgr.load(path) is True |
| 1877 |
|
| 1878 |
|
| 1879 |
@pytest.mark.skipif( |
| 1880 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1881 |
) |
| 1882 |
def test_plugin_vapid_prune_read_errors(tmpdir): |
| 1883 |
"""Pruning tells a missing file, bad JSON and a disk problem apart.""" |
| 1884 |
|
| 1885 |
tmpdir0 = tmpdir.mkdir("pruneerrs") |
| 1886 |
path = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1887 |
_write_subscriptions(path, ["https://fcm.googleapis.com/fcm/send/abc123"]) |
| 1888 |
|
| 1889 |
smgr = WebPushSubscriptionManager() |
| 1890 |
assert smgr.load(path) is True |
| 1891 |
|
| 1892 |
# The file goes away before we can rewrite it |
| 1893 |
with mock.patch("builtins.open", side_effect=FileNotFoundError): |
| 1894 |
assert smgr.prune(["abc123"]) is False |
| 1895 |
|
| 1896 |
# Leave newly invalid content untouched rather than overwriting it. |
| 1897 |
with mock.patch( |
| 1898 |
"json.load", side_effect=json.decoder.JSONDecodeError("bad", "", 0) |
| 1899 |
): |
| 1900 |
assert smgr.prune(["abc123"]) is False |
| 1901 |
|
| 1902 |
# We are no longer allowed to read it |
| 1903 |
with mock.patch("builtins.open", side_effect=OSError): |
| 1904 |
assert smgr.prune(["abc123"]) is False |
| 1905 |
|
| 1906 |
# The file is untouched throughout |
| 1907 |
with open(path) as f: |
| 1908 |
assert set(json.load(f)) == {"abc123"} |
| 1909 |
|
| 1910 |
|
| 1911 |
@pytest.mark.skipif( |
| 1912 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1913 |
) |
| 1914 |
def test_plugin_vapid_write_errors(tmpdir): |
| 1915 |
"""Writing tells a content problem apart from a disk problem.""" |
| 1916 |
|
| 1917 |
tmpdir0 = tmpdir.mkdir("writeerrs") |
| 1918 |
path = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1919 |
|
| 1920 |
smgr = WebPushSubscriptionManager() |
| 1921 |
assert smgr.add(_subscription("https://web.push.apple.com/abc123")) is True |
| 1922 |
|
| 1923 |
# Content that will not turn into JSON |
| 1924 |
with mock.patch("json.dump", side_effect=TypeError): |
| 1925 |
assert smgr.write(path) is False |
| 1926 |
|
| 1927 |
# A circular reference raises ValueError rather than TypeError |
| 1928 |
with mock.patch("json.dump", side_effect=ValueError): |
| 1929 |
assert smgr.write(path) is False |
| 1930 |
|
| 1931 |
# ...and a disk problem is reported as one |
| 1932 |
with mock.patch("json.dump", side_effect=OSError): |
| 1933 |
assert smgr.write(path) is False |
| 1934 |
|
| 1935 |
# Nothing was left behind by any of those |
| 1936 |
assert os.listdir(str(tmpdir0)) == [] |
| 1937 |
|
| 1938 |
assert smgr.write(path) is True |
| 1939 |
|
| 1940 |
|
| 1941 |
@pytest.mark.skipif( |
| 1942 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1943 |
) |
| 1944 |
def test_plugin_vapid_write_unreadable_mode(tmpdir): |
| 1945 |
"""A file whose mode cannot be read is still replaced.""" |
| 1946 |
|
| 1947 |
tmpdir0 = tmpdir.mkdir("modeerr") |
| 1948 |
path = os.path.join(str(tmpdir0), "subscriptions.json") |
| 1949 |
|
| 1950 |
smgr = WebPushSubscriptionManager() |
| 1951 |
assert smgr.add(_subscription("https://web.push.apple.com/abc123")) is True |
| 1952 |
assert smgr.write(path) is True |
| 1953 |
|
| 1954 |
# We cannot stat what is already there, so our own mode is kept rather |
| 1955 |
# than guessing at one |
| 1956 |
assert smgr.add(_subscription("https://web.push.apple.com/xyz789")) is True |
| 1957 |
with mock.patch("os.stat", side_effect=PermissionError): |
| 1958 |
assert smgr.write(path) is True |
| 1959 |
|
| 1960 |
with open(path) as f: |
| 1961 |
assert set(json.load(f)) == {"abc123", "xyz789"} |
| 1962 |
|
| 1963 |
|
| 1964 |
@pytest.mark.skipif( |
| 1965 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1966 |
) |
| 1967 |
@mock.patch("requests.post") |
| 1968 |
def test_plugin_vapid_expired_is_reported(mock_post, tmpdir): |
| 1969 |
"""An expired subscription is reported as a failure.""" |
| 1970 |
|
| 1971 |
def respond(url, *args, **kwargs): |
| 1972 |
# One device is gone, the other is fine |
| 1973 |
return _mk_resp( |
| 1974 |
requests.codes.gone |
| 1975 |
if url.endswith("DEAD") |
| 1976 |
else requests.codes.created |
| 1977 |
) |
| 1978 |
|
| 1979 |
mock_post.side_effect = respond |
| 1980 |
|
| 1981 |
obj, _ = _mk_notifier( |
| 1982 |
tmpdir, |
| 1983 |
{ |
| 1984 |
"live": "https://web.push.apple.com/LIVE", |
| 1985 |
"dead": "https://web.push.apple.com/DEAD", |
| 1986 |
}, |
| 1987 |
["live", "dead"], |
| 1988 |
name="expreport", |
| 1989 |
) |
| 1990 |
|
| 1991 |
# The expired endpoint is a real failure; it is pruned below so no |
| 1992 |
# retry will reach for it again. |
| 1993 |
assert obj.send("test") is False |
| 1994 |
assert "dead" not in obj.subscriptions |
| 1995 |
|
| 1996 |
|
| 1997 |
@pytest.mark.skipif( |
| 1998 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 1999 |
) |
| 2000 |
@mock.patch("requests.post") |
| 2001 |
def test_plugin_vapid_all_expired_still_fails(mock_post, tmpdir): |
| 2002 |
"""Nothing reaching anybody is a failure however we got there.""" |
| 2003 |
|
| 2004 |
mock_post.return_value = _mk_resp(requests.codes.gone) |
| 2005 |
|
| 2006 |
obj, _ = _mk_notifier( |
| 2007 |
tmpdir, |
| 2008 |
["https://web.push.apple.com/DEAD"], |
| 2009 |
["dead"], |
| 2010 |
name="allexp", |
| 2011 |
) |
| 2012 |
|
| 2013 |
# Every target expired, so the message went nowhere. |
| 2014 |
assert obj.send("test") is False |
| 2015 |
|
| 2016 |
|
| 2017 |
@pytest.mark.skipif( |
| 2018 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 2019 |
) |
| 2020 |
def test_plugin_vapid_image_url(tmpdir): |
| 2021 |
"""The image flag survives a round trip through the URL.""" |
| 2022 |
|
| 2023 |
tmpdir0 = tmpdir.mkdir("imgurl") |
| 2024 |
asset_ = asset.AppriseAsset( |
| 2025 |
storage_mode=PersistentStoreMode.FLUSH, |
| 2026 |
storage_path=str(tmpdir0), |
| 2027 |
pem_autogen=True, |
| 2028 |
) |
| 2029 |
|
| 2030 |
# Images are included by default |
| 2031 |
obj = Apprise.instantiate("vapid://[email protected]/abc123", asset=asset_) |
| 2032 |
assert obj.include_image is True |
| 2033 |
assert "image=yes" in obj.url() |
| 2034 |
|
| 2035 |
# Turning them off is remembered rather than quietly lost when the URL |
| 2036 |
# is rebuilt |
| 2037 |
off = Apprise.instantiate( |
| 2038 |
"vapid://[email protected]/abc123?image=no", asset=asset_ |
| 2039 |
) |
| 2040 |
assert off.include_image is False |
| 2041 |
assert "image=no" in off.url() |
| 2042 |
|
| 2043 |
assert Apprise.instantiate(off.url()).include_image is False |
| 2044 |
assert Apprise.instantiate(obj.url()).include_image is True |
| 2045 |
|
| 2046 |
|
| 2047 |
@pytest.mark.skipif( |
| 2048 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 2049 |
) |
| 2050 |
def test_plugin_vapid_autogen(tmpdir): |
| 2051 |
"""Create and manage the default subscription file.""" |
| 2052 |
|
| 2053 |
tmpdir0 = tmpdir.mkdir("autogen") |
| 2054 |
|
| 2055 |
# With nothing to work with there is no file and nothing to load |
| 2056 |
smgr = WebPushSubscriptionManager() |
| 2057 |
assert smgr.subscription_file is None |
| 2058 |
assert smgr.loaded is False |
| 2059 |
assert smgr.autogen() is False |
| 2060 |
assert smgr.load() is False |
| 2061 |
assert smgr.loaded is True |
| 2062 |
|
| 2063 |
# A directory gives us a file of our own to create |
| 2064 |
smgr = WebPushSubscriptionManager(str(tmpdir0)) |
| 2065 |
assert smgr.subscription_file == os.path.join( |
| 2066 |
str(tmpdir0), WebPushSubscriptionManager.default_filename |
| 2067 |
) |
| 2068 |
assert smgr.autogen() is True |
| 2069 |
|
| 2070 |
# The file shows what a subscription is supposed to look like |
| 2071 |
with open(smgr.subscription_file) as f: |
| 2072 |
assert json.load(f) == smgr.template |
| 2073 |
|
| 2074 |
# We do not overwrite what is already there |
| 2075 |
assert smgr.autogen() is False |
| 2076 |
|
| 2077 |
# The placeholder is not a working subscription, so loading it leaves us |
| 2078 |
# with nothing to notify |
| 2079 |
assert smgr.load() is True |
| 2080 |
assert len(smgr) == 0 |
| 2081 |
|
| 2082 |
# A file we were pointed at belongs to our user, not to us |
| 2083 |
subfile = os.path.join(str(tmpdir0), "mine.json") |
| 2084 |
smgr = WebPushSubscriptionManager(str(tmpdir0), subfile=subfile) |
| 2085 |
assert smgr.subscription_file == subfile |
| 2086 |
assert smgr.autogen() is False |
| 2087 |
assert not os.path.exists(subfile) |
| 2088 |
|
| 2089 |
# A file we cannot write does not cause an exception |
| 2090 |
tmpdir1 = tmpdir.mkdir("autogen-fail") |
| 2091 |
smgr = WebPushSubscriptionManager(str(tmpdir1)) |
| 2092 |
with mock.patch("json.dump", side_effect=OSError): |
| 2093 |
assert smgr.autogen() is False |
| 2094 |
|
| 2095 |
|
| 2096 |
@pytest.mark.skipif( |
| 2097 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 2098 |
) |
| 2099 |
def test_plugin_vapid_autogen_disabled(tmpdir): |
| 2100 |
"""No starter file is left behind when auto-generation is turned off.""" |
| 2101 |
|
| 2102 |
tmpdir0 = tmpdir.mkdir("noautogen") |
| 2103 |
asset_ = asset.AppriseAsset( |
| 2104 |
storage_mode=PersistentStoreMode.FLUSH, |
| 2105 |
storage_path=str(tmpdir0), |
| 2106 |
pem_autogen=False, |
| 2107 |
) |
| 2108 |
|
| 2109 |
obj = Apprise.instantiate("vapid://[email protected]/abc123", asset=asset_) |
| 2110 |
assert obj.subfile is not None |
| 2111 |
assert not os.path.exists(obj.subfile) |
| 2112 |
|
| 2113 |
# There is nothing for us to notify with |
| 2114 |
assert obj.send("test") is False |
| 2115 |
|
| 2116 |
|
| 2117 |
@pytest.mark.skipif( |
| 2118 |
"cryptography" not in sys.modules, reason="Requires cryptography" |
| 2119 |
) |
| 2120 |
def test_plugin_vapid_rejects_malformed_subscription(tmpdir): |
| 2121 |
"""Reject malformed subscription data without raising exceptions.""" |
| 2122 |
|
| 2123 |
smgr = WebPushSubscriptionManager() |
| 2124 |
|
| 2125 |
# The keys entry is not an object we can read |
| 2126 |
for keys in (None, "nope", [1, 2], 42): |
| 2127 |
assert ( |
| 2128 |
smgr.add( |
| 2129 |
{"endpoint": "https://web.push.apple.com/X", "keys": keys}, |
| 2130 |
name="junk", |
| 2131 |
) |
| 2132 |
is False |
| 2133 |
) |
| 2134 |
|
| 2135 |
# The keys are there, but they are not base64 |
| 2136 |
assert ( |
| 2137 |
smgr.add( |
| 2138 |
{ |
| 2139 |
"endpoint": "https://web.push.apple.com/X", |
| 2140 |
"keys": {"p256dh": "!!", "auth": "!!"}, |
| 2141 |
}, |
| 2142 |
name="junk", |
| 2143 |
) |
| 2144 |
is False |
| 2145 |
) |
| 2146 |
|
| 2147 |
assert len(smgr) == 0 |
| 2148 |
|
| 2149 |
# Reject nesting deep enough to exhaust the JSON parser |
| 2150 |
tmpdir0 = tmpdir.mkdir("junk") |
| 2151 |
path = os.path.join(str(tmpdir0), "subscriptions.json") |
| 2152 |
with open(path, "w") as f: |
| 2153 |
f.write("[" * 200000 + "]" * 200000) |
| 2154 |
|
| 2155 |
assert smgr.load(path) is False |
| 2156 |
|
| 2157 |
# Reject the same nesting when supplied as a string |
| 2158 |
assert WebPushSubscription().load("[" * 200000 + "]" * 200000) is False |