| 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 |
import logging |
| 30 |
from unittest import mock |
| 31 |
|
| 32 |
from helpers import AppriseURLTester |
| 33 |
import pytest |
| 34 |
import requests |
| 35 |
|
| 36 |
from apprise import Apprise, NotifyType |
| 37 |
from apprise.plugins.zoom import ( |
| 38 |
ZOOM_MODE_DEFAULT, |
| 39 |
NotifyZoom, |
| 40 |
ZoomMode, |
| 41 |
) |
| 42 |
|
| 43 |
logging.disable(logging.CRITICAL) |
| 44 |
|
| 45 |
|
| 46 |
WEBHOOK_ID = "abcHook123" |
| 47 |
TOKEN = "xyzVerify456" |
| 48 |
|
| 49 |
|
| 50 |
apprise_url_tests = ( |
| 51 |
|
| 52 |
( |
| 53 |
"zoom://", |
| 54 |
{ |
| 55 |
"instance": TypeError, |
| 56 |
}, |
| 57 |
), |
| 58 |
|
| 59 |
( |
| 60 |
"zoom://:@/", |
| 61 |
{ |
| 62 |
"instance": TypeError, |
| 63 |
}, |
| 64 |
), |
| 65 |
|
| 66 |
( |
| 67 |
"zoom://{}".format(WEBHOOK_ID), |
| 68 |
{ |
| 69 |
"instance": TypeError, |
| 70 |
}, |
| 71 |
), |
| 72 |
|
| 73 |
( |
| 74 |
"zoom://{}/{}".format(WEBHOOK_ID, TOKEN), |
| 75 |
{ |
| 76 |
"instance": NotifyZoom, |
| 77 |
"privacy_url": "zoom://a...3/x...6/", |
| 78 |
}, |
| 79 |
), |
| 80 |
|
| 81 |
( |
| 82 |
"zoom://{}/{}?mode=full".format(WEBHOOK_ID, TOKEN), |
| 83 |
{ |
| 84 |
"instance": NotifyZoom, |
| 85 |
"privacy_url": "zoom://a...3/x...6/", |
| 86 |
}, |
| 87 |
), |
| 88 |
|
| 89 |
( |
| 90 |
"zoom://{}/{}?mode=simple".format(WEBHOOK_ID, TOKEN), |
| 91 |
{ |
| 92 |
"instance": NotifyZoom, |
| 93 |
"privacy_url": "zoom://a...3/x...6/", |
| 94 |
}, |
| 95 |
), |
| 96 |
|
| 97 |
( |
| 98 |
"zoom://{}/{}?mode=invalid".format(WEBHOOK_ID, TOKEN), |
| 99 |
{ |
| 100 |
"instance": TypeError, |
| 101 |
}, |
| 102 |
), |
| 103 |
|
| 104 |
( |
| 105 |
"https://inbots.zoom.us/incoming/hook/{}?token={}".format( |
| 106 |
WEBHOOK_ID, TOKEN |
| 107 |
), |
| 108 |
{ |
| 109 |
"instance": NotifyZoom, |
| 110 |
}, |
| 111 |
), |
| 112 |
|
| 113 |
( |
| 114 |
"zoom://{}/{}".format(WEBHOOK_ID, TOKEN), |
| 115 |
{ |
| 116 |
"instance": NotifyZoom, |
| 117 |
"response": False, |
| 118 |
"requests_response_code": (requests.codes.internal_server_error), |
| 119 |
}, |
| 120 |
), |
| 121 |
|
| 122 |
( |
| 123 |
"zoom://{}/{}".format(WEBHOOK_ID, TOKEN), |
| 124 |
{ |
| 125 |
"instance": NotifyZoom, |
| 126 |
"response": False, |
| 127 |
"requests_response_code": 999, |
| 128 |
}, |
| 129 |
), |
| 130 |
|
| 131 |
( |
| 132 |
"zoom://{}/{}".format(WEBHOOK_ID, TOKEN), |
| 133 |
{ |
| 134 |
"instance": NotifyZoom, |
| 135 |
"test_requests_exceptions": True, |
| 136 |
}, |
| 137 |
), |
| 138 |
|
| 139 |
( |
| 140 |
"zoom://{}/{}?mode=simple".format(WEBHOOK_ID, TOKEN), |
| 141 |
{ |
| 142 |
"instance": NotifyZoom, |
| 143 |
"response": False, |
| 144 |
"requests_response_code": (requests.codes.internal_server_error), |
| 145 |
}, |
| 146 |
), |
| 147 |
|
| 148 |
( |
| 149 |
"zoom://{}/{}?mode=simple".format(WEBHOOK_ID, TOKEN), |
| 150 |
{ |
| 151 |
"instance": NotifyZoom, |
| 152 |
"response": False, |
| 153 |
"requests_response_code": 999, |
| 154 |
}, |
| 155 |
), |
| 156 |
|
| 157 |
( |
| 158 |
"zoom://{}/{}?mode=simple".format(WEBHOOK_ID, TOKEN), |
| 159 |
{ |
| 160 |
"instance": NotifyZoom, |
| 161 |
"test_requests_exceptions": True, |
| 162 |
}, |
| 163 |
), |
| 164 |
) |
| 165 |
|
| 166 |
|
| 167 |
def test_plugin_zoom_urls(): |
| 168 |
"""NotifyZoom() Apprise URLs.""" |
| 169 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 170 |
|
| 171 |
|
| 172 |
def test_plugin_zoom_init(): |
| 173 |
"""Test NotifyZoom initialization and validation.""" |
| 174 |
|
| 175 |
|
| 176 |
with pytest.raises(TypeError): |
| 177 |
NotifyZoom(webhook_id=None, token=TOKEN) |
| 178 |
|
| 179 |
|
| 180 |
with pytest.raises(TypeError): |
| 181 |
NotifyZoom(webhook_id="", token=TOKEN) |
| 182 |
|
| 183 |
|
| 184 |
with pytest.raises(TypeError): |
| 185 |
NotifyZoom(webhook_id="bad/id", token=TOKEN) |
| 186 |
|
| 187 |
|
| 188 |
with pytest.raises(TypeError): |
| 189 |
NotifyZoom(webhook_id=WEBHOOK_ID, token=None) |
| 190 |
|
| 191 |
|
| 192 |
with pytest.raises(TypeError): |
| 193 |
NotifyZoom(webhook_id=WEBHOOK_ID, token="") |
| 194 |
|
| 195 |
|
| 196 |
with pytest.raises(TypeError): |
| 197 |
NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN, mode="bogus") |
| 198 |
|
| 199 |
|
| 200 |
with pytest.raises(TypeError): |
| 201 |
NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN, mode="") |
| 202 |
|
| 203 |
|
| 204 |
with pytest.raises(TypeError): |
| 205 |
NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN, mode=" ") |
| 206 |
|
| 207 |
|
| 208 |
obj = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN) |
| 209 |
assert obj.mode == ZOOM_MODE_DEFAULT |
| 210 |
|
| 211 |
|
| 212 |
obj = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN, mode="full") |
| 213 |
assert obj.mode == ZoomMode.FULL |
| 214 |
|
| 215 |
|
| 216 |
obj = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN, mode="simple") |
| 217 |
assert obj.mode == ZoomMode.SIMPLE |
| 218 |
|
| 219 |
|
| 220 |
obj = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN, mode="sim") |
| 221 |
assert obj.mode == ZoomMode.SIMPLE |
| 222 |
|
| 223 |
|
| 224 |
obj = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN, mode="f") |
| 225 |
assert obj.mode == ZoomMode.FULL |
| 226 |
|
| 227 |
|
| 228 |
def test_plugin_zoom_url_round_trip(): |
| 229 |
"""Verify that url() -> parse_url() -> __init__ is lossless.""" |
| 230 |
|
| 231 |
|
| 232 |
obj1 = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN, mode="full") |
| 233 |
result = NotifyZoom.parse_url(obj1.url()) |
| 234 |
obj2 = NotifyZoom(**result) |
| 235 |
assert obj1.url_identifier == obj2.url_identifier |
| 236 |
assert obj1.mode == obj2.mode |
| 237 |
|
| 238 |
|
| 239 |
obj1 = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN, mode="simple") |
| 240 |
result = NotifyZoom.parse_url(obj1.url()) |
| 241 |
obj2 = NotifyZoom(**result) |
| 242 |
assert obj1.url_identifier == obj2.url_identifier |
| 243 |
assert obj1.mode == obj2.mode |
| 244 |
|
| 245 |
|
| 246 |
def test_plugin_zoom_url_privacy(): |
| 247 |
"""Verify url(privacy=True) masks webhook_id and token.""" |
| 248 |
obj = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN) |
| 249 |
priv = obj.url(privacy=True) |
| 250 |
assert WEBHOOK_ID not in priv |
| 251 |
assert TOKEN not in priv |
| 252 |
|
| 253 |
assert priv.startswith("zoom://") |
| 254 |
|
| 255 |
|
| 256 |
def test_plugin_zoom_url_identifier(): |
| 257 |
"""Verify url_identifier contains webhook_id, token, and mode.""" |
| 258 |
obj = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN) |
| 259 |
uid = obj.url_identifier |
| 260 |
assert "zoom" in uid |
| 261 |
assert WEBHOOK_ID in uid |
| 262 |
assert TOKEN in uid |
| 263 |
|
| 264 |
|
| 265 |
obj2 = NotifyZoom(webhook_id=WEBHOOK_ID, token="other") |
| 266 |
assert obj.url_identifier != obj2.url_identifier |
| 267 |
|
| 268 |
|
| 269 |
obj_full = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN, mode="full") |
| 270 |
obj_simple = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN, mode="simple") |
| 271 |
assert obj_full.url_identifier != obj_simple.url_identifier |
| 272 |
|
| 273 |
assert obj_full.url_identifier[3] == ZoomMode.FULL |
| 274 |
assert obj_simple.url_identifier[3] == ZoomMode.SIMPLE |
| 275 |
|
| 276 |
|
| 277 |
@mock.patch("requests.post") |
| 278 |
def test_plugin_zoom_full_send(mock_post): |
| 279 |
"""Test _send_full() success and failure paths.""" |
| 280 |
|
| 281 |
def _mk_resp(code=requests.codes.ok): |
| 282 |
r = mock.Mock() |
| 283 |
r.status_code = code |
| 284 |
r.content = b"" |
| 285 |
return r |
| 286 |
|
| 287 |
mock_post.return_value = _mk_resp() |
| 288 |
|
| 289 |
|
| 290 |
obj = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN) |
| 291 |
assert obj.notify( |
| 292 |
body="body text", |
| 293 |
title="My Title", |
| 294 |
notify_type=NotifyType.INFO, |
| 295 |
) |
| 296 |
assert mock_post.call_count == 1 |
| 297 |
|
| 298 |
call_url = mock_post.call_args[0][0] |
| 299 |
assert "format=full" in call_url |
| 300 |
|
| 301 |
mock_post.reset_mock() |
| 302 |
|
| 303 |
|
| 304 |
assert obj.notify( |
| 305 |
body="body only", |
| 306 |
title="", |
| 307 |
notify_type=NotifyType.INFO, |
| 308 |
) |
| 309 |
assert mock_post.call_count == 1 |
| 310 |
|
| 311 |
mock_post.reset_mock() |
| 312 |
|
| 313 |
|
| 314 |
mock_post.return_value = _mk_resp(requests.codes.no_content) |
| 315 |
assert obj.notify(body="test", notify_type=NotifyType.INFO) |
| 316 |
|
| 317 |
mock_post.reset_mock() |
| 318 |
|
| 319 |
|
| 320 |
mock_post.return_value = _mk_resp(requests.codes.internal_server_error) |
| 321 |
assert not obj.notify(body="test", notify_type=NotifyType.INFO) |
| 322 |
|
| 323 |
mock_post.reset_mock() |
| 324 |
|
| 325 |
|
| 326 |
mock_post.return_value = _mk_resp(999) |
| 327 |
assert not obj.notify(body="test", notify_type=NotifyType.INFO) |
| 328 |
|
| 329 |
mock_post.reset_mock() |
| 330 |
|
| 331 |
|
| 332 |
mock_post.side_effect = requests.RequestException("boom") |
| 333 |
assert not obj.notify(body="test", notify_type=NotifyType.INFO) |
| 334 |
|
| 335 |
|
| 336 |
@mock.patch("requests.post") |
| 337 |
def test_plugin_zoom_simple_send(mock_post): |
| 338 |
"""Test _send_simple() success and failure paths.""" |
| 339 |
|
| 340 |
def _mk_resp(code=requests.codes.ok): |
| 341 |
r = mock.Mock() |
| 342 |
r.status_code = code |
| 343 |
r.content = b"" |
| 344 |
return r |
| 345 |
|
| 346 |
mock_post.return_value = _mk_resp() |
| 347 |
|
| 348 |
|
| 349 |
obj = NotifyZoom(webhook_id=WEBHOOK_ID, token=TOKEN, mode="simple") |
| 350 |
|
| 351 |
|
| 352 |
assert obj.notify( |
| 353 |
body="msg body", |
| 354 |
title="Alert", |
| 355 |
notify_type=NotifyType.INFO, |
| 356 |
) |
| 357 |
assert mock_post.call_count == 1 |
| 358 |
|
| 359 |
call_url = mock_post.call_args[0][0] |
| 360 |
assert "format" not in call_url |
| 361 |
|
| 362 |
mock_post.reset_mock() |
| 363 |
|
| 364 |
|
| 365 |
assert obj.notify( |
| 366 |
body="no title", |
| 367 |
title="", |
| 368 |
notify_type=NotifyType.INFO, |
| 369 |
) |
| 370 |
|
| 371 |
mock_post.reset_mock() |
| 372 |
|
| 373 |
|
| 374 |
mock_post.return_value = _mk_resp(requests.codes.no_content) |
| 375 |
assert obj.notify(body="test", notify_type=NotifyType.INFO) |
| 376 |
|
| 377 |
mock_post.reset_mock() |
| 378 |
|
| 379 |
|
| 380 |
mock_post.return_value = _mk_resp(requests.codes.internal_server_error) |
| 381 |
assert not obj.notify(body="test", notify_type=NotifyType.INFO) |
| 382 |
|
| 383 |
mock_post.reset_mock() |
| 384 |
|
| 385 |
|
| 386 |
mock_post.return_value = _mk_resp(999) |
| 387 |
assert not obj.notify(body="test", notify_type=NotifyType.INFO) |
| 388 |
|
| 389 |
mock_post.reset_mock() |
| 390 |
|
| 391 |
|
| 392 |
mock_post.side_effect = requests.RequestException("err") |
| 393 |
assert not obj.notify(body="test", notify_type=NotifyType.INFO) |
| 394 |
|
| 395 |
|
| 396 |
def test_plugin_zoom_parse_url(): |
| 397 |
"""Exercise parse_url() edge cases.""" |
| 398 |
|
| 399 |
|
| 400 |
result = NotifyZoom.parse_url("zoom://myhookid/mytoken/") |
| 401 |
assert result["webhook_id"] == "myhookid" |
| 402 |
assert result["token"] == "mytoken" |
| 403 |
|
| 404 |
|
| 405 |
result = NotifyZoom.parse_url("zoom://myhookid/?token=override123") |
| 406 |
assert result["webhook_id"] == "myhookid" |
| 407 |
assert result["token"] == "override123" |
| 408 |
|
| 409 |
|
| 410 |
result = NotifyZoom.parse_url("zoom://myhookid/mytoken/?mode=simple") |
| 411 |
assert result["mode"] == "simple" |
| 412 |
|
| 413 |
|
| 414 |
result = NotifyZoom.parse_url(None) |
| 415 |
assert result is None |
| 416 |
|
| 417 |
|
| 418 |
def test_plugin_zoom_parse_native_url(): |
| 419 |
"""Exercise parse_native_url().""" |
| 420 |
|
| 421 |
|
| 422 |
result = NotifyZoom.parse_native_url( |
| 423 |
"https://inbots.zoom.us/incoming/hook/{}?token={}".format( |
| 424 |
WEBHOOK_ID, TOKEN |
| 425 |
) |
| 426 |
) |
| 427 |
assert result is not None |
| 428 |
assert result["webhook_id"] == WEBHOOK_ID |
| 429 |
assert result["token"] == TOKEN |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
result = NotifyZoom.parse_native_url( |
| 434 |
"https://inbots.zoom.us/incoming/hook/{}".format(WEBHOOK_ID) |
| 435 |
) |
| 436 |
assert result is not None |
| 437 |
assert result["webhook_id"] == WEBHOOK_ID |
| 438 |
|
| 439 |
|
| 440 |
assert ( |
| 441 |
NotifyZoom.parse_native_url("https://example.com/not/a/zoom/url") |
| 442 |
is None |
| 443 |
) |
| 444 |
|
| 445 |
|
| 446 |
@mock.patch("requests.post") |
| 447 |
def test_plugin_zoom_apprise_integration(mock_post): |
| 448 |
"""Test end-to-end via Apprise.add() and Apprise.notify().""" |
| 449 |
r = mock.Mock() |
| 450 |
r.status_code = requests.codes.ok |
| 451 |
r.content = b"" |
| 452 |
mock_post.return_value = r |
| 453 |
|
| 454 |
app = Apprise() |
| 455 |
assert app.add("zoom://{}/{}".format(WEBHOOK_ID, TOKEN)) |
| 456 |
assert app.notify(title="Hello", body="World") |
| 457 |
assert mock_post.call_count == 1 |