| 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 logging |
| 29 |
import sys |
| 30 |
import types |
| 31 |
from unittest.mock import Mock, call |
| 32 |
|
| 33 |
from helpers import reload_plugin |
| 34 |
import pytest |
| 35 |
|
| 36 |
import apprise |
| 37 |
from apprise.exception import AppriseImproperlyConfigured |
| 38 |
from apprise.plugins.glib import GLibUrgency, NotifyGLib |
| 39 |
|
| 40 |
# Disable logging output during testing |
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
|
| 44 |
@pytest.fixture |
| 45 |
def enabled_glib_environment(monkeypatch): |
| 46 |
""" |
| 47 |
Fully mocked GI/GLib/Gio/GdkPixbuf environment for local and CI. |
| 48 |
""" |
| 49 |
# Step 1: Fake gi and repository |
| 50 |
gi = types.ModuleType("gi") |
| 51 |
gi.require_version = Mock() |
| 52 |
|
| 53 |
fake_variant = Mock(name="Variant") |
| 54 |
fake_error = type("GLibError", (Exception,), {}) |
| 55 |
fake_pixbuf = Mock() |
| 56 |
fake_image = Mock() |
| 57 |
|
| 58 |
fake_pixbuf.new_from_file.return_value = fake_image |
| 59 |
fake_image.get_width.return_value = 100 |
| 60 |
fake_image.get_height.return_value = 100 |
| 61 |
fake_image.get_rowstride.return_value = 1 |
| 62 |
fake_image.get_has_alpha.return_value = False |
| 63 |
fake_image.get_bits_per_sample.return_value = 8 |
| 64 |
fake_image.get_n_channels.return_value = 1 |
| 65 |
fake_image.get_pixels.return_value = b"" |
| 66 |
|
| 67 |
gi.repository = types.SimpleNamespace( |
| 68 |
Gio=Mock(), |
| 69 |
GLib=types.SimpleNamespace(Variant=fake_variant, Error=fake_error), |
| 70 |
GdkPixbuf=types.SimpleNamespace(Pixbuf=fake_pixbuf), |
| 71 |
) |
| 72 |
|
| 73 |
# Step 2: Inject into sys.modules |
| 74 |
sys.modules["gi"] = gi |
| 75 |
sys.modules["gi.repository"] = gi.repository |
| 76 |
|
| 77 |
# Step 3: Reload plugin with all mocks in place |
| 78 |
reload_plugin("glib") |
| 79 |
|
| 80 |
|
| 81 |
def test_plugin_glib_gdkpixbuf_attribute_error(monkeypatch): |
| 82 |
"""Simulate AttributeError from importing GdkPixbuf""" |
| 83 |
|
| 84 |
# Create gi module |
| 85 |
gi = types.ModuleType("gi") |
| 86 |
|
| 87 |
# Create gi.repository mock, but DO NOT include GdkPixbuf |
| 88 |
gi.repository = types.SimpleNamespace( |
| 89 |
Gio=Mock(), |
| 90 |
GLib=types.SimpleNamespace( |
| 91 |
Variant=Mock(), Error=type("GLibError", (Exception,), {}) |
| 92 |
), |
| 93 |
# GdkPixbuf missing entirely triggers AttributeError |
| 94 |
) |
| 95 |
|
| 96 |
def fake_require_version(name, version): |
| 97 |
if name == "GdkPixbuf": |
| 98 |
# Simulate success in require_version |
| 99 |
return |
| 100 |
return |
| 101 |
|
| 102 |
gi.require_version = Mock(side_effect=fake_require_version) |
| 103 |
|
| 104 |
# Inject into sys.modules |
| 105 |
sys.modules["gi"] = gi |
| 106 |
sys.modules["gi.repository"] = gi.repository |
| 107 |
|
| 108 |
# Trigger the plugin reload with our patched environment |
| 109 |
reload_plugin("glib") |
| 110 |
|
| 111 |
from apprise.plugins import glib as plugin_glib |
| 112 |
|
| 113 |
assert plugin_glib.NOTIFY_GLIB_IMAGE_SUPPORT is False |
| 114 |
|
| 115 |
|
| 116 |
def test_plugin_glib_basic_notify(enabled_glib_environment): |
| 117 |
"""Basic notification path""" |
| 118 |
obj = apprise.Apprise.instantiate("glib://", suppress_exceptions=False) |
| 119 |
assert isinstance(obj, NotifyGLib) |
| 120 |
assert bool(obj.notify("body", title="title")) is True |
| 121 |
|
| 122 |
|
| 123 |
def test_plugin_glib_url_includes_coordinates(enabled_glib_environment): |
| 124 |
"""Test that x/y coordinates appear in the rendered URL.""" |
| 125 |
obj = apprise.Apprise.instantiate( |
| 126 |
"glib://_/?x=7&y=9", suppress_exceptions=False |
| 127 |
) |
| 128 |
url = obj.url(privacy=False) |
| 129 |
|
| 130 |
assert "x=7" in url |
| 131 |
assert "y=9" in url |
| 132 |
|
| 133 |
|
| 134 |
def test_plugin_glib_zero_coordinate_round_trip(enabled_glib_environment): |
| 135 |
""" |
| 136 |
Coordinate 0 is a valid screen edge (send() emits it via an `is None` |
| 137 |
guard); url() must preserve it so the configuration survives a |
| 138 |
save/reload cycle instead of being silently dropped. |
| 139 |
""" |
| 140 |
obj = apprise.Apprise.instantiate( |
| 141 |
"glib://_/?x=0&y=0", suppress_exceptions=False |
| 142 |
) |
| 143 |
assert isinstance(obj, NotifyGLib) |
| 144 |
assert obj.x_axis == 0 |
| 145 |
assert obj.y_axis == 0 |
| 146 |
|
| 147 |
url = obj.url() |
| 148 |
assert "x=0" in url |
| 149 |
assert "y=0" in url |
| 150 |
|
| 151 |
# The rendered URL must round-trip back to an identical object rather |
| 152 |
# than dropping x=0 (which previously also raised a TypeError on reload |
| 153 |
# whenever the surviving axis kept the guard truthy). |
| 154 |
reloaded = apprise.Apprise.instantiate(url, suppress_exceptions=False) |
| 155 |
assert isinstance(reloaded, NotifyGLib) |
| 156 |
assert reloaded.x_axis == 0 |
| 157 |
assert reloaded.y_axis == 0 |
| 158 |
|
| 159 |
# A single zero axis must also survive without dropping the other value. |
| 160 |
obj = apprise.Apprise.instantiate( |
| 161 |
"glib://_/?x=0&y=5", suppress_exceptions=False |
| 162 |
) |
| 163 |
reloaded = apprise.Apprise.instantiate( |
| 164 |
obj.url(), suppress_exceptions=False |
| 165 |
) |
| 166 |
assert reloaded.x_axis == 0 |
| 167 |
assert reloaded.y_axis == 5 |
| 168 |
|
| 169 |
|
| 170 |
def test_plugin_glib_icon_fails_gracefully(mocker, enabled_glib_environment): |
| 171 |
"""Simulate image load failure""" |
| 172 |
import gi |
| 173 |
|
| 174 |
gi.repository.GdkPixbuf.Pixbuf.new_from_file.side_effect = AttributeError( |
| 175 |
"fail" |
| 176 |
) |
| 177 |
obj = apprise.Apprise.instantiate("glib://", suppress_exceptions=False) |
| 178 |
spy = mocker.spy(obj, "logger") |
| 179 |
assert bool(obj.notify("msg", title="t")) is True |
| 180 |
assert any( |
| 181 |
"Could not load notification icon" in str(x) |
| 182 |
for x in spy.warning.call_args_list |
| 183 |
) |
| 184 |
|
| 185 |
|
| 186 |
def test_plugin_glib_send_raises_glib_error(mocker, enabled_glib_environment): |
| 187 |
"""Simulate GLib.Error in DBusProxy creation""" |
| 188 |
import gi |
| 189 |
|
| 190 |
gi.repository.Gio.DBusProxy.new_for_bus_sync.side_effect = ( |
| 191 |
gi.repository.GLib.Error("fail") |
| 192 |
) |
| 193 |
obj = apprise.Apprise.instantiate("glib://", suppress_exceptions=False) |
| 194 |
assert bool(obj.notify("fail test")) is False |
| 195 |
|
| 196 |
|
| 197 |
def test_plugin_glib_send_raises_generic(mocker, enabled_glib_environment): |
| 198 |
"""Simulate generic error in gio_iface.Notify()""" |
| 199 |
# Re: https://github.com/caronc/apprise/issues/1383 |
| 200 |
# This test validates that the NotifyGLib plugin correctly handles a |
| 201 |
# generic exception raised by the `Notify()` method call on a mocked |
| 202 |
# DBus interface. However, it is only meaningful in environments that: |
| 203 |
# |
| 204 |
# 1. Do NOT have PyGObject (`gi`) installed, OR |
| 205 |
# 2. Have `gi`, but without introspection or live bindings activated. |
| 206 |
# |
| 207 |
# When PyGObject is installed and active, the `gi.repository` namespace |
| 208 |
# becomes populated by introspected C-based objects that do not behave |
| 209 |
# like regular Python functions. This causes mock patching via |
| 210 |
# `mocker.patch("gi.repository.Gio.DBusProxy.new_for_bus_sync")` to |
| 211 |
# silently fail or be ignored, as Python's mocking machinery cannot |
| 212 |
# reliably override these introspected symbols. |
| 213 |
# |
| 214 |
# This test exists to ensure coverage of legacy or minimal environments |
| 215 |
# where Apprise's GLib support can still be used through soft mocks, |
| 216 |
# such as CI/CD pipelines or headless test setups where PyGObject is |
| 217 |
# absent or stubbed (as done via `enabled_glib_environment`). |
| 218 |
# |
| 219 |
# Note: In production environments with active PyGObject, exception |
| 220 |
# handling is already tested via `GLib.Error` branches or during actual |
| 221 |
# usage of `NotifyGLib.send()`. This test supplements that by simulating |
| 222 |
# the rare fallback case of a non-GLib-related exception during Notify(). |
| 223 |
import gi |
| 224 |
|
| 225 |
if hasattr(gi, "repository"): |
| 226 |
pytest.skip( |
| 227 |
"pygobject introspection active, test won't behave as expected" |
| 228 |
) |
| 229 |
|
| 230 |
fake_iface = Mock() |
| 231 |
fake_iface.Notify.side_effect = RuntimeError("boom") |
| 232 |
|
| 233 |
mocker.patch( |
| 234 |
"gi.repository.Gio.DBusProxy.new_for_bus_sync", |
| 235 |
return_value=fake_iface, |
| 236 |
) |
| 237 |
|
| 238 |
obj = apprise.Apprise.instantiate("glib://", suppress_exceptions=False) |
| 239 |
logger = mocker.spy(obj, "logger") |
| 240 |
assert bool(obj.notify("boom", title="fail")) is False |
| 241 |
logger.warning.assert_called_with("Failed to send GLib/Gio notification.") |
| 242 |
|
| 243 |
|
| 244 |
def test_plugin_glib_disabled(mocker, enabled_glib_environment): |
| 245 |
"""Test disabled plugin returns False on notify()""" |
| 246 |
obj = apprise.Apprise.instantiate("glib://", suppress_exceptions=False) |
| 247 |
obj.enabled = False |
| 248 |
assert bool(obj.notify("x")) is False |
| 249 |
|
| 250 |
|
| 251 |
def test_plugin_glib_invalid_coords(): |
| 252 |
"""Invalid x/y coordinates are rejected.""" |
| 253 |
with pytest.raises(AppriseImproperlyConfigured): |
| 254 |
NotifyGLib(x_axis="bad", y_axis="1") |
| 255 |
with pytest.raises(AppriseImproperlyConfigured): |
| 256 |
NotifyGLib(x_axis="1", y_axis="bad") |
| 257 |
|
| 258 |
|
| 259 |
def test_plugin_glib_urgency_parsing(): |
| 260 |
"""Urgency variants map correctly""" |
| 261 |
assert NotifyGLib(urgency="high").urgency == GLibUrgency.HIGH |
| 262 |
assert NotifyGLib(urgency="invalid").urgency == GLibUrgency.NORMAL |
| 263 |
assert NotifyGLib(urgency="2").urgency == GLibUrgency.HIGH |
| 264 |
assert NotifyGLib(urgency=0).urgency == GLibUrgency.LOW |
| 265 |
|
| 266 |
|
| 267 |
def test_plugin_glib_parse_url_fields(): |
| 268 |
url = "glib://_/?x=5&y=5&image=no&priority=high" |
| 269 |
result = NotifyGLib.parse_url(url) |
| 270 |
assert result["x_axis"] == "5" |
| 271 |
assert result["y_axis"] == "5" |
| 272 |
assert result["include_image"] is False |
| 273 |
assert result["urgency"] == "high" |
| 274 |
|
| 275 |
|
| 276 |
def test_plugin_glib_xy_axis_applied_to_variant(enabled_glib_environment): |
| 277 |
"""Ensure x/y values are added to GLib.Variant payload.""" |
| 278 |
obj = apprise.Apprise.instantiate( |
| 279 |
"glib://_/?x=5&y=10", suppress_exceptions=False |
| 280 |
) |
| 281 |
|
| 282 |
# Patch GLib.Variant to track calls |
| 283 |
import gi |
| 284 |
|
| 285 |
spy_variant = Mock(wraps=gi.repository.GLib.Variant) |
| 286 |
gi.repository.GLib.Variant = spy_variant |
| 287 |
|
| 288 |
assert bool(obj.notify("Test with coords", title="xy")) is True |
| 289 |
|
| 290 |
# Check x and y were added to meta_payload |
| 291 |
assert call("i", 5) in spy_variant.mock_calls |
| 292 |
assert call("i", 10) in spy_variant.mock_calls |
| 293 |
|
| 294 |
|
| 295 |
def test_plugin_glib_no_image_support(monkeypatch, enabled_glib_environment): |
| 296 |
"""Simulate GdkPixbuf unavailable""" |
| 297 |
monkeypatch.setattr( |
| 298 |
"apprise.plugins.glib.NOTIFY_GLIB_IMAGE_SUPPORT", False |
| 299 |
) |
| 300 |
obj = apprise.Apprise.instantiate("glib://", suppress_exceptions=False) |
| 301 |
assert bool(obj.notify("no image")) is True |
| 302 |
|
| 303 |
|
| 304 |
def test_plugin_glib_url_redaction(enabled_glib_environment): |
| 305 |
"""url() privacy mode redacts safely""" |
| 306 |
obj = apprise.Apprise.instantiate( |
| 307 |
"glib://_/?image=no&urgency=high", suppress_exceptions=False |
| 308 |
) |
| 309 |
url = obj.url(privacy=True) |
| 310 |
assert "image=" in url |
| 311 |
assert "urgency=" in url |
| 312 |
assert url.startswith("glib://_/") |
| 313 |
|
| 314 |
|
| 315 |
def test_plugin_glib_require_version_importerror(monkeypatch): |
| 316 |
"""Simulate gi.require_version() raising ImportError""" |
| 317 |
gi = types.ModuleType("gi") |
| 318 |
gi.require_version = Mock(side_effect=ImportError("no gio")) |
| 319 |
sys.modules["gi"] = gi |
| 320 |
reload_plugin("glib") |
| 321 |
obj = apprise.Apprise.instantiate("glib://", suppress_exceptions=False) |
| 322 |
assert not isinstance(obj, NotifyGLib) |
| 323 |
|
| 324 |
|
| 325 |
def test_plugin_glib_require_version_valueerror(monkeypatch): |
| 326 |
"""Simulate gi.require_version() raising ValueError without reload |
| 327 |
crash.""" |
| 328 |
|
| 329 |
import gi |
| 330 |
|
| 331 |
import apprise.plugins.glib as plugin_glib |
| 332 |
|
| 333 |
# Patch require_version after import |
| 334 |
monkeypatch.setattr( |
| 335 |
gi, "require_version", Mock(side_effect=ValueError("fail")) |
| 336 |
) |
| 337 |
|
| 338 |
# Re-evaluate plugin support logic manually |
| 339 |
try: |
| 340 |
gi.require_version("Gio", "2.0") |
| 341 |
|
| 342 |
except Exception: |
| 343 |
plugin_glib.NOTIFY_GLIB_SUPPORT_ENABLED = False |
| 344 |
plugin_glib.NotifyGLib.enabled = False |
| 345 |
|
| 346 |
# Confirm plugin is now marked disabled |
| 347 |
assert not plugin_glib.NotifyGLib.enabled |
| 348 |
|
| 349 |
# Apprise will skip this plugin |
| 350 |
obj = apprise.Apprise.instantiate("glib://", suppress_exceptions=False) |
| 351 |
assert not isinstance(obj, plugin_glib.NotifyGLib) |
| 352 |
|
| 353 |
|
| 354 |
def test_plugin_glib_gdkpixbuf_require_version_valueerror(monkeypatch): |
| 355 |
"""Simulate gi.require_version('GdkPixbuf', ...) raising ValueError""" |
| 356 |
|
| 357 |
# Step 1: Mock GI |
| 358 |
gi = types.ModuleType("gi") |
| 359 |
gi.repository = types.ModuleType("gi.repository") |
| 360 |
|
| 361 |
def fake_require_version(name: str, version: str) -> None: |
| 362 |
if name == "GdkPixbuf": |
| 363 |
raise ValueError("GdkPixbuf unavailable") |
| 364 |
|
| 365 |
gi.require_version = Mock(side_effect=fake_require_version) |
| 366 |
|
| 367 |
# Step 2: Patch into sys.modules |
| 368 |
sys.modules["gi"] = gi |
| 369 |
sys.modules["gi.repository"] = gi.repository |
| 370 |
|
| 371 |
# Step 3: Reload plugin to trigger branch |
| 372 |
reload_plugin("glib") |
| 373 |
|
| 374 |
# Step 4: Confirm GdkPixbuf image support was not enabled |
| 375 |
from apprise.plugins import glib as plugin_glib |
| 376 |
|
| 377 |
assert plugin_glib.NOTIFY_GLIB_IMAGE_SUPPORT is False |
| 378 |
|
| 379 |
|
| 380 |
def test_plugin_glib_notify_generic_exception( |
| 381 |
mocker, enabled_glib_environment |
| 382 |
): |
| 383 |
""" |
| 384 |
Test that a generic exception occurring during the notification send |
| 385 |
is caught and handled (returning False). |
| 386 |
""" |
| 387 |
import gi |
| 388 |
|
| 389 |
# 1. Create a mock object to act as the DBus Proxy |
| 390 |
mock_proxy = mocker.Mock() |
| 391 |
|
| 392 |
# 2. Force the Notify method to raise a generic Exception |
| 393 |
# This triggers the 'except Exception as e:' |
| 394 |
mock_proxy.Notify.side_effect = Exception("Simulated Generic Failure") |
| 395 |
|
| 396 |
# 3. Patch the Gio constructor to return our crashing mock |
| 397 |
# The code calls: Gio.DBusProxy.new_for_bus_sync(...) |
| 398 |
gi.repository.Gio.DBusProxy.new_for_bus_sync.return_value = mock_proxy |
| 399 |
|
| 400 |
# 4. Instantiate the plugin |
| 401 |
from apprise.plugins.glib import NotifyGLib |
| 402 |
|
| 403 |
obj = NotifyGLib(targets=["glib://"]) |
| 404 |
|
| 405 |
# 5. Spy on the logger to ensure the warning is logged |
| 406 |
logger_spy = mocker.spy(obj, "logger") |
| 407 |
|
| 408 |
# 6. Execute notify() |
| 409 |
# It should catch the exception and return False |
| 410 |
assert bool(obj.notify(title="Title", body="Body")) is False |
| 411 |
|
| 412 |
# 7. Verify the specific log message from glib.py |
| 413 |
logger_spy.warning.assert_called_with( |
| 414 |
"Failed to send GLib/Gio notification." |
| 415 |
) |