Debian Codegraph @

tests/test_plugin_dingtalk.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 requests
35
36 from apprise import Apprise, NotifyFormat
37 from apprise.plugins.dingtalk import NotifyDingTalk
38
39 logging.disable(logging.CRITICAL)
40
41 # Our Testing URLs
42 apprise_url_tests = (
43 (
44 "dingtalk://",
45 {
46 # No Access Token specified
47 "instance": TypeError,
48 },
49 ),
50 (
51 "dingtalk://a_bd_/",
52 {
53 # invalid Access Token
54 "instance": TypeError,
55 },
56 ),
57 (
58 "dingtalk://12345678",
59 {
60 # access token
61 "instance": NotifyDingTalk,
62 # Our expected url(privacy=True) startswith() response:
63 "privacy_url": "dingtalk://1...8",
64 },
65 ),
66 (
67 "dingtalk://{}/{}".format("a" * 8, "1" * 14),
68 {
69 # access token + phone number
70 "instance": NotifyDingTalk,
71 },
72 ),
73 (
74 "dingtalk://{}/{}/invalid".format("a" * 8, "1" * 3),
75 {
76 # access token + 2 invalid phone numbers
77 "instance": NotifyDingTalk,
78 },
79 ),
80 (
81 "dingtalk://{}/?to={}".format("a" * 8, "1" * 14),
82 {
83 # access token + phone number using 'to'
84 "instance": NotifyDingTalk,
85 },
86 ),
87 # Test secret via user@
88 (
89 "dingtalk://secret@{}/?to={}".format("a" * 8, "1" * 14),
90 {
91 # access token + phone number using 'to'
92 "instance": NotifyDingTalk,
93 # Our expected url(privacy=True) startswith() response:
94 "privacy_url": "dingtalk://****@a...a",
95 },
96 ),
97 # Test secret via secret= and token=
98 (
99 "dingtalk://?token={}&to={}&secret={}".format(
100 "b" * 8, "1" * 14, "a" * 15
101 ),
102 {
103 # access token + phone number using 'to'
104 "instance": NotifyDingTalk,
105 "privacy_url": "dingtalk://****@b...b",
106 },
107 ),
108 # Invalid secret
109 (
110 "dingtalk://{}/?to={}&secret=_".format("a" * 8, "1" * 14),
111 {
112 "instance": TypeError,
113 },
114 ),
115 (
116 "dingtalk://{}?format=markdown".format("a" * 8),
117 {
118 # access token
119 "instance": NotifyDingTalk,
120 },
121 ),
122 (
123 "dingtalk://{}".format("a" * 8),
124 {
125 "instance": NotifyDingTalk,
126 # throw a bizarre code forcing us to fail to look it up
127 "response": False,
128 "requests_response_code": 999,
129 },
130 ),
131 (
132 "dingtalk://{}".format("a" * 8),
133 {
134 "instance": NotifyDingTalk,
135 # Throws a series of i/o exceptions with this flag
136 # is set and tests that we gracefully handle them
137 "test_requests_exceptions": True,
138 },
139 ),
140 )
141
142
143 def test_plugin_dingtalk_urls():
144 """NotifyDingTalk() Apprise URLs."""
145
146 # Run our general tests
147 AppriseURLTester(tests=apprise_url_tests).run_all()
148
149
150 @mock.patch("requests.post")
151 def test_plugin_dingtalk_html_to_markdown_format(mock_post):
152 """NotifyDingTalk(): HTML body is converted to Markdown."""
153
154 # Prepare Mock
155 mock_post.return_value = requests.Request()
156 mock_post.return_value.status_code = requests.codes.ok
157
158 # Load via Apprise URL with markdown format enabled
159 aobj = Apprise()
160 assert aobj.add("dingtalk://{}?format=markdown".format("a" * 8))
161
162 # Notify with an HTML body; the framework converts it to Markdown
163 # before dispatching to the DingTalk plugin
164 assert (
165 aobj.notify(
166 body="<b>hello</b> <i>world</i>",
167 body_format=NotifyFormat.HTML,
168 )
169 is True
170 )
171 assert mock_post.call_count == 1
172
173 # The body must arrive as Markdown, not stripped plain text
174 payload = loads(mock_post.call_args_list[0][1]["data"])
175 assert payload["markdown"]["text"] == "**hello** *world*"