| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
from importlib import reload |
| 29 |
|
| 30 |
|
| 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 |
|
| 56 |
win32api = types.ModuleType("win32api") |
| 57 |
win32api.GetModuleHandle = mock.Mock(name="win32api.GetModuleHandle") |
| 58 |
win32api.PostQuitMessage = mock.Mock(name="win32api.PostQuitMessage") |
| 59 |
|
| 60 |
|
| 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 |
|
| 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
saved_apprise = { |
| 96 |
k: v for k, v in sys.modules.items() if k.startswith("apprise.") |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 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 |
|
| 109 |
yield win32gui |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
for mod in [k for k in sys.modules if k.startswith("apprise.")]: |
| 114 |
del sys.modules[mod] |
| 115 |
|
| 116 |
|
| 117 |
for stub in ("win32api", "win32con", "win32gui"): |
| 118 |
sys.modules.pop(stub, None) |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
sys.modules.update(saved_apprise) |
| 124 |
|
| 125 |
|
| 126 |
@pytest.mark.skipif( |
| 127 |
( |
| 128 |
"win32api" in sys.modules |
| 129 |
or "win32con" in sys.modules |
| 130 |
or "win32gui" in sys.modules |
| 131 |
), |
| 132 |
reason="Requires non-windows platform", |
| 133 |
) |
| 134 |
def test_plugin_windows_mocked(mocked_win32_environment): |
| 135 |
"""NotifyWindows() General Checks (via non-Windows platform)""" |
| 136 |
|
| 137 |
|
| 138 |
win32gui = mocked_win32_environment |
| 139 |
|
| 140 |
|
| 141 |
obj = apprise.Apprise.instantiate("windows://", suppress_exceptions=False) |
| 142 |
obj.duration = 0 |
| 143 |
|
| 144 |
|
| 145 |
assert isinstance(obj.url(), str) |
| 146 |
|
| 147 |
|
| 148 |
assert obj.url_id() is None |
| 149 |
|
| 150 |
|
| 151 |
assert obj.enabled is True |
| 152 |
|
| 153 |
|
| 154 |
obj._on_destroy(0, "", 0, 0) |
| 155 |
|
| 156 |
|
| 157 |
assert ( |
| 158 |
obj.notify( |
| 159 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 160 |
) |
| 161 |
is True |
| 162 |
) |
| 163 |
|
| 164 |
obj = apprise.Apprise.instantiate( |
| 165 |
"windows://_/?image=True", suppress_exceptions=False |
| 166 |
) |
| 167 |
obj.duration = 0 |
| 168 |
assert isinstance(obj.url(), str) |
| 169 |
assert ( |
| 170 |
obj.notify( |
| 171 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 172 |
) |
| 173 |
is True |
| 174 |
) |
| 175 |
|
| 176 |
obj = apprise.Apprise.instantiate( |
| 177 |
"windows://_/?image=False", suppress_exceptions=False |
| 178 |
) |
| 179 |
obj.duration = 0 |
| 180 |
assert isinstance(obj.url(), str) |
| 181 |
assert ( |
| 182 |
obj.notify( |
| 183 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 184 |
) |
| 185 |
is True |
| 186 |
) |
| 187 |
|
| 188 |
obj = apprise.Apprise.instantiate( |
| 189 |
"windows://_/?duration=1", suppress_exceptions=False |
| 190 |
) |
| 191 |
assert isinstance(obj.url(), str) |
| 192 |
|
| 193 |
assert obj.duration == 1 |
| 194 |
assert ( |
| 195 |
obj.notify( |
| 196 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 197 |
) |
| 198 |
is True |
| 199 |
) |
| 200 |
|
| 201 |
obj = apprise.Apprise.instantiate( |
| 202 |
"windows://_/?duration=invalid", suppress_exceptions=False |
| 203 |
) |
| 204 |
|
| 205 |
assert obj.duration == obj.default_popup_duration_sec |
| 206 |
|
| 207 |
obj = apprise.Apprise.instantiate( |
| 208 |
"windows://_/?duration=-1", suppress_exceptions=False |
| 209 |
) |
| 210 |
|
| 211 |
assert obj.duration == obj.default_popup_duration_sec |
| 212 |
|
| 213 |
obj = apprise.Apprise.instantiate( |
| 214 |
"windows://_/?duration=0", suppress_exceptions=False |
| 215 |
) |
| 216 |
|
| 217 |
assert obj.duration == obj.default_popup_duration_sec |
| 218 |
|
| 219 |
|
| 220 |
obj.duration = 0 |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
win32gui.LoadImage.side_effect = AttributeError |
| 225 |
assert ( |
| 226 |
obj.notify( |
| 227 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 228 |
) |
| 229 |
is True |
| 230 |
) |
| 231 |
|
| 232 |
win32gui.LoadImage.side_effect = None |
| 233 |
|
| 234 |
|
| 235 |
win32gui.UpdateWindow.side_effect = AttributeError |
| 236 |
assert ( |
| 237 |
obj.notify( |
| 238 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 239 |
) |
| 240 |
is False |
| 241 |
) |
| 242 |
|
| 243 |
win32gui.UpdateWindow.side_effect = None |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
obj.enabled = False |
| 248 |
assert ( |
| 249 |
obj.notify( |
| 250 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 251 |
) |
| 252 |
is False |
| 253 |
) |
| 254 |
|
| 255 |
|
| 256 |
@pytest.mark.skipif( |
| 257 |
"win32api" not in sys.modules |
| 258 |
and "win32con" not in sys.modules |
| 259 |
and "win32gui" not in sys.modules, |
| 260 |
reason="Requires win32api, win32con, and win32gui", |
| 261 |
) |
| 262 |
@mock.patch("win32gui.UpdateWindow") |
| 263 |
@mock.patch("win32gui.Shell_NotifyIcon") |
| 264 |
@mock.patch("win32gui.LoadImage") |
| 265 |
def test_plugin_windows_native( |
| 266 |
mock_loadimage, mock_notify, mock_update_window |
| 267 |
): |
| 268 |
"""NotifyWindows() General Checks (via Windows platform)""" |
| 269 |
|
| 270 |
|
| 271 |
obj = apprise.Apprise.instantiate("windows://", suppress_exceptions=False) |
| 272 |
obj.duration = 0 |
| 273 |
|
| 274 |
|
| 275 |
assert isinstance(obj.url(), str) |
| 276 |
|
| 277 |
|
| 278 |
assert obj.enabled is True |
| 279 |
|
| 280 |
|
| 281 |
obj._on_destroy(0, "", 0, 0) |
| 282 |
|
| 283 |
|
| 284 |
assert ( |
| 285 |
obj.notify( |
| 286 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 287 |
) |
| 288 |
is True |
| 289 |
) |
| 290 |
|
| 291 |
obj = apprise.Apprise.instantiate( |
| 292 |
"windows://_/?image=True", suppress_exceptions=False |
| 293 |
) |
| 294 |
obj.duration = 0 |
| 295 |
assert isinstance(obj.url(), str) |
| 296 |
assert ( |
| 297 |
obj.notify( |
| 298 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 299 |
) |
| 300 |
is True |
| 301 |
) |
| 302 |
|
| 303 |
obj = apprise.Apprise.instantiate( |
| 304 |
"windows://_/?image=False", suppress_exceptions=False |
| 305 |
) |
| 306 |
obj.duration = 0 |
| 307 |
assert isinstance(obj.url(), str) |
| 308 |
assert ( |
| 309 |
obj.notify( |
| 310 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 311 |
) |
| 312 |
is True |
| 313 |
) |
| 314 |
|
| 315 |
obj = apprise.Apprise.instantiate( |
| 316 |
"windows://_/?duration=1", suppress_exceptions=False |
| 317 |
) |
| 318 |
assert isinstance(obj.url(), str) |
| 319 |
assert ( |
| 320 |
obj.notify( |
| 321 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 322 |
) |
| 323 |
is True |
| 324 |
) |
| 325 |
|
| 326 |
assert obj.duration == 1 |
| 327 |
|
| 328 |
obj = apprise.Apprise.instantiate( |
| 329 |
"windows://_/?duration=invalid", suppress_exceptions=False |
| 330 |
) |
| 331 |
|
| 332 |
assert obj.duration == obj.default_popup_duration_sec |
| 333 |
|
| 334 |
obj = apprise.Apprise.instantiate( |
| 335 |
"windows://_/?duration=-1", suppress_exceptions=False |
| 336 |
) |
| 337 |
|
| 338 |
assert obj.duration == obj.default_popup_duration_sec |
| 339 |
|
| 340 |
obj = apprise.Apprise.instantiate( |
| 341 |
"windows://_/?duration=0", suppress_exceptions=False |
| 342 |
) |
| 343 |
|
| 344 |
assert obj.duration == obj.default_popup_duration_sec |
| 345 |
|
| 346 |
|
| 347 |
obj = apprise.Apprise.instantiate("windows://", suppress_exceptions=False) |
| 348 |
obj.duration = 0 |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
mock_loadimage.side_effect = AttributeError |
| 353 |
assert ( |
| 354 |
obj.notify( |
| 355 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 356 |
) |
| 357 |
is True |
| 358 |
) |
| 359 |
|
| 360 |
mock_loadimage.side_effect = None |
| 361 |
|
| 362 |
|
| 363 |
mock_update_window.side_effect = AttributeError |
| 364 |
assert ( |
| 365 |
obj.notify( |
| 366 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 367 |
) |
| 368 |
is False |
| 369 |
) |
| 370 |
|
| 371 |
mock_update_window.side_effect = None |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
obj.enabled = False |
| 376 |
assert ( |
| 377 |
obj.notify( |
| 378 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 379 |
) |
| 380 |
is False |
| 381 |
) |