| 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 |
import logging |
| 29 |
import os |
| 30 |
import sys |
| 31 |
from unittest.mock import Mock |
| 32 |
|
| 33 |
from helpers import reload_plugin |
| 34 |
import pytest |
| 35 |
|
| 36 |
import apprise |
| 37 |
from apprise.plugins.macosx import NotifyMacOSX |
| 38 |
|
| 39 |
|
| 40 |
logging.disable(logging.CRITICAL) |
| 41 |
|
| 42 |
|
| 43 |
if sys.platform not in ["darwin", "linux"]: |
| 44 |
pytest.skip( |
| 45 |
"Only makes sense on macOS, but testable in Linux", |
| 46 |
allow_module_level=True, |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
@pytest.fixture |
| 51 |
def pretend_macos(mocker): |
| 52 |
"""Fixture to simulate a macOS environment.""" |
| 53 |
mocker.patch("platform.system", return_value="Darwin") |
| 54 |
mocker.patch("platform.mac_ver", return_value=("10.8", ("", "", ""), "")) |
| 55 |
|
| 56 |
|
| 57 |
reload_plugin("macosx") |
| 58 |
|
| 59 |
|
| 60 |
@pytest.fixture |
| 61 |
def terminal_notifier(mocker, tmp_path): |
| 62 |
"""Fixture for providing a surrogate for the `terminal-notifier` |
| 63 |
program.""" |
| 64 |
notifier_program = tmp_path.joinpath("terminal-notifier") |
| 65 |
notifier_program.write_text("#!/bin/sh\n\necho hello") |
| 66 |
|
| 67 |
|
| 68 |
os.chmod(notifier_program, 0o755) |
| 69 |
|
| 70 |
|
| 71 |
mocker.patch( |
| 72 |
"apprise.plugins.macosx.NotifyMacOSX.notify_paths", |
| 73 |
(str(notifier_program),), |
| 74 |
) |
| 75 |
|
| 76 |
yield notifier_program |
| 77 |
|
| 78 |
|
| 79 |
@pytest.fixture |
| 80 |
def macos_notify_environment(pretend_macos, terminal_notifier): |
| 81 |
"""Fixture to bundle general test case setup. |
| 82 |
|
| 83 |
Use this fixture if you don't need access to the individual members. |
| 84 |
""" |
| 85 |
pass |
| 86 |
|
| 87 |
|
| 88 |
def test_plugin_macosx_general_success(macos_notify_environment): |
| 89 |
"""NotifyMacOSX() general checks.""" |
| 90 |
|
| 91 |
|
| 92 |
obj = apprise.Apprise.instantiate( |
| 93 |
"macosx://_/?image=True", suppress_exceptions=False |
| 94 |
) |
| 95 |
assert isinstance(obj, NotifyMacOSX) is True |
| 96 |
|
| 97 |
|
| 98 |
assert isinstance(obj.url(), str) is True |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
assert obj.url_id() is None |
| 104 |
|
| 105 |
|
| 106 |
assert ( |
| 107 |
obj.notify( |
| 108 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 109 |
) |
| 110 |
is True |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
assert ( |
| 115 |
obj.notify(title="", body="body", notify_type=apprise.NotifyType.INFO) |
| 116 |
is True |
| 117 |
) |
| 118 |
|
| 119 |
obj = apprise.Apprise.instantiate( |
| 120 |
"macosx://_/?image=True", suppress_exceptions=False |
| 121 |
) |
| 122 |
assert isinstance(obj, NotifyMacOSX) is True |
| 123 |
assert ( |
| 124 |
obj.notify( |
| 125 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 126 |
) |
| 127 |
is True |
| 128 |
) |
| 129 |
|
| 130 |
obj = apprise.Apprise.instantiate( |
| 131 |
"macosx://_/?image=False", suppress_exceptions=False |
| 132 |
) |
| 133 |
assert isinstance(obj, NotifyMacOSX) is True |
| 134 |
assert isinstance(obj.url(), str) is True |
| 135 |
assert ( |
| 136 |
obj.notify( |
| 137 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 138 |
) |
| 139 |
is True |
| 140 |
) |
| 141 |
|
| 142 |
|
| 143 |
obj = apprise.Apprise.instantiate( |
| 144 |
"macosx://_/?sound=default", suppress_exceptions=False |
| 145 |
) |
| 146 |
assert isinstance(obj, NotifyMacOSX) is True |
| 147 |
assert obj.sound == "default" |
| 148 |
assert isinstance(obj.url(), str) is True |
| 149 |
assert ( |
| 150 |
obj.notify( |
| 151 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 152 |
) |
| 153 |
is True |
| 154 |
) |
| 155 |
|
| 156 |
|
| 157 |
obj = apprise.Apprise.instantiate( |
| 158 |
"macosx://_/?click=http://google.com", suppress_exceptions=False |
| 159 |
) |
| 160 |
assert isinstance(obj, NotifyMacOSX) is True |
| 161 |
assert obj.click == "http://google.com" |
| 162 |
assert isinstance(obj.url(), str) is True |
| 163 |
assert ( |
| 164 |
obj.notify( |
| 165 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 166 |
) |
| 167 |
is True |
| 168 |
) |
| 169 |
|
| 170 |
|
| 171 |
def test_plugin_macosx_terminal_notifier_not_executable( |
| 172 |
pretend_macos, terminal_notifier |
| 173 |
): |
| 174 |
"""When the `terminal-notifier` program is inaccessible or not executable, |
| 175 |
we are unable to send notifications.""" |
| 176 |
|
| 177 |
obj = apprise.Apprise.instantiate("macosx://", suppress_exceptions=False) |
| 178 |
|
| 179 |
|
| 180 |
os.chmod(terminal_notifier, 0o644) |
| 181 |
|
| 182 |
assert ( |
| 183 |
obj.notify( |
| 184 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 185 |
) |
| 186 |
is False |
| 187 |
) |
| 188 |
|
| 189 |
|
| 190 |
def test_plugin_macosx_terminal_notifier_invalid(macos_notify_environment): |
| 191 |
"""When the `terminal-notifier` program is wrongly addressed, notifications |
| 192 |
should fail.""" |
| 193 |
|
| 194 |
obj = apprise.Apprise.instantiate("macosx://", suppress_exceptions=False) |
| 195 |
|
| 196 |
|
| 197 |
obj.notify_path = "invalid_missing-file" |
| 198 |
assert not os.path.isfile(obj.notify_path) |
| 199 |
|
| 200 |
assert ( |
| 201 |
obj.notify( |
| 202 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 203 |
) |
| 204 |
is False |
| 205 |
) |
| 206 |
|
| 207 |
|
| 208 |
def test_plugin_macosx_terminal_notifier_croaks( |
| 209 |
mocker, macos_notify_environment |
| 210 |
): |
| 211 |
"""When the `terminal-notifier` program croaks on execution, notifications |
| 212 |
should fail.""" |
| 213 |
|
| 214 |
|
| 215 |
mocker.patch("subprocess.Popen", return_value=Mock(returncode=1)) |
| 216 |
|
| 217 |
obj = apprise.Apprise.instantiate("macosx://", suppress_exceptions=False) |
| 218 |
assert isinstance(obj, NotifyMacOSX) is True |
| 219 |
assert ( |
| 220 |
obj.notify( |
| 221 |
title="title", body="body", notify_type=apprise.NotifyType.INFO |
| 222 |
) |
| 223 |
is False |
| 224 |
) |
| 225 |
|
| 226 |
|
| 227 |
def test_plugin_macosx_pretend_linux(mocker, pretend_macos): |
| 228 |
"""The notification object is disabled when pretending to run on Linux.""" |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
mocker.patch("platform.system", return_value="Linux") |
| 233 |
reload_plugin("macosx") |
| 234 |
|
| 235 |
|
| 236 |
obj = apprise.Apprise.instantiate("macosx://", suppress_exceptions=False) |
| 237 |
assert obj is None |
| 238 |
|
| 239 |
|
| 240 |
@pytest.mark.parametrize("macos_version", ["9.12", "10.7"]) |
| 241 |
def test_plugin_macosx_pretend_old_macos(mocker, macos_version): |
| 242 |
"""The notification object is disabled when pretending to run on older |
| 243 |
macOS.""" |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
mocker.patch( |
| 248 |
"platform.mac_ver", return_value=(macos_version, ("", "", ""), "") |
| 249 |
) |
| 250 |
reload_plugin("macosx") |
| 251 |
|
| 252 |
obj = apprise.Apprise.instantiate("macosx://", suppress_exceptions=False) |
| 253 |
assert obj is None |