2019-01-30 02:09:32 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2019 Chris Caron <lead2gold@gmail.com>
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# This code is licensed under the MIT License.
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files(the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions :
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
2022-10-16 19:43:15 +02:00
|
|
|
import importlib
|
|
|
|
import logging
|
2019-01-30 02:09:32 +01:00
|
|
|
import sys
|
|
|
|
import types
|
2022-10-08 02:28:36 +02:00
|
|
|
from unittest import mock
|
2022-10-16 19:43:15 +02:00
|
|
|
from unittest.mock import Mock, call, ANY
|
|
|
|
|
|
|
|
import pytest
|
2022-10-08 02:28:36 +02:00
|
|
|
|
2019-01-30 02:09:32 +01:00
|
|
|
import apprise
|
2022-10-16 19:43:15 +02:00
|
|
|
from apprise.plugins.NotifyGnome import GnomeUrgency, NotifyGnome
|
2022-10-09 11:28:18 +02:00
|
|
|
from helpers import reload_plugin
|
2019-01-30 02:09:32 +01:00
|
|
|
|
2019-02-25 07:02:29 +01:00
|
|
|
# Disable logging for a cleaner testing output
|
|
|
|
logging.disable(logging.CRITICAL)
|
|
|
|
|
2019-01-30 02:09:32 +01:00
|
|
|
|
2022-10-16 19:43:15 +02:00
|
|
|
def setup_glib_environment():
|
2019-01-30 02:09:32 +01:00
|
|
|
"""
|
2022-10-16 19:43:15 +02:00
|
|
|
Setup a heavily mocked Glib environment.
|
2019-01-30 02:09:32 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Our module base
|
|
|
|
gi_name = 'gi'
|
|
|
|
|
|
|
|
# First we do an import without the gi library available to ensure
|
|
|
|
# we can handle cases when the library simply isn't available
|
|
|
|
|
|
|
|
if gi_name in sys.modules:
|
|
|
|
# Test cases where the gi library exists; we want to remove it
|
|
|
|
# for the purpose of testing and capture the handling of the
|
|
|
|
# library when it is missing
|
|
|
|
del sys.modules[gi_name]
|
2022-10-09 11:28:18 +02:00
|
|
|
reload_plugin('NotifyGnome')
|
2019-01-30 02:09:32 +01:00
|
|
|
|
2021-03-26 21:52:00 +01:00
|
|
|
# We need to fake our gnome environment for testing purposes since
|
|
|
|
# the gi library isn't available in Travis CI
|
2019-01-30 02:09:32 +01:00
|
|
|
gi = types.ModuleType(gi_name)
|
|
|
|
gi.repository = types.ModuleType(gi_name + '.repository')
|
|
|
|
gi.module = types.ModuleType(gi_name + '.module')
|
|
|
|
|
|
|
|
mock_pixbuf = mock.Mock()
|
|
|
|
mock_notify = mock.Mock()
|
|
|
|
|
|
|
|
gi.repository.GdkPixbuf = \
|
|
|
|
types.ModuleType(gi_name + '.repository.GdkPixbuf')
|
|
|
|
gi.repository.GdkPixbuf.Pixbuf = mock_pixbuf
|
|
|
|
gi.repository.Notify = mock.Mock()
|
|
|
|
gi.repository.Notify.init.return_value = True
|
|
|
|
gi.repository.Notify.Notification = mock_notify
|
|
|
|
|
2019-02-23 21:42:29 +01:00
|
|
|
# Emulate require_version function:
|
2019-01-30 02:09:32 +01:00
|
|
|
gi.require_version = mock.Mock(
|
|
|
|
name=gi_name + '.require_version')
|
|
|
|
|
|
|
|
# Force the fake module to exist
|
|
|
|
sys.modules[gi_name] = gi
|
|
|
|
sys.modules[gi_name + '.repository'] = gi.repository
|
|
|
|
sys.modules[gi_name + '.repository.Notify'] = gi.repository.Notify
|
|
|
|
|
|
|
|
# Notify Object
|
|
|
|
notify_obj = mock.Mock()
|
|
|
|
notify_obj.set_urgency.return_value = True
|
|
|
|
notify_obj.set_icon_from_pixbuf.return_value = True
|
|
|
|
notify_obj.set_image_from_pixbuf.return_value = True
|
|
|
|
notify_obj.show.return_value = True
|
|
|
|
mock_notify.new.return_value = notify_obj
|
|
|
|
mock_pixbuf.new_from_file.return_value = True
|
2022-10-09 11:28:18 +02:00
|
|
|
|
2022-10-16 19:43:15 +02:00
|
|
|
# When patching something which has a side effect on the module-level code
|
|
|
|
# of a plugin, make sure to reload it.
|
|
|
|
current_module = sys.modules[__name__]
|
|
|
|
reload_plugin('NotifyGnome', replace_in=current_module)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def glib_environment():
|
|
|
|
"""
|
|
|
|
Fixture to provide a mocked Glib environment to test case functions.
|
|
|
|
"""
|
|
|
|
setup_glib_environment()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def obj(glib_environment):
|
|
|
|
"""
|
|
|
|
Fixture to provide a mocked Apprise instance.
|
|
|
|
"""
|
2019-01-30 02:09:32 +01:00
|
|
|
|
|
|
|
# Create our instance
|
|
|
|
obj = apprise.Apprise.instantiate('gnome://', suppress_exceptions=False)
|
2021-11-25 21:20:22 +01:00
|
|
|
assert obj is not None
|
2022-10-16 19:43:15 +02:00
|
|
|
assert isinstance(obj, NotifyGnome) is True
|
2021-11-25 21:20:22 +01:00
|
|
|
|
|
|
|
# Set our duration to 0 to speed up timeouts (for testing)
|
2019-01-30 02:09:32 +01:00
|
|
|
obj.duration = 0
|
|
|
|
|
|
|
|
# Check that it found our mocked environments
|
2021-11-25 21:20:22 +01:00
|
|
|
assert obj.enabled is True
|
2019-01-30 02:09:32 +01:00
|
|
|
|
2022-10-16 19:43:15 +02:00
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
def test_plugin_gnome_general_success(obj):
|
|
|
|
"""
|
|
|
|
NotifyGnome() general checks
|
|
|
|
"""
|
|
|
|
|
2019-02-13 23:48:09 +01:00
|
|
|
# Test url() call
|
2022-10-08 02:28:36 +02:00
|
|
|
assert isinstance(obj.url(), str) is True
|
2019-02-13 23:48:09 +01:00
|
|
|
|
2019-01-30 02:09:32 +01:00
|
|
|
# test notifications
|
2019-03-27 04:06:20 +01:00
|
|
|
assert obj.notify(title='title', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is True
|
2019-01-30 02:09:32 +01:00
|
|
|
|
|
|
|
# test notification without a title
|
2019-03-27 04:06:20 +01:00
|
|
|
assert obj.notify(title='', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is True
|
|
|
|
|
2022-10-16 19:43:15 +02:00
|
|
|
|
|
|
|
def test_plugin_gnome_image_success(glib_environment):
|
|
|
|
"""
|
|
|
|
Verify using the `image` query argument works as intended.
|
|
|
|
"""
|
|
|
|
|
2019-03-27 04:06:20 +01:00
|
|
|
obj = apprise.Apprise.instantiate(
|
|
|
|
'gnome://_/?image=True', suppress_exceptions=False)
|
2022-10-09 11:28:18 +02:00
|
|
|
assert isinstance(obj, NotifyGnome) is True
|
2019-03-27 04:06:20 +01:00
|
|
|
assert obj.notify(title='title', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is True
|
|
|
|
|
|
|
|
obj = apprise.Apprise.instantiate(
|
|
|
|
'gnome://_/?image=False', suppress_exceptions=False)
|
2022-10-09 11:28:18 +02:00
|
|
|
assert isinstance(obj, NotifyGnome) is True
|
2019-03-27 04:06:20 +01:00
|
|
|
assert obj.notify(title='title', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is True
|
|
|
|
|
2022-10-16 19:43:15 +02:00
|
|
|
|
|
|
|
def test_plugin_gnome_priority(glib_environment):
|
|
|
|
"""
|
|
|
|
Verify correctness of the `priority` query argument.
|
|
|
|
"""
|
|
|
|
|
2019-03-27 04:06:20 +01:00
|
|
|
# Test Priority (alias of urgency)
|
|
|
|
obj = apprise.Apprise.instantiate(
|
|
|
|
'gnome://_/?priority=invalid', suppress_exceptions=False)
|
2022-10-09 11:28:18 +02:00
|
|
|
assert isinstance(obj, NotifyGnome) is True
|
2019-03-27 04:06:20 +01:00
|
|
|
assert obj.urgency == 1
|
|
|
|
assert obj.notify(title='title', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is True
|
|
|
|
|
|
|
|
obj = apprise.Apprise.instantiate(
|
|
|
|
'gnome://_/?priority=high', suppress_exceptions=False)
|
2022-10-09 11:28:18 +02:00
|
|
|
assert isinstance(obj, NotifyGnome) is True
|
2019-03-27 04:06:20 +01:00
|
|
|
assert obj.urgency == 2
|
|
|
|
assert obj.notify(title='title', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is True
|
|
|
|
|
|
|
|
obj = apprise.Apprise.instantiate(
|
|
|
|
'gnome://_/?priority=2', suppress_exceptions=False)
|
2022-10-09 11:28:18 +02:00
|
|
|
assert isinstance(obj, NotifyGnome) is True
|
2019-03-27 04:06:20 +01:00
|
|
|
assert obj.urgency == 2
|
|
|
|
assert obj.notify(title='title', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is True
|
|
|
|
|
2022-10-16 19:43:15 +02:00
|
|
|
|
|
|
|
def test_plugin_gnome_urgency(glib_environment):
|
|
|
|
"""
|
|
|
|
Verify correctness of the `urgency` query argument.
|
|
|
|
"""
|
|
|
|
|
2019-03-27 04:06:20 +01:00
|
|
|
# Test Urgeny
|
|
|
|
obj = apprise.Apprise.instantiate(
|
|
|
|
'gnome://_/?urgency=invalid', suppress_exceptions=False)
|
|
|
|
assert obj.urgency == 1
|
2022-10-09 11:28:18 +02:00
|
|
|
assert isinstance(obj, NotifyGnome) is True
|
2019-03-27 04:06:20 +01:00
|
|
|
assert obj.notify(title='title', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is True
|
|
|
|
|
|
|
|
obj = apprise.Apprise.instantiate(
|
|
|
|
'gnome://_/?urgency=high', suppress_exceptions=False)
|
|
|
|
assert obj.urgency == 2
|
2022-10-09 11:28:18 +02:00
|
|
|
assert isinstance(obj, NotifyGnome) is True
|
2019-03-27 04:06:20 +01:00
|
|
|
assert obj.notify(title='title', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is True
|
|
|
|
|
|
|
|
obj = apprise.Apprise.instantiate(
|
|
|
|
'gnome://_/?urgency=2', suppress_exceptions=False)
|
2022-10-09 11:28:18 +02:00
|
|
|
assert isinstance(obj, NotifyGnome) is True
|
2019-03-27 04:06:20 +01:00
|
|
|
assert obj.urgency == 2
|
|
|
|
assert obj.notify(title='title', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is True
|
2019-01-30 02:09:32 +01:00
|
|
|
|
2022-10-16 19:43:15 +02:00
|
|
|
|
|
|
|
def test_plugin_gnome_parse_configuration(obj):
|
|
|
|
"""
|
|
|
|
Verify configuration parsing works correctly.
|
|
|
|
"""
|
|
|
|
|
2022-06-09 23:54:57 +02:00
|
|
|
# Test configuration parsing
|
|
|
|
content = """
|
|
|
|
urls:
|
|
|
|
- gnome://:
|
|
|
|
- priority: 0
|
|
|
|
tag: gnome_int low
|
|
|
|
- priority: "0"
|
|
|
|
tag: gnome_str_int low
|
|
|
|
- priority: low
|
|
|
|
tag: gnome_str low
|
|
|
|
- urgency: 0
|
|
|
|
tag: gnome_int low
|
|
|
|
- urgency: "0"
|
|
|
|
tag: gnome_str_int low
|
|
|
|
- urgency: low
|
|
|
|
tag: gnome_str low
|
|
|
|
|
|
|
|
# These will take on normal (default) urgency
|
|
|
|
- priority: invalid
|
|
|
|
tag: gnome_invalid
|
|
|
|
- urgency: invalid
|
|
|
|
tag: gnome_invalid
|
|
|
|
|
|
|
|
- gnome://:
|
|
|
|
- priority: 2
|
|
|
|
tag: gnome_int high
|
|
|
|
- priority: "2"
|
|
|
|
tag: gnome_str_int high
|
|
|
|
- priority: high
|
|
|
|
tag: gnome_str high
|
|
|
|
- urgency: 2
|
|
|
|
tag: gnome_int high
|
|
|
|
- urgency: "2"
|
|
|
|
tag: gnome_str_int high
|
|
|
|
- urgency: high
|
|
|
|
tag: gnome_str high
|
|
|
|
"""
|
|
|
|
|
|
|
|
# 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 14 servers from that
|
|
|
|
# 6x low
|
|
|
|
# 6x high
|
|
|
|
# 2x invalid (so takes on normal urgency)
|
|
|
|
assert len(ac.servers()) == 14
|
|
|
|
assert len(aobj) == 14
|
|
|
|
assert len([x for x in aobj.find(tag='low')]) == 6
|
|
|
|
for s in aobj.find(tag='low'):
|
|
|
|
assert s.urgency == GnomeUrgency.LOW
|
|
|
|
|
|
|
|
assert len([x for x in aobj.find(tag='high')]) == 6
|
|
|
|
for s in aobj.find(tag='high'):
|
|
|
|
assert s.urgency == GnomeUrgency.HIGH
|
|
|
|
|
|
|
|
assert len([x for x in aobj.find(tag='gnome_str')]) == 4
|
|
|
|
assert len([x for x in aobj.find(tag='gnome_str_int')]) == 4
|
|
|
|
assert len([x for x in aobj.find(tag='gnome_int')]) == 4
|
|
|
|
|
|
|
|
assert len([x for x in aobj.find(tag='gnome_invalid')]) == 2
|
|
|
|
for s in aobj.find(tag='gnome_invalid'):
|
|
|
|
assert s.urgency == GnomeUrgency.NORMAL
|
|
|
|
|
2022-10-16 19:43:15 +02:00
|
|
|
|
|
|
|
def test_plugin_gnome_missing_icon(mocker, obj):
|
|
|
|
"""
|
|
|
|
Verify the notification will be submitted, even if loading the icon fails.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Inject error when loading icon.
|
|
|
|
gi = importlib.import_module("gi")
|
|
|
|
gi.repository.GdkPixbuf.Pixbuf.new_from_file.side_effect = \
|
|
|
|
AttributeError("Something failed")
|
|
|
|
|
|
|
|
logger: Mock = mocker.spy(obj, "logger")
|
2019-03-27 04:06:20 +01:00
|
|
|
assert obj.notify(title='title', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is True
|
2022-10-16 19:43:15 +02:00
|
|
|
assert logger.mock_calls == [
|
2022-10-16 22:53:36 +02:00
|
|
|
call.warning('Could not load notification icon (%s).', ANY),
|
|
|
|
call.debug('Gnome Exception: Something failed'),
|
2022-10-16 19:43:15 +02:00
|
|
|
call.info('Sent Gnome notification.'),
|
|
|
|
]
|
2019-01-30 02:09:32 +01:00
|
|
|
|
|
|
|
|
2022-10-16 19:43:15 +02:00
|
|
|
def test_plugin_gnome_disabled_plugin(obj):
|
|
|
|
"""
|
|
|
|
Verify notification will not be submitted if plugin is disabled.
|
|
|
|
"""
|
2021-11-25 21:20:22 +01:00
|
|
|
obj.enabled = False
|
2019-03-27 04:06:20 +01:00
|
|
|
assert obj.notify(title='title', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is False
|
2019-01-30 02:09:32 +01:00
|
|
|
|
2022-10-16 19:43:15 +02:00
|
|
|
|
|
|
|
def test_plugin_gnome_set_urgency():
|
|
|
|
"""
|
|
|
|
Test the setting of an urgency, through `priority` keyword argument.
|
|
|
|
"""
|
2022-10-09 11:28:18 +02:00
|
|
|
NotifyGnome(priority=0)
|
2019-02-24 17:43:01 +01:00
|
|
|
|
|
|
|
|
2022-10-16 19:43:15 +02:00
|
|
|
def test_plugin_gnome_gi_croaks():
|
|
|
|
"""
|
|
|
|
Verify notification fails when `gi.require_version()` croaks.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Make `require_version` function raise an error.
|
|
|
|
try:
|
|
|
|
gi = importlib.import_module("gi")
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
raise pytest.skip("`gi` package not installed")
|
|
|
|
gi.require_version.side_effect = ValueError("Something failed")
|
2019-02-24 17:43:01 +01:00
|
|
|
|
2022-10-16 19:43:15 +02:00
|
|
|
# When patching something which has a side effect on the module-level code
|
|
|
|
# of a plugin, make sure to reload it.
|
|
|
|
current_module = sys.modules[__name__]
|
|
|
|
reload_plugin('NotifyGnome', replace_in=current_module)
|
|
|
|
|
|
|
|
# Create instance.
|
2019-02-24 17:43:01 +01:00
|
|
|
obj = apprise.Apprise.instantiate('gnome://', suppress_exceptions=False)
|
2022-10-16 19:43:15 +02:00
|
|
|
|
|
|
|
# The notifier is marked disabled.
|
2021-11-25 21:20:22 +01:00
|
|
|
assert obj is None
|
2022-10-16 19:43:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_plugin_gnome_notify_croaks(mocker, obj):
|
|
|
|
"""
|
|
|
|
Fail gracefully if underlying object croaks for whatever reason.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Inject an error when invoking `gi.repository.Notify`.
|
|
|
|
mocker.patch('gi.repository.Notify.Notification.new',
|
|
|
|
side_effect=AttributeError("Something failed"))
|
|
|
|
|
|
|
|
logger: Mock = mocker.spy(obj, "logger")
|
|
|
|
assert obj.notify(
|
|
|
|
title='title', body='body',
|
|
|
|
notify_type=apprise.NotifyType.INFO) is False
|
|
|
|
assert logger.mock_calls == [
|
2022-10-16 22:53:36 +02:00
|
|
|
call.warning('Failed to send Gnome notification.'),
|
|
|
|
call.debug('Gnome Exception: Something failed'),
|
2022-10-16 19:43:15 +02:00
|
|
|
]
|