Skip to content

Commit

Permalink
Update mattermost_test.py
Browse files Browse the repository at this point in the history
add opensearch discover url related test
  • Loading branch information
luffynextgen authored Nov 15, 2023
1 parent a520201 commit 24488fb
Showing 1 changed file with 198 additions and 0 deletions.
198 changes: 198 additions & 0 deletions tests/alerters/mattermost_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,204 @@ def test_mattermost_kibana_discover_color():
actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
assert expected_data == actual_data

def test_mattermost_attach_opensearch_discover_url_when_generated():
rule = {
'name': 'Test Rule',
'type': 'any',
'alert_text_type': 'alert_text_only',
'mattermost_attach_opensearch_discover_url': True,
'mattermost_webhook_url': 'http://please.dontgohere.mattermost',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = MattermostAlerter(rule)
match = {
'@timestamp': '2021-01-01T00:00:00',
'opensearch_discover_url': 'http://localhost:5601/app/discover#/'
}
with mock.patch('requests.post') as mock_post_request:
alert.alert([match])

expected_data = {
'attachments': [
{
'fallback': 'Test Rule: ',
'color': 'danger',
'title': 'Test Rule',
'pretext': '',
'fields': [],
'text': 'Test Rule\n\n'
},
{
'color': '#ec4b98',
'title': 'Discover in opensearch',
'title_link': 'http://localhost:5601/app/discover#/'
}
],
'username': 'elastalert',
'channel': '',
'icon_emoji': ':ghost:'
}
mock_post_request.assert_called_once_with(
rule['mattermost_webhook_url'],
data=mock.ANY,
headers={'content-type': 'application/json'},
verify=True,
proxies=None
)

actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
assert expected_data == actual_data


def test_mattermost_attach_opensearch_discover_url_when_not_generated():
rule = {
'name': 'Test Rule',
'type': 'any',
'alert_text_type': 'alert_text_only',
'mattermost_attach_opensearch_discover_url': True,
'mattermost_webhook_url': 'http://please.dontgohere.mattermost',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = MattermostAlerter(rule)
match = {
'@timestamp': '2021-01-01T00:00:00'
}
with mock.patch('requests.post') as mock_post_request:
alert.alert([match])

expected_data = {
'attachments': [
{
'fallback': 'Test Rule: ',
'color': 'danger',
'title': 'Test Rule',
'pretext': '',
'fields': [],
'text': 'Test Rule\n\n'
}
],
'username': 'elastalert',
'channel': '',
'icon_emoji': ':ghost:'
}
mock_post_request.assert_called_once_with(
rule['mattermost_webhook_url'],
data=mock.ANY,
headers={'content-type': 'application/json'},
verify=True,
proxies=None
)

actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
assert expected_data == actual_data


def test_mattermost_opensearch_discover_title():
rule = {
'name': 'Test Rule',
'type': 'any',
'alert_text_type': 'alert_text_only',
'mattermost_attach_opensearch_discover_url': True,
'mattermost_opensearch_discover_title': 'Click to discover in opensearch',
'mattermost_webhook_url': 'http://please.dontgohere.mattermost',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = MattermostAlerter(rule)
match = {
'@timestamp': '2021-01-01T00:00:00',
'opensearch_discover_url': 'http://localhost:5601/app/discover#/'
}
with mock.patch('requests.post') as mock_post_request:
alert.alert([match])

expected_data = {
'attachments': [
{
'fallback': 'Test Rule: ',
'color': 'danger',
'title': 'Test Rule',
'pretext': '',
'fields': [],
'text': 'Test Rule\n\n'
},
{
'color': '#ec4b98',
'title': 'Click to discover in opensearch',
'title_link': 'http://localhost:5601/app/discover#/'
}
],
'username': 'elastalert',
'channel': '',
'icon_emoji': ':ghost:'
}
mock_post_request.assert_called_once_with(
rule['mattermost_webhook_url'],
data=mock.ANY,
headers={'content-type': 'application/json'},
verify=True,
proxies=None
)

actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
assert expected_data == actual_data


def test_mattermost_opensearch_discover_color():
rule = {
'name': 'Test Rule',
'type': 'any',
'alert_text_type': 'alert_text_only',
'mattermost_attach_opensearch_discover_url': True,
'mattermost_opensearch_discover_color': 'blue',
'mattermost_webhook_url': 'http://please.dontgohere.mattermost',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = MattermostAlerter(rule)
match = {
'@timestamp': '2021-01-01T00:00:00',
'opensearch_discover_url': 'http://localhost:5601/app/discover#/'
}
with mock.patch('requests.post') as mock_post_request:
alert.alert([match])

expected_data = {
'attachments': [
{
'fallback': 'Test Rule: ',
'color': 'danger',
'title': 'Test Rule',
'pretext': '',
'fields': [],
'text': 'Test Rule\n\n'
},
{
'color': 'blue',
'title': 'Discover in opensearch',
'title_link': 'http://localhost:5601/app/discover#/'
}
],
'username': 'elastalert',
'channel': '',
'icon_emoji': ':ghost:'
}
mock_post_request.assert_called_once_with(
rule['mattermost_webhook_url'],
data=mock.ANY,
headers={'content-type': 'application/json'},
verify=True,
proxies=None
)

actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
assert expected_data == actual_data

def test_mattermost_username_override():
rule = {
Expand Down

0 comments on commit 24488fb

Please sign in to comment.