| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
from json import dumps |
| 29 |
|
| 30 |
|
| 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 |
|
| 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 |
|
| 58 |
apprise_url_tests = ( |
| 59 |
( |
| 60 |
"opsgenie://", |
| 61 |
{ |
| 62 |
|
| 63 |
"instance": TypeError, |
| 64 |
}, |
| 65 |
), |
| 66 |
( |
| 67 |
"opsgenie://:@/", |
| 68 |
{ |
| 69 |
|
| 70 |
"instance": TypeError, |
| 71 |
}, |
| 72 |
), |
| 73 |
( |
| 74 |
"opsgenie://%20%20/", |
| 75 |
{ |
| 76 |
|
| 77 |
"instance": TypeError, |
| 78 |
}, |
| 79 |
), |
| 80 |
( |
| 81 |
"opsgenie://apikey/user/?region=xx", |
| 82 |
{ |
| 83 |
|
| 84 |
"instance": TypeError, |
| 85 |
}, |
| 86 |
), |
| 87 |
( |
| 88 |
"opsgenie://user@apikey/", |
| 89 |
{ |
| 90 |
|
| 91 |
"instance": NotifyOpsgenie, |
| 92 |
"notify_type": NotifyType.WARNING, |
| 93 |
|
| 94 |
"requests_response_text": "{", |
| 95 |
|
| 96 |
"notify_response": False, |
| 97 |
}, |
| 98 |
), |
| 99 |
( |
| 100 |
"opsgenie://apikey/", |
| 101 |
{ |
| 102 |
|
| 103 |
"instance": NotifyOpsgenie, |
| 104 |
"notify_type": NotifyType.FAILURE, |
| 105 |
|
| 106 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 107 |
}, |
| 108 |
), |
| 109 |
( |
| 110 |
"opsgenie://apikey/user", |
| 111 |
{ |
| 112 |
|
| 113 |
"instance": NotifyOpsgenie, |
| 114 |
"notify_type": NotifyType.FAILURE, |
| 115 |
|
| 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 |
|
| 124 |
"instance": NotifyOpsgenie, |
| 125 |
"notify_type": NotifyType.FAILURE, |
| 126 |
|
| 127 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 128 |
}, |
| 129 |
), |
| 130 |
( |
| 131 |
"opsgenie://apikey/@user?entity=A%20Entity", |
| 132 |
{ |
| 133 |
|
| 134 |
"instance": NotifyOpsgenie, |
| 135 |
"notify_type": NotifyType.FAILURE, |
| 136 |
|
| 137 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 138 |
}, |
| 139 |
), |
| 140 |
( |
| 141 |
"opsgenie://apikey/@user?alias=An%20Alias", |
| 142 |
{ |
| 143 |
|
| 144 |
"instance": NotifyOpsgenie, |
| 145 |
"notify_type": NotifyType.FAILURE, |
| 146 |
|
| 147 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 148 |
}, |
| 149 |
), |
| 150 |
|
| 151 |
( |
| 152 |
"opsgenie://apikey/@user?action=invalid", |
| 153 |
{ |
| 154 |
|
| 155 |
"instance": TypeError, |
| 156 |
}, |
| 157 |
), |
| 158 |
( |
| 159 |
"opsgenie://from@apikey/@user?:invalid=note", |
| 160 |
{ |
| 161 |
|
| 162 |
"instance": TypeError, |
| 163 |
}, |
| 164 |
), |
| 165 |
( |
| 166 |
"opsgenie://apikey/@user?:warning=invalid", |
| 167 |
{ |
| 168 |
|
| 169 |
"instance": TypeError, |
| 170 |
}, |
| 171 |
), |
| 172 |
|
| 173 |
( |
| 174 |
"opsgenie://apikey/@user?entity=index&action=new", |
| 175 |
{ |
| 176 |
|
| 177 |
"instance": NotifyOpsgenie, |
| 178 |
"notify_type": NotifyType.FAILURE, |
| 179 |
|
| 180 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 181 |
}, |
| 182 |
), |
| 183 |
|
| 184 |
( |
| 185 |
"opsgenie://apikey/@user?entity=index&action=acknowledge", |
| 186 |
{ |
| 187 |
|
| 188 |
"instance": NotifyOpsgenie, |
| 189 |
"notify_type": NotifyType.SUCCESS, |
| 190 |
|
| 191 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 192 |
}, |
| 193 |
), |
| 194 |
( |
| 195 |
"opsgenie://from@apikey/@user?entity=index&action=note", |
| 196 |
{ |
| 197 |
|
| 198 |
"instance": NotifyOpsgenie, |
| 199 |
"notify_type": NotifyType.SUCCESS, |
| 200 |
|
| 201 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 202 |
}, |
| 203 |
), |
| 204 |
( |
| 205 |
"opsgenie://from@apikey/@user?entity=index&action=note", |
| 206 |
{ |
| 207 |
|
| 208 |
"instance": NotifyOpsgenie, |
| 209 |
"notify_type": NotifyType.SUCCESS, |
| 210 |
|
| 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 |
|
| 220 |
"instance": NotifyOpsgenie, |
| 221 |
"notify_type": NotifyType.SUCCESS, |
| 222 |
|
| 223 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 224 |
}, |
| 225 |
), |
| 226 |
( |
| 227 |
"opsgenie://apikey/@user?entity=index&action=delete", |
| 228 |
{ |
| 229 |
|
| 230 |
"instance": NotifyOpsgenie, |
| 231 |
"notify_type": NotifyType.SUCCESS, |
| 232 |
|
| 233 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 234 |
}, |
| 235 |
), |
| 236 |
|
| 237 |
( |
| 238 |
"opsgenie://apikey/@user?entity=index2&:info=new", |
| 239 |
{ |
| 240 |
|
| 241 |
"instance": NotifyOpsgenie, |
| 242 |
"notify_type": NotifyType.INFO, |
| 243 |
|
| 244 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 245 |
}, |
| 246 |
), |
| 247 |
( |
| 248 |
"opsgenie://joe@apikey/@user?priority=p3", |
| 249 |
{ |
| 250 |
|
| 251 |
"instance": NotifyOpsgenie, |
| 252 |
"notify_type": NotifyType.FAILURE, |
| 253 |
|
| 254 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 255 |
}, |
| 256 |
), |
| 257 |
( |
| 258 |
"opsgenie://apikey/?tags=comma,separated", |
| 259 |
{ |
| 260 |
|
| 261 |
|
| 262 |
"instance": NotifyOpsgenie, |
| 263 |
"notify_type": NotifyType.FAILURE, |
| 264 |
|
| 265 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 266 |
}, |
| 267 |
), |
| 268 |
( |
| 269 |
"opsgenie://apikey/@user?priority=invalid", |
| 270 |
{ |
| 271 |
|
| 272 |
"instance": NotifyOpsgenie, |
| 273 |
"notify_type": NotifyType.FAILURE, |
| 274 |
|
| 275 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 276 |
}, |
| 277 |
), |
| 278 |
( |
| 279 |
"opsgenie://apikey/[email protected]/#team/*sche/^esc/%20/a", |
| 280 |
{ |
| 281 |
|
| 282 |
|
| 283 |
"instance": NotifyOpsgenie, |
| 284 |
"notify_type": NotifyType.FAILURE, |
| 285 |
|
| 286 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 287 |
}, |
| 288 |
), |
| 289 |
( |
| 290 |
f"opsgenie://apikey/@{UUID4}/#{UUID4}/*{UUID4}/^{UUID4}/", |
| 291 |
{ |
| 292 |
|
| 293 |
"instance": NotifyOpsgenie, |
| 294 |
"notify_type": NotifyType.FAILURE, |
| 295 |
|
| 296 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 297 |
}, |
| 298 |
), |
| 299 |
|
| 300 |
|
| 301 |
( |
| 302 |
f"opsgenie://apikey/{UUID4}/#{UUID4}/*{UUID4}/^{UUID4}/", |
| 303 |
{ |
| 304 |
|
| 305 |
"instance": NotifyOpsgenie, |
| 306 |
"notify_type": NotifyType.FAILURE, |
| 307 |
|
| 308 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 309 |
}, |
| 310 |
), |
| 311 |
( |
| 312 |
"opsgenie://apikey?to=#team,user&+key=value&+type=override", |
| 313 |
{ |
| 314 |
|
| 315 |
"instance": NotifyOpsgenie, |
| 316 |
"notify_type": NotifyType.FAILURE, |
| 317 |
|
| 318 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 319 |
}, |
| 320 |
), |
| 321 |
( |
| 322 |
"opsgenie://apikey/#team/@user/?batch=yes", |
| 323 |
{ |
| 324 |
|
| 325 |
"instance": NotifyOpsgenie, |
| 326 |
"notify_type": NotifyType.FAILURE, |
| 327 |
|
| 328 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 329 |
}, |
| 330 |
), |
| 331 |
( |
| 332 |
"opsgenie://apikey/#team/@user/?batch=no", |
| 333 |
{ |
| 334 |
|
| 335 |
"instance": NotifyOpsgenie, |
| 336 |
"notify_type": NotifyType.FAILURE, |
| 337 |
|
| 338 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 339 |
}, |
| 340 |
), |
| 341 |
( |
| 342 |
"opsgenie://?apikey=abc&to=user", |
| 343 |
{ |
| 344 |
|
| 345 |
"instance": NotifyOpsgenie, |
| 346 |
"notify_type": NotifyType.FAILURE, |
| 347 |
|
| 348 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 349 |
}, |
| 350 |
), |
| 351 |
( |
| 352 |
"opsgenie://apikey/#team/user/", |
| 353 |
{ |
| 354 |
"instance": NotifyOpsgenie, |
| 355 |
|
| 356 |
"notify_type": NotifyType.FAILURE, |
| 357 |
|
| 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 |
|
| 369 |
"requests_response_text": OPSGENIE_GOOD_RESPONSE, |
| 370 |
|
| 371 |
|
| 372 |
"test_requests_exceptions": True, |
| 373 |
}, |
| 374 |
), |
| 375 |
) |
| 376 |
|
| 377 |
|
| 378 |
def test_plugin_opsgenie_urls(tmpdir): |
| 379 |
"""NotifyOpsgenie() Apprise URLs.""" |
| 380 |
|
| 381 |
|
| 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 |
|
| 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 |
|
| 421 |
ac = apprise.AppriseConfig() |
| 422 |
assert ac.add_config(content=content) is True |
| 423 |
|
| 424 |
aobj = apprise.Apprise() |
| 425 |
|
| 426 |
|
| 427 |
aobj.add(ac) |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 476 |
instance.store["a62f2225bf"] = "garbage" |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
assert instance.notify("test", "key", NotifyType.FAILURE) is True |
| 481 |
assert len(instance.store.keys()) == 1 |
| 482 |
|
| 483 |
|
| 484 |
assert instance.notify("test", "key2", NotifyType.FAILURE) is True |
| 485 |
assert len(instance.store.keys()) == 2 |