Debian Codegraph @

tests/test_plugin_growl.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 import sys
29 from unittest import mock
30
31 import pytest
32
33 import apprise
34 from apprise import NotifyBase
35 from apprise.plugins.growl import GrowlPriority, NotifyGrowl
36
37 try:
38 from gntp import errors
39
40 TEST_GROWL_EXCEPTIONS = (
41 errors.NetworkError(0, "gntp.ParseError() not handled"),
42 errors.AuthError(0, "gntp.AuthError() not handled"),
43 errors.ParseError(0, "gntp.ParseError() not handled"),
44 errors.UnsupportedError(0, "gntp.UnsupportedError() not handled"),
45 )
46
47 except ImportError:
48 # no problem; gntp isn't available to us
49 pass
50
51 # Disable logging for a cleaner testing output
52 import logging
53
54 logging.disable(logging.CRITICAL)
55
56
57 @pytest.mark.skipif(
58 "gntp" in sys.modules, reason="Requires that gntp NOT be installed"
59 )
60 def test_plugin_growl_gntp_import_error():
61 """NotifyGrowl() Import Error."""
62 # If the object is disabled, then it can't be instantiated
63 obj = apprise.Apprise.instantiate("growl://growl.server")
64 assert obj is None
65
66
67 @pytest.mark.skipif("gntp" not in sys.modules, reason="Requires gntp")
68 @mock.patch("gntp.notifier.GrowlNotifier")
69 def test_plugin_growl_exception_handling(mock_gntp):
70 """NotifyGrowl() Exception Handling."""
71 TEST_GROWL_EXCEPTIONS = (
72 errors.NetworkError(0, "gntp.ParseError() not handled"),
73 errors.AuthError(0, "gntp.AuthError() not handled"),
74 errors.ParseError(0, "gntp.ParseError() not handled"),
75 errors.UnsupportedError(0, "gntp.UnsupportedError() not handled"),
76 )
77
78 mock_notifier = mock.Mock()
79 mock_gntp.return_value = mock_notifier
80 mock_notifier.notify.return_value = True
81
82 # First we test the growl.register() function
83 for exception in TEST_GROWL_EXCEPTIONS:
84 mock_notifier.register.side_effect = exception
85
86 # instantiate our object
87 obj = apprise.Apprise.instantiate(
88 "growl://growl.server.hostname", suppress_exceptions=False
89 )
90
91 # Verify Growl object was instantiated
92 assert obj is not None
93
94 # We will fail to send the notification because our registration
95 # would have failed
96 assert (
97 obj.notify(
98 title="test", body="body", notify_type=apprise.NotifyType.INFO
99 )
100 is False
101 )
102
103 # Now we test the growl.notify() function
104 mock_notifier.register.side_effect = None
105 for exception in TEST_GROWL_EXCEPTIONS:
106 mock_notifier.notify.side_effect = exception
107
108 # instantiate our object
109 obj = apprise.Apprise.instantiate(
110 "growl://growl.server.hostname", suppress_exceptions=False
111 )
112
113 # Verify Growl object was instantiated
114 assert obj is not None
115
116 # We will fail to send the notification because of the underlining
117 # notify() call throws an exception
118 assert (
119 obj.notify(
120 title="test", body="body", notify_type=apprise.NotifyType.INFO
121 )
122 is False
123 )
124
125
126 @pytest.mark.skipif("gntp" not in sys.modules, reason="Requires gntp")
127 @mock.patch("gntp.notifier.GrowlNotifier")
128 def test_plugin_growl_general(mock_gntp):
129 """NotifyGrowl() General Checks."""
130
131 urls = (
132 ##################################
133 # NotifyGrowl
134 ##################################
135 (
136 "growl://",
137 {
138 "instance": None,
139 },
140 ),
141 ("growl://:@/", {"instance": None}),
142 (
143 "growl://[email protected]",
144 {
145 "instance": NotifyGrowl,
146 },
147 ),
148 (
149 "growl://ignored:[email protected]",
150 {
151 "instance": NotifyGrowl,
152 },
153 ),
154 (
155 "growl://growl.server",
156 {
157 "instance": NotifyGrowl,
158 # don't include an image by default
159 "include_image": False,
160 },
161 ),
162 (
163 "growl://growl.server?version=1",
164 {
165 "instance": NotifyGrowl,
166 },
167 ),
168 # Test sticky flag
169 (
170 "growl://growl.server?sticky=yes",
171 {
172 "instance": NotifyGrowl,
173 },
174 ),
175 (
176 "growl://growl.server?sticky=no",
177 {
178 "instance": NotifyGrowl,
179 },
180 ),
181 # Force a failure
182 (
183 "growl://growl.server?version=1",
184 {
185 "instance": NotifyGrowl,
186 "growl_response": None,
187 },
188 ),
189 (
190 "growl://growl.server?version=2",
191 {
192 # don't include an image by default
193 "include_image": False,
194 "instance": NotifyGrowl,
195 },
196 ),
197 (
198 "growl://growl.server?version=2",
199 {
200 # don't include an image by default
201 "include_image": False,
202 "instance": NotifyGrowl,
203 "growl_response": None,
204 },
205 ),
206 # Priorities
207 (
208 "growl://[email protected]?priority=low",
209 {
210 "instance": NotifyGrowl,
211 },
212 ),
213 (
214 "growl://[email protected]?priority=moderate",
215 {
216 "instance": NotifyGrowl,
217 },
218 ),
219 (
220 "growl://[email protected]?priority=normal",
221 {
222 "instance": NotifyGrowl,
223 },
224 ),
225 (
226 "growl://[email protected]?priority=high",
227 {
228 "instance": NotifyGrowl,
229 },
230 ),
231 (
232 "growl://[email protected]?priority=emergency",
233 {
234 "instance": NotifyGrowl,
235 },
236 ),
237 # Invalid Priorities
238 (
239 "growl://[email protected]?priority=invalid",
240 {
241 "instance": NotifyGrowl,
242 },
243 ),
244 (
245 "growl://[email protected]?priority=",
246 {
247 "instance": NotifyGrowl,
248 },
249 ),
250 # invalid version
251 (
252 "growl://growl.server?version=",
253 {
254 "instance": NotifyGrowl,
255 },
256 ),
257 (
258 "growl://growl.server?version=crap",
259 {
260 "instance": NotifyGrowl,
261 },
262 ),
263 # Ports
264 (
265 "growl://growl.changeport:2000",
266 {
267 "instance": NotifyGrowl,
268 },
269 ),
270 (
271 "growl://growl.garbageport:garbage",
272 {
273 "instance": NotifyGrowl,
274 },
275 ),
276 (
277 "growl://growl.colon:",
278 {
279 "instance": NotifyGrowl,
280 },
281 ),
282 )
283
284 # iterate over our dictionary and test it out
285 for url, meta in urls:
286 # Our expected instance
287 instance = meta.get("instance", None)
288
289 # Our expected exception
290 exception = meta.get("exception", None)
291
292 # Our expected server objects
293 self = meta.get("self", None)
294
295 # Our expected Query response (True, False, or exception type)
296 response = meta.get("response", True)
297
298 # Allow us to force the server response code to be something other then
299 # the defaults
300 growl_response = meta.get("growl_response", bool(response))
301
302 mock_notifier = mock.Mock()
303 mock_gntp.return_value = mock_notifier
304 mock_notifier.notify.side_effect = None
305
306 # Store our response
307 mock_notifier.notify.return_value = growl_response
308
309 try:
310 obj = apprise.Apprise.instantiate(url, suppress_exceptions=False)
311
312 assert exception is None
313
314 if obj is None:
315 # We're done
316 continue
317
318 if instance is None:
319 # Expected None but didn't get it
320 raise AssertionError()
321
322 assert isinstance(obj, instance) is True
323
324 # Test our URL Identifier is generated
325 assert isinstance(obj.url_id(), str) is True
326
327 if isinstance(obj, NotifyBase):
328 # We loaded okay; now lets make sure we can reverse this url
329 assert isinstance(obj.url(), str) is True
330
331 # Test our privacy=True flag
332 assert isinstance(obj.url(privacy=True), str) is True
333
334 # Instantiate the exact same object again using the URL from
335 # the one that was already created properly
336 obj_cmp = apprise.Apprise.instantiate(obj.url())
337
338 # Our object should be the same instance as what we had
339 # originally expected above.
340 if not isinstance(obj_cmp, NotifyBase):
341 # Assert messages are hard to trace back with the way
342 # these tests work. Just printing before throwing our
343 # assertion failure makes things easier to debug later on
344 raise AssertionError()
345
346 if self:
347 # Iterate over our expected entries inside of our object
348 for key, val in self.items():
349 # Test that our object has the desired key
350 assert hasattr(key, obj)
351 assert getattr(key, obj) == val
352
353 try:
354 # check that we're as expected
355 assert (
356 obj.notify(
357 title="test",
358 body="body",
359 notify_type=apprise.NotifyType.INFO,
360 )
361 == response
362 )
363
364 except Exception as e:
365 # Check that we were expecting this exception to happen
366 assert isinstance(e, response)
367
368 except AssertionError:
369 # Don't mess with these entries
370 raise
371
372 except Exception as e:
373 # Handle our exception
374 assert exception is not None
375 assert isinstance(e, exception)
376
377
378 @pytest.mark.skipif("gntp" not in sys.modules, reason="Requires gntp")
379 @mock.patch("gntp.notifier.GrowlNotifier")
380 def test_plugin_growl_config_files(mock_gntp):
381 """NotifyGrowl() Config File Cases."""
382 content = """
383 urls:
384 - growl://[email protected]:
385 - priority: -2
386 tag: growl_int low
387 - priority: "-2"
388 tag: growl_str_int low
389 - priority: low
390 tag: growl_str low
391
392 # This will take on moderate (default) priority
393 - priority: invalid
394 tag: growl_invalid
395
396 - growl://[email protected]:
397 - priority: 2
398 tag: growl_int emerg
399 - priority: "2"
400 tag: growl_str_int emerg
401 - priority: emergency
402 tag: growl_str emerg
403 """
404
405 mock_notifier = mock.Mock()
406 mock_gntp.return_value = mock_notifier
407 mock_notifier.notify.return_value = True
408
409 # Create ourselves a config object
410 ac = apprise.AppriseConfig()
411 assert ac.add_config(content=content) is True
412
413 aobj = apprise.Apprise()
414
415 # Add our configuration
416 aobj.add(ac)
417
418 # We should be able to read our 7 servers from that
419 # 3x low
420 # 3x emerg
421 # 1x invalid (so takes on normal priority)
422 assert len(ac.servers()) == 7
423 assert len(aobj) == 7
424 assert len(list(aobj.find(tag="low"))) == 3
425 for s in aobj.find(tag="low"):
426 assert s.priority == GrowlPriority.LOW
427
428 assert len(list(aobj.find(tag="emerg"))) == 3
429 for s in aobj.find(tag="emerg"):
430 assert s.priority == GrowlPriority.EMERGENCY
431
432 assert len(list(aobj.find(tag="growl_str"))) == 2
433 assert len(list(aobj.find(tag="growl_str_int"))) == 2
434 assert len(list(aobj.find(tag="growl_int"))) == 2
435
436 assert len(list(aobj.find(tag="growl_invalid"))) == 1
437 assert (
438 next(aobj.find(tag="growl_invalid")).priority == GrowlPriority.NORMAL
439 )