| 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 |
import json |
| 29 |
|
| 30 |
|
| 31 |
import logging |
| 32 |
import os |
| 33 |
from unittest import mock |
| 34 |
|
| 35 |
from helpers import AppriseURLTester |
| 36 |
import pytest |
| 37 |
import requests |
| 38 |
|
| 39 |
from apprise import Apprise, AppriseAttachment |
| 40 |
from apprise.plugins.mattermost import MattermostMode, NotifyMattermost |
| 41 |
|
| 42 |
|
| 43 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 44 |
|
| 45 |
logging.disable(logging.CRITICAL) |
| 46 |
|
| 47 |
|
| 48 |
apprise_url_tests = ( |
| 49 |
("mmost://", {"instance": None}), |
| 50 |
("mmosts://", {"instance": None}), |
| 51 |
("mmost://:@/", {"instance": None}), |
| 52 |
( |
| 53 |
"mmosts://localhost", |
| 54 |
{ |
| 55 |
|
| 56 |
"instance": TypeError, |
| 57 |
}, |
| 58 |
), |
| 59 |
( |
| 60 |
"mmost://localhost/3ccdd113474722377935511fc85d3dd4", |
| 61 |
{ |
| 62 |
"instance": NotifyMattermost, |
| 63 |
}, |
| 64 |
), |
| 65 |
( |
| 66 |
( |
| 67 |
"mmost://localhost/3ccdd113474722377935511fc85d3dd4" |
| 68 |
"?icon_url=http://localhost/test.png" |
| 69 |
), |
| 70 |
{ |
| 71 |
"instance": NotifyMattermost, |
| 72 |
}, |
| 73 |
), |
| 74 |
( |
| 75 |
"mmost://user@localhost/3ccdd113474722377935511fc85d3dd4?channel=test", |
| 76 |
{ |
| 77 |
"instance": NotifyMattermost, |
| 78 |
}, |
| 79 |
), |
| 80 |
( |
| 81 |
( |
| 82 |
"mmost://user@localhost/3ccdd113474722377935511fc85d3dd4" |
| 83 |
"?channels=test" |
| 84 |
), |
| 85 |
{ |
| 86 |
"instance": NotifyMattermost, |
| 87 |
}, |
| 88 |
), |
| 89 |
( |
| 90 |
"mmost://user@localhost/3ccdd113474722377935511fc85d3dd4?to=test", |
| 91 |
{ |
| 92 |
"instance": NotifyMattermost, |
| 93 |
|
| 94 |
"privacy_url": "mmost://user@localhost/3...4/", |
| 95 |
}, |
| 96 |
), |
| 97 |
( |
| 98 |
( |
| 99 |
"mmost://localhost/3ccdd113474722377935511fc85d3dd4" |
| 100 |
"?to=test&image=True" |
| 101 |
), |
| 102 |
{"instance": NotifyMattermost}, |
| 103 |
), |
| 104 |
( |
| 105 |
( |
| 106 |
|
| 107 |
"mmost://localhost/3ccdd113474722377935511fc85d3dd4" |
| 108 |
"?to=test&team=chester" |
| 109 |
), |
| 110 |
{ |
| 111 |
"instance": NotifyMattermost, |
| 112 |
|
| 113 |
"requests_response_text": '{"file_infos": [{"id": "abc"}]}', |
| 114 |
}, |
| 115 |
), |
| 116 |
( |
| 117 |
( |
| 118 |
|
| 119 |
"mmost://localhost/3ccdd113474722377935511fc85d3dd4" |
| 120 |
"?to=general&botname=foobar" |
| 121 |
), |
| 122 |
{"instance": NotifyMattermost}, |
| 123 |
), |
| 124 |
( |
| 125 |
( |
| 126 |
"mmost://team@localhost/3ccdd113474722377935511fc85d3dd4" |
| 127 |
"?channel=$!garbag3^&mode=bot" |
| 128 |
), |
| 129 |
{ |
| 130 |
"instance": NotifyMattermost, |
| 131 |
|
| 132 |
"notify_response": False, |
| 133 |
}, |
| 134 |
), |
| 135 |
( |
| 136 |
( |
| 137 |
"mmost://localhost/3ccdd113474722377935511fc85d3dd4" |
| 138 |
"?to=test&image=False" |
| 139 |
), |
| 140 |
{"instance": NotifyMattermost}, |
| 141 |
), |
| 142 |
( |
| 143 |
( |
| 144 |
"mmost://localhost/3ccdd113474722377935511fc85d3dd4" |
| 145 |
"?to=test&image=True" |
| 146 |
), |
| 147 |
{ |
| 148 |
"instance": NotifyMattermost, |
| 149 |
|
| 150 |
"include_image": False, |
| 151 |
}, |
| 152 |
), |
| 153 |
( |
| 154 |
"mmost://localhost:8080/3ccdd113474722377935511fc85d3dd4", |
| 155 |
{ |
| 156 |
"instance": NotifyMattermost, |
| 157 |
|
| 158 |
"privacy_url": "mmost://localhost:8080/3...4/", |
| 159 |
}, |
| 160 |
), |
| 161 |
( |
| 162 |
"mmost://localhost:8080/3ccdd113474722377935511fc85d3dd4", |
| 163 |
{ |
| 164 |
"instance": NotifyMattermost, |
| 165 |
}, |
| 166 |
), |
| 167 |
( |
| 168 |
"mmost://localhost:invalid-port/3ccdd113474722377935511fc85d3dd4", |
| 169 |
{ |
| 170 |
"instance": None, |
| 171 |
}, |
| 172 |
), |
| 173 |
( |
| 174 |
"mmosts://localhost/3ccdd113474722377935511fc85d3dd4", |
| 175 |
{ |
| 176 |
"instance": NotifyMattermost, |
| 177 |
}, |
| 178 |
), |
| 179 |
( |
| 180 |
( |
| 181 |
"https://mattermost.example.com/hooks/" |
| 182 |
"3ccdd113474722377935511fc85d3dd4" |
| 183 |
), |
| 184 |
{ |
| 185 |
"instance": NotifyMattermost, |
| 186 |
|
| 187 |
"privacy_url": "mmosts://mattermost.example.com/3...4/", |
| 188 |
}, |
| 189 |
), |
| 190 |
|
| 191 |
( |
| 192 |
"mmosts://localhost/a/path/3ccdd113474722377935511fc85d3dd4", |
| 193 |
{"instance": NotifyMattermost}, |
| 194 |
), |
| 195 |
( |
| 196 |
"mmosts://localhost/////3ccdd113474722377935511fc85d3dd4///", |
| 197 |
{"instance": NotifyMattermost}, |
| 198 |
), |
| 199 |
|
| 200 |
("mmost://localhost/token?mode=w", {"instance": NotifyMattermost}), |
| 201 |
( |
| 202 |
"mmost://localhost/token?mode=b&to=channel-id-1", |
| 203 |
{ |
| 204 |
"instance": NotifyMattermost, |
| 205 |
|
| 206 |
"requests_response_text": '{"file_infos": [{"id": "abc"}]}', |
| 207 |
}, |
| 208 |
), |
| 209 |
( |
| 210 |
"mmost://localhost/token?mode=invalid", |
| 211 |
{ |
| 212 |
|
| 213 |
"instance": TypeError, |
| 214 |
}, |
| 215 |
), |
| 216 |
( |
| 217 |
"mmosts://localhost/a/path/3ccdd113474722377935511fc85d3dd4", |
| 218 |
{ |
| 219 |
"instance": NotifyMattermost, |
| 220 |
}, |
| 221 |
), |
| 222 |
( |
| 223 |
"mmosts://localhost/////3ccdd113474722377935511fc85d3dd4///", |
| 224 |
{ |
| 225 |
"instance": NotifyMattermost, |
| 226 |
}, |
| 227 |
), |
| 228 |
( |
| 229 |
"mmost://localhost/3ccdd113474722377935511fc85d3dd4", |
| 230 |
{ |
| 231 |
"instance": NotifyMattermost, |
| 232 |
|
| 233 |
"response": False, |
| 234 |
"requests_response_code": requests.codes.internal_server_error, |
| 235 |
}, |
| 236 |
), |
| 237 |
( |
| 238 |
"mmost://localhost/3ccdd113474722377935511fc85d3dd4", |
| 239 |
{ |
| 240 |
"instance": NotifyMattermost, |
| 241 |
|
| 242 |
"response": False, |
| 243 |
"requests_response_code": 999, |
| 244 |
}, |
| 245 |
), |
| 246 |
( |
| 247 |
"mmost://localhost/3ccdd113474722377935511fc85d3dd4", |
| 248 |
{ |
| 249 |
"instance": NotifyMattermost, |
| 250 |
|
| 251 |
|
| 252 |
"test_requests_exceptions": True, |
| 253 |
}, |
| 254 |
), |
| 255 |
) |
| 256 |
|
| 257 |
|
| 258 |
@pytest.fixture |
| 259 |
def request_get_mock(mocker): |
| 260 |
"""Prepare requests.get mock.""" |
| 261 |
mock_get = mocker.patch("requests.get") |
| 262 |
mock_get.return_value = requests.Request() |
| 263 |
mock_get.return_value.status_code = requests.codes.ok |
| 264 |
mock_get.return_value.content = b'{"id":"abc123"}' |
| 265 |
return mock_get |
| 266 |
|
| 267 |
|
| 268 |
@pytest.fixture |
| 269 |
def request_post_mock(mocker): |
| 270 |
"""Prepare requests mock.""" |
| 271 |
mock_post = mocker.patch("requests.post") |
| 272 |
mock_post.return_value = requests.Request() |
| 273 |
mock_post.return_value.status_code = requests.codes.ok |
| 274 |
mock_post.return_value.content = b"" |
| 275 |
return mock_post |
| 276 |
|
| 277 |
|
| 278 |
def test_plugin_mattermost_urls(): |
| 279 |
"""NotifyMattermost() Apprise URLs.""" |
| 280 |
|
| 281 |
|
| 282 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 283 |
|
| 284 |
|
| 285 |
def test_plugin_mattermost_edge_cases(): |
| 286 |
"""NotifyMattermost() Edge Cases.""" |
| 287 |
|
| 288 |
|
| 289 |
with pytest.raises(TypeError): |
| 290 |
NotifyMattermost(None) |
| 291 |
with pytest.raises(TypeError): |
| 292 |
NotifyMattermost(" ") |
| 293 |
|
| 294 |
|
| 295 |
def test_plugin_mattermost_len_webhook_and_bot( |
| 296 |
request_post_mock, request_get_mock |
| 297 |
): |
| 298 |
"""NotifyMattermost() __len__() behaviour.""" |
| 299 |
|
| 300 |
obj = Apprise.instantiate("mmost://localhost/token") |
| 301 |
assert isinstance(obj, NotifyMattermost) |
| 302 |
assert obj.mode == MattermostMode.WEBHOOK |
| 303 |
assert len(obj) == 1 |
| 304 |
|
| 305 |
|
| 306 |
obj = Apprise.instantiate("mmost://localhost/token?channels=a,b,c") |
| 307 |
assert isinstance(obj, NotifyMattermost) |
| 308 |
assert obj.mode == MattermostMode.WEBHOOK |
| 309 |
assert len(obj) == 3 |
| 310 |
|
| 311 |
|
| 312 |
obj = Apprise.instantiate("mmost://localhost/token?mode=bot&to=id1,id2") |
| 313 |
assert isinstance(obj, NotifyMattermost) |
| 314 |
assert obj.mode == MattermostMode.BOT |
| 315 |
assert len(obj) == 2 |
| 316 |
assert obj.notify("test") is True |
| 317 |
assert "mode=bot" in obj.url() |
| 318 |
|
| 319 |
|
| 320 |
obj = Apprise.instantiate("mmost://localhost/token?mode=bot&to=#chan,id1") |
| 321 |
assert isinstance(obj, NotifyMattermost) |
| 322 |
assert obj.mode == MattermostMode.BOT |
| 323 |
|
| 324 |
assert len(obj) == 1 |
| 325 |
assert obj.notify("test") is True |
| 326 |
assert "mode=bot" in obj.url() |
| 327 |
|
| 328 |
obj = Apprise.instantiate("mmost://localhost/token?mode=bot&to=#chan") |
| 329 |
assert isinstance(obj, NotifyMattermost) |
| 330 |
assert obj.mode == MattermostMode.BOT |
| 331 |
|
| 332 |
assert len(obj) == 1 |
| 333 |
assert obj.notify("test") is False |
| 334 |
assert "mode=bot" in obj.url() |
| 335 |
|
| 336 |
obj = Apprise.instantiate( |
| 337 |
"mmost://team@localhost/token?mode=bot&to=#chan,id1" |
| 338 |
) |
| 339 |
assert isinstance(obj, NotifyMattermost) |
| 340 |
assert obj.mode == MattermostMode.BOT |
| 341 |
assert len(obj) == 2 |
| 342 |
|
| 343 |
assert obj.notify("test") is True |
| 344 |
assert "mode=bot" in obj.url() |
| 345 |
|
| 346 |
assert obj.notify("test") is True |
| 347 |
|
| 348 |
obj = Apprise.instantiate( |
| 349 |
"mmost://team@localhost/token?mode=bot&to=#chan,id1" |
| 350 |
) |
| 351 |
assert isinstance(obj, NotifyMattermost) |
| 352 |
assert obj.mode == MattermostMode.BOT |
| 353 |
|
| 354 |
request_get_mock.return_value.content = b"}" |
| 355 |
assert obj.notify("test") is False |
| 356 |
|
| 357 |
obj = Apprise.instantiate( |
| 358 |
"mmost://team@localhost/token?mode=bot&to=#chan,id1" |
| 359 |
) |
| 360 |
assert isinstance(obj, NotifyMattermost) |
| 361 |
assert obj.mode == MattermostMode.BOT |
| 362 |
|
| 363 |
request_get_mock.return_value.content = b"{}" |
| 364 |
assert obj.notify("test") is False |
| 365 |
|
| 366 |
obj = Apprise.instantiate( |
| 367 |
"mmost://team@localhost/token?mode=bot&to=#chan,id1" |
| 368 |
) |
| 369 |
assert isinstance(obj, NotifyMattermost) |
| 370 |
assert obj.mode == MattermostMode.BOT |
| 371 |
|
| 372 |
request_get_mock.side_effect = requests.RequestException |
| 373 |
assert obj.notify("test") is False |
| 374 |
|
| 375 |
|
| 376 |
def test_plugin_mattermost_channels(request_post_mock): |
| 377 |
"""NotifyMattermost() Channel Testing.""" |
| 378 |
|
| 379 |
|
| 380 |
user = "user1" |
| 381 |
token = "token" |
| 382 |
channels = ["#one", "two"] |
| 383 |
|
| 384 |
|
| 385 |
obj = Apprise.instantiate( |
| 386 |
"mmost://{user}@localhost:8065/{token}?channels={channels}".format( |
| 387 |
user=user, token=token, channels=",".join(channels) |
| 388 |
) |
| 389 |
) |
| 390 |
|
| 391 |
assert isinstance(obj, NotifyMattermost) |
| 392 |
assert obj.notify(body="body", title="title") is True |
| 393 |
|
| 394 |
assert request_post_mock.called is True |
| 395 |
assert request_post_mock.call_count == 2 |
| 396 |
assert request_post_mock.call_args_list[0][0][0].startswith( |
| 397 |
"http://localhost:8065/hooks/token" |
| 398 |
) |
| 399 |
|
| 400 |
|
| 401 |
posted_json = json.loads(request_post_mock.call_args_list[0][1]["data"]) |
| 402 |
assert "username" in posted_json |
| 403 |
assert "channel" in posted_json |
| 404 |
assert "text" in posted_json |
| 405 |
assert posted_json["username"] == "user1" |
| 406 |
assert posted_json["channel"] == "one" |
| 407 |
assert posted_json["text"] == "title\r\nbody" |
| 408 |
|
| 409 |
|
| 410 |
posted_json = json.loads(request_post_mock.call_args_list[1][1]["data"]) |
| 411 |
assert posted_json["username"] == "user1" |
| 412 |
assert posted_json["channel"] == "two" |
| 413 |
assert posted_json["text"] == "title\r\nbody" |
| 414 |
|
| 415 |
|
| 416 |
def test_mattermost_post_default_port(request_post_mock): |
| 417 |
|
| 418 |
token = "token" |
| 419 |
|
| 420 |
|
| 421 |
obj = Apprise.instantiate(f"mmosts://mattermost.example.com/{token}") |
| 422 |
|
| 423 |
assert isinstance(obj, NotifyMattermost) |
| 424 |
assert obj.notify(body="body", title="title") is True |
| 425 |
|
| 426 |
|
| 427 |
assert request_post_mock.called is True |
| 428 |
assert request_post_mock.call_count == 1 |
| 429 |
assert request_post_mock.call_args_list[0][0][0].startswith( |
| 430 |
"https://mattermost.example.com/hooks/token" |
| 431 |
) |
| 432 |
|
| 433 |
|
| 434 |
posted_json = json.loads(request_post_mock.call_args_list[0][1]["data"]) |
| 435 |
assert "text" in posted_json |
| 436 |
assert posted_json["text"] == "title\r\nbody" |
| 437 |
|
| 438 |
|
| 439 |
def test_mattermost_icon_override(request_post_mock): |
| 440 |
|
| 441 |
token = "token" |
| 442 |
icon_url = "http://localhost/test.png" |
| 443 |
|
| 444 |
|
| 445 |
obj = Apprise.instantiate( |
| 446 |
"mmost://mattermost.example.com/{token}?icon_url={icon_url}".format( |
| 447 |
token=token, |
| 448 |
icon_url=icon_url, |
| 449 |
) |
| 450 |
) |
| 451 |
|
| 452 |
assert isinstance(obj, NotifyMattermost) |
| 453 |
assert obj.notify(body="body", title="title") is True |
| 454 |
|
| 455 |
assert request_post_mock.called is True |
| 456 |
assert request_post_mock.call_count == 1 |
| 457 |
|
| 458 |
|
| 459 |
posted_json = json.loads(request_post_mock.call_args_list[0][1]["data"]) |
| 460 |
assert posted_json["icon_url"] == icon_url |
| 461 |
|
| 462 |
|
| 463 |
def test_plugin_mattermost_webhook_payload_variants(request_post_mock, mocker): |
| 464 |
"""Webhook mode covers icon_url vs include_image branches, and optional |
| 465 |
channel.""" |
| 466 |
|
| 467 |
mocker.patch.object( |
| 468 |
NotifyMattermost, "image_url", return_value="http://img/ok.png" |
| 469 |
) |
| 470 |
|
| 471 |
|
| 472 |
obj = Apprise.instantiate("mmost://user@localhost/token?to=test") |
| 473 |
assert isinstance(obj, NotifyMattermost) |
| 474 |
assert obj.mode == MattermostMode.WEBHOOK |
| 475 |
assert obj.notify(body="body", title="title") is True |
| 476 |
|
| 477 |
posted_json = json.loads(request_post_mock.call_args_list[-1][1]["data"]) |
| 478 |
assert posted_json["username"] == "user" |
| 479 |
assert posted_json["channel"] == "test" |
| 480 |
assert posted_json["text"] == "title\r\nbody" |
| 481 |
assert posted_json["icon_url"] == "http://img/ok.png" |
| 482 |
|
| 483 |
|
| 484 |
request_post_mock.reset_mock() |
| 485 |
obj = Apprise.instantiate( |
| 486 |
"mmost://user@localhost/token?to=test&icon_url=http://x/icon.png" |
| 487 |
) |
| 488 |
assert isinstance(obj, NotifyMattermost) |
| 489 |
assert obj.notify(body="body", title="title") is True |
| 490 |
|
| 491 |
posted_json = json.loads(request_post_mock.call_args_list[0][1]["data"]) |
| 492 |
assert posted_json["icon_url"] == "http://x/icon.png" |
| 493 |
|
| 494 |
|
| 495 |
request_post_mock.reset_mock() |
| 496 |
obj = Apprise.instantiate("mmost://user@localhost/token") |
| 497 |
assert isinstance(obj, NotifyMattermost) |
| 498 |
assert obj.notify(body="body", title="title") is True |
| 499 |
|
| 500 |
posted_json = json.loads(request_post_mock.call_args_list[0][1]["data"]) |
| 501 |
assert "channel" not in posted_json |
| 502 |
|
| 503 |
|
| 504 |
def test_plugin_mattermost_webhook_http_error_and_exception( |
| 505 |
request_post_mock, mocker |
| 506 |
): |
| 507 |
"""Webhook mode error paths.""" |
| 508 |
obj = Apprise.instantiate("mmost://localhost/token?to=test") |
| 509 |
assert isinstance(obj, NotifyMattermost) |
| 510 |
assert obj.mode == MattermostMode.WEBHOOK |
| 511 |
|
| 512 |
|
| 513 |
request_post_mock.return_value.status_code = requests.codes.bad_request |
| 514 |
assert obj.notify(body="body", title="title") is False |
| 515 |
|
| 516 |
|
| 517 |
request_post_mock.reset_mock() |
| 518 |
request_post_mock.side_effect = requests.RequestException("boom") |
| 519 |
assert obj.notify(body="body", title="title") is False |
| 520 |
|
| 521 |
|
| 522 |
def test_plugin_mattermost_bot_mode_success_and_payload(request_post_mock): |
| 523 |
"""Bot mode success path, headers and payload.""" |
| 524 |
bearer = "bearerToken" |
| 525 |
channel_id = "channel-id-123" |
| 526 |
|
| 527 |
obj = Apprise.instantiate( |
| 528 |
f"mmosts://mattermost.example.com/{bearer}?mode=bot&to={channel_id}" |
| 529 |
) |
| 530 |
assert isinstance(obj, NotifyMattermost) |
| 531 |
assert obj.mode == MattermostMode.BOT |
| 532 |
|
| 533 |
|
| 534 |
request_post_mock.return_value.status_code = requests.codes.created |
| 535 |
|
| 536 |
assert obj.notify(body="body", title="title") is True |
| 537 |
assert request_post_mock.called is True |
| 538 |
assert request_post_mock.call_count == 1 |
| 539 |
|
| 540 |
url = request_post_mock.call_args_list[0][0][0] |
| 541 |
assert url.startswith("https://mattermost.example.com/api/v4/posts") |
| 542 |
|
| 543 |
headers = request_post_mock.call_args_list[0][1]["headers"] |
| 544 |
assert headers.get("Authorization") == f"Bearer {bearer}" |
| 545 |
|
| 546 |
posted_json = json.loads(request_post_mock.call_args_list[0][1]["data"]) |
| 547 |
assert posted_json["channel_id"] == channel_id |
| 548 |
assert posted_json["message"] == "title\r\nbody" |
| 549 |
assert "text" not in posted_json |
| 550 |
|
| 551 |
|
| 552 |
def test_plugin_mattermost_bot_mode_requires_channel_id(request_post_mock): |
| 553 |
"""Bot mode requires at least one channel_id target.""" |
| 554 |
bearer = "bearerToken" |
| 555 |
obj = Apprise.instantiate(f"mmost://localhost/{bearer}?mode=bot") |
| 556 |
assert isinstance(obj, NotifyMattermost) |
| 557 |
assert obj.mode == MattermostMode.BOT |
| 558 |
|
| 559 |
request_post_mock.return_value.status_code = requests.codes.created |
| 560 |
assert obj.notify(body="body", title="title") is False |
| 561 |
assert request_post_mock.called is False |
| 562 |
|
| 563 |
|
| 564 |
def test_plugin_mattermost_bot_mode_http_error_and_exception( |
| 565 |
request_post_mock, |
| 566 |
): |
| 567 |
"""Bot mode error paths.""" |
| 568 |
bearer = "bearerToken" |
| 569 |
obj = Apprise.instantiate(f"mmost://localhost/{bearer}?mode=bot&to=id1") |
| 570 |
assert isinstance(obj, NotifyMattermost) |
| 571 |
assert obj.mode == MattermostMode.BOT |
| 572 |
|
| 573 |
|
| 574 |
request_post_mock.return_value.status_code = requests.codes.unauthorized |
| 575 |
assert obj.notify(body="body", title="title") is False |
| 576 |
|
| 577 |
|
| 578 |
request_post_mock.reset_mock() |
| 579 |
request_post_mock.side_effect = requests.RequestException("boom") |
| 580 |
assert obj.notify(body="body", title="title") is False |
| 581 |
|
| 582 |
|
| 583 |
def test_plugin_mattermost_bot_channel_lookup_success( |
| 584 |
request_post_mock, request_get_mock |
| 585 |
): |
| 586 |
"""Bot mode resolves #channel via team lookup.""" |
| 587 |
bearer = "bearerToken" |
| 588 |
team = "myteam" |
| 589 |
channel = "#general" |
| 590 |
channel_id = "cid123" |
| 591 |
|
| 592 |
|
| 593 |
request_get_mock.return_value.content = json.dumps( |
| 594 |
{"id": channel_id} |
| 595 |
).encode("utf-8") |
| 596 |
|
| 597 |
obj = Apprise.instantiate( |
| 598 |
"mmost://localhost/{bearer}?mode=bot&team={team}&to={chan}".format( |
| 599 |
bearer=bearer, team=team, chan=channel |
| 600 |
) |
| 601 |
) |
| 602 |
assert isinstance(obj, NotifyMattermost) |
| 603 |
assert obj.mode == MattermostMode.BOT |
| 604 |
|
| 605 |
request_post_mock.return_value.status_code = requests.codes.created |
| 606 |
assert obj.notify(body="body", title="title") is True |
| 607 |
|
| 608 |
assert request_get_mock.called is True |
| 609 |
get_url = request_get_mock.call_args_list[0][0][0] |
| 610 |
assert "/api/v4/teams/name/" in get_url |
| 611 |
assert "/channels/name/" in get_url |
| 612 |
|
| 613 |
posted_json = json.loads(request_post_mock.call_args_list[0][1]["data"]) |
| 614 |
assert posted_json["channel_id"] == channel_id |
| 615 |
assert posted_json["message"] == "title\r\nbody" |
| 616 |
|
| 617 |
|
| 618 |
def test_plugin_mattermost_bot_channel_lookup_partial_success( |
| 619 |
request_post_mock, request_get_mock |
| 620 |
): |
| 621 |
"""One lookup fails, one succeeds, overall result is False.""" |
| 622 |
bearer = "bearerToken" |
| 623 |
team = "myteam" |
| 624 |
|
| 625 |
def side_effect(*args, **kwargs): |
| 626 |
r = requests.Request() |
| 627 |
url = args[0] |
| 628 |
if url.endswith("/channels/name/good"): |
| 629 |
r.status_code = requests.codes.ok |
| 630 |
r.content = b'{"id":"cid-good"}' |
| 631 |
else: |
| 632 |
r.status_code = requests.codes.not_found |
| 633 |
r.content = b"" |
| 634 |
return r |
| 635 |
|
| 636 |
request_get_mock.side_effect = side_effect |
| 637 |
|
| 638 |
obj = Apprise.instantiate( |
| 639 |
"mmost://localhost/{bearer}?mode=bot&team={team}&to=#good,#bad".format( |
| 640 |
bearer=bearer, team=team |
| 641 |
) |
| 642 |
) |
| 643 |
assert isinstance(obj, NotifyMattermost) |
| 644 |
request_post_mock.return_value.status_code = requests.codes.created |
| 645 |
|
| 646 |
assert obj.notify(body="body", title="title") is False |
| 647 |
assert request_post_mock.call_count == 1 |
| 648 |
posted_json = json.loads(request_post_mock.call_args_list[0][1]["data"]) |
| 649 |
assert posted_json["channel_id"] == "cid-good" |
| 650 |
|
| 651 |
|
| 652 |
def test_plugin_mattermost_webhook_attachment_warning(request_post_mock): |
| 653 |
"""Webhook mode has attachment_support=False; pipeline drops attach.""" |
| 654 |
obj = Apprise.instantiate("mmost://localhost/token") |
| 655 |
assert isinstance(obj, NotifyMattermost) |
| 656 |
assert obj.mode == MattermostMode.WEBHOOK |
| 657 |
assert obj.attachment_support is False |
| 658 |
|
| 659 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 660 |
|
| 661 |
|
| 662 |
assert obj.notify(body="body", title="title", attach=attach) is True |
| 663 |
assert request_post_mock.call_count == 1 |
| 664 |
|
| 665 |
|
| 666 |
assert "files" not in str(request_post_mock.call_args) |
| 667 |
|
| 668 |
|
| 669 |
def test_plugin_mattermost_webhook_botname_in_url(request_post_mock): |
| 670 |
"""Webhook botname is the {user}@ URL prefix; ?botname= is an alias.""" |
| 671 |
|
| 672 |
obj = Apprise.instantiate("mmost://mybot@localhost/token?to=general") |
| 673 |
obj2 = Apprise.instantiate( |
| 674 |
"mmost://localhost/token?botname=mybot&to=general" |
| 675 |
) |
| 676 |
assert isinstance(obj, NotifyMattermost) |
| 677 |
assert isinstance(obj2, NotifyMattermost) |
| 678 |
assert obj.mode == MattermostMode.WEBHOOK |
| 679 |
assert obj.user == "mybot" |
| 680 |
assert obj2.user == "mybot" |
| 681 |
|
| 682 |
|
| 683 |
url = obj.url() |
| 684 |
assert "mybot@" in url |
| 685 |
assert "botname" not in url |
| 686 |
|
| 687 |
|
| 688 |
obj3 = Apprise.instantiate(url) |
| 689 |
assert isinstance(obj3, NotifyMattermost) |
| 690 |
assert obj3.user == "mybot" |
| 691 |
assert obj3.mode == MattermostMode.WEBHOOK |
| 692 |
|
| 693 |
|
| 694 |
request_post_mock.return_value.status_code = requests.codes.ok |
| 695 |
assert obj.notify(body="body", title="title") is True |
| 696 |
posted = json.loads(request_post_mock.call_args_list[0][1]["data"]) |
| 697 |
assert posted["username"] == "mybot" |
| 698 |
|
| 699 |
|
| 700 |
def test_plugin_mattermost_bot_team_in_url(request_post_mock): |
| 701 |
"""Bot mode team is the {user}@ URL prefix; ?team= is an alias.""" |
| 702 |
obj = Apprise.instantiate("mmost://myteam@localhost/token?mode=bot&to=id1") |
| 703 |
assert isinstance(obj, NotifyMattermost) |
| 704 |
assert obj.mode == MattermostMode.BOT |
| 705 |
assert obj.user == "myteam" |
| 706 |
|
| 707 |
|
| 708 |
url = obj.url() |
| 709 |
assert "myteam@" in url |
| 710 |
assert "mode=bot" in url |
| 711 |
|
| 712 |
|
| 713 |
def test_plugin_mattermost_bot_attachments(request_post_mock): |
| 714 |
"""Bot mode uploads attachments and includes file_ids in the post.""" |
| 715 |
bearer = "bearerToken" |
| 716 |
channel_id = "channel-id-123" |
| 717 |
file_id = "uploaded-file-id-abc" |
| 718 |
|
| 719 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 720 |
|
| 721 |
def _mk_resp(content, code=requests.codes.ok): |
| 722 |
r = requests.Request() |
| 723 |
r.status_code = code |
| 724 |
r.content = content |
| 725 |
return r |
| 726 |
|
| 727 |
upload_resp = _mk_resp( |
| 728 |
json.dumps({"file_infos": [{"id": file_id}]}).encode("utf-8") |
| 729 |
) |
| 730 |
post_resp = _mk_resp(b"{}", requests.codes.created) |
| 731 |
|
| 732 |
request_post_mock.side_effect = [upload_resp, post_resp] |
| 733 |
|
| 734 |
obj = Apprise.instantiate( |
| 735 |
f"mmost://localhost/{bearer}?mode=bot&to={channel_id}" |
| 736 |
) |
| 737 |
assert isinstance(obj, NotifyMattermost) |
| 738 |
assert obj.mode == MattermostMode.BOT |
| 739 |
|
| 740 |
assert obj.notify(body="body", title="title", attach=attach) is True |
| 741 |
assert request_post_mock.call_count == 2 |
| 742 |
|
| 743 |
|
| 744 |
upload_call = request_post_mock.call_args_list[0] |
| 745 |
assert "/api/v4/files" in upload_call[0][0] |
| 746 |
assert upload_call[1]["data"] == {"channel_id": channel_id} |
| 747 |
assert "files" in upload_call[1]["files"] |
| 748 |
|
| 749 |
|
| 750 |
post_call = request_post_mock.call_args_list[1] |
| 751 |
assert "/api/v4/posts" in post_call[0][0] |
| 752 |
post_json = json.loads(post_call[1]["data"]) |
| 753 |
assert post_json["file_ids"] == [file_id] |
| 754 |
assert post_json["message"] == "title\r\nbody" |
| 755 |
assert post_json["channel_id"] == channel_id |
| 756 |
|
| 757 |
|
| 758 |
def test_plugin_mattermost_bot_multi_attach_multi_target(request_post_mock): |
| 759 |
"""Multiple attachments and multiple targets each get their own uploads.""" |
| 760 |
bearer = "bearerToken" |
| 761 |
ch1 = "channel-001" |
| 762 |
ch2 = "channel-002" |
| 763 |
fid1 = "fid-001" |
| 764 |
fid2 = "fid-002" |
| 765 |
|
| 766 |
attach = AppriseAttachment() |
| 767 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 768 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png")) |
| 769 |
|
| 770 |
def _mk_upload(fid): |
| 771 |
r = requests.Request() |
| 772 |
r.status_code = requests.codes.ok |
| 773 |
r.content = json.dumps({"file_infos": [{"id": fid}]}).encode("utf-8") |
| 774 |
return r |
| 775 |
|
| 776 |
def _mk_post(): |
| 777 |
r = requests.Request() |
| 778 |
r.status_code = requests.codes.created |
| 779 |
r.content = b"{}" |
| 780 |
return r |
| 781 |
|
| 782 |
|
| 783 |
request_post_mock.side_effect = [ |
| 784 |
_mk_upload(fid1), |
| 785 |
_mk_upload(fid2), |
| 786 |
_mk_post(), |
| 787 |
_mk_upload(fid1), |
| 788 |
_mk_upload(fid2), |
| 789 |
_mk_post(), |
| 790 |
] |
| 791 |
|
| 792 |
obj = Apprise.instantiate( |
| 793 |
"mmost://localhost/{b}?mode=bot&to={c1},{c2}".format( |
| 794 |
b=bearer, c1=ch1, c2=ch2 |
| 795 |
) |
| 796 |
) |
| 797 |
assert isinstance(obj, NotifyMattermost) |
| 798 |
assert obj.notify(body="body", title="title", attach=attach) is True |
| 799 |
assert request_post_mock.call_count == 6 |
| 800 |
|
| 801 |
|
| 802 |
post1 = json.loads(request_post_mock.call_args_list[2][1]["data"]) |
| 803 |
assert post1["file_ids"] == [fid1, fid2] |
| 804 |
assert post1["channel_id"] == ch1 |
| 805 |
|
| 806 |
post2 = json.loads(request_post_mock.call_args_list[5][1]["data"]) |
| 807 |
assert post2["file_ids"] == [fid1, fid2] |
| 808 |
assert post2["channel_id"] == ch2 |
| 809 |
|
| 810 |
|
| 811 |
def test_plugin_mattermost_bot_attachment_upload_http_error( |
| 812 |
request_post_mock, |
| 813 |
): |
| 814 |
"""Upload HTTP error marks the whole target as failed.""" |
| 815 |
bearer = "bearerToken" |
| 816 |
channel_id = "channel-id-123" |
| 817 |
|
| 818 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 819 |
|
| 820 |
r = requests.Request() |
| 821 |
r.status_code = requests.codes.internal_server_error |
| 822 |
r.content = b"" |
| 823 |
request_post_mock.return_value = r |
| 824 |
|
| 825 |
obj = Apprise.instantiate( |
| 826 |
f"mmost://localhost/{bearer}?mode=bot&to={channel_id}" |
| 827 |
) |
| 828 |
assert isinstance(obj, NotifyMattermost) |
| 829 |
assert obj.notify(body="body", title="title", attach=attach) is False |
| 830 |
|
| 831 |
assert request_post_mock.call_count == 1 |
| 832 |
assert "/api/v4/files" in request_post_mock.call_args_list[0][0][0] |
| 833 |
|
| 834 |
|
| 835 |
def test_plugin_mattermost_bot_attachment_request_exception( |
| 836 |
request_post_mock, |
| 837 |
): |
| 838 |
"""RequestException during upload marks the target as failed.""" |
| 839 |
bearer = "bearerToken" |
| 840 |
channel_id = "channel-id-123" |
| 841 |
|
| 842 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 843 |
|
| 844 |
request_post_mock.side_effect = requests.RequestException("boom") |
| 845 |
|
| 846 |
obj = Apprise.instantiate( |
| 847 |
f"mmost://localhost/{bearer}?mode=bot&to={channel_id}" |
| 848 |
) |
| 849 |
assert isinstance(obj, NotifyMattermost) |
| 850 |
assert obj.notify(body="body", title="title", attach=attach) is False |
| 851 |
assert request_post_mock.call_count == 1 |
| 852 |
|
| 853 |
|
| 854 |
def test_plugin_mattermost_bot_attachment_ioerror(request_post_mock): |
| 855 |
"""OSError reading the attachment marks the target as failed.""" |
| 856 |
bearer = "bearerToken" |
| 857 |
channel_id = "channel-id-123" |
| 858 |
|
| 859 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 860 |
|
| 861 |
with mock.patch( |
| 862 |
"apprise.attachment.file.AttachFile.open", |
| 863 |
side_effect=OSError("disk error"), |
| 864 |
): |
| 865 |
obj = Apprise.instantiate( |
| 866 |
f"mmost://localhost/{bearer}?mode=bot&to={channel_id}" |
| 867 |
) |
| 868 |
assert isinstance(obj, NotifyMattermost) |
| 869 |
assert obj.notify(body="body", title="title", attach=attach) is False |
| 870 |
|
| 871 |
|
| 872 |
assert request_post_mock.call_count == 0 |
| 873 |
|
| 874 |
|
| 875 |
def test_plugin_mattermost_bot_attachment_valueerror(request_post_mock): |
| 876 |
"""ValueError from a closed stream is handled the same as OSError.""" |
| 877 |
bearer = "bearerToken" |
| 878 |
channel_id = "channel-id-123" |
| 879 |
|
| 880 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 881 |
|
| 882 |
with mock.patch( |
| 883 |
"apprise.attachment.file.AttachFile.open", |
| 884 |
side_effect=ValueError("I/O operation on closed file"), |
| 885 |
): |
| 886 |
obj = Apprise.instantiate( |
| 887 |
f"mmost://localhost/{bearer}?mode=bot&to={channel_id}" |
| 888 |
) |
| 889 |
assert isinstance(obj, NotifyMattermost) |
| 890 |
assert obj.notify(body="body", title="title", attach=attach) is False |
| 891 |
|
| 892 |
assert request_post_mock.call_count == 0 |
| 893 |
|
| 894 |
|
| 895 |
def test_plugin_mattermost_bot_attachment_bad_response(request_post_mock): |
| 896 |
"""Unparseable upload response marks the target as failed.""" |
| 897 |
bearer = "bearerToken" |
| 898 |
channel_id = "channel-id-123" |
| 899 |
|
| 900 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 901 |
|
| 902 |
r = requests.Request() |
| 903 |
r.status_code = requests.codes.ok |
| 904 |
r.content = b"not-json{{{" |
| 905 |
request_post_mock.return_value = r |
| 906 |
|
| 907 |
obj = Apprise.instantiate( |
| 908 |
f"mmost://localhost/{bearer}?mode=bot&to={channel_id}" |
| 909 |
) |
| 910 |
assert isinstance(obj, NotifyMattermost) |
| 911 |
assert obj.notify(body="body", title="title", attach=attach) is False |
| 912 |
assert request_post_mock.call_count == 1 |
| 913 |
|
| 914 |
|
| 915 |
def test_plugin_mattermost_bot_attachment_no_file_id(request_post_mock): |
| 916 |
"""Upload response with empty file_id marks the target as failed.""" |
| 917 |
bearer = "bearerToken" |
| 918 |
channel_id = "channel-id-123" |
| 919 |
|
| 920 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 921 |
|
| 922 |
r = requests.Request() |
| 923 |
r.status_code = requests.codes.ok |
| 924 |
|
| 925 |
r.content = json.dumps({"file_infos": [{"id": ""}]}).encode("utf-8") |
| 926 |
request_post_mock.return_value = r |
| 927 |
|
| 928 |
obj = Apprise.instantiate( |
| 929 |
f"mmost://localhost/{bearer}?mode=bot&to={channel_id}" |
| 930 |
) |
| 931 |
assert isinstance(obj, NotifyMattermost) |
| 932 |
assert obj.notify(body="body", title="title", attach=attach) is False |
| 933 |
|
| 934 |
|
| 935 |
def test_plugin_mattermost_bot_attachment_invalid(request_post_mock): |
| 936 |
"""An inaccessible attachment path marks the target as failed.""" |
| 937 |
bearer = "bearerToken" |
| 938 |
channel_id = "channel-id-123" |
| 939 |
|
| 940 |
attach = AppriseAttachment("/path/does/not/exist.gif") |
| 941 |
|
| 942 |
obj = Apprise.instantiate( |
| 943 |
f"mmost://localhost/{bearer}?mode=bot&to={channel_id}" |
| 944 |
) |
| 945 |
assert isinstance(obj, NotifyMattermost) |
| 946 |
assert obj.notify(body="body", title="title", attach=attach) is False |
| 947 |
|
| 948 |
assert request_post_mock.call_count == 0 |
| 949 |
|
| 950 |
|
| 951 |
def test_plugin_mattermost_bot_attachment_partial_target_failure( |
| 952 |
request_post_mock, |
| 953 |
): |
| 954 |
"""Attachment upload failure on one target does not prevent others.""" |
| 955 |
bearer = "bearerToken" |
| 956 |
ch1 = "channel-001" |
| 957 |
ch2 = "channel-002" |
| 958 |
fid = "fid-001" |
| 959 |
|
| 960 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 961 |
|
| 962 |
def _mk_upload_ok(): |
| 963 |
r = requests.Request() |
| 964 |
r.status_code = requests.codes.ok |
| 965 |
r.content = json.dumps({"file_infos": [{"id": fid}]}).encode("utf-8") |
| 966 |
return r |
| 967 |
|
| 968 |
def _mk_upload_fail(): |
| 969 |
r = requests.Request() |
| 970 |
r.status_code = requests.codes.internal_server_error |
| 971 |
r.content = b"" |
| 972 |
return r |
| 973 |
|
| 974 |
def _mk_post(): |
| 975 |
r = requests.Request() |
| 976 |
r.status_code = requests.codes.created |
| 977 |
r.content = b"{}" |
| 978 |
return r |
| 979 |
|
| 980 |
|
| 981 |
request_post_mock.side_effect = [ |
| 982 |
_mk_upload_ok(), |
| 983 |
_mk_post(), |
| 984 |
_mk_upload_fail(), |
| 985 |
] |
| 986 |
|
| 987 |
obj = Apprise.instantiate( |
| 988 |
"mmost://localhost/{b}?mode=bot&to={c1},{c2}".format( |
| 989 |
b=bearer, c1=ch1, c2=ch2 |
| 990 |
) |
| 991 |
) |
| 992 |
assert isinstance(obj, NotifyMattermost) |
| 993 |
|
| 994 |
assert obj.notify(body="body", title="title", attach=attach) is False |
| 995 |
|
| 996 |
assert request_post_mock.call_count == 3 |
| 997 |
|
| 998 |
|
| 999 |
def test_plugin_mattermost_bot_attachment_memory_multi_target( |
| 1000 |
request_post_mock, |
| 1001 |
): |
| 1002 |
"""Attachment bytes are pre-read once so every target gets a fresh stream. |
| 1003 |
|
| 1004 |
Simulates an attachment whose open() raises ValueError on the second |
| 1005 |
call (as AttachMemory does when its BytesIO is closed after the first |
| 1006 |
read). Our pre-read approach calls open() exactly once per attachment |
| 1007 |
before the target loop, then wraps the bytes in a fresh io.BytesIO for |
| 1008 |
each target upload. |
| 1009 |
""" |
| 1010 |
bearer = "bearerToken" |
| 1011 |
ch1 = "channel-001" |
| 1012 |
ch2 = "channel-002" |
| 1013 |
fid = "fid-001" |
| 1014 |
|
| 1015 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
_orig_open = attach.attachments[0].open |
| 1020 |
open_calls = [0] |
| 1021 |
|
| 1022 |
def _open_once(*args, **kwargs): |
| 1023 |
open_calls[0] += 1 |
| 1024 |
if open_calls[0] > 1: |
| 1025 |
raise ValueError("stream already closed") |
| 1026 |
return _orig_open(*args, **kwargs) |
| 1027 |
|
| 1028 |
def _mk_upload(): |
| 1029 |
r = requests.Request() |
| 1030 |
r.status_code = requests.codes.ok |
| 1031 |
r.content = json.dumps({"file_infos": [{"id": fid}]}).encode("utf-8") |
| 1032 |
return r |
| 1033 |
|
| 1034 |
def _mk_post(): |
| 1035 |
r = requests.Request() |
| 1036 |
r.status_code = requests.codes.created |
| 1037 |
r.content = b"{}" |
| 1038 |
return r |
| 1039 |
|
| 1040 |
|
| 1041 |
request_post_mock.side_effect = [ |
| 1042 |
_mk_upload(), |
| 1043 |
_mk_post(), |
| 1044 |
_mk_upload(), |
| 1045 |
_mk_post(), |
| 1046 |
] |
| 1047 |
|
| 1048 |
obj = Apprise.instantiate( |
| 1049 |
"mmost://localhost/{b}?mode=bot&to={c1},{c2}".format( |
| 1050 |
b=bearer, c1=ch1, c2=ch2 |
| 1051 |
) |
| 1052 |
) |
| 1053 |
assert isinstance(obj, NotifyMattermost) |
| 1054 |
|
| 1055 |
with mock.patch.object(attach.attachments[0], "open", _open_once): |
| 1056 |
assert obj.notify(body="body", title="title", attach=attach) is True |
| 1057 |
|
| 1058 |
|
| 1059 |
assert open_calls[0] == 1 |
| 1060 |
|
| 1061 |
assert request_post_mock.call_count == 4 |
| 1062 |
|
| 1063 |
post1 = json.loads(request_post_mock.call_args_list[1][1]["data"]) |
| 1064 |
assert post1["file_ids"] == [fid] |
| 1065 |
assert post1["channel_id"] == ch1 |
| 1066 |
|
| 1067 |
post2 = json.loads(request_post_mock.call_args_list[3][1]["data"]) |
| 1068 |
assert post2["file_ids"] == [fid] |
| 1069 |
assert post2["channel_id"] == ch2 |