Debian Codegraph @

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