Debian Codegraph @

tests/test_plugin_opsgenie.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 from json import dumps
29
30 # Disable logging for a cleaner testing output
31 import logging
32 from unittest import mock
33
34 from helpers import AppriseURLTester
35 import requests
36
37 import apprise
38 from apprise.plugins.opsgenie import (
39 NotifyOpsgenie,
40 NotifyType,
41 OpsgeniePriority,
42 )
43
44 logging.disable(logging.CRITICAL)
45
46 # a test UUID we can use
47 UUID4 = "8b799edf-6f98-4d3a-9be7-2862fb4e5752"
48
49 OPSGENIE_GOOD_RESPONSE = dumps(
50 {
51 "result": "Request will be processed",
52 "took": 0.204,
53 "requestId": "43a29c5c-3dbf-4fa4-9c26-f4f71023e120",
54 }
55 )
56
57 # Our Testing URLs
58 apprise_url_tests = (
59 (
60 "opsgenie://",
61 {
62 # We failed to identify any valid authentication
63 "instance": TypeError,
64 },
65 ),
66 (
67 "opsgenie://:@/",
68 {
69 # We failed to identify any valid authentication
70 "instance": TypeError,
71 },
72 ),
73 (
74 "opsgenie://%20%20/",
75 {
76 # invalid apikey specified
77 "instance": TypeError,
78 },
79 ),
80 (
81 "opsgenie://apikey/user/?region=xx",
82 {
83 # invalid region id
84 "instance": TypeError,
85 },
86 ),
87 (
88 "opsgenie://user@apikey/",
89 {
90 # No targets specified; this is allowed
91 "instance": NotifyOpsgenie,
92 "notify_type": NotifyType.WARNING,
93 # Bad response returned
94 "requests_response_text": "{",
95 # We will not be successful sending the notice
96 "notify_response": False,
97 },
98 ),
99 (
100 "opsgenie://apikey/",
101 {
102 # No targets specified; this is allowed
103 "instance": NotifyOpsgenie,
104 "notify_type": NotifyType.FAILURE,
105 # Our response expected server response
106 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
107 },
108 ),
109 (
110 "opsgenie://apikey/user",
111 {
112 # Valid user
113 "instance": NotifyOpsgenie,
114 "notify_type": NotifyType.FAILURE,
115 # Our response expected server response
116 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
117 "privacy_url": "opsgenie://a...y/%40user",
118 },
119 ),
120 (
121 "opsgenie://apikey/@user?region=eu",
122 {
123 # European Region
124 "instance": NotifyOpsgenie,
125 "notify_type": NotifyType.FAILURE,
126 # Our response expected server response
127 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
128 },
129 ),
130 (
131 "opsgenie://apikey/@user?entity=A%20Entity",
132 {
133 # Assign an entity
134 "instance": NotifyOpsgenie,
135 "notify_type": NotifyType.FAILURE,
136 # Our response expected server response
137 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
138 },
139 ),
140 (
141 "opsgenie://apikey/@user?alias=An%20Alias",
142 {
143 # Assign an alias
144 "instance": NotifyOpsgenie,
145 "notify_type": NotifyType.FAILURE,
146 # Our response expected server response
147 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
148 },
149 ),
150 # Bad Action
151 (
152 "opsgenie://apikey/@user?action=invalid",
153 {
154 # Assign an entity
155 "instance": TypeError,
156 },
157 ),
158 (
159 "opsgenie://from@apikey/@user?:invalid=note",
160 {
161 # Assign an entity
162 "instance": TypeError,
163 },
164 ),
165 (
166 "opsgenie://apikey/@user?:warning=invalid",
167 {
168 # Assign an entity
169 "instance": TypeError,
170 },
171 ),
172 # Creates an index entry
173 (
174 "opsgenie://apikey/@user?entity=index&action=new",
175 {
176 # Assign an entity
177 "instance": NotifyOpsgenie,
178 "notify_type": NotifyType.FAILURE,
179 # Our response expected server response
180 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
181 },
182 ),
183 # Now action it
184 (
185 "opsgenie://apikey/@user?entity=index&action=acknowledge",
186 {
187 # Assign an entity
188 "instance": NotifyOpsgenie,
189 "notify_type": NotifyType.SUCCESS,
190 # Our response expected server response
191 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
192 },
193 ),
194 (
195 "opsgenie://from@apikey/@user?entity=index&action=note",
196 {
197 # Assign an entity
198 "instance": NotifyOpsgenie,
199 "notify_type": NotifyType.SUCCESS,
200 # Our response expected server response
201 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
202 },
203 ),
204 (
205 "opsgenie://from@apikey/@user?entity=index&action=note",
206 {
207 # Assign an entity
208 "instance": NotifyOpsgenie,
209 "notify_type": NotifyType.SUCCESS,
210 # Our response expected server response
211 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
212 "response": False,
213 "requests_response_code": 500,
214 },
215 ),
216 (
217 "opsgenie://apikey/@user?entity=index&action=close",
218 {
219 # Assign an entity
220 "instance": NotifyOpsgenie,
221 "notify_type": NotifyType.SUCCESS,
222 # Our response expected server response
223 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
224 },
225 ),
226 (
227 "opsgenie://apikey/@user?entity=index&action=delete",
228 {
229 # Assign an entity
230 "instance": NotifyOpsgenie,
231 "notify_type": NotifyType.SUCCESS,
232 # Our response expected server response
233 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
234 },
235 ),
236 # map info messages to generate a new message
237 (
238 "opsgenie://apikey/@user?entity=index2&:info=new",
239 {
240 # Assign an entity
241 "instance": NotifyOpsgenie,
242 "notify_type": NotifyType.INFO,
243 # Our response expected server response
244 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
245 },
246 ),
247 (
248 "opsgenie://joe@apikey/@user?priority=p3",
249 {
250 # Assign our priority
251 "instance": NotifyOpsgenie,
252 "notify_type": NotifyType.FAILURE,
253 # Our response expected server response
254 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
255 },
256 ),
257 (
258 "opsgenie://apikey/?tags=comma,separated",
259 {
260 # Test our our 'tags' (tag is reserved in Apprise) but not 'tags'
261 # Also test the fact we do not need to define a target
262 "instance": NotifyOpsgenie,
263 "notify_type": NotifyType.FAILURE,
264 # Our response expected server response
265 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
266 },
267 ),
268 (
269 "opsgenie://apikey/@user?priority=invalid",
270 {
271 # Invalid priority (loads using default)
272 "instance": NotifyOpsgenie,
273 "notify_type": NotifyType.FAILURE,
274 # Our response expected server response
275 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
276 },
277 ),
278 (
279 "opsgenie://apikey/[email protected]/#team/*sche/^esc/%20/a",
280 {
281 # Valid user (email), valid schedule, Escalated ID,
282 # an invalid entry (%20), and too short of an entry (a)
283 "instance": NotifyOpsgenie,
284 "notify_type": NotifyType.FAILURE,
285 # Our response expected server response
286 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
287 },
288 ),
289 (
290 f"opsgenie://apikey/@{UUID4}/#{UUID4}/*{UUID4}/^{UUID4}/",
291 {
292 # similar to the above, except we use the UUID's
293 "instance": NotifyOpsgenie,
294 "notify_type": NotifyType.FAILURE,
295 # Our response expected server response
296 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
297 },
298 ),
299 # Same link as before but @ missing at the front causing an ambigious
300 # lookup however the entry is treated a though a @ was in front (user)
301 (
302 f"opsgenie://apikey/{UUID4}/#{UUID4}/*{UUID4}/^{UUID4}/",
303 {
304 # similar to the above, except we use the UUID's
305 "instance": NotifyOpsgenie,
306 "notify_type": NotifyType.FAILURE,
307 # Our response expected server response
308 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
309 },
310 ),
311 (
312 "opsgenie://apikey?to=#team,user&+key=value&+type=override",
313 {
314 # Test to= and details (key/value pair) also override 'type'
315 "instance": NotifyOpsgenie,
316 "notify_type": NotifyType.FAILURE,
317 # Our response expected server response
318 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
319 },
320 ),
321 (
322 "opsgenie://apikey/#team/@user/?batch=yes",
323 {
324 # Test batch=
325 "instance": NotifyOpsgenie,
326 "notify_type": NotifyType.FAILURE,
327 # Our response expected server response
328 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
329 },
330 ),
331 (
332 "opsgenie://apikey/#team/@user/?batch=no",
333 {
334 # Test batch=
335 "instance": NotifyOpsgenie,
336 "notify_type": NotifyType.FAILURE,
337 # Our response expected server response
338 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
339 },
340 ),
341 (
342 "opsgenie://?apikey=abc&to=user",
343 {
344 # Test Kwargs
345 "instance": NotifyOpsgenie,
346 "notify_type": NotifyType.FAILURE,
347 # Our response expected server response
348 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
349 },
350 ),
351 (
352 "opsgenie://apikey/#team/user/",
353 {
354 "instance": NotifyOpsgenie,
355 # throw a bizarre code forcing us to fail to look it up
356 "notify_type": NotifyType.FAILURE,
357 # Our response expected server response
358 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
359 "response": False,
360 "requests_response_code": 999,
361 },
362 ),
363 (
364 "opsgenie://apikey/#topic1/device/",
365 {
366 "instance": NotifyOpsgenie,
367 "notify_type": NotifyType.FAILURE,
368 # Our response expected server response
369 "requests_response_text": OPSGENIE_GOOD_RESPONSE,
370 # Throws a series of i/o exceptions with this flag
371 # is set and tests that we gracefully handle them
372 "test_requests_exceptions": True,
373 },
374 ),
375 )
376
377
378 def test_plugin_opsgenie_urls(tmpdir):
379 """NotifyOpsgenie() Apprise URLs."""
380
381 # Run our general tests
382 AppriseURLTester(tests=apprise_url_tests).run_all(str(tmpdir))
383
384
385 @mock.patch("requests.post")
386 def test_plugin_opsgenie_config_files(mock_post):
387 """NotifyOpsgenie() Config File Cases."""
388 content = """
389 urls:
390 - opsgenie://apikey/user:
391 - priority: 1
392 tag: opsgenie_int low
393 - priority: "1"
394 tag: opsgenie_str_int low
395 - priority: "p1"
396 tag: opsgenie_pstr_int low
397 - priority: low
398 tag: opsgenie_str low
399
400 # This will take on moderate (default) priority
401 - priority: invalid
402 tag: opsgenie_invalid
403
404 - opsgenie://apikey2/user2:
405 - priority: 5
406 tag: opsgenie_int emerg
407 - priority: "5"
408 tag: opsgenie_str_int emerg
409 - priority: "p5"
410 tag: opsgenie_pstr_int emerg
411 - priority: emergency
412 tag: opsgenie_str emerg
413 """
414
415 # Prepare Mock
416 mock_post.return_value = requests.Request()
417 mock_post.return_value.status_code = requests.codes.ok
418 mock_post.return_value.content = OPSGENIE_GOOD_RESPONSE
419
420 # Create ourselves a config object
421 ac = apprise.AppriseConfig()
422 assert ac.add_config(content=content) is True
423
424 aobj = apprise.Apprise()
425
426 # Add our configuration
427 aobj.add(ac)
428
429 # We should be able to read our 9 servers from that
430 # 4x low
431 # 4x emerg
432 # 1x invalid (so takes on normal priority)
433 assert len(ac.servers()) == 9
434 assert len(aobj) == 9
435 assert len(list(aobj.find(tag="low"))) == 4
436 for s in aobj.find(tag="low"):
437 assert s.priority == OpsgeniePriority.LOW
438
439 assert len(list(aobj.find(tag="emerg"))) == 4
440 for s in aobj.find(tag="emerg"):
441 assert s.priority == OpsgeniePriority.EMERGENCY
442
443 assert len(list(aobj.find(tag="opsgenie_str"))) == 2
444 assert len(list(aobj.find(tag="opsgenie_str_int"))) == 2
445 assert len(list(aobj.find(tag="opsgenie_pstr_int"))) == 2
446 assert len(list(aobj.find(tag="opsgenie_int"))) == 2
447
448 assert len(list(aobj.find(tag="opsgenie_invalid"))) == 1
449 assert (
450 next(aobj.find(tag="opsgenie_invalid")).priority
451 == OpsgeniePriority.NORMAL
452 )
453
454
455 @mock.patch("requests.post")
456 def test_plugin_opsgenie_edge_case(mock_post):
457 """NotifyOpsgenie() Edge Cases."""
458 # Prepare Mock
459 mock_post.return_value = requests.Request()
460 mock_post.return_value.status_code = requests.codes.ok
461 mock_post.return_value.content = OPSGENIE_GOOD_RESPONSE
462
463 instance = apprise.Apprise.instantiate("opsgenie://apikey")
464 assert isinstance(instance, NotifyOpsgenie)
465
466 assert len(instance.store.keys()) == 0
467 assert instance.notify("test", "key", NotifyType.FAILURE) is True
468 assert len(instance.store.keys()) == 1
469
470 # Again just causes same index to get over-written
471 assert instance.notify("test", "key", NotifyType.FAILURE) is True
472 assert len(instance.store.keys()) == 1
473 assert "a62f2225bf" in instance.store
474
475 # Assign it garbage
476 instance.store["a62f2225bf"] = "garbage"
477 # This causes an internal check to fail where the keys are expected to be
478 # as a list (this one is now a string)
479 # content self corrects and things are fine
480 assert instance.notify("test", "key", NotifyType.FAILURE) is True
481 assert len(instance.store.keys()) == 1
482
483 # new key is new index
484 assert instance.notify("test", "key2", NotifyType.FAILURE) is True
485 assert len(instance.store.keys()) == 2