| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
# Disable logging for a cleaner testing output |
| 29 |
from json import loads |
| 30 |
import logging |
| 31 |
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.exception import AppriseImproperlyConfigured |
| 40 |
from apprise.plugins.rocketchat import NotifyRocketChat |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
# Our Testing URLs |
| 45 |
apprise_url_tests = ( |
| 46 |
( |
| 47 |
"rocket://", |
| 48 |
{ |
| 49 |
"instance": None, |
| 50 |
}, |
| 51 |
), |
| 52 |
( |
| 53 |
"rockets://", |
| 54 |
{ |
| 55 |
"instance": None, |
| 56 |
}, |
| 57 |
), |
| 58 |
( |
| 59 |
"rocket://:@/", |
| 60 |
{ |
| 61 |
"instance": None, |
| 62 |
}, |
| 63 |
), |
| 64 |
# No username or pass |
| 65 |
( |
| 66 |
"rocket://localhost", |
| 67 |
{ |
| 68 |
"instance": AppriseImproperlyConfigured, |
| 69 |
}, |
| 70 |
), |
| 71 |
# No room or channel |
| 72 |
( |
| 73 |
"rocket://user:pass@localhost", |
| 74 |
{ |
| 75 |
"instance": AppriseImproperlyConfigured, |
| 76 |
}, |
| 77 |
), |
| 78 |
# No valid rooms or channels |
| 79 |
( |
| 80 |
"rocket://user:pass@localhost/#/!/@", |
| 81 |
{ |
| 82 |
"instance": AppriseImproperlyConfigured, |
| 83 |
}, |
| 84 |
), |
| 85 |
# No user/pass combo |
| 86 |
( |
| 87 |
"rocket://user@localhost/room/", |
| 88 |
{ |
| 89 |
"instance": AppriseImproperlyConfigured, |
| 90 |
}, |
| 91 |
), |
| 92 |
# No user/pass combo |
| 93 |
( |
| 94 |
"rocket://localhost/room/", |
| 95 |
{ |
| 96 |
"instance": AppriseImproperlyConfigured, |
| 97 |
}, |
| 98 |
), |
| 99 |
# A room and port identifier |
| 100 |
( |
| 101 |
"rocket://user:pass@localhost:8080/room/", |
| 102 |
{ |
| 103 |
"instance": NotifyRocketChat, |
| 104 |
# The response text is expected to be the following on a success |
| 105 |
"requests_response_text": { |
| 106 |
"status": "success", |
| 107 |
"data": { |
| 108 |
"authToken": "abcd", |
| 109 |
"userId": "user", |
| 110 |
}, |
| 111 |
}, |
| 112 |
"privacy_url": "rocket://user:****@localhost", |
| 113 |
}, |
| 114 |
), |
| 115 |
# A channel (using the to=) |
| 116 |
( |
| 117 |
"rockets://user:pass@localhost?to=#channel", |
| 118 |
{ |
| 119 |
"instance": NotifyRocketChat, |
| 120 |
# The response text is expected to be the following on a success |
| 121 |
"requests_response_text": { |
| 122 |
"status": "success", |
| 123 |
"data": { |
| 124 |
"authToken": "abcd", |
| 125 |
"userId": "user", |
| 126 |
}, |
| 127 |
}, |
| 128 |
"privacy_url": "rockets://user:****@localhost", |
| 129 |
}, |
| 130 |
), |
| 131 |
# A channel |
| 132 |
( |
| 133 |
"rockets://user:pass@localhost/#channel", |
| 134 |
{ |
| 135 |
"instance": NotifyRocketChat, |
| 136 |
# The response text is expected to be the following on a success |
| 137 |
"requests_response_text": { |
| 138 |
"status": "success", |
| 139 |
"data": { |
| 140 |
"authToken": "abcd", |
| 141 |
"userId": "user", |
| 142 |
}, |
| 143 |
}, |
| 144 |
"privacy_url": "rockets://user:****@localhost", |
| 145 |
}, |
| 146 |
), |
| 147 |
# A channel using token based |
| 148 |
( |
| 149 |
"rockets://user:token@localhost/#channel?mode=token", |
| 150 |
{ |
| 151 |
"instance": NotifyRocketChat, |
| 152 |
"privacy_url": "rockets://user:****@localhost", |
| 153 |
}, |
| 154 |
), |
| 155 |
# Token is detected based o it's length |
| 156 |
( |
| 157 |
"rockets://user:{}@localhost/#channel".format("t" * 40), |
| 158 |
{ |
| 159 |
"instance": NotifyRocketChat, |
| 160 |
"privacy_url": "rockets://user:****@localhost", |
| 161 |
}, |
| 162 |
), |
| 163 |
# Several channels |
| 164 |
( |
| 165 |
"rocket://user:pass@localhost/#channel1/#channel2/?avatar=Yes", |
| 166 |
{ |
| 167 |
"instance": NotifyRocketChat, |
| 168 |
# The response text is expected to be the following on a success |
| 169 |
"requests_response_text": { |
| 170 |
"status": "success", |
| 171 |
"data": { |
| 172 |
"authToken": "abcd", |
| 173 |
"userId": "user", |
| 174 |
}, |
| 175 |
}, |
| 176 |
"privacy_url": "rocket://user:****@localhost", |
| 177 |
}, |
| 178 |
), |
| 179 |
# Several Rooms |
| 180 |
( |
| 181 |
"rocket://user:pass@localhost/room1/room2", |
| 182 |
{ |
| 183 |
"instance": NotifyRocketChat, |
| 184 |
# The response text is expected to be the following on a success |
| 185 |
"requests_response_text": { |
| 186 |
"status": "success", |
| 187 |
"data": { |
| 188 |
"authToken": "abcd", |
| 189 |
"userId": "user", |
| 190 |
}, |
| 191 |
}, |
| 192 |
"privacy_url": "rocket://user:****@localhost", |
| 193 |
}, |
| 194 |
), |
| 195 |
# A room and channel |
| 196 |
( |
| 197 |
"rocket://user:pass@localhost/room/#channel?mode=basic&avatar=Yes", |
| 198 |
{ |
| 199 |
"instance": NotifyRocketChat, |
| 200 |
# The response text is expected to be the following on a success |
| 201 |
"requests_response_text": { |
| 202 |
"status": "success", |
| 203 |
"data": { |
| 204 |
"authToken": "abcd", |
| 205 |
"userId": "user", |
| 206 |
}, |
| 207 |
}, |
| 208 |
# Our expected url(privacy=True) startswith() response: |
| 209 |
"privacy_url": "rocket://user:****@localhost", |
| 210 |
}, |
| 211 |
), |
| 212 |
# A user/pass where the pass matches a webtoken |
| 213 |
# to ensure we get the right mode, we enforce basic mode |
| 214 |
# so that web/token gets interpreted as a password |
| 215 |
( |
| 216 |
"rockets://user:pass%2Fwithslash@localhost/#channel/?mode=basic", |
| 217 |
{ |
| 218 |
"instance": NotifyRocketChat, |
| 219 |
# The response text is expected to be the following on a success |
| 220 |
"requests_response_text": { |
| 221 |
"status": "success", |
| 222 |
"data": { |
| 223 |
"authToken": "abcd", |
| 224 |
"userId": "user", |
| 225 |
}, |
| 226 |
}, |
| 227 |
"privacy_url": "rockets://user:****@localhost", |
| 228 |
}, |
| 229 |
), |
| 230 |
# A room and channel |
| 231 |
( |
| 232 |
"rockets://user:pass@localhost/rooma/#channela", |
| 233 |
{ |
| 234 |
# The response text is expected to be the following on a success |
| 235 |
"requests_response_code": requests.codes.ok, |
| 236 |
"requests_response_text": { |
| 237 |
# return something other then a success message type |
| 238 |
"status": "failure", |
| 239 |
}, |
| 240 |
# Exception is thrown in this case |
| 241 |
"instance": NotifyRocketChat, |
| 242 |
# Notifications will fail in this event |
| 243 |
"response": False, |
| 244 |
}, |
| 245 |
), |
| 246 |
# A web token |
| 247 |
( |
| 248 |
"rockets://web/token@localhost/@user/#channel/roomid", |
| 249 |
{ |
| 250 |
"instance": NotifyRocketChat, |
| 251 |
"privacy_url": "rockets://****@localhost/#channel/roomid", |
| 252 |
}, |
| 253 |
), |
| 254 |
( |
| 255 |
"rockets://user:web/token@localhost/@user/?mode=webhook", |
| 256 |
{ |
| 257 |
"instance": NotifyRocketChat, |
| 258 |
"privacy_url": "rockets://user:****@localhost", |
| 259 |
}, |
| 260 |
), |
| 261 |
( |
| 262 |
"rockets://user:web/token@localhost?to=@user2,#channel2", |
| 263 |
{ |
| 264 |
"instance": NotifyRocketChat, |
| 265 |
}, |
| 266 |
), |
| 267 |
( |
| 268 |
"rockets://web/token@localhost/?avatar=No", |
| 269 |
{ |
| 270 |
# a simple webhook token with default values |
| 271 |
"instance": NotifyRocketChat, |
| 272 |
# Our expected url(privacy=True) startswith() response: |
| 273 |
"privacy_url": "rockets://****@localhost/", |
| 274 |
}, |
| 275 |
), |
| 276 |
( |
| 277 |
"rockets://localhost/@user/?mode=webhook&webhook=web/token", |
| 278 |
{ |
| 279 |
"instance": NotifyRocketChat, |
| 280 |
"privacy_url": "rockets://****@localhost/@user", |
| 281 |
}, |
| 282 |
), |
| 283 |
( |
| 284 |
"rockets://user:web/token@localhost/@user/?mode=invalid", |
| 285 |
{ |
| 286 |
# invalid mode |
| 287 |
"instance": AppriseImproperlyConfigured, |
| 288 |
}, |
| 289 |
), |
| 290 |
( |
| 291 |
"rocket://user:pass@localhost:8081/room1/room2", |
| 292 |
{ |
| 293 |
"instance": NotifyRocketChat, |
| 294 |
# force a failure using basic mode |
| 295 |
"response": False, |
| 296 |
"requests_response_code": requests.codes.internal_server_error, |
| 297 |
}, |
| 298 |
), |
| 299 |
( |
| 300 |
"rockets://user:web/token@localhost?to=@user3,#channel3", |
| 301 |
{ |
| 302 |
"instance": NotifyRocketChat, |
| 303 |
# force a failure using webhook mode |
| 304 |
"response": False, |
| 305 |
"requests_response_code": requests.codes.internal_server_error, |
| 306 |
}, |
| 307 |
), |
| 308 |
( |
| 309 |
"rocket://user:pass@localhost:8082/#channel", |
| 310 |
{ |
| 311 |
"instance": NotifyRocketChat, |
| 312 |
# throw a bizarre code forcing us to fail to look it up |
| 313 |
"response": False, |
| 314 |
"requests_response_code": 999, |
| 315 |
}, |
| 316 |
), |
| 317 |
( |
| 318 |
"rocket://user:pass@localhost:8083/#chan1/#chan2/room", |
| 319 |
{ |
| 320 |
"instance": NotifyRocketChat, |
| 321 |
# Throws a series of i/o exceptions with this flag |
| 322 |
# is set and tests that we gracefully handle them |
| 323 |
"test_requests_exceptions": True, |
| 324 |
}, |
| 325 |
), |
| 326 |
) |
| 327 |
|
| 328 |
|
| 329 |
def test_plugin_rocket_chat_urls(): |
| 330 |
"""NotifyRocketChat() Apprise URLs.""" |
| 331 |
|
| 332 |
# Run our general tests |
| 333 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 334 |
|
| 335 |
|
| 336 |
@mock.patch("requests.get") |
| 337 |
@mock.patch("requests.post") |
| 338 |
def test_plugin_rocket_chat_edge_cases(mock_post, mock_get): |
| 339 |
"""NotifyRocketChat() Edge Cases.""" |
| 340 |
|
| 341 |
# Chat ID |
| 342 |
recipients = "AbcD1245, @l2g, @lead2gold, #channel, #channel2" |
| 343 |
|
| 344 |
# Authentication |
| 345 |
user = "myuser" |
| 346 |
password = "mypass" |
| 347 |
|
| 348 |
# Prepare Mock |
| 349 |
mock_get.return_value = requests.Request() |
| 350 |
mock_post.return_value = requests.Request() |
| 351 |
mock_post.return_value.status_code = requests.codes.ok |
| 352 |
mock_get.return_value.status_code = requests.codes.ok |
| 353 |
mock_post.return_value.content = "" |
| 354 |
mock_get.return_value.content = "" |
| 355 |
|
| 356 |
obj = NotifyRocketChat(user=user, password=password, targets=recipients) |
| 357 |
assert isinstance(obj, NotifyRocketChat) is True |
| 358 |
assert len(obj.channels) == 2 |
| 359 |
assert len(obj.users) == 2 |
| 360 |
assert len(obj.rooms) == 1 |
| 361 |
|
| 362 |
# No Webhook specified |
| 363 |
with pytest.raises(AppriseImproperlyConfigured): |
| 364 |
obj = NotifyRocketChat(webhook=None, mode="webhook") |
| 365 |
|
| 366 |
# |
| 367 |
# Logout |
| 368 |
# |
| 369 |
assert obj.logout() is True |
| 370 |
|
| 371 |
# Invalid JSON during Login |
| 372 |
mock_post.return_value.content = "{" |
| 373 |
mock_get.return_value.content = "}" |
| 374 |
assert obj.login() is False |
| 375 |
|
| 376 |
# Prepare Mock to fail |
| 377 |
mock_post.return_value.content = "" |
| 378 |
mock_get.return_value.content = "" |
| 379 |
mock_post.return_value.status_code = requests.codes.internal_server_error |
| 380 |
mock_get.return_value.status_code = requests.codes.internal_server_error |
| 381 |
|
| 382 |
# |
| 383 |
# Send Notification |
| 384 |
# |
| 385 |
assert ( |
| 386 |
bool( |
| 387 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 388 |
) |
| 389 |
is False |
| 390 |
) |
| 391 |
assert obj._send(payload="test", notify_type=NotifyType.INFO) is False |
| 392 |
|
| 393 |
# |
| 394 |
# Logout |
| 395 |
# |
| 396 |
assert obj.logout() is False |
| 397 |
|
| 398 |
# KeyError handling |
| 399 |
mock_post.return_value.status_code = 999 |
| 400 |
mock_get.return_value.status_code = 999 |
| 401 |
|
| 402 |
# |
| 403 |
# Send Notification |
| 404 |
# |
| 405 |
assert ( |
| 406 |
bool( |
| 407 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 408 |
) |
| 409 |
is False |
| 410 |
) |
| 411 |
assert obj._send(payload="test", notify_type=NotifyType.INFO) is False |
| 412 |
|
| 413 |
# |
| 414 |
# Logout |
| 415 |
# |
| 416 |
assert obj.logout() is False |
| 417 |
|
| 418 |
# Generate exceptions |
| 419 |
mock_get.side_effect = requests.ConnectionError( |
| 420 |
0, "requests.ConnectionError() not handled" |
| 421 |
) |
| 422 |
mock_post.side_effect = mock_get.side_effect |
| 423 |
|
| 424 |
# |
| 425 |
# Send Notification |
| 426 |
# |
| 427 |
assert obj._send(payload="test", notify_type=NotifyType.INFO) is False |
| 428 |
|
| 429 |
# Attempt the check again but fake a successful login |
| 430 |
obj.login = mock.Mock() |
| 431 |
obj.login.return_value = True |
| 432 |
assert ( |
| 433 |
bool( |
| 434 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 435 |
) |
| 436 |
is False |
| 437 |
) |
| 438 |
# |
| 439 |
# Logout |
| 440 |
# |
| 441 |
assert obj.logout() is False |
| 442 |
|
| 443 |
|
| 444 |
@mock.patch("requests.post") |
| 445 |
def test_plugin_rocket_chat_html_to_markdown_format(mock_post): |
| 446 |
"""NotifyRocketChat(): HTML body is converted to Markdown.""" |
| 447 |
|
| 448 |
# Prepare Mock -- webhook mode needs only one POST, no login |
| 449 |
mock_post.return_value = requests.Request() |
| 450 |
mock_post.return_value.status_code = requests.codes.ok |
| 451 |
mock_post.return_value.content = "" |
| 452 |
|
| 453 |
# Use webhook mode to avoid the login/logout GET flow |
| 454 |
aobj = apprise.Apprise() |
| 455 |
assert aobj.add("rockets://web/token@localhost/") |
| 456 |
|
| 457 |
# Notify with an HTML body; the framework should convert it |
| 458 |
# to Markdown before dispatching to RocketChat |
| 459 |
assert ( |
| 460 |
bool( |
| 461 |
aobj.notify( |
| 462 |
body="<b>hello</b> <i>world</i>", |
| 463 |
body_format=NotifyFormat.HTML, |
| 464 |
) |
| 465 |
) |
| 466 |
is True |
| 467 |
) |
| 468 |
assert mock_post.call_count == 1 |
| 469 |
|
| 470 |
# The body must arrive as Markdown, not as stripped plain text |
| 471 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 472 |
assert payload["text"] == "**hello** *world*" |