Debian Codegraph @

tests/test_plugin_rocket_chat.py

raw symbols SCIP
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.plugins.rocketchat import NotifyRocketChat
40
41 logging.disable(logging.CRITICAL)
42
43 # Our Testing URLs
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 # No username or pass
64 (
65 "rocket://localhost",
66 {
67 "instance": TypeError,
68 },
69 ),
70 # No room or channel
71 (
72 "rocket://user:pass@localhost",
73 {
74 "instance": TypeError,
75 },
76 ),
77 # No valid rooms or channels
78 (
79 "rocket://user:pass@localhost/#/!/@",
80 {
81 "instance": TypeError,
82 },
83 ),
84 # No user/pass combo
85 (
86 "rocket://user@localhost/room/",
87 {
88 "instance": TypeError,
89 },
90 ),
91 # No user/pass combo
92 (
93 "rocket://localhost/room/",
94 {
95 "instance": TypeError,
96 },
97 ),
98 # A room and port identifier
99 (
100 "rocket://user:pass@localhost:8080/room/",
101 {
102 "instance": NotifyRocketChat,
103 # The response text is expected to be the following on a success
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 # A channel (using the to=)
115 (
116 "rockets://user:pass@localhost?to=#channel",
117 {
118 "instance": NotifyRocketChat,
119 # The response text is expected to be the following on a success
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 # A channel
131 (
132 "rockets://user:pass@localhost/#channel",
133 {
134 "instance": NotifyRocketChat,
135 # The response text is expected to be the following on a success
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 # A channel using token based
147 (
148 "rockets://user:token@localhost/#channel?mode=token",
149 {
150 "instance": NotifyRocketChat,
151 "privacy_url": "rockets://user:****@localhost",
152 },
153 ),
154 # Token is detected based o it's length
155 (
156 "rockets://user:{}@localhost/#channel".format("t" * 40),
157 {
158 "instance": NotifyRocketChat,
159 "privacy_url": "rockets://user:****@localhost",
160 },
161 ),
162 # Several channels
163 (
164 "rocket://user:pass@localhost/#channel1/#channel2/?avatar=Yes",
165 {
166 "instance": NotifyRocketChat,
167 # The response text is expected to be the following on a success
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 # Several Rooms
179 (
180 "rocket://user:pass@localhost/room1/room2",
181 {
182 "instance": NotifyRocketChat,
183 # The response text is expected to be the following on a success
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 # A room and channel
195 (
196 "rocket://user:pass@localhost/room/#channel?mode=basic&avatar=Yes",
197 {
198 "instance": NotifyRocketChat,
199 # The response text is expected to be the following on a success
200 "requests_response_text": {
201 "status": "success",
202 "data": {
203 "authToken": "abcd",
204 "userId": "user",
205 },
206 },
207 # Our expected url(privacy=True) startswith() response:
208 "privacy_url": "rocket://user:****@localhost",
209 },
210 ),
211 # A user/pass where the pass matches a webtoken
212 # to ensure we get the right mode, we enforce basic mode
213 # so that web/token gets interpreted as a password
214 (
215 "rockets://user:pass%2Fwithslash@localhost/#channel/?mode=basic",
216 {
217 "instance": NotifyRocketChat,
218 # The response text is expected to be the following on a success
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 # A room and channel
230 (
231 "rockets://user:pass@localhost/rooma/#channela",
232 {
233 # The response text is expected to be the following on a success
234 "requests_response_code": requests.codes.ok,
235 "requests_response_text": {
236 # return something other then a success message type
237 "status": "failure",
238 },
239 # Exception is thrown in this case
240 "instance": NotifyRocketChat,
241 # Notifications will fail in this event
242 "response": False,
243 },
244 ),
245 # A web token
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 # a simple webhook token with default values
270 "instance": NotifyRocketChat,
271 # Our expected url(privacy=True) startswith() response:
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 # invalid mode
286 "instance": TypeError,
287 },
288 ),
289 (
290 "rocket://user:pass@localhost:8081/room1/room2",
291 {
292 "instance": NotifyRocketChat,
293 # force a failure using basic mode
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 # force a failure using webhook mode
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 # throw a bizarre code forcing us to fail to look it up
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 # Throws a series of i/o exceptions with this flag
321 # is set and tests that we gracefully handle them
322 "test_requests_exceptions": True,
323 },
324 ),
325 )
326
327
328 def test_plugin_rocket_chat_urls():
329 """NotifyRocketChat() Apprise URLs."""
330
331 # Run our general tests
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 # Chat ID
341 recipients = "AbcD1245, @l2g, @lead2gold, #channel, #channel2"
342
343 # Authentication
344 user = "myuser"
345 password = "mypass"
346
347 # Prepare Mock
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 # No Webhook specified
362 with pytest.raises(TypeError):
363 obj = NotifyRocketChat(webhook=None, mode="webhook")
364
365 #
366 # Logout
367 #
368 assert obj.logout() is True
369
370 # Invalid JSON during Login
371 mock_post.return_value.content = "{"
372 mock_get.return_value.content = "}"
373 assert obj.login() is False
374
375 # Prepare Mock to fail
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 # Send Notification
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 # Logout
392 #
393 assert obj.logout() is False
394
395 # KeyError handling
396 mock_post.return_value.status_code = 999
397 mock_get.return_value.status_code = 999
398
399 #
400 # Send Notification
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 # Logout
410 #
411 assert obj.logout() is False
412
413 # Generate exceptions
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 # Send Notification
421 #
422 assert obj._send(payload="test", notify_type=NotifyType.INFO) is False
423
424 # Attempt the check again but fake a successful login
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 # Logout
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 # Prepare Mock -- webhook mode needs only one POST, no login
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 # Use webhook mode to avoid the login/logout GET flow
447 aobj = apprise.Apprise()
448 assert aobj.add("rockets://web/token@localhost/")
449
450 # Notify with an HTML body; the framework should convert it
451 # to Markdown before dispatching to RocketChat
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 # The body must arrive as Markdown, not as stripped plain text
462 payload = loads(mock_post.call_args_list[0][1]["data"])
463 assert payload["text"] == "**hello** *world*"