Debian Codegraph @

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