Debian Codegraph @

tests/test_plugin_jira.py

raw symbols SCIP
1 # BSD 2-Clause License
2 #
3 # Apprise - Push Notification Library.
4 # Copyright (c) 2025, 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.jira import JiraPriority, NotifyJira, NotifyType
39
40 logging.disable(logging.CRITICAL)
41
42 # a test UUID we can use
43 UUID4 = "8b799edf-6f98-4d3a-9be7-2862fb4e5752"
44
45 JIRA_GOOD_RESPONSE = dumps(
46 {
47 "result": "Request will be processed",
48 "took": 0.204,
49 "requestId": "43a29c5c-3dbf-4fa4-9c26-f4f71023e120",
50 }
51 )
52
53 # Our Testing URLs
54 apprise_url_tests = (
55 (
56 "jira://",
57 {
58 # We failed to identify any valid authentication
59 "instance": TypeError,
60 },
61 ),
62 (
63 "jira://:@/",
64 {
65 # We failed to identify any valid authentication
66 "instance": TypeError,
67 },
68 ),
69 (
70 "jira://%20%20/",
71 {
72 # invalid apikey specified
73 "instance": TypeError,
74 },
75 ),
76 (
77 "jira://apikey/user/?region=xx",
78 {
79 # invalid region id
80 "instance": TypeError,
81 },
82 ),
83 (
84 "jira://user@apikey/",
85 {
86 # No targets specified; this is allowed
87 "instance": NotifyJira,
88 "notify_type": NotifyType.WARNING,
89 # Bad response returned
90 "requests_response_text": "{",
91 # We will not be successful sending the notice
92 "notify_response": False,
93 },
94 ),
95 (
96 "jira://apikey/",
97 {
98 # No targets specified; this is allowed
99 "instance": NotifyJira,
100 "notify_type": NotifyType.FAILURE,
101 # Our response expected server response
102 "requests_response_text": JIRA_GOOD_RESPONSE,
103 },
104 ),
105 (
106 "jira://apikey/user",
107 {
108 # Valid user
109 "instance": NotifyJira,
110 "notify_type": NotifyType.FAILURE,
111 # Our response expected server response
112 "requests_response_text": JIRA_GOOD_RESPONSE,
113 "privacy_url": "jira://a...y/%40user",
114 },
115 ),
116 (
117 "jira://apikey/@user?region=eu",
118 {
119 # European Region
120 "instance": NotifyJira,
121 "notify_type": NotifyType.FAILURE,
122 # Our response expected server response
123 "requests_response_text": JIRA_GOOD_RESPONSE,
124 },
125 ),
126 (
127 "jira://apikey/@user?entity=A%20Entity",
128 {
129 # Assign an entity
130 "instance": NotifyJira,
131 "notify_type": NotifyType.FAILURE,
132 # Our response expected server response
133 "requests_response_text": JIRA_GOOD_RESPONSE,
134 },
135 ),
136 (
137 "jira://apikey/@user?alias=An%20Alias",
138 {
139 # Assign an alias
140 "instance": NotifyJira,
141 "notify_type": NotifyType.FAILURE,
142 # Our response expected server response
143 "requests_response_text": JIRA_GOOD_RESPONSE,
144 },
145 ),
146 # Bad Action
147 (
148 "jira://apikey/@user?action=invalid",
149 {
150 # Assign an entity
151 "instance": TypeError,
152 },
153 ),
154 (
155 "jira://from@apikey/@user?:invalid=note",
156 {
157 # Assign an entity
158 "instance": TypeError,
159 },
160 ),
161 (
162 "jira://apikey/@user?:warning=invalid",
163 {
164 # Assign an entity
165 "instance": TypeError,
166 },
167 ),
168 # Creates an index entry
169 (
170 "jira://apikey/@user?entity=index&action=new",
171 {
172 # Assign an entity
173 "instance": NotifyJira,
174 "notify_type": NotifyType.FAILURE,
175 # Our response expected server response
176 "requests_response_text": JIRA_GOOD_RESPONSE,
177 },
178 ),
179 # Now action it
180 (
181 "jira://apikey/@user?entity=index&action=acknowledge",
182 {
183 # Assign an entity
184 "instance": NotifyJira,
185 "notify_type": NotifyType.SUCCESS,
186 # Our response expected server response
187 "requests_response_text": JIRA_GOOD_RESPONSE,
188 },
189 ),
190 (
191 "jira://from@apikey/@user?entity=index&action=note",
192 {
193 # Assign an entity
194 "instance": NotifyJira,
195 "notify_type": NotifyType.SUCCESS,
196 # Our response expected server response
197 "requests_response_text": JIRA_GOOD_RESPONSE,
198 },
199 ),
200 (
201 "jira://from@apikey/@user?entity=index&action=note",
202 {
203 # Assign an entity
204 "instance": NotifyJira,
205 "notify_type": NotifyType.SUCCESS,
206 # Our response expected server response
207 "requests_response_text": JIRA_GOOD_RESPONSE,
208 "response": False,
209 "requests_response_code": 500,
210 },
211 ),
212 (
213 "jira://apikey/@user?entity=index&action=close",
214 {
215 # Assign an entity
216 "instance": NotifyJira,
217 "notify_type": NotifyType.SUCCESS,
218 # Our response expected server response
219 "requests_response_text": JIRA_GOOD_RESPONSE,
220 },
221 ),
222 (
223 "jira://apikey/@user?entity=index&action=delete",
224 {
225 # Assign an entity
226 "instance": NotifyJira,
227 "notify_type": NotifyType.SUCCESS,
228 # Our response expected server response
229 "requests_response_text": JIRA_GOOD_RESPONSE,
230 },
231 ),
232 # map info messages to generate a new message
233 (
234 "jira://apikey/@user?entity=index2&:info=new",
235 {
236 # Assign an entity
237 "instance": NotifyJira,
238 "notify_type": NotifyType.INFO,
239 # Our response expected server response
240 "requests_response_text": JIRA_GOOD_RESPONSE,
241 },
242 ),
243 (
244 "jira://joe@apikey/@user?priority=p3",
245 {
246 # Assign our priority
247 "instance": NotifyJira,
248 "notify_type": NotifyType.FAILURE,
249 # Our response expected server response
250 "requests_response_text": JIRA_GOOD_RESPONSE,
251 },
252 ),
253 (
254 "jira://apikey/?tags=comma,separated",
255 {
256 # Test our our 'tags' (tag is reserved in Apprise) but not 'tags'
257 # Also test the fact we do not need to define a target
258 "instance": NotifyJira,
259 "notify_type": NotifyType.FAILURE,
260 # Our response expected server response
261 "requests_response_text": JIRA_GOOD_RESPONSE,
262 },
263 ),
264 (
265 "jira://apikey/@user?priority=invalid",
266 {
267 # Invalid priority (loads using default)
268 "instance": NotifyJira,
269 "notify_type": NotifyType.FAILURE,
270 # Our response expected server response
271 "requests_response_text": JIRA_GOOD_RESPONSE,
272 },
273 ),
274 (
275 "jira://apikey/[email protected]/#team/*sche/^esc/%20/a",
276 {
277 # Valid user (email), valid schedule, Escalated ID,
278 # an invalid entry (%20), and too short of an entry (a)
279 "instance": NotifyJira,
280 "notify_type": NotifyType.FAILURE,
281 # Our response expected server response
282 "requests_response_text": JIRA_GOOD_RESPONSE,
283 },
284 ),
285 (
286 "jira://apikey/@{}/#{}/*{}/^{}/".format(UUID4, UUID4, UUID4, UUID4),
287 {
288 # similar to the above, except we use the UUID's
289 "instance": NotifyJira,
290 "notify_type": NotifyType.FAILURE,
291 # Our response expected server response
292 "requests_response_text": JIRA_GOOD_RESPONSE,
293 },
294 ),
295 # Same link as before but @ missing at the front causing an ambigious
296 # lookup however the entry is treated a though a @ was in front (user)
297 (
298 "jira://apikey/{}/#{}/*{}/^{}/".format(UUID4, UUID4, UUID4, UUID4),
299 {
300 # similar to the above, except we use the UUID's
301 "instance": NotifyJira,
302 "notify_type": NotifyType.FAILURE,
303 # Our response expected server response
304 "requests_response_text": JIRA_GOOD_RESPONSE,
305 },
306 ),
307 (
308 "jira://apikey?to=#team,user&+key=value&+type=override",
309 {
310 # Test to= and details (key/value pair) also override 'type'
311 "instance": NotifyJira,
312 "notify_type": NotifyType.FAILURE,
313 # Our response expected server response
314 "requests_response_text": JIRA_GOOD_RESPONSE,
315 },
316 ),
317 (
318 "jira://apikey/#team/@user/?batch=yes",
319 {
320 # Test batch=
321 "instance": NotifyJira,
322 "notify_type": NotifyType.FAILURE,
323 # Our response expected server response
324 "requests_response_text": JIRA_GOOD_RESPONSE,
325 },
326 ),
327 (
328 "jira://apikey/#team/@user/?batch=no",
329 {
330 # Test batch=
331 "instance": NotifyJira,
332 "notify_type": NotifyType.FAILURE,
333 # Our response expected server response
334 "requests_response_text": JIRA_GOOD_RESPONSE,
335 },
336 ),
337 (
338 "jira://?apikey=abc&to=user",
339 {
340 # Test Kwargs
341 "instance": NotifyJira,
342 "notify_type": NotifyType.FAILURE,
343 # Our response expected server response
344 "requests_response_text": JIRA_GOOD_RESPONSE,
345 },
346 ),
347 (
348 "jira://apikey/#team/user/",
349 {
350 "instance": NotifyJira,
351 # throw a bizzare code forcing us to fail to look it up
352 "notify_type": NotifyType.FAILURE,
353 # Our response expected server response
354 "requests_response_text": JIRA_GOOD_RESPONSE,
355 "response": False,
356 "requests_response_code": 999,
357 },
358 ),
359 (
360 "jira://apikey/#topic1/device/",
361 {
362 "instance": NotifyJira,
363 "notify_type": NotifyType.FAILURE,
364 # Our response expected server response
365 "requests_response_text": JIRA_GOOD_RESPONSE,
366 # Throws a series of connection and transfer exceptions when this
367 # flag is set and tests that we gracfully handle them
368 "test_requests_exceptions": True,
369 },
370 ),
371 )
372
373
374 def test_plugin_jira_urls(tmpdir):
375 """
376 NotifyJira() Apprise URLs
377
378 """
379
380 # Run our general tests
381 AppriseURLTester(tests=apprise_url_tests).run_all(str(tmpdir))
382
383
384 @mock.patch("requests.post")
385 def test_plugin_jira_config_files(mock_post):
386 """
387 NotifyJira() Config File Cases
388 """
389 content = """
390 urls:
391 - jira://apikey/user:
392 - priority: 1
393 tag: jira_int low
394 - priority: "1"
395 tag: jira_str_int low
396 - priority: "p1"
397 tag: jira_pstr_int low
398 - priority: low
399 tag: jira_str low
400
401 # This will take on moderate (default) priority
402 - priority: invalid
403 tag: jira_invalid
404
405 - jira://apikey2/user2:
406 - priority: 5
407 tag: jira_int emerg
408 - priority: "5"
409 tag: jira_str_int emerg
410 - priority: "p5"
411 tag: jira_pstr_int emerg
412 - priority: emergency
413 tag: jira_str emerg
414 """
415
416 # Prepare Mock
417 mock_post.return_value = requests.Request()
418 mock_post.return_value.status_code = requests.codes.ok
419 mock_post.return_value.content = JIRA_GOOD_RESPONSE
420
421 # Create ourselves a config object
422 ac = apprise.AppriseConfig()
423 assert ac.add_config(content=content) is True
424
425 aobj = apprise.Apprise()
426
427 # Add our configuration
428 aobj.add(ac)
429
430 # We should be able to read our 9 servers from that
431 # 4x low
432 # 4x emerg
433 # 1x invalid (so takes on normal priority)
434 assert len(ac.servers()) == 9
435 assert len(aobj) == 9
436 assert len(list(aobj.find(tag="low"))) == 4
437 for s in aobj.find(tag="low"):
438 assert s.priority == JiraPriority.LOW
439
440 assert len(list(aobj.find(tag="emerg"))) == 4
441 for s in aobj.find(tag="emerg"):
442 assert s.priority == JiraPriority.EMERGENCY
443
444 assert len(list(aobj.find(tag="jira_str"))) == 2
445 assert len(list(aobj.find(tag="jira_str_int"))) == 2
446 assert len(list(aobj.find(tag="jira_pstr_int"))) == 2
447 assert len(list(aobj.find(tag="jira_int"))) == 2
448
449 assert len(list(aobj.find(tag="jira_invalid"))) == 1
450 assert next(aobj.find(tag="jira_invalid")).priority == JiraPriority.NORMAL
451
452
453 @mock.patch("requests.post")
454 def test_plugin_jira_edge_case(mock_post):
455 """
456 NotifyJira() Edge Cases
457 """
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 = JIRA_GOOD_RESPONSE
462
463 instance = apprise.Apprise.instantiate("jira://apikey")
464 assert isinstance(instance, NotifyJira)
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
486
487
488 @mock.patch("requests.post")
489 def test_plugin_jira_mapping(mock_post):
490 """
491 NotifyJira() Action Mapping
492
493 Verify that custom :key=value URL mappings correctly override the default
494 action taken for each notification type, and that self.mapping (not the
495 removed module-level constant) is used in send().
496 """
497 # Prepare Mock
498 mock_post.return_value = requests.Request()
499 mock_post.return_value.status_code = requests.codes.ok
500 mock_post.return_value.content = JIRA_GOOD_RESPONSE
501
502 base_url = "https://api.atlassian.com/jsm/ops/integration/v2/alerts"
503
504 # --- Default mapping: INFO → NOTE ---
505 # INFO adds a note to an existing alert; with no stored request IDs there
506 # is nothing to annotate, so no HTTP call is made. send() still returns
507 # True (nothing to do is not an error).
508 instance = apprise.Apprise.instantiate("jira://apikey/@user")
509 assert isinstance(instance, NotifyJira)
510
511 assert instance.notify("body", "title", NotifyType.INFO) is True
512 assert mock_post.call_count == 0
513
514 # --- Default mapping: WARNING/FAILURE → NEW ---
515 # These should POST to the base alerts URL immediately.
516 mock_post.reset_mock()
517 assert instance.notify("body", "title", NotifyType.WARNING) is True
518 assert mock_post.call_count == 1
519 assert mock_post.call_args[0][0] == base_url
520
521 mock_post.reset_mock()
522 assert instance.notify("body", "title", NotifyType.FAILURE) is True
523 assert mock_post.call_count == 1
524 assert mock_post.call_args[0][0] == base_url
525
526 # --- Default mapping: SUCCESS → CLOSE ---
527 # instance already has stored request IDs from the WARNING/FAILURE calls
528 # above, so SUCCESS should POST to the /close endpoint.
529 mock_post.reset_mock()
530 assert instance.notify("body", "title", NotifyType.SUCCESS) is True
531 assert mock_post.call_count >= 1
532 assert mock_post.call_args[0][0].endswith("/close")
533
534 # Fresh instance: SUCCESS with no stored IDs → nothing to close, no call.
535 fresh = apprise.Apprise.instantiate("jira://apikey/@user")
536 mock_post.reset_mock()
537 assert fresh.notify("body", "title", NotifyType.SUCCESS) is True
538 assert mock_post.call_count == 0
539
540 # --- Custom mapping: :info=new overrides INFO → NOTE with INFO → NEW ---
541 instance2 = apprise.Apprise.instantiate("jira://apikey/@user?:info=new")
542 assert isinstance(instance2, NotifyJira)
543
544 mock_post.reset_mock()
545 assert instance2.notify("body", "title", NotifyType.INFO) is True
546 # A POST to the base alerts URL must have been made
547 assert mock_post.call_count == 1
548 assert mock_post.call_args[0][0] == base_url
549
550 # --- URL round-trip preserves custom mapping ---
551 # The colon prefix is kept unencoded for readability.
552 url = instance2.url()
553 assert ":info=new" in url
554
555 # Re-instantiate from the reconstructed URL and verify the mapping
556 instance3 = apprise.Apprise.instantiate(url)
557 assert isinstance(instance3, NotifyJira)
558 assert instance3.mapping[NotifyType.INFO] == "new"
559
560 mock_post.reset_mock()
561 assert instance3.notify("body", "title", NotifyType.INFO) is True
562 assert mock_post.call_count == 1
563 assert mock_post.call_args[0][0] == base_url