| 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 |
from json import loads |
| 30 |
import logging |
| 31 |
import os |
| 32 |
from unittest import mock |
| 33 |
|
| 34 |
from helpers import AppriseURLTester |
| 35 |
import pytest |
| 36 |
import requests |
| 37 |
|
| 38 |
from apprise import Apprise, AppriseAttachment, NotifyFormat, NotifyType |
| 39 |
from apprise.plugins.webexteams import ( |
| 40 |
NotifyWebexTeams, |
| 41 |
WebexTeamsMode, |
| 42 |
) |
| 43 |
|
| 44 |
logging.disable(logging.CRITICAL) |
| 45 |
|
| 46 |
|
| 47 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 48 |
|
| 49 |
|
| 50 |
WEBHOOK_TOKEN = "a" * 80 |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
BOT_TOKEN = "Bc10" * 50 |
| 57 |
|
| 58 |
|
| 59 |
ROOM_ID = "Y2lzY29zcGFyazovL3VzL1JPTU9NLzEyMzQ1Njc4OTAxMjM0" |
| 60 |
ROOM_ID2 = "Y2lzY29zcGFyazovL3VzL1JPTU9NL2FiY2RlZjEyMzQ1Njc4" |
| 61 |
|
| 62 |
|
| 63 |
apprise_url_tests = ( |
| 64 |
( |
| 65 |
"wxteams://", |
| 66 |
{ |
| 67 |
|
| 68 |
"instance": TypeError, |
| 69 |
}, |
| 70 |
), |
| 71 |
( |
| 72 |
"wxteams://:@/", |
| 73 |
{ |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
"instance": TypeError, |
| 78 |
}, |
| 79 |
), |
| 80 |
( |
| 81 |
"wxteams://{}".format("a" * 80), |
| 82 |
{ |
| 83 |
|
| 84 |
"instance": NotifyWebexTeams, |
| 85 |
|
| 86 |
"privacy_url": "wxteams://a...a/", |
| 87 |
}, |
| 88 |
), |
| 89 |
( |
| 90 |
"wxteams://?token={}".format("a" * 80), |
| 91 |
{ |
| 92 |
|
| 93 |
"instance": NotifyWebexTeams, |
| 94 |
|
| 95 |
"privacy_url": "wxteams://a...a/", |
| 96 |
}, |
| 97 |
), |
| 98 |
( |
| 99 |
"webex://{}".format("a" * 140), |
| 100 |
{ |
| 101 |
|
| 102 |
"instance": NotifyWebexTeams, |
| 103 |
|
| 104 |
"privacy_url": "wxteams://a...a/", |
| 105 |
}, |
| 106 |
), |
| 107 |
|
| 108 |
( |
| 109 |
"https://api.ciscospark.com/v1/webhooks/incoming/{}".format("a" * 80), |
| 110 |
{ |
| 111 |
|
| 112 |
"instance": NotifyWebexTeams, |
| 113 |
}, |
| 114 |
), |
| 115 |
|
| 116 |
( |
| 117 |
"https://webexapis.com/v1/webhooks/incoming/{}".format("a" * 100), |
| 118 |
{ |
| 119 |
|
| 120 |
"instance": NotifyWebexTeams, |
| 121 |
}, |
| 122 |
), |
| 123 |
|
| 124 |
( |
| 125 |
"https://api.ciscospark.com/v1/webhooks/incoming/{}" |
| 126 |
"?format=text".format("a" * 80), |
| 127 |
{ |
| 128 |
|
| 129 |
"instance": NotifyWebexTeams, |
| 130 |
}, |
| 131 |
), |
| 132 |
|
| 133 |
( |
| 134 |
"wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID), |
| 135 |
{ |
| 136 |
"instance": NotifyWebexTeams, |
| 137 |
"privacy_url": "wxteams://B...0/", |
| 138 |
}, |
| 139 |
), |
| 140 |
|
| 141 |
( |
| 142 |
"wxteams://{}?mode=bot".format("a" * 80), |
| 143 |
{ |
| 144 |
"instance": NotifyWebexTeams, |
| 145 |
|
| 146 |
"notify_response": False, |
| 147 |
}, |
| 148 |
), |
| 149 |
|
| 150 |
( |
| 151 |
"wxteams://{}?mode=webhook".format("a" * 80), |
| 152 |
{ |
| 153 |
"instance": NotifyWebexTeams, |
| 154 |
"privacy_url": "wxteams://a...a/", |
| 155 |
}, |
| 156 |
), |
| 157 |
|
| 158 |
( |
| 159 |
"wxteams://{}?mode=invalid".format("a" * 80), |
| 160 |
{ |
| 161 |
"instance": TypeError, |
| 162 |
}, |
| 163 |
), |
| 164 |
|
| 165 |
( |
| 166 |
"wxteams://{}".format("a" * 80), |
| 167 |
{ |
| 168 |
"instance": NotifyWebexTeams, |
| 169 |
"response": False, |
| 170 |
"requests_response_code": (requests.codes.internal_server_error), |
| 171 |
}, |
| 172 |
), |
| 173 |
|
| 174 |
( |
| 175 |
"wxteams://{}".format("a" * 80), |
| 176 |
{ |
| 177 |
"instance": NotifyWebexTeams, |
| 178 |
|
| 179 |
"response": False, |
| 180 |
"requests_response_code": 999, |
| 181 |
}, |
| 182 |
), |
| 183 |
|
| 184 |
( |
| 185 |
"wxteams://{}".format("a" * 80), |
| 186 |
{ |
| 187 |
"instance": NotifyWebexTeams, |
| 188 |
|
| 189 |
|
| 190 |
"test_requests_exceptions": True, |
| 191 |
}, |
| 192 |
), |
| 193 |
|
| 194 |
( |
| 195 |
"wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID), |
| 196 |
{ |
| 197 |
"instance": NotifyWebexTeams, |
| 198 |
"response": False, |
| 199 |
"requests_response_code": (requests.codes.internal_server_error), |
| 200 |
}, |
| 201 |
), |
| 202 |
|
| 203 |
( |
| 204 |
"wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID), |
| 205 |
{ |
| 206 |
"instance": NotifyWebexTeams, |
| 207 |
"response": False, |
| 208 |
"requests_response_code": 999, |
| 209 |
}, |
| 210 |
), |
| 211 |
|
| 212 |
( |
| 213 |
"wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID), |
| 214 |
{ |
| 215 |
"instance": NotifyWebexTeams, |
| 216 |
"test_requests_exceptions": True, |
| 217 |
}, |
| 218 |
), |
| 219 |
) |
| 220 |
|
| 221 |
|
| 222 |
def test_plugin_webex_teams_urls(): |
| 223 |
"""NotifyWebexTeams() Apprise URLs.""" |
| 224 |
|
| 225 |
|
| 226 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 227 |
|
| 228 |
|
| 229 |
def test_plugin_webex_teams_webhook_mode(): |
| 230 |
"""NotifyWebexTeams() Webhook mode direct tests.""" |
| 231 |
|
| 232 |
|
| 233 |
token = "b" * 80 |
| 234 |
|
| 235 |
obj = NotifyWebexTeams(token=token) |
| 236 |
assert isinstance(obj, NotifyWebexTeams) |
| 237 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 238 |
|
| 239 |
|
| 240 |
url = obj.url() |
| 241 |
assert "wxteams://" in url |
| 242 |
assert "mode=webhook" in url |
| 243 |
|
| 244 |
|
| 245 |
result = NotifyWebexTeams.parse_url(url) |
| 246 |
assert result is not None |
| 247 |
obj2 = NotifyWebexTeams(**result) |
| 248 |
assert obj2.mode == WebexTeamsMode.WEBHOOK |
| 249 |
assert obj2.token == token |
| 250 |
|
| 251 |
|
| 252 |
priv = obj.url(privacy=True) |
| 253 |
assert obj.token not in priv |
| 254 |
|
| 255 |
|
| 256 |
assert obj.url_identifier == ( |
| 257 |
NotifyWebexTeams.secure_protocol[0], |
| 258 |
token, |
| 259 |
) |
| 260 |
|
| 261 |
|
| 262 |
assert NotifyWebexTeams(token="c" * 80) |
| 263 |
|
| 264 |
assert NotifyWebexTeams(token="c" * 160) |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
obj_short = NotifyWebexTeams(token="c" * 79) |
| 269 |
assert obj_short.mode == WebexTeamsMode.BOT |
| 270 |
assert obj_short.send(body="test") is False |
| 271 |
|
| 272 |
|
| 273 |
obj_long = NotifyWebexTeams(token="c" * 161) |
| 274 |
assert obj_long.mode == WebexTeamsMode.BOT |
| 275 |
assert obj_long.send(body="test") is False |
| 276 |
|
| 277 |
|
| 278 |
with pytest.raises(TypeError): |
| 279 |
NotifyWebexTeams(token=None) |
| 280 |
|
| 281 |
|
| 282 |
with pytest.raises(TypeError): |
| 283 |
NotifyWebexTeams(token="") |
| 284 |
|
| 285 |
|
| 286 |
obj_hyph = NotifyWebexTeams(token="a-b-" * 20) |
| 287 |
assert obj_hyph.mode == WebexTeamsMode.BOT |
| 288 |
assert obj_hyph.send(body="test") is False |
| 289 |
|
| 290 |
|
| 291 |
def test_plugin_webex_teams_bot_mode(): |
| 292 |
"""NotifyWebexTeams() Bot mode direct tests.""" |
| 293 |
|
| 294 |
obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID]) |
| 295 |
assert isinstance(obj, NotifyWebexTeams) |
| 296 |
assert obj.mode == WebexTeamsMode.BOT |
| 297 |
assert obj.access_token == BOT_TOKEN |
| 298 |
assert ROOM_ID in obj.targets |
| 299 |
|
| 300 |
|
| 301 |
url = obj.url() |
| 302 |
assert "wxteams://" in url |
| 303 |
assert "mode=bot" in url |
| 304 |
|
| 305 |
result = NotifyWebexTeams.parse_url(url) |
| 306 |
assert result is not None |
| 307 |
obj2 = NotifyWebexTeams(**result) |
| 308 |
assert obj2.mode == WebexTeamsMode.BOT |
| 309 |
assert obj2.access_token == BOT_TOKEN |
| 310 |
assert ROOM_ID in obj2.targets |
| 311 |
|
| 312 |
|
| 313 |
priv = obj.url(privacy=True) |
| 314 |
assert BOT_TOKEN not in priv |
| 315 |
|
| 316 |
|
| 317 |
assert obj.url_identifier == ( |
| 318 |
NotifyWebexTeams.secure_protocol[0], |
| 319 |
BOT_TOKEN, |
| 320 |
) |
| 321 |
|
| 322 |
|
| 323 |
obj3 = NotifyWebexTeams( |
| 324 |
access_token=BOT_TOKEN, targets=[ROOM_ID, ROOM_ID2] |
| 325 |
) |
| 326 |
assert len(obj3.targets) == 2 |
| 327 |
|
| 328 |
|
| 329 |
obj_no_rooms = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[]) |
| 330 |
assert isinstance(obj_no_rooms, NotifyWebexTeams) |
| 331 |
assert obj_no_rooms.send(body="test") is False |
| 332 |
|
| 333 |
|
| 334 |
with pytest.raises(TypeError): |
| 335 |
NotifyWebexTeams(access_token=None, targets=[ROOM_ID]) |
| 336 |
|
| 337 |
|
| 338 |
with pytest.raises(TypeError): |
| 339 |
NotifyWebexTeams(mode="bot", targets=[ROOM_ID]) |
| 340 |
|
| 341 |
|
| 342 |
obj4 = NotifyWebexTeams(token="a" * 80, mode="bot", targets=[ROOM_ID]) |
| 343 |
assert obj4.mode == WebexTeamsMode.BOT |
| 344 |
|
| 345 |
assert obj4.access_token == "a" * 80 |
| 346 |
|
| 347 |
|
| 348 |
with pytest.raises(TypeError): |
| 349 |
NotifyWebexTeams( |
| 350 |
access_token=BOT_TOKEN, |
| 351 |
targets=[ROOM_ID], |
| 352 |
mode="invalid", |
| 353 |
) |
| 354 |
|
| 355 |
|
| 356 |
def test_plugin_webex_teams_body_maxlen(): |
| 357 |
"""NotifyWebexTeams() body_maxlen varies by mode.""" |
| 358 |
|
| 359 |
|
| 360 |
obj = NotifyWebexTeams(token=WEBHOOK_TOKEN) |
| 361 |
assert obj.body_maxlen == 1000 |
| 362 |
|
| 363 |
|
| 364 |
obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID]) |
| 365 |
assert obj.body_maxlen == 7439 |
| 366 |
|
| 367 |
|
| 368 |
def test_plugin_webex_teams_mode_detection(): |
| 369 |
"""NotifyWebexTeams() auto-detects mode from token format.""" |
| 370 |
|
| 371 |
|
| 372 |
obj = NotifyWebexTeams(token="a" * 80) |
| 373 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
obj = NotifyWebexTeams(token="A" * 80) |
| 378 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 379 |
|
| 380 |
|
| 381 |
obj = NotifyWebexTeams(token="a" * 200, targets=[ROOM_ID]) |
| 382 |
assert obj.mode == WebexTeamsMode.BOT |
| 383 |
|
| 384 |
|
| 385 |
obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID]) |
| 386 |
assert obj.mode == WebexTeamsMode.BOT |
| 387 |
|
| 388 |
|
| 389 |
def test_plugin_webex_teams_url_parsing(): |
| 390 |
"""NotifyWebexTeams() URL parsing edge cases.""" |
| 391 |
|
| 392 |
|
| 393 |
result = NotifyWebexTeams.parse_url("wxteams://?token={}".format("a" * 80)) |
| 394 |
assert result is not None |
| 395 |
obj = NotifyWebexTeams(**result) |
| 396 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 397 |
assert obj.token == "a" * 80 |
| 398 |
|
| 399 |
|
| 400 |
result = NotifyWebexTeams.parse_url( |
| 401 |
"wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID) |
| 402 |
) |
| 403 |
assert result is not None |
| 404 |
obj = NotifyWebexTeams(**result) |
| 405 |
assert obj.mode == WebexTeamsMode.BOT |
| 406 |
assert ROOM_ID in obj.targets |
| 407 |
|
| 408 |
|
| 409 |
result = NotifyWebexTeams.parse_url( |
| 410 |
"wxteams://{}/{}/{}".format(BOT_TOKEN, ROOM_ID, ROOM_ID2) |
| 411 |
) |
| 412 |
assert result is not None |
| 413 |
obj = NotifyWebexTeams(**result) |
| 414 |
assert obj.mode == WebexTeamsMode.BOT |
| 415 |
assert len(obj.targets) == 2 |
| 416 |
|
| 417 |
|
| 418 |
result = NotifyWebexTeams.parse_url( |
| 419 |
"wxteams://{}?to={}&mode=bot".format(BOT_TOKEN, ROOM_ID) |
| 420 |
) |
| 421 |
assert result is not None |
| 422 |
obj = NotifyWebexTeams(**result) |
| 423 |
assert obj.mode == WebexTeamsMode.BOT |
| 424 |
assert ROOM_ID in obj.targets |
| 425 |
|
| 426 |
|
| 427 |
result = NotifyWebexTeams.parse_native_url( |
| 428 |
"https://api.ciscospark.com/v1/webhooks/incoming/{}".format("a" * 80) |
| 429 |
) |
| 430 |
assert result is not None |
| 431 |
obj = NotifyWebexTeams(**result) |
| 432 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 433 |
|
| 434 |
|
| 435 |
result = NotifyWebexTeams.parse_native_url( |
| 436 |
"https://webexapis.com/v1/webhooks/incoming/{}".format("a" * 100) |
| 437 |
) |
| 438 |
assert result is not None |
| 439 |
obj = NotifyWebexTeams(**result) |
| 440 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 441 |
|
| 442 |
|
| 443 |
assert ( |
| 444 |
NotifyWebexTeams.parse_native_url( |
| 445 |
"https://example.com/not/a/webex/url" |
| 446 |
) |
| 447 |
is None |
| 448 |
) |
| 449 |
|
| 450 |
|
| 451 |
result = NotifyWebexTeams.parse_url( |
| 452 |
"wxteams://{}?mode=webhook".format("a" * 80) |
| 453 |
) |
| 454 |
assert result is not None |
| 455 |
obj = NotifyWebexTeams(**result) |
| 456 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 457 |
|
| 458 |
|
| 459 |
@mock.patch("requests.post") |
| 460 |
def test_plugin_webex_teams_webhook_send(mock_post): |
| 461 |
"""NotifyWebexTeams() Webhook send() coverage.""" |
| 462 |
|
| 463 |
|
| 464 |
response = mock.Mock() |
| 465 |
response.status_code = requests.codes.ok |
| 466 |
response.content = b"" |
| 467 |
mock_post.return_value = response |
| 468 |
|
| 469 |
obj = NotifyWebexTeams(token=WEBHOOK_TOKEN) |
| 470 |
|
| 471 |
|
| 472 |
assert obj.send(body="test message") is True |
| 473 |
assert mock_post.call_count == 1 |
| 474 |
call_url = mock_post.call_args[0][0] |
| 475 |
assert "webhooks/incoming" in call_url |
| 476 |
assert WEBHOOK_TOKEN in call_url |
| 477 |
|
| 478 |
|
| 479 |
headers = mock_post.call_args[1]["headers"] |
| 480 |
assert headers["Content-Type"] == "application/json" |
| 481 |
|
| 482 |
mock_post.reset_mock() |
| 483 |
|
| 484 |
|
| 485 |
response.status_code = requests.codes.no_content |
| 486 |
assert obj.send(body="test") is True |
| 487 |
|
| 488 |
mock_post.reset_mock() |
| 489 |
|
| 490 |
|
| 491 |
response.status_code = requests.codes.internal_server_error |
| 492 |
assert obj.send(body="test") is False |
| 493 |
|
| 494 |
mock_post.reset_mock() |
| 495 |
|
| 496 |
|
| 497 |
response.status_code = 999 |
| 498 |
assert obj.send(body="test") is False |
| 499 |
|
| 500 |
mock_post.reset_mock() |
| 501 |
|
| 502 |
|
| 503 |
mock_post.side_effect = requests.RequestException() |
| 504 |
assert obj.send(body="test") is False |
| 505 |
|
| 506 |
mock_post.side_effect = None |
| 507 |
mock_post.return_value = response |
| 508 |
response.status_code = requests.codes.ok |
| 509 |
|
| 510 |
|
| 511 |
mock_post.reset_mock() |
| 512 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 513 |
attach = AppriseAttachment(path) |
| 514 |
assert obj.send(body="test", attach=attach) is True |
| 515 |
|
| 516 |
assert mock_post.call_count == 1 |
| 517 |
|
| 518 |
|
| 519 |
@mock.patch("requests.post") |
| 520 |
def test_plugin_webex_teams_bot_send(mock_post): |
| 521 |
"""NotifyWebexTeams() Bot send() text-only coverage.""" |
| 522 |
|
| 523 |
response = mock.Mock() |
| 524 |
response.status_code = requests.codes.ok |
| 525 |
response.content = b"" |
| 526 |
mock_post.return_value = response |
| 527 |
|
| 528 |
obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID]) |
| 529 |
|
| 530 |
|
| 531 |
assert obj.send(body="hello from bot") is True |
| 532 |
assert mock_post.call_count == 1 |
| 533 |
call_url = mock_post.call_args[0][0] |
| 534 |
assert call_url == "https://webexapis.com/v1/messages" |
| 535 |
|
| 536 |
|
| 537 |
headers = mock_post.call_args[1]["headers"] |
| 538 |
assert headers["Authorization"] == f"Bearer {BOT_TOKEN}" |
| 539 |
assert headers["Content-Type"] == "application/json" |
| 540 |
|
| 541 |
mock_post.reset_mock() |
| 542 |
|
| 543 |
|
| 544 |
obj2 = NotifyWebexTeams( |
| 545 |
access_token=BOT_TOKEN, targets=[ROOM_ID, ROOM_ID2] |
| 546 |
) |
| 547 |
assert obj2.send(body="broadcast") is True |
| 548 |
assert mock_post.call_count == 2 |
| 549 |
|
| 550 |
mock_post.reset_mock() |
| 551 |
|
| 552 |
|
| 553 |
response.status_code = requests.codes.internal_server_error |
| 554 |
assert obj.send(body="test") is False |
| 555 |
|
| 556 |
mock_post.reset_mock() |
| 557 |
|
| 558 |
response.status_code = 999 |
| 559 |
assert obj.send(body="test") is False |
| 560 |
|
| 561 |
mock_post.reset_mock() |
| 562 |
|
| 563 |
|
| 564 |
mock_post.side_effect = requests.RequestException() |
| 565 |
assert obj.send(body="test") is False |
| 566 |
|
| 567 |
mock_post.side_effect = None |
| 568 |
response.status_code = requests.codes.ok |
| 569 |
mock_post.return_value = response |
| 570 |
|
| 571 |
|
| 572 |
mock_post.reset_mock() |
| 573 |
fail_response = mock.Mock() |
| 574 |
fail_response.status_code = requests.codes.internal_server_error |
| 575 |
fail_response.content = b"" |
| 576 |
|
| 577 |
mock_post.side_effect = [response, fail_response] |
| 578 |
obj2 = NotifyWebexTeams( |
| 579 |
access_token=BOT_TOKEN, targets=[ROOM_ID, ROOM_ID2] |
| 580 |
) |
| 581 |
assert obj2.send(body="test") is False |
| 582 |
|
| 583 |
|
| 584 |
@mock.patch("requests.post") |
| 585 |
def test_plugin_webex_teams_bot_attachments(mock_post): |
| 586 |
"""NotifyWebexTeams() Bot mode attachment coverage.""" |
| 587 |
|
| 588 |
response = mock.Mock() |
| 589 |
response.status_code = requests.codes.ok |
| 590 |
response.content = b"" |
| 591 |
mock_post.return_value = response |
| 592 |
|
| 593 |
obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID]) |
| 594 |
|
| 595 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 596 |
attach = AppriseAttachment(path) |
| 597 |
|
| 598 |
|
| 599 |
mock_post.reset_mock() |
| 600 |
assert ( |
| 601 |
obj.notify( |
| 602 |
body="see attached", |
| 603 |
title="", |
| 604 |
notify_type=NotifyType.INFO, |
| 605 |
attach=attach, |
| 606 |
) |
| 607 |
is True |
| 608 |
) |
| 609 |
|
| 610 |
assert mock_post.call_count == 1 |
| 611 |
call_url = mock_post.call_args[0][0] |
| 612 |
assert call_url == "https://webexapis.com/v1/messages" |
| 613 |
|
| 614 |
headers = mock_post.call_args[1]["headers"] |
| 615 |
assert "Content-Type" not in headers |
| 616 |
|
| 617 |
assert mock_post.call_args[1].get("files") is not None |
| 618 |
|
| 619 |
|
| 620 |
mock_post.reset_mock() |
| 621 |
path2 = os.path.join(TEST_VAR_DIR, "apprise-test.png") |
| 622 |
attach2 = AppriseAttachment([path, path2]) |
| 623 |
assert obj.send(body="two files", attach=attach2) is True |
| 624 |
assert mock_post.call_count == 2 |
| 625 |
|
| 626 |
|
| 627 |
mock_post.reset_mock() |
| 628 |
obj2 = NotifyWebexTeams( |
| 629 |
access_token=BOT_TOKEN, targets=[ROOM_ID, ROOM_ID2] |
| 630 |
) |
| 631 |
assert obj2.send(body="room broadcast", attach=attach) is True |
| 632 |
assert mock_post.call_count == 2 |
| 633 |
|
| 634 |
|
| 635 |
mock_post.reset_mock() |
| 636 |
mock_post.side_effect = requests.RequestException() |
| 637 |
assert obj.send(body="test", attach=attach) is False |
| 638 |
|
| 639 |
mock_post.side_effect = None |
| 640 |
mock_post.return_value = response |
| 641 |
|
| 642 |
|
| 643 |
mock_post.reset_mock() |
| 644 |
mock_post.side_effect = OSError() |
| 645 |
assert obj.send(body="test", attach=attach) is False |
| 646 |
|
| 647 |
mock_post.side_effect = None |
| 648 |
mock_post.return_value = response |
| 649 |
|
| 650 |
|
| 651 |
mock_post.reset_mock() |
| 652 |
bad_response = mock.Mock() |
| 653 |
bad_response.status_code = requests.codes.internal_server_error |
| 654 |
bad_response.content = b"" |
| 655 |
mock_post.return_value = bad_response |
| 656 |
assert obj.send(body="test", attach=attach) is False |
| 657 |
|
| 658 |
mock_post.return_value = response |
| 659 |
|
| 660 |
|
| 661 |
mock_post.reset_mock() |
| 662 |
mock_post.side_effect = None |
| 663 |
with mock.patch( |
| 664 |
"apprise.attachment.file.AttachFile.open", |
| 665 |
side_effect=OSError("open failed"), |
| 666 |
): |
| 667 |
assert obj.send(body="test", attach=attach) is False |
| 668 |
mock_post.side_effect = None |
| 669 |
mock_post.return_value = response |
| 670 |
|
| 671 |
|
| 672 |
mock_post.reset_mock() |
| 673 |
invalid_path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/file.jpg") |
| 674 |
bad_attach = AppriseAttachment(invalid_path) |
| 675 |
assert ( |
| 676 |
obj.notify( |
| 677 |
body="test", |
| 678 |
notify_type=NotifyType.INFO, |
| 679 |
attach=bad_attach, |
| 680 |
) |
| 681 |
is False |
| 682 |
) |
| 683 |
|
| 684 |
assert mock_post.call_count == 0 |
| 685 |
|
| 686 |
|
| 687 |
@mock.patch("requests.post") |
| 688 |
def test_plugin_webex_teams_apprise_integration(mock_post): |
| 689 |
"""NotifyWebexTeams() Apprise.notify() integration.""" |
| 690 |
|
| 691 |
response = mock.Mock() |
| 692 |
response.status_code = requests.codes.ok |
| 693 |
response.content = b"" |
| 694 |
mock_post.return_value = response |
| 695 |
|
| 696 |
|
| 697 |
app = Apprise() |
| 698 |
assert app.add("wxteams://{}".format(WEBHOOK_TOKEN)) |
| 699 |
assert app.notify(body="webhook test") is True |
| 700 |
|
| 701 |
mock_post.reset_mock() |
| 702 |
|
| 703 |
|
| 704 |
app2 = Apprise() |
| 705 |
assert app2.add("wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID)) |
| 706 |
assert app2.notify(body="bot test") is True |
| 707 |
|
| 708 |
|
| 709 |
@mock.patch("requests.post") |
| 710 |
def test_plugin_webex_teams_html_to_markdown_format(mock_post): |
| 711 |
"""NotifyWebexTeams(): HTML body is converted to Markdown.""" |
| 712 |
|
| 713 |
|
| 714 |
mock_post.return_value = requests.Request() |
| 715 |
mock_post.return_value.status_code = requests.codes.ok |
| 716 |
|
| 717 |
|
| 718 |
aobj = Apprise() |
| 719 |
assert aobj.add("wxteams://{}".format("a" * 80)) |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
assert ( |
| 724 |
aobj.notify( |
| 725 |
body="<b>hello</b> <i>world</i>", |
| 726 |
body_format=NotifyFormat.HTML, |
| 727 |
) |
| 728 |
is True |
| 729 |
) |
| 730 |
assert mock_post.call_count == 1 |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 735 |
assert payload["markdown"] == "**hello** *world*" |