| 1 |
# |
| 2 |
# BSD 2-Clause License |
| 3 |
# |
| 4 |
# Apprise - Push Notification Library. |
| 5 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 6 |
# |
| 7 |
# Redistribution and use in source and binary forms, with or without |
| 8 |
# modification, are permitted provided that the following conditions are met: |
| 9 |
# |
| 10 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 11 |
# this list of conditions and the following disclaimer. |
| 12 |
# |
| 13 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 |
# this list of conditions and the following disclaimer in the documentation |
| 15 |
# and/or other materials provided with the distribution. |
| 16 |
# |
| 17 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 |
# POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
| 29 |
# Disable logging for a cleaner testing output |
| 30 |
from json import dumps, loads |
| 31 |
import logging |
| 32 |
from unittest import mock |
| 33 |
|
| 34 |
from helpers import AppriseURLTester |
| 35 |
import requests |
| 36 |
|
| 37 |
from apprise import Apprise |
| 38 |
from apprise.common import NotifyFormat |
| 39 |
from apprise.exception import AppriseImproperlyConfigured |
| 40 |
from apprise.plugins.pushplus import ( |
| 41 |
PUSHPLUS_CHANNEL_DEFAULT, |
| 42 |
PUSHPLUS_CHANNELS, |
| 43 |
PUSHPLUS_FORMAT_MAP, |
| 44 |
NotifyPushplus, |
| 45 |
PushPlusChannel, |
| 46 |
) |
| 47 |
|
| 48 |
logging.disable(logging.CRITICAL) |
| 49 |
|
| 50 |
# A 32-character token used throughout the tests (minimum valid length) |
| 51 |
GOOD_TOKEN = "abc123def456ghi789jkl012mno345pq" |
| 52 |
|
| 53 |
# A 64-character token (upper boundary of the allowed length range) |
| 54 |
LONG_TOKEN = "a" * 64 |
| 55 |
|
| 56 |
# PushPlus always returns HTTP 200; success/failure lives in the JSON body |
| 57 |
GOOD_RESPONSE = dumps({"code": 200, "msg": "ok", "data": "msgid"}) |
| 58 |
BAD_RESPONSE = dumps({"code": 907, "msg": "Token does not exist."}) |
| 59 |
|
| 60 |
# Our Testing URLs |
| 61 |
apprise_url_tests = ( |
| 62 |
# ---------------------------------------------------------------- |
| 63 |
# Invalid / missing token cases |
| 64 |
# ---------------------------------------------------------------- |
| 65 |
( |
| 66 |
"pushplus://", |
| 67 |
{ |
| 68 |
# Empty token must raise |
| 69 |
"instance": AppriseImproperlyConfigured, |
| 70 |
}, |
| 71 |
), |
| 72 |
( |
| 73 |
"pushplus://short", |
| 74 |
{ |
| 75 |
# Token too short (fewer than 32 characters) |
| 76 |
"instance": AppriseImproperlyConfigured, |
| 77 |
}, |
| 78 |
), |
| 79 |
( |
| 80 |
"pushplus://invalid!chars00000000000000000000000000000", |
| 81 |
{ |
| 82 |
# Token contains characters not allowed by the regex |
| 83 |
"instance": AppriseImproperlyConfigured, |
| 84 |
}, |
| 85 |
), |
| 86 |
# ---------------------------------------------------------------- |
| 87 |
# Valid token -- basic personal notification (no targets) |
| 88 |
# ---------------------------------------------------------------- |
| 89 |
( |
| 90 |
"pushplus://{}".format(GOOD_TOKEN), |
| 91 |
{ |
| 92 |
"instance": NotifyPushplus, |
| 93 |
# Privacy URL must mask the token |
| 94 |
"privacy_url": "pushplus://****/", |
| 95 |
"requests_response_text": GOOD_RESPONSE, |
| 96 |
}, |
| 97 |
), |
| 98 |
# Token supplied as a query parameter instead of the URL host |
| 99 |
( |
| 100 |
"pushplus://?token={}".format(GOOD_TOKEN), |
| 101 |
{ |
| 102 |
"instance": NotifyPushplus, |
| 103 |
"privacy_url": "pushplus://****/", |
| 104 |
"requests_response_text": GOOD_RESPONSE, |
| 105 |
}, |
| 106 |
), |
| 107 |
# 64-character (maximum-length) token |
| 108 |
( |
| 109 |
"pushplus://{}".format(LONG_TOKEN), |
| 110 |
{ |
| 111 |
"instance": NotifyPushplus, |
| 112 |
"requests_response_text": GOOD_RESPONSE, |
| 113 |
}, |
| 114 |
), |
| 115 |
# ---------------------------------------------------------------- |
| 116 |
# Group topic variations |
| 117 |
# ---------------------------------------------------------------- |
| 118 |
# Single topic in the URL path (no prefix required) |
| 119 |
( |
| 120 |
"pushplus://{}/mygroup".format(GOOD_TOKEN), |
| 121 |
{ |
| 122 |
"instance": NotifyPushplus, |
| 123 |
"privacy_url": "pushplus://****/mygroup/", |
| 124 |
"requests_response_text": GOOD_RESPONSE, |
| 125 |
}, |
| 126 |
), |
| 127 |
# Two topics in the path -- two API calls are made |
| 128 |
( |
| 129 |
"pushplus://{}/group1/group2".format(GOOD_TOKEN), |
| 130 |
{ |
| 131 |
"instance": NotifyPushplus, |
| 132 |
"requests_response_text": GOOD_RESPONSE, |
| 133 |
}, |
| 134 |
), |
| 135 |
# Topic supplied via the backward-compatible ?topic= parameter |
| 136 |
( |
| 137 |
"pushplus://{}/?topic=mygroup".format(GOOD_TOKEN), |
| 138 |
{ |
| 139 |
"instance": NotifyPushplus, |
| 140 |
"requests_response_text": GOOD_RESPONSE, |
| 141 |
}, |
| 142 |
), |
| 143 |
# Topic supplied via the ?to= convenience alias |
| 144 |
( |
| 145 |
"pushplus://{}/?to=mygroup".format(GOOD_TOKEN), |
| 146 |
{ |
| 147 |
"instance": NotifyPushplus, |
| 148 |
"requests_response_text": GOOD_RESPONSE, |
| 149 |
}, |
| 150 |
), |
| 151 |
# Invalid topic characters are silently moved to invalid_targets; |
| 152 |
# the object still instantiates and sends a personal notification |
| 153 |
( |
| 154 |
"pushplus://{}/bad!topic".format(GOOD_TOKEN), |
| 155 |
{ |
| 156 |
"instance": NotifyPushplus, |
| 157 |
"requests_response_text": GOOD_RESPONSE, |
| 158 |
}, |
| 159 |
), |
| 160 |
# ---------------------------------------------------------------- |
| 161 |
# Delivery channel via ?channel= query parameter |
| 162 |
# ---------------------------------------------------------------- |
| 163 |
( |
| 164 |
"pushplus://{}?channel=mail".format(GOOD_TOKEN), |
| 165 |
{ |
| 166 |
"instance": NotifyPushplus, |
| 167 |
"requests_response_text": GOOD_RESPONSE, |
| 168 |
}, |
| 169 |
), |
| 170 |
( |
| 171 |
"pushplus://{}?channel=sms".format(GOOD_TOKEN), |
| 172 |
{ |
| 173 |
"instance": NotifyPushplus, |
| 174 |
"requests_response_text": GOOD_RESPONSE, |
| 175 |
}, |
| 176 |
), |
| 177 |
( |
| 178 |
"pushplus://{}?channel=cp".format(GOOD_TOKEN), |
| 179 |
{ |
| 180 |
"instance": NotifyPushplus, |
| 181 |
"requests_response_text": GOOD_RESPONSE, |
| 182 |
}, |
| 183 |
), |
| 184 |
# wecom is a friendly alias that maps to the "cp" API channel |
| 185 |
( |
| 186 |
"pushplus://{}?channel=wecom".format(GOOD_TOKEN), |
| 187 |
{ |
| 188 |
"instance": NotifyPushplus, |
| 189 |
"requests_response_text": GOOD_RESPONSE, |
| 190 |
}, |
| 191 |
), |
| 192 |
# mode= is a synonym for channel= |
| 193 |
( |
| 194 |
"pushplus://{}?mode=mail".format(GOOD_TOKEN), |
| 195 |
{ |
| 196 |
"instance": NotifyPushplus, |
| 197 |
"requests_response_text": GOOD_RESPONSE, |
| 198 |
}, |
| 199 |
), |
| 200 |
# ---------------------------------------------------------------- |
| 201 |
# Schema alias that auto-sets the delivery channel |
| 202 |
# ---------------------------------------------------------------- |
| 203 |
# wecom:// auto-sets channel=cp (WeCom / Enterprise WeChat) |
| 204 |
( |
| 205 |
"wecom://{}".format(GOOD_TOKEN), |
| 206 |
{ |
| 207 |
"instance": NotifyPushplus, |
| 208 |
"requests_response_text": GOOD_RESPONSE, |
| 209 |
}, |
| 210 |
), |
| 211 |
# ---------------------------------------------------------------- |
| 212 |
# Webhook channel with a named endpoint -- both input forms accepted |
| 213 |
# ---------------------------------------------------------------- |
| 214 |
( |
| 215 |
"pushplus://{}?channel=webhook&name=myhook".format(GOOD_TOKEN), |
| 216 |
{ |
| 217 |
"instance": NotifyPushplus, |
| 218 |
# url() emits the compact schema://name@token form |
| 219 |
"privacy_url": "pushplus://myhook@", |
| 220 |
"requests_response_text": GOOD_RESPONSE, |
| 221 |
}, |
| 222 |
), |
| 223 |
# schema://name@token form -- channel=webhook is implied |
| 224 |
( |
| 225 |
"pushplus://myhook@{}".format(GOOD_TOKEN), |
| 226 |
{ |
| 227 |
"instance": NotifyPushplus, |
| 228 |
"privacy_url": "pushplus://myhook@", |
| 229 |
"requests_response_text": GOOD_RESPONSE, |
| 230 |
}, |
| 231 |
), |
| 232 |
# ---------------------------------------------------------------- |
| 233 |
# Native PushPlus API URL (parse_native_url support) |
| 234 |
# ---------------------------------------------------------------- |
| 235 |
( |
| 236 |
"https://www.pushplus.plus/send?token={}".format(GOOD_TOKEN), |
| 237 |
{ |
| 238 |
"instance": NotifyPushplus, |
| 239 |
"requests_response_text": GOOD_RESPONSE, |
| 240 |
}, |
| 241 |
), |
| 242 |
# ---------------------------------------------------------------- |
| 243 |
# API application-level failure (HTTP 200 but JSON code != 200) |
| 244 |
# ---------------------------------------------------------------- |
| 245 |
( |
| 246 |
"pushplus://{}".format(GOOD_TOKEN), |
| 247 |
{ |
| 248 |
"instance": NotifyPushplus, |
| 249 |
"response": False, |
| 250 |
"requests_response_text": BAD_RESPONSE, |
| 251 |
}, |
| 252 |
), |
| 253 |
# Unparsable JSON body is treated as a failure |
| 254 |
( |
| 255 |
"pushplus://{}".format(GOOD_TOKEN), |
| 256 |
{ |
| 257 |
"instance": NotifyPushplus, |
| 258 |
"response": False, |
| 259 |
"requests_response_text": "{bad json", |
| 260 |
}, |
| 261 |
), |
| 262 |
# None body is treated as a failure |
| 263 |
( |
| 264 |
"pushplus://{}".format(GOOD_TOKEN), |
| 265 |
{ |
| 266 |
"instance": NotifyPushplus, |
| 267 |
"response": False, |
| 268 |
"requests_response_text": None, |
| 269 |
}, |
| 270 |
), |
| 271 |
# ---------------------------------------------------------------- |
| 272 |
# HTTP-level failures |
| 273 |
# ---------------------------------------------------------------- |
| 274 |
( |
| 275 |
"pushplus://{}".format(GOOD_TOKEN), |
| 276 |
{ |
| 277 |
"instance": NotifyPushplus, |
| 278 |
"response": False, |
| 279 |
"requests_response_code": requests.codes.internal_server_error, |
| 280 |
}, |
| 281 |
), |
| 282 |
( |
| 283 |
"pushplus://{}".format(GOOD_TOKEN), |
| 284 |
{ |
| 285 |
"instance": NotifyPushplus, |
| 286 |
"response": False, |
| 287 |
"requests_response_code": 999, |
| 288 |
}, |
| 289 |
), |
| 290 |
# ---------------------------------------------------------------- |
| 291 |
# Connection / socket exceptions |
| 292 |
# ---------------------------------------------------------------- |
| 293 |
( |
| 294 |
"pushplus://{}".format(GOOD_TOKEN), |
| 295 |
{ |
| 296 |
"instance": NotifyPushplus, |
| 297 |
"test_requests_exceptions": True, |
| 298 |
}, |
| 299 |
), |
| 300 |
) |
| 301 |
|
| 302 |
|
| 303 |
def test_plugin_pushplus_urls(): |
| 304 |
"""NotifyPushplus() Apprise URL test suite.""" |
| 305 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 306 |
|
| 307 |
|
| 308 |
def test_plugin_pushplus_init(): |
| 309 |
"""NotifyPushplus() initialisation and parameter handling.""" |
| 310 |
|
| 311 |
# Minimal valid instantiation -- no targets |
| 312 |
obj = NotifyPushplus(token=GOOD_TOKEN) |
| 313 |
assert obj.token == GOOD_TOKEN |
| 314 |
assert obj.topics == [] |
| 315 |
assert obj.channel == PUSHPLUS_CHANNEL_DEFAULT |
| 316 |
assert obj.webhook is None |
| 317 |
assert obj.invalid_targets == [] |
| 318 |
|
| 319 |
# Single group topic via targets list (no prefix required) |
| 320 |
obj = NotifyPushplus(token=GOOD_TOKEN, targets=["mygroup"]) |
| 321 |
assert obj.topics == ["mygroup"] |
| 322 |
assert obj.channel == PUSHPLUS_CHANNEL_DEFAULT |
| 323 |
|
| 324 |
# Multiple group topics |
| 325 |
obj = NotifyPushplus( |
| 326 |
token=GOOD_TOKEN, targets=["group1", "group2", "group3"] |
| 327 |
) |
| 328 |
assert obj.topics == ["group1", "group2", "group3"] |
| 329 |
|
| 330 |
# Explicit channel via channel= argument |
| 331 |
obj = NotifyPushplus(token=GOOD_TOKEN, channel="mail") |
| 332 |
assert obj.channel == PushPlusChannel.MAIL |
| 333 |
|
| 334 |
# wecom friendly alias normalises to the "cp" API value |
| 335 |
obj = NotifyPushplus(token=GOOD_TOKEN, channel="wecom") |
| 336 |
assert obj.channel == PushPlusChannel.WECOM |
| 337 |
assert obj.channel == "cp" |
| 338 |
|
| 339 |
# cp is the canonical API value (same result as wecom) |
| 340 |
obj = NotifyPushplus(token=GOOD_TOKEN, channel="cp") |
| 341 |
assert obj.channel == "cp" |
| 342 |
|
| 343 |
# Channel is case-insensitive |
| 344 |
obj = NotifyPushplus(token=GOOD_TOKEN, channel="MAIL") |
| 345 |
assert obj.channel == PushPlusChannel.MAIL |
| 346 |
|
| 347 |
# An invalid channel is rejected. |
| 348 |
import pytest |
| 349 |
|
| 350 |
with pytest.raises(AppriseImproperlyConfigured): |
| 351 |
NotifyPushplus(token=GOOD_TOKEN, channel="invalid_channel") |
| 352 |
|
| 353 |
# Invalid target (bad characters) goes to invalid_targets |
| 354 |
obj = NotifyPushplus(token=GOOD_TOKEN, targets=["bad!target"]) |
| 355 |
assert obj.topics == [] |
| 356 |
assert obj.invalid_targets == ["bad!target"] |
| 357 |
|
| 358 |
# An invalid token is rejected. |
| 359 |
with pytest.raises(AppriseImproperlyConfigured): |
| 360 |
NotifyPushplus(token="short") |
| 361 |
|
| 362 |
with pytest.raises(AppriseImproperlyConfigured): |
| 363 |
NotifyPushplus(token="bad!token" + "0" * 30) |
| 364 |
|
| 365 |
# Webhook value stored as None when falsy |
| 366 |
obj = NotifyPushplus(token=GOOD_TOKEN, webhook="") |
| 367 |
assert obj.webhook is None |
| 368 |
|
| 369 |
# Webhook value stored when truthy |
| 370 |
obj = NotifyPushplus( |
| 371 |
token=GOOD_TOKEN, |
| 372 |
channel="webhook", |
| 373 |
webhook="myhook", |
| 374 |
) |
| 375 |
assert obj.webhook == "myhook" |
| 376 |
assert obj.channel == PushPlusChannel.WEBHOOK |
| 377 |
|
| 378 |
|
| 379 |
def test_plugin_pushplus_schema_aliases(): |
| 380 |
"""NotifyPushplus() wecom:// schema alias.""" |
| 381 |
|
| 382 |
# wecom:// schema sets channel=cp (WeCom API value) |
| 383 |
result = NotifyPushplus.parse_url("wecom://{}".format(GOOD_TOKEN)) |
| 384 |
assert result is not None |
| 385 |
obj = NotifyPushplus(**result) |
| 386 |
assert obj.channel == PushPlusChannel.WECOM |
| 387 |
assert obj.channel == "cp" |
| 388 |
|
| 389 |
# An explicit channel= overrides the schema-implied channel |
| 390 |
result = NotifyPushplus.parse_url( |
| 391 |
"wecom://{}?channel=mail".format(GOOD_TOKEN) |
| 392 |
) |
| 393 |
assert result is not None |
| 394 |
obj = NotifyPushplus(**result) |
| 395 |
assert obj.channel == PushPlusChannel.MAIL |
| 396 |
|
| 397 |
|
| 398 |
def test_plugin_pushplus_all_channels(): |
| 399 |
"""NotifyPushplus() accepts every documented API channel value.""" |
| 400 |
|
| 401 |
for ch in PUSHPLUS_CHANNELS: |
| 402 |
# Each channel must initialise without raising |
| 403 |
obj = NotifyPushplus(token=GOOD_TOKEN, channel=ch) |
| 404 |
assert obj.channel == ch |
| 405 |
|
| 406 |
# Channel matching must be case-insensitive |
| 407 |
obj2 = NotifyPushplus(token=GOOD_TOKEN, channel=ch.upper()) |
| 408 |
assert obj2.channel == ch |
| 409 |
|
| 410 |
|
| 411 |
def test_plugin_pushplus_url(): |
| 412 |
"""NotifyPushplus() url() output and round-trip fidelity.""" |
| 413 |
|
| 414 |
# Personal notification URL -- no topics in path, default channel omitted |
| 415 |
obj = NotifyPushplus(token=GOOD_TOKEN) |
| 416 |
url = obj.url() |
| 417 |
assert url.startswith("pushplus://") |
| 418 |
# Token appears in plain text |
| 419 |
assert GOOD_TOKEN in url |
| 420 |
# No topic path segment |
| 421 |
assert "/mygroup" not in url |
| 422 |
# Default channel is omitted from params |
| 423 |
assert "channel=" not in url |
| 424 |
|
| 425 |
# Privacy URL must mask the token |
| 426 |
priv = obj.url(privacy=True) |
| 427 |
assert GOOD_TOKEN not in priv |
| 428 |
assert "****" in priv |
| 429 |
|
| 430 |
# Group topic URL -- topic appears in the path |
| 431 |
obj = NotifyPushplus(token=GOOD_TOKEN, targets=["mygroup"]) |
| 432 |
url = obj.url() |
| 433 |
assert "/mygroup/" in url |
| 434 |
priv = obj.url(privacy=True) |
| 435 |
assert "/mygroup/" in priv |
| 436 |
assert GOOD_TOKEN not in priv |
| 437 |
|
| 438 |
# Multiple topics all appear in the path |
| 439 |
obj = NotifyPushplus(token=GOOD_TOKEN, targets=["grp1", "grp2"]) |
| 440 |
url = obj.url() |
| 441 |
assert "grp1" in url |
| 442 |
assert "grp2" in url |
| 443 |
|
| 444 |
# Non-default channel appears as ?channel= query parameter |
| 445 |
obj = NotifyPushplus(token=GOOD_TOKEN, channel="mail") |
| 446 |
url = obj.url() |
| 447 |
assert "channel=mail" in url |
| 448 |
|
| 449 |
# Default channel (wechat) is suppressed from the URL |
| 450 |
obj = NotifyPushplus(token=GOOD_TOKEN, channel="wechat") |
| 451 |
url = obj.url() |
| 452 |
assert "channel=" not in url |
| 453 |
|
| 454 |
# When channel=webhook with a name, url() uses schema://name@token form |
| 455 |
obj = NotifyPushplus( |
| 456 |
token=GOOD_TOKEN, |
| 457 |
channel="webhook", |
| 458 |
webhook="myhook", |
| 459 |
) |
| 460 |
url = obj.url() |
| 461 |
assert "pushplus://myhook@" in url |
| 462 |
# channel= and name= are both implied by user@ -- omitted from params |
| 463 |
assert "channel=" not in url |
| 464 |
assert "name=" not in url |
| 465 |
|
| 466 |
# Privacy URL preserves the webhook name but masks the token |
| 467 |
priv = obj.url(privacy=True) |
| 468 |
assert "pushplus://myhook@" in priv |
| 469 |
assert GOOD_TOKEN not in priv |
| 470 |
assert "****" in priv |
| 471 |
|
| 472 |
# Webhook prefix form with group topics -- schema://name@token/topic/ form |
| 473 |
obj = NotifyPushplus( |
| 474 |
token=GOOD_TOKEN, |
| 475 |
channel="webhook", |
| 476 |
webhook="myhook", |
| 477 |
targets=["mygroup"], |
| 478 |
) |
| 479 |
url = obj.url() |
| 480 |
assert "pushplus://myhook@" in url |
| 481 |
assert "mygroup" in url |
| 482 |
assert "channel=" not in url |
| 483 |
assert "name=" not in url |
| 484 |
|
| 485 |
# Webhook name suppressed when channel is not webhook |
| 486 |
obj = NotifyPushplus( |
| 487 |
token=GOOD_TOKEN, |
| 488 |
channel="mail", |
| 489 |
webhook="myhook", |
| 490 |
) |
| 491 |
url = obj.url() |
| 492 |
assert "name=" not in url |
| 493 |
assert "@" not in url |
| 494 |
|
| 495 |
# Webhook channel without a name uses the plain ?channel=webhook form |
| 496 |
obj = NotifyPushplus(token=GOOD_TOKEN, channel="webhook") |
| 497 |
url = obj.url() |
| 498 |
assert "name=" not in url |
| 499 |
assert "channel=webhook" in url |
| 500 |
assert "@" not in url |
| 501 |
|
| 502 |
# Invalid targets are preserved in the URL path for round-trip fidelity |
| 503 |
obj = NotifyPushplus(token=GOOD_TOKEN, targets=["bad!target"]) |
| 504 |
url = obj.url() |
| 505 |
assert "bad" in url |
| 506 |
|
| 507 |
# url_identifier contains only the schema and token (no targets or channel) |
| 508 |
obj = NotifyPushplus(token=GOOD_TOKEN, targets=["grp"]) |
| 509 |
assert obj.url_identifier == ("pushplus", GOOD_TOKEN) |
| 510 |
|
| 511 |
# Schema alias (wecom://) always normalises back to pushplus:// |
| 512 |
result = NotifyPushplus.parse_url("wecom://{}".format(GOOD_TOKEN)) |
| 513 |
obj = NotifyPushplus(**result) |
| 514 |
url = obj.url() |
| 515 |
assert url.startswith("pushplus://") |
| 516 |
assert "channel=cp" in url |
| 517 |
|
| 518 |
|
| 519 |
@mock.patch("requests.post") |
| 520 |
def test_plugin_pushplus_send_ok(mock_post): |
| 521 |
"""NotifyPushplus() successful personal-notification send.""" |
| 522 |
|
| 523 |
# Prepare a successful mock response |
| 524 |
response = mock.Mock() |
| 525 |
response.status_code = requests.codes.ok |
| 526 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 527 |
mock_post.return_value = response |
| 528 |
|
| 529 |
# Personal notification (no topic) -- single API call |
| 530 |
obj = NotifyPushplus(token=GOOD_TOKEN) |
| 531 |
assert obj.send(body="hello", title="title") is True |
| 532 |
assert mock_post.call_count == 1 |
| 533 |
|
| 534 |
# Verify the endpoint URL |
| 535 |
call = mock_post.call_args |
| 536 |
assert call[0][0] == "https://www.pushplus.plus/send" |
| 537 |
|
| 538 |
# Verify the core payload fields |
| 539 |
payload = loads(call[1]["data"]) |
| 540 |
assert payload["token"] == GOOD_TOKEN |
| 541 |
assert payload["content"] == "hello" |
| 542 |
assert payload["title"] == "title" |
| 543 |
# Default format is HTML -- PushPlus template should be "html" |
| 544 |
assert payload["template"] == PUSHPLUS_FORMAT_MAP[NotifyFormat.HTML] |
| 545 |
assert payload["channel"] == PUSHPLUS_CHANNEL_DEFAULT |
| 546 |
# topic and webhook keys must be absent when not configured |
| 547 |
assert "topic" not in payload |
| 548 |
assert "webhook" not in payload |
| 549 |
|
| 550 |
mock_post.reset_mock() |
| 551 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 552 |
|
| 553 |
# When no title is supplied the body is used as the title fallback |
| 554 |
assert obj.send(body="only body") is True |
| 555 |
payload = loads(mock_post.call_args[1]["data"]) |
| 556 |
assert payload["title"] == "only body" |
| 557 |
assert payload["content"] == "only body" |
| 558 |
|
| 559 |
|
| 560 |
@mock.patch("requests.post") |
| 561 |
def test_plugin_pushplus_send_formats(mock_post): |
| 562 |
"""NotifyPushplus() maps notify_format to the PushPlus template field.""" |
| 563 |
|
| 564 |
response = mock.Mock() |
| 565 |
response.status_code = requests.codes.ok |
| 566 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 567 |
mock_post.return_value = response |
| 568 |
|
| 569 |
obj = NotifyPushplus(token=GOOD_TOKEN) |
| 570 |
|
| 571 |
# Markdown format -> "markdown" template |
| 572 |
obj.notify_format = NotifyFormat.MARKDOWN |
| 573 |
assert obj.send(body="# heading") is True |
| 574 |
payload = loads(mock_post.call_args[1]["data"]) |
| 575 |
assert payload["template"] == "markdown" |
| 576 |
|
| 577 |
mock_post.reset_mock() |
| 578 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 579 |
|
| 580 |
# Text format -> "txt" template |
| 581 |
obj.notify_format = NotifyFormat.TEXT |
| 582 |
assert obj.send(body="plain text") is True |
| 583 |
payload = loads(mock_post.call_args[1]["data"]) |
| 584 |
assert payload["template"] == "txt" |
| 585 |
|
| 586 |
mock_post.reset_mock() |
| 587 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 588 |
|
| 589 |
# HTML format -> "html" template |
| 590 |
obj.notify_format = NotifyFormat.HTML |
| 591 |
assert obj.send(body="<b>bold</b>") is True |
| 592 |
payload = loads(mock_post.call_args[1]["data"]) |
| 593 |
assert payload["template"] == "html" |
| 594 |
|
| 595 |
|
| 596 |
@mock.patch("requests.post") |
| 597 |
def test_plugin_pushplus_send_topic(mock_post): |
| 598 |
"""NotifyPushplus() includes the topic in the payload when configured.""" |
| 599 |
|
| 600 |
response = mock.Mock() |
| 601 |
response.status_code = requests.codes.ok |
| 602 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 603 |
mock_post.return_value = response |
| 604 |
|
| 605 |
# Single topic -- one API call, topic present in payload |
| 606 |
obj = NotifyPushplus(token=GOOD_TOKEN, targets=["mygroup"]) |
| 607 |
assert obj.send(body="group msg") is True |
| 608 |
assert mock_post.call_count == 1 |
| 609 |
payload = loads(mock_post.call_args[1]["data"]) |
| 610 |
assert payload["topic"] == "mygroup" |
| 611 |
|
| 612 |
|
| 613 |
@mock.patch("requests.post") |
| 614 |
def test_plugin_pushplus_send_multiple_topics(mock_post): |
| 615 |
"""NotifyPushplus() makes one API call per group topic.""" |
| 616 |
|
| 617 |
response = mock.Mock() |
| 618 |
response.status_code = requests.codes.ok |
| 619 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 620 |
mock_post.return_value = response |
| 621 |
|
| 622 |
# Three topics -> three separate API calls |
| 623 |
obj = NotifyPushplus( |
| 624 |
token=GOOD_TOKEN, targets=["topic1", "topic2", "topic3"] |
| 625 |
) |
| 626 |
assert len(obj.topics) == 3 |
| 627 |
assert obj.send(body="multi msg") is True |
| 628 |
assert mock_post.call_count == 3 |
| 629 |
|
| 630 |
# Verify each call carries the correct topic value |
| 631 |
calls = mock_post.call_args_list |
| 632 |
topics_sent = [loads(c[1]["data"])["topic"] for c in calls] |
| 633 |
assert topics_sent == ["topic1", "topic2", "topic3"] |
| 634 |
|
| 635 |
mock_post.reset_mock() |
| 636 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 637 |
|
| 638 |
# Topics with a channel override -- all API calls use the specified channel |
| 639 |
obj2 = NotifyPushplus( |
| 640 |
token=GOOD_TOKEN, targets=["grp1", "grp2"], channel="mail" |
| 641 |
) |
| 642 |
assert len(obj2.topics) == 2 |
| 643 |
assert obj2.channel == PushPlusChannel.MAIL |
| 644 |
assert obj2.send(body="mixed") is True |
| 645 |
assert mock_post.call_count == 2 |
| 646 |
for call in mock_post.call_args_list: |
| 647 |
assert loads(call[1]["data"])["channel"] == "mail" |
| 648 |
|
| 649 |
mock_post.reset_mock() |
| 650 |
|
| 651 |
# Partial failure: first topic succeeds, second fails -> overall False |
| 652 |
good_resp = mock.Mock() |
| 653 |
good_resp.status_code = requests.codes.ok |
| 654 |
good_resp.content = GOOD_RESPONSE.encode("utf-8") |
| 655 |
bad_resp = mock.Mock() |
| 656 |
bad_resp.status_code = requests.codes.ok |
| 657 |
bad_resp.content = BAD_RESPONSE.encode("utf-8") |
| 658 |
mock_post.side_effect = [good_resp, bad_resp] |
| 659 |
|
| 660 |
obj3 = NotifyPushplus(token=GOOD_TOKEN, targets=["grp1", "grp2"]) |
| 661 |
assert obj3.send(body="partial") is False |
| 662 |
assert mock_post.call_count == 2 |
| 663 |
|
| 664 |
|
| 665 |
@mock.patch("requests.post") |
| 666 |
def test_plugin_pushplus_send_webhook(mock_post): |
| 667 |
"""NotifyPushplus() webhook channel behaviour.""" |
| 668 |
|
| 669 |
response = mock.Mock() |
| 670 |
response.status_code = requests.codes.ok |
| 671 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 672 |
mock_post.return_value = response |
| 673 |
|
| 674 |
# Webhook channel with a named endpoint -- webhook key in payload |
| 675 |
obj = NotifyPushplus( |
| 676 |
token=GOOD_TOKEN, |
| 677 |
channel="webhook", |
| 678 |
webhook="myhook", |
| 679 |
) |
| 680 |
assert obj.send(body="hook msg") is True |
| 681 |
payload = loads(mock_post.call_args[1]["data"]) |
| 682 |
assert payload["channel"] == PushPlusChannel.WEBHOOK |
| 683 |
assert payload["webhook"] == "myhook" |
| 684 |
|
| 685 |
mock_post.reset_mock() |
| 686 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 687 |
|
| 688 |
# Webhook channel without a name -- webhook key must be absent |
| 689 |
obj2 = NotifyPushplus(token=GOOD_TOKEN, channel="webhook") |
| 690 |
assert obj2.send(body="hook no name") is True |
| 691 |
payload2 = loads(mock_post.call_args[1]["data"]) |
| 692 |
assert "webhook" not in payload2 |
| 693 |
|
| 694 |
mock_post.reset_mock() |
| 695 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 696 |
|
| 697 |
# Non-webhook channel with a webhook value set -- key must be absent |
| 698 |
obj3 = NotifyPushplus( |
| 699 |
token=GOOD_TOKEN, |
| 700 |
channel="mail", |
| 701 |
webhook="myhook", |
| 702 |
) |
| 703 |
assert obj3.send(body="mail msg") is True |
| 704 |
payload3 = loads(mock_post.call_args[1]["data"]) |
| 705 |
assert "webhook" not in payload3 |
| 706 |
|
| 707 |
|
| 708 |
@mock.patch("requests.post") |
| 709 |
def test_plugin_pushplus_send_http_error(mock_post): |
| 710 |
"""NotifyPushplus() handles HTTP-level errors correctly.""" |
| 711 |
|
| 712 |
response = mock.Mock() |
| 713 |
response.content = b"" |
| 714 |
mock_post.return_value = response |
| 715 |
|
| 716 |
obj = NotifyPushplus(token=GOOD_TOKEN) |
| 717 |
|
| 718 |
# HTTP 500 |
| 719 |
response.status_code = requests.codes.internal_server_error |
| 720 |
assert obj.send(body="msg") is False |
| 721 |
|
| 722 |
# Unknown HTTP status code |
| 723 |
response.status_code = 999 |
| 724 |
assert obj.send(body="msg") is False |
| 725 |
|
| 726 |
|
| 727 |
@mock.patch("requests.post") |
| 728 |
def test_plugin_pushplus_send_api_error(mock_post): |
| 729 |
"""NotifyPushplus() handles API-level failures (HTTP 200, bad code).""" |
| 730 |
|
| 731 |
response = mock.Mock() |
| 732 |
response.status_code = requests.codes.ok |
| 733 |
mock_post.return_value = response |
| 734 |
|
| 735 |
obj = NotifyPushplus(token=GOOD_TOKEN) |
| 736 |
|
| 737 |
# Known API error code (907 = Token does not exist) |
| 738 |
response.content = BAD_RESPONSE.encode("utf-8") |
| 739 |
assert obj.send(body="msg") is False |
| 740 |
|
| 741 |
# Unknown API error code with a msg field -- falls back to msg |
| 742 |
response.content = dumps({"code": 999, "msg": "some error"}).encode( |
| 743 |
"utf-8" |
| 744 |
) |
| 745 |
assert obj.send(body="msg") is False |
| 746 |
|
| 747 |
# Unknown API error code with no msg field -- generic fallback |
| 748 |
response.content = dumps({"code": 999}).encode("utf-8") |
| 749 |
assert obj.send(body="msg") is False |
| 750 |
|
| 751 |
# Unparsable JSON body |
| 752 |
response.content = b"{bad json" |
| 753 |
assert obj.send(body="msg") is False |
| 754 |
|
| 755 |
# None content -- treated as unparsable |
| 756 |
response.content = None |
| 757 |
assert obj.send(body="msg") is False |
| 758 |
|
| 759 |
|
| 760 |
@mock.patch("requests.post") |
| 761 |
def test_plugin_pushplus_send_exception(mock_post): |
| 762 |
"""NotifyPushplus() handles connection exceptions gracefully.""" |
| 763 |
mock_post.side_effect = requests.RequestException("connection error") |
| 764 |
obj = NotifyPushplus(token=GOOD_TOKEN) |
| 765 |
assert obj.send(body="msg") is False |
| 766 |
|
| 767 |
|
| 768 |
@mock.patch("requests.post") |
| 769 |
def test_plugin_pushplus_send_exception_multi_topic(mock_post): |
| 770 |
"""NotifyPushplus() continues after a connection error on one topic.""" |
| 771 |
|
| 772 |
# First call raises, second succeeds -- overall result is False |
| 773 |
good_resp = mock.Mock() |
| 774 |
good_resp.status_code = requests.codes.ok |
| 775 |
good_resp.content = GOOD_RESPONSE.encode("utf-8") |
| 776 |
mock_post.side_effect = [ |
| 777 |
requests.RequestException("network error"), |
| 778 |
good_resp, |
| 779 |
] |
| 780 |
|
| 781 |
obj = NotifyPushplus(token=GOOD_TOKEN, targets=["grp1", "grp2"]) |
| 782 |
assert obj.send(body="msg") is False |
| 783 |
# Both topics must still be attempted |
| 784 |
assert mock_post.call_count == 2 |
| 785 |
|
| 786 |
|
| 787 |
def test_plugin_pushplus_parse_url(): |
| 788 |
"""NotifyPushplus() parse_url() extracts all supported fields.""" |
| 789 |
|
| 790 |
# Token in the host position |
| 791 |
result = NotifyPushplus.parse_url("pushplus://{}".format(GOOD_TOKEN)) |
| 792 |
assert result["token"] == GOOD_TOKEN |
| 793 |
assert result["targets"] == [] |
| 794 |
|
| 795 |
# Token supplied as a ?token= query parameter |
| 796 |
result = NotifyPushplus.parse_url( |
| 797 |
"pushplus://?token={}".format(GOOD_TOKEN) |
| 798 |
) |
| 799 |
assert result["token"] == GOOD_TOKEN |
| 800 |
|
| 801 |
# Topic in the URL path becomes a plain targets entry |
| 802 |
result = NotifyPushplus.parse_url( |
| 803 |
"pushplus://{}/mygroup".format(GOOD_TOKEN) |
| 804 |
) |
| 805 |
assert "mygroup" in result["targets"] |
| 806 |
|
| 807 |
# Two topics in the path |
| 808 |
result = NotifyPushplus.parse_url( |
| 809 |
"pushplus://{}/grp1/grp2".format(GOOD_TOKEN) |
| 810 |
) |
| 811 |
assert "grp1" in result["targets"] |
| 812 |
assert "grp2" in result["targets"] |
| 813 |
|
| 814 |
# Backward compat: ?topic= adds a plain topic entry |
| 815 |
result = NotifyPushplus.parse_url( |
| 816 |
"pushplus://{}/?topic=mygroup".format(GOOD_TOKEN) |
| 817 |
) |
| 818 |
assert "mygroup" in result["targets"] |
| 819 |
|
| 820 |
# ?to= alias appends additional targets |
| 821 |
result = NotifyPushplus.parse_url( |
| 822 |
"pushplus://{}/?to=mygroup".format(GOOD_TOKEN) |
| 823 |
) |
| 824 |
assert "mygroup" in result["targets"] |
| 825 |
|
| 826 |
# Delivery channel via ?channel= |
| 827 |
result = NotifyPushplus.parse_url( |
| 828 |
"pushplus://{}?channel=mail".format(GOOD_TOKEN) |
| 829 |
) |
| 830 |
assert result.get("channel") == "mail" |
| 831 |
|
| 832 |
# Delivery channel via ?mode= alias |
| 833 |
result = NotifyPushplus.parse_url( |
| 834 |
"pushplus://{}?mode=sms".format(GOOD_TOKEN) |
| 835 |
) |
| 836 |
assert result.get("channel") == "sms" |
| 837 |
|
| 838 |
# channel= takes precedence over mode= when both are supplied |
| 839 |
result = NotifyPushplus.parse_url( |
| 840 |
"pushplus://{}?mode=sms&channel=mail".format(GOOD_TOKEN) |
| 841 |
) |
| 842 |
assert result.get("channel") == "mail" |
| 843 |
|
| 844 |
# Webhook name extracted into the "webhook" key from ?name= |
| 845 |
result = NotifyPushplus.parse_url( |
| 846 |
"pushplus://{}?channel=webhook&name=myhook".format(GOOD_TOKEN) |
| 847 |
) |
| 848 |
assert result.get("webhook") == "myhook" |
| 849 |
assert result.get("channel") == "webhook" |
| 850 |
|
| 851 |
# schema://name@token form: user@ -> webhook, implies channel=webhook |
| 852 |
result = NotifyPushplus.parse_url( |
| 853 |
"pushplus://myhook@{}".format(GOOD_TOKEN) |
| 854 |
) |
| 855 |
assert result.get("webhook") == "myhook" |
| 856 |
assert result.get("channel") == PushPlusChannel.WEBHOOK |
| 857 |
|
| 858 |
# Explicit ?channel= overrides the webhook implication from user@ |
| 859 |
result = NotifyPushplus.parse_url( |
| 860 |
"pushplus://myhook@{}?channel=mail".format(GOOD_TOKEN) |
| 861 |
) |
| 862 |
assert result.get("webhook") == "myhook" |
| 863 |
assert result.get("channel") == "mail" |
| 864 |
|
| 865 |
# ?name= takes precedence over user@ when both are supplied |
| 866 |
result = NotifyPushplus.parse_url( |
| 867 |
"pushplus://other@{}?channel=webhook&name=myhook".format(GOOD_TOKEN) |
| 868 |
) |
| 869 |
assert result.get("webhook") == "myhook" |
| 870 |
assert result.get("channel") == "webhook" |
| 871 |
|
| 872 |
|
| 873 |
def test_plugin_pushplus_parse_native_url(): |
| 874 |
"""NotifyPushplus() parse_native_url() handles all edge cases.""" |
| 875 |
|
| 876 |
# Basic native URL -- token extracted correctly |
| 877 |
result = NotifyPushplus.parse_native_url( |
| 878 |
"https://www.pushplus.plus/send?token={}".format(GOOD_TOKEN) |
| 879 |
) |
| 880 |
assert result is not None |
| 881 |
obj = NotifyPushplus(**result) |
| 882 |
assert obj.token == GOOD_TOKEN |
| 883 |
|
| 884 |
# Native URL with topic parameter preserved via round-trip |
| 885 |
result = NotifyPushplus.parse_native_url( |
| 886 |
"https://www.pushplus.plus/send?token={}&topic=mygroup".format( |
| 887 |
GOOD_TOKEN |
| 888 |
) |
| 889 |
) |
| 890 |
assert result is not None |
| 891 |
obj = NotifyPushplus(**result) |
| 892 |
assert "mygroup" in obj.topics |
| 893 |
|
| 894 |
# Native URL with channel parameter preserved |
| 895 |
result = NotifyPushplus.parse_native_url( |
| 896 |
"https://www.pushplus.plus/send?token={}&channel=mail".format( |
| 897 |
GOOD_TOKEN |
| 898 |
) |
| 899 |
) |
| 900 |
assert result is not None |
| 901 |
obj = NotifyPushplus(**result) |
| 902 |
assert obj.channel == PushPlusChannel.MAIL |
| 903 |
|
| 904 |
# Native URL with webhook name parameter preserved |
| 905 |
result = NotifyPushplus.parse_native_url( |
| 906 |
"https://www.pushplus.plus/send" |
| 907 |
"?token={}&channel=webhook&name=myhook".format(GOOD_TOKEN) |
| 908 |
) |
| 909 |
assert result is not None |
| 910 |
obj = NotifyPushplus(**result) |
| 911 |
assert obj.webhook == "myhook" |
| 912 |
assert obj.channel == PushPlusChannel.WEBHOOK |
| 913 |
|
| 914 |
# Native URL without a token -- not matched, returns None |
| 915 |
assert ( |
| 916 |
NotifyPushplus.parse_native_url( |
| 917 |
"https://www.pushplus.plus/send?channel=mail" |
| 918 |
) |
| 919 |
is None |
| 920 |
) |
| 921 |
|
| 922 |
# Completely different domain -- not matched, returns None |
| 923 |
assert ( |
| 924 |
NotifyPushplus.parse_native_url( |
| 925 |
"https://other.example.com/send?token={}".format(GOOD_TOKEN) |
| 926 |
) |
| 927 |
is None |
| 928 |
) |
| 929 |
|
| 930 |
# Native URL with no query string -- not matched, returns None |
| 931 |
assert ( |
| 932 |
NotifyPushplus.parse_native_url("https://www.pushplus.plus/send") |
| 933 |
is None |
| 934 |
) |
| 935 |
|
| 936 |
|
| 937 |
@mock.patch("requests.post") |
| 938 |
def test_plugin_pushplus_apprise_integration(mock_post): |
| 939 |
"""NotifyPushplus() integrates correctly with the Apprise object.""" |
| 940 |
|
| 941 |
response = mock.Mock() |
| 942 |
response.status_code = requests.codes.ok |
| 943 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 944 |
mock_post.return_value = response |
| 945 |
|
| 946 |
# Personal URL -- single notification |
| 947 |
aobj = Apprise() |
| 948 |
assert aobj.add("pushplus://{}".format(GOOD_TOKEN)) |
| 949 |
assert len(aobj) == 1 |
| 950 |
assert bool(aobj.notify(title="Test Title", body="Test Body")) is True |
| 951 |
assert mock_post.call_count == 1 |
| 952 |
|
| 953 |
mock_post.reset_mock() |
| 954 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 955 |
|
| 956 |
# Group URL with markdown format override |
| 957 |
aobj2 = Apprise() |
| 958 |
assert aobj2.add( |
| 959 |
"pushplus://{}/mygroup?format=markdown".format(GOOD_TOKEN) |
| 960 |
) |
| 961 |
assert bool(aobj2.notify(body="Group message")) is True |
| 962 |
assert mock_post.call_count == 1 |
| 963 |
payload = loads(mock_post.call_args[1]["data"]) |
| 964 |
# format=markdown must map to the PushPlus "markdown" template |
| 965 |
assert payload["template"] == "markdown" |
| 966 |
assert payload["topic"] == "mygroup" |
| 967 |
|
| 968 |
mock_post.reset_mock() |
| 969 |
response.content = GOOD_RESPONSE.encode("utf-8") |
| 970 |
|
| 971 |
# wecom:// schema alias -- channel=cp in payload |
| 972 |
aobj3 = Apprise() |
| 973 |
assert aobj3.add("wecom://{}".format(GOOD_TOKEN)) |
| 974 |
assert bool(aobj3.notify(body="WeCom message")) is True |
| 975 |
payload = loads(mock_post.call_args[1]["data"]) |
| 976 |
assert payload["channel"] == PushPlusChannel.WECOM |