| 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 importlib |
| 29 |
import logging |
| 30 |
import sys |
| 31 |
import types |
| 32 |
from unittest import mock |
| 33 |
from unittest.mock import ANY, Mock, call |
| 34 |
|
| 35 |
from helpers import reload_plugin |
| 36 |
import pytest |
| 37 |
|
| 38 |
import apprise |
| 39 |
from apprise.plugins.gnome import GnomeUrgency, NotifyGnome |
| 40 |
|
| 41 |
# Disable logging for a cleaner testing output |
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
|
| 45 |
def setup_glib_environment(): |
| 46 |
"""Setup a heavily mocked Glib environment.""" |
| 47 |
|
| 48 |
# Our module base |
| 49 |
gi_name = "gi" |
| 50 |
|
| 51 |
# First we do an import without the gi library available to ensure |
| 52 |
# we can handle cases when the library simply isn't available |
| 53 |
|
| 54 |
if gi_name in sys.modules: |
| 55 |
# Test cases where the gi library exists; we want to remove it |
| 56 |
# for the purpose of testing and capture the handling of the |
| 57 |
# library when it is missing |
| 58 |
del sys.modules[gi_name] |
| 59 |
reload_plugin("gnome") |
| 60 |
|
| 61 |
# We need to fake our gnome environment for testing purposes since |
| 62 |
# the gi library isn't available on CI |
| 63 |
gi = types.ModuleType(gi_name) |
| 64 |
gi.repository = types.ModuleType(gi_name + ".repository") |
| 65 |
gi.module = types.ModuleType(gi_name + ".module") |
| 66 |
|
| 67 |
mock_pixbuf = mock.Mock() |
| 68 |
mock_notify = mock.Mock() |
| 69 |
|
| 70 |
gi.repository.GdkPixbuf = types.ModuleType( |
| 71 |
gi_name + ".repository.GdkPixbuf" |
| 72 |
) |
| 73 |
gi.repository.GdkPixbuf.Pixbuf = mock_pixbuf |
| 74 |
gi.repository.Notify = mock.Mock() |
| 75 |
gi.repository.Notify.init.return_value = True |
| 76 |
gi.repository.Notify.Notification = mock_notify |
| 77 |
|
| 78 |
# Emulate require_version function: |
| 79 |
gi.require_version = mock.Mock(name=gi_name + ".require_version") |
| 80 |
|
| 81 |
# Force the fake module to exist |
| 82 |
sys.modules[gi_name] = gi |
| 83 |
sys.modules[gi_name + ".repository"] = gi.repository |
| 84 |
sys.modules[gi_name + ".repository.Notify"] = gi.repository.Notify |
| 85 |
|
| 86 |
# Notify Object |
| 87 |
notify_obj = mock.Mock() |
| 88 |
notify_obj.set_urgency.return_value = True |
| 89 |
notify_obj.set_icon_from_pixbuf.return_value = True |
| 90 |
notify_obj.set_image_from_pixbuf.return_value = True |
| 91 |
notify_obj.show.return_value = True |
| 92 |
mock_notify.new.return_value = notify_obj |
| 93 |
mock_pixbuf.new_from_file.return_value = True |
| 94 |
|
| 95 |
# When patching something which has a side effect on the module-level code |
| 96 |
# of a plugin, make sure to reload it. |
| 97 |
reload_plugin("gnome") |
| 98 |
|
| 99 |
|
| 100 |
@pytest.fixture |
| 101 |
def glib_environment(): |
| 102 |
"""Fixture to provide a mocked Glib environment to test case functions.""" |
| 103 |
setup_glib_environment() |
| 104 |
|
| 105 |
|
| 106 |
@pytest.fixture |
| 107 |
def obj(glib_environment): |
| 108 |
"""Fixture to provide a mocked Apprise instance.""" |
| 109 |
|
| 110 |
# Create our instance |
| 111 |
obj = apprise.Apprise.instantiate("gnome://", suppress_exceptions=False) |
| 112 |
assert obj is not None |
| 113 |
assert isinstance(obj, NotifyGnome) is True |
| 114 |
|
| 115 |
# Set our duration to 0 to speed up timeouts (for testing) |
| 116 |
obj.duration = 0 |
| 117 |
|
| 118 |
# Check that it found our mocked environments |
| 119 |
assert obj.enabled is True |
| 120 |
|
| 121 |
return obj |
| 122 |
|
| 123 |
|
| 124 |
def test_plugin_gnome_general_success(obj): |
| 125 |
"""NotifyGnome() general checks.""" |
| 126 |
|
| 127 |
# Test url() call |
| 128 |
assert isinstance(obj.url(), str) is True |
| 129 |
|
| 130 |
# our URL Identifier is disabled |
| 131 |
assert obj.url_id() is None |
| 132 |
|
| 133 |
# test notifications |
| 134 |
assert ( |
| 135 |
bool( |
| 136 |
obj.notify( |
| 137 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 138 |
) |
| 139 |
) |
| 140 |
is True |
| 141 |
) |
| 142 |
|
| 143 |
# test notification without a title |
| 144 |
assert ( |
| 145 |
bool( |
| 146 |
obj.notify( |
| 147 |
title="", body="body", notify_type=apprise.NotifyType.INFO |
| 148 |
) |
| 149 |
) |
| 150 |
is True |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
def test_plugin_gnome_image_success(glib_environment): |
| 155 |
"""Verify using the `image` query argument works as intended.""" |
| 156 |
|
| 157 |
obj = apprise.Apprise.instantiate( |
| 158 |
"gnome://_/?image=True", suppress_exceptions=False |
| 159 |
) |
| 160 |
assert isinstance(obj, NotifyGnome) is True |
| 161 |
assert ( |
| 162 |
bool( |
| 163 |
obj.notify( |
| 164 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 165 |
) |
| 166 |
) |
| 167 |
is True |
| 168 |
) |
| 169 |
|
| 170 |
obj = apprise.Apprise.instantiate( |
| 171 |
"gnome://_/?image=False", suppress_exceptions=False |
| 172 |
) |
| 173 |
assert isinstance(obj, NotifyGnome) is True |
| 174 |
assert ( |
| 175 |
bool( |
| 176 |
obj.notify( |
| 177 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 178 |
) |
| 179 |
) |
| 180 |
is True |
| 181 |
) |
| 182 |
|
| 183 |
|
| 184 |
def test_plugin_gnome_priority(glib_environment): |
| 185 |
"""Verify correctness of the `priority` query argument.""" |
| 186 |
|
| 187 |
# Test Priority (alias of urgency) |
| 188 |
obj = apprise.Apprise.instantiate( |
| 189 |
"gnome://_/?priority=invalid", suppress_exceptions=False |
| 190 |
) |
| 191 |
assert isinstance(obj, NotifyGnome) is True |
| 192 |
assert obj.urgency == 1 |
| 193 |
assert ( |
| 194 |
bool( |
| 195 |
obj.notify( |
| 196 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 197 |
) |
| 198 |
) |
| 199 |
is True |
| 200 |
) |
| 201 |
|
| 202 |
obj = apprise.Apprise.instantiate( |
| 203 |
"gnome://_/?priority=high", suppress_exceptions=False |
| 204 |
) |
| 205 |
assert isinstance(obj, NotifyGnome) is True |
| 206 |
assert obj.urgency == 2 |
| 207 |
assert ( |
| 208 |
bool( |
| 209 |
obj.notify( |
| 210 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 211 |
) |
| 212 |
) |
| 213 |
is True |
| 214 |
) |
| 215 |
|
| 216 |
obj = apprise.Apprise.instantiate( |
| 217 |
"gnome://_/?priority=2", suppress_exceptions=False |
| 218 |
) |
| 219 |
assert isinstance(obj, NotifyGnome) is True |
| 220 |
assert obj.urgency == 2 |
| 221 |
assert ( |
| 222 |
bool( |
| 223 |
obj.notify( |
| 224 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 225 |
) |
| 226 |
) |
| 227 |
is True |
| 228 |
) |
| 229 |
|
| 230 |
|
| 231 |
def test_plugin_gnome_urgency(glib_environment): |
| 232 |
"""Verify correctness of the `urgency` query argument.""" |
| 233 |
|
| 234 |
# Test Urgeny |
| 235 |
obj = apprise.Apprise.instantiate( |
| 236 |
"gnome://_/?urgency=invalid", suppress_exceptions=False |
| 237 |
) |
| 238 |
assert obj.urgency == 1 |
| 239 |
assert isinstance(obj, NotifyGnome) is True |
| 240 |
assert ( |
| 241 |
bool( |
| 242 |
obj.notify( |
| 243 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 244 |
) |
| 245 |
) |
| 246 |
is True |
| 247 |
) |
| 248 |
|
| 249 |
obj = apprise.Apprise.instantiate( |
| 250 |
"gnome://_/?urgency=high", suppress_exceptions=False |
| 251 |
) |
| 252 |
assert obj.urgency == 2 |
| 253 |
assert isinstance(obj, NotifyGnome) is True |
| 254 |
assert ( |
| 255 |
bool( |
| 256 |
obj.notify( |
| 257 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 258 |
) |
| 259 |
) |
| 260 |
is True |
| 261 |
) |
| 262 |
|
| 263 |
obj = apprise.Apprise.instantiate( |
| 264 |
"gnome://_/?urgency=2", suppress_exceptions=False |
| 265 |
) |
| 266 |
assert isinstance(obj, NotifyGnome) is True |
| 267 |
assert obj.urgency == 2 |
| 268 |
assert ( |
| 269 |
bool( |
| 270 |
obj.notify( |
| 271 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 272 |
) |
| 273 |
) |
| 274 |
is True |
| 275 |
) |
| 276 |
|
| 277 |
|
| 278 |
def test_plugin_gnome_parse_configuration(obj): |
| 279 |
"""Verify configuration parsing works correctly.""" |
| 280 |
|
| 281 |
# Test configuration parsing |
| 282 |
content = """ |
| 283 |
urls: |
| 284 |
- gnome://: |
| 285 |
- priority: 0 |
| 286 |
tag: gnome_int low |
| 287 |
- priority: "0" |
| 288 |
tag: gnome_str_int low |
| 289 |
- priority: low |
| 290 |
tag: gnome_str low |
| 291 |
- urgency: 0 |
| 292 |
tag: gnome_int low |
| 293 |
- urgency: "0" |
| 294 |
tag: gnome_str_int low |
| 295 |
- urgency: low |
| 296 |
tag: gnome_str low |
| 297 |
|
| 298 |
# These will take on normal (default) urgency |
| 299 |
- priority: invalid |
| 300 |
tag: gnome_invalid |
| 301 |
- urgency: invalid |
| 302 |
tag: gnome_invalid |
| 303 |
|
| 304 |
- gnome://: |
| 305 |
- priority: 2 |
| 306 |
tag: gnome_int high |
| 307 |
- priority: "2" |
| 308 |
tag: gnome_str_int high |
| 309 |
- priority: high |
| 310 |
tag: gnome_str high |
| 311 |
- urgency: 2 |
| 312 |
tag: gnome_int high |
| 313 |
- urgency: "2" |
| 314 |
tag: gnome_str_int high |
| 315 |
- urgency: high |
| 316 |
tag: gnome_str high |
| 317 |
""" |
| 318 |
|
| 319 |
# Create ourselves a config object |
| 320 |
ac = apprise.AppriseConfig() |
| 321 |
assert ac.add_config(content=content) is True |
| 322 |
|
| 323 |
aobj = apprise.Apprise() |
| 324 |
|
| 325 |
# Add our configuration |
| 326 |
aobj.add(ac) |
| 327 |
|
| 328 |
# We should be able to read our 14 services from that |
| 329 |
# 6x low |
| 330 |
# 6x high |
| 331 |
# 2x invalid (so takes on normal urgency) |
| 332 |
assert len(ac.services()) == 14 |
| 333 |
assert len(aobj) == 14 |
| 334 |
assert len(list(aobj.find(tag="low"))) == 6 |
| 335 |
for s in aobj.find(tag="low"): |
| 336 |
assert s.urgency == GnomeUrgency.LOW |
| 337 |
|
| 338 |
assert len(list(aobj.find(tag="high"))) == 6 |
| 339 |
for s in aobj.find(tag="high"): |
| 340 |
assert s.urgency == GnomeUrgency.HIGH |
| 341 |
|
| 342 |
assert len(list(aobj.find(tag="gnome_str"))) == 4 |
| 343 |
assert len(list(aobj.find(tag="gnome_str_int"))) == 4 |
| 344 |
assert len(list(aobj.find(tag="gnome_int"))) == 4 |
| 345 |
|
| 346 |
assert len(list(aobj.find(tag="gnome_invalid"))) == 2 |
| 347 |
for s in aobj.find(tag="gnome_invalid"): |
| 348 |
assert s.urgency == GnomeUrgency.NORMAL |
| 349 |
|
| 350 |
|
| 351 |
def test_plugin_gnome_missing_icon(mocker, obj): |
| 352 |
"""Verify the notification will be submitted, even if loading the icon |
| 353 |
fails.""" |
| 354 |
|
| 355 |
# Inject error when loading icon. |
| 356 |
gi = importlib.import_module("gi") |
| 357 |
gi.repository.GdkPixbuf.Pixbuf.new_from_file.side_effect = AttributeError( |
| 358 |
"Something failed" |
| 359 |
) |
| 360 |
|
| 361 |
logger: Mock = mocker.spy(obj, "logger") |
| 362 |
assert ( |
| 363 |
bool( |
| 364 |
obj.notify( |
| 365 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 366 |
) |
| 367 |
) |
| 368 |
is True |
| 369 |
) |
| 370 |
assert logger.mock_calls == [ |
| 371 |
call.warning("Could not load notification icon (%s).", ANY), |
| 372 |
call.debug("Gnome Exception: Something failed"), |
| 373 |
call.info("Sent Gnome notification."), |
| 374 |
call.debug("%s send() completed in %.2fs.", ANY, ANY), |
| 375 |
] |
| 376 |
|
| 377 |
|
| 378 |
def test_plugin_gnome_disabled_plugin(obj): |
| 379 |
"""Verify notification will not be submitted if plugin is disabled.""" |
| 380 |
obj.enabled = False |
| 381 |
assert ( |
| 382 |
bool( |
| 383 |
obj.notify( |
| 384 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 385 |
) |
| 386 |
) |
| 387 |
is False |
| 388 |
) |
| 389 |
|
| 390 |
|
| 391 |
def test_plugin_gnome_set_urgency(): |
| 392 |
"""Test the setting of an urgency, through `priority` keyword argument.""" |
| 393 |
NotifyGnome(priority=0) |
| 394 |
|
| 395 |
|
| 396 |
@pytest.mark.skipif("gi" not in sys.modules, reason="Requires gi library") |
| 397 |
def test_plugin_gnome_gi_croaks(): |
| 398 |
"""Verify notification fails when `gi.require_version()` croaks.""" |
| 399 |
|
| 400 |
# Make `require_version` function raise an error. |
| 401 |
gi = importlib.import_module("gi") |
| 402 |
gi.require_version.side_effect = ValueError("Something failed") |
| 403 |
|
| 404 |
# When patching something which has a side effect on the module-level code |
| 405 |
# of a plugin, make sure to reload it. |
| 406 |
reload_plugin("gnome") |
| 407 |
|
| 408 |
# Create instance. |
| 409 |
obj = apprise.Apprise.instantiate("gnome://", suppress_exceptions=False) |
| 410 |
|
| 411 |
# The notifier is marked disabled. |
| 412 |
assert obj is None |
| 413 |
|
| 414 |
|
| 415 |
@pytest.mark.skipif("gi" not in sys.modules, reason="Requires gi library") |
| 416 |
def test_plugin_gnome_notify_croaks(mocker, obj): |
| 417 |
"""Fail gracefully if underlying object croaks for whatever reason.""" |
| 418 |
|
| 419 |
# Inject an error when invoking `gi.repository.Notify`. |
| 420 |
mocker.patch( |
| 421 |
"gi.repository.Notify.Notification.new", |
| 422 |
side_effect=AttributeError("Something failed"), |
| 423 |
) |
| 424 |
|
| 425 |
logger: Mock = mocker.spy(obj, "logger") |
| 426 |
assert ( |
| 427 |
bool( |
| 428 |
obj.notify( |
| 429 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 430 |
) |
| 431 |
) |
| 432 |
is False |
| 433 |
) |
| 434 |
assert logger.mock_calls == [ |
| 435 |
call.warning("Failed to send Gnome notification."), |
| 436 |
call.debug("Gnome Exception: Something failed"), |
| 437 |
call.debug("%s send() completed in %.2fs.", ANY, ANY), |
| 438 |
] |
| 439 |
|
| 440 |
|
| 441 |
def test_plugin_gnome_show_exception(mocker, obj): |
| 442 |
""" |
| 443 |
Test that an exception raised during notification.show() is caught |
| 444 |
by the outer try/except block. |
| 445 |
""" |
| 446 |
# 1. Import the mocked 'gi' library. |
| 447 |
# The 'obj' fixture (via 'glib_environment') has already set this up |
| 448 |
# in sys.modules, so this import works even if Gnome isn't installed. |
| 449 |
import gi |
| 450 |
|
| 451 |
# 2. Retrieve the mocked notification instance. |
| 452 |
# Your setup_glib_environment() sets |
| 453 |
# 'gi.repository.Notify.Notification.new' to return a mock object. |
| 454 |
notification_instance = gi.repository.Notify.Notification.new.return_value |
| 455 |
|
| 456 |
# 3. Force the .show() method to raise an exception. |
| 457 |
notification_instance.show.side_effect = Exception( |
| 458 |
"Simulated Gnome Show Failure" |
| 459 |
) |
| 460 |
|
| 461 |
# 4. Spy on the logger to verify the output. |
| 462 |
logger_spy = mocker.spy(obj, "logger") |
| 463 |
|
| 464 |
# 5. Execute the notification. |
| 465 |
# It should crash at .show(), catch the exception, and return False. |
| 466 |
assert bool(obj.notify(title="Title", body="Body")) is False |
| 467 |
|
| 468 |
# 6. Verify the specific log message. |
| 469 |
logger_spy.warning.assert_called_with("Failed to send Gnome notification.") |