Skip to content

Commit

Permalink
Add unit test for testing formatted opsgenie_addr
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedelema17 authored Nov 11, 2024
1 parent e9d03ed commit 0420e17
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/alerters/opsgenie_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,3 +1258,33 @@ def test_opsgenie_get_details2():
actual = alert.get_details(match)
expected = {}
assert expected == actual


def test_formatted_opsgenie_addr(caplog):
caplog.set_level(logging.INFO)
rule = {
'name': 'testOGalert',
'opsgenie_key': 'ogkey',
'opsgenie_addr': 'https://api.opsgenie.com/v2/alerts/{alert_id}',
'type': mock_rule()
}
with mock.patch('requests.post') as mock_post:
rep = requests
rep.status_code = 202
url = rule.get('opsgenie_addr')
matches = [{'alert_id': '1234','@timestamp': '2014-10-31T00:00:00'}]
mock_post.return_value = rep
url.format(**matches[0])

alert = OpsGenieAlerter(rule)
alert.alert(matches)
mcal = mock_post._mock_call_args_list
assert mcal[0][0][0] == (f'https://api.opsgenie.com/v2/alerts/{matches[0].get("alert_id")}')
assert mock_post.called

assert mcal[0][1]['headers']['Authorization'] == 'GenieKey ogkey'
assert mcal[0][1]['json']['source'] == 'ElastAlert'
user, level, message = caplog.record_tuples[0]
assert "Error response from https://api.opsgenie.com/v2/alerts \n API Response: <MagicMock name='post()' id=" not in message
assert ('elastalert', logging.INFO, 'Alert sent to OpsGenie') == caplog.record_tuples[0]

0 comments on commit 0420e17

Please sign in to comment.