Debian Codegraph @

tests/test_plugin_pinglet.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 NotifyType
39 from apprise.plugins.pinglet import NotifyPinglet, PingletPriority
40
41 logging.disable(logging.CRITICAL)
42
43 # Our Testing URLs
44 apprise_url_tests = (
45 (
46 "pinglet://",
47 {
48 "instance": None,
49 },
50 ),
51 # An invalid url
52 (
53 "pinglet://:@/",
54 {
55 "instance": None,
56 },
57 ),
58 # No API Key specified
59 (
60 "pinglet://hostname/acme/deploys",
61 {
62 "instance": TypeError,
63 },
64 ),
65 # No namespace and/or topic specified
66 (
67 "pinglet://token@hostname",
68 {
69 "instance": TypeError,
70 },
71 ),
72 (
73 "pinglet://token@hostname/deploys",
74 {
75 "instance": TypeError,
76 },
77 ),
78 # Provide an API Key, namespace, and topic
79 (
80 "pinglet://abc123@hostname/acme/deploys",
81 {
82 "instance": NotifyPinglet,
83 # Our expected url(privacy=True) startswith() response:
84 "privacy_url": "pinglet://****@hostname/acme/deploys",
85 },
86 ),
87 # Secure protocol
88 (
89 "pinglets://abc123@hostname/acme/deploys",
90 {
91 "instance": NotifyPinglet,
92 "privacy_url": "pinglets://****@hostname/acme/deploys",
93 },
94 ),
95 # API Key as a query argument
96 (
97 "pinglets://hostname/acme/deploys?token=abc123",
98 {
99 "instance": NotifyPinglet,
100 "privacy_url": "pinglets://****@hostname/acme/deploys",
101 },
102 ),
103 # A path prefix (such as a reverse-proxy mount point)
104 (
105 "pinglet://abc123@hostname/prefix/acme/deploys",
106 {
107 "instance": NotifyPinglet,
108 "privacy_url": "pinglet://****@hostname/prefix/acme/deploys",
109 },
110 ),
111 # Host with a port
112 (
113 "pinglet://abc123@hostname:8080/acme/deploys",
114 {
115 "instance": NotifyPinglet,
116 "privacy_url": "pinglet://****@hostname:8080/acme/deploys",
117 },
118 ),
119 # Provide a priority
120 (
121 "pinglet://abc123@hostname/acme/deploys?priority=urgent",
122 {
123 "instance": NotifyPinglet,
124 },
125 ),
126 # Priorities support prefix matching (s => silent)
127 (
128 "pinglet://abc123@hostname/acme/deploys?priority=s",
129 {
130 "instance": NotifyPinglet,
131 },
132 ),
133 # An invalid priority takes on the default (normal)
134 (
135 "pinglet://abc123@hostname/acme/deploys?priority=invalid",
136 {
137 "instance": NotifyPinglet,
138 },
139 ),
140 # Badges (:key=value) and metadata (+key=value)
141 (
142 "pinglet://abc123@hostname/acme/deploys?:CPU=95%25&+region=eu-west",
143 {
144 "instance": NotifyPinglet,
145 },
146 ),
147 (
148 "pinglet://abc123@hostname/acme/deploys",
149 {
150 "instance": NotifyPinglet,
151 # force a failure
152 "response": False,
153 "requests_response_code": requests.codes.internal_server_error,
154 },
155 ),
156 (
157 "pinglets://abc123@localhost/acme/deploys",
158 {
159 "instance": NotifyPinglet,
160 # throw a bizarre code forcing us to fail to look it up
161 "response": False,
162 "requests_response_code": 999,
163 },
164 ),
165 (
166 "pinglet://abc123@localhost/acme/deploys",
167 {
168 "instance": NotifyPinglet,
169 # Throws a series of i/o exceptions with this flag
170 # is set and tests that we gracefully handle them
171 "test_requests_exceptions": True,
172 },
173 ),
174 )
175
176
177 def test_plugin_pinglet_urls():
178 """NotifyPinglet() Apprise URLs."""
179
180 # Run our general tests
181 AppriseURLTester(tests=apprise_url_tests).run_all()
182
183
184 def test_plugin_pinglet_edge_cases():
185 """NotifyPinglet() Edge Cases."""
186 # Initializes the plugin with an invalid token
187 with pytest.raises(TypeError):
188 NotifyPinglet(token=None, namespace="acme", topic="deploys")
189 # Whitespace also acts as an invalid token value
190 with pytest.raises(TypeError):
191 NotifyPinglet(token=" ", namespace="acme", topic="deploys")
192
193 # Missing namespace and/or topic
194 with pytest.raises(TypeError):
195 NotifyPinglet(token="abc123", namespace=None, topic="deploys")
196 with pytest.raises(TypeError):
197 NotifyPinglet(token="abc123", namespace="acme", topic=None)
198
199 # Direct instantiation without a fullpath defaults to "/"
200 obj = NotifyPinglet(token="abc123", namespace="acme", topic="deploys")
201 assert obj.fullpath == "/"
202
203
204 @mock.patch("requests.post")
205 def test_plugin_pinglet_payload(mock_post):
206 """NotifyPinglet() Payload Construction."""
207
208 # Prepare Mock
209 mock_post.return_value = requests.Request()
210 mock_post.return_value.status_code = requests.codes.ok
211
212 aobj = apprise.Apprise()
213 assert aobj.add(
214 "pinglets://[email protected]/acme/deploys"
215 "?priority=urgent&:CPU=95%25&:Host=web-1&+region=eu-west"
216 )
217
218 assert (
219 aobj.notify(
220 title="Hello",
221 body="It works",
222 notify_type=NotifyType.FAILURE,
223 )
224 is True
225 )
226
227 assert mock_post.call_count == 1
228 assert (
229 mock_post.call_args_list[0][0][0]
230 == "https://app.pinglet.co.uk/acme/deploys"
231 )
232
233 headers = mock_post.call_args_list[0][1]["headers"]
234 assert headers["Authorization"] == "Bearer mykey"
235
236 payload = loads(mock_post.call_args_list[0][1]["data"])
237 assert payload["title"] == "Hello"
238 assert payload["message"] == "It works"
239 assert payload["priority"] == PingletPriority.URGENT
240 # The FAILURE notification type maps to Pinglet's 'error' level
241 assert payload["level"] == "error"
242 assert payload["badges"] == {"CPU": "95%", "Host": "web-1"}
243 assert payload["data"] == {"region": "eu-west"}
244
245
246 @mock.patch("requests.post")
247 def test_plugin_pinglet_payload_defaults(mock_post):
248 """NotifyPinglet() Payload Defaults."""
249
250 # Prepare Mock
251 mock_post.return_value = requests.Request()
252 mock_post.return_value.status_code = requests.codes.ok
253
254 aobj = apprise.Apprise()
255 assert aobj.add("pinglet://mykey@hostname/acme/deploys")
256
257 assert aobj.notify(body="It works") is True
258
259 assert mock_post.call_count == 1
260 assert mock_post.call_args_list[0][0][0] == "http://hostname/acme/deploys"
261
262 payload = loads(mock_post.call_args_list[0][1]["data"])
263 # An empty title is omitted entirely
264 assert "title" not in payload
265 # No badges/metadata were specified so they are omitted
266 assert "badges" not in payload
267 assert "data" not in payload
268 assert payload["priority"] == PingletPriority.NORMAL
269 # The INFO notification type maps to Pinglet's 'info' level
270 assert payload["level"] == "info"
271
272
273 def test_plugin_pinglet_limits():
274 """NotifyPinglet() Badge and Metadata Limits."""
275
276 # Badges are capped at 3 entries; keys are truncated to 24
277 # characters, and values to 32
278 obj = apprise.Apprise.instantiate(
279 "pinglet://abc123@hostname/acme/deploys"
280 "?:{}={}&:b=2&:c=3&:d=4&:e=5".format("k" * 30, "v" * 40)
281 )
282 assert isinstance(obj, NotifyPinglet)
283 assert len(obj.badges) == 3
284 assert "k" * 24 in obj.badges
285 assert obj.badges["k" * 24] == "v" * 32
286
287 # Metadata keys are truncated to 64 characters, and values to 256
288 obj = apprise.Apprise.instantiate(
289 "pinglet://abc123@hostname/acme/deploys?+{}={}".format(
290 "k" * 70, "v" * 300
291 )
292 )
293 assert isinstance(obj, NotifyPinglet)
294 assert obj.data == {"k" * 64: "v" * 256}
295
296
297 def test_plugin_pinglet_priorities():
298 """NotifyPinglet() Priority Handling."""
299
300 for priority, expected in (
301 ("silent", PingletPriority.SILENT),
302 ("s", PingletPriority.SILENT),
303 ("NORMAL", PingletPriority.NORMAL),
304 ("urgent", PingletPriority.URGENT),
305 ("u", PingletPriority.URGENT),
306 # Invalid entries take on the default
307 ("invalid", PingletPriority.NORMAL),
308 ("", PingletPriority.NORMAL),
309 ):
310 obj = apprise.Apprise.instantiate(
311 f"pinglet://abc123@hostname/acme/deploys?priority={priority}"
312 )
313 assert isinstance(obj, NotifyPinglet)
314 assert obj.priority == expected, f"priority={priority}"