| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
# Disable logging for a cleaner testing output |
| 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.exception import AppriseImproperlyConfigured |
| 40 |
from apprise.plugins.webexteams import ( |
| 41 |
NotifyWebexTeams, |
| 42 |
WebexTeamsMode, |
| 43 |
) |
| 44 |
|
| 45 |
logging.disable(logging.CRITICAL) |
| 46 |
|
| 47 |
# Attachment Directory |
| 48 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 49 |
|
| 50 |
# A valid webhook token (80 lowercase alphanumeric chars) |
| 51 |
WEBHOOK_TOKEN = "a" * 80 |
| 52 |
|
| 53 |
# A valid bot access token (longer than 160 chars, contains hyphen) |
| 54 |
# Must be > 160 chars or contain non-alphanumeric chars to bypass |
| 55 |
# the webhook-token regex (which only allows [a-z0-9], 80-160 chars). |
| 56 |
# Using 200 chars ending in '0' so the privacy URL ends in '0'. |
| 57 |
BOT_TOKEN = "Bc10" * 50 # 200 chars, ends in '0' |
| 58 |
|
| 59 |
# A valid Webex room ID (base64url-like, 50 chars) |
| 60 |
ROOM_ID = "Y2lzY29zcGFyazovL3VzL1JPTU9NLzEyMzQ1Njc4OTAxMjM0" |
| 61 |
ROOM_ID2 = "Y2lzY29zcGFyazovL3VzL1JPTU9NL2FiY2RlZjEyMzQ1Njc4" |
| 62 |
|
| 63 |
# Our Testing URLs |
| 64 |
apprise_url_tests = ( |
| 65 |
( |
| 66 |
"wxteams://", |
| 67 |
{ |
| 68 |
# Token missing |
| 69 |
"instance": AppriseImproperlyConfigured, |
| 70 |
}, |
| 71 |
), |
| 72 |
( |
| 73 |
"wxteams://:@/", |
| 74 |
{ |
| 75 |
# We don't have strict host checking on for wxteams, so this |
| 76 |
# URL actually becomes parseable and :@ becomes a hostname. |
| 77 |
# The below errors because a second token wasn't found |
| 78 |
"instance": AppriseImproperlyConfigured, |
| 79 |
}, |
| 80 |
), |
| 81 |
( |
| 82 |
"wxteams://{}".format("a" * 80), |
| 83 |
{ |
| 84 |
# webhook token provided - we're good |
| 85 |
"instance": NotifyWebexTeams, |
| 86 |
# Our expected url(privacy=True) startswith() response: |
| 87 |
"privacy_url": "wxteams://a...a/", |
| 88 |
}, |
| 89 |
), |
| 90 |
( |
| 91 |
"wxteams://?token={}".format("a" * 80), |
| 92 |
{ |
| 93 |
# token as query param - we're good |
| 94 |
"instance": NotifyWebexTeams, |
| 95 |
# Our expected url(privacy=True) startswith() response: |
| 96 |
"privacy_url": "wxteams://a...a/", |
| 97 |
}, |
| 98 |
), |
| 99 |
( |
| 100 |
"webex://{}".format("a" * 140), |
| 101 |
{ |
| 102 |
# token provided via 'webex' schema - we're good |
| 103 |
"instance": NotifyWebexTeams, |
| 104 |
# Our expected url(privacy=True) startswith() response: |
| 105 |
"privacy_url": "wxteams://a...a/", |
| 106 |
}, |
| 107 |
), |
| 108 |
# Support Native Webhook URLs |
| 109 |
( |
| 110 |
"https://api.ciscospark.com/v1/webhooks/incoming/{}".format("a" * 80), |
| 111 |
{ |
| 112 |
# token provided - we're good |
| 113 |
"instance": NotifyWebexTeams, |
| 114 |
}, |
| 115 |
), |
| 116 |
# Support New Native Webhook URLs |
| 117 |
( |
| 118 |
"https://webexapis.com/v1/webhooks/incoming/{}".format("a" * 100), |
| 119 |
{ |
| 120 |
# token provided - we're good |
| 121 |
"instance": NotifyWebexTeams, |
| 122 |
}, |
| 123 |
), |
| 124 |
# Support Native Webhook URLs with arguments |
| 125 |
( |
| 126 |
"https://api.ciscospark.com/v1/webhooks/incoming/{}" |
| 127 |
"?format=text".format("a" * 80), |
| 128 |
{ |
| 129 |
# token provided - we're good |
| 130 |
"instance": NotifyWebexTeams, |
| 131 |
}, |
| 132 |
), |
| 133 |
# Bot mode: access_token + room ID |
| 134 |
( |
| 135 |
"wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID), |
| 136 |
{ |
| 137 |
"instance": NotifyWebexTeams, |
| 138 |
"privacy_url": "wxteams://B...0/", |
| 139 |
}, |
| 140 |
), |
| 141 |
# Bot mode: explicit mode=bot with no room IDs - loads but fails to send |
| 142 |
( |
| 143 |
"wxteams://{}?mode=bot".format("a" * 80), |
| 144 |
{ |
| 145 |
"instance": NotifyWebexTeams, |
| 146 |
# notify() returns False with no targets (no HTTP call needed) |
| 147 |
"notify_response": False, |
| 148 |
}, |
| 149 |
), |
| 150 |
# Explicit mode=webhook with a valid webhook token |
| 151 |
( |
| 152 |
"wxteams://{}?mode=webhook".format("a" * 80), |
| 153 |
{ |
| 154 |
"instance": NotifyWebexTeams, |
| 155 |
"privacy_url": "wxteams://a...a/", |
| 156 |
}, |
| 157 |
), |
| 158 |
# Invalid mode |
| 159 |
( |
| 160 |
"wxteams://{}?mode=invalid".format("a" * 80), |
| 161 |
{ |
| 162 |
"instance": AppriseImproperlyConfigured, |
| 163 |
}, |
| 164 |
), |
| 165 |
# Webhook mode: HTTP 500 response |
| 166 |
( |
| 167 |
"wxteams://{}".format("a" * 80), |
| 168 |
{ |
| 169 |
"instance": NotifyWebexTeams, |
| 170 |
"response": False, |
| 171 |
"requests_response_code": (requests.codes.internal_server_error), |
| 172 |
}, |
| 173 |
), |
| 174 |
# Webhook mode: unusual HTTP response code |
| 175 |
( |
| 176 |
"wxteams://{}".format("a" * 80), |
| 177 |
{ |
| 178 |
"instance": NotifyWebexTeams, |
| 179 |
# throw a bizarre code forcing us to fail to look it up |
| 180 |
"response": False, |
| 181 |
"requests_response_code": 999, |
| 182 |
}, |
| 183 |
), |
| 184 |
# Webhook mode: request exception |
| 185 |
( |
| 186 |
"wxteams://{}".format("a" * 80), |
| 187 |
{ |
| 188 |
"instance": NotifyWebexTeams, |
| 189 |
# Throws a series of i/o exceptions with this flag set and |
| 190 |
# tests that we gracefully handle them |
| 191 |
"test_requests_exceptions": True, |
| 192 |
}, |
| 193 |
), |
| 194 |
# Bot mode: HTTP 500 response |
| 195 |
( |
| 196 |
"wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID), |
| 197 |
{ |
| 198 |
"instance": NotifyWebexTeams, |
| 199 |
"response": False, |
| 200 |
"requests_response_code": (requests.codes.internal_server_error), |
| 201 |
}, |
| 202 |
), |
| 203 |
# Bot mode: unusual HTTP response code |
| 204 |
( |
| 205 |
"wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID), |
| 206 |
{ |
| 207 |
"instance": NotifyWebexTeams, |
| 208 |
"response": False, |
| 209 |
"requests_response_code": 999, |
| 210 |
}, |
| 211 |
), |
| 212 |
# Bot mode: request exception |
| 213 |
( |
| 214 |
"wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID), |
| 215 |
{ |
| 216 |
"instance": NotifyWebexTeams, |
| 217 |
"test_requests_exceptions": True, |
| 218 |
}, |
| 219 |
), |
| 220 |
) |
| 221 |
|
| 222 |
|
| 223 |
def test_plugin_webex_teams_urls(): |
| 224 |
"""NotifyWebexTeams() Apprise URLs.""" |
| 225 |
|
| 226 |
# Run our general tests |
| 227 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 228 |
|
| 229 |
|
| 230 |
def test_plugin_webex_teams_webhook_mode(): |
| 231 |
"""NotifyWebexTeams() Webhook mode direct tests.""" |
| 232 |
|
| 233 |
# Valid webhook token |
| 234 |
token = "b" * 80 |
| 235 |
|
| 236 |
obj = NotifyWebexTeams(token=token) |
| 237 |
assert isinstance(obj, NotifyWebexTeams) |
| 238 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 239 |
|
| 240 |
# url() round-trip |
| 241 |
url = obj.url() |
| 242 |
assert "wxteams://" in url |
| 243 |
assert "mode=webhook" in url |
| 244 |
|
| 245 |
# parse_url round-trip |
| 246 |
result = NotifyWebexTeams.parse_url(url) |
| 247 |
assert result is not None |
| 248 |
obj2 = NotifyWebexTeams(**result) |
| 249 |
assert obj2.mode == WebexTeamsMode.WEBHOOK |
| 250 |
assert obj2.token == token |
| 251 |
|
| 252 |
# privacy URL |
| 253 |
priv = obj.url(privacy=True) |
| 254 |
assert obj.token not in priv |
| 255 |
|
| 256 |
# url_identifier |
| 257 |
assert obj.url_identifier == ( |
| 258 |
NotifyWebexTeams.secure_protocol[0], |
| 259 |
token, |
| 260 |
) |
| 261 |
|
| 262 |
# 80-char token (minimum) |
| 263 |
assert NotifyWebexTeams(token="c" * 80) |
| 264 |
# 160-char token (maximum) |
| 265 |
assert NotifyWebexTeams(token="c" * 160) |
| 266 |
|
| 267 |
# Too short for webhook -> falls to bot mode (no targets), loads but |
| 268 |
# send() returns False |
| 269 |
obj_short = NotifyWebexTeams(token="c" * 79) |
| 270 |
assert obj_short.mode == WebexTeamsMode.BOT |
| 271 |
assert obj_short.send(body="test") is False |
| 272 |
|
| 273 |
# Too long for webhook -> bot mode, same behaviour |
| 274 |
obj_long = NotifyWebexTeams(token="c" * 161) |
| 275 |
assert obj_long.mode == WebexTeamsMode.BOT |
| 276 |
assert obj_long.send(body="test") is False |
| 277 |
|
| 278 |
# No token candidate is invalid. |
| 279 |
with pytest.raises(AppriseImproperlyConfigured): |
| 280 |
NotifyWebexTeams(token=None) |
| 281 |
|
| 282 |
# An empty token is invalid. |
| 283 |
with pytest.raises(AppriseImproperlyConfigured): |
| 284 |
NotifyWebexTeams(token="") |
| 285 |
|
| 286 |
# Non-alphanumeric chars -> falls to bot mode (no targets) |
| 287 |
obj_hyph = NotifyWebexTeams(token="a-b-" * 20) |
| 288 |
assert obj_hyph.mode == WebexTeamsMode.BOT |
| 289 |
assert obj_hyph.send(body="test") is False |
| 290 |
|
| 291 |
|
| 292 |
def test_plugin_webex_teams_bot_mode(): |
| 293 |
"""NotifyWebexTeams() Bot mode direct tests.""" |
| 294 |
|
| 295 |
obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID]) |
| 296 |
assert isinstance(obj, NotifyWebexTeams) |
| 297 |
assert obj.mode == WebexTeamsMode.BOT |
| 298 |
assert obj.access_token == BOT_TOKEN |
| 299 |
assert ROOM_ID in obj.targets |
| 300 |
|
| 301 |
# url() round-trip |
| 302 |
url = obj.url() |
| 303 |
assert "wxteams://" in url |
| 304 |
assert "mode=bot" in url |
| 305 |
|
| 306 |
result = NotifyWebexTeams.parse_url(url) |
| 307 |
assert result is not None |
| 308 |
obj2 = NotifyWebexTeams(**result) |
| 309 |
assert obj2.mode == WebexTeamsMode.BOT |
| 310 |
assert obj2.access_token == BOT_TOKEN |
| 311 |
assert ROOM_ID in obj2.targets |
| 312 |
|
| 313 |
# privacy URL masks the token |
| 314 |
priv = obj.url(privacy=True) |
| 315 |
assert BOT_TOKEN not in priv |
| 316 |
|
| 317 |
# url_identifier |
| 318 |
assert obj.url_identifier == ( |
| 319 |
NotifyWebexTeams.secure_protocol[0], |
| 320 |
BOT_TOKEN, |
| 321 |
) |
| 322 |
|
| 323 |
# Multiple rooms |
| 324 |
obj3 = NotifyWebexTeams( |
| 325 |
access_token=BOT_TOKEN, targets=[ROOM_ID, ROOM_ID2] |
| 326 |
) |
| 327 |
assert len(obj3.targets) == 2 |
| 328 |
|
| 329 |
# No room IDs -> loads fine, but send() returns False |
| 330 |
obj_no_rooms = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[]) |
| 331 |
assert isinstance(obj_no_rooms, NotifyWebexTeams) |
| 332 |
assert obj_no_rooms.send(body="test") is False |
| 333 |
|
| 334 |
# An access token is required. |
| 335 |
with pytest.raises(AppriseImproperlyConfigured): |
| 336 |
NotifyWebexTeams(access_token=None, targets=[ROOM_ID]) |
| 337 |
|
| 338 |
# Bot mode requires a token or access token. |
| 339 |
with pytest.raises(AppriseImproperlyConfigured): |
| 340 |
NotifyWebexTeams(mode="bot", targets=[ROOM_ID]) |
| 341 |
|
| 342 |
# Explicit mode=bot on a short (webhook-style) token |
| 343 |
obj4 = NotifyWebexTeams(token="a" * 80, mode="bot", targets=[ROOM_ID]) |
| 344 |
assert obj4.mode == WebexTeamsMode.BOT |
| 345 |
# The 'token' argument is used as access_token in bot mode |
| 346 |
assert obj4.access_token == "a" * 80 |
| 347 |
|
| 348 |
# Invalid mode |
| 349 |
with pytest.raises(AppriseImproperlyConfigured): |
| 350 |
NotifyWebexTeams( |
| 351 |
access_token=BOT_TOKEN, |
| 352 |
targets=[ROOM_ID], |
| 353 |
mode="invalid", |
| 354 |
) |
| 355 |
|
| 356 |
|
| 357 |
def test_plugin_webex_teams_body_maxlen(): |
| 358 |
"""NotifyWebexTeams() body_maxlen varies by mode.""" |
| 359 |
|
| 360 |
# Webhook mode: 1000 chars |
| 361 |
obj = NotifyWebexTeams(token=WEBHOOK_TOKEN) |
| 362 |
assert obj.body_maxlen == 1000 |
| 363 |
|
| 364 |
# Bot mode: 7439 chars |
| 365 |
obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID]) |
| 366 |
assert obj.body_maxlen == 7439 |
| 367 |
|
| 368 |
|
| 369 |
def test_plugin_webex_teams_mode_detection(): |
| 370 |
"""NotifyWebexTeams() auto-detects mode from token format.""" |
| 371 |
|
| 372 |
# 80-char lowercase alphanumeric -> WEBHOOK |
| 373 |
obj = NotifyWebexTeams(token="a" * 80) |
| 374 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 375 |
|
| 376 |
# 80-char uppercase alphanumeric also matches webhook regex |
| 377 |
# (regex uses 'i' flag); no targets means webhook mode |
| 378 |
obj = NotifyWebexTeams(token="A" * 80) |
| 379 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 380 |
|
| 381 |
# Token longer than 160 chars -> fails webhook regex -> BOT |
| 382 |
obj = NotifyWebexTeams(token="a" * 200, targets=[ROOM_ID]) |
| 383 |
assert obj.mode == WebexTeamsMode.BOT |
| 384 |
|
| 385 |
# Token with hyphens (non-alphanumeric) -> fails webhook regex -> BOT |
| 386 |
obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID]) |
| 387 |
assert obj.mode == WebexTeamsMode.BOT |
| 388 |
|
| 389 |
|
| 390 |
def test_plugin_webex_teams_url_parsing(): |
| 391 |
"""NotifyWebexTeams() URL parsing edge cases.""" |
| 392 |
|
| 393 |
# Webhook via ?token= param |
| 394 |
result = NotifyWebexTeams.parse_url("wxteams://?token={}".format("a" * 80)) |
| 395 |
assert result is not None |
| 396 |
obj = NotifyWebexTeams(**result) |
| 397 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 398 |
assert obj.token == "a" * 80 |
| 399 |
|
| 400 |
# Bot mode: access_token in host, room ID in path |
| 401 |
result = NotifyWebexTeams.parse_url( |
| 402 |
"wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID) |
| 403 |
) |
| 404 |
assert result is not None |
| 405 |
obj = NotifyWebexTeams(**result) |
| 406 |
assert obj.mode == WebexTeamsMode.BOT |
| 407 |
assert ROOM_ID in obj.targets |
| 408 |
|
| 409 |
# Bot mode: multiple room IDs in path |
| 410 |
result = NotifyWebexTeams.parse_url( |
| 411 |
"wxteams://{}/{}/{}".format(BOT_TOKEN, ROOM_ID, ROOM_ID2) |
| 412 |
) |
| 413 |
assert result is not None |
| 414 |
obj = NotifyWebexTeams(**result) |
| 415 |
assert obj.mode == WebexTeamsMode.BOT |
| 416 |
assert len(obj.targets) == 2 |
| 417 |
|
| 418 |
# Bot mode: room IDs via ?to= param |
| 419 |
result = NotifyWebexTeams.parse_url( |
| 420 |
"wxteams://{}?to={}&mode=bot".format(BOT_TOKEN, ROOM_ID) |
| 421 |
) |
| 422 |
assert result is not None |
| 423 |
obj = NotifyWebexTeams(**result) |
| 424 |
assert obj.mode == WebexTeamsMode.BOT |
| 425 |
assert ROOM_ID in obj.targets |
| 426 |
|
| 427 |
# Native webhook URL |
| 428 |
result = NotifyWebexTeams.parse_native_url( |
| 429 |
"https://api.ciscospark.com/v1/webhooks/incoming/{}".format("a" * 80) |
| 430 |
) |
| 431 |
assert result is not None |
| 432 |
obj = NotifyWebexTeams(**result) |
| 433 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 434 |
|
| 435 |
# New native webhook URL |
| 436 |
result = NotifyWebexTeams.parse_native_url( |
| 437 |
"https://webexapis.com/v1/webhooks/incoming/{}".format("a" * 100) |
| 438 |
) |
| 439 |
assert result is not None |
| 440 |
obj = NotifyWebexTeams(**result) |
| 441 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 442 |
|
| 443 |
# parse_native_url returns None for non-matching URLs |
| 444 |
assert ( |
| 445 |
NotifyWebexTeams.parse_native_url( |
| 446 |
"https://example.com/not/a/webex/url" |
| 447 |
) |
| 448 |
is None |
| 449 |
) |
| 450 |
|
| 451 |
# Webhook mode forced with ?mode=webhook even with path entries |
| 452 |
result = NotifyWebexTeams.parse_url( |
| 453 |
"wxteams://{}?mode=webhook".format("a" * 80) |
| 454 |
) |
| 455 |
assert result is not None |
| 456 |
obj = NotifyWebexTeams(**result) |
| 457 |
assert obj.mode == WebexTeamsMode.WEBHOOK |
| 458 |
|
| 459 |
|
| 460 |
@mock.patch("requests.post") |
| 461 |
def test_plugin_webex_teams_webhook_send(mock_post): |
| 462 |
"""NotifyWebexTeams() Webhook send() coverage.""" |
| 463 |
|
| 464 |
# Prepare a good response |
| 465 |
response = mock.Mock() |
| 466 |
response.status_code = requests.codes.ok |
| 467 |
response.content = b"" |
| 468 |
mock_post.return_value = response |
| 469 |
|
| 470 |
obj = NotifyWebexTeams(token=WEBHOOK_TOKEN) |
| 471 |
|
| 472 |
# Successful send |
| 473 |
assert obj.send(body="test message") is True |
| 474 |
assert mock_post.call_count == 1 |
| 475 |
call_url = mock_post.call_args[0][0] |
| 476 |
assert "webhooks/incoming" in call_url |
| 477 |
assert WEBHOOK_TOKEN in call_url |
| 478 |
|
| 479 |
# Verify correct Content-Type header |
| 480 |
headers = mock_post.call_args[1]["headers"] |
| 481 |
assert headers["Content-Type"] == "application/json" |
| 482 |
|
| 483 |
mock_post.reset_mock() |
| 484 |
|
| 485 |
# 204 No Content is also acceptable |
| 486 |
response.status_code = requests.codes.no_content |
| 487 |
assert obj.send(body="test") is True |
| 488 |
|
| 489 |
mock_post.reset_mock() |
| 490 |
|
| 491 |
# HTTP error |
| 492 |
response.status_code = requests.codes.internal_server_error |
| 493 |
assert obj.send(body="test") is False |
| 494 |
|
| 495 |
mock_post.reset_mock() |
| 496 |
|
| 497 |
# Unknown HTTP error code |
| 498 |
response.status_code = 999 |
| 499 |
assert obj.send(body="test") is False |
| 500 |
|
| 501 |
mock_post.reset_mock() |
| 502 |
|
| 503 |
# Connection error |
| 504 |
mock_post.side_effect = requests.RequestException() |
| 505 |
assert obj.send(body="test") is False |
| 506 |
|
| 507 |
mock_post.side_effect = None |
| 508 |
mock_post.return_value = response |
| 509 |
response.status_code = requests.codes.ok |
| 510 |
|
| 511 |
# Webhook warns about attachments (but still proceeds) |
| 512 |
mock_post.reset_mock() |
| 513 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 514 |
attach = AppriseAttachment(path) |
| 515 |
assert obj.send(body="test", attach=attach) is True |
| 516 |
# Only one POST call: text-only webhook |
| 517 |
assert mock_post.call_count == 1 |
| 518 |
|
| 519 |
|
| 520 |
@mock.patch("requests.post") |
| 521 |
def test_plugin_webex_teams_bot_send(mock_post): |
| 522 |
"""NotifyWebexTeams() Bot send() text-only coverage.""" |
| 523 |
|
| 524 |
response = mock.Mock() |
| 525 |
response.status_code = requests.codes.ok |
| 526 |
response.content = b"" |
| 527 |
mock_post.return_value = response |
| 528 |
|
| 529 |
obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID]) |
| 530 |
|
| 531 |
# Successful text send |
| 532 |
assert obj.send(body="hello from bot") is True |
| 533 |
assert mock_post.call_count == 1 |
| 534 |
call_url = mock_post.call_args[0][0] |
| 535 |
assert call_url == "https://webexapis.com/v1/messages" |
| 536 |
|
| 537 |
# Verify Bearer authorization |
| 538 |
headers = mock_post.call_args[1]["headers"] |
| 539 |
assert headers["Authorization"] == f"Bearer {BOT_TOKEN}" |
| 540 |
assert headers["Content-Type"] == "application/json" |
| 541 |
|
| 542 |
mock_post.reset_mock() |
| 543 |
|
| 544 |
# Two rooms -> two calls |
| 545 |
obj2 = NotifyWebexTeams( |
| 546 |
access_token=BOT_TOKEN, targets=[ROOM_ID, ROOM_ID2] |
| 547 |
) |
| 548 |
assert obj2.send(body="broadcast") is True |
| 549 |
assert mock_post.call_count == 2 |
| 550 |
|
| 551 |
mock_post.reset_mock() |
| 552 |
|
| 553 |
# HTTP error for bot mode |
| 554 |
response.status_code = requests.codes.internal_server_error |
| 555 |
assert obj.send(body="test") is False |
| 556 |
|
| 557 |
mock_post.reset_mock() |
| 558 |
|
| 559 |
response.status_code = 999 |
| 560 |
assert obj.send(body="test") is False |
| 561 |
|
| 562 |
mock_post.reset_mock() |
| 563 |
|
| 564 |
# Connection error for bot mode |
| 565 |
mock_post.side_effect = requests.RequestException() |
| 566 |
assert obj.send(body="test") is False |
| 567 |
|
| 568 |
mock_post.side_effect = None |
| 569 |
response.status_code = requests.codes.ok |
| 570 |
mock_post.return_value = response |
| 571 |
|
| 572 |
# Two rooms: second fails -> overall failure reported |
| 573 |
mock_post.reset_mock() |
| 574 |
fail_response = mock.Mock() |
| 575 |
fail_response.status_code = requests.codes.internal_server_error |
| 576 |
fail_response.content = b"" |
| 577 |
|
| 578 |
mock_post.side_effect = [response, fail_response] |
| 579 |
obj2 = NotifyWebexTeams( |
| 580 |
access_token=BOT_TOKEN, targets=[ROOM_ID, ROOM_ID2] |
| 581 |
) |
| 582 |
assert obj2.send(body="test") is False |
| 583 |
|
| 584 |
|
| 585 |
@mock.patch("requests.post") |
| 586 |
def test_plugin_webex_teams_bot_attachments(mock_post): |
| 587 |
"""NotifyWebexTeams() Bot mode attachment coverage.""" |
| 588 |
|
| 589 |
response = mock.Mock() |
| 590 |
response.status_code = requests.codes.ok |
| 591 |
response.content = b"" |
| 592 |
mock_post.return_value = response |
| 593 |
|
| 594 |
obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID]) |
| 595 |
|
| 596 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 597 |
attach = AppriseAttachment(path) |
| 598 |
|
| 599 |
# Successful single attachment |
| 600 |
mock_post.reset_mock() |
| 601 |
assert ( |
| 602 |
bool( |
| 603 |
obj.notify( |
| 604 |
body="see attached", |
| 605 |
title="", |
| 606 |
notify_type=NotifyType.INFO, |
| 607 |
attach=attach, |
| 608 |
) |
| 609 |
) |
| 610 |
is True |
| 611 |
) |
| 612 |
# One POST call for the single attachment |
| 613 |
assert mock_post.call_count == 1 |
| 614 |
call_url = mock_post.call_args[0][0] |
| 615 |
assert call_url == "https://webexapis.com/v1/messages" |
| 616 |
# No Content-Type header set (multipart sets its own) |
| 617 |
headers = mock_post.call_args[1]["headers"] |
| 618 |
assert "Content-Type" not in headers |
| 619 |
# files kwarg was provided |
| 620 |
assert mock_post.call_args[1].get("files") is not None |
| 621 |
|
| 622 |
# Multiple attachments -> one POST per attachment |
| 623 |
mock_post.reset_mock() |
| 624 |
path2 = os.path.join(TEST_VAR_DIR, "apprise-test.png") |
| 625 |
attach2 = AppriseAttachment([path, path2]) |
| 626 |
assert obj.send(body="two files", attach=attach2) is True |
| 627 |
assert mock_post.call_count == 2 |
| 628 |
|
| 629 |
# Two rooms, one attachment -> 2 POST calls |
| 630 |
mock_post.reset_mock() |
| 631 |
obj2 = NotifyWebexTeams( |
| 632 |
access_token=BOT_TOKEN, targets=[ROOM_ID, ROOM_ID2] |
| 633 |
) |
| 634 |
assert obj2.send(body="room broadcast", attach=attach) is True |
| 635 |
assert mock_post.call_count == 2 |
| 636 |
|
| 637 |
# Connection error on attachment POST |
| 638 |
mock_post.reset_mock() |
| 639 |
mock_post.side_effect = requests.RequestException() |
| 640 |
assert obj.send(body="test", attach=attach) is False |
| 641 |
|
| 642 |
mock_post.side_effect = None |
| 643 |
mock_post.return_value = response |
| 644 |
|
| 645 |
# OSError reading attachment |
| 646 |
mock_post.reset_mock() |
| 647 |
mock_post.side_effect = OSError() |
| 648 |
assert obj.send(body="test", attach=attach) is False |
| 649 |
|
| 650 |
mock_post.side_effect = None |
| 651 |
mock_post.return_value = response |
| 652 |
|
| 653 |
# HTTP error on attachment upload |
| 654 |
mock_post.reset_mock() |
| 655 |
bad_response = mock.Mock() |
| 656 |
bad_response.status_code = requests.codes.internal_server_error |
| 657 |
bad_response.content = b"" |
| 658 |
mock_post.return_value = bad_response |
| 659 |
assert obj.send(body="test", attach=attach) is False |
| 660 |
|
| 661 |
mock_post.return_value = response |
| 662 |
|
| 663 |
# OSError raised by attachment.open() (before the file handle is used) |
| 664 |
mock_post.reset_mock() |
| 665 |
mock_post.side_effect = None |
| 666 |
with mock.patch( |
| 667 |
"apprise.attachment.file.AttachFile.open", |
| 668 |
side_effect=OSError("open failed"), |
| 669 |
): |
| 670 |
assert obj.send(body="test", attach=attach) is False |
| 671 |
mock_post.side_effect = None |
| 672 |
mock_post.return_value = response |
| 673 |
|
| 674 |
# Invalid/inaccessible attachment |
| 675 |
mock_post.reset_mock() |
| 676 |
invalid_path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/file.jpg") |
| 677 |
bad_attach = AppriseAttachment(invalid_path) |
| 678 |
assert ( |
| 679 |
bool( |
| 680 |
obj.notify( |
| 681 |
body="test", |
| 682 |
notify_type=NotifyType.INFO, |
| 683 |
attach=bad_attach, |
| 684 |
) |
| 685 |
) |
| 686 |
is False |
| 687 |
) |
| 688 |
# No POST should have been made |
| 689 |
assert mock_post.call_count == 0 |
| 690 |
|
| 691 |
|
| 692 |
@mock.patch("requests.post") |
| 693 |
def test_plugin_webex_teams_apprise_integration(mock_post): |
| 694 |
"""NotifyWebexTeams() Apprise.notify() integration.""" |
| 695 |
|
| 696 |
response = mock.Mock() |
| 697 |
response.status_code = requests.codes.ok |
| 698 |
response.content = b"" |
| 699 |
mock_post.return_value = response |
| 700 |
|
| 701 |
# Webhook mode via Apprise |
| 702 |
app = Apprise() |
| 703 |
assert app.add("wxteams://{}".format(WEBHOOK_TOKEN)) |
| 704 |
assert bool(app.notify(body="webhook test")) is True |
| 705 |
|
| 706 |
mock_post.reset_mock() |
| 707 |
|
| 708 |
# Bot mode via Apprise |
| 709 |
app2 = Apprise() |
| 710 |
assert app2.add("wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID)) |
| 711 |
assert bool(app2.notify(body="bot test")) is True |
| 712 |
|
| 713 |
|
| 714 |
@mock.patch("requests.post") |
| 715 |
def test_plugin_webex_teams_html_to_markdown_format(mock_post): |
| 716 |
"""NotifyWebexTeams(): HTML body is converted to Markdown.""" |
| 717 |
|
| 718 |
# Prepare Mock |
| 719 |
mock_post.return_value = requests.Request() |
| 720 |
mock_post.return_value.status_code = requests.codes.ok |
| 721 |
|
| 722 |
# Use a webhook URL (80-char token, webhook mode) |
| 723 |
aobj = Apprise() |
| 724 |
assert aobj.add("wxteams://{}".format("a" * 80)) |
| 725 |
|
| 726 |
# Notify with an HTML body; the framework should convert it |
| 727 |
# to Markdown before dispatching to Webex Teams |
| 728 |
assert ( |
| 729 |
bool( |
| 730 |
aobj.notify( |
| 731 |
body="<b>hello</b> <i>world</i>", |
| 732 |
body_format=NotifyFormat.HTML, |
| 733 |
) |
| 734 |
) |
| 735 |
is True |
| 736 |
) |
| 737 |
assert mock_post.call_count == 1 |
| 738 |
|
| 739 |
# The body must arrive as Markdown (not plain text) in the |
| 740 |
# "markdown" key, since notify_format is MARKDOWN by default |
| 741 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 742 |
assert payload["markdown"] == "**hello** *world*" |