| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
from json import loads |
| 30 |
import logging |
| 31 |
from unittest import mock |
| 32 |
|
| 33 |
from helpers import AppriseURLTester |
| 34 |
import pytest |
| 35 |
import requests |
| 36 |
|
| 37 |
import apprise |
| 38 |
from apprise import NotifyFormat, NotifyType |
| 39 |
from apprise.plugins.rocketchat import NotifyRocketChat |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
|
| 44 |
apprise_url_tests = ( |
| 45 |
( |
| 46 |
"rocket://", |
| 47 |
{ |
| 48 |
"instance": None, |
| 49 |
}, |
| 50 |
), |
| 51 |
( |
| 52 |
"rockets://", |
| 53 |
{ |
| 54 |
"instance": None, |
| 55 |
}, |
| 56 |
), |
| 57 |
( |
| 58 |
"rocket://:@/", |
| 59 |
{ |
| 60 |
"instance": None, |
| 61 |
}, |
| 62 |
), |
| 63 |
|
| 64 |
( |
| 65 |
"rocket://localhost", |
| 66 |
{ |
| 67 |
"instance": TypeError, |
| 68 |
}, |
| 69 |
), |
| 70 |
|
| 71 |
( |
| 72 |
"rocket://user:pass@localhost", |
| 73 |
{ |
| 74 |
"instance": TypeError, |
| 75 |
}, |
| 76 |
), |
| 77 |
|
| 78 |
( |
| 79 |
"rocket://user:pass@localhost/#/!/@", |
| 80 |
{ |
| 81 |
"instance": TypeError, |
| 82 |
}, |
| 83 |
), |
| 84 |
|
| 85 |
( |
| 86 |
"rocket://user@localhost/room/", |
| 87 |
{ |
| 88 |
"instance": TypeError, |
| 89 |
}, |
| 90 |
), |
| 91 |
|
| 92 |
( |
| 93 |
"rocket://localhost/room/", |
| 94 |
{ |
| 95 |
"instance": TypeError, |
| 96 |
}, |
| 97 |
), |
| 98 |
|
| 99 |
( |
| 100 |
"rocket://user:pass@localhost:8080/room/", |
| 101 |
{ |
| 102 |
"instance": NotifyRocketChat, |
| 103 |
|
| 104 |
"requests_response_text": { |
| 105 |
"status": "success", |
| 106 |
"data": { |
| 107 |
"authToken": "abcd", |
| 108 |
"userId": "user", |
| 109 |
}, |
| 110 |
}, |
| 111 |
"privacy_url": "rocket://user:****@localhost", |
| 112 |
}, |
| 113 |
), |
| 114 |
|
| 115 |
( |
| 116 |
"rockets://user:pass@localhost?to=#channel", |
| 117 |
{ |
| 118 |
"instance": NotifyRocketChat, |
| 119 |
|
| 120 |
"requests_response_text": { |
| 121 |
"status": "success", |
| 122 |
"data": { |
| 123 |
"authToken": "abcd", |
| 124 |
"userId": "user", |
| 125 |
}, |
| 126 |
}, |
| 127 |
"privacy_url": "rockets://user:****@localhost", |
| 128 |
}, |
| 129 |
), |
| 130 |
|
| 131 |
( |
| 132 |
"rockets://user:pass@localhost/#channel", |
| 133 |
{ |
| 134 |
"instance": NotifyRocketChat, |
| 135 |
|
| 136 |
"requests_response_text": { |
| 137 |
"status": "success", |
| 138 |
"data": { |
| 139 |
"authToken": "abcd", |
| 140 |
"userId": "user", |
| 141 |
}, |
| 142 |
}, |
| 143 |
"privacy_url": "rockets://user:****@localhost", |
| 144 |
}, |
| 145 |
), |
| 146 |
|
| 147 |
( |
| 148 |
"rockets://user:token@localhost/#channel?mode=token", |
| 149 |
{ |
| 150 |
"instance": NotifyRocketChat, |
| 151 |
"privacy_url": "rockets://user:****@localhost", |
| 152 |
}, |
| 153 |
), |
| 154 |
|
| 155 |
( |
| 156 |
"rockets://user:{}@localhost/#channel".format("t" * 40), |
| 157 |
{ |
| 158 |
"instance": NotifyRocketChat, |
| 159 |
"privacy_url": "rockets://user:****@localhost", |
| 160 |
}, |
| 161 |
), |
| 162 |
|
| 163 |
( |
| 164 |
"rocket://user:pass@localhost/#channel1/#channel2/?avatar=Yes", |
| 165 |
{ |
| 166 |
"instance": NotifyRocketChat, |
| 167 |
|
| 168 |
"requests_response_text": { |
| 169 |
"status": "success", |
| 170 |
"data": { |
| 171 |
"authToken": "abcd", |
| 172 |
"userId": "user", |
| 173 |
}, |
| 174 |
}, |
| 175 |
"privacy_url": "rocket://user:****@localhost", |
| 176 |
}, |
| 177 |
), |
| 178 |
|
| 179 |
( |
| 180 |
"rocket://user:pass@localhost/room1/room2", |
| 181 |
{ |
| 182 |
"instance": NotifyRocketChat, |
| 183 |
|
| 184 |
"requests_response_text": { |
| 185 |
"status": "success", |
| 186 |
"data": { |
| 187 |
"authToken": "abcd", |
| 188 |
"userId": "user", |
| 189 |
}, |
| 190 |
}, |
| 191 |
"privacy_url": "rocket://user:****@localhost", |
| 192 |
}, |
| 193 |
), |
| 194 |
|
| 195 |
( |
| 196 |
"rocket://user:pass@localhost/room/#channel?mode=basic&avatar=Yes", |
| 197 |
{ |
| 198 |
"instance": NotifyRocketChat, |
| 199 |
|
| 200 |
"requests_response_text": { |
| 201 |
"status": "success", |
| 202 |
"data": { |
| 203 |
"authToken": "abcd", |
| 204 |
"userId": "user", |
| 205 |
}, |
| 206 |
}, |
| 207 |
|
| 208 |
"privacy_url": "rocket://user:****@localhost", |
| 209 |
}, |
| 210 |
), |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
( |
| 215 |
"rockets://user:pass%2Fwithslash@localhost/#channel/?mode=basic", |
| 216 |
{ |
| 217 |
"instance": NotifyRocketChat, |
| 218 |
|
| 219 |
"requests_response_text": { |
| 220 |
"status": "success", |
| 221 |
"data": { |
| 222 |
"authToken": "abcd", |
| 223 |
"userId": "user", |
| 224 |
}, |
| 225 |
}, |
| 226 |
"privacy_url": "rockets://user:****@localhost", |
| 227 |
}, |
| 228 |
), |
| 229 |
|
| 230 |
( |
| 231 |
"rockets://user:pass@localhost/rooma/#channela", |
| 232 |
{ |
| 233 |
|
| 234 |
"requests_response_code": requests.codes.ok, |
| 235 |
"requests_response_text": { |
| 236 |
|
| 237 |
"status": "failure", |
| 238 |
}, |
| 239 |
|
| 240 |
"instance": NotifyRocketChat, |
| 241 |
|
| 242 |
"response": False, |
| 243 |
}, |
| 244 |
), |
| 245 |
|
| 246 |
( |
| 247 |
"rockets://web/token@localhost/@user/#channel/roomid", |
| 248 |
{ |
| 249 |
"instance": NotifyRocketChat, |
| 250 |
"privacy_url": "rockets://****@localhost/#channel/roomid", |
| 251 |
}, |
| 252 |
), |
| 253 |
( |
| 254 |
"rockets://user:web/token@localhost/@user/?mode=webhook", |
| 255 |
{ |
| 256 |
"instance": NotifyRocketChat, |
| 257 |
"privacy_url": "rockets://user:****@localhost", |
| 258 |
}, |
| 259 |
), |
| 260 |
( |
| 261 |
"rockets://user:web/token@localhost?to=@user2,#channel2", |
| 262 |
{ |
| 263 |
"instance": NotifyRocketChat, |
| 264 |
}, |
| 265 |
), |
| 266 |
( |
| 267 |
"rockets://web/token@localhost/?avatar=No", |
| 268 |
{ |
| 269 |
|
| 270 |
"instance": NotifyRocketChat, |
| 271 |
|
| 272 |
"privacy_url": "rockets://****@localhost/", |
| 273 |
}, |
| 274 |
), |
| 275 |
( |
| 276 |
"rockets://localhost/@user/?mode=webhook&webhook=web/token", |
| 277 |
{ |
| 278 |
"instance": NotifyRocketChat, |
| 279 |
"privacy_url": "rockets://****@localhost/@user", |
| 280 |
}, |
| 281 |
), |
| 282 |
( |
| 283 |
"rockets://user:web/token@localhost/@user/?mode=invalid", |
| 284 |
{ |
| 285 |
|
| 286 |
"instance": TypeError, |
| 287 |
}, |
| 288 |
), |
| 289 |
( |
| 290 |
"rocket://user:pass@localhost:8081/room1/room2", |
| 291 |
{ |
| 292 |
"instance": NotifyRocketChat, |
| 293 |
|
| 294 |
"response": False, |
| 295 |
"requests_response_code": requests.codes.internal_server_error, |
| 296 |
}, |
| 297 |
), |
| 298 |
( |
| 299 |
"rockets://user:web/token@localhost?to=@user3,#channel3", |
| 300 |
{ |
| 301 |
"instance": NotifyRocketChat, |
| 302 |
|
| 303 |
"response": False, |
| 304 |
"requests_response_code": requests.codes.internal_server_error, |
| 305 |
}, |
| 306 |
), |
| 307 |
( |
| 308 |
"rocket://user:pass@localhost:8082/#channel", |
| 309 |
{ |
| 310 |
"instance": NotifyRocketChat, |
| 311 |
|
| 312 |
"response": False, |
| 313 |
"requests_response_code": 999, |
| 314 |
}, |
| 315 |
), |
| 316 |
( |
| 317 |
"rocket://user:pass@localhost:8083/#chan1/#chan2/room", |
| 318 |
{ |
| 319 |
"instance": NotifyRocketChat, |
| 320 |
|
| 321 |
|
| 322 |
"test_requests_exceptions": True, |
| 323 |
}, |
| 324 |
), |
| 325 |
) |
| 326 |
|
| 327 |
|
| 328 |
def test_plugin_rocket_chat_urls(): |
| 329 |
"""NotifyRocketChat() Apprise URLs.""" |
| 330 |
|
| 331 |
|
| 332 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 333 |
|
| 334 |
|
| 335 |
@mock.patch("requests.get") |
| 336 |
@mock.patch("requests.post") |
| 337 |
def test_plugin_rocket_chat_edge_cases(mock_post, mock_get): |
| 338 |
"""NotifyRocketChat() Edge Cases.""" |
| 339 |
|
| 340 |
|
| 341 |
recipients = "AbcD1245, @l2g, @lead2gold, #channel, #channel2" |
| 342 |
|
| 343 |
|
| 344 |
user = "myuser" |
| 345 |
password = "mypass" |
| 346 |
|
| 347 |
|
| 348 |
mock_get.return_value = requests.Request() |
| 349 |
mock_post.return_value = requests.Request() |
| 350 |
mock_post.return_value.status_code = requests.codes.ok |
| 351 |
mock_get.return_value.status_code = requests.codes.ok |
| 352 |
mock_post.return_value.content = "" |
| 353 |
mock_get.return_value.content = "" |
| 354 |
|
| 355 |
obj = NotifyRocketChat(user=user, password=password, targets=recipients) |
| 356 |
assert isinstance(obj, NotifyRocketChat) is True |
| 357 |
assert len(obj.channels) == 2 |
| 358 |
assert len(obj.users) == 2 |
| 359 |
assert len(obj.rooms) == 1 |
| 360 |
|
| 361 |
|
| 362 |
with pytest.raises(TypeError): |
| 363 |
obj = NotifyRocketChat(webhook=None, mode="webhook") |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
assert obj.logout() is True |
| 369 |
|
| 370 |
|
| 371 |
mock_post.return_value.content = "{" |
| 372 |
mock_get.return_value.content = "}" |
| 373 |
assert obj.login() is False |
| 374 |
|
| 375 |
|
| 376 |
mock_post.return_value.content = "" |
| 377 |
mock_get.return_value.content = "" |
| 378 |
mock_post.return_value.status_code = requests.codes.internal_server_error |
| 379 |
mock_get.return_value.status_code = requests.codes.internal_server_error |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
assert ( |
| 385 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 386 |
is False |
| 387 |
) |
| 388 |
assert obj._send(payload="test", notify_type=NotifyType.INFO) is False |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
assert obj.logout() is False |
| 394 |
|
| 395 |
|
| 396 |
mock_post.return_value.status_code = 999 |
| 397 |
mock_get.return_value.status_code = 999 |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
assert ( |
| 403 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 404 |
is False |
| 405 |
) |
| 406 |
assert obj._send(payload="test", notify_type=NotifyType.INFO) is False |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
assert obj.logout() is False |
| 412 |
|
| 413 |
|
| 414 |
mock_get.side_effect = requests.ConnectionError( |
| 415 |
0, "requests.ConnectionError() not handled" |
| 416 |
) |
| 417 |
mock_post.side_effect = mock_get.side_effect |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
assert obj._send(payload="test", notify_type=NotifyType.INFO) is False |
| 423 |
|
| 424 |
|
| 425 |
obj.login = mock.Mock() |
| 426 |
obj.login.return_value = True |
| 427 |
assert ( |
| 428 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 429 |
is False |
| 430 |
) |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
assert obj.logout() is False |
| 435 |
|
| 436 |
|
| 437 |
@mock.patch("requests.post") |
| 438 |
def test_plugin_rocket_chat_html_to_markdown_format(mock_post): |
| 439 |
"""NotifyRocketChat(): HTML body is converted to Markdown.""" |
| 440 |
|
| 441 |
|
| 442 |
mock_post.return_value = requests.Request() |
| 443 |
mock_post.return_value.status_code = requests.codes.ok |
| 444 |
mock_post.return_value.content = "" |
| 445 |
|
| 446 |
|
| 447 |
aobj = apprise.Apprise() |
| 448 |
assert aobj.add("rockets://web/token@localhost/") |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
assert ( |
| 453 |
aobj.notify( |
| 454 |
body="<b>hello</b> <i>world</i>", |
| 455 |
body_format=NotifyFormat.HTML, |
| 456 |
) |
| 457 |
is True |
| 458 |
) |
| 459 |
assert mock_post.call_count == 1 |
| 460 |
|
| 461 |
|
| 462 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 463 |
assert payload["text"] == "**hello** *world*" |