| 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 |
from importlib import reload |
| 29 |
|
| 30 |
# Disable logging for a cleaner testing output |
| 31 |
import logging |
| 32 |
import sys |
| 33 |
import types |
| 34 |
from unittest import mock |
| 35 |
|
| 36 |
import pytest |
| 37 |
|
| 38 |
import apprise |
| 39 |
|
| 40 |
logging.disable(logging.CRITICAL) |
| 41 |
|
| 42 |
|
| 43 |
@pytest.fixture |
| 44 |
def mocked_win32_environment(): |
| 45 |
"""Inject fake win32 modules, reload apprise, and fully restore |
| 46 |
sys.modules on teardown. |
| 47 |
|
| 48 |
Previously the setup lived inside test_plugin_windows_mocked itself, |
| 49 |
with no cleanup. That permanently abandoned all apprise.* module |
| 50 |
objects, causing order-dependent failures in every test that runs |
| 51 |
afterwards and holds a module-level `from apprise.plugins.X import` |
| 52 |
reference. A fixture with a yield guarantees cleanup even on failure. |
| 53 |
""" |
| 54 |
|
| 55 |
# Build fake win32api |
| 56 |
win32api = types.ModuleType("win32api") |
| 57 |
win32api.GetModuleHandle = mock.Mock(name="win32api.GetModuleHandle") |
| 58 |
win32api.PostQuitMessage = mock.Mock(name="win32api.PostQuitMessage") |
| 59 |
|
| 60 |
# Build fake win32con |
| 61 |
win32con = types.ModuleType("win32con") |
| 62 |
win32con.CW_USEDEFAULT = mock.Mock(name="win32con.CW_USEDEFAULT") |
| 63 |
win32con.IDI_APPLICATION = mock.Mock(name="win32con.IDI_APPLICATION") |
| 64 |
win32con.IMAGE_ICON = mock.Mock(name="win32con.IMAGE_ICON") |
| 65 |
win32con.LR_DEFAULTSIZE = 1 |
| 66 |
win32con.LR_LOADFROMFILE = 2 |
| 67 |
win32con.WM_DESTROY = mock.Mock(name="win32con.WM_DESTROY") |
| 68 |
win32con.WM_USER = 0 |
| 69 |
win32con.WS_OVERLAPPED = 1 |
| 70 |
win32con.WS_SYSMENU = 2 |
| 71 |
|
| 72 |
# Build fake win32gui |
| 73 |
win32gui = types.ModuleType("win32gui") |
| 74 |
win32gui.CreateWindow = mock.Mock(name="win32gui.CreateWindow") |
| 75 |
win32gui.DestroyWindow = mock.Mock(name="win32gui.DestroyWindow") |
| 76 |
win32gui.LoadIcon = mock.Mock(name="win32gui.LoadIcon") |
| 77 |
win32gui.LoadImage = mock.Mock(name="win32gui.LoadImage") |
| 78 |
win32gui.NIF_ICON = 1 |
| 79 |
win32gui.NIF_INFO = mock.Mock(name="win32gui.NIF_INFO") |
| 80 |
win32gui.NIF_MESSAGE = 2 |
| 81 |
win32gui.NIF_TIP = 4 |
| 82 |
win32gui.NIM_ADD = mock.Mock(name="win32gui.NIM_ADD") |
| 83 |
win32gui.NIM_DELETE = mock.Mock(name="win32gui.NIM_DELETE") |
| 84 |
win32gui.NIM_MODIFY = mock.Mock(name="win32gui.NIM_MODIFY") |
| 85 |
win32gui.RegisterClass = mock.Mock(name="win32gui.RegisterClass") |
| 86 |
win32gui.UnregisterClass = mock.Mock(name="win32gui.UnregisterClass") |
| 87 |
win32gui.Shell_NotifyIcon = mock.Mock(name="win32gui.Shell_NotifyIcon") |
| 88 |
win32gui.UpdateWindow = mock.Mock(name="win32gui.UpdateWindow") |
| 89 |
win32gui.WNDCLASS = mock.Mock(name="win32gui.WNDCLASS") |
| 90 |
|
| 91 |
# Save all apprise.* module references before wiping them. |
| 92 |
# These are the same module objects whose __dict__ entries are |
| 93 |
# pointed to by module-level imports in other test files; restoring |
| 94 |
# them keeps those references valid after the test completes. |
| 95 |
saved_apprise = { |
| 96 |
k: v for k, v in sys.modules.items() if k.startswith("apprise.") |
| 97 |
} |
| 98 |
|
| 99 |
# Inject win32 stubs so the plugin's top-level import succeeds, |
| 100 |
# then wipe apprise.* and reload so it picks them up. |
| 101 |
sys.modules["win32api"] = win32api |
| 102 |
sys.modules["win32con"] = win32con |
| 103 |
sys.modules["win32gui"] = win32gui |
| 104 |
for mod in list(saved_apprise): |
| 105 |
del sys.modules[mod] |
| 106 |
reload(apprise) |
| 107 |
|
| 108 |
# Yield win32gui -- the test uses it to configure side-effects |
| 109 |
yield win32gui |
| 110 |
|
| 111 |
# --- Teardown --- |
| 112 |
# Remove all apprise.* entries created during this test |
| 113 |
for mod in [k for k in sys.modules if k.startswith("apprise.")]: |
| 114 |
del sys.modules[mod] |
| 115 |
|
| 116 |
# Remove the injected win32 stubs |
| 117 |
for stub in ("win32api", "win32con", "win32gui"): |
| 118 |
sys.modules.pop(stub, None) |
| 119 |
|
| 120 |
# Restore the original apprise.* module objects so that any |
| 121 |
# module-level `from apprise.plugins.X import NotifyX` in other |
| 122 |
# test files continues to point at live (non-abandoned) module dicts. |
| 123 |
sys.modules.update(saved_apprise) |
| 124 |
|
| 125 |
# sys.modules.update() above only fixes the dict *keys* -- Python's |
| 126 |
# import machinery separately sets each submodule as an ATTRIBUTE on |
| 127 |
# its parent package too (e.g. sys.modules["apprise"].apprise), and |
| 128 |
# that binding is untouched by the update. Left stale, any later |
| 129 |
# `import apprise.apprise as x` anywhere else in the test session -- |
| 130 |
# resolved via getattr(sys.modules["apprise"], "apprise"), not a |
| 131 |
# sys.modules lookup -- would silently get the module object this |
| 132 |
# fixture created and already discarded above, rather than the real, |
| 133 |
# restored one. |
| 134 |
for name, mod in saved_apprise.items(): |
| 135 |
parent_name, _, attr = name.rpartition(".") |
| 136 |
parent = sys.modules.get(parent_name) |
| 137 |
if parent is not None: |
| 138 |
setattr(parent, attr, mod) |
| 139 |
|
| 140 |
|
| 141 |
@pytest.mark.skipif( |
| 142 |
( |
| 143 |
"win32api" in sys.modules |
| 144 |
or "win32con" in sys.modules |
| 145 |
or "win32gui" in sys.modules |
| 146 |
), |
| 147 |
reason="Requires non-windows platform", |
| 148 |
) |
| 149 |
def test_plugin_windows_mocked(mocked_win32_environment): |
| 150 |
"""NotifyWindows() General Checks (via non-Windows platform)""" |
| 151 |
|
| 152 |
# Retrieve the win32gui mock that the fixture set up |
| 153 |
win32gui = mocked_win32_environment |
| 154 |
|
| 155 |
# Create our instance |
| 156 |
obj = apprise.Apprise.instantiate("windows://", suppress_exceptions=False) |
| 157 |
obj.duration = 0 |
| 158 |
|
| 159 |
# Test URL functionality |
| 160 |
assert isinstance(obj.url(), str) |
| 161 |
|
| 162 |
# Verify that a URL ID can not be generated |
| 163 |
assert obj.url_id() is None |
| 164 |
|
| 165 |
# Check that it found our mocked environments |
| 166 |
assert obj.enabled is True |
| 167 |
|
| 168 |
# _on_destroy check |
| 169 |
obj._on_destroy(0, "", 0, 0) |
| 170 |
|
| 171 |
# test notifications |
| 172 |
assert ( |
| 173 |
bool( |
| 174 |
obj.notify( |
| 175 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 176 |
) |
| 177 |
) |
| 178 |
is True |
| 179 |
) |
| 180 |
|
| 181 |
obj = apprise.Apprise.instantiate( |
| 182 |
"windows://_/?image=True", suppress_exceptions=False |
| 183 |
) |
| 184 |
obj.duration = 0 |
| 185 |
assert isinstance(obj.url(), str) |
| 186 |
assert ( |
| 187 |
bool( |
| 188 |
obj.notify( |
| 189 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 190 |
) |
| 191 |
) |
| 192 |
is True |
| 193 |
) |
| 194 |
|
| 195 |
obj = apprise.Apprise.instantiate( |
| 196 |
"windows://_/?image=False", suppress_exceptions=False |
| 197 |
) |
| 198 |
obj.duration = 0 |
| 199 |
assert isinstance(obj.url(), str) |
| 200 |
assert ( |
| 201 |
bool( |
| 202 |
obj.notify( |
| 203 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 204 |
) |
| 205 |
) |
| 206 |
is True |
| 207 |
) |
| 208 |
|
| 209 |
obj = apprise.Apprise.instantiate( |
| 210 |
"windows://_/?duration=1", suppress_exceptions=False |
| 211 |
) |
| 212 |
assert isinstance(obj.url(), str) |
| 213 |
# loads okay |
| 214 |
assert obj.duration == 1 |
| 215 |
assert ( |
| 216 |
bool( |
| 217 |
obj.notify( |
| 218 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 219 |
) |
| 220 |
) |
| 221 |
is True |
| 222 |
) |
| 223 |
|
| 224 |
obj = apprise.Apprise.instantiate( |
| 225 |
"windows://_/?duration=invalid", suppress_exceptions=False |
| 226 |
) |
| 227 |
# Falls back to default |
| 228 |
assert obj.duration == obj.default_popup_duration_sec |
| 229 |
|
| 230 |
obj = apprise.Apprise.instantiate( |
| 231 |
"windows://_/?duration=-1", suppress_exceptions=False |
| 232 |
) |
| 233 |
# Falls back to default |
| 234 |
assert obj.duration == obj.default_popup_duration_sec |
| 235 |
|
| 236 |
obj = apprise.Apprise.instantiate( |
| 237 |
"windows://_/?duration=0", suppress_exceptions=False |
| 238 |
) |
| 239 |
# Falls back to default |
| 240 |
assert obj.duration == obj.default_popup_duration_sec |
| 241 |
|
| 242 |
# To avoid slowdowns (for testing), turn it to zero for now |
| 243 |
obj.duration = 0 |
| 244 |
|
| 245 |
# Test our loading of our icon exception; it will still allow the |
| 246 |
# notification to be sent |
| 247 |
win32gui.LoadImage.side_effect = AttributeError |
| 248 |
assert ( |
| 249 |
bool( |
| 250 |
obj.notify( |
| 251 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 252 |
) |
| 253 |
) |
| 254 |
is True |
| 255 |
) |
| 256 |
# Undo our change |
| 257 |
win32gui.LoadImage.side_effect = None |
| 258 |
|
| 259 |
# Test our global exception handling |
| 260 |
win32gui.UpdateWindow.side_effect = AttributeError |
| 261 |
assert ( |
| 262 |
bool( |
| 263 |
obj.notify( |
| 264 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 265 |
) |
| 266 |
) |
| 267 |
is False |
| 268 |
) |
| 269 |
# Undo our change |
| 270 |
win32gui.UpdateWindow.side_effect = None |
| 271 |
|
| 272 |
# Toggle our testing for when we can't send notifications because the |
| 273 |
# package has been made unavailable to us |
| 274 |
obj.enabled = False |
| 275 |
assert ( |
| 276 |
bool( |
| 277 |
obj.notify( |
| 278 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 279 |
) |
| 280 |
) |
| 281 |
is False |
| 282 |
) |
| 283 |
|
| 284 |
|
| 285 |
@pytest.mark.skipif( |
| 286 |
"win32api" not in sys.modules |
| 287 |
and "win32con" not in sys.modules |
| 288 |
and "win32gui" not in sys.modules, |
| 289 |
reason="Requires win32api, win32con, and win32gui", |
| 290 |
) |
| 291 |
@mock.patch("win32gui.UpdateWindow") |
| 292 |
@mock.patch("win32gui.Shell_NotifyIcon") |
| 293 |
@mock.patch("win32gui.LoadImage") |
| 294 |
def test_plugin_windows_native( |
| 295 |
mock_loadimage, mock_notify, mock_update_window |
| 296 |
): |
| 297 |
"""NotifyWindows() General Checks (via Windows platform)""" |
| 298 |
|
| 299 |
# Create our instance |
| 300 |
obj = apprise.Apprise.instantiate("windows://", suppress_exceptions=False) |
| 301 |
obj.duration = 0 |
| 302 |
|
| 303 |
# Test URL functionality |
| 304 |
assert isinstance(obj.url(), str) |
| 305 |
|
| 306 |
# Check that it found our mocked environments |
| 307 |
assert obj.enabled is True |
| 308 |
|
| 309 |
# _on_destroy check |
| 310 |
obj._on_destroy(0, "", 0, 0) |
| 311 |
|
| 312 |
# test notifications |
| 313 |
assert ( |
| 314 |
bool( |
| 315 |
obj.notify( |
| 316 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 317 |
) |
| 318 |
) |
| 319 |
is True |
| 320 |
) |
| 321 |
|
| 322 |
obj = apprise.Apprise.instantiate( |
| 323 |
"windows://_/?image=True", suppress_exceptions=False |
| 324 |
) |
| 325 |
obj.duration = 0 |
| 326 |
assert isinstance(obj.url(), str) |
| 327 |
assert ( |
| 328 |
bool( |
| 329 |
obj.notify( |
| 330 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 331 |
) |
| 332 |
) |
| 333 |
is True |
| 334 |
) |
| 335 |
|
| 336 |
obj = apprise.Apprise.instantiate( |
| 337 |
"windows://_/?image=False", suppress_exceptions=False |
| 338 |
) |
| 339 |
obj.duration = 0 |
| 340 |
assert isinstance(obj.url(), str) |
| 341 |
assert ( |
| 342 |
bool( |
| 343 |
obj.notify( |
| 344 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 345 |
) |
| 346 |
) |
| 347 |
is True |
| 348 |
) |
| 349 |
|
| 350 |
obj = apprise.Apprise.instantiate( |
| 351 |
"windows://_/?duration=1", suppress_exceptions=False |
| 352 |
) |
| 353 |
assert isinstance(obj.url(), str) |
| 354 |
assert ( |
| 355 |
bool( |
| 356 |
obj.notify( |
| 357 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 358 |
) |
| 359 |
) |
| 360 |
is True |
| 361 |
) |
| 362 |
# loads okay |
| 363 |
assert obj.duration == 1 |
| 364 |
|
| 365 |
obj = apprise.Apprise.instantiate( |
| 366 |
"windows://_/?duration=invalid", suppress_exceptions=False |
| 367 |
) |
| 368 |
# Falls back to default |
| 369 |
assert obj.duration == obj.default_popup_duration_sec |
| 370 |
|
| 371 |
obj = apprise.Apprise.instantiate( |
| 372 |
"windows://_/?duration=-1", suppress_exceptions=False |
| 373 |
) |
| 374 |
# Falls back to default |
| 375 |
assert obj.duration == obj.default_popup_duration_sec |
| 376 |
|
| 377 |
obj = apprise.Apprise.instantiate( |
| 378 |
"windows://_/?duration=0", suppress_exceptions=False |
| 379 |
) |
| 380 |
# Falls back to default |
| 381 |
assert obj.duration == obj.default_popup_duration_sec |
| 382 |
|
| 383 |
# To avoid slowdowns (for testing), turn it to zero for now |
| 384 |
obj = apprise.Apprise.instantiate("windows://", suppress_exceptions=False) |
| 385 |
obj.duration = 0 |
| 386 |
|
| 387 |
# Test our loading of our icon exception; it will still allow the |
| 388 |
# notification to be sent |
| 389 |
mock_loadimage.side_effect = AttributeError |
| 390 |
assert ( |
| 391 |
bool( |
| 392 |
obj.notify( |
| 393 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 394 |
) |
| 395 |
) |
| 396 |
is True |
| 397 |
) |
| 398 |
# Undo our change |
| 399 |
mock_loadimage.side_effect = None |
| 400 |
|
| 401 |
# Test our global exception handling |
| 402 |
mock_update_window.side_effect = AttributeError |
| 403 |
assert ( |
| 404 |
bool( |
| 405 |
obj.notify( |
| 406 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 407 |
) |
| 408 |
) |
| 409 |
is False |
| 410 |
) |
| 411 |
# Undo our change |
| 412 |
mock_update_window.side_effect = None |
| 413 |
|
| 414 |
# Toggle our testing for when we can't send notifications because the |
| 415 |
# package has been made unavailable to us |
| 416 |
obj.enabled = False |
| 417 |
assert ( |
| 418 |
bool( |
| 419 |
obj.notify( |
| 420 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 421 |
) |
| 422 |
) |
| 423 |
is False |
| 424 |
) |