Debian Codegraph @

tests/test_plugin_gotify.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 # Disable logging for a cleaner testing output
29 from json import loads
30 import logging
31 from unittest import mock
32
33 from helpers import AppriseURLTester
34 import pytest
35 import requests
36
37 import apprise
38 from apprise import NotifyFormat
39 from apprise.plugins.gotify import GotifyPriority, NotifyGotify
40
41 logging.disable(logging.CRITICAL)
42
43 # Our Testing URLs
44 apprise_url_tests = (
45 (
46 "gotify://",
47 {
48 "instance": None,
49 },
50 ),
51 # No token specified
52 (
53 "gotify://hostname",
54 {
55 "instance": TypeError,
56 },
57 ),
58 # Provide a hostname and token
59 (
60 "gotify://hostname/%s" % ("t" * 16),
61 {
62 "instance": NotifyGotify,
63 # Our expected url(privacy=True) startswith() response:
64 "privacy_url": "gotify://hostname/t...t",
65 },
66 ),
67 # Provide a hostname, path, and token
68 (
69 "gotify://hostname/a/path/ending/in/a/slash/%s" % ("u" * 16),
70 {
71 "instance": NotifyGotify,
72 # Our expected url(privacy=True) startswith() response:
73 "privacy_url": "gotify://hostname/a/path/ending/in/a/slash/u...u/",
74 },
75 ),
76 # Markdown test
77 (
78 "gotify://hostname/%s?format=markdown" % ("t" * 16),
79 {
80 "instance": NotifyGotify,
81 },
82 ),
83 # Provide a hostname, path, and token
84 (
85 "gotify://hostname/a/path/not/ending/in/a/slash/%s" % ("v" * 16),
86 {
87 "instance": NotifyGotify,
88 # Our expected url(privacy=True) startswith() response:
89 "privacy_url": (
90 "gotify://hostname/a/path/not/ending/in/a/slash/v...v/"
91 ),
92 },
93 ),
94 # Provide a priority
95 (
96 "gotify://hostname/%s?priority=high" % ("i" * 16),
97 {
98 "instance": NotifyGotify,
99 },
100 ),
101 # Provide an invalid priority
102 (
103 "gotify://hostname:8008/%s?priority=invalid" % ("i" * 16),
104 {
105 "instance": NotifyGotify,
106 },
107 ),
108 # An invalid url
109 (
110 "gotify://:@/",
111 {
112 "instance": None,
113 },
114 ),
115 (
116 "gotify://hostname/%s/" % ("t" * 16),
117 {
118 "instance": NotifyGotify,
119 # force a failure
120 "response": False,
121 "requests_response_code": requests.codes.internal_server_error,
122 },
123 ),
124 (
125 "gotifys://localhost/%s/" % ("t" * 16),
126 {
127 "instance": NotifyGotify,
128 # throw a bizarre code forcing us to fail to look it up
129 "response": False,
130 "requests_response_code": 999,
131 },
132 ),
133 (
134 "gotify://localhost/%s/" % ("t" * 16),
135 {
136 "instance": NotifyGotify,
137 # Throws a series of i/o exceptions with this flag
138 # is set and tests that we gracefully handle them
139 "test_requests_exceptions": True,
140 },
141 ),
142 )
143
144
145 def test_plugin_gotify_urls():
146 """NotifyGotify() Apprise URLs."""
147
148 # Run our general tests
149 AppriseURLTester(tests=apprise_url_tests).run_all()
150
151
152 def test_plugin_gotify_edge_cases():
153 """NotifyGotify() Edge Cases."""
154 # Initializes the plugin with an invalid token
155 with pytest.raises(TypeError):
156 NotifyGotify(token=None)
157 # Whitespace also acts as an invalid token value
158 with pytest.raises(TypeError):
159 NotifyGotify(token=" ")
160
161
162 @mock.patch("requests.post")
163 def test_plugin_gotify_config_files(mock_post):
164 """NotifyGotify() Config File Cases."""
165 content = """
166 urls:
167 - gotify://hostname/{}:
168 - priority: 0
169 tag: gotify_int low
170 - priority: "0"
171 tag: gotify_str_int low
172 # We want to make sure our '1' does not match the '10' entry
173 - priority: "1"
174 tag: gotify_str_int low
175 - priority: low
176 tag: gotify_str low
177
178 # This will take on moderate (default) priority
179 - priority: invalid
180 tag: gotify_invalid
181
182 - gotify://hostname/{}:
183 - priority: 10
184 tag: gotify_int emerg
185 - priority: "10"
186 tag: gotify_str_int emerg
187 - priority: emergency
188 tag: gotify_str emerg
189 """.format("a" * 16, "b" * 16)
190
191 # Prepare Mock
192 mock_post.return_value = requests.Request()
193 mock_post.return_value.status_code = requests.codes.ok
194
195 # Create ourselves a config object
196 ac = apprise.AppriseConfig()
197 assert ac.add_config(content=content) is True
198
199 aobj = apprise.Apprise()
200
201 # Add our configuration
202 aobj.add(ac)
203
204 # We should be able to read our 8 servers from that
205 # 4x low
206 # 3x emerg
207 # 1x invalid (so takes on normal priority)
208 assert len(ac.servers()) == 8
209 assert len(aobj) == 8
210 assert len(list(aobj.find(tag="low"))) == 4
211 for s in aobj.find(tag="low"):
212 assert s.priority == GotifyPriority.LOW
213
214 assert len(list(aobj.find(tag="emerg"))) == 3
215 for s in aobj.find(tag="emerg"):
216 assert s.priority == GotifyPriority.EMERGENCY
217
218 assert len(list(aobj.find(tag="gotify_str"))) == 2
219 assert len(list(aobj.find(tag="gotify_str_int"))) == 3
220 assert len(list(aobj.find(tag="gotify_int"))) == 2
221
222 assert len(list(aobj.find(tag="gotify_invalid"))) == 1
223 assert (
224 next(aobj.find(tag="gotify_invalid")).priority == GotifyPriority.NORMAL
225 )
226
227
228 @mock.patch("requests.post")
229 def test_plugin_gotify_html_to_markdown_format(mock_post):
230 """NotifyGotify(): HTML body is converted to Markdown."""
231
232 # Prepare Mock
233 mock_post.return_value = requests.Request()
234 mock_post.return_value.status_code = requests.codes.ok
235
236 # Instantiate a Gotify plugin in Markdown mode
237 aobj = apprise.Apprise()
238 assert aobj.add("gotify://hostname/{}?format=markdown".format("t" * 16))
239
240 # Notify with an HTML body; the framework should convert it
241 # to Markdown before dispatching to Gotify
242 assert (
243 aobj.notify(
244 body="<b>hello</b> <i>world</i>",
245 body_format=NotifyFormat.HTML,
246 )
247 is True
248 )
249 assert mock_post.call_count == 1
250
251 # The body must arrive as Markdown, not as stripped plain text
252 payload = loads(mock_post.call_args_list[0][1]["data"])
253 assert payload["message"] == "**hello** *world*"