Debian Codegraph @

tests/test_plugin_pushsafer.py

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