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