Debian Codegraph @

tests/test_plugin_pushsafer.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 dumps
29
30 # Disable logging for a cleaner testing output
31 import logging
32 import os
33 from unittest import mock
34
35 from helpers import AppriseURLTester
36 import pytest
37 import requests
38
39 from apprise import AppriseAttachment, NotifyType
40 from apprise.plugins.pushsafer import NotifyPushSafer
41
42 logging.disable(logging.CRITICAL)
43
44 # Attachment Directory
45 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
46
47 # Our Testing URLs
48 apprise_url_tests = (
49 (
50 "psafer://:@/",
51 {
52 "instance": TypeError,
53 },
54 ),
55 (
56 "psafer://",
57 {
58 "instance": TypeError,
59 },
60 ),
61 (
62 "psafers://",
63 {
64 "instance": TypeError,
65 },
66 ),
67 (
68 "psafer://{}".format("a" * 20),
69 {
70 "instance": NotifyPushSafer,
71 # This will fail because we're also expecting a server
72 # acknowledgement
73 "notify_response": False,
74 },
75 ),
76 (
77 "psafer://{}".format("b" * 20),
78 {
79 "instance": NotifyPushSafer,
80 # invalid JSON response
81 "requests_response_text": "{",
82 "notify_response": False,
83 },
84 ),
85 (
86 "psafer://{}".format("c" * 20),
87 {
88 "instance": NotifyPushSafer,
89 # A failure has status set to zero
90 # We also expect an 'error' flag to be set
91 "requests_response_text": {"status": 0, "error": "we failed"},
92 "notify_response": False,
93 },
94 ),
95 (
96 "psafers://{}".format("d" * 20),
97 {
98 "instance": NotifyPushSafer,
99 # A failure has status set to zero
100 # Test without an 'error' flag
101 "requests_response_text": {
102 "status": 0,
103 },
104 "notify_response": False,
105 },
106 ),
107 # This will notify all users ('a')
108 (
109 "psafer://{}".format("e" * 20),
110 {
111 "instance": NotifyPushSafer,
112 # A status of 1 is a success
113 "requests_response_text": {
114 "status": 1,
115 },
116 },
117 ),
118 # This will notify a selected set of devices
119 (
120 "psafer://{}/12/24/53".format("e" * 20),
121 {
122 "instance": NotifyPushSafer,
123 # A status of 1 is a success
124 "requests_response_text": {
125 "status": 1,
126 },
127 },
128 ),
129 # Same as above, but exercises the to= argument
130 (
131 "psafer://{}?to=12,24,53".format("e" * 20),
132 {
133 "instance": NotifyPushSafer,
134 # A status of 1 is a success
135 "requests_response_text": {
136 "status": 1,
137 },
138 },
139 ),
140 # Set priority
141 (
142 "psafer://{}?priority=emergency".format("f" * 20),
143 {
144 "instance": NotifyPushSafer,
145 "requests_response_text": {
146 "status": 1,
147 },
148 },
149 ),
150 # Support integer value too
151 (
152 "psafer://{}?priority=-1".format("f" * 20),
153 {
154 "instance": NotifyPushSafer,
155 "requests_response_text": {
156 "status": 1,
157 },
158 "force_debug": True,
159 },
160 ),
161 # Invalid priority
162 (
163 "psafer://{}?priority=invalid".format("f" * 20),
164 {
165 # Invalid Priority
166 "instance": TypeError,
167 },
168 ),
169 # Invalid priority
170 (
171 "psafer://{}?priority=25".format("f" * 20),
172 {
173 # Invalid Priority
174 "instance": TypeError,
175 },
176 ),
177 # Set sound
178 (
179 "psafer://{}?sound=ok".format("g" * 20),
180 {
181 "instance": NotifyPushSafer,
182 "requests_response_text": {
183 "status": 1,
184 },
185 },
186 ),
187 # Support integer value too
188 (
189 "psafers://{}?sound=14".format("g" * 20),
190 {
191 "instance": NotifyPushSafer,
192 "requests_response_text": {
193 "status": 1,
194 },
195 "privacy_url": "psafers://g...g",
196 },
197 ),
198 # Invalid sound
199 (
200 "psafer://{}?sound=invalid".format("h" * 20),
201 {
202 # Invalid Sound
203 "instance": TypeError,
204 },
205 ),
206 (
207 "psafer://{}?sound=94000".format("h" * 20),
208 {
209 # Invalid Sound
210 "instance": TypeError,
211 },
212 ),
213 # Set vibration (integer only)
214 (
215 "psafers://{}?vibration=1".format("h" * 20),
216 {
217 "instance": NotifyPushSafer,
218 "requests_response_text": {
219 "status": 1,
220 },
221 "privacy_url": "psafers://h...h",
222 },
223 ),
224 # Invalid sound
225 (
226 "psafer://{}?vibration=invalid".format("h" * 20),
227 {
228 # Invalid Vibration
229 "instance": TypeError,
230 },
231 ),
232 # Invalid vibration
233 (
234 "psafer://{}?vibration=25000".format("h" * 20),
235 {
236 # Invalid Vibration
237 "instance": TypeError,
238 },
239 ),
240 (
241 "psafers://{}".format("d" * 20),
242 {
243 "instance": NotifyPushSafer,
244 # A failure has status set to zero
245 # Test without an 'error' flag
246 "requests_response_text": {
247 "status": 0,
248 },
249 # force a failure
250 "response": False,
251 "requests_response_code": requests.codes.internal_server_error,
252 },
253 ),
254 (
255 "psafer://{}".format("d" * 20),
256 {
257 "instance": NotifyPushSafer,
258 # A failure has status set to zero
259 # Test without an 'error' flag
260 "requests_response_text": {
261 "status": 0,
262 },
263 # throw a bizarre code forcing us to fail to look it up
264 "response": False,
265 "requests_response_code": 999,
266 },
267 ),
268 (
269 "psafers://{}".format("d" * 20),
270 {
271 "instance": NotifyPushSafer,
272 # A failure has status set to zero
273 # Test without an 'error' flag
274 "requests_response_text": {
275 "status": 0,
276 },
277 # Throws a series of connection and transfer exceptions when this
278 # flag is set and tests that we gracefully handle them
279 "test_requests_exceptions": True,
280 },
281 ),
282 )
283
284
285 def test_plugin_pushsafer_urls():
286 """NotifyPushSafer() Apprise URLs."""
287
288 # Run our general tests
289 AppriseURLTester(tests=apprise_url_tests).run_all()
290
291
292 @mock.patch("requests.post")
293 def test_plugin_pushsafer_general(mock_post):
294 """NotifyPushSafer() General Tests."""
295
296 # Private Key
297 privatekey = "abc123"
298
299 # Prepare Mock
300 mock_post.return_value = requests.Request()
301 mock_post.return_value.status_code = requests.codes.ok
302 mock_post.return_value.content = dumps(
303 {
304 "status": 1,
305 "success": "okay",
306 }
307 )
308
309 # Exception should be thrown about the fact no private key was specified
310 with pytest.raises(TypeError):
311 NotifyPushSafer(privatekey=None)
312
313 # Multiple Attachment Support
314 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
315 attach = AppriseAttachment()
316 for _ in range(0, 4):
317 attach.add(path)
318
319 obj = NotifyPushSafer(privatekey=privatekey)
320 assert (
321 obj.notify(
322 body="body",
323 title="title",
324 notify_type=NotifyType.INFO,
325 attach=attach,
326 )
327 is True
328 )
329
330 # Test error reading attachment from disk
331 with mock.patch("builtins.open", side_effect=OSError):
332 obj.notify(
333 body="body",
334 title="title",
335 notify_type=NotifyType.INFO,
336 attach=attach,
337 )
338
339 # Test unsupported mime type
340 attach = AppriseAttachment(path)
341
342 attach[0]._mimetype = "application/octet-stream"
343
344 # We gracefully just don't send the attachment in these cases;
345 # The notify itself will still be successful
346 mock_post.reset_mock()
347 assert (
348 obj.notify(
349 body="body",
350 title="title",
351 notify_type=NotifyType.INFO,
352 attach=attach,
353 )
354 is True
355 )
356
357 # the 'p', 'p2', and 'p3' are the data variables used when including an
358 # image.
359 assert "data" in mock_post.call_args[1]
360 assert "p" not in mock_post.call_args[1]["data"]
361 assert "p2" not in mock_post.call_args[1]["data"]
362 assert "p3" not in mock_post.call_args[1]["data"]
363
364 # Invalid file path
365 path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg")
366 assert (
367 obj.notify(
368 body="body",
369 title="title",
370 notify_type=NotifyType.INFO,
371 attach=path,
372 )
373 is False
374 )