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