| 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 |
from json import loads |
| 29 |
import logging |
| 30 |
from unittest import mock |
| 31 |
|
| 32 |
import requests |
| 33 |
|
| 34 |
import apprise |
| 35 |
from apprise import LOGGER_NAME |
| 36 |
|
| 37 |
|
| 38 |
@mock.patch("requests.request") |
| 39 |
def test_apprise_interpret_escapes(mock_request): |
| 40 |
""" |
| 41 |
API: Apprise() interpret-escape tests |
| 42 |
""" |
| 43 |
|
| 44 |
# Prepare Mock |
| 45 |
mock_request.return_value = requests.Request() |
| 46 |
mock_request.return_value.status_code = requests.codes.ok |
| 47 |
|
| 48 |
# Default Escapes interpretation Mode is set to disable |
| 49 |
asset = apprise.AppriseAsset() |
| 50 |
assert asset.interpret_escapes is False |
| 51 |
|
| 52 |
# Load our asset |
| 53 |
a = apprise.Apprise(asset=asset) |
| 54 |
|
| 55 |
# add a test service |
| 56 |
assert a.add("json://localhost") is True |
| 57 |
|
| 58 |
# Our services should carry this flag |
| 59 |
assert a[0].asset.interpret_escapes is False |
| 60 |
|
| 61 |
# Send notification |
| 62 |
assert bool(a.notify("ab\\ncd")) is True |
| 63 |
|
| 64 |
# Test our call count |
| 65 |
assert mock_request.call_count == 1 |
| 66 |
details = mock_request.call_args_list[0] |
| 67 |
assert details[0][0] == "POST" |
| 68 |
|
| 69 |
# content is not escaped |
| 70 |
assert loads(details[1]["data"]).get("message", "") == "ab\\ncd" |
| 71 |
|
| 72 |
# Reset |
| 73 |
mock_request.reset_mock() |
| 74 |
|
| 75 |
# Send notification and provide override: |
| 76 |
assert bool(a.notify("ab\\ncd", interpret_escapes=True)) is True |
| 77 |
|
| 78 |
# Test our call count |
| 79 |
assert mock_request.call_count == 1 |
| 80 |
details = mock_request.call_args_list[0] |
| 81 |
assert details[0][0] == "POST" |
| 82 |
|
| 83 |
# content IS escaped |
| 84 |
assert loads(details[1]["data"]).get("message", "") == "ab\ncd" |
| 85 |
|
| 86 |
# Reset |
| 87 |
mock_request.reset_mock() |
| 88 |
|
| 89 |
# |
| 90 |
# Now we test the reverse setup where we set the AppriseAsset |
| 91 |
# object to True but force it off through the notify() calls |
| 92 |
# |
| 93 |
|
| 94 |
# Default Escapes interpretation Mode is set to disable |
| 95 |
asset = apprise.AppriseAsset(interpret_escapes=True) |
| 96 |
assert asset.interpret_escapes is True |
| 97 |
|
| 98 |
# Load our asset |
| 99 |
a = apprise.Apprise(asset=asset) |
| 100 |
|
| 101 |
# add a test service |
| 102 |
assert a.add("json://localhost") is True |
| 103 |
|
| 104 |
# Our services should carry this flag |
| 105 |
assert a[0].asset.interpret_escapes is True |
| 106 |
|
| 107 |
# Send notification |
| 108 |
assert bool(a.notify("ab\\ncd")) is True |
| 109 |
|
| 110 |
# Test our call count |
| 111 |
assert mock_request.call_count == 1 |
| 112 |
details = mock_request.call_args_list[0] |
| 113 |
assert details[0][0] == "POST" |
| 114 |
|
| 115 |
# content IS escaped |
| 116 |
assert loads(details[1]["data"]).get("message", "") == "ab\ncd" |
| 117 |
|
| 118 |
# Reset |
| 119 |
mock_request.reset_mock() |
| 120 |
|
| 121 |
# Send notification and provide override: |
| 122 |
assert bool(a.notify("ab\\ncd", interpret_escapes=False)) is True |
| 123 |
|
| 124 |
# Test our call count |
| 125 |
assert mock_request.call_count == 1 |
| 126 |
details = mock_request.call_args_list[0] |
| 127 |
assert details[0][0] == "POST" |
| 128 |
|
| 129 |
# content is NOT escaped |
| 130 |
assert loads(details[1]["data"]).get("message", "") == "ab\\ncd" |
| 131 |
|
| 132 |
|
| 133 |
@mock.patch("requests.request") |
| 134 |
def test_apprise_escaping(mock_request): |
| 135 |
""" |
| 136 |
API: Apprise() escaping tests |
| 137 |
|
| 138 |
""" |
| 139 |
a = apprise.Apprise() |
| 140 |
|
| 141 |
response = mock.Mock() |
| 142 |
response.content = "" |
| 143 |
response.status_code = requests.codes.ok |
| 144 |
mock_request.return_value = response |
| 145 |
|
| 146 |
# Create ourselves a test object to work with |
| 147 |
a.add("json://localhost") |
| 148 |
|
| 149 |
# Escape our content |
| 150 |
assert a.notify( |
| 151 |
title="\\r\\ntitle\\r\\n", |
| 152 |
body="\\r\\nbody\\r\\n", |
| 153 |
interpret_escapes=True, |
| 154 |
) |
| 155 |
|
| 156 |
# Verify our content was escaped correctly |
| 157 |
assert mock_request.call_count == 1 |
| 158 |
details = mock_request.call_args_list[0] |
| 159 |
assert details[0][0] == "POST" |
| 160 |
result = loads(details[1]["data"]) |
| 161 |
assert result["title"] == "title" |
| 162 |
assert result["message"] == "\r\nbody" |
| 163 |
|
| 164 |
# Reset our mock object |
| 165 |
mock_request.reset_mock() |
| 166 |
|
| 167 |
# |
| 168 |
# Support Specially encoded content: |
| 169 |
# |
| 170 |
|
| 171 |
# Escape our content |
| 172 |
assert a.notify( |
| 173 |
# Google Translated to Arabic: "Let's make the world a better place." |
| 174 |
title="دعونا نجعل العالم مكانا أفضل.\\r\\t\\t\\n\\r\\n", |
| 175 |
# Google Translated to Hungarian: "One line of code at a time.' |
| 176 |
body="Egy sor kódot egyszerre.\\r\\n\\r\\r\\n", |
| 177 |
# Our Escape Flag |
| 178 |
interpret_escapes=True, |
| 179 |
) |
| 180 |
|
| 181 |
# Verify our content was escaped correctly |
| 182 |
assert mock_request.call_count == 1 |
| 183 |
details = mock_request.call_args_list[0] |
| 184 |
assert details[0][0] == "POST" |
| 185 |
result = loads(details[1]["data"]) |
| 186 |
assert result["title"] == "دعونا نجعل العالم مكانا أفضل." |
| 187 |
assert result["message"] == "Egy sor kódot egyszerre." |
| 188 |
|
| 189 |
# Error handling |
| 190 |
# |
| 191 |
# We can't escape the content below |
| 192 |
assert bool(a.notify(title=None, body=4, interpret_escapes=True)) is False |
| 193 |
assert bool(a.notify(title=4, body=None, interpret_escapes=True)) is False |
| 194 |
assert ( |
| 195 |
bool(a.notify(title=object(), body=False, interpret_escapes=True)) |
| 196 |
is False |
| 197 |
) |
| 198 |
assert ( |
| 199 |
bool(a.notify(title=False, body=object(), interpret_escapes=True)) |
| 200 |
is False |
| 201 |
) |
| 202 |
|
| 203 |
# We support bytes |
| 204 |
assert ( |
| 205 |
bool( |
| 206 |
a.notify( |
| 207 |
title=b"byte title", body=b"byte body", interpret_escapes=True |
| 208 |
) |
| 209 |
) |
| 210 |
is True |
| 211 |
) |
| 212 |
|
| 213 |
# However they're escaped as 'utf-8' by default unless we tell Apprise |
| 214 |
# otherwise |
| 215 |
# Now test hebrew types (outside of default utf-8) |
| 216 |
# כותרת נפלאה translates to 'A wonderful title' |
| 217 |
# זו הודעה translates to 'This is a notification' |
| 218 |
title = "כותרת נפלאה".encode("ISO-8859-8") |
| 219 |
body = "[_[זו הודעה](http://localhost)_".encode("ISO-8859-8") |
| 220 |
assert ( |
| 221 |
bool(a.notify(title=title, body=body, interpret_escapes=True)) is False |
| 222 |
) |
| 223 |
|
| 224 |
# However if we let Apprise know in advance the encoding, it will handle |
| 225 |
# it for us |
| 226 |
asset = apprise.AppriseAsset(encoding="ISO-8859-8") |
| 227 |
a = apprise.Apprise(asset=asset) |
| 228 |
# Create ourselves a test object to work with |
| 229 |
a.add("json://localhost") |
| 230 |
assert ( |
| 231 |
bool(a.notify(title=title, body=body, interpret_escapes=True)) is True |
| 232 |
) |
| 233 |
|
| 234 |
# We'll restore our configuration back to how it was now |
| 235 |
a = apprise.Apprise() |
| 236 |
a.add("json://localhost") |
| 237 |
|
| 238 |
# The body is proessed first, so the errors thrown above get tested on |
| 239 |
# the body only. Now we run similar tests but only make the title |
| 240 |
# bad and always mark the body good |
| 241 |
assert ( |
| 242 |
bool(a.notify(title=None, body="valid", interpret_escapes=True)) |
| 243 |
is True |
| 244 |
) |
| 245 |
assert ( |
| 246 |
bool(a.notify(title=4, body="valid", interpret_escapes=True)) is False |
| 247 |
) |
| 248 |
assert ( |
| 249 |
bool(a.notify(title=object(), body="valid", interpret_escapes=True)) |
| 250 |
is False |
| 251 |
) |
| 252 |
assert ( |
| 253 |
bool(a.notify(title=False, body="valid", interpret_escapes=True)) |
| 254 |
is True |
| 255 |
) |
| 256 |
# Bytes are supported |
| 257 |
assert ( |
| 258 |
bool( |
| 259 |
a.notify(title=b"byte title", body="valid", interpret_escapes=True) |
| 260 |
) |
| 261 |
is True |
| 262 |
) |
| 263 |
|
| 264 |
|
| 265 |
def test_apprise_escaping_non_string_inputs(caplog): |
| 266 |
"""Reject non-string message bodies and titles during escape handling.""" |
| 267 |
# Some test modules disable logging globally at import time |
| 268 |
# This ensures we're correctly configured for this test. |
| 269 |
logging.disable(logging.NOTSET) |
| 270 |
caplog.set_level(logging.ERROR, logger=LOGGER_NAME) |
| 271 |
|
| 272 |
a = apprise.Apprise() |
| 273 |
|
| 274 |
response = mock.Mock() |
| 275 |
response.content = "" |
| 276 |
response.status_code = requests.codes.ok |
| 277 |
|
| 278 |
try: |
| 279 |
with mock.patch("requests.request", return_value=response): |
| 280 |
a.add("json://localhost") |
| 281 |
|
| 282 |
result = a.notify( |
| 283 |
title="valid", |
| 284 |
body=["not", "a", "string"], |
| 285 |
interpret_escapes=True, |
| 286 |
) |
| 287 |
assert bool(result) is False |
| 288 |
# Like the malformed cases above, invalid input fails before |
| 289 |
# dispatch. |
| 290 |
assert list(result) == [] |
| 291 |
assert "Failed to escape message body" in caplog.text |
| 292 |
|
| 293 |
caplog.clear() |
| 294 |
|
| 295 |
with mock.patch("requests.request", return_value=response): |
| 296 |
result = a.notify( |
| 297 |
title=["not", "a", "string"], |
| 298 |
body="valid", |
| 299 |
interpret_escapes=True, |
| 300 |
) |
| 301 |
assert bool(result) is False |
| 302 |
assert list(result) == [] |
| 303 |
# Title escaping reports the same user-facing error as body |
| 304 |
# escaping. |
| 305 |
assert "Failed to escape message body" in caplog.text |
| 306 |
|
| 307 |
finally: |
| 308 |
logging.disable(logging.CRITICAL) |