2017-12-14 03:35:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-10-13 23:08:01 +02:00
|
|
|
# BSD 2-Clause License
|
2017-12-14 03:35:59 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# Apprise - Push Notification Library.
|
2024-01-27 21:35:11 +01:00
|
|
|
# Copyright (c) 2024, Chris Caron <lead2gold@gmail.com>
|
2017-12-14 03:35:59 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
2017-12-14 03:35:59 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
2017-12-14 03:35:59 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
2017-12-14 03:35:59 +01:00
|
|
|
#
|
2023-02-09 09:54:55 +01:00
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
2017-12-14 03:35:59 +01:00
|
|
|
|
2020-08-04 18:04:23 +02:00
|
|
|
import sys
|
2022-10-08 02:28:36 +02:00
|
|
|
from unittest import mock
|
2022-09-01 02:05:40 +02:00
|
|
|
|
2020-08-04 18:04:23 +02:00
|
|
|
import pytest
|
|
|
|
import apprise
|
2022-10-09 11:28:18 +02:00
|
|
|
from apprise import NotifyBase
|
2024-04-27 19:02:27 +02:00
|
|
|
from apprise.plugins.growl import GrowlPriority, NotifyGrowl
|
2019-02-13 23:48:09 +01:00
|
|
|
|
2020-08-04 18:04:23 +02:00
|
|
|
try:
|
|
|
|
from gntp import errors
|
|
|
|
|
|
|
|
TEST_GROWL_EXCEPTIONS = (
|
|
|
|
errors.NetworkError(
|
|
|
|
0, 'gntp.ParseError() not handled'),
|
|
|
|
errors.AuthError(
|
|
|
|
0, 'gntp.AuthError() not handled'),
|
|
|
|
errors.ParseError(
|
|
|
|
0, 'gntp.ParseError() not handled'),
|
|
|
|
errors.UnsupportedError(
|
|
|
|
0, 'gntp.UnsupportedError() not handled'),
|
|
|
|
)
|
2021-11-25 21:20:22 +01:00
|
|
|
|
2020-08-04 18:04:23 +02:00
|
|
|
except ImportError:
|
2021-11-25 21:20:22 +01:00
|
|
|
# no problem; gntp isn't available to us
|
|
|
|
pass
|
2020-08-04 18:04:23 +02:00
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
# Disable logging for a cleaner testing output
|
|
|
|
import logging
|
|
|
|
logging.disable(logging.CRITICAL)
|
2020-08-04 18:04:23 +02:00
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'gntp' in sys.modules,
|
|
|
|
reason="Requires that gntp NOT be installed")
|
|
|
|
def test_plugin_growl_gntp_import_error():
|
2020-08-04 18:04:23 +02:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifyGrowl() Import Error
|
2020-08-04 18:04:23 +02:00
|
|
|
|
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
# If the object is disabled, then it can't be instantiated
|
2020-08-04 18:04:23 +02:00
|
|
|
obj = apprise.Apprise.instantiate('growl://growl.server')
|
2021-11-25 21:20:22 +01:00
|
|
|
assert obj is None
|
2020-08-04 18:04:23 +02:00
|
|
|
|
|
|
|
|
2021-11-25 21:20:22 +01:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
'gntp' not in sys.modules, reason="Requires gntp")
|
2020-08-04 18:04:23 +02:00
|
|
|
@mock.patch('gntp.notifier.GrowlNotifier')
|
2021-11-25 21:20:22 +01:00
|
|
|
def test_plugin_growl_exception_handling(mock_gntp):
|
2020-08-04 18:04:23 +02:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifyGrowl() Exception Handling
|
2020-08-04 18:04:23 +02:00
|
|
|
"""
|
|
|
|
TEST_GROWL_EXCEPTIONS = (
|
|
|
|
errors.NetworkError(
|
|
|
|
0, 'gntp.ParseError() not handled'),
|
|
|
|
errors.AuthError(
|
|
|
|
0, 'gntp.AuthError() not handled'),
|
|
|
|
errors.ParseError(
|
|
|
|
0, 'gntp.ParseError() not handled'),
|
|
|
|
errors.UnsupportedError(
|
|
|
|
0, 'gntp.UnsupportedError() not handled'),
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_notifier = mock.Mock()
|
|
|
|
mock_gntp.return_value = mock_notifier
|
|
|
|
mock_notifier.notify.return_value = True
|
|
|
|
|
|
|
|
# First we test the growl.register() function
|
|
|
|
for exception in TEST_GROWL_EXCEPTIONS:
|
|
|
|
mock_notifier.register.side_effect = exception
|
|
|
|
|
|
|
|
# instantiate our object
|
|
|
|
obj = apprise.Apprise.instantiate(
|
|
|
|
'growl://growl.server.hostname', suppress_exceptions=False)
|
|
|
|
|
|
|
|
# Verify Growl object was instantiated
|
|
|
|
assert obj is not None
|
|
|
|
|
|
|
|
# We will fail to send the notification because our registration
|
|
|
|
# would have failed
|
|
|
|
assert obj.notify(
|
|
|
|
title='test', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is False
|
|
|
|
|
|
|
|
# Now we test the growl.notify() function
|
|
|
|
mock_notifier.register.side_effect = None
|
|
|
|
for exception in TEST_GROWL_EXCEPTIONS:
|
|
|
|
mock_notifier.notify.side_effect = exception
|
|
|
|
|
|
|
|
# instantiate our object
|
|
|
|
obj = apprise.Apprise.instantiate(
|
|
|
|
'growl://growl.server.hostname', suppress_exceptions=False)
|
|
|
|
|
|
|
|
# Verify Growl object was instantiated
|
|
|
|
assert obj is not None
|
|
|
|
|
|
|
|
# We will fail to send the notification because of the underlining
|
|
|
|
# notify() call throws an exception
|
|
|
|
assert obj.notify(
|
|
|
|
title='test', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is False
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
2021-11-25 21:20:22 +01:00
|
|
|
'gntp' not in sys.modules, reason="Requires gntp")
|
2020-08-04 18:04:23 +02:00
|
|
|
@mock.patch('gntp.notifier.GrowlNotifier')
|
2021-11-25 21:20:22 +01:00
|
|
|
def test_plugin_growl_general(mock_gntp):
|
2017-12-14 03:35:59 +01:00
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
NotifyGrowl() General Checks
|
2017-12-14 03:35:59 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2020-08-04 18:04:23 +02:00
|
|
|
urls = (
|
|
|
|
##################################
|
|
|
|
# NotifyGrowl
|
|
|
|
##################################
|
|
|
|
('growl://', {
|
|
|
|
'instance': None,
|
|
|
|
}),
|
|
|
|
('growl://:@/', {
|
|
|
|
'instance': None
|
|
|
|
}),
|
|
|
|
|
|
|
|
('growl://pass@growl.server', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
('growl://ignored:pass@growl.server', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
('growl://growl.server', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
# don't include an image by default
|
|
|
|
'include_image': False,
|
|
|
|
}),
|
|
|
|
('growl://growl.server?version=1', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
# Test sticky flag
|
|
|
|
('growl://growl.server?sticky=yes', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
('growl://growl.server?sticky=no', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
# Force a failure
|
|
|
|
('growl://growl.server?version=1', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
'growl_response': None,
|
|
|
|
}),
|
|
|
|
('growl://growl.server?version=2', {
|
|
|
|
# don't include an image by default
|
|
|
|
'include_image': False,
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
('growl://growl.server?version=2', {
|
|
|
|
# don't include an image by default
|
|
|
|
'include_image': False,
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
'growl_response': None,
|
|
|
|
}),
|
|
|
|
|
|
|
|
# Priorities
|
|
|
|
('growl://pass@growl.server?priority=low', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
('growl://pass@growl.server?priority=moderate', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
('growl://pass@growl.server?priority=normal', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
('growl://pass@growl.server?priority=high', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
('growl://pass@growl.server?priority=emergency', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
# Invalid Priorities
|
|
|
|
('growl://pass@growl.server?priority=invalid', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
('growl://pass@growl.server?priority=', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
# invalid version
|
|
|
|
('growl://growl.server?version=', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
('growl://growl.server?version=crap', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
# Ports
|
|
|
|
('growl://growl.changeport:2000', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
('growl://growl.garbageport:garbage', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
('growl://growl.colon:', {
|
2022-10-09 11:28:18 +02:00
|
|
|
'instance': NotifyGrowl,
|
2020-08-04 18:04:23 +02:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
2017-12-14 03:35:59 +01:00
|
|
|
# iterate over our dictionary and test it out
|
2020-08-04 18:04:23 +02:00
|
|
|
for (url, meta) in urls:
|
2017-12-14 03:35:59 +01:00
|
|
|
|
|
|
|
# Our expected instance
|
|
|
|
instance = meta.get('instance', None)
|
|
|
|
|
|
|
|
# Our expected exception
|
|
|
|
exception = meta.get('exception', None)
|
|
|
|
|
|
|
|
# Our expected server objects
|
|
|
|
self = meta.get('self', None)
|
|
|
|
|
|
|
|
# Our expected Query response (True, False, or exception type)
|
|
|
|
response = meta.get('response', True)
|
|
|
|
|
|
|
|
# Allow us to force the server response code to be something other then
|
|
|
|
# the defaults
|
|
|
|
growl_response = meta.get(
|
|
|
|
'growl_response', True if response else False)
|
|
|
|
|
|
|
|
mock_notifier = mock.Mock()
|
|
|
|
mock_gntp.return_value = mock_notifier
|
2020-08-04 18:04:23 +02:00
|
|
|
mock_notifier.notify.side_effect = None
|
2017-12-14 03:35:59 +01:00
|
|
|
|
2020-08-04 18:04:23 +02:00
|
|
|
# Store our response
|
|
|
|
mock_notifier.notify.return_value = growl_response
|
2017-12-14 03:35:59 +01:00
|
|
|
|
|
|
|
try:
|
2020-08-04 18:04:23 +02:00
|
|
|
obj = apprise.Apprise.instantiate(url, suppress_exceptions=False)
|
2017-12-14 03:35:59 +01:00
|
|
|
|
2019-10-09 18:39:31 +02:00
|
|
|
assert exception is None
|
2017-12-14 03:35:59 +01:00
|
|
|
|
|
|
|
if obj is None:
|
|
|
|
# We're done
|
|
|
|
continue
|
|
|
|
|
|
|
|
if instance is None:
|
|
|
|
# Expected None but didn't get it
|
2019-10-09 18:39:31 +02:00
|
|
|
assert False
|
2017-12-14 03:35:59 +01:00
|
|
|
|
2019-10-09 18:39:31 +02:00
|
|
|
assert isinstance(obj, instance) is True
|
2019-02-13 23:48:09 +01:00
|
|
|
|
2024-08-23 03:44:14 +02:00
|
|
|
# Test our URL Identifier is generated
|
|
|
|
assert isinstance(obj.url_id(), str) is True
|
|
|
|
|
2022-10-09 11:28:18 +02:00
|
|
|
if isinstance(obj, NotifyBase):
|
2019-02-13 23:48:09 +01:00
|
|
|
# We loaded okay; now lets make sure we can reverse this url
|
2022-10-08 02:28:36 +02:00
|
|
|
assert isinstance(obj.url(), str) is True
|
2019-02-13 23:48:09 +01:00
|
|
|
|
2019-09-29 23:17:25 +02:00
|
|
|
# Test our privacy=True flag
|
2019-10-09 18:39:31 +02:00
|
|
|
assert isinstance(
|
2022-10-08 02:28:36 +02:00
|
|
|
obj.url(privacy=True), str) is True
|
2019-09-29 23:17:25 +02:00
|
|
|
|
2019-02-13 23:48:09 +01:00
|
|
|
# Instantiate the exact same object again using the URL from
|
|
|
|
# the one that was already created properly
|
2020-08-04 18:04:23 +02:00
|
|
|
obj_cmp = apprise.Apprise.instantiate(obj.url())
|
2019-02-13 23:48:09 +01:00
|
|
|
|
|
|
|
# Our object should be the same instance as what we had
|
|
|
|
# originally expected above.
|
2022-10-09 11:28:18 +02:00
|
|
|
if not isinstance(obj_cmp, NotifyBase):
|
2019-02-13 23:48:09 +01:00
|
|
|
# Assert messages are hard to trace back with the way
|
|
|
|
# these tests work. Just printing before throwing our
|
|
|
|
# assertion failure makes things easier to debug later on
|
|
|
|
print('TEST FAIL: {} regenerated as {}'.format(
|
|
|
|
url, obj.url()))
|
2019-10-09 18:39:31 +02:00
|
|
|
assert False
|
2017-12-14 03:35:59 +01:00
|
|
|
|
|
|
|
if self:
|
|
|
|
# Iterate over our expected entries inside of our object
|
|
|
|
for key, val in self.items():
|
|
|
|
# Test that our object has the desired key
|
2019-10-09 18:39:31 +02:00
|
|
|
assert hasattr(key, obj)
|
|
|
|
assert getattr(key, obj) == val
|
2017-12-14 03:35:59 +01:00
|
|
|
|
|
|
|
try:
|
2020-08-04 18:04:23 +02:00
|
|
|
# check that we're as expected
|
|
|
|
assert obj.notify(
|
|
|
|
title='test', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) == response
|
2017-12-14 03:35:59 +01:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
# Check that we were expecting this exception to happen
|
|
|
|
assert isinstance(e, response)
|
|
|
|
|
|
|
|
except AssertionError:
|
|
|
|
# Don't mess with these entries
|
|
|
|
print('%s AssertionError' % url)
|
|
|
|
raise
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
# Handle our exception
|
|
|
|
print('%s / %s' % (url, str(e)))
|
2019-10-09 18:39:31 +02:00
|
|
|
assert exception is not None
|
|
|
|
assert isinstance(e, exception)
|
2022-06-09 23:54:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
'gntp' not in sys.modules, reason="Requires gntp")
|
|
|
|
@mock.patch('gntp.notifier.GrowlNotifier')
|
2022-10-16 16:48:44 +02:00
|
|
|
def test_plugin_growl_config_files(mock_gntp):
|
2022-06-09 23:54:57 +02:00
|
|
|
"""
|
|
|
|
NotifyGrowl() Config File Cases
|
|
|
|
"""
|
|
|
|
content = """
|
|
|
|
urls:
|
|
|
|
- growl://pass@growl.server:
|
|
|
|
- priority: -2
|
|
|
|
tag: growl_int low
|
|
|
|
- priority: "-2"
|
|
|
|
tag: growl_str_int low
|
|
|
|
- priority: low
|
|
|
|
tag: growl_str low
|
|
|
|
|
|
|
|
# This will take on moderate (default) priority
|
|
|
|
- priority: invalid
|
|
|
|
tag: growl_invalid
|
|
|
|
|
|
|
|
- growl://pass@growl.server:
|
|
|
|
- priority: 2
|
|
|
|
tag: growl_int emerg
|
|
|
|
- priority: "2"
|
|
|
|
tag: growl_str_int emerg
|
|
|
|
- priority: emergency
|
|
|
|
tag: growl_str emerg
|
|
|
|
"""
|
|
|
|
|
|
|
|
mock_notifier = mock.Mock()
|
|
|
|
mock_gntp.return_value = mock_notifier
|
|
|
|
mock_notifier.notify.return_value = True
|
|
|
|
|
|
|
|
# Create ourselves a config object
|
|
|
|
ac = apprise.AppriseConfig()
|
|
|
|
assert ac.add_config(content=content) is True
|
|
|
|
|
|
|
|
aobj = apprise.Apprise()
|
|
|
|
|
|
|
|
# Add our configuration
|
|
|
|
aobj.add(ac)
|
|
|
|
|
|
|
|
# We should be able to read our 7 servers from that
|
|
|
|
# 3x low
|
|
|
|
# 3x emerg
|
|
|
|
# 1x invalid (so takes on normal priority)
|
|
|
|
assert len(ac.servers()) == 7
|
|
|
|
assert len(aobj) == 7
|
|
|
|
assert len([x for x in aobj.find(tag='low')]) == 3
|
|
|
|
for s in aobj.find(tag='low'):
|
|
|
|
assert s.priority == GrowlPriority.LOW
|
|
|
|
|
|
|
|
assert len([x for x in aobj.find(tag='emerg')]) == 3
|
|
|
|
for s in aobj.find(tag='emerg'):
|
|
|
|
assert s.priority == GrowlPriority.EMERGENCY
|
|
|
|
|
|
|
|
assert len([x for x in aobj.find(tag='growl_str')]) == 2
|
|
|
|
assert len([x for x in aobj.find(tag='growl_str_int')]) == 2
|
|
|
|
assert len([x for x in aobj.find(tag='growl_int')]) == 2
|
|
|
|
|
|
|
|
assert len([x for x in aobj.find(tag='growl_invalid')]) == 1
|
|
|
|
assert next(aobj.find(tag='growl_invalid')).priority == \
|
|
|
|
GrowlPriority.NORMAL
|