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