| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 49 |
pass |
| 50 |
|
| 51 |
|
| 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 |
|
| 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 |
|
| 83 |
for exception in TEST_GROWL_EXCEPTIONS: |
| 84 |
mock_notifier.register.side_effect = exception |
| 85 |
|
| 86 |
|
| 87 |
obj = apprise.Apprise.instantiate( |
| 88 |
"growl://growl.server.hostname", suppress_exceptions=False |
| 89 |
) |
| 90 |
|
| 91 |
|
| 92 |
assert obj is not None |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
assert ( |
| 97 |
obj.notify( |
| 98 |
title="test", body="body", notify_type=apprise.NotifyType.INFO |
| 99 |
) |
| 100 |
is False |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
mock_notifier.register.side_effect = None |
| 105 |
for exception in TEST_GROWL_EXCEPTIONS: |
| 106 |
mock_notifier.notify.side_effect = exception |
| 107 |
|
| 108 |
|
| 109 |
obj = apprise.Apprise.instantiate( |
| 110 |
"growl://growl.server.hostname", suppress_exceptions=False |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
assert obj is not None |
| 115 |
|
| 116 |
|
| 117 |
|
| 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 |
|
| 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 |
|
| 159 |
"include_image": False, |
| 160 |
}, |
| 161 |
), |
| 162 |
( |
| 163 |
"growl://growl.server?version=1", |
| 164 |
{ |
| 165 |
"instance": NotifyGrowl, |
| 166 |
}, |
| 167 |
), |
| 168 |
|
| 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 |
|
| 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 |
|
| 193 |
"include_image": False, |
| 194 |
"instance": NotifyGrowl, |
| 195 |
}, |
| 196 |
), |
| 197 |
( |
| 198 |
"growl://growl.server?version=2", |
| 199 |
{ |
| 200 |
|
| 201 |
"include_image": False, |
| 202 |
"instance": NotifyGrowl, |
| 203 |
"growl_response": None, |
| 204 |
}, |
| 205 |
), |
| 206 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 285 |
for url, meta in urls: |
| 286 |
|
| 287 |
instance = meta.get("instance", None) |
| 288 |
|
| 289 |
|
| 290 |
exception = meta.get("exception", None) |
| 291 |
|
| 292 |
|
| 293 |
self = meta.get("self", None) |
| 294 |
|
| 295 |
|
| 296 |
response = meta.get("response", True) |
| 297 |
|
| 298 |
|
| 299 |
|
| 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 |
|
| 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 |
|
| 316 |
continue |
| 317 |
|
| 318 |
if instance is None: |
| 319 |
|
| 320 |
raise AssertionError() |
| 321 |
|
| 322 |
assert isinstance(obj, instance) is True |
| 323 |
|
| 324 |
|
| 325 |
assert isinstance(obj.url_id(), str) is True |
| 326 |
|
| 327 |
if isinstance(obj, NotifyBase): |
| 328 |
|
| 329 |
assert isinstance(obj.url(), str) is True |
| 330 |
|
| 331 |
|
| 332 |
assert isinstance(obj.url(privacy=True), str) is True |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
obj_cmp = apprise.Apprise.instantiate(obj.url()) |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
if not isinstance(obj_cmp, NotifyBase): |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
raise AssertionError() |
| 345 |
|
| 346 |
if self: |
| 347 |
|
| 348 |
for key, val in self.items(): |
| 349 |
|
| 350 |
assert hasattr(key, obj) |
| 351 |
assert getattr(key, obj) == val |
| 352 |
|
| 353 |
try: |
| 354 |
|
| 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 |
|
| 366 |
assert isinstance(e, response) |
| 367 |
|
| 368 |
except AssertionError: |
| 369 |
|
| 370 |
raise |
| 371 |
|
| 372 |
except Exception as e: |
| 373 |
|
| 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 |
|
| 410 |
ac = apprise.AppriseConfig() |
| 411 |
assert ac.add_config(content=content) is True |
| 412 |
|
| 413 |
aobj = apprise.Apprise() |
| 414 |
|
| 415 |
|
| 416 |
aobj.add(ac) |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 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 |
) |