| 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 |
| 9 |
# are met: |
| 10 |
# |
| 11 |
# 1. Redistributions of source code must retain the above copyright |
| 12 |
# notice, this list of conditions and the following disclaimer. |
| 13 |
# |
| 14 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
# notice, this list of conditions and the following disclaimer in |
| 16 |
# the documentation and/or other materials provided with the |
| 17 |
# distribution. |
| 18 |
# |
| 19 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 22 |
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 23 |
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 25 |
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 27 |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 29 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 |
# POSSIBILITY OF SUCH DAMAGE. |
| 31 |
|
| 32 |
# Disable logging for a cleaner testing output |
| 33 |
from json import dumps |
| 34 |
import logging |
| 35 |
from unittest import mock |
| 36 |
|
| 37 |
from helpers import AppriseURLTester |
| 38 |
import pytest |
| 39 |
import requests |
| 40 |
|
| 41 |
from apprise import Apprise |
| 42 |
from apprise.common import NotifyFormat |
| 43 |
from apprise.exception import AppriseImproperlyConfigured |
| 44 |
from apprise.plugins.wechat import ( |
| 45 |
WECHAT_ERROR_CODES, |
| 46 |
WECHAT_TOKEN_ERROR_CODES, |
| 47 |
NotifyWeChat, |
| 48 |
) |
| 49 |
|
| 50 |
logging.disable(logging.CRITICAL) |
| 51 |
|
| 52 |
# Fixed test credentials |
| 53 |
CORPID = "wwcorpid1234567890" |
| 54 |
CORPSECRET = "abcSecret9" |
| 55 |
AGENTID = "1000002" |
| 56 |
|
| 57 |
# A successful response serves double-duty: the token GET needs |
| 58 |
# access_token and expires_in; the message POST needs errcode == 0. |
| 59 |
GOOD_RESPONSE = dumps( |
| 60 |
{ |
| 61 |
"errcode": 0, |
| 62 |
"errmsg": "ok", |
| 63 |
"access_token": "TOKEN12345678", |
| 64 |
"expires_in": 7200, |
| 65 |
} |
| 66 |
) |
| 67 |
|
| 68 |
# pprint(CORPSECRET, privacy=True) -> first + "..." + last char |
| 69 |
# "abcSecret9" -> "a...9" |
| 70 |
PRIVACY_SECRET = "a...9" |
| 71 |
|
| 72 |
# Our Testing URLs |
| 73 |
apprise_url_tests = ( |
| 74 |
# ---------------------------------------------------------------- |
| 75 |
# Invalid / missing credential cases |
| 76 |
# ---------------------------------------------------------------- |
| 77 |
( |
| 78 |
"wechat://", |
| 79 |
{ |
| 80 |
# No credentials at all |
| 81 |
"instance": AppriseImproperlyConfigured, |
| 82 |
}, |
| 83 |
), |
| 84 |
( |
| 85 |
# Missing corpsecret and agentid |
| 86 |
"wechat://{}".format(CORPID), |
| 87 |
{ |
| 88 |
"instance": AppriseImproperlyConfigured, |
| 89 |
}, |
| 90 |
), |
| 91 |
( |
| 92 |
# Missing agentid (non-numeric host provided as port only) |
| 93 |
"wechat://{}:{}@".format(CORPID, CORPSECRET), |
| 94 |
{ |
| 95 |
"instance": AppriseImproperlyConfigured, |
| 96 |
}, |
| 97 |
), |
| 98 |
( |
| 99 |
# Non-numeric agentid |
| 100 |
"wechat://{}:{}@notanumber".format(CORPID, CORPSECRET), |
| 101 |
{ |
| 102 |
"instance": AppriseImproperlyConfigured, |
| 103 |
}, |
| 104 |
), |
| 105 |
# ---------------------------------------------------------------- |
| 106 |
# No targets -- plugin loads but send() returns False early |
| 107 |
# ---------------------------------------------------------------- |
| 108 |
( |
| 109 |
"wechat://{}:{}@{}".format(CORPID, CORPSECRET, AGENTID), |
| 110 |
{ |
| 111 |
"instance": NotifyWeChat, |
| 112 |
"notify_response": False, |
| 113 |
"privacy_url": "wechat://{}:{}@{}/".format( |
| 114 |
CORPID, PRIVACY_SECRET, AGENTID |
| 115 |
), |
| 116 |
}, |
| 117 |
), |
| 118 |
# ---------------------------------------------------------------- |
| 119 |
# @all broadcast -- sends to entire organisation |
| 120 |
# ---------------------------------------------------------------- |
| 121 |
( |
| 122 |
"wechat://{}:{}@{}/@all".format(CORPID, CORPSECRET, AGENTID), |
| 123 |
{ |
| 124 |
"instance": NotifyWeChat, |
| 125 |
"requests_response_text": GOOD_RESPONSE, |
| 126 |
"privacy_url": "wechat://{}:{}@{}/@all/".format( |
| 127 |
CORPID, PRIVACY_SECRET, AGENTID |
| 128 |
), |
| 129 |
}, |
| 130 |
), |
| 131 |
# ---------------------------------------------------------------- |
| 132 |
# Single user target (bare form -- no @ prefix) |
| 133 |
# ---------------------------------------------------------------- |
| 134 |
( |
| 135 |
"wechat://{}:{}@{}/johndoe".format(CORPID, CORPSECRET, AGENTID), |
| 136 |
{ |
| 137 |
"instance": NotifyWeChat, |
| 138 |
"requests_response_text": GOOD_RESPONSE, |
| 139 |
}, |
| 140 |
), |
| 141 |
# ---------------------------------------------------------------- |
| 142 |
# Single user target (@ prefixed form) |
| 143 |
# ---------------------------------------------------------------- |
| 144 |
( |
| 145 |
"wechat://{}:{}@{}/@johndoe".format(CORPID, CORPSECRET, AGENTID), |
| 146 |
{ |
| 147 |
"instance": NotifyWeChat, |
| 148 |
"requests_response_text": GOOD_RESPONSE, |
| 149 |
}, |
| 150 |
), |
| 151 |
# ---------------------------------------------------------------- |
| 152 |
# Multiple user targets |
| 153 |
# ---------------------------------------------------------------- |
| 154 |
( |
| 155 |
"wechat://{}:{}@{}/user1/user2/user3".format( |
| 156 |
CORPID, CORPSECRET, AGENTID |
| 157 |
), |
| 158 |
{ |
| 159 |
"instance": NotifyWeChat, |
| 160 |
"requests_response_text": GOOD_RESPONSE, |
| 161 |
}, |
| 162 |
), |
| 163 |
# ---------------------------------------------------------------- |
| 164 |
# Department target (%23 decodes to # prefix) |
| 165 |
# ---------------------------------------------------------------- |
| 166 |
( |
| 167 |
"wechat://{}:{}@{}/%23100".format(CORPID, CORPSECRET, AGENTID), |
| 168 |
{ |
| 169 |
"instance": NotifyWeChat, |
| 170 |
"requests_response_text": GOOD_RESPONSE, |
| 171 |
}, |
| 172 |
), |
| 173 |
# ---------------------------------------------------------------- |
| 174 |
# Tag target (+ prefix) |
| 175 |
# ---------------------------------------------------------------- |
| 176 |
( |
| 177 |
"wechat://{}:{}@{}/+7".format(CORPID, CORPSECRET, AGENTID), |
| 178 |
{ |
| 179 |
"instance": NotifyWeChat, |
| 180 |
"requests_response_text": GOOD_RESPONSE, |
| 181 |
}, |
| 182 |
), |
| 183 |
# ---------------------------------------------------------------- |
| 184 |
# Mixed recipients (user + department + tag) |
| 185 |
# ---------------------------------------------------------------- |
| 186 |
( |
| 187 |
"wechat://{}:{}@{}/john/%23200/+3".format(CORPID, CORPSECRET, AGENTID), |
| 188 |
{ |
| 189 |
"instance": NotifyWeChat, |
| 190 |
"requests_response_text": GOOD_RESPONSE, |
| 191 |
}, |
| 192 |
), |
| 193 |
# ---------------------------------------------------------------- |
| 194 |
# Targets supplied via ?to= query parameter |
| 195 |
# ---------------------------------------------------------------- |
| 196 |
( |
| 197 |
"wechat://{}:{}@{}?to=@all".format(CORPID, CORPSECRET, AGENTID), |
| 198 |
{ |
| 199 |
"instance": NotifyWeChat, |
| 200 |
"requests_response_text": GOOD_RESPONSE, |
| 201 |
}, |
| 202 |
), |
| 203 |
# ---------------------------------------------------------------- |
| 204 |
# Markdown format |
| 205 |
# ---------------------------------------------------------------- |
| 206 |
( |
| 207 |
"wechat://{}:{}@{}/@all?format=markdown".format( |
| 208 |
CORPID, CORPSECRET, AGENTID |
| 209 |
), |
| 210 |
{ |
| 211 |
"instance": NotifyWeChat, |
| 212 |
"requests_response_text": GOOD_RESPONSE, |
| 213 |
}, |
| 214 |
), |
| 215 |
# ---------------------------------------------------------------- |
| 216 |
# HTTP error responses -- token GET fails -> overall send fails |
| 217 |
# ---------------------------------------------------------------- |
| 218 |
( |
| 219 |
"wechat://{}:{}@{}/@all".format(CORPID, CORPSECRET, AGENTID), |
| 220 |
{ |
| 221 |
"instance": NotifyWeChat, |
| 222 |
"response": False, |
| 223 |
"requests_response_code": requests.codes.internal_server_error, |
| 224 |
}, |
| 225 |
), |
| 226 |
( |
| 227 |
"wechat://{}:{}@{}/@all".format(CORPID, CORPSECRET, AGENTID), |
| 228 |
{ |
| 229 |
"instance": NotifyWeChat, |
| 230 |
"response": False, |
| 231 |
"requests_response_code": 999, |
| 232 |
}, |
| 233 |
), |
| 234 |
# ---------------------------------------------------------------- |
| 235 |
# Network exception on all requests |
| 236 |
# ---------------------------------------------------------------- |
| 237 |
( |
| 238 |
"wechat://{}:{}@{}/@all".format(CORPID, CORPSECRET, AGENTID), |
| 239 |
{ |
| 240 |
"instance": NotifyWeChat, |
| 241 |
"test_requests_exceptions": True, |
| 242 |
}, |
| 243 |
), |
| 244 |
) |
| 245 |
|
| 246 |
|
| 247 |
def test_plugin_wechat_urls(): |
| 248 |
"""Run the standard Apprise URL tester for all wechat:// entries.""" |
| 249 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 250 |
|
| 251 |
|
| 252 |
def test_plugin_wechat_init(): |
| 253 |
"""Verify __init__ validation for required fields.""" |
| 254 |
|
| 255 |
# Valid construction |
| 256 |
obj = NotifyWeChat(corpid=CORPID, corpsecret=CORPSECRET, agentid=AGENTID) |
| 257 |
assert isinstance(obj, NotifyWeChat) |
| 258 |
|
| 259 |
# corpid is required |
| 260 |
with ( |
| 261 |
mock.patch("apprise.plugins.wechat.validate_regex", return_value=None), |
| 262 |
mock.patch.object(NotifyWeChat, "logger"), |
| 263 |
pytest.raises(AppriseImproperlyConfigured), |
| 264 |
): |
| 265 |
NotifyWeChat(corpid="", corpsecret=CORPSECRET, agentid=AGENTID) |
| 266 |
|
| 267 |
# corpsecret is required |
| 268 |
with pytest.raises(AppriseImproperlyConfigured): |
| 269 |
NotifyWeChat(corpid=CORPID, corpsecret="", agentid=AGENTID) |
| 270 |
|
| 271 |
# agentid must be numeric |
| 272 |
with pytest.raises(AppriseImproperlyConfigured): |
| 273 |
NotifyWeChat(corpid=CORPID, corpsecret=CORPSECRET, agentid="notnum") |
| 274 |
|
| 275 |
# agentid=None must be rejected |
| 276 |
with pytest.raises(AppriseImproperlyConfigured): |
| 277 |
NotifyWeChat(corpid=CORPID, corpsecret=CORPSECRET, agentid=None) |
| 278 |
|
| 279 |
# Invalid targets are silently dropped; valid ones are kept |
| 280 |
obj = NotifyWeChat( |
| 281 |
corpid=CORPID, |
| 282 |
corpsecret=CORPSECRET, |
| 283 |
agentid=AGENTID, |
| 284 |
targets=["validuser", "!!invalid!!", "#42", "+7"], |
| 285 |
) |
| 286 |
assert "validuser" in obj.users |
| 287 |
assert "42" in obj.departments |
| 288 |
assert "7" in obj.tag_ids |
| 289 |
assert "!!invalid!!" in obj.invalid_targets |
| 290 |
|
| 291 |
# The @ prefix on user IDs is optional on input; it is stripped |
| 292 |
# by the regex before storing so the API payload stays prefix-free. |
| 293 |
# The bare keyword "all" is normalised to "@all" (WeCom API form). |
| 294 |
obj = NotifyWeChat( |
| 295 |
corpid=CORPID, |
| 296 |
corpsecret=CORPSECRET, |
| 297 |
agentid=AGENTID, |
| 298 |
targets=["@johndoe", "@all", "all"], |
| 299 |
) |
| 300 |
assert "johndoe" in obj.users |
| 301 |
# Both "@all" and bare "all" must normalise to "@all" |
| 302 |
assert obj.users.count("@all") == 2 |
| 303 |
|
| 304 |
|
| 305 |
def test_plugin_wechat_url_round_trip(): |
| 306 |
"""Verify that url() and parse_url() form a lossless round-trip.""" |
| 307 |
|
| 308 |
obj1 = NotifyWeChat( |
| 309 |
corpid=CORPID, |
| 310 |
corpsecret=CORPSECRET, |
| 311 |
agentid=AGENTID, |
| 312 |
targets=["alice", "#10", "+2", "@all"], |
| 313 |
) |
| 314 |
url = obj1.url() |
| 315 |
result = NotifyWeChat.parse_url(url) |
| 316 |
assert result is not None |
| 317 |
obj2 = NotifyWeChat(**result) |
| 318 |
|
| 319 |
# Connection identity must be preserved |
| 320 |
assert obj1.url_identifier == obj2.url_identifier |
| 321 |
|
| 322 |
# Target counts must match |
| 323 |
assert len(obj1) == len(obj2) |
| 324 |
|
| 325 |
|
| 326 |
def test_plugin_wechat_url(): |
| 327 |
"""Verify url() output for various target combinations.""" |
| 328 |
|
| 329 |
# No targets |
| 330 |
obj = NotifyWeChat(corpid=CORPID, corpsecret=CORPSECRET, agentid=AGENTID) |
| 331 |
url = obj.url() |
| 332 |
assert CORPID in url |
| 333 |
assert CORPSECRET in url |
| 334 |
assert AGENTID in url |
| 335 |
|
| 336 |
# Privacy URL masks the corpsecret |
| 337 |
privacy = obj.url(privacy=True) |
| 338 |
assert CORPSECRET not in privacy |
| 339 |
assert PRIVACY_SECRET in privacy |
| 340 |
assert CORPID in privacy |
| 341 |
|
| 342 |
# Department gets encoded as %23 |
| 343 |
obj = NotifyWeChat( |
| 344 |
corpid=CORPID, |
| 345 |
corpsecret=CORPSECRET, |
| 346 |
agentid=AGENTID, |
| 347 |
targets=["#5"], |
| 348 |
) |
| 349 |
assert "%235" in obj.url() |
| 350 |
|
| 351 |
# Tag gets + prefix |
| 352 |
obj = NotifyWeChat( |
| 353 |
corpid=CORPID, |
| 354 |
corpsecret=CORPSECRET, |
| 355 |
agentid=AGENTID, |
| 356 |
targets=["+3"], |
| 357 |
) |
| 358 |
assert "+3" in obj.url() |
| 359 |
|
| 360 |
# @all is kept as-is |
| 361 |
obj = NotifyWeChat( |
| 362 |
corpid=CORPID, |
| 363 |
corpsecret=CORPSECRET, |
| 364 |
agentid=AGENTID, |
| 365 |
targets=["@all"], |
| 366 |
) |
| 367 |
assert "@all" in obj.url() |
| 368 |
|
| 369 |
# Regular user IDs are always emitted with @ prefix in the URL |
| 370 |
obj = NotifyWeChat( |
| 371 |
corpid=CORPID, |
| 372 |
corpsecret=CORPSECRET, |
| 373 |
agentid=AGENTID, |
| 374 |
targets=["alice"], |
| 375 |
) |
| 376 |
assert "@alice" in obj.url() |
| 377 |
|
| 378 |
# Invalid targets survive the round-trip |
| 379 |
obj = NotifyWeChat( |
| 380 |
corpid=CORPID, |
| 381 |
corpsecret=CORPSECRET, |
| 382 |
agentid=AGENTID, |
| 383 |
targets=["!!bad!!"], |
| 384 |
) |
| 385 |
assert obj.invalid_targets |
| 386 |
# url() must include the invalid target so it round-trips |
| 387 |
assert obj.url() != "" |
| 388 |
|
| 389 |
|
| 390 |
def test_plugin_wechat_url_identifier(): |
| 391 |
"""url_identifier must differ when credentials differ.""" |
| 392 |
|
| 393 |
obj1 = NotifyWeChat(corpid=CORPID, corpsecret=CORPSECRET, agentid=AGENTID) |
| 394 |
obj2 = NotifyWeChat( |
| 395 |
corpid="other_corp", corpsecret=CORPSECRET, agentid=AGENTID |
| 396 |
) |
| 397 |
obj3 = NotifyWeChat( |
| 398 |
corpid=CORPID, corpsecret="other_secret", agentid=AGENTID |
| 399 |
) |
| 400 |
obj4 = NotifyWeChat( |
| 401 |
corpid=CORPID, corpsecret=CORPSECRET, agentid="9999999" |
| 402 |
) |
| 403 |
|
| 404 |
assert obj1.url_identifier != obj2.url_identifier |
| 405 |
assert obj1.url_identifier != obj3.url_identifier |
| 406 |
assert obj1.url_identifier != obj4.url_identifier |
| 407 |
|
| 408 |
|
| 409 |
@mock.patch("requests.get") |
| 410 |
@mock.patch("requests.post") |
| 411 |
def test_plugin_wechat_send_ok(mock_post, mock_get): |
| 412 |
"""Successful end-to-end send: token GET + message POST both succeed.""" |
| 413 |
|
| 414 |
def _mk_resp(data, code=requests.codes.ok): |
| 415 |
r = mock.Mock() |
| 416 |
r.status_code = code |
| 417 |
r.content = dumps(data).encode("utf-8") |
| 418 |
return r |
| 419 |
|
| 420 |
# Token GET -> success + message POST -> success |
| 421 |
mock_get.return_value = _mk_resp( |
| 422 |
{ |
| 423 |
"errcode": 0, |
| 424 |
"errmsg": "ok", |
| 425 |
"access_token": "MYTOKEN", |
| 426 |
"expires_in": 7200, |
| 427 |
} |
| 428 |
) |
| 429 |
mock_post.return_value = _mk_resp({"errcode": 0, "errmsg": "ok"}) |
| 430 |
|
| 431 |
obj = NotifyWeChat( |
| 432 |
corpid=CORPID, |
| 433 |
corpsecret=CORPSECRET, |
| 434 |
agentid=AGENTID, |
| 435 |
targets=["@all"], |
| 436 |
) |
| 437 |
assert obj.send(body="Hello") is True |
| 438 |
assert mock_get.call_count == 1 |
| 439 |
assert mock_post.call_count == 1 |
| 440 |
|
| 441 |
|
| 442 |
@mock.patch("requests.get") |
| 443 |
@mock.patch("requests.post") |
| 444 |
def test_plugin_wechat_send_cached_token(mock_post, mock_get): |
| 445 |
"""Second send reuses the cached token without a new GET request.""" |
| 446 |
|
| 447 |
def _mk_resp(data, code=requests.codes.ok): |
| 448 |
r = mock.Mock() |
| 449 |
r.status_code = code |
| 450 |
r.content = dumps(data).encode("utf-8") |
| 451 |
return r |
| 452 |
|
| 453 |
mock_get.return_value = _mk_resp( |
| 454 |
{ |
| 455 |
"errcode": 0, |
| 456 |
"errmsg": "ok", |
| 457 |
"access_token": "CACHED_TOKEN", |
| 458 |
"expires_in": 7200, |
| 459 |
} |
| 460 |
) |
| 461 |
mock_post.return_value = _mk_resp({"errcode": 0, "errmsg": "ok"}) |
| 462 |
|
| 463 |
obj = NotifyWeChat( |
| 464 |
corpid=CORPID, |
| 465 |
corpsecret=CORPSECRET, |
| 466 |
agentid=AGENTID, |
| 467 |
targets=["@all"], |
| 468 |
) |
| 469 |
# First send fetches the token |
| 470 |
assert obj.send(body="First") is True |
| 471 |
assert mock_get.call_count == 1 |
| 472 |
|
| 473 |
# Second send reuses the cached token |
| 474 |
assert obj.send(body="Second") is True |
| 475 |
assert mock_get.call_count == 1 # still 1 -- no second GET |
| 476 |
|
| 477 |
|
| 478 |
@mock.patch("requests.get") |
| 479 |
@mock.patch("requests.post") |
| 480 |
def test_plugin_wechat_send_all_targets(mock_post, mock_get): |
| 481 |
"""A single send delivers to users, departments, and tags in one POST.""" |
| 482 |
|
| 483 |
def _mk_resp(data, code=requests.codes.ok): |
| 484 |
r = mock.Mock() |
| 485 |
r.status_code = code |
| 486 |
r.content = dumps(data).encode("utf-8") |
| 487 |
return r |
| 488 |
|
| 489 |
mock_get.return_value = _mk_resp( |
| 490 |
{ |
| 491 |
"errcode": 0, |
| 492 |
"errmsg": "ok", |
| 493 |
"access_token": "TOKEN", |
| 494 |
"expires_in": 7200, |
| 495 |
} |
| 496 |
) |
| 497 |
mock_post.return_value = _mk_resp({"errcode": 0, "errmsg": "ok"}) |
| 498 |
|
| 499 |
obj = NotifyWeChat( |
| 500 |
corpid=CORPID, |
| 501 |
corpsecret=CORPSECRET, |
| 502 |
agentid=AGENTID, |
| 503 |
targets=["alice", "bob", "#10", "#20", "+3", "+5"], |
| 504 |
) |
| 505 |
assert obj.send(body="Hello") is True |
| 506 |
assert mock_post.call_count == 1 |
| 507 |
|
| 508 |
# Verify payload recipients |
| 509 |
import json |
| 510 |
|
| 511 |
call_kwargs = mock_post.call_args |
| 512 |
payload = json.loads(call_kwargs[1]["data"]) |
| 513 |
assert set(payload["touser"].split("|")) == {"alice", "bob"} |
| 514 |
assert set(payload["toparty"].split("|")) == {"10", "20"} |
| 515 |
assert set(payload["totag"].split("|")) == {"3", "5"} |
| 516 |
|
| 517 |
|
| 518 |
@mock.patch("requests.get") |
| 519 |
@mock.patch("requests.post") |
| 520 |
def test_plugin_wechat_send_markdown(mock_post, mock_get): |
| 521 |
"""Markdown format produces msgtype=markdown in the payload.""" |
| 522 |
|
| 523 |
def _mk_resp(data, code=requests.codes.ok): |
| 524 |
r = mock.Mock() |
| 525 |
r.status_code = code |
| 526 |
r.content = dumps(data).encode("utf-8") |
| 527 |
return r |
| 528 |
|
| 529 |
mock_get.return_value = _mk_resp( |
| 530 |
{ |
| 531 |
"errcode": 0, |
| 532 |
"errmsg": "ok", |
| 533 |
"access_token": "TOKEN", |
| 534 |
"expires_in": 7200, |
| 535 |
} |
| 536 |
) |
| 537 |
mock_post.return_value = _mk_resp({"errcode": 0, "errmsg": "ok"}) |
| 538 |
|
| 539 |
obj = NotifyWeChat( |
| 540 |
corpid=CORPID, |
| 541 |
corpsecret=CORPSECRET, |
| 542 |
agentid=AGENTID, |
| 543 |
targets=["@all"], |
| 544 |
format=NotifyFormat.MARKDOWN, |
| 545 |
) |
| 546 |
assert obj.send(body="# Hello") is True |
| 547 |
|
| 548 |
import json |
| 549 |
|
| 550 |
payload = json.loads(mock_post.call_args[1]["data"]) |
| 551 |
assert payload["msgtype"] == "markdown" |
| 552 |
assert "markdown" in payload |
| 553 |
|
| 554 |
|
| 555 |
@mock.patch("requests.get") |
| 556 |
@mock.patch("requests.post") |
| 557 |
def test_plugin_wechat_send_no_targets(mock_post, mock_get): |
| 558 |
"""send() returns False immediately when no valid targets are present.""" |
| 559 |
|
| 560 |
obj = NotifyWeChat(corpid=CORPID, corpsecret=CORPSECRET, agentid=AGENTID) |
| 561 |
# No targets -> early return False, no network calls |
| 562 |
assert obj.send(body="Hello") is False |
| 563 |
assert mock_get.call_count == 0 |
| 564 |
assert mock_post.call_count == 0 |
| 565 |
|
| 566 |
|
| 567 |
@mock.patch("requests.get") |
| 568 |
@mock.patch("requests.post") |
| 569 |
def test_plugin_wechat_token_fetch_http_error(mock_post, mock_get): |
| 570 |
"""Token GET returning HTTP 500 causes send() to fail.""" |
| 571 |
|
| 572 |
r = mock.Mock() |
| 573 |
r.status_code = requests.codes.internal_server_error |
| 574 |
r.content = b"" |
| 575 |
mock_get.return_value = r |
| 576 |
|
| 577 |
obj = NotifyWeChat( |
| 578 |
corpid=CORPID, |
| 579 |
corpsecret=CORPSECRET, |
| 580 |
agentid=AGENTID, |
| 581 |
targets=["@all"], |
| 582 |
) |
| 583 |
assert obj.send(body="Hello") is False |
| 584 |
assert mock_get.call_count == 1 |
| 585 |
assert mock_post.call_count == 0 |
| 586 |
|
| 587 |
|
| 588 |
@mock.patch("requests.get") |
| 589 |
@mock.patch("requests.post") |
| 590 |
def test_plugin_wechat_token_fetch_api_error(mock_post, mock_get): |
| 591 |
"""Token GET returning errcode != 0 causes send() to fail.""" |
| 592 |
|
| 593 |
def _mk_resp(data, code=requests.codes.ok): |
| 594 |
r = mock.Mock() |
| 595 |
r.status_code = code |
| 596 |
r.content = dumps(data).encode("utf-8") |
| 597 |
return r |
| 598 |
|
| 599 |
# errcode 40001 -- invalid credential |
| 600 |
mock_get.return_value = _mk_resp( |
| 601 |
{"errcode": 40001, "errmsg": "invalid credential"} |
| 602 |
) |
| 603 |
|
| 604 |
obj = NotifyWeChat( |
| 605 |
corpid=CORPID, |
| 606 |
corpsecret=CORPSECRET, |
| 607 |
agentid=AGENTID, |
| 608 |
targets=["@all"], |
| 609 |
) |
| 610 |
assert obj.send(body="Hello") is False |
| 611 |
assert mock_post.call_count == 0 |
| 612 |
|
| 613 |
|
| 614 |
@mock.patch("requests.get") |
| 615 |
@mock.patch("requests.post") |
| 616 |
def test_plugin_wechat_token_fetch_no_token_field(mock_post, mock_get): |
| 617 |
"""Token GET returning errcode 0 but no access_token causes failure.""" |
| 618 |
|
| 619 |
def _mk_resp(data, code=requests.codes.ok): |
| 620 |
r = mock.Mock() |
| 621 |
r.status_code = code |
| 622 |
r.content = dumps(data).encode("utf-8") |
| 623 |
return r |
| 624 |
|
| 625 |
# errcode 0 but access_token is absent |
| 626 |
mock_get.return_value = _mk_resp({"errcode": 0, "errmsg": "ok"}) |
| 627 |
|
| 628 |
obj = NotifyWeChat( |
| 629 |
corpid=CORPID, |
| 630 |
corpsecret=CORPSECRET, |
| 631 |
agentid=AGENTID, |
| 632 |
targets=["@all"], |
| 633 |
) |
| 634 |
assert obj.send(body="Hello") is False |
| 635 |
|
| 636 |
|
| 637 |
@mock.patch("requests.get") |
| 638 |
@mock.patch("requests.post") |
| 639 |
def test_plugin_wechat_token_fetch_bad_json(mock_post, mock_get): |
| 640 |
"""Token GET returning unparsable JSON is handled gracefully.""" |
| 641 |
|
| 642 |
r = mock.Mock() |
| 643 |
r.status_code = requests.codes.ok |
| 644 |
r.content = b"{bad json" |
| 645 |
mock_get.return_value = r |
| 646 |
|
| 647 |
obj = NotifyWeChat( |
| 648 |
corpid=CORPID, |
| 649 |
corpsecret=CORPSECRET, |
| 650 |
agentid=AGENTID, |
| 651 |
targets=["@all"], |
| 652 |
) |
| 653 |
assert obj.send(body="Hello") is False |
| 654 |
|
| 655 |
|
| 656 |
@mock.patch("requests.get") |
| 657 |
@mock.patch("requests.post") |
| 658 |
def test_plugin_wechat_token_fetch_request_exception(mock_post, mock_get): |
| 659 |
"""RequestException during token GET causes send() to fail.""" |
| 660 |
|
| 661 |
mock_get.side_effect = requests.RequestException("connection error") |
| 662 |
|
| 663 |
obj = NotifyWeChat( |
| 664 |
corpid=CORPID, |
| 665 |
corpsecret=CORPSECRET, |
| 666 |
agentid=AGENTID, |
| 667 |
targets=["@all"], |
| 668 |
) |
| 669 |
assert obj.send(body="Hello") is False |
| 670 |
|
| 671 |
|
| 672 |
@mock.patch("requests.get") |
| 673 |
@mock.patch("requests.post") |
| 674 |
def test_plugin_wechat_send_http_error(mock_post, mock_get): |
| 675 |
"""Message POST returning HTTP 500 causes send() to fail.""" |
| 676 |
|
| 677 |
def _mk_resp(data, code=requests.codes.ok): |
| 678 |
r = mock.Mock() |
| 679 |
r.status_code = code |
| 680 |
r.content = dumps(data).encode("utf-8") |
| 681 |
return r |
| 682 |
|
| 683 |
mock_get.return_value = _mk_resp( |
| 684 |
{ |
| 685 |
"errcode": 0, |
| 686 |
"errmsg": "ok", |
| 687 |
"access_token": "TOKEN", |
| 688 |
"expires_in": 7200, |
| 689 |
} |
| 690 |
) |
| 691 |
mock_post.return_value = _mk_resp( |
| 692 |
{}, code=requests.codes.internal_server_error |
| 693 |
) |
| 694 |
mock_post.return_value.content = b"" |
| 695 |
|
| 696 |
obj = NotifyWeChat( |
| 697 |
corpid=CORPID, |
| 698 |
corpsecret=CORPSECRET, |
| 699 |
agentid=AGENTID, |
| 700 |
targets=["@all"], |
| 701 |
) |
| 702 |
assert obj.send(body="Hello") is False |
| 703 |
|
| 704 |
|
| 705 |
@mock.patch("requests.get") |
| 706 |
@mock.patch("requests.post") |
| 707 |
def test_plugin_wechat_send_api_error(mock_post, mock_get): |
| 708 |
"""Message POST returning errcode != 0 causes send() to fail.""" |
| 709 |
|
| 710 |
def _mk_resp(data, code=requests.codes.ok): |
| 711 |
r = mock.Mock() |
| 712 |
r.status_code = code |
| 713 |
r.content = dumps(data).encode("utf-8") |
| 714 |
return r |
| 715 |
|
| 716 |
mock_get.return_value = _mk_resp( |
| 717 |
{ |
| 718 |
"errcode": 0, |
| 719 |
"errmsg": "ok", |
| 720 |
"access_token": "TOKEN", |
| 721 |
"expires_in": 7200, |
| 722 |
} |
| 723 |
) |
| 724 |
# 81013 -- all recipients invalid |
| 725 |
mock_post.return_value = _mk_resp( |
| 726 |
{"errcode": 81013, "errmsg": "All recipients invalid"} |
| 727 |
) |
| 728 |
|
| 729 |
obj = NotifyWeChat( |
| 730 |
corpid=CORPID, |
| 731 |
corpsecret=CORPSECRET, |
| 732 |
agentid=AGENTID, |
| 733 |
targets=["@all"], |
| 734 |
) |
| 735 |
assert obj.send(body="Hello") is False |
| 736 |
|
| 737 |
|
| 738 |
@mock.patch("requests.get") |
| 739 |
@mock.patch("requests.post") |
| 740 |
def test_plugin_wechat_send_token_expiry_evicts_cache(mock_post, mock_get): |
| 741 |
"""A token-expired errcode from the POST evicts the cached token.""" |
| 742 |
|
| 743 |
def _mk_resp(data, code=requests.codes.ok): |
| 744 |
r = mock.Mock() |
| 745 |
r.status_code = code |
| 746 |
r.content = dumps(data).encode("utf-8") |
| 747 |
return r |
| 748 |
|
| 749 |
mock_get.return_value = _mk_resp( |
| 750 |
{ |
| 751 |
"errcode": 0, |
| 752 |
"errmsg": "ok", |
| 753 |
"access_token": "TOKEN", |
| 754 |
"expires_in": 7200, |
| 755 |
} |
| 756 |
) |
| 757 |
# POST returns 42001 -- token expired |
| 758 |
mock_post.return_value = _mk_resp( |
| 759 |
{"errcode": 42001, "errmsg": "access_token expired"} |
| 760 |
) |
| 761 |
|
| 762 |
obj = NotifyWeChat( |
| 763 |
corpid=CORPID, |
| 764 |
corpsecret=CORPSECRET, |
| 765 |
agentid=AGENTID, |
| 766 |
targets=["@all"], |
| 767 |
) |
| 768 |
assert obj.send(body="Hello") is False |
| 769 |
|
| 770 |
# Verify the store no longer holds the token |
| 771 |
assert obj.store.get("access_token") is None |
| 772 |
|
| 773 |
|
| 774 |
@mock.patch("requests.get") |
| 775 |
@mock.patch("requests.post") |
| 776 |
def test_plugin_wechat_send_token_expiry_all_codes(mock_post, mock_get): |
| 777 |
"""All WECHAT_TOKEN_ERROR_CODES cause the cached token to be evicted.""" |
| 778 |
|
| 779 |
def _mk_resp(data, code=requests.codes.ok): |
| 780 |
r = mock.Mock() |
| 781 |
r.status_code = code |
| 782 |
r.content = dumps(data).encode("utf-8") |
| 783 |
return r |
| 784 |
|
| 785 |
for errcode in WECHAT_TOKEN_ERROR_CODES: |
| 786 |
mock_get.reset_mock() |
| 787 |
mock_post.reset_mock() |
| 788 |
|
| 789 |
mock_get.return_value = _mk_resp( |
| 790 |
{ |
| 791 |
"errcode": 0, |
| 792 |
"errmsg": "ok", |
| 793 |
"access_token": "TOKEN", |
| 794 |
"expires_in": 7200, |
| 795 |
} |
| 796 |
) |
| 797 |
mock_post.return_value = _mk_resp( |
| 798 |
{"errcode": errcode, "errmsg": "token error"} |
| 799 |
) |
| 800 |
|
| 801 |
obj = NotifyWeChat( |
| 802 |
corpid=CORPID, |
| 803 |
corpsecret=CORPSECRET, |
| 804 |
agentid=AGENTID, |
| 805 |
targets=["@all"], |
| 806 |
) |
| 807 |
assert obj.send(body="Hello") is False |
| 808 |
assert obj.store.get("access_token") is None |
| 809 |
|
| 810 |
|
| 811 |
@mock.patch("requests.get") |
| 812 |
@mock.patch("requests.post") |
| 813 |
def test_plugin_wechat_send_bad_json(mock_post, mock_get): |
| 814 |
"""Message POST returning unparsable JSON is handled gracefully.""" |
| 815 |
|
| 816 |
def _mk_resp(data, code=requests.codes.ok): |
| 817 |
r = mock.Mock() |
| 818 |
r.status_code = code |
| 819 |
r.content = dumps(data).encode("utf-8") |
| 820 |
return r |
| 821 |
|
| 822 |
mock_get.return_value = _mk_resp( |
| 823 |
{ |
| 824 |
"errcode": 0, |
| 825 |
"errmsg": "ok", |
| 826 |
"access_token": "TOKEN", |
| 827 |
"expires_in": 7200, |
| 828 |
} |
| 829 |
) |
| 830 |
|
| 831 |
# POST returns unparsable body |
| 832 |
post_r = mock.Mock() |
| 833 |
post_r.status_code = requests.codes.ok |
| 834 |
post_r.content = b"{bad json" |
| 835 |
mock_post.return_value = post_r |
| 836 |
|
| 837 |
obj = NotifyWeChat( |
| 838 |
corpid=CORPID, |
| 839 |
corpsecret=CORPSECRET, |
| 840 |
agentid=AGENTID, |
| 841 |
targets=["@all"], |
| 842 |
) |
| 843 |
# Bad JSON -> content = {} -> errcode = -1 -> failure |
| 844 |
assert obj.send(body="Hello") is False |
| 845 |
|
| 846 |
|
| 847 |
@mock.patch("requests.get") |
| 848 |
@mock.patch("requests.post") |
| 849 |
def test_plugin_wechat_send_request_exception(mock_post, mock_get): |
| 850 |
"""RequestException during message POST causes send() to fail.""" |
| 851 |
|
| 852 |
def _mk_resp(data, code=requests.codes.ok): |
| 853 |
r = mock.Mock() |
| 854 |
r.status_code = code |
| 855 |
r.content = dumps(data).encode("utf-8") |
| 856 |
return r |
| 857 |
|
| 858 |
mock_get.return_value = _mk_resp( |
| 859 |
{ |
| 860 |
"errcode": 0, |
| 861 |
"errmsg": "ok", |
| 862 |
"access_token": "TOKEN", |
| 863 |
"expires_in": 7200, |
| 864 |
} |
| 865 |
) |
| 866 |
mock_post.side_effect = requests.RequestException("network error") |
| 867 |
|
| 868 |
obj = NotifyWeChat( |
| 869 |
corpid=CORPID, |
| 870 |
corpsecret=CORPSECRET, |
| 871 |
agentid=AGENTID, |
| 872 |
targets=["@all"], |
| 873 |
) |
| 874 |
assert obj.send(body="Hello") is False |
| 875 |
|
| 876 |
|
| 877 |
def test_plugin_wechat_parse_url(): |
| 878 |
"""Verify parse_url() handles all supported URL forms.""" |
| 879 |
|
| 880 |
# Standard form: corpid:corpsecret@agentid/targets |
| 881 |
url = "wechat://{}:{}@{}/alice".format(CORPID, CORPSECRET, AGENTID) |
| 882 |
result = NotifyWeChat.parse_url(url) |
| 883 |
assert result is not None |
| 884 |
assert result["corpid"] == CORPID |
| 885 |
assert result["corpsecret"] == CORPSECRET |
| 886 |
assert result["agentid"] == AGENTID |
| 887 |
assert "alice" in result["targets"] |
| 888 |
|
| 889 |
# Department target (%23 decodes to #) |
| 890 |
url = "wechat://{}:{}@{}/%23999".format(CORPID, CORPSECRET, AGENTID) |
| 891 |
result = NotifyWeChat.parse_url(url) |
| 892 |
assert "#999" in result["targets"] |
| 893 |
|
| 894 |
# Tag target |
| 895 |
url = "wechat://{}:{}@{}/+42".format(CORPID, CORPSECRET, AGENTID) |
| 896 |
result = NotifyWeChat.parse_url(url) |
| 897 |
assert "+42" in result["targets"] |
| 898 |
|
| 899 |
# ?to= adds targets |
| 900 |
url = "wechat://{}:{}@{}?to=@all".format(CORPID, CORPSECRET, AGENTID) |
| 901 |
result = NotifyWeChat.parse_url(url) |
| 902 |
assert "@all" in result["targets"] |
| 903 |
|
| 904 |
# None and non-string inputs return None |
| 905 |
assert NotifyWeChat.parse_url(None) is None |
| 906 |
|
| 907 |
|
| 908 |
def test_plugin_wechat_parse_native_url(): |
| 909 |
"""parse_native_url() always returns None -- no native URL form.""" |
| 910 |
assert ( |
| 911 |
NotifyWeChat.parse_native_url("https://qyapi.weixin.qq.com/any/url") |
| 912 |
is None |
| 913 |
) |
| 914 |
|
| 915 |
|
| 916 |
def test_plugin_wechat_error_codes(): |
| 917 |
"""Verify the error code constants are populated correctly.""" |
| 918 |
assert 0 in WECHAT_ERROR_CODES |
| 919 |
assert 40001 in WECHAT_TOKEN_ERROR_CODES |
| 920 |
assert 40014 in WECHAT_TOKEN_ERROR_CODES |
| 921 |
assert 42001 in WECHAT_TOKEN_ERROR_CODES |
| 922 |
|
| 923 |
|
| 924 |
@mock.patch("requests.get") |
| 925 |
@mock.patch("requests.post") |
| 926 |
def test_plugin_wechat_apprise_integration(mock_post, mock_get): |
| 927 |
"""End-to-end Apprise().notify() integration test.""" |
| 928 |
|
| 929 |
def _mk_resp(data, code=requests.codes.ok): |
| 930 |
r = mock.Mock() |
| 931 |
r.status_code = code |
| 932 |
r.content = dumps(data).encode("utf-8") |
| 933 |
return r |
| 934 |
|
| 935 |
mock_get.return_value = _mk_resp( |
| 936 |
{ |
| 937 |
"errcode": 0, |
| 938 |
"errmsg": "ok", |
| 939 |
"access_token": "TOKEN", |
| 940 |
"expires_in": 7200, |
| 941 |
} |
| 942 |
) |
| 943 |
mock_post.return_value = _mk_resp({"errcode": 0, "errmsg": "ok"}) |
| 944 |
|
| 945 |
app = Apprise() |
| 946 |
url = "wechat://{}:{}@{}/@all".format(CORPID, CORPSECRET, AGENTID) |
| 947 |
assert app.add(url) is True |
| 948 |
assert bool(app.notify(title="T", body="B")) is True |
| 949 |
|
| 950 |
|
| 951 |
@mock.patch("requests.get") |
| 952 |
@mock.patch("requests.post") |
| 953 |
def test_plugin_wechat_send_unknown_api_error(mock_post, mock_get): |
| 954 |
"""An unknown errcode falls back to the errmsg field in the response.""" |
| 955 |
|
| 956 |
def _mk_resp(data, code=requests.codes.ok): |
| 957 |
r = mock.Mock() |
| 958 |
r.status_code = code |
| 959 |
r.content = dumps(data).encode("utf-8") |
| 960 |
return r |
| 961 |
|
| 962 |
mock_get.return_value = _mk_resp( |
| 963 |
{ |
| 964 |
"errcode": 0, |
| 965 |
"errmsg": "ok", |
| 966 |
"access_token": "TOKEN", |
| 967 |
"expires_in": 7200, |
| 968 |
} |
| 969 |
) |
| 970 |
# Use an error code not in WECHAT_ERROR_CODES |
| 971 |
mock_post.return_value = _mk_resp( |
| 972 |
{"errcode": 99999, "errmsg": "custom error message"} |
| 973 |
) |
| 974 |
|
| 975 |
obj = NotifyWeChat( |
| 976 |
corpid=CORPID, |
| 977 |
corpsecret=CORPSECRET, |
| 978 |
agentid=AGENTID, |
| 979 |
targets=["@all"], |
| 980 |
) |
| 981 |
assert obj.send(body="Hello") is False |
| 982 |
|
| 983 |
|
| 984 |
@mock.patch("requests.get") |
| 985 |
@mock.patch("requests.post") |
| 986 |
def test_plugin_wechat_token_unknown_api_error(mock_post, mock_get): |
| 987 |
"""Unknown errcode from token GET falls back to errmsg field.""" |
| 988 |
|
| 989 |
r = mock.Mock() |
| 990 |
r.status_code = requests.codes.ok |
| 991 |
r.content = dumps( |
| 992 |
{"errcode": 99999, "errmsg": "custom token error"} |
| 993 |
).encode("utf-8") |
| 994 |
mock_get.return_value = r |
| 995 |
|
| 996 |
obj = NotifyWeChat( |
| 997 |
corpid=CORPID, |
| 998 |
corpsecret=CORPSECRET, |
| 999 |
agentid=AGENTID, |
| 1000 |
targets=["@all"], |
| 1001 |
) |
| 1002 |
assert obj.send(body="Hello") is False |