Debian Codegraph @

tests/test_plugin_pushward.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
30 # Disable logging for a cleaner testing output
31 import logging
32 from unittest import mock
33
34 from helpers import AppriseURLTester
35 import pytest
36 import requests
37
38 from apprise import Apprise, AppriseAsset, NotifyType
39 from apprise.plugins.pushward import NotifyPushWard, pushward_level
40
41 logging.disable(logging.CRITICAL)
42
43 # Our Testing URLs
44 apprise_url_tests = (
45 (
46 "pushward://",
47 {
48 # No API Key specified
49 "instance": TypeError,
50 },
51 ),
52 (
53 "pushward://invalid",
54 {
55 # API Key does not match the hlk_ pattern
56 "instance": TypeError,
57 },
58 ),
59 (
60 "pushward://hlk_abc123",
61 {
62 # A valid API Key
63 "instance": NotifyPushWard,
64 # Our expected url(privacy=True) startswith() response:
65 "privacy_url": "pushward://h...3/",
66 },
67 ),
68 (
69 "pushward://?apikey=hlk_abc123",
70 {
71 # API Key provided as a query argument
72 "instance": NotifyPushWard,
73 "privacy_url": "pushward://h...3/",
74 },
75 ),
76 (
77 "pushward://hlk_abc123?level=critical&volume=0.8",
78 {
79 # critical level with a volume
80 "instance": NotifyPushWard,
81 "privacy_url": "pushward://h...3/",
82 },
83 ),
84 (
85 "pushward://hlk_abc123?info=passive&failure=critical",
86 {
87 # per-notification-type level overrides
88 "instance": NotifyPushWard,
89 "privacy_url": "pushward://h...3/",
90 },
91 ),
92 (
93 "pushward://hlk_abc123?info=bogus",
94 {
95 # An invalid per-type level
96 "instance": TypeError,
97 },
98 ),
99 (
100 "pushward://hlk_abc123?level=invalid",
101 {
102 # Invalid level provided
103 "instance": TypeError,
104 },
105 ),
106 (
107 "pushward://hlk_abc123?volume=2.0",
108 {
109 # Volume out of range
110 "instance": TypeError,
111 },
112 ),
113 (
114 "pushward://hlk_abc123?volume=invalid",
115 {
116 # Volume that cannot be parsed as a float
117 "instance": TypeError,
118 },
119 ),
120 (
121 "pushward://hlk_abc123",
122 {
123 "instance": NotifyPushWard,
124 # Force a failure with a known response code
125 "response": False,
126 "requests_response_code": requests.codes.internal_server_error,
127 },
128 ),
129 (
130 "pushward://hlk_abc123",
131 {
132 "instance": NotifyPushWard,
133 # Throw a bizarre code forcing us to fail to look it up
134 "response": False,
135 "requests_response_code": 999,
136 },
137 ),
138 (
139 "pushward://hlk_abc123",
140 {
141 "instance": NotifyPushWard,
142 # Drive it through a series of socket exceptions
143 "test_requests_exceptions": True,
144 },
145 ),
146 )
147
148
149 def test_plugin_pushward_urls():
150 """NotifyPushWard() Apprise URLs."""
151
152 # Run our general tests
153 AppriseURLTester(tests=apprise_url_tests).run_all()
154
155
156 def test_plugin_pushward_edge_cases():
157 """NotifyPushWard() Edge Cases."""
158
159 # No API Key
160 with pytest.raises(TypeError):
161 NotifyPushWard(apikey=None)
162
163 # Whitespace only API Key
164 with pytest.raises(TypeError):
165 NotifyPushWard(apikey=" ")
166
167 # API Key that does not match the hlk_ pattern
168 with pytest.raises(TypeError):
169 NotifyPushWard(apikey="invalid")
170
171 # Invalid level
172 with pytest.raises(TypeError):
173 NotifyPushWard(apikey="hlk_abc123", level="invalid")
174
175 # Volume above the allowable range
176 with pytest.raises(TypeError):
177 NotifyPushWard(apikey="hlk_abc123", volume=2.0)
178
179 # Volume below the allowable range
180 with pytest.raises(TypeError):
181 NotifyPushWard(apikey="hlk_abc123", volume=-0.5)
182
183 # Volume that cannot be coerced to a float
184 with pytest.raises(TypeError):
185 NotifyPushWard(apikey="hlk_abc123", volume="invalid")
186
187
188 @mock.patch("requests.post")
189 def test_plugin_pushward_send(mock_post):
190 """NotifyPushWard() a successful (200) send."""
191
192 response = mock.Mock()
193 response.status_code = requests.codes.ok
194 response.content = b""
195 mock_post.return_value = response
196
197 obj = Apprise.instantiate("pushward://hlk_abc123")
198 assert isinstance(obj, NotifyPushWard)
199 assert obj.notify(title="title", body="body") is True
200
201 assert mock_post.call_count == 1
202 details = mock_post.call_args
203 assert details[0][0] == "https://api.pushward.app/notifications"
204
205 headers = details[1]["headers"]
206 assert headers["Authorization"] == "Bearer hlk_abc123"
207 assert headers["User-Agent"] == obj.app_id
208
209 payload = loads(details[1]["data"])
210 assert payload["title"] == "title"
211 assert payload["body"] == "body"
212 # NotifyType.INFO maps to the "active" level
213 assert payload["level"] == "active"
214 # An icon is attached (the default asset provides an image mask)
215 assert "icon_url" in payload
216 # No volume is sent for a non-critical notification
217 assert "volume" not in payload
218
219
220 @mock.patch("requests.post")
221 def test_plugin_pushward_created(mock_post):
222 """NotifyPushWard() treats a 201 Created response as success."""
223
224 response = mock.Mock()
225 response.status_code = requests.codes.created
226 response.content = b""
227 mock_post.return_value = response
228
229 obj = Apprise.instantiate("pushward://hlk_abc123")
230 assert obj.notify(title="title", body="body") is True
231
232
233 @mock.patch("requests.post")
234 def test_plugin_pushward_level_override(mock_post):
235 """NotifyPushWard() honors an explicit ?level= override."""
236
237 response = mock.Mock()
238 response.status_code = requests.codes.ok
239 response.content = b""
240 mock_post.return_value = response
241
242 obj = Apprise.instantiate("pushward://hlk_abc123?level=passive")
243 assert isinstance(obj, NotifyPushWard)
244 assert obj.level == "passive"
245 assert obj.notify(title="t", body="b") is True
246
247 payload = loads(mock_post.call_args[1]["data"])
248 assert payload["level"] == "passive"
249
250
251 @mock.patch("requests.post")
252 def test_plugin_pushward_notify_type_mapping(mock_post):
253 """NotifyPushWard() derives the level from notify_type when unset."""
254
255 response = mock.Mock()
256 response.status_code = requests.codes.ok
257 response.content = b""
258 mock_post.return_value = response
259
260 obj = Apprise.instantiate("pushward://hlk_abc123")
261 assert (
262 obj.notify(title="t", body="b", notify_type=NotifyType.WARNING) is True
263 )
264
265 payload = loads(mock_post.call_args[1]["data"])
266 # WARNING maps to the "time-sensitive" level
267 assert payload["level"] == "time-sensitive"
268
269
270 @mock.patch("requests.post")
271 def test_plugin_pushward_critical_volume(mock_post):
272 """NotifyPushWard() sends a volume only for critical notifications."""
273
274 response = mock.Mock()
275 response.status_code = requests.codes.ok
276 response.content = b""
277 mock_post.return_value = response
278
279 obj = Apprise.instantiate(
280 "pushward://hlk_abc123?level=critical&volume=0.8"
281 )
282 assert isinstance(obj, NotifyPushWard)
283 assert obj.level == "critical"
284 assert obj.volume == 0.8
285 assert obj.notify(title="t", body="b") is True
286
287 payload = loads(mock_post.call_args[1]["data"])
288 assert payload["level"] == "critical"
289 assert payload["volume"] == 0.8
290
291 # The volume is dropped when the level is not critical
292 obj = Apprise.instantiate("pushward://hlk_abc123?level=active&volume=0.5")
293 assert obj.notify(title="t", body="b") is True
294 payload = loads(mock_post.call_args[1]["data"])
295 assert "volume" not in payload
296
297
298 @mock.patch("requests.post")
299 def test_plugin_pushward_no_image(mock_post):
300 """NotifyPushWard() omits icon_url when no image is available."""
301
302 response = mock.Mock()
303 response.status_code = requests.codes.ok
304 response.content = b""
305 mock_post.return_value = response
306
307 asset = AppriseAsset(image_url_mask=None)
308 obj = Apprise.instantiate("pushward://hlk_abc123", asset=asset)
309 assert obj.notify(title="t", body="b") is True
310
311 payload = loads(mock_post.call_args[1]["data"])
312 assert "icon_url" not in payload
313
314
315 @mock.patch("requests.post")
316 def test_plugin_pushward_empty_title(mock_post):
317 """NotifyPushWard() falls back to a default when the title is empty."""
318
319 response = mock.Mock()
320 response.status_code = requests.codes.ok
321 response.content = b""
322 mock_post.return_value = response
323
324 # No title provided; falls back to the asset descriptor
325 obj = Apprise.instantiate("pushward://hlk_abc123")
326 assert obj.notify(body="body only") is True
327
328 payload = loads(mock_post.call_args[1]["data"])
329 assert payload["title"]
330 assert payload["title"] == obj.app_desc
331 assert payload["body"] == "body only"
332
333 # When the descriptor is also empty, fall back to the application id
334 asset = AppriseAsset(app_desc="")
335 obj = Apprise.instantiate("pushward://hlk_abc123", asset=asset)
336 assert obj.notify(body="body only") is True
337
338 payload = loads(mock_post.call_args[1]["data"])
339 assert payload["title"] == obj.app_id
340
341
342 @mock.patch("requests.post")
343 def test_plugin_pushward_url_roundtrip(mock_post):
344 """NotifyPushWard() url() round-trips losslessly."""
345
346 response = mock.Mock()
347 response.status_code = requests.codes.ok
348 response.content = b""
349 mock_post.return_value = response
350
351 obj = Apprise.instantiate(
352 "pushward://hlk_abc123?level=critical&volume=0.8"
353 )
354 assert isinstance(obj, NotifyPushWard)
355
356 # Regenerate the URL and re-instantiate
357 obj2 = Apprise.instantiate(obj.url())
358 assert isinstance(obj2, NotifyPushWard)
359 assert obj.url_identifier == obj2.url_identifier
360 assert obj2.apikey == "hlk_abc123"
361 assert obj2.level == "critical"
362 assert obj2.volume == 0.8
363
364
365 def test_plugin_pushward_parse_url():
366 """NotifyPushWard() parse_url() behavior."""
367
368 # An unparseable (non-string) URL returns None
369 assert NotifyPushWard.parse_url(None) is None
370
371 # API Key carried in the host
372 results = NotifyPushWard.parse_url("pushward://hlk_abc123")
373 assert results["apikey"] == "hlk_abc123"
374
375 # API Key carried in a query argument (overrides the host)
376 results = NotifyPushWard.parse_url("pushward://?apikey=hlk_xyz")
377 assert results["apikey"] == "hlk_xyz"
378
379 # level + volume carried in the query
380 results = NotifyPushWard.parse_url(
381 "pushward://hlk_abc123?level=critical&volume=0.5"
382 )
383 assert results["apikey"] == "hlk_abc123"
384 assert results["level"] == "critical"
385 assert results["volume"] == "0.5"
386
387 # Case is preserved on the key (hlk_ keys are case-sensitive)
388 results = NotifyPushWard.parse_url("pushward://hlk_AbC123")
389 assert results["apikey"] == "hlk_AbC123"
390
391
392 def test_plugin_pushward_level_resolution():
393 """NotifyPushWard() level short-form resolution."""
394
395 # Full names, short-forms, and unique single-letter prefixes
396 assert pushward_level("passive") == "passive"
397 assert pushward_level("crit") == "critical"
398 assert pushward_level("c") == "critical"
399 assert pushward_level("t") == "time-sensitive"
400 assert pushward_level("ACTIVE") == "active"
401
402 # Empty or non-matching input resolves to None
403 assert pushward_level("") is None
404 assert pushward_level(" ") is None
405 assert pushward_level("bogus") is None
406
407
408 @mock.patch("requests.post")
409 def test_plugin_pushward_per_type_levels(mock_post):
410 """NotifyPushWard() per-notification-type level overrides."""
411
412 response = mock.Mock()
413 response.status_code = requests.codes.ok
414 response.content = b""
415 mock_post.return_value = response
416
417 obj = Apprise.instantiate(
418 "pushward://hlk_abc123?info=passive&failure=crit"
419 )
420 assert isinstance(obj, NotifyPushWard)
421 assert obj.level_map[NotifyType.INFO] == "passive"
422 # Short-form resolves to the canonical level
423 assert obj.level_map[NotifyType.FAILURE] == "critical"
424 # Untouched types keep their defaults
425 assert obj.level_map[NotifyType.WARNING] == "time-sensitive"
426
427 assert obj.notify(title="t", body="b", notify_type=NotifyType.INFO) is True
428 assert loads(mock_post.call_args[1]["data"])["level"] == "passive"
429
430 assert (
431 obj.notify(title="t", body="b", notify_type=NotifyType.FAILURE) is True
432 )
433 assert loads(mock_post.call_args[1]["data"])["level"] == "critical"
434
435 # A global ?level= still forces every type
436 obj = Apprise.instantiate(
437 "pushward://hlk_abc123?level=passive&info=critical"
438 )
439 assert obj.notify(title="t", body="b", notify_type=NotifyType.INFO) is True
440 assert loads(mock_post.call_args[1]["data"])["level"] == "passive"