| 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 |
from inspect import cleandoc |
| 29 |
from json import dumps, loads |
| 30 |
|
| 31 |
|
| 32 |
import logging |
| 33 |
import os |
| 34 |
from unittest import mock |
| 35 |
|
| 36 |
from helpers import AppriseURLTester |
| 37 |
import pytest |
| 38 |
import requests |
| 39 |
|
| 40 |
from apprise import Apprise, AppriseAttachment, NotifyFormat, NotifyType |
| 41 |
from apprise.plugins.slack import NotifySlack, SlackMode |
| 42 |
|
| 43 |
logging.disable(logging.CRITICAL) |
| 44 |
|
| 45 |
|
| 46 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 47 |
|
| 48 |
|
| 49 |
apprise_url_tests = ( |
| 50 |
( |
| 51 |
"slack://", |
| 52 |
{ |
| 53 |
"instance": TypeError, |
| 54 |
}, |
| 55 |
), |
| 56 |
( |
| 57 |
"slack://:@/", |
| 58 |
{ |
| 59 |
"instance": TypeError, |
| 60 |
}, |
| 61 |
), |
| 62 |
( |
| 63 |
"slack://T1JJ3T3L2", |
| 64 |
{ |
| 65 |
|
| 66 |
"instance": TypeError, |
| 67 |
}, |
| 68 |
), |
| 69 |
( |
| 70 |
"slack://T1JJ3T3L2/A1BRTD4JD/", |
| 71 |
{ |
| 72 |
|
| 73 |
"instance": TypeError, |
| 74 |
}, |
| 75 |
), |
| 76 |
( |
| 77 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/?mode=invalid", |
| 78 |
{ |
| 79 |
|
| 80 |
"instance": TypeError, |
| 81 |
}, |
| 82 |
), |
| 83 |
( |
| 84 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/#hmm/#-invalid-", |
| 85 |
{ |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
"instance": NotifySlack, |
| 90 |
|
| 91 |
|
| 92 |
"response": False, |
| 93 |
"requests_response_text": { |
| 94 |
"ok": False, |
| 95 |
"message": "Bad Channel", |
| 96 |
}, |
| 97 |
}, |
| 98 |
), |
| 99 |
( |
| 100 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/#channel", |
| 101 |
{ |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
"instance": NotifySlack, |
| 106 |
|
| 107 |
"include_image": False, |
| 108 |
"requests_response_text": "ok", |
| 109 |
}, |
| 110 |
), |
| 111 |
( |
| 112 |
( |
| 113 |
"slack://[email protected]/#nuxref?footer=no" |
| 114 |
"×tamp=yes" |
| 115 |
), |
| 116 |
{ |
| 117 |
"instance": NotifySlack, |
| 118 |
"requests_response_text": { |
| 119 |
"ok": True, |
| 120 |
"message": "", |
| 121 |
}, |
| 122 |
}, |
| 123 |
), |
| 124 |
( |
| 125 |
( |
| 126 |
"slack://[email protected]/#nuxref?footer=yes" |
| 127 |
"×tamp=no" |
| 128 |
), |
| 129 |
{ |
| 130 |
"instance": NotifySlack, |
| 131 |
"requests_response_text": { |
| 132 |
"ok": True, |
| 133 |
"message": "", |
| 134 |
}, |
| 135 |
}, |
| 136 |
), |
| 137 |
|
| 138 |
( |
| 139 |
( |
| 140 |
"slack://?token=xoxe.xoxb-1234-1234-abc124&to=#nuxref&footer=no" |
| 141 |
"&user=test" |
| 142 |
), |
| 143 |
{ |
| 144 |
"instance": NotifySlack, |
| 145 |
"requests_response_text": { |
| 146 |
"ok": True, |
| 147 |
"message": "", |
| 148 |
}, |
| 149 |
"privacy_url": "slack://[email protected]/nuxref/", |
| 150 |
}, |
| 151 |
), |
| 152 |
( |
| 153 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/+id/@id/", |
| 154 |
{ |
| 155 |
|
| 156 |
|
| 157 |
"instance": NotifySlack, |
| 158 |
"requests_response_text": "ok", |
| 159 |
}, |
| 160 |
), |
| 161 |
( |
| 162 |
( |
| 163 |
"slack://username@T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 164 |
"?to=#nuxref" |
| 165 |
), |
| 166 |
{ |
| 167 |
"instance": NotifySlack, |
| 168 |
|
| 169 |
"privacy_url": "slack://[email protected]/A...D/T...Q/", |
| 170 |
"requests_response_text": "ok", |
| 171 |
}, |
| 172 |
), |
| 173 |
( |
| 174 |
( |
| 175 |
"slack://username@T1JJ3T3L2/A1BRTD4JD/" |
| 176 |
"TIiajkdnlazkcOXrIdevi7FQ/#nuxref" |
| 177 |
), |
| 178 |
{ |
| 179 |
"instance": NotifySlack, |
| 180 |
"requests_response_text": "ok", |
| 181 |
}, |
| 182 |
), |
| 183 |
|
| 184 |
( |
| 185 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnl/[email protected]", |
| 186 |
{ |
| 187 |
"instance": NotifySlack, |
| 188 |
"requests_response_text": "ok", |
| 189 |
|
| 190 |
"notify_response": False, |
| 191 |
}, |
| 192 |
), |
| 193 |
|
| 194 |
( |
| 195 |
"slack://bot@_/#nuxref?token=T1JJ3T3L2/A1BRTD4JD/TIiajkdnadfdajkjkfl/", |
| 196 |
{ |
| 197 |
"instance": NotifySlack, |
| 198 |
"requests_response_text": "ok", |
| 199 |
}, |
| 200 |
), |
| 201 |
|
| 202 |
( |
| 203 |
( |
| 204 |
"slack://?token=T1JJ3T3L2/A1BRTD4JD" |
| 205 |
"/TIiajkdnlazkcOXrIdevi7FQ/&to=#chan" |
| 206 |
), |
| 207 |
{ |
| 208 |
"instance": NotifySlack, |
| 209 |
"requests_response_text": "ok", |
| 210 |
}, |
| 211 |
), |
| 212 |
|
| 213 |
( |
| 214 |
( |
| 215 |
"slack://username@T1JJ3T3L2/A1BRTD4JD/" |
| 216 |
"TIiajkdnlazkcOXrIdevi7FQ/#nuxref" |
| 217 |
), |
| 218 |
{ |
| 219 |
"instance": NotifySlack, |
| 220 |
"requests_response_text": "fail", |
| 221 |
|
| 222 |
"notify_response": False, |
| 223 |
}, |
| 224 |
), |
| 225 |
|
| 226 |
( |
| 227 |
( |
| 228 |
"slack://username@xoxb-1234-1234-abc124/#nuxref?footer=no" |
| 229 |
"×tamp=yes" |
| 230 |
), |
| 231 |
{ |
| 232 |
"instance": NotifySlack, |
| 233 |
"requests_response_text": { |
| 234 |
"ok": True, |
| 235 |
"message": "", |
| 236 |
}, |
| 237 |
}, |
| 238 |
), |
| 239 |
( |
| 240 |
( |
| 241 |
"slack://username@xoxb-1234-1234-abc124/#nuxref?footer=yes" |
| 242 |
"×tamp=yes" |
| 243 |
), |
| 244 |
{ |
| 245 |
"instance": NotifySlack, |
| 246 |
"requests_response_text": { |
| 247 |
"ok": True, |
| 248 |
"message": "", |
| 249 |
}, |
| 250 |
}, |
| 251 |
), |
| 252 |
( |
| 253 |
( |
| 254 |
"slack://username@xoxb-1234-1234-abc124/#nuxref?footer=yes" |
| 255 |
"×tamp=no" |
| 256 |
), |
| 257 |
{ |
| 258 |
"instance": NotifySlack, |
| 259 |
"requests_response_text": { |
| 260 |
"ok": True, |
| 261 |
"message": "", |
| 262 |
}, |
| 263 |
}, |
| 264 |
), |
| 265 |
( |
| 266 |
( |
| 267 |
"slack://username@xoxb-1234-1234-abc124/#nuxref?footer=yes" |
| 268 |
"×tamp=no" |
| 269 |
), |
| 270 |
{ |
| 271 |
"instance": NotifySlack, |
| 272 |
"requests_response_text": { |
| 273 |
"ok": True, |
| 274 |
"message": "", |
| 275 |
}, |
| 276 |
}, |
| 277 |
), |
| 278 |
|
| 279 |
( |
| 280 |
( |
| 281 |
"slack://?token=T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 282 |
"&to=#chan&mode=hook" |
| 283 |
), |
| 284 |
{"instance": NotifySlack, "requests_response_text": "ok"}, |
| 285 |
), |
| 286 |
|
| 287 |
( |
| 288 |
( |
| 289 |
"slack://?token=T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 290 |
"&to=#chan&mode=bot" |
| 291 |
), |
| 292 |
{"instance": TypeError}, |
| 293 |
), |
| 294 |
|
| 295 |
( |
| 296 |
( |
| 297 |
"slack://?token=T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 298 |
"&to=#chan&blocks=yes&footer=yes×tamp=no" |
| 299 |
), |
| 300 |
{"instance": NotifySlack, "requests_response_text": "ok"}, |
| 301 |
), |
| 302 |
|
| 303 |
( |
| 304 |
( |
| 305 |
"slack://?token=T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 306 |
"&to=#chan&blocks=yes&footer=yes×tamp=yes" |
| 307 |
), |
| 308 |
{"instance": NotifySlack, "requests_response_text": "ok"}, |
| 309 |
), |
| 310 |
|
| 311 |
( |
| 312 |
( |
| 313 |
"slack://?token=T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 314 |
"&to=#chan&blocks=yes&footer=no×tamp=yes" |
| 315 |
), |
| 316 |
{"instance": NotifySlack, "requests_response_text": "ok"}, |
| 317 |
), |
| 318 |
|
| 319 |
( |
| 320 |
( |
| 321 |
"slack://?token=T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 322 |
"&to=#chan&blocks=yes&footer=no×tamp=no" |
| 323 |
), |
| 324 |
{"instance": NotifySlack, "requests_response_text": "ok"}, |
| 325 |
), |
| 326 |
( |
| 327 |
( |
| 328 |
"slack://?token=T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 329 |
"&to=#chan&blocks=yes&footer=yes&image=no" |
| 330 |
), |
| 331 |
{"instance": NotifySlack, "requests_response_text": "ok"}, |
| 332 |
), |
| 333 |
( |
| 334 |
( |
| 335 |
"slack://?token=T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 336 |
"&to=#chan&blocks=yes&format=text" |
| 337 |
), |
| 338 |
{"instance": NotifySlack, "requests_response_text": "ok"}, |
| 339 |
), |
| 340 |
( |
| 341 |
( |
| 342 |
"slack://?token=T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 343 |
"&to=#chan&blocks=no&format=text" |
| 344 |
), |
| 345 |
{"instance": NotifySlack, "requests_response_text": "ok"}, |
| 346 |
), |
| 347 |
|
| 348 |
( |
| 349 |
"slack://?token=xoxb-1234-1234-abc124&to=#nuxref&footer=no&user=test", |
| 350 |
{ |
| 351 |
"instance": NotifySlack, |
| 352 |
"requests_response_text": { |
| 353 |
"ok": True, |
| 354 |
"message": "", |
| 355 |
}, |
| 356 |
|
| 357 |
"privacy_url": "slack://[email protected]/nuxref/", |
| 358 |
}, |
| 359 |
), |
| 360 |
|
| 361 |
( |
| 362 |
"slack://?token=xoxb-1234-1234-abc124&to=#nuxref,#$,#-&footer=no", |
| 363 |
{ |
| 364 |
"instance": NotifySlack, |
| 365 |
"requests_response_text": { |
| 366 |
"ok": True, |
| 367 |
"message": "", |
| 368 |
}, |
| 369 |
|
| 370 |
"notify_response": False, |
| 371 |
}, |
| 372 |
), |
| 373 |
( |
| 374 |
"slack://username@xoxb-1234-1234-abc124/#nuxref", |
| 375 |
{ |
| 376 |
"instance": NotifySlack, |
| 377 |
"requests_response_text": { |
| 378 |
"ok": True, |
| 379 |
"message": "", |
| 380 |
}, |
| 381 |
|
| 382 |
|
| 383 |
"response": False, |
| 384 |
}, |
| 385 |
), |
| 386 |
( |
| 387 |
"slack://username@T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ", |
| 388 |
{ |
| 389 |
|
| 390 |
"instance": NotifySlack, |
| 391 |
"requests_response_text": "ok", |
| 392 |
}, |
| 393 |
), |
| 394 |
|
| 395 |
( |
| 396 |
"https://hooks.slack.com/services/{}/{}/{}".format( |
| 397 |
"A" * 9, "B" * 9, "c" * 24 |
| 398 |
), |
| 399 |
{ |
| 400 |
"instance": NotifySlack, |
| 401 |
"requests_response_text": "ok", |
| 402 |
"url_matches": "mode=hook", |
| 403 |
}, |
| 404 |
), |
| 405 |
( |
| 406 |
"https://hooks.slack-gov.com/services/{}/{}/{}".format( |
| 407 |
"A" * 9, "B" * 9, "c" * 24 |
| 408 |
), |
| 409 |
{ |
| 410 |
"instance": NotifySlack, |
| 411 |
"requests_response_text": "ok", |
| 412 |
"url_matches": "mode=gov-hook", |
| 413 |
}, |
| 414 |
), |
| 415 |
|
| 416 |
( |
| 417 |
"https://hooks.slack.com/services/{}/{}/{}?format=text".format( |
| 418 |
"A" * 9, "B" * 9, "c" * 24 |
| 419 |
), |
| 420 |
{ |
| 421 |
"instance": NotifySlack, |
| 422 |
"requests_response_text": "ok", |
| 423 |
}, |
| 424 |
), |
| 425 |
( |
| 426 |
"slack://username@-INVALID-/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/#cool", |
| 427 |
{ |
| 428 |
|
| 429 |
"instance": TypeError, |
| 430 |
}, |
| 431 |
), |
| 432 |
( |
| 433 |
"slack://username@T1JJ3T3L2/-INVALID-/TIiajkdnlazkcOXrIdevi7FQ/#great", |
| 434 |
{ |
| 435 |
|
| 436 |
"instance": TypeError, |
| 437 |
}, |
| 438 |
), |
| 439 |
( |
| 440 |
"slack://username@T1JJ3T3L2/A1BRTD4JD/-INVALID-/#channel", |
| 441 |
{ |
| 442 |
|
| 443 |
"instance": TypeError, |
| 444 |
}, |
| 445 |
), |
| 446 |
( |
| 447 |
"slack://l2g@T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/#usenet", |
| 448 |
{ |
| 449 |
"instance": NotifySlack, |
| 450 |
|
| 451 |
"response": False, |
| 452 |
"requests_response_code": requests.codes.internal_server_error, |
| 453 |
"requests_response_text": "ok", |
| 454 |
}, |
| 455 |
), |
| 456 |
( |
| 457 |
"slack://respect@T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/#a", |
| 458 |
{ |
| 459 |
"instance": NotifySlack, |
| 460 |
|
| 461 |
"response": False, |
| 462 |
"requests_response_code": 999, |
| 463 |
"requests_response_text": "ok", |
| 464 |
}, |
| 465 |
), |
| 466 |
( |
| 467 |
"slack://notify@T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/#b", |
| 468 |
{ |
| 469 |
"instance": NotifySlack, |
| 470 |
|
| 471 |
|
| 472 |
"test_requests_exceptions": True, |
| 473 |
"requests_response_text": "ok", |
| 474 |
}, |
| 475 |
), |
| 476 |
( |
| 477 |
"slack://notify@T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/#b:100", |
| 478 |
{ |
| 479 |
"instance": NotifySlack, |
| 480 |
"requests_response_text": "ok", |
| 481 |
}, |
| 482 |
), |
| 483 |
( |
| 484 |
"slack://notify@T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/+124:100", |
| 485 |
{ |
| 486 |
"instance": NotifySlack, |
| 487 |
"requests_response_text": "ok", |
| 488 |
}, |
| 489 |
), |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
( |
| 495 |
( |
| 496 |
"slack://notify@T1JJ3T3L2/A1BRTD4JD/" |
| 497 |
"TIiajkdnlazkcOXrIdevi7FQ/+124:100/@chan" |
| 498 |
), |
| 499 |
{ |
| 500 |
"instance": NotifySlack, |
| 501 |
"requests_response_text": "ok", |
| 502 |
}, |
| 503 |
), |
| 504 |
( |
| 505 |
"slack://notify@T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/#b:bad", |
| 506 |
{ |
| 507 |
"instance": NotifySlack, |
| 508 |
"requests_response_text": "ok", |
| 509 |
|
| 510 |
"response": False, |
| 511 |
}, |
| 512 |
), |
| 513 |
|
| 514 |
( |
| 515 |
"slack://T1JJ3T3L2/Ft07XXXX/XXXXXXXX/YYYYYYYY/?mode=workflow", |
| 516 |
{ |
| 517 |
"instance": NotifySlack, |
| 518 |
}, |
| 519 |
), |
| 520 |
|
| 521 |
( |
| 522 |
"slack://T1JJ3T3L2/XXXXXXXX/YYYYYYYY/?mode=trigger", |
| 523 |
{ |
| 524 |
"instance": NotifySlack, |
| 525 |
}, |
| 526 |
), |
| 527 |
|
| 528 |
( |
| 529 |
( |
| 530 |
"https://hooks.slack.com/workflows/" |
| 531 |
"T1JJ3T3L2/Ft07XXXX/XXXXXXXX/YYYYYYYY" |
| 532 |
), |
| 533 |
{ |
| 534 |
"instance": NotifySlack, |
| 535 |
"privacy_url": "slack://T...2/F...X/X...X/Y...Y/", |
| 536 |
}, |
| 537 |
), |
| 538 |
|
| 539 |
( |
| 540 |
"https://hooks.slack.com/triggers/T1JJ3T3L2/XXXXXXXX/YYYYYYYY", |
| 541 |
{ |
| 542 |
"instance": NotifySlack, |
| 543 |
"privacy_url": "slack://T...2/X...X/Y...Y/", |
| 544 |
}, |
| 545 |
), |
| 546 |
|
| 547 |
( |
| 548 |
"slack://T1JJ3T3L2/XXXXXXXX/?mode=workflow", |
| 549 |
{ |
| 550 |
"instance": TypeError, |
| 551 |
}, |
| 552 |
), |
| 553 |
|
| 554 |
( |
| 555 |
"slack://T1JJ3T3L2/XXXXXXXX/YYYYYYYY/?mode=workflow", |
| 556 |
{ |
| 557 |
"instance": TypeError, |
| 558 |
}, |
| 559 |
), |
| 560 |
|
| 561 |
( |
| 562 |
"slack://T1JJ3T3L2/Ft07XXXX/XXXXXXXX/YYYYYYYY/?mode=trigger", |
| 563 |
{ |
| 564 |
"instance": TypeError, |
| 565 |
}, |
| 566 |
), |
| 567 |
) |
| 568 |
|
| 569 |
|
| 570 |
def test_plugin_slack_urls(): |
| 571 |
"""NotifySlack() Apprise URLs.""" |
| 572 |
|
| 573 |
|
| 574 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 575 |
|
| 576 |
|
| 577 |
@mock.patch("requests.request") |
| 578 |
def test_plugin_slack_oauth_access_token(mock_request): |
| 579 |
"""NotifySlack() OAuth Access Token Tests.""" |
| 580 |
|
| 581 |
|
| 582 |
token = "xo-invalid" |
| 583 |
|
| 584 |
request = mock.Mock() |
| 585 |
request.content = dumps( |
| 586 |
{ |
| 587 |
"ok": True, |
| 588 |
"message": "", |
| 589 |
"channel": "C123456", |
| 590 |
} |
| 591 |
) |
| 592 |
request.status_code = requests.codes.ok |
| 593 |
|
| 594 |
|
| 595 |
with pytest.raises(TypeError): |
| 596 |
NotifySlack(access_token=token) |
| 597 |
|
| 598 |
|
| 599 |
token = "xoxb-1234-1234-abc124" |
| 600 |
|
| 601 |
|
| 602 |
rotating_token = "xoxe.xoxb-1234-1234-abc124" |
| 603 |
|
| 604 |
|
| 605 |
mock_request.return_value = request |
| 606 |
|
| 607 |
obj = NotifySlack(access_token=token, targets="#apprise") |
| 608 |
assert isinstance(obj, NotifySlack) is True |
| 609 |
assert isinstance(obj.url(), str) is True |
| 610 |
|
| 611 |
|
| 612 |
assert obj.send(body="test") is True |
| 613 |
|
| 614 |
|
| 615 |
obj = NotifySlack(access_token=rotating_token, targets="#apprise") |
| 616 |
assert isinstance(obj, NotifySlack) is True |
| 617 |
assert isinstance(obj.url(), str) is True |
| 618 |
assert obj.send(body="test") is True |
| 619 |
|
| 620 |
|
| 621 |
with pytest.raises(TypeError): |
| 622 |
NotifySlack(access_token="xoxe.xo-invalid", targets="#apprise") |
| 623 |
|
| 624 |
|
| 625 |
mock_request.reset_mock() |
| 626 |
mock_request.side_effect = [ |
| 627 |
request, |
| 628 |
mock.Mock( |
| 629 |
**{ |
| 630 |
"content": dumps( |
| 631 |
{ |
| 632 |
"ok": True, |
| 633 |
"upload_url": "https://files.slack.com/upload/v1/ABC123", |
| 634 |
"file_id": "F123ABC456", |
| 635 |
} |
| 636 |
), |
| 637 |
"status_code": requests.codes.ok, |
| 638 |
} |
| 639 |
), |
| 640 |
mock.Mock( |
| 641 |
**{"content": b"OK - 123", "status_code": requests.codes.ok} |
| 642 |
), |
| 643 |
mock.Mock( |
| 644 |
**{ |
| 645 |
"content": dumps( |
| 646 |
{ |
| 647 |
"ok": True, |
| 648 |
"files": [{"id": "F123ABC456", "title": "slack-test"}], |
| 649 |
} |
| 650 |
), |
| 651 |
"status_code": requests.codes.ok, |
| 652 |
} |
| 653 |
), |
| 654 |
] |
| 655 |
|
| 656 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 657 |
attach = AppriseAttachment(path) |
| 658 |
assert ( |
| 659 |
obj.notify( |
| 660 |
body="body", |
| 661 |
title="title", |
| 662 |
notify_type=NotifyType.INFO, |
| 663 |
attach=attach, |
| 664 |
) |
| 665 |
is True |
| 666 |
) |
| 667 |
|
| 668 |
assert mock_request.call_count == 4 |
| 669 |
assert mock_request.call_args_list[0][0][0] == "post" |
| 670 |
assert ( |
| 671 |
mock_request.call_args_list[0][0][1] |
| 672 |
== "https://slack.com/api/chat.postMessage" |
| 673 |
) |
| 674 |
assert mock_request.call_args_list[1][0][0] == "get" |
| 675 |
assert ( |
| 676 |
mock_request.call_args_list[1][0][1] |
| 677 |
== "https://slack.com/api/files.getUploadURLExternal" |
| 678 |
) |
| 679 |
assert mock_request.call_args_list[2][0][0] == "post" |
| 680 |
assert ( |
| 681 |
mock_request.call_args_list[2][0][1] |
| 682 |
== "https://files.slack.com/upload/v1/ABC123" |
| 683 |
) |
| 684 |
assert mock_request.call_args_list[3][0][0] == "post" |
| 685 |
assert ( |
| 686 |
mock_request.call_args_list[3][0][1] |
| 687 |
== "https://slack.com/api/files.completeUploadExternal" |
| 688 |
) |
| 689 |
|
| 690 |
|
| 691 |
mock_request.return_value = None |
| 692 |
mock_request.side_effect = ( |
| 693 |
request, |
| 694 |
requests.ConnectionError(0, "requests.ConnectionError() not handled"), |
| 695 |
) |
| 696 |
assert ( |
| 697 |
obj.notify( |
| 698 |
body="body", |
| 699 |
title="title", |
| 700 |
notify_type=NotifyType.INFO, |
| 701 |
attach=attach, |
| 702 |
) |
| 703 |
is False |
| 704 |
) |
| 705 |
|
| 706 |
|
| 707 |
mock_request.return_value = None |
| 708 |
mock_request.side_effect = (request, OSError(0, "OSError")) |
| 709 |
assert ( |
| 710 |
obj.notify( |
| 711 |
body="body", |
| 712 |
title="title", |
| 713 |
notify_type=NotifyType.INFO, |
| 714 |
attach=attach, |
| 715 |
) |
| 716 |
is False |
| 717 |
) |
| 718 |
|
| 719 |
|
| 720 |
mock_request.return_value = request |
| 721 |
mock_request.side_effect = None |
| 722 |
|
| 723 |
|
| 724 |
path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 725 |
assert ( |
| 726 |
obj.notify( |
| 727 |
body="body", |
| 728 |
title="title", |
| 729 |
notify_type=NotifyType.INFO, |
| 730 |
attach=path, |
| 731 |
) |
| 732 |
is False |
| 733 |
) |
| 734 |
|
| 735 |
|
| 736 |
mock_request.reset_mock() |
| 737 |
mock_request.side_effect = [ |
| 738 |
request, |
| 739 |
mock.Mock( |
| 740 |
**{ |
| 741 |
"content": dumps( |
| 742 |
{ |
| 743 |
"ok": False, |
| 744 |
} |
| 745 |
), |
| 746 |
"status_code": requests.codes.internal_server_error, |
| 747 |
} |
| 748 |
), |
| 749 |
] |
| 750 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 751 |
attach = AppriseAttachment(path) |
| 752 |
|
| 753 |
assert ( |
| 754 |
obj.notify( |
| 755 |
body="body", |
| 756 |
title="title", |
| 757 |
notify_type=NotifyType.INFO, |
| 758 |
attach=attach, |
| 759 |
) |
| 760 |
is False |
| 761 |
) |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
request.content = "{" |
| 766 |
mock_request.reset_mock() |
| 767 |
mock_request.return_value = request |
| 768 |
mock_request.side_effect = None |
| 769 |
|
| 770 |
|
| 771 |
assert obj.send(body="test", attach=attach) is False |
| 772 |
|
| 773 |
request.content = dumps( |
| 774 |
{ |
| 775 |
"ok": False, |
| 776 |
"message": "We failed", |
| 777 |
} |
| 778 |
) |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
assert obj.send(body="test", attach=attach) is False |
| 783 |
|
| 784 |
|
| 785 |
mock_request.side_effect = OSError("Attachment Error") |
| 786 |
mock_request.return_value = None |
| 787 |
|
| 788 |
|
| 789 |
assert obj.send(body="test") is False |
| 790 |
|
| 791 |
|
| 792 |
@mock.patch("requests.request") |
| 793 |
def test_plugin_slack_webhook_mode(mock_request): |
| 794 |
"""NotifySlack() Webhook Mode Tests.""" |
| 795 |
|
| 796 |
|
| 797 |
mock_request.return_value = requests.Request() |
| 798 |
mock_request.return_value.status_code = requests.codes.ok |
| 799 |
mock_request.return_value.content = b"ok" |
| 800 |
mock_request.return_value.text = "ok" |
| 801 |
|
| 802 |
|
| 803 |
token_a = "A" * 9 |
| 804 |
token_b = "B" * 9 |
| 805 |
token_c = "c" * 24 |
| 806 |
channels = "chan1,#chan2,+BAK4K23G5,@user,,," |
| 807 |
|
| 808 |
obj = NotifySlack( |
| 809 |
token_a=token_a, token_b=token_b, token_c=token_c, targets=channels |
| 810 |
) |
| 811 |
assert len(obj.channels) == 4 |
| 812 |
|
| 813 |
|
| 814 |
assert ( |
| 815 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 816 |
is True |
| 817 |
) |
| 818 |
|
| 819 |
|
| 820 |
with pytest.raises(TypeError): |
| 821 |
NotifySlack( |
| 822 |
token_a=None, token_b=token_b, token_c=token_c, targets=channels |
| 823 |
) |
| 824 |
|
| 825 |
|
| 826 |
obj = NotifySlack( |
| 827 |
token_a=token_a, |
| 828 |
token_b=token_b, |
| 829 |
token_c=token_c, |
| 830 |
targets=channels, |
| 831 |
include_image=True, |
| 832 |
) |
| 833 |
|
| 834 |
|
| 835 |
assert ( |
| 836 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 837 |
is True |
| 838 |
) |
| 839 |
|
| 840 |
|
| 841 |
@mock.patch("requests.request") |
| 842 |
def test_plugin_slack_username_payload_by_mode(mock_request): |
| 843 |
"""NotifySlack() Username payload behavior by mode.""" |
| 844 |
|
| 845 |
|
| 846 |
response = mock.Mock() |
| 847 |
response.status_code = requests.codes.ok |
| 848 |
response.content = b"ok" |
| 849 |
response.text = "ok" |
| 850 |
mock_request.return_value = response |
| 851 |
|
| 852 |
token_a = "A" * 9 |
| 853 |
token_b = "B" * 9 |
| 854 |
token_c = "c" * 24 |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
obj = NotifySlack(token_a=token_a, token_b=token_b, token_c=token_c) |
| 859 |
assert obj.notify(body="body", title="title") is True |
| 860 |
payload = loads(mock_request.call_args.kwargs["data"]) |
| 861 |
assert "username" not in payload |
| 862 |
|
| 863 |
|
| 864 |
mock_request.reset_mock() |
| 865 |
obj = NotifySlack( |
| 866 |
token_a=token_a, |
| 867 |
token_b=token_b, |
| 868 |
token_c=token_c, |
| 869 |
user="CustomWebhookName", |
| 870 |
) |
| 871 |
assert obj.notify(body="body", title="title") is True |
| 872 |
payload = loads(mock_request.call_args.kwargs["data"]) |
| 873 |
assert payload.get("username") == "CustomWebhookName" |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
mock_request.reset_mock() |
| 878 |
response.content = dumps({"ok": True, "channel": "C123456"}) |
| 879 |
obj = NotifySlack(access_token="xoxb-1234-1234-abc124", targets="#test") |
| 880 |
assert obj.notify(body="body", title="title") is True |
| 881 |
payload = loads(mock_request.call_args.kwargs["data"]) |
| 882 |
assert "username" not in payload |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
mock_request.reset_mock() |
| 887 |
response.content = b"ok" |
| 888 |
obj = NotifySlack( |
| 889 |
token_a=token_a, |
| 890 |
token_b=token_b, |
| 891 |
token_c=token_c, |
| 892 |
mode=SlackMode.WEBHOOK_GOV, |
| 893 |
) |
| 894 |
assert obj.notify(body="body", title="title") is True |
| 895 |
payload = loads(mock_request.call_args.kwargs["data"]) |
| 896 |
assert "username" not in payload |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
mock_request.reset_mock() |
| 901 |
response.content = b"ok" |
| 902 |
obj = NotifySlack( |
| 903 |
token_a=token_a, |
| 904 |
token_b=token_b, |
| 905 |
token_c=token_c, |
| 906 |
use_blocks=True, |
| 907 |
) |
| 908 |
assert obj.notify(body="body", title="title") is True |
| 909 |
payload = loads(mock_request.call_args.kwargs["data"]) |
| 910 |
assert "username" not in payload |
| 911 |
|
| 912 |
|
| 913 |
@mock.patch("requests.request") |
| 914 |
@mock.patch("requests.get") |
| 915 |
def test_plugin_slack_send_by_email(mock_get, mock_request): |
| 916 |
"""NotifySlack() Send by Email Tests.""" |
| 917 |
|
| 918 |
|
| 919 |
token = "xoxb-1234-1234-abc124" |
| 920 |
|
| 921 |
request = mock.Mock() |
| 922 |
request.content = dumps( |
| 923 |
{"ok": True, "message": "", "user": {"id": "ABCD1234"}} |
| 924 |
) |
| 925 |
request.status_code = requests.codes.ok |
| 926 |
|
| 927 |
|
| 928 |
mock_request.return_value = request |
| 929 |
mock_get.return_value = request |
| 930 |
|
| 931 |
|
| 932 |
obj = NotifySlack(access_token=token, targets="[email protected]") |
| 933 |
assert isinstance(obj, NotifySlack) is True |
| 934 |
assert isinstance(obj.url(), str) is True |
| 935 |
|
| 936 |
|
| 937 |
assert mock_request.call_count == 0 |
| 938 |
assert mock_get.call_count == 0 |
| 939 |
|
| 940 |
|
| 941 |
assert ( |
| 942 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 943 |
is True |
| 944 |
) |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
assert mock_get.call_count == 1 |
| 949 |
assert mock_request.call_count == 1 |
| 950 |
assert ( |
| 951 |
mock_get.call_args_list[0][0][0] |
| 952 |
== "https://slack.com/api/users.lookupByEmail" |
| 953 |
) |
| 954 |
assert ( |
| 955 |
mock_request.call_args_list[0][0][1] |
| 956 |
== "https://slack.com/api/chat.postMessage" |
| 957 |
) |
| 958 |
|
| 959 |
|
| 960 |
mock_request.reset_mock() |
| 961 |
mock_get.reset_mock() |
| 962 |
|
| 963 |
|
| 964 |
mock_request.return_value = request |
| 965 |
mock_get.return_value = request |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
assert ( |
| 970 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 971 |
is True |
| 972 |
) |
| 973 |
|
| 974 |
assert mock_get.call_count == 0 |
| 975 |
assert mock_request.call_count == 1 |
| 976 |
assert ( |
| 977 |
mock_request.call_args_list[0][0][1] |
| 978 |
== "https://slack.com/api/chat.postMessage" |
| 979 |
) |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
request.content = dumps( |
| 985 |
{ |
| 986 |
"ok": False, |
| 987 |
"message": "", |
| 988 |
} |
| 989 |
) |
| 990 |
|
| 991 |
|
| 992 |
mock_request.reset_mock() |
| 993 |
mock_get.reset_mock() |
| 994 |
|
| 995 |
|
| 996 |
mock_request.return_value = request |
| 997 |
mock_get.return_value = request |
| 998 |
|
| 999 |
|
| 1000 |
obj = NotifySlack(access_token=token, targets="[email protected]") |
| 1001 |
assert isinstance(obj, NotifySlack) is True |
| 1002 |
assert isinstance(obj.url(), str) is True |
| 1003 |
|
| 1004 |
|
| 1005 |
assert mock_request.call_count == 0 |
| 1006 |
assert mock_get.call_count == 0 |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
assert ( |
| 1011 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 1012 |
is False |
| 1013 |
) |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
assert mock_get.call_count == 1 |
| 1018 |
assert mock_request.call_count == 0 |
| 1019 |
assert ( |
| 1020 |
mock_get.call_args_list[0][0][0] |
| 1021 |
== "https://slack.com/api/users.lookupByEmail" |
| 1022 |
) |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
request.content = "}" |
| 1028 |
|
| 1029 |
|
| 1030 |
mock_request.reset_mock() |
| 1031 |
mock_get.reset_mock() |
| 1032 |
|
| 1033 |
|
| 1034 |
mock_request.return_value = request |
| 1035 |
mock_get.return_value = request |
| 1036 |
|
| 1037 |
|
| 1038 |
obj = NotifySlack(access_token=token, targets="[email protected]") |
| 1039 |
assert isinstance(obj, NotifySlack) is True |
| 1040 |
assert isinstance(obj.url(), str) is True |
| 1041 |
|
| 1042 |
|
| 1043 |
assert mock_request.call_count == 0 |
| 1044 |
assert mock_get.call_count == 0 |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
assert ( |
| 1049 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 1050 |
is False |
| 1051 |
) |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
assert mock_get.call_count == 1 |
| 1056 |
assert mock_request.call_count == 0 |
| 1057 |
assert ( |
| 1058 |
mock_get.call_args_list[0][0][0] |
| 1059 |
== "https://slack.com/api/users.lookupByEmail" |
| 1060 |
) |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
request.content = "}" |
| 1066 |
|
| 1067 |
|
| 1068 |
mock_request.reset_mock() |
| 1069 |
mock_get.reset_mock() |
| 1070 |
|
| 1071 |
|
| 1072 |
mock_request.return_value = request |
| 1073 |
mock_get.return_value = request |
| 1074 |
|
| 1075 |
|
| 1076 |
obj = NotifySlack(access_token=token, targets="[email protected]") |
| 1077 |
assert isinstance(obj, NotifySlack) is True |
| 1078 |
assert isinstance(obj.url(), str) is True |
| 1079 |
|
| 1080 |
|
| 1081 |
assert mock_request.call_count == 0 |
| 1082 |
assert mock_get.call_count == 0 |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
assert ( |
| 1087 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 1088 |
is False |
| 1089 |
) |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
assert mock_get.call_count == 1 |
| 1094 |
assert mock_request.call_count == 0 |
| 1095 |
assert ( |
| 1096 |
mock_get.call_args_list[0][0][0] |
| 1097 |
== "https://slack.com/api/users.lookupByEmail" |
| 1098 |
) |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
request.content = dumps( |
| 1105 |
{"ok": True, "message": "", "user": {"id": "ABCD1234"}} |
| 1106 |
) |
| 1107 |
|
| 1108 |
request.status_code = requests.codes.ok |
| 1109 |
|
| 1110 |
|
| 1111 |
mock_request.reset_mock() |
| 1112 |
mock_get.reset_mock() |
| 1113 |
|
| 1114 |
|
| 1115 |
mock_request.return_value = request |
| 1116 |
mock_get.side_effect = requests.ConnectionError( |
| 1117 |
0, "requests.ConnectionError() not handled" |
| 1118 |
) |
| 1119 |
|
| 1120 |
|
| 1121 |
obj = NotifySlack(access_token=token, targets="[email protected]") |
| 1122 |
assert isinstance(obj, NotifySlack) is True |
| 1123 |
assert isinstance(obj.url(), str) is True |
| 1124 |
|
| 1125 |
|
| 1126 |
assert mock_request.call_count == 0 |
| 1127 |
assert mock_get.call_count == 0 |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
assert ( |
| 1132 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 1133 |
is False |
| 1134 |
) |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
assert mock_get.call_count == 1 |
| 1139 |
assert mock_request.call_count == 0 |
| 1140 |
assert ( |
| 1141 |
mock_get.call_args_list[0][0][0] |
| 1142 |
== "https://slack.com/api/users.lookupByEmail" |
| 1143 |
) |
| 1144 |
|
| 1145 |
|
| 1146 |
@mock.patch("requests.request") |
| 1147 |
@mock.patch("requests.get") |
| 1148 |
def test_plugin_slack_markdown(mock_get, mock_request): |
| 1149 |
"""NotifySlack() Markdown tests.""" |
| 1150 |
|
| 1151 |
request = mock.Mock() |
| 1152 |
request.content = b"ok" |
| 1153 |
request.status_code = requests.codes.ok |
| 1154 |
|
| 1155 |
|
| 1156 |
mock_request.return_value = request |
| 1157 |
mock_get.return_value = request |
| 1158 |
|
| 1159 |
|
| 1160 |
aobj = Apprise() |
| 1161 |
assert aobj.add( |
| 1162 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/#channel" |
| 1163 |
) |
| 1164 |
|
| 1165 |
body = cleandoc(""" |
| 1166 |
Here is a <https://slack.com|Slack Link> we want to support as part of it's |
| 1167 |
markdown. |
| 1168 |
|
| 1169 |
This one has arguments we want to preserve: |
| 1170 |
<https://slack.com?arg=val&arg2=val2|Slack Link>. |
| 1171 |
We also want to be able to support <https://slack.com> links without the |
| 1172 |
description. |
| 1173 |
|
| 1174 |
Channel Testing |
| 1175 |
<!channelA> |
| 1176 |
<!channelA|Description> |
| 1177 |
|
| 1178 |
User ID Testing |
| 1179 |
<@U1ZQL9N3Y> |
| 1180 |
<@U1ZQL9N3Y|heheh> |
| 1181 |
""") |
| 1182 |
|
| 1183 |
|
| 1184 |
assert aobj.notify(body=body, title="title", notify_type=NotifyType.INFO) |
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
assert mock_get.call_count == 0 |
| 1189 |
assert mock_request.call_count == 1 |
| 1190 |
assert ( |
| 1191 |
mock_request.call_args_list[0][0][1] |
| 1192 |
== "https://hooks.slack.com/services/T1JJ3T3L2/A1BRTD4JD/" |
| 1193 |
"TIiajkdnlazkcOXrIdevi7FQ" |
| 1194 |
) |
| 1195 |
|
| 1196 |
data = loads(mock_request.call_args_list[0][1]["data"]) |
| 1197 |
assert ( |
| 1198 |
data["attachments"][0]["text"] |
| 1199 |
== "Here is a <https://slack.com|Slack Link> we want to support as" |
| 1200 |
" part " |
| 1201 |
"of it's\nmarkdown.\n\nThis one has arguments we want to preserve:" |
| 1202 |
"\n <https://slack.com?arg=val&arg2=val2|Slack Link>.\n" |
| 1203 |
"We also want to be able to support <https://slack.com> " |
| 1204 |
"links without the\ndescription." |
| 1205 |
"\n\nChannel Testing\n<!channelA>\n<!channelA|Description>\n\n" |
| 1206 |
"User ID Testing\n<@U1ZQL9N3Y>\n<@U1ZQL9N3Y|heheh>" |
| 1207 |
) |
| 1208 |
|
| 1209 |
|
| 1210 |
@mock.patch("requests.request") |
| 1211 |
def test_plugin_slack_single_thread_reply(mock_request): |
| 1212 |
"""NotifySlack() Send Notification as a Reply.""" |
| 1213 |
|
| 1214 |
|
| 1215 |
token = "xoxb-1234-1234-abc124" |
| 1216 |
thread_id = 100 |
| 1217 |
request = mock.Mock() |
| 1218 |
request.content = dumps( |
| 1219 |
{"ok": True, "message": "", "user": {"id": "ABCD1234"}} |
| 1220 |
) |
| 1221 |
request.status_code = requests.codes.ok |
| 1222 |
|
| 1223 |
|
| 1224 |
mock_request.return_value = request |
| 1225 |
|
| 1226 |
|
| 1227 |
obj = NotifySlack(access_token=token, targets=[f"#general:{thread_id}"]) |
| 1228 |
assert isinstance(obj, NotifySlack) is True |
| 1229 |
assert isinstance(obj.url(), str) is True |
| 1230 |
|
| 1231 |
|
| 1232 |
assert mock_request.call_count == 0 |
| 1233 |
|
| 1234 |
|
| 1235 |
assert ( |
| 1236 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 1237 |
is True |
| 1238 |
) |
| 1239 |
|
| 1240 |
|
| 1241 |
assert mock_request.call_count == 1 |
| 1242 |
assert ( |
| 1243 |
mock_request.call_args_list[0][0][1] |
| 1244 |
== "https://slack.com/api/chat.postMessage" |
| 1245 |
) |
| 1246 |
assert loads(mock_request.call_args_list[0][1]["data"]).get( |
| 1247 |
"thread_ts" |
| 1248 |
) == str(thread_id) |
| 1249 |
|
| 1250 |
|
| 1251 |
@mock.patch("requests.request") |
| 1252 |
def test_plugin_slack_multiple_thread_reply(mock_request): |
| 1253 |
"""NotifySlack() Send Notification to multiple channels as Reply.""" |
| 1254 |
|
| 1255 |
|
| 1256 |
token = "xoxb-1234-1234-abc124" |
| 1257 |
thread_id_1, thread_id_2 = 100, 200 |
| 1258 |
request = mock.Mock() |
| 1259 |
request.content = dumps( |
| 1260 |
{"ok": True, "message": "", "user": {"id": "ABCD1234"}} |
| 1261 |
) |
| 1262 |
request.status_code = requests.codes.ok |
| 1263 |
|
| 1264 |
|
| 1265 |
mock_request.return_value = request |
| 1266 |
|
| 1267 |
|
| 1268 |
obj = NotifySlack( |
| 1269 |
access_token=token, |
| 1270 |
targets=[f"#general:{thread_id_1}", f"#other:{thread_id_2}"], |
| 1271 |
) |
| 1272 |
assert isinstance(obj, NotifySlack) is True |
| 1273 |
assert isinstance(obj.url(), str) is True |
| 1274 |
|
| 1275 |
|
| 1276 |
assert mock_request.call_count == 0 |
| 1277 |
|
| 1278 |
|
| 1279 |
assert ( |
| 1280 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 1281 |
is True |
| 1282 |
) |
| 1283 |
|
| 1284 |
|
| 1285 |
assert mock_request.call_count == 2 |
| 1286 |
assert ( |
| 1287 |
mock_request.call_args_list[0][0][1] |
| 1288 |
== "https://slack.com/api/chat.postMessage" |
| 1289 |
) |
| 1290 |
assert loads(mock_request.call_args_list[0][1]["data"]).get( |
| 1291 |
"thread_ts" |
| 1292 |
) == str(thread_id_1) |
| 1293 |
assert loads(mock_request.call_args_list[1][1]["data"]).get( |
| 1294 |
"thread_ts" |
| 1295 |
) == str(thread_id_2) |
| 1296 |
|
| 1297 |
|
| 1298 |
@mock.patch("requests.request") |
| 1299 |
def test_plugin_slack_file_upload_success(mock_request): |
| 1300 |
"""Test Slack BOT attachment upload success path.""" |
| 1301 |
|
| 1302 |
token = "xoxb-1234-1234-abc124" |
| 1303 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 1304 |
attach = AppriseAttachment(path) |
| 1305 |
|
| 1306 |
|
| 1307 |
mock_request.side_effect = [ |
| 1308 |
mock.Mock( |
| 1309 |
**{ |
| 1310 |
"content": dumps( |
| 1311 |
{ |
| 1312 |
"ok": True, |
| 1313 |
"channel": "C123456", |
| 1314 |
} |
| 1315 |
), |
| 1316 |
"status_code": requests.codes.ok, |
| 1317 |
} |
| 1318 |
), |
| 1319 |
mock.Mock( |
| 1320 |
**{ |
| 1321 |
"content": dumps( |
| 1322 |
{ |
| 1323 |
"ok": True, |
| 1324 |
"upload_url": "https://files.slack.com/upload/v1/ABC123", |
| 1325 |
"file_id": "F123ABC456", |
| 1326 |
} |
| 1327 |
), |
| 1328 |
"status_code": requests.codes.ok, |
| 1329 |
} |
| 1330 |
), |
| 1331 |
mock.Mock( |
| 1332 |
**{ |
| 1333 |
"content": b"OK - 123", |
| 1334 |
"status_code": requests.codes.ok, |
| 1335 |
} |
| 1336 |
), |
| 1337 |
mock.Mock( |
| 1338 |
**{ |
| 1339 |
"content": dumps( |
| 1340 |
{ |
| 1341 |
"ok": True, |
| 1342 |
"files": [ |
| 1343 |
{"id": "F123ABC456", "title": "apprise-test"} |
| 1344 |
], |
| 1345 |
} |
| 1346 |
), |
| 1347 |
"status_code": requests.codes.ok, |
| 1348 |
} |
| 1349 |
), |
| 1350 |
] |
| 1351 |
|
| 1352 |
obj = NotifySlack(access_token=token, targets=["#general"]) |
| 1353 |
assert ( |
| 1354 |
obj.notify( |
| 1355 |
body="Success path test", |
| 1356 |
title="Slack Upload OK", |
| 1357 |
notify_type=NotifyType.INFO, |
| 1358 |
attach=attach, |
| 1359 |
) |
| 1360 |
is True |
| 1361 |
) |
| 1362 |
|
| 1363 |
|
| 1364 |
@mock.patch("requests.request") |
| 1365 |
def test_plugin_slack_file_upload_fails_missing_files(mock_request): |
| 1366 |
"""Test that file upload fails when 'files' is missing or empty.""" |
| 1367 |
|
| 1368 |
token = "xoxb-1234-1234-abc124" |
| 1369 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 1370 |
attach = AppriseAttachment(path) |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
mock_request.side_effect = [ |
| 1379 |
mock.Mock( |
| 1380 |
**{ |
| 1381 |
"content": dumps( |
| 1382 |
{ |
| 1383 |
"ok": True, |
| 1384 |
"channel": "C555555", |
| 1385 |
} |
| 1386 |
), |
| 1387 |
"status_code": requests.codes.ok, |
| 1388 |
} |
| 1389 |
), |
| 1390 |
mock.Mock( |
| 1391 |
**{ |
| 1392 |
"content": dumps( |
| 1393 |
{ |
| 1394 |
"ok": True, |
| 1395 |
"upload_url": "https://files.slack.com/upload/v1/X99999", |
| 1396 |
"file_id": "F999XYZ888", |
| 1397 |
} |
| 1398 |
), |
| 1399 |
"status_code": requests.codes.ok, |
| 1400 |
} |
| 1401 |
), |
| 1402 |
mock.Mock( |
| 1403 |
**{ |
| 1404 |
"content": b"OK - 2048", |
| 1405 |
"status_code": requests.codes.ok, |
| 1406 |
} |
| 1407 |
), |
| 1408 |
|
| 1409 |
mock.Mock( |
| 1410 |
**{ |
| 1411 |
"content": dumps( |
| 1412 |
{ |
| 1413 |
"ok": True, |
| 1414 |
"files": [], |
| 1415 |
} |
| 1416 |
), |
| 1417 |
"status_code": requests.codes.ok, |
| 1418 |
} |
| 1419 |
), |
| 1420 |
] |
| 1421 |
|
| 1422 |
obj = NotifySlack(access_token=token, targets=["#fail-channel"]) |
| 1423 |
result = obj.notify( |
| 1424 |
body="This should trigger a failed file upload", |
| 1425 |
title="Trigger failure", |
| 1426 |
notify_type=NotifyType.INFO, |
| 1427 |
attach=attach, |
| 1428 |
) |
| 1429 |
|
| 1430 |
assert result is False |
| 1431 |
|
| 1432 |
|
| 1433 |
@mock.patch("requests.request") |
| 1434 |
def test_plugin_slack_attach_memory(mock_request): |
| 1435 |
"""Regression: AttachMemory must be sendable without OSError.""" |
| 1436 |
from apprise.attachment.memory import AttachMemory |
| 1437 |
|
| 1438 |
token = "xoxb-1234-1234-abc124" |
| 1439 |
|
| 1440 |
mock_request.side_effect = [ |
| 1441 |
mock.Mock( |
| 1442 |
**{ |
| 1443 |
"content": dumps({"ok": True, "channel": "C123456"}), |
| 1444 |
"status_code": requests.codes.ok, |
| 1445 |
} |
| 1446 |
), |
| 1447 |
mock.Mock( |
| 1448 |
**{ |
| 1449 |
"content": dumps( |
| 1450 |
{ |
| 1451 |
"ok": True, |
| 1452 |
"upload_url": "https://files.slack.com/upload/v1/ABC123", |
| 1453 |
"file_id": "F123ABC456", |
| 1454 |
} |
| 1455 |
), |
| 1456 |
"status_code": requests.codes.ok, |
| 1457 |
} |
| 1458 |
), |
| 1459 |
mock.Mock( |
| 1460 |
**{ |
| 1461 |
"content": b"OK - 100", |
| 1462 |
"status_code": requests.codes.ok, |
| 1463 |
} |
| 1464 |
), |
| 1465 |
mock.Mock( |
| 1466 |
**{ |
| 1467 |
"content": dumps( |
| 1468 |
{ |
| 1469 |
"ok": True, |
| 1470 |
"files": [{"id": "F123ABC456", "title": "report"}], |
| 1471 |
} |
| 1472 |
), |
| 1473 |
"status_code": requests.codes.ok, |
| 1474 |
} |
| 1475 |
), |
| 1476 |
] |
| 1477 |
|
| 1478 |
obj = NotifySlack(access_token=token, targets=["#general"]) |
| 1479 |
|
| 1480 |
mem = AttachMemory( |
| 1481 |
content=b"<html><body><h1>Test</h1></body></html>", |
| 1482 |
name="report.html", |
| 1483 |
mimetype="text/html", |
| 1484 |
) |
| 1485 |
|
| 1486 |
assert obj.notify(body="Test", attach=mem) is True |
| 1487 |
assert mock_request.call_count >= 1 |
| 1488 |
|
| 1489 |
|
| 1490 |
@mock.patch("requests.request") |
| 1491 |
def test_plugin_slack_template_blocks(mock_request, tmpdir): |
| 1492 |
"""NotifySlack() - blocks mode with JSON template.""" |
| 1493 |
|
| 1494 |
mock_request.return_value = mock.Mock( |
| 1495 |
**{ |
| 1496 |
"content": b"ok", |
| 1497 |
"status_code": requests.codes.ok, |
| 1498 |
} |
| 1499 |
) |
| 1500 |
|
| 1501 |
|
| 1502 |
template = tmpdir.join("blocks.json") |
| 1503 |
template.write( |
| 1504 |
cleandoc(""" |
| 1505 |
{ |
| 1506 |
"blocks": [ |
| 1507 |
{ |
| 1508 |
"type": "header", |
| 1509 |
"text": { |
| 1510 |
"type": "plain_text", |
| 1511 |
"text": "{{app_title}}" |
| 1512 |
} |
| 1513 |
}, |
| 1514 |
{ |
| 1515 |
"type": "section", |
| 1516 |
"text": { |
| 1517 |
"type": "mrkdwn", |
| 1518 |
"text": "{{app_body}}" |
| 1519 |
} |
| 1520 |
} |
| 1521 |
], |
| 1522 |
"color": "{{app_color}}" |
| 1523 |
} |
| 1524 |
""") |
| 1525 |
) |
| 1526 |
|
| 1527 |
|
| 1528 |
obj = Apprise.instantiate( |
| 1529 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 1530 |
"?blocks=yes&template={}&:mykey=myval".format(str(template)) |
| 1531 |
) |
| 1532 |
assert isinstance(obj, NotifySlack) |
| 1533 |
|
| 1534 |
|
| 1535 |
assert "mykey" in obj.tokens |
| 1536 |
assert obj.tokens["mykey"] == "myval" |
| 1537 |
assert obj.template |
| 1538 |
|
| 1539 |
|
| 1540 |
assert ( |
| 1541 |
obj.notify(body="hello", title="world", notify_type=NotifyType.INFO) |
| 1542 |
is True |
| 1543 |
) |
| 1544 |
assert mock_request.called is True |
| 1545 |
|
| 1546 |
|
| 1547 |
posted = loads(mock_request.call_args_list[0][1]["data"]) |
| 1548 |
assert "attachments" in posted |
| 1549 |
assert "blocks" in posted["attachments"][0] |
| 1550 |
|
| 1551 |
blocks = posted["attachments"][0]["blocks"] |
| 1552 |
assert any(b.get("type") == "header" for b in blocks) |
| 1553 |
assert any(b.get("type") == "section" for b in blocks) |
| 1554 |
|
| 1555 |
header = next(b for b in blocks if b.get("type") == "header") |
| 1556 |
assert header["text"]["text"] == "world" |
| 1557 |
section = next(b for b in blocks if b.get("type") == "section") |
| 1558 |
assert section["text"]["text"] == "hello" |
| 1559 |
|
| 1560 |
|
| 1561 |
@mock.patch("requests.request") |
| 1562 |
def test_plugin_slack_template_blocks_implied(mock_request, tmpdir): |
| 1563 |
"""NotifySlack() - template= alone implies blocks=yes.""" |
| 1564 |
|
| 1565 |
mock_request.return_value = mock.Mock( |
| 1566 |
**{ |
| 1567 |
"content": b"ok", |
| 1568 |
"status_code": requests.codes.ok, |
| 1569 |
} |
| 1570 |
) |
| 1571 |
|
| 1572 |
template = tmpdir.join("implied.json") |
| 1573 |
template.write( |
| 1574 |
cleandoc(""" |
| 1575 |
{ |
| 1576 |
"blocks": [ |
| 1577 |
{ |
| 1578 |
"type": "section", |
| 1579 |
"text": {"type": "mrkdwn", "text": "{{app_body}}"} |
| 1580 |
} |
| 1581 |
] |
| 1582 |
} |
| 1583 |
""") |
| 1584 |
) |
| 1585 |
|
| 1586 |
|
| 1587 |
obj = Apprise.instantiate( |
| 1588 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 1589 |
"?template={}".format(str(template)) |
| 1590 |
) |
| 1591 |
assert isinstance(obj, NotifySlack) |
| 1592 |
|
| 1593 |
assert obj.use_blocks is True |
| 1594 |
assert ( |
| 1595 |
obj.notify(body="implied", title="t", notify_type=NotifyType.INFO) |
| 1596 |
is True |
| 1597 |
) |
| 1598 |
assert mock_request.called is True |
| 1599 |
posted = loads(mock_request.call_args_list[0][1]["data"]) |
| 1600 |
blocks = posted["attachments"][0]["blocks"] |
| 1601 |
section = next(b for b in blocks if b.get("type") == "section") |
| 1602 |
assert section["text"]["text"] == "implied" |
| 1603 |
|
| 1604 |
|
| 1605 |
@mock.patch("requests.request") |
| 1606 |
def test_plugin_slack_blocks_payload_uses_username(mock_request): |
| 1607 |
"""NotifySlack() - blocks payload includes explicit username.""" |
| 1608 |
mock_request.return_value = mock.Mock( |
| 1609 |
**{"content": b"ok", "status_code": requests.codes.ok} |
| 1610 |
) |
| 1611 |
|
| 1612 |
obj = NotifySlack( |
| 1613 |
token_a="T1JJ3T3L2", |
| 1614 |
token_b="A1BRTD4JD", |
| 1615 |
token_c="TIiajkdnlazkcOXrIdevi7FQ", |
| 1616 |
use_blocks=True, |
| 1617 |
user="BlockBot", |
| 1618 |
) |
| 1619 |
|
| 1620 |
assert obj.notify(body="hello", title="world", notify_type=NotifyType.INFO) |
| 1621 |
payload = loads(mock_request.call_args.kwargs["data"]) |
| 1622 |
assert payload["username"] == "BlockBot" |
| 1623 |
|
| 1624 |
|
| 1625 |
@mock.patch("requests.request") |
| 1626 |
def test_plugin_slack_template_blocks_payload_uses_username( |
| 1627 |
mock_request, tmpdir |
| 1628 |
): |
| 1629 |
"""NotifySlack() - template blocks payload includes explicit username.""" |
| 1630 |
mock_request.return_value = mock.Mock( |
| 1631 |
**{"content": b"ok", "status_code": requests.codes.ok} |
| 1632 |
) |
| 1633 |
|
| 1634 |
template = tmpdir.join("username_blocks.json") |
| 1635 |
template.write( |
| 1636 |
cleandoc(""" |
| 1637 |
{ |
| 1638 |
"blocks": [ |
| 1639 |
{ |
| 1640 |
"type": "section", |
| 1641 |
"text": {"type": "mrkdwn", "text": "{{app_body}}"} |
| 1642 |
} |
| 1643 |
] |
| 1644 |
} |
| 1645 |
""") |
| 1646 |
) |
| 1647 |
|
| 1648 |
obj = NotifySlack( |
| 1649 |
token_a="T1JJ3T3L2", |
| 1650 |
token_b="A1BRTD4JD", |
| 1651 |
token_c="TIiajkdnlazkcOXrIdevi7FQ", |
| 1652 |
template=str(template), |
| 1653 |
user="TemplateBot", |
| 1654 |
) |
| 1655 |
|
| 1656 |
assert obj.notify(body="hello", title="world", notify_type=NotifyType.INFO) |
| 1657 |
payload = loads(mock_request.call_args.kwargs["data"]) |
| 1658 |
assert payload["username"] == "TemplateBot" |
| 1659 |
|
| 1660 |
|
| 1661 |
@mock.patch("requests.request") |
| 1662 |
def test_plugin_slack_template_blocks_no_username(mock_request, tmpdir): |
| 1663 |
"""NotifySlack() - template blocks omit username when no botname set.""" |
| 1664 |
mock_request.return_value = mock.Mock( |
| 1665 |
**{"content": b"ok", "status_code": requests.codes.ok} |
| 1666 |
) |
| 1667 |
|
| 1668 |
template = tmpdir.join("no_username_blocks.json") |
| 1669 |
template.write( |
| 1670 |
cleandoc(""" |
| 1671 |
{ |
| 1672 |
"blocks": [ |
| 1673 |
{ |
| 1674 |
"type": "section", |
| 1675 |
"text": {"type": "mrkdwn", "text": "{{app_body}}"} |
| 1676 |
} |
| 1677 |
] |
| 1678 |
} |
| 1679 |
""") |
| 1680 |
) |
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
obj = NotifySlack( |
| 1685 |
token_a="T1JJ3T3L2", |
| 1686 |
token_b="A1BRTD4JD", |
| 1687 |
token_c="TIiajkdnlazkcOXrIdevi7FQ", |
| 1688 |
template=str(template), |
| 1689 |
) |
| 1690 |
|
| 1691 |
assert obj.notify(body="hello", title="world", notify_type=NotifyType.INFO) |
| 1692 |
payload = loads(mock_request.call_args.kwargs["data"]) |
| 1693 |
assert "username" not in payload |
| 1694 |
|
| 1695 |
|
| 1696 |
@mock.patch("requests.request") |
| 1697 |
def test_plugin_slack_template_invalid_json(mock_request, tmpdir): |
| 1698 |
"""NotifySlack() - blocks template with invalid JSON fails gracefully.""" |
| 1699 |
mock_request.return_value = mock.Mock( |
| 1700 |
**{"content": b"ok", "status_code": requests.codes.ok} |
| 1701 |
) |
| 1702 |
|
| 1703 |
template = tmpdir.join("bad.json") |
| 1704 |
template.write("{ not valid json }") |
| 1705 |
|
| 1706 |
obj = Apprise.instantiate( |
| 1707 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 1708 |
"?blocks=yes&template={}".format(str(template)) |
| 1709 |
) |
| 1710 |
assert isinstance(obj, NotifySlack) |
| 1711 |
|
| 1712 |
|
| 1713 |
assert ( |
| 1714 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 1715 |
) |
| 1716 |
|
| 1717 |
assert mock_request.called is False |
| 1718 |
|
| 1719 |
|
| 1720 |
@mock.patch("requests.request") |
| 1721 |
def test_plugin_slack_template_blocks_not_list(mock_request, tmpdir): |
| 1722 |
"""NotifySlack() - blocks template where 'blocks' is not a list fails.""" |
| 1723 |
mock_request.return_value = mock.Mock( |
| 1724 |
**{"content": b"ok", "status_code": requests.codes.ok} |
| 1725 |
) |
| 1726 |
|
| 1727 |
|
| 1728 |
template = tmpdir.join("bad_blocks.json") |
| 1729 |
template.write('{"blocks": "not-a-list"}') |
| 1730 |
|
| 1731 |
obj = Apprise.instantiate( |
| 1732 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 1733 |
"?blocks=yes&template={}".format(str(template)) |
| 1734 |
) |
| 1735 |
assert isinstance(obj, NotifySlack) |
| 1736 |
assert ( |
| 1737 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 1738 |
) |
| 1739 |
assert mock_request.called is False |
| 1740 |
|
| 1741 |
|
| 1742 |
template.write('{"blocks": []}') |
| 1743 |
obj2 = Apprise.instantiate( |
| 1744 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 1745 |
"?blocks=yes&template={}".format(str(template)) |
| 1746 |
) |
| 1747 |
assert isinstance(obj2, NotifySlack) |
| 1748 |
assert ( |
| 1749 |
obj2.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 1750 |
) |
| 1751 |
assert mock_request.called is False |
| 1752 |
|
| 1753 |
|
| 1754 |
@mock.patch("requests.request") |
| 1755 |
def test_plugin_slack_template_block_missing_type(mock_request, tmpdir): |
| 1756 |
"""NotifySlack() - block missing 'type' string is rejected.""" |
| 1757 |
mock_request.return_value = mock.Mock( |
| 1758 |
**{"content": b"ok", "status_code": requests.codes.ok} |
| 1759 |
) |
| 1760 |
|
| 1761 |
|
| 1762 |
template = tmpdir.join("no_type.json") |
| 1763 |
template.write('{"blocks": [{"text": {"type": "mrkdwn", "text": "hi"}}]}') |
| 1764 |
|
| 1765 |
obj = Apprise.instantiate( |
| 1766 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 1767 |
"?blocks=yes&template={}".format(str(template)) |
| 1768 |
) |
| 1769 |
assert isinstance(obj, NotifySlack) |
| 1770 |
assert ( |
| 1771 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 1772 |
) |
| 1773 |
assert mock_request.called is False |
| 1774 |
|
| 1775 |
|
| 1776 |
@mock.patch("requests.request") |
| 1777 |
def test_plugin_slack_template_load_error(mock_request, tmpdir): |
| 1778 |
"""NotifySlack() - template OSError during read fails gracefully.""" |
| 1779 |
mock_request.return_value = mock.Mock( |
| 1780 |
**{"content": b"ok", "status_code": requests.codes.ok} |
| 1781 |
) |
| 1782 |
|
| 1783 |
|
| 1784 |
template = tmpdir.join("empty.json") |
| 1785 |
template.write("") |
| 1786 |
|
| 1787 |
obj = Apprise.instantiate( |
| 1788 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 1789 |
"?blocks=yes&template={}".format(str(template)) |
| 1790 |
) |
| 1791 |
assert isinstance(obj, NotifySlack) |
| 1792 |
|
| 1793 |
with mock.patch("builtins.open", side_effect=OSError): |
| 1794 |
|
| 1795 |
assert ( |
| 1796 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) |
| 1797 |
is False |
| 1798 |
) |
| 1799 |
assert mock_request.called is False |
| 1800 |
|
| 1801 |
|
| 1802 |
def test_plugin_slack_template_bad_tokens(): |
| 1803 |
"""NotifySlack() - invalid tokens type raises TypeError.""" |
| 1804 |
with pytest.raises(TypeError): |
| 1805 |
NotifySlack( |
| 1806 |
token_a="T1JJ3T3L2", |
| 1807 |
token_b="A1BRTD4JD", |
| 1808 |
token_c="TIiajkdnlazkcOXrIdevi7FQ", |
| 1809 |
tokens="not-a-dict", |
| 1810 |
) |
| 1811 |
|
| 1812 |
|
| 1813 |
@mock.patch("requests.request") |
| 1814 |
def test_plugin_slack_template_url_roundtrip(mock_request, tmpdir): |
| 1815 |
"""NotifySlack() - template + tokens survive url()/parse_url() |
| 1816 |
round-trip.""" |
| 1817 |
mock_request.return_value = mock.Mock( |
| 1818 |
**{"content": b"ok", "status_code": requests.codes.ok} |
| 1819 |
) |
| 1820 |
|
| 1821 |
template = tmpdir.join("rt.json") |
| 1822 |
template.write( |
| 1823 |
cleandoc(""" |
| 1824 |
{ |
| 1825 |
"blocks": [ |
| 1826 |
{ |
| 1827 |
"type": "section", |
| 1828 |
"text": {"type": "mrkdwn", "text": "{{app_body}}"} |
| 1829 |
} |
| 1830 |
] |
| 1831 |
} |
| 1832 |
""") |
| 1833 |
) |
| 1834 |
|
| 1835 |
|
| 1836 |
obj1 = NotifySlack( |
| 1837 |
token_a="T1JJ3T3L2", |
| 1838 |
token_b="A1BRTD4JD", |
| 1839 |
token_c="TIiajkdnlazkcOXrIdevi7FQ", |
| 1840 |
use_blocks=True, |
| 1841 |
template=str(template), |
| 1842 |
tokens={"key1": "val1", "key2": "val2"}, |
| 1843 |
) |
| 1844 |
|
| 1845 |
|
| 1846 |
url = obj1.url() |
| 1847 |
result = NotifySlack.parse_url(url) |
| 1848 |
assert result is not None |
| 1849 |
|
| 1850 |
obj2 = NotifySlack(**result) |
| 1851 |
assert isinstance(obj2, NotifySlack) |
| 1852 |
|
| 1853 |
|
| 1854 |
assert obj1.url_identifier == obj2.url_identifier |
| 1855 |
|
| 1856 |
|
| 1857 |
assert obj2.tokens.get("key1") == "val1" |
| 1858 |
assert obj2.tokens.get("key2") == "val2" |
| 1859 |
|
| 1860 |
|
| 1861 |
assert obj2.template |
| 1862 |
|
| 1863 |
|
| 1864 |
@mock.patch("requests.request") |
| 1865 |
def test_plugin_slack_template_inaccessible(mock_request, tmpdir): |
| 1866 |
"""NotifySlack() - template attachment that cannot be accessed fails.""" |
| 1867 |
mock_request.return_value = mock.Mock( |
| 1868 |
**{"content": b"ok", "status_code": requests.codes.ok} |
| 1869 |
) |
| 1870 |
|
| 1871 |
|
| 1872 |
missing = str(tmpdir.join("missing.json")) |
| 1873 |
|
| 1874 |
obj = Apprise.instantiate( |
| 1875 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 1876 |
"?blocks=yes&template={}".format(missing) |
| 1877 |
) |
| 1878 |
assert isinstance(obj, NotifySlack) |
| 1879 |
|
| 1880 |
|
| 1881 |
assert ( |
| 1882 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 1883 |
) |
| 1884 |
assert mock_request.called is False |
| 1885 |
|
| 1886 |
|
| 1887 |
def test_plugin_slack_template_add_failure(): |
| 1888 |
"""NotifySlack() - TypeError when AppriseAttachment.add() drops entry.""" |
| 1889 |
|
| 1890 |
with mock.patch("apprise.plugins.slack.AppriseAttachment") as mock_cls: |
| 1891 |
inst = mock.MagicMock() |
| 1892 |
inst.__len__ = mock.Mock(return_value=0) |
| 1893 |
mock_cls.return_value = inst |
| 1894 |
|
| 1895 |
with pytest.raises(TypeError): |
| 1896 |
NotifySlack( |
| 1897 |
token_a="T1JJ3T3L2", |
| 1898 |
token_b="A1BRTD4JD", |
| 1899 |
token_c="TIiajkdnlazkcOXrIdevi7FQ", |
| 1900 |
template="file:///some/template.json", |
| 1901 |
) |
| 1902 |
|
| 1903 |
|
| 1904 |
@mock.patch("requests.request") |
| 1905 |
def test_plugin_slack_template_content_not_dict(mock_request, tmpdir): |
| 1906 |
"""NotifySlack() - template that parses to a JSON array is rejected.""" |
| 1907 |
mock_request.return_value = mock.Mock( |
| 1908 |
**{"content": b"ok", "status_code": requests.codes.ok} |
| 1909 |
) |
| 1910 |
|
| 1911 |
|
| 1912 |
template = tmpdir.join("array.json") |
| 1913 |
template.write('[{"type": "section"}]') |
| 1914 |
|
| 1915 |
obj = Apprise.instantiate( |
| 1916 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 1917 |
"?template={}".format(str(template)) |
| 1918 |
) |
| 1919 |
assert isinstance(obj, NotifySlack) |
| 1920 |
assert ( |
| 1921 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 1922 |
) |
| 1923 |
assert mock_request.called is False |
| 1924 |
|
| 1925 |
|
| 1926 |
@mock.patch("requests.request") |
| 1927 |
def test_plugin_slack_template_block_not_dict(mock_request, tmpdir): |
| 1928 |
"""NotifySlack() - non-dict entry in blocks list is rejected.""" |
| 1929 |
mock_request.return_value = mock.Mock( |
| 1930 |
**{"content": b"ok", "status_code": requests.codes.ok} |
| 1931 |
) |
| 1932 |
|
| 1933 |
|
| 1934 |
template = tmpdir.join("bad_block.json") |
| 1935 |
template.write('{"blocks": ["not-a-dict"]}') |
| 1936 |
|
| 1937 |
obj = Apprise.instantiate( |
| 1938 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 1939 |
"?template={}".format(str(template)) |
| 1940 |
) |
| 1941 |
assert isinstance(obj, NotifySlack) |
| 1942 |
assert ( |
| 1943 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 1944 |
) |
| 1945 |
assert mock_request.called is False |
| 1946 |
|
| 1947 |
|
| 1948 |
@mock.patch("requests.request") |
| 1949 |
def test_plugin_slack_template_none_token_value(mock_request, tmpdir): |
| 1950 |
"""NotifySlack() - None token value (e.g. app_image_url) is coerced |
| 1951 |
to empty string before JSON-escaping.""" |
| 1952 |
mock_request.return_value = mock.Mock( |
| 1953 |
**{"content": b"ok", "status_code": requests.codes.ok} |
| 1954 |
) |
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
template = tmpdir.join("img.json") |
| 1959 |
template.write( |
| 1960 |
'{"blocks": [{"type": "section",' |
| 1961 |
' "text": {"type": "mrkdwn",' |
| 1962 |
' "text": "{{app_body}} img={{app_image_url}}"}}]}' |
| 1963 |
) |
| 1964 |
|
| 1965 |
obj = Apprise.instantiate( |
| 1966 |
"slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/" |
| 1967 |
"?image=no&template={}".format(str(template)) |
| 1968 |
) |
| 1969 |
assert isinstance(obj, NotifySlack) |
| 1970 |
|
| 1971 |
assert ( |
| 1972 |
obj.notify(body="hello", title="t", notify_type=NotifyType.INFO) |
| 1973 |
is True |
| 1974 |
) |
| 1975 |
assert mock_request.called is True |
| 1976 |
payload = loads(mock_request.call_args_list[0][1]["data"]) |
| 1977 |
block_text = payload["attachments"][0]["blocks"][0]["text"]["text"] |
| 1978 |
|
| 1979 |
assert "img=" in block_text |
| 1980 |
assert "ul" not in block_text |
| 1981 |
|
| 1982 |
|
| 1983 |
@mock.patch("requests.request") |
| 1984 |
def test_plugin_slack_workflow_default_payload(mock_request): |
| 1985 |
"""NotifySlack() - Workflow Builder mode, default text payload.""" |
| 1986 |
mock_request.return_value = mock.Mock( |
| 1987 |
**{"content": b"", "status_code": requests.codes.ok} |
| 1988 |
) |
| 1989 |
|
| 1990 |
|
| 1991 |
obj = Apprise.instantiate( |
| 1992 |
"slack://T1JJ3T3L2/Ft07XXXX/XXXXXXXX/YYYYYYYY/?mode=workflow" |
| 1993 |
) |
| 1994 |
assert isinstance(obj, NotifySlack) |
| 1995 |
assert obj.mode == "workflow" |
| 1996 |
assert obj.workflow_path == [ |
| 1997 |
"T1JJ3T3L2", |
| 1998 |
"Ft07XXXX", |
| 1999 |
"XXXXXXXX", |
| 2000 |
"YYYYYYYY", |
| 2001 |
] |
| 2002 |
|
| 2003 |
|
| 2004 |
assert ( |
| 2005 |
obj.notify(body="hello", title="Alert", notify_type=NotifyType.INFO) |
| 2006 |
is True |
| 2007 |
) |
| 2008 |
assert mock_request.called is True |
| 2009 |
|
| 2010 |
call_url = mock_request.call_args_list[0][0][1] |
| 2011 |
assert "hooks.slack.com/workflows/" in call_url |
| 2012 |
assert "T1JJ3T3L2/Ft07XXXX/XXXXXXXX/YYYYYYYY" in call_url |
| 2013 |
|
| 2014 |
posted = loads(mock_request.call_args_list[0][1]["data"]) |
| 2015 |
assert posted["text"] == "Alert: hello" |
| 2016 |
|
| 2017 |
mock_request.reset_mock() |
| 2018 |
|
| 2019 |
|
| 2020 |
assert ( |
| 2021 |
obj.notify(body="body only", title="", notify_type=NotifyType.INFO) |
| 2022 |
is True |
| 2023 |
) |
| 2024 |
posted = loads(mock_request.call_args_list[0][1]["data"]) |
| 2025 |
assert posted["text"] == "body only" |
| 2026 |
|
| 2027 |
|
| 2028 |
@mock.patch("requests.request") |
| 2029 |
def test_plugin_slack_workflow_trigger_mode(mock_request): |
| 2030 |
"""NotifySlack() - Workflow trigger mode (/triggers/ URL).""" |
| 2031 |
mock_request.return_value = mock.Mock( |
| 2032 |
**{"content": b"", "status_code": requests.codes.ok} |
| 2033 |
) |
| 2034 |
|
| 2035 |
|
| 2036 |
obj = Apprise.instantiate( |
| 2037 |
"slack://T1JJ3T3L2/XXXXXXXX/YYYYYYYY/?mode=trigger" |
| 2038 |
) |
| 2039 |
assert isinstance(obj, NotifySlack) |
| 2040 |
assert obj.mode == "trigger" |
| 2041 |
assert obj.workflow_path == ["T1JJ3T3L2", "XXXXXXXX", "YYYYYYYY"] |
| 2042 |
|
| 2043 |
assert obj.notify(body="hi", title="", notify_type=NotifyType.INFO) is True |
| 2044 |
call_url = mock_request.call_args_list[0][0][1] |
| 2045 |
assert "hooks.slack.com/triggers/" in call_url |
| 2046 |
assert "T1JJ3T3L2/XXXXXXXX/YYYYYYYY" in call_url |
| 2047 |
|
| 2048 |
|
| 2049 |
@mock.patch("requests.request") |
| 2050 |
def test_plugin_slack_workflow_native_url(mock_request): |
| 2051 |
"""NotifySlack() - parse_native_url() handles workflow/trigger URLs.""" |
| 2052 |
mock_request.return_value = mock.Mock( |
| 2053 |
**{"content": b"", "status_code": requests.codes.ok} |
| 2054 |
) |
| 2055 |
|
| 2056 |
|
| 2057 |
obj = Apprise.instantiate( |
| 2058 |
"https://hooks.slack.com/workflows" |
| 2059 |
"/T1JJ3T3L2/Ft07XXXX/XXXXXXXX/YYYYYYYY" |
| 2060 |
) |
| 2061 |
assert isinstance(obj, NotifySlack) |
| 2062 |
assert obj.mode == "workflow" |
| 2063 |
assert obj.workflow_path == [ |
| 2064 |
"T1JJ3T3L2", |
| 2065 |
"Ft07XXXX", |
| 2066 |
"XXXXXXXX", |
| 2067 |
"YYYYYYYY", |
| 2068 |
] |
| 2069 |
assert ( |
| 2070 |
obj.notify(body="native", title="", notify_type=NotifyType.INFO) |
| 2071 |
is True |
| 2072 |
) |
| 2073 |
call_url = mock_request.call_args_list[0][0][1] |
| 2074 |
assert "hooks.slack.com/workflows/T1JJ3T3L2" in call_url |
| 2075 |
|
| 2076 |
mock_request.reset_mock() |
| 2077 |
|
| 2078 |
|
| 2079 |
obj2 = Apprise.instantiate( |
| 2080 |
"https://hooks.slack.com/triggers/T1JJ3T3L2/XXXXXXXX/YYYYYYYY" |
| 2081 |
) |
| 2082 |
assert isinstance(obj2, NotifySlack) |
| 2083 |
assert obj2.mode == "trigger" |
| 2084 |
assert ( |
| 2085 |
obj2.notify(body="trigger", title="", notify_type=NotifyType.INFO) |
| 2086 |
is True |
| 2087 |
) |
| 2088 |
call_url2 = mock_request.call_args_list[0][0][1] |
| 2089 |
assert "hooks.slack.com/triggers/T1JJ3T3L2" in call_url2 |
| 2090 |
|
| 2091 |
|
| 2092 |
def test_plugin_slack_parse_native_url_invalid(): |
| 2093 |
"""NotifySlack() - parse_native_url() returns None for non-Slack URLs.""" |
| 2094 |
assert ( |
| 2095 |
NotifySlack.parse_native_url( |
| 2096 |
"https://hooks.slack.com/not-a-supported-path/T1JJ3T3L2" |
| 2097 |
) |
| 2098 |
is None |
| 2099 |
) |
| 2100 |
|
| 2101 |
|
| 2102 |
@mock.patch("requests.request") |
| 2103 |
def test_plugin_slack_workflow_url_roundtrip(mock_request): |
| 2104 |
"""NotifySlack() - workflow url()/parse_url() round-trip.""" |
| 2105 |
mock_request.return_value = mock.Mock( |
| 2106 |
**{"content": b"", "status_code": requests.codes.ok} |
| 2107 |
) |
| 2108 |
|
| 2109 |
original = "slack://T1JJ3T3L2/Ft07XXXX/XXXXXXXX/YYYYYYYY/?mode=workflow" |
| 2110 |
obj = Apprise.instantiate(original) |
| 2111 |
assert isinstance(obj, NotifySlack) |
| 2112 |
|
| 2113 |
|
| 2114 |
rebuilt = Apprise.instantiate(obj.url()) |
| 2115 |
assert isinstance(rebuilt, NotifySlack) |
| 2116 |
assert rebuilt.mode == obj.mode |
| 2117 |
assert rebuilt.workflow_path == obj.workflow_path |
| 2118 |
|
| 2119 |
|
| 2120 |
def test_plugin_slack_workflow_invalid_path(): |
| 2121 |
"""NotifySlack() - invalid path segment counts raise TypeError.""" |
| 2122 |
from apprise.plugins.slack import NotifySlack as _NS |
| 2123 |
|
| 2124 |
|
| 2125 |
with pytest.raises(TypeError): |
| 2126 |
_NS(workflow_path=["T1JJ3T3L2", "XXXXXXXX"], mode="workflow") |
| 2127 |
|
| 2128 |
|
| 2129 |
with pytest.raises(TypeError): |
| 2130 |
_NS( |
| 2131 |
workflow_path=["T1JJ3T3L2", "XXXXXXXX", "YYYYYYYY"], |
| 2132 |
mode="workflow", |
| 2133 |
) |
| 2134 |
|
| 2135 |
|
| 2136 |
with pytest.raises(TypeError): |
| 2137 |
_NS( |
| 2138 |
workflow_path=["T1JJ3T3L2", "Ft07XXXX", "XXXXXXXX", "YYYY"], |
| 2139 |
mode="trigger", |
| 2140 |
) |
| 2141 |
|
| 2142 |
|
| 2143 |
with pytest.raises(TypeError): |
| 2144 |
_NS(workflow_path=[], mode="workflow") |
| 2145 |
|
| 2146 |
|
| 2147 |
with pytest.raises(TypeError): |
| 2148 |
_NS(workflow_path=42, mode="workflow") |
| 2149 |
|
| 2150 |
|
| 2151 |
obj = _NS(workflow_path=["T1JJ3T3L2", "Ft07XXXX", "XXXXXXXX", "YYY"]) |
| 2152 |
assert obj.mode == "workflow" |
| 2153 |
assert obj.workflow_path == ["T1JJ3T3L2", "Ft07XXXX", "XXXXXXXX", "YYY"] |
| 2154 |
|
| 2155 |
|
| 2156 |
obj = _NS(workflow_path=["T1JJ3T3L2", "XXXXXXXX", "YYYYYYYY"]) |
| 2157 |
assert obj.mode == "trigger" |
| 2158 |
assert obj.workflow_path == ["T1JJ3T3L2", "XXXXXXXX", "YYYYYYYY"] |
| 2159 |
|
| 2160 |
|
| 2161 |
with pytest.raises(TypeError): |
| 2162 |
_NS(workflow_path=["T1JJ3T3L2", "XXXXXXXX"]) |
| 2163 |
|
| 2164 |
|
| 2165 |
@mock.patch("requests.request") |
| 2166 |
def test_plugin_slack_workflow_template(mock_request, tmpdir): |
| 2167 |
"""NotifySlack() - Workflow mode with Block Kit JSON template.""" |
| 2168 |
mock_request.return_value = mock.Mock( |
| 2169 |
**{"content": b"", "status_code": requests.codes.ok} |
| 2170 |
) |
| 2171 |
|
| 2172 |
template = tmpdir.join("wf_blocks.json") |
| 2173 |
template.write( |
| 2174 |
cleandoc(""" |
| 2175 |
{ |
| 2176 |
"blocks": [ |
| 2177 |
{ |
| 2178 |
"type": "section", |
| 2179 |
"text": {"type": "mrkdwn", "text": "{{app_body}}"} |
| 2180 |
} |
| 2181 |
] |
| 2182 |
} |
| 2183 |
""") |
| 2184 |
) |
| 2185 |
|
| 2186 |
obj = Apprise.instantiate( |
| 2187 |
"slack://T1JJ3T3L2/Ft07XXXX/XXXXXXXX/YYYYYYYY/" |
| 2188 |
"?mode=workflow&template={}".format(str(template)) |
| 2189 |
) |
| 2190 |
assert isinstance(obj, NotifySlack) |
| 2191 |
assert obj.mode == "workflow" |
| 2192 |
assert ( |
| 2193 |
obj.notify(body="wf-tmpl", title="", notify_type=NotifyType.INFO) |
| 2194 |
is True |
| 2195 |
) |
| 2196 |
assert mock_request.called is True |
| 2197 |
call_url = mock_request.call_args_list[0][0][1] |
| 2198 |
assert "hooks.slack.com/workflows/" in call_url |
| 2199 |
posted = loads(mock_request.call_args_list[0][1]["data"]) |
| 2200 |
blocks = posted["blocks"] |
| 2201 |
section = next(b for b in blocks if b.get("type") == "section") |
| 2202 |
assert section["text"]["text"] == "wf-tmpl" |
| 2203 |
|
| 2204 |
|
| 2205 |
bad_template = tmpdir.join("bad.json") |
| 2206 |
bad_template.write("not-json!") |
| 2207 |
obj2 = Apprise.instantiate( |
| 2208 |
"slack://T1JJ3T3L2/Ft07XXXX/XXXXXXXX/YYYYYYYY/" |
| 2209 |
"?mode=workflow&template={}".format(str(bad_template)) |
| 2210 |
) |
| 2211 |
assert isinstance(obj2, NotifySlack) |
| 2212 |
assert ( |
| 2213 |
obj2.notify(body="x", title="", notify_type=NotifyType.INFO) is False |
| 2214 |
) |
| 2215 |
assert mock_request.call_count == 1 |
| 2216 |
|
| 2217 |
|
| 2218 |
@mock.patch("requests.request") |
| 2219 |
def test_plugin_slack_html_to_markdown_format(mock_request): |
| 2220 |
"""NotifySlack(): HTML body is converted to Markdown.""" |
| 2221 |
|
| 2222 |
|
| 2223 |
slack_token = "T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ" |
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
mock_request.return_value = requests.Request() |
| 2228 |
mock_request.return_value.status_code = requests.codes.ok |
| 2229 |
mock_request.return_value.content = b"ok" |
| 2230 |
mock_request.return_value.text = "ok" |
| 2231 |
|
| 2232 |
|
| 2233 |
aobj = Apprise() |
| 2234 |
assert aobj.add("slack://{}/#general".format(slack_token)) |
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
assert ( |
| 2239 |
aobj.notify( |
| 2240 |
body="<b>hello</b> <i>world</i>", |
| 2241 |
body_format=NotifyFormat.HTML, |
| 2242 |
) |
| 2243 |
is True |
| 2244 |
) |
| 2245 |
assert mock_request.call_count == 1 |
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
payload = loads(mock_request.call_args_list[0][1]["data"]) |
| 2250 |
assert payload["attachments"][0]["text"] == "*hello* _world_" |
| 2251 |
|
| 2252 |
mock_request.reset_mock() |
| 2253 |
|
| 2254 |
|
| 2255 |
assert ( |
| 2256 |
aobj.notify( |
| 2257 |
body='<a href="https://example.com/x">click here</a>', |
| 2258 |
body_format=NotifyFormat.HTML, |
| 2259 |
) |
| 2260 |
is True |
| 2261 |
) |
| 2262 |
payload = loads(mock_request.call_args_list[0][1]["data"]) |
| 2263 |
assert ( |
| 2264 |
payload["attachments"][0]["text"] |
| 2265 |
== "<https://example.com/x|click here>" |
| 2266 |
) |
| 2267 |
|
| 2268 |
mock_request.reset_mock() |
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
assert ( |
| 2273 |
aobj.notify( |
| 2274 |
body="<p>2 < 3 & 4</p>", |
| 2275 |
body_format=NotifyFormat.HTML, |
| 2276 |
) |
| 2277 |
is True |
| 2278 |
) |
| 2279 |
payload = loads(mock_request.call_args_list[0][1]["data"]) |
| 2280 |
assert payload["attachments"][0]["text"] == "2 < 3 & 4" |
| 2281 |
|
| 2282 |
mock_request.reset_mock() |
| 2283 |
|
| 2284 |
|
| 2285 |
assert aobj.notify(body="**already** slack-bound markdown #tag") is True |
| 2286 |
payload = loads(mock_request.call_args_list[0][1]["data"]) |
| 2287 |
assert ( |
| 2288 |
payload["attachments"][0]["text"] |
| 2289 |
== "**already** slack-bound markdown #tag" |
| 2290 |
) |
| 2291 |
|
| 2292 |
|
| 2293 |
@mock.patch("requests.request") |
| 2294 |
def test_plugin_slack_html_to_markdown_hardening(mock_request): |
| 2295 |
"""Test edge cases in the CommonMark-to-Slack dialect adaptation.""" |
| 2296 |
|
| 2297 |
slack_token = "T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ" |
| 2298 |
|
| 2299 |
mock_request.return_value = requests.Request() |
| 2300 |
mock_request.return_value.status_code = requests.codes.ok |
| 2301 |
mock_request.return_value.content = b"ok" |
| 2302 |
mock_request.return_value.text = "ok" |
| 2303 |
|
| 2304 |
def notify(body): |
| 2305 |
aobj = Apprise() |
| 2306 |
assert aobj.add("slack://{}/#general".format(slack_token)) |
| 2307 |
assert aobj.notify(body=body, body_format=NotifyFormat.HTML) is True |
| 2308 |
payload = loads(mock_request.call_args_list[-1][1]["data"]) |
| 2309 |
mock_request.reset_mock() |
| 2310 |
return payload["attachments"][0]["text"] |
| 2311 |
|
| 2312 |
|
| 2313 |
assert notify("<code>a < b & c</code>") == "`a < b & c`" |
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
assert notify("<b><i>x</i></b>") == "*_x_*" |
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
assert ( |
| 2322 |
notify('<a href="https://example.com/x"><b>click</b></a>') |
| 2323 |
== "<https://example.com/x|*click*>" |
| 2324 |
) |
| 2325 |
|
| 2326 |
|
| 2327 |
assert notify("<b>A</b><b>B</b>") == "*A**B*" |
| 2328 |
|
| 2329 |
|
| 2330 |
assert ( |
| 2331 |
notify('<a href="https://e/x>TAIL">click</a>') |
| 2332 |
== "<https://e/x>TAIL|click>" |
| 2333 |
) |
| 2334 |
assert ( |
| 2335 |
notify('<a href="https://e/x?a=1&b=2">click</a>') |
| 2336 |
== "<https://e/x?a=1&b=2|click>" |
| 2337 |
) |
| 2338 |
|
| 2339 |
|
| 2340 |
|
| 2341 |
assert ( |
| 2342 |
notify('<a href="https://e/x|evil">click</a>') |
| 2343 |
== "<https://e/x%7Cevil|click>" |
| 2344 |
) |
| 2345 |
|
| 2346 |
|
| 2347 |
|
| 2348 |
assert notify("literal \x020\x02 text, no code or links at all") == ( |
| 2349 |
"literal \x020\x02 text, no code or links at all" |
| 2350 |
) |
| 2351 |
|
| 2352 |
|
| 2353 |
assert NotifySlack._commonmark_to_slack("text ``unterminated") == ( |
| 2354 |
"text ``unterminated" |
| 2355 |
) |
| 2356 |
|
| 2357 |
|
| 2358 |
|
| 2359 |
assert NotifySlack._commonmark_to_slack("****x") == "x" |
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
aobj = Apprise() |
| 2364 |
assert aobj.add("slack://{}/#general?overflow=split".format(slack_token)) |
| 2365 |
assert ( |
| 2366 |
aobj.notify( |
| 2367 |
body="<b>" + ("x" * 39990) + "</b>", |
| 2368 |
body_format=NotifyFormat.HTML, |
| 2369 |
) |
| 2370 |
is True |
| 2371 |
) |
| 2372 |
assert mock_request.call_count == 2 |
| 2373 |
texts = [ |
| 2374 |
loads(c[1]["data"])["attachments"][0]["text"] |
| 2375 |
for c in mock_request.call_args_list |
| 2376 |
] |
| 2377 |
for text in texts: |
| 2378 |
assert text.count("*") % 2 == 0 |
| 2379 |
assert text.count("_") % 2 == 0 |
| 2380 |
|
| 2381 |
|
| 2382 |
|
| 2383 |
assert ( |
| 2384 |
NotifySlack._commonmark_to_slack( |
| 2385 |
"a\\>b\\(c\\)d\\[e\\]f\\!g\\#h\\*i\\_j\\~k\\\\l" |
| 2386 |
) |
| 2387 |
== "a>b(c)d[e]f!g#h\\*i\\_j\\~k\\\\l" |
| 2388 |
) |
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
assert ( |
| 2393 |
NotifySlack._commonmark_to_slack("[text](<https://incomplete") |
| 2394 |
== "[text](<https://incomplete" |
| 2395 |
) |
| 2396 |
|
| 2397 |
|
| 2398 |
def test_plugin_slack_parse_native_url_fallthrough(): |
| 2399 |
"""parse_native_url() returns None for unrecognized URLs.""" |
| 2400 |
|
| 2401 |
|
| 2402 |
assert ( |
| 2403 |
NotifySlack.parse_native_url( |
| 2404 |
"https://not-slack.com/services/ABC/DEF/GHI" |
| 2405 |
) |
| 2406 |
is None |
| 2407 |
) |