Debian Codegraph @

tests/test_plugin_macosx.py

raw symbols SCIP
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 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 # Disable logging for a cleaner testing output.
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 # Reload plugin module, in order to re-run module-level code.
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 # Set execute bit.
68 os.chmod(notifier_program, 0o755)
69
70 # Make the notifier use the temporary file instead of `terminal-notifier`.
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 # Toggle Enable Flag
92 obj = apprise.Apprise.instantiate(
93 "macosx://_/?image=True", suppress_exceptions=False
94 )
95 assert isinstance(obj, NotifyMacOSX) is True
96
97 # Test url() call
98 assert isinstance(obj.url(), str) is True
99
100 # URL Identifier has been disabled as this isn't unique enough
101 # to be mapped to more the 1 end point; verify that None is always
102 # returned
103 assert obj.url_id() is None
104
105 # test notifications
106 assert (
107 obj.notify(
108 title="title", body="body", notify_type=apprise.NotifyType.INFO
109 )
110 is True
111 )
112
113 # test notification without a title
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 # Test Sound
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 # Test Click (-open support)
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 # Unset the executable bit.
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 # Let's disrupt the path location.
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 # Emulate a failing program.
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 # When patching something which has a side effect on the module-level code
231 # of a plugin, make sure to reload it.
232 mocker.patch("platform.system", return_value="Linux")
233 reload_plugin("macosx")
234
235 # Our object is disabled.
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 # When patching something which has a side effect on the module-level code
246 # of a plugin, make sure to reload it.
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