Debian Codegraph @

tests/test_plugin_webex_teams.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 import os
32 from unittest import mock
33
34 from helpers import AppriseURLTester
35 import pytest
36 import requests
37
38 from apprise import Apprise, AppriseAttachment, NotifyFormat, NotifyType
39 from apprise.plugins.webexteams import (
40 NotifyWebexTeams,
41 WebexTeamsMode,
42 )
43
44 logging.disable(logging.CRITICAL)
45
46 # Attachment Directory
47 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
48
49 # A valid webhook token (80 lowercase alphanumeric chars)
50 WEBHOOK_TOKEN = "a" * 80
51
52 # A valid bot access token (longer than 160 chars, contains hyphen)
53 # Must be > 160 chars or contain non-alphanumeric chars to bypass
54 # the webhook-token regex (which only allows [a-z0-9], 80-160 chars).
55 # Using 200 chars ending in '0' so the privacy URL ends in '0'.
56 BOT_TOKEN = "Bc10" * 50 # 200 chars, ends in '0'
57
58 # A valid Webex room ID (base64url-like, 50 chars)
59 ROOM_ID = "Y2lzY29zcGFyazovL3VzL1JPTU9NLzEyMzQ1Njc4OTAxMjM0"
60 ROOM_ID2 = "Y2lzY29zcGFyazovL3VzL1JPTU9NL2FiY2RlZjEyMzQ1Njc4"
61
62 # Our Testing URLs
63 apprise_url_tests = (
64 (
65 "wxteams://",
66 {
67 # Token missing
68 "instance": TypeError,
69 },
70 ),
71 (
72 "wxteams://:@/",
73 {
74 # We don't have strict host checking on for wxteams, so this
75 # URL actually becomes parseable and :@ becomes a hostname.
76 # The below errors because a second token wasn't found
77 "instance": TypeError,
78 },
79 ),
80 (
81 "wxteams://{}".format("a" * 80),
82 {
83 # webhook token provided - we're good
84 "instance": NotifyWebexTeams,
85 # Our expected url(privacy=True) startswith() response:
86 "privacy_url": "wxteams://a...a/",
87 },
88 ),
89 (
90 "wxteams://?token={}".format("a" * 80),
91 {
92 # token as query param - we're good
93 "instance": NotifyWebexTeams,
94 # Our expected url(privacy=True) startswith() response:
95 "privacy_url": "wxteams://a...a/",
96 },
97 ),
98 (
99 "webex://{}".format("a" * 140),
100 {
101 # token provided via 'webex' schema - we're good
102 "instance": NotifyWebexTeams,
103 # Our expected url(privacy=True) startswith() response:
104 "privacy_url": "wxteams://a...a/",
105 },
106 ),
107 # Support Native Webhook URLs
108 (
109 "https://api.ciscospark.com/v1/webhooks/incoming/{}".format("a" * 80),
110 {
111 # token provided - we're good
112 "instance": NotifyWebexTeams,
113 },
114 ),
115 # Support New Native Webhook URLs
116 (
117 "https://webexapis.com/v1/webhooks/incoming/{}".format("a" * 100),
118 {
119 # token provided - we're good
120 "instance": NotifyWebexTeams,
121 },
122 ),
123 # Support Native Webhook URLs with arguments
124 (
125 "https://api.ciscospark.com/v1/webhooks/incoming/{}"
126 "?format=text".format("a" * 80),
127 {
128 # token provided - we're good
129 "instance": NotifyWebexTeams,
130 },
131 ),
132 # Bot mode: access_token + room ID
133 (
134 "wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID),
135 {
136 "instance": NotifyWebexTeams,
137 "privacy_url": "wxteams://B...0/",
138 },
139 ),
140 # Bot mode: explicit mode=bot with no room IDs - loads but fails to send
141 (
142 "wxteams://{}?mode=bot".format("a" * 80),
143 {
144 "instance": NotifyWebexTeams,
145 # notify() returns False with no targets (no HTTP call needed)
146 "notify_response": False,
147 },
148 ),
149 # Explicit mode=webhook with a valid webhook token
150 (
151 "wxteams://{}?mode=webhook".format("a" * 80),
152 {
153 "instance": NotifyWebexTeams,
154 "privacy_url": "wxteams://a...a/",
155 },
156 ),
157 # Invalid mode
158 (
159 "wxteams://{}?mode=invalid".format("a" * 80),
160 {
161 "instance": TypeError,
162 },
163 ),
164 # Webhook mode: HTTP 500 response
165 (
166 "wxteams://{}".format("a" * 80),
167 {
168 "instance": NotifyWebexTeams,
169 "response": False,
170 "requests_response_code": (requests.codes.internal_server_error),
171 },
172 ),
173 # Webhook mode: unusual HTTP response code
174 (
175 "wxteams://{}".format("a" * 80),
176 {
177 "instance": NotifyWebexTeams,
178 # throw a bizarre code forcing us to fail to look it up
179 "response": False,
180 "requests_response_code": 999,
181 },
182 ),
183 # Webhook mode: request exception
184 (
185 "wxteams://{}".format("a" * 80),
186 {
187 "instance": NotifyWebexTeams,
188 # Throws a series of i/o exceptions with this flag set and
189 # tests that we gracefully handle them
190 "test_requests_exceptions": True,
191 },
192 ),
193 # Bot mode: HTTP 500 response
194 (
195 "wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID),
196 {
197 "instance": NotifyWebexTeams,
198 "response": False,
199 "requests_response_code": (requests.codes.internal_server_error),
200 },
201 ),
202 # Bot mode: unusual HTTP response code
203 (
204 "wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID),
205 {
206 "instance": NotifyWebexTeams,
207 "response": False,
208 "requests_response_code": 999,
209 },
210 ),
211 # Bot mode: request exception
212 (
213 "wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID),
214 {
215 "instance": NotifyWebexTeams,
216 "test_requests_exceptions": True,
217 },
218 ),
219 )
220
221
222 def test_plugin_webex_teams_urls():
223 """NotifyWebexTeams() Apprise URLs."""
224
225 # Run our general tests
226 AppriseURLTester(tests=apprise_url_tests).run_all()
227
228
229 def test_plugin_webex_teams_webhook_mode():
230 """NotifyWebexTeams() Webhook mode direct tests."""
231
232 # Valid webhook token
233 token = "b" * 80
234
235 obj = NotifyWebexTeams(token=token)
236 assert isinstance(obj, NotifyWebexTeams)
237 assert obj.mode == WebexTeamsMode.WEBHOOK
238
239 # url() round-trip
240 url = obj.url()
241 assert "wxteams://" in url
242 assert "mode=webhook" in url
243
244 # parse_url round-trip
245 result = NotifyWebexTeams.parse_url(url)
246 assert result is not None
247 obj2 = NotifyWebexTeams(**result)
248 assert obj2.mode == WebexTeamsMode.WEBHOOK
249 assert obj2.token == token
250
251 # privacy URL
252 priv = obj.url(privacy=True)
253 assert obj.token not in priv
254
255 # url_identifier
256 assert obj.url_identifier == (
257 NotifyWebexTeams.secure_protocol[0],
258 token,
259 )
260
261 # 80-char token (minimum)
262 assert NotifyWebexTeams(token="c" * 80)
263 # 160-char token (maximum)
264 assert NotifyWebexTeams(token="c" * 160)
265
266 # Too short for webhook -> falls to bot mode (no targets), loads but
267 # send() returns False
268 obj_short = NotifyWebexTeams(token="c" * 79)
269 assert obj_short.mode == WebexTeamsMode.BOT
270 assert obj_short.send(body="test") is False
271
272 # Too long for webhook -> bot mode, same behaviour
273 obj_long = NotifyWebexTeams(token="c" * 161)
274 assert obj_long.mode == WebexTeamsMode.BOT
275 assert obj_long.send(body="test") is False
276
277 # No token -> TypeError (no candidate at all)
278 with pytest.raises(TypeError):
279 NotifyWebexTeams(token=None)
280
281 # Empty token -> TypeError
282 with pytest.raises(TypeError):
283 NotifyWebexTeams(token="")
284
285 # Non-alphanumeric chars -> falls to bot mode (no targets)
286 obj_hyph = NotifyWebexTeams(token="a-b-" * 20)
287 assert obj_hyph.mode == WebexTeamsMode.BOT
288 assert obj_hyph.send(body="test") is False
289
290
291 def test_plugin_webex_teams_bot_mode():
292 """NotifyWebexTeams() Bot mode direct tests."""
293
294 obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID])
295 assert isinstance(obj, NotifyWebexTeams)
296 assert obj.mode == WebexTeamsMode.BOT
297 assert obj.access_token == BOT_TOKEN
298 assert ROOM_ID in obj.targets
299
300 # url() round-trip
301 url = obj.url()
302 assert "wxteams://" in url
303 assert "mode=bot" in url
304
305 result = NotifyWebexTeams.parse_url(url)
306 assert result is not None
307 obj2 = NotifyWebexTeams(**result)
308 assert obj2.mode == WebexTeamsMode.BOT
309 assert obj2.access_token == BOT_TOKEN
310 assert ROOM_ID in obj2.targets
311
312 # privacy URL masks the token
313 priv = obj.url(privacy=True)
314 assert BOT_TOKEN not in priv
315
316 # url_identifier
317 assert obj.url_identifier == (
318 NotifyWebexTeams.secure_protocol[0],
319 BOT_TOKEN,
320 )
321
322 # Multiple rooms
323 obj3 = NotifyWebexTeams(
324 access_token=BOT_TOKEN, targets=[ROOM_ID, ROOM_ID2]
325 )
326 assert len(obj3.targets) == 2
327
328 # No room IDs -> loads fine, but send() returns False
329 obj_no_rooms = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[])
330 assert isinstance(obj_no_rooms, NotifyWebexTeams)
331 assert obj_no_rooms.send(body="test") is False
332
333 # No access_token -> TypeError
334 with pytest.raises(TypeError):
335 NotifyWebexTeams(access_token=None, targets=[ROOM_ID])
336
337 # Explicit bot mode with neither token nor access_token -> TypeError
338 with pytest.raises(TypeError):
339 NotifyWebexTeams(mode="bot", targets=[ROOM_ID])
340
341 # Explicit mode=bot on a short (webhook-style) token
342 obj4 = NotifyWebexTeams(token="a" * 80, mode="bot", targets=[ROOM_ID])
343 assert obj4.mode == WebexTeamsMode.BOT
344 # The 'token' argument is used as access_token in bot mode
345 assert obj4.access_token == "a" * 80
346
347 # Invalid mode
348 with pytest.raises(TypeError):
349 NotifyWebexTeams(
350 access_token=BOT_TOKEN,
351 targets=[ROOM_ID],
352 mode="invalid",
353 )
354
355
356 def test_plugin_webex_teams_body_maxlen():
357 """NotifyWebexTeams() body_maxlen varies by mode."""
358
359 # Webhook mode: 1000 chars
360 obj = NotifyWebexTeams(token=WEBHOOK_TOKEN)
361 assert obj.body_maxlen == 1000
362
363 # Bot mode: 7439 chars
364 obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID])
365 assert obj.body_maxlen == 7439
366
367
368 def test_plugin_webex_teams_mode_detection():
369 """NotifyWebexTeams() auto-detects mode from token format."""
370
371 # 80-char lowercase alphanumeric -> WEBHOOK
372 obj = NotifyWebexTeams(token="a" * 80)
373 assert obj.mode == WebexTeamsMode.WEBHOOK
374
375 # 80-char uppercase alphanumeric also matches webhook regex
376 # (regex uses 'i' flag); no targets means webhook mode
377 obj = NotifyWebexTeams(token="A" * 80)
378 assert obj.mode == WebexTeamsMode.WEBHOOK
379
380 # Token longer than 160 chars -> fails webhook regex -> BOT
381 obj = NotifyWebexTeams(token="a" * 200, targets=[ROOM_ID])
382 assert obj.mode == WebexTeamsMode.BOT
383
384 # Token with hyphens (non-alphanumeric) -> fails webhook regex -> BOT
385 obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID])
386 assert obj.mode == WebexTeamsMode.BOT
387
388
389 def test_plugin_webex_teams_url_parsing():
390 """NotifyWebexTeams() URL parsing edge cases."""
391
392 # Webhook via ?token= param
393 result = NotifyWebexTeams.parse_url("wxteams://?token={}".format("a" * 80))
394 assert result is not None
395 obj = NotifyWebexTeams(**result)
396 assert obj.mode == WebexTeamsMode.WEBHOOK
397 assert obj.token == "a" * 80
398
399 # Bot mode: access_token in host, room ID in path
400 result = NotifyWebexTeams.parse_url(
401 "wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID)
402 )
403 assert result is not None
404 obj = NotifyWebexTeams(**result)
405 assert obj.mode == WebexTeamsMode.BOT
406 assert ROOM_ID in obj.targets
407
408 # Bot mode: multiple room IDs in path
409 result = NotifyWebexTeams.parse_url(
410 "wxteams://{}/{}/{}".format(BOT_TOKEN, ROOM_ID, ROOM_ID2)
411 )
412 assert result is not None
413 obj = NotifyWebexTeams(**result)
414 assert obj.mode == WebexTeamsMode.BOT
415 assert len(obj.targets) == 2
416
417 # Bot mode: room IDs via ?to= param
418 result = NotifyWebexTeams.parse_url(
419 "wxteams://{}?to={}&mode=bot".format(BOT_TOKEN, ROOM_ID)
420 )
421 assert result is not None
422 obj = NotifyWebexTeams(**result)
423 assert obj.mode == WebexTeamsMode.BOT
424 assert ROOM_ID in obj.targets
425
426 # Native webhook URL
427 result = NotifyWebexTeams.parse_native_url(
428 "https://api.ciscospark.com/v1/webhooks/incoming/{}".format("a" * 80)
429 )
430 assert result is not None
431 obj = NotifyWebexTeams(**result)
432 assert obj.mode == WebexTeamsMode.WEBHOOK
433
434 # New native webhook URL
435 result = NotifyWebexTeams.parse_native_url(
436 "https://webexapis.com/v1/webhooks/incoming/{}".format("a" * 100)
437 )
438 assert result is not None
439 obj = NotifyWebexTeams(**result)
440 assert obj.mode == WebexTeamsMode.WEBHOOK
441
442 # parse_native_url returns None for non-matching URLs
443 assert (
444 NotifyWebexTeams.parse_native_url(
445 "https://example.com/not/a/webex/url"
446 )
447 is None
448 )
449
450 # Webhook mode forced with ?mode=webhook even with path entries
451 result = NotifyWebexTeams.parse_url(
452 "wxteams://{}?mode=webhook".format("a" * 80)
453 )
454 assert result is not None
455 obj = NotifyWebexTeams(**result)
456 assert obj.mode == WebexTeamsMode.WEBHOOK
457
458
459 @mock.patch("requests.post")
460 def test_plugin_webex_teams_webhook_send(mock_post):
461 """NotifyWebexTeams() Webhook send() coverage."""
462
463 # Prepare a good response
464 response = mock.Mock()
465 response.status_code = requests.codes.ok
466 response.content = b""
467 mock_post.return_value = response
468
469 obj = NotifyWebexTeams(token=WEBHOOK_TOKEN)
470
471 # Successful send
472 assert obj.send(body="test message") is True
473 assert mock_post.call_count == 1
474 call_url = mock_post.call_args[0][0]
475 assert "webhooks/incoming" in call_url
476 assert WEBHOOK_TOKEN in call_url
477
478 # Verify correct Content-Type header
479 headers = mock_post.call_args[1]["headers"]
480 assert headers["Content-Type"] == "application/json"
481
482 mock_post.reset_mock()
483
484 # 204 No Content is also acceptable
485 response.status_code = requests.codes.no_content
486 assert obj.send(body="test") is True
487
488 mock_post.reset_mock()
489
490 # HTTP error
491 response.status_code = requests.codes.internal_server_error
492 assert obj.send(body="test") is False
493
494 mock_post.reset_mock()
495
496 # Unknown HTTP error code
497 response.status_code = 999
498 assert obj.send(body="test") is False
499
500 mock_post.reset_mock()
501
502 # Connection error
503 mock_post.side_effect = requests.RequestException()
504 assert obj.send(body="test") is False
505
506 mock_post.side_effect = None
507 mock_post.return_value = response
508 response.status_code = requests.codes.ok
509
510 # Webhook warns about attachments (but still proceeds)
511 mock_post.reset_mock()
512 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
513 attach = AppriseAttachment(path)
514 assert obj.send(body="test", attach=attach) is True
515 # Only one POST call: text-only webhook
516 assert mock_post.call_count == 1
517
518
519 @mock.patch("requests.post")
520 def test_plugin_webex_teams_bot_send(mock_post):
521 """NotifyWebexTeams() Bot send() text-only coverage."""
522
523 response = mock.Mock()
524 response.status_code = requests.codes.ok
525 response.content = b""
526 mock_post.return_value = response
527
528 obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID])
529
530 # Successful text send
531 assert obj.send(body="hello from bot") is True
532 assert mock_post.call_count == 1
533 call_url = mock_post.call_args[0][0]
534 assert call_url == "https://webexapis.com/v1/messages"
535
536 # Verify Bearer authorization
537 headers = mock_post.call_args[1]["headers"]
538 assert headers["Authorization"] == f"Bearer {BOT_TOKEN}"
539 assert headers["Content-Type"] == "application/json"
540
541 mock_post.reset_mock()
542
543 # Two rooms -> two calls
544 obj2 = NotifyWebexTeams(
545 access_token=BOT_TOKEN, targets=[ROOM_ID, ROOM_ID2]
546 )
547 assert obj2.send(body="broadcast") is True
548 assert mock_post.call_count == 2
549
550 mock_post.reset_mock()
551
552 # HTTP error for bot mode
553 response.status_code = requests.codes.internal_server_error
554 assert obj.send(body="test") is False
555
556 mock_post.reset_mock()
557
558 response.status_code = 999
559 assert obj.send(body="test") is False
560
561 mock_post.reset_mock()
562
563 # Connection error for bot mode
564 mock_post.side_effect = requests.RequestException()
565 assert obj.send(body="test") is False
566
567 mock_post.side_effect = None
568 response.status_code = requests.codes.ok
569 mock_post.return_value = response
570
571 # Two rooms: second fails -> overall failure reported
572 mock_post.reset_mock()
573 fail_response = mock.Mock()
574 fail_response.status_code = requests.codes.internal_server_error
575 fail_response.content = b""
576
577 mock_post.side_effect = [response, fail_response]
578 obj2 = NotifyWebexTeams(
579 access_token=BOT_TOKEN, targets=[ROOM_ID, ROOM_ID2]
580 )
581 assert obj2.send(body="test") is False
582
583
584 @mock.patch("requests.post")
585 def test_plugin_webex_teams_bot_attachments(mock_post):
586 """NotifyWebexTeams() Bot mode attachment coverage."""
587
588 response = mock.Mock()
589 response.status_code = requests.codes.ok
590 response.content = b""
591 mock_post.return_value = response
592
593 obj = NotifyWebexTeams(access_token=BOT_TOKEN, targets=[ROOM_ID])
594
595 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
596 attach = AppriseAttachment(path)
597
598 # Successful single attachment
599 mock_post.reset_mock()
600 assert (
601 obj.notify(
602 body="see attached",
603 title="",
604 notify_type=NotifyType.INFO,
605 attach=attach,
606 )
607 is True
608 )
609 # One POST call for the single attachment
610 assert mock_post.call_count == 1
611 call_url = mock_post.call_args[0][0]
612 assert call_url == "https://webexapis.com/v1/messages"
613 # No Content-Type header set (multipart sets its own)
614 headers = mock_post.call_args[1]["headers"]
615 assert "Content-Type" not in headers
616 # files kwarg was provided
617 assert mock_post.call_args[1].get("files") is not None
618
619 # Multiple attachments -> one POST per attachment
620 mock_post.reset_mock()
621 path2 = os.path.join(TEST_VAR_DIR, "apprise-test.png")
622 attach2 = AppriseAttachment([path, path2])
623 assert obj.send(body="two files", attach=attach2) is True
624 assert mock_post.call_count == 2
625
626 # Two rooms, one attachment -> 2 POST calls
627 mock_post.reset_mock()
628 obj2 = NotifyWebexTeams(
629 access_token=BOT_TOKEN, targets=[ROOM_ID, ROOM_ID2]
630 )
631 assert obj2.send(body="room broadcast", attach=attach) is True
632 assert mock_post.call_count == 2
633
634 # Connection error on attachment POST
635 mock_post.reset_mock()
636 mock_post.side_effect = requests.RequestException()
637 assert obj.send(body="test", attach=attach) is False
638
639 mock_post.side_effect = None
640 mock_post.return_value = response
641
642 # OSError reading attachment
643 mock_post.reset_mock()
644 mock_post.side_effect = OSError()
645 assert obj.send(body="test", attach=attach) is False
646
647 mock_post.side_effect = None
648 mock_post.return_value = response
649
650 # HTTP error on attachment upload
651 mock_post.reset_mock()
652 bad_response = mock.Mock()
653 bad_response.status_code = requests.codes.internal_server_error
654 bad_response.content = b""
655 mock_post.return_value = bad_response
656 assert obj.send(body="test", attach=attach) is False
657
658 mock_post.return_value = response
659
660 # OSError raised by attachment.open() (before the file handle is used)
661 mock_post.reset_mock()
662 mock_post.side_effect = None
663 with mock.patch(
664 "apprise.attachment.file.AttachFile.open",
665 side_effect=OSError("open failed"),
666 ):
667 assert obj.send(body="test", attach=attach) is False
668 mock_post.side_effect = None
669 mock_post.return_value = response
670
671 # Invalid/inaccessible attachment
672 mock_post.reset_mock()
673 invalid_path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/file.jpg")
674 bad_attach = AppriseAttachment(invalid_path)
675 assert (
676 obj.notify(
677 body="test",
678 notify_type=NotifyType.INFO,
679 attach=bad_attach,
680 )
681 is False
682 )
683 # No POST should have been made
684 assert mock_post.call_count == 0
685
686
687 @mock.patch("requests.post")
688 def test_plugin_webex_teams_apprise_integration(mock_post):
689 """NotifyWebexTeams() Apprise.notify() integration."""
690
691 response = mock.Mock()
692 response.status_code = requests.codes.ok
693 response.content = b""
694 mock_post.return_value = response
695
696 # Webhook mode via Apprise
697 app = Apprise()
698 assert app.add("wxteams://{}".format(WEBHOOK_TOKEN))
699 assert app.notify(body="webhook test") is True
700
701 mock_post.reset_mock()
702
703 # Bot mode via Apprise
704 app2 = Apprise()
705 assert app2.add("wxteams://{}/{}".format(BOT_TOKEN, ROOM_ID))
706 assert app2.notify(body="bot test") is True
707
708
709 @mock.patch("requests.post")
710 def test_plugin_webex_teams_html_to_markdown_format(mock_post):
711 """NotifyWebexTeams(): HTML body is converted to Markdown."""
712
713 # Prepare Mock
714 mock_post.return_value = requests.Request()
715 mock_post.return_value.status_code = requests.codes.ok
716
717 # Use a webhook URL (80-char token, webhook mode)
718 aobj = Apprise()
719 assert aobj.add("wxteams://{}".format("a" * 80))
720
721 # Notify with an HTML body; the framework should convert it
722 # to Markdown before dispatching to Webex Teams
723 assert (
724 aobj.notify(
725 body="<b>hello</b> <i>world</i>",
726 body_format=NotifyFormat.HTML,
727 )
728 is True
729 )
730 assert mock_post.call_count == 1
731
732 # The body must arrive as Markdown (not plain text) in the
733 # "markdown" key, since notify_format is MARKDOWN by default
734 payload = loads(mock_post.call_args_list[0][1]["data"])
735 assert payload["markdown"] == "**hello** *world*"