Debian Codegraph @

tests/test_plugin_trigv.py

raw
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 from unittest import mock
30
31 from helpers import AppriseURLTester
32 import pytest
33 import requests
34
35 import apprise
36 from apprise.exception import AppriseImproperlyConfigured
37 from apprise.plugins.trigv import NotifyTrigv
38
39 logging.disable(logging.CRITICAL)
40
41 VALID_API_KEY = "trgv_a1b2c3d4_0123456789abcdef0123456789abcdef"
42
43 apprise_url_tests = (
44 (
45 "trigvs://",
46 {
47 "instance": AppriseImproperlyConfigured,
48 },
49 ),
50 (
51 "trigv://",
52 {
53 "instance": AppriseImproperlyConfigured,
54 },
55 ),
56 (
57 f"trigvs://{VALID_API_KEY}",
58 {
59 "instance": NotifyTrigv,
60 },
61 ),
62 (
63 f"trigvs://{VALID_API_KEY}/general",
64 {
65 "instance": NotifyTrigv,
66 },
67 ),
68 (
69 f"trigvs://{VALID_API_KEY}/deploys",
70 {
71 "instance": NotifyTrigv,
72 },
73 ),
74 (
75 f"trigv://{VALID_API_KEY}@trigv-platform.test/general",
76 {
77 "instance": NotifyTrigv,
78 },
79 ),
80 (
81 f"trigv://{VALID_API_KEY}@trigv-platform.test:8080/general",
82 {
83 "instance": NotifyTrigv,
84 },
85 ),
86 (
87 f"trigvs://{VALID_API_KEY}/alerts/ops",
88 {
89 "instance": NotifyTrigv,
90 },
91 ),
92 (
93 f"trigvs://{VALID_API_KEY}/?to=alerts,ops",
94 {
95 "instance": NotifyTrigv,
96 },
97 ),
98 (
99 f"trigvs://{VALID_API_KEY}/?url=https://example.com",
100 {
101 "instance": NotifyTrigv,
102 },
103 ),
104 (
105 f"trigvs://{VALID_API_KEY}/?urgency=time_sensitive",
106 {
107 "instance": NotifyTrigv,
108 },
109 ),
110 (
111 "trigvs://not-a-valid-key",
112 {
113 "instance": AppriseImproperlyConfigured,
114 },
115 ),
116 (
117 f"trigvs://{VALID_API_KEY}/bad.channel/",
118 {
119 "instance": AppriseImproperlyConfigured,
120 },
121 ),
122 (
123 f"trigvs://{VALID_API_KEY}",
124 {
125 "instance": NotifyTrigv,
126 "response": False,
127 "requests_response_code": requests.codes.unauthorized,
128 },
129 ),
130 (
131 f"trigvs://{VALID_API_KEY}",
132 {
133 "instance": NotifyTrigv,
134 "response": False,
135 "requests_response_code": requests.codes.unprocessable_entity,
136 },
137 ),
138 (
139 f"trigvs://{VALID_API_KEY}",
140 {
141 "instance": NotifyTrigv,
142 "test_requests_exceptions": True,
143 },
144 ),
145 )
146
147
148 def test_plugin_trigv_urls():
149 """NotifyTrigv() Apprise URLs."""
150
151 AppriseURLTester(tests=apprise_url_tests).run_all()
152
153
154 @mock.patch("requests.post")
155 def test_plugin_trigv_general(mock_post):
156 """NotifyTrigv() General Checks."""
157
158 response = mock.Mock()
159 response.content = b'{"event":{"public_id":"evt_test","status":"queued"}}'
160 response.status_code = requests.codes.accepted
161 mock_post.return_value = response
162
163 obj = apprise.Apprise.instantiate(f"trigvs://{VALID_API_KEY}/alerts")
164 assert isinstance(obj, NotifyTrigv)
165 assert obj.targets == ["alerts"]
166 assert len(obj) == 1
167
168 assert (
169 obj.notify(
170 body="Backup failed",
171 title="Cron",
172 notify_type=apprise.NotifyType.FAILURE,
173 )
174 is True
175 )
176
177 assert mock_post.call_count == 1
178 assert mock_post.call_args_list[0][0][0] == (
179 "https://api.trigv.com/api/v1/events"
180 )
181
182 headers = mock_post.call_args_list[0][1]["headers"]
183 assert headers["Authorization"] == f"Bearer {VALID_API_KEY}"
184 assert headers["Content-Type"] == "application/json"
185
186 import json
187
188 payload = json.loads(mock_post.call_args_list[0][1]["data"])
189 assert payload["channel"] == "alerts"
190 assert payload["title"] == "Cron"
191 assert payload["description"] == "Backup failed"
192 assert payload["level"] == "error"
193 assert payload["delivery_urgency"] == "time_sensitive"
194
195
196 @mock.patch("requests.post")
197 def test_plugin_trigv_local_host(mock_post):
198 """NotifyTrigv() custom hostname for local development."""
199
200 response = mock.Mock()
201 response.status_code = requests.codes.accepted
202 mock_post.return_value = response
203
204 obj = apprise.Apprise.instantiate(
205 f"trigv://{VALID_API_KEY}@trigv-platform.test/general"
206 )
207 assert isinstance(obj, NotifyTrigv)
208
209 assert obj.notify(body="Hello", title="Test") is True
210 assert (
211 mock_post.call_args_list[0][0][0]
212 == "http://trigv-platform.test/api/v1/events"
213 )
214
215
216 @mock.patch("requests.post")
217 def test_plugin_trigv_custom_port(mock_post):
218 """NotifyTrigv() custom hostname with an explicit port."""
219
220 response = mock.Mock()
221 response.status_code = requests.codes.accepted
222 mock_post.return_value = response
223
224 obj = apprise.Apprise.instantiate(
225 f"trigvs://{VALID_API_KEY}@trigv-platform.test:8443/general"
226 )
227 assert isinstance(obj, NotifyTrigv)
228 assert obj.port == 8443
229
230 assert obj.notify(body="Hello", title="Test") is True
231 assert (
232 mock_post.call_args_list[0][0][0]
233 == "https://trigv-platform.test:8443/api/v1/events"
234 )
235
236
237 @mock.patch("requests.post")
238 def test_plugin_trigv_multiple_channels(mock_post):
239 """NotifyTrigv() notifies every channel specified."""
240
241 response = mock.Mock()
242 response.status_code = requests.codes.accepted
243 mock_post.return_value = response
244
245 obj = NotifyTrigv(api_key=VALID_API_KEY, targets=["alerts", "ops"])
246 assert obj.targets == ["alerts", "ops"]
247 assert len(obj) == 2
248
249 assert obj.send(body="test", title="title") is True
250 assert mock_post.call_count == 2
251
252 import json
253
254 channels_notified = {
255 json.loads(call[1]["data"])["channel"]
256 for call in mock_post.call_args_list
257 }
258 assert channels_notified == {"alerts", "ops"}
259
260
261 @mock.patch("requests.post")
262 def test_plugin_trigv_multiple_channels_partial_failure(mock_post):
263 """NotifyTrigv() still tries every channel if one of them fails."""
264
265 ok_response = mock.Mock()
266 ok_response.status_code = requests.codes.accepted
267
268 error_response = mock.Mock()
269 error_response.status_code = requests.codes.internal_server_error
270 error_response.content = b""
271
272 mock_post.side_effect = [error_response, ok_response]
273
274 obj = NotifyTrigv(api_key=VALID_API_KEY, targets=["alerts", "ops"])
275 assert obj.send(body="test", title="title") is False
276 assert mock_post.call_count == 2
277
278
279 @mock.patch("requests.post")
280 def test_plugin_trigv_multiple_channels_request_exception(mock_post):
281 """NotifyTrigv() marks a failure when one channel raises."""
282
283 ok_response = mock.Mock()
284 ok_response.status_code = requests.codes.accepted
285
286 mock_post.side_effect = [
287 requests.RequestException(),
288 ok_response,
289 ]
290
291 obj = NotifyTrigv(api_key=VALID_API_KEY, targets=["alerts", "ops"])
292 assert obj.send(body="test", title="title") is False
293 assert mock_post.call_count == 2
294
295
296 @mock.patch("requests.post")
297 def test_plugin_trigv_duplicate_success(mock_post):
298 """NotifyTrigv() treats HTTP 200 idempotent duplicate as success."""
299
300 response = mock.Mock()
301 response.status_code = requests.codes.ok
302 mock_post.return_value = response
303
304 obj = NotifyTrigv(api_key=VALID_API_KEY)
305 assert obj.send(body="test", title="title") is True
306
307
308 @mock.patch("requests.post")
309 def test_plugin_trigv_optional_payload_and_priority(mock_post):
310 """NotifyTrigv() includes optional fields and maps numeric priority."""
311
312 response = mock.Mock()
313 response.status_code = requests.codes.accepted
314 mock_post.return_value = response
315
316 obj = NotifyTrigv(
317 api_key=VALID_API_KEY,
318 image_url="https://example.com/image.png",
319 event_type="backup.failed",
320 priority=1,
321 )
322 assert obj.send(body="", title="") is True
323
324 import json
325
326 payload = json.loads(mock_post.call_args_list[0][1]["data"])
327 assert payload["title"] == obj.app_desc
328 assert payload["description"] == obj.app_desc
329 assert payload["image_url"] == "https://example.com/image.png"
330 assert payload["event_type"] == "backup.failed"
331 assert payload["delivery_urgency"] == "time_sensitive"
332
333
334 @mock.patch("requests.post")
335 def test_plugin_trigv_empty_body_and_invalid_priority(mock_post):
336 """NotifyTrigv() omits an empty body and ignores invalid priority."""
337
338 response = mock.Mock()
339 response.status_code = requests.codes.accepted
340 mock_post.return_value = response
341
342 obj = NotifyTrigv(api_key=VALID_API_KEY, priority="invalid")
343 assert obj.send(body="", title="Title", notify_type="unknown") is True
344
345 import json
346
347 payload = json.loads(mock_post.call_args_list[0][1]["data"])
348 assert "description" not in payload
349 assert payload["level"] == "info"
350 assert payload["delivery_urgency"] == "standard"
351
352
353 def test_plugin_trigv_invalid_urgency():
354 """NotifyTrigv() rejects unknown urgency."""
355
356 with pytest.raises(AppriseImproperlyConfigured):
357 NotifyTrigv(api_key=VALID_API_KEY, urgency="critical")
358
359
360 def test_plugin_trigv_to_alias():
361 """NotifyTrigv() parses ?to= into additional targets."""
362
363 parsed = NotifyTrigv.parse_url(
364 f"trigvs://{VALID_API_KEY}/alerts/?to=ops,escalation"
365 )
366 obj = NotifyTrigv(**parsed)
367 # parse_list() de-duplicates and sorts every target it is given
368 assert obj.targets == ["alerts", "escalation", "ops"]
369
370
371 @mock.patch("requests.post")
372 def test_plugin_trigv_url_roundtrip(mock_post):
373 """NotifyTrigv() url() round-trips optional query parameters."""
374
375 mock_post.return_value = mock.Mock()
376 mock_post.return_value.status_code = requests.codes.accepted
377
378 obj = NotifyTrigv(
379 api_key=VALID_API_KEY,
380 targets="deploys",
381 supplemental_url="https://example.com/run/1",
382 image_url="https://example.com/image.png",
383 urgency="time_sensitive",
384 event_type="deploy.completed",
385 priority=2,
386 )
387
388 generated = obj.url()
389 assert "deploys" in generated
390 assert "url=" in generated
391 assert "image_url=" in generated
392 assert "urgency=time_sensitive" in generated
393 assert "event_type=deploy.completed" in generated
394 assert "priority=2" in generated
395
396 parsed = NotifyTrigv.parse_url(generated)
397 obj2 = NotifyTrigv(**parsed)
398 assert obj2.targets == ["deploys"]
399 assert obj2.supplemental_url == "https://example.com/run/1"
400 assert obj2.image_url == "https://example.com/image.png"
401 assert obj2.urgency == "time_sensitive"
402 assert obj2.event_type == "deploy.completed"
403 assert obj2.priority == 2