| 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.exception import AppriseImproperlyConfigured |
| 38 |
from apprise.plugins.dingtalk import NotifyDingTalk |
| 39 |
|
| 40 |
logging.disable(logging.CRITICAL) |
| 41 |
|
| 42 |
# Our Testing URLs |
| 43 |
apprise_url_tests = ( |
| 44 |
( |
| 45 |
"dingtalk://", |
| 46 |
{ |
| 47 |
# No Access Token specified |
| 48 |
"instance": AppriseImproperlyConfigured, |
| 49 |
}, |
| 50 |
), |
| 51 |
( |
| 52 |
"dingtalk://a_bd_/", |
| 53 |
{ |
| 54 |
# invalid Access Token |
| 55 |
"instance": AppriseImproperlyConfigured, |
| 56 |
}, |
| 57 |
), |
| 58 |
( |
| 59 |
"dingtalk://12345678", |
| 60 |
{ |
| 61 |
# access token |
| 62 |
"instance": NotifyDingTalk, |
| 63 |
# Our expected url(privacy=True) startswith() response: |
| 64 |
"privacy_url": "dingtalk://1...8", |
| 65 |
}, |
| 66 |
), |
| 67 |
( |
| 68 |
"dingtalk://{}/{}".format("a" * 8, "1" * 14), |
| 69 |
{ |
| 70 |
# access token + phone number |
| 71 |
"instance": NotifyDingTalk, |
| 72 |
}, |
| 73 |
), |
| 74 |
( |
| 75 |
"dingtalk://{}/{}/invalid".format("a" * 8, "1" * 3), |
| 76 |
{ |
| 77 |
# access token + 2 invalid phone numbers |
| 78 |
"instance": NotifyDingTalk, |
| 79 |
}, |
| 80 |
), |
| 81 |
( |
| 82 |
"dingtalk://{}/?to={}".format("a" * 8, "1" * 14), |
| 83 |
{ |
| 84 |
# access token + phone number using 'to' |
| 85 |
"instance": NotifyDingTalk, |
| 86 |
}, |
| 87 |
), |
| 88 |
# Test secret via user@ |
| 89 |
( |
| 90 |
"dingtalk://secret@{}/?to={}".format("a" * 8, "1" * 14), |
| 91 |
{ |
| 92 |
# access token + phone number using 'to' |
| 93 |
"instance": NotifyDingTalk, |
| 94 |
# Our expected url(privacy=True) startswith() response: |
| 95 |
"privacy_url": "dingtalk://****@a...a", |
| 96 |
}, |
| 97 |
), |
| 98 |
# Test secret via secret= and token= |
| 99 |
( |
| 100 |
"dingtalk://?token={}&to={}&secret={}".format( |
| 101 |
"b" * 8, "1" * 14, "a" * 15 |
| 102 |
), |
| 103 |
{ |
| 104 |
# access token + phone number using 'to' |
| 105 |
"instance": NotifyDingTalk, |
| 106 |
"privacy_url": "dingtalk://****@b...b", |
| 107 |
}, |
| 108 |
), |
| 109 |
# Invalid secret |
| 110 |
( |
| 111 |
"dingtalk://{}/?to={}&secret=_".format("a" * 8, "1" * 14), |
| 112 |
{ |
| 113 |
"instance": AppriseImproperlyConfigured, |
| 114 |
}, |
| 115 |
), |
| 116 |
( |
| 117 |
"dingtalk://{}?format=markdown".format("a" * 8), |
| 118 |
{ |
| 119 |
# access token |
| 120 |
"instance": NotifyDingTalk, |
| 121 |
}, |
| 122 |
), |
| 123 |
( |
| 124 |
"dingtalk://{}".format("a" * 8), |
| 125 |
{ |
| 126 |
"instance": NotifyDingTalk, |
| 127 |
# throw a bizarre code forcing us to fail to look it up |
| 128 |
"response": False, |
| 129 |
"requests_response_code": 999, |
| 130 |
}, |
| 131 |
), |
| 132 |
( |
| 133 |
"dingtalk://{}".format("a" * 8), |
| 134 |
{ |
| 135 |
"instance": NotifyDingTalk, |
| 136 |
# Throws a series of i/o exceptions with this flag |
| 137 |
# is set and tests that we gracefully handle them |
| 138 |
"test_requests_exceptions": True, |
| 139 |
}, |
| 140 |
), |
| 141 |
) |
| 142 |
|
| 143 |
|
| 144 |
def test_plugin_dingtalk_urls(): |
| 145 |
"""NotifyDingTalk() Apprise URLs.""" |
| 146 |
|
| 147 |
# Run our general tests |
| 148 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 149 |
|
| 150 |
|
| 151 |
@mock.patch("requests.post") |
| 152 |
def test_plugin_dingtalk_html_to_markdown_format(mock_post): |
| 153 |
"""NotifyDingTalk(): HTML body is converted to Markdown.""" |
| 154 |
|
| 155 |
# Prepare Mock |
| 156 |
mock_post.return_value = requests.Request() |
| 157 |
mock_post.return_value.status_code = requests.codes.ok |
| 158 |
|
| 159 |
# Load via Apprise URL with markdown format enabled |
| 160 |
aobj = Apprise() |
| 161 |
assert aobj.add("dingtalk://{}?format=markdown".format("a" * 8)) |
| 162 |
|
| 163 |
# Notify with an HTML body; the framework converts it to Markdown |
| 164 |
# before dispatching to the DingTalk plugin |
| 165 |
assert ( |
| 166 |
bool( |
| 167 |
aobj.notify( |
| 168 |
body="<b>hello</b> <i>world</i>", |
| 169 |
body_format=NotifyFormat.HTML, |
| 170 |
) |
| 171 |
) |
| 172 |
is True |
| 173 |
) |
| 174 |
assert mock_post.call_count == 1 |
| 175 |
|
| 176 |
# The body must arrive as Markdown, not stripped plain text |
| 177 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 178 |
assert payload["markdown"]["text"] == "**hello** *world*" |
| 179 |
|
| 180 |
|
| 181 |
@mock.patch("requests.post") |
| 182 |
def test_plugin_dingtalk_msgtype(mock_post): |
| 183 |
"""NotifyDingTalk(): msgtype tracks the notification format.""" |
| 184 |
|
| 185 |
# Prepare Mock |
| 186 |
mock_post.return_value = requests.Request() |
| 187 |
mock_post.return_value.status_code = requests.codes.ok |
| 188 |
|
| 189 |
# Text is our default format |
| 190 |
aobj = Apprise() |
| 191 |
assert aobj.add("dingtalk://{}".format("a" * 8)) |
| 192 |
assert bool(aobj.notify(title="title", body="body")) is True |
| 193 |
assert mock_post.call_count == 1 |
| 194 |
|
| 195 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 196 |
assert payload["msgtype"] == "text" |
| 197 |
assert payload["text"]["content"] == "title\r\nbody" |
| 198 |
assert "markdown" not in payload |
| 199 |
|
| 200 |
mock_post.reset_mock() |
| 201 |
|
| 202 |
# Markdown must flip the msgtype over as well |
| 203 |
aobj = Apprise() |
| 204 |
assert aobj.add("dingtalk://{}?format=markdown".format("a" * 8)) |
| 205 |
assert bool(aobj.notify(title="title", body="body")) is True |
| 206 |
assert mock_post.call_count == 1 |
| 207 |
|
| 208 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 209 |
assert payload["msgtype"] == "markdown" |
| 210 |
assert payload["markdown"]["title"] == "title" |
| 211 |
assert payload["markdown"]["text"] == "body" |
| 212 |
assert "text" not in payload |
| 213 |
|
| 214 |
|
| 215 |
@mock.patch("requests.post") |
| 216 |
def test_plugin_dingtalk_markdown_no_title(mock_post): |
| 217 |
"""NotifyDingTalk(): a markdown message always carries a title.""" |
| 218 |
|
| 219 |
# Prepare Mock |
| 220 |
mock_post.return_value = requests.Request() |
| 221 |
mock_post.return_value.status_code = requests.codes.ok |
| 222 |
|
| 223 |
# DingTalk rejects a markdown message with an empty title, so we |
| 224 |
# substitute our application description when none was provided |
| 225 |
obj = Apprise.instantiate("dingtalk://{}?format=markdown".format("a" * 8)) |
| 226 |
assert obj.notify(body="body") is True |
| 227 |
assert mock_post.call_count == 1 |
| 228 |
|
| 229 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 230 |
assert payload["markdown"]["title"] == obj.app_desc |
| 231 |
|
| 232 |
|
| 233 |
@mock.patch("requests.post") |
| 234 |
def test_plugin_dingtalk_multi_format(mock_post): |
| 235 |
"""NotifyDingTalk(): markdown input aligns without an override.""" |
| 236 |
|
| 237 |
# Prepare Mock |
| 238 |
mock_post.return_value = requests.Request() |
| 239 |
mock_post.return_value.status_code = requests.codes.ok |
| 240 |
|
| 241 |
# DingTalk declares both text and markdown, so a caller supplying a |
| 242 |
# Markdown body is delivered as markdown with no ?format= needed |
| 243 |
aobj = Apprise() |
| 244 |
assert aobj.add("dingtalk://{}".format("a" * 8)) |
| 245 |
assert ( |
| 246 |
bool( |
| 247 |
aobj.notify( |
| 248 |
body="**hello** *world*", |
| 249 |
body_format=NotifyFormat.MARKDOWN, |
| 250 |
) |
| 251 |
) |
| 252 |
is True |
| 253 |
) |
| 254 |
assert mock_post.call_count == 1 |
| 255 |
|
| 256 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 257 |
assert payload["msgtype"] == "markdown" |
| 258 |
assert payload["markdown"]["text"] == "**hello** *world*" |
| 259 |
|
| 260 |
mock_post.reset_mock() |
| 261 |
|
| 262 |
# A plain text body still arrives as text |
| 263 |
aobj = Apprise() |
| 264 |
assert aobj.add("dingtalk://{}".format("a" * 8)) |
| 265 |
assert ( |
| 266 |
bool(aobj.notify(body="hello world", body_format=NotifyFormat.TEXT)) |
| 267 |
is True |
| 268 |
) |
| 269 |
assert mock_post.call_count == 1 |
| 270 |
|
| 271 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 272 |
assert payload["msgtype"] == "text" |
| 273 |
assert payload["text"]["content"] == "hello world" |
| 274 |
|
| 275 |
mock_post.reset_mock() |
| 276 |
|
| 277 |
# An explicit ?format=text override wins over a Markdown body |
| 278 |
aobj = Apprise() |
| 279 |
assert aobj.add("dingtalk://{}?format=text".format("a" * 8)) |
| 280 |
assert ( |
| 281 |
bool( |
| 282 |
aobj.notify( |
| 283 |
body="**hello** *world*", |
| 284 |
body_format=NotifyFormat.MARKDOWN, |
| 285 |
) |
| 286 |
) |
| 287 |
is True |
| 288 |
) |
| 289 |
assert mock_post.call_count == 1 |
| 290 |
|
| 291 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 292 |
assert payload["msgtype"] == "text" |
| 293 |
|
| 294 |
|
| 295 |
def test_plugin_dingtalk_title_maxlen(): |
| 296 |
"""NotifyDingTalk(): a title field only exists in markdown mode.""" |
| 297 |
|
| 298 |
# Text is our default, so the framework folds the title into the body |
| 299 |
obj = Apprise.instantiate("dingtalk://{}".format("a" * 8)) |
| 300 |
assert obj.title_maxlen == 0 |
| 301 |
|
| 302 |
# Markdown carries its own title field |
| 303 |
obj = Apprise.instantiate("dingtalk://{}?format=markdown".format("a" * 8)) |
| 304 |
assert obj.title_maxlen > 0 |
| 305 |
|
| 306 |
|
| 307 |
def test_plugin_dingtalk_url_format_round_trip(): |
| 308 |
"""NotifyDingTalk(): an explicit format= survives a round trip.""" |
| 309 |
|
| 310 |
# Without an override, format= is left off so the URL stays flexible |
| 311 |
obj = Apprise.instantiate("dingtalk://{}".format("a" * 8)) |
| 312 |
assert "format=" not in obj.url() |
| 313 |
|
| 314 |
# An explicit override is always preserved |
| 315 |
obj = Apprise.instantiate("dingtalk://{}?format=markdown".format("a" * 8)) |
| 316 |
assert "format=markdown" in obj.url() |
| 317 |
|
| 318 |
# Re-loading the generated URL gives us the same object back |
| 319 |
obj2 = Apprise.instantiate(obj.url()) |
| 320 |
assert obj2.url_identifier == obj.url_identifier |
| 321 |
assert obj2.title_maxlen == obj.title_maxlen |