Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gitter #230

Merged
merged 2 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion elastalert/alerters/gitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def alert(self, matches):
}

try:
response = requests.post(self.gitter_webhook_url, json.dumps(payload, cls=DateTimeEncoder), headers=headers, proxies=proxies)
response = requests.post(self.gitter_webhook_url,
data=json.dumps(payload, cls=DateTimeEncoder),
headers=headers,
proxies=proxies)
response.raise_for_status()
except RequestException as e:
raise EAException("Error posting to Gitter: %s" % e)
Expand Down
136 changes: 61 additions & 75 deletions tests/alerters/gitter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,22 @@
from elastalert.util import EAException


def test_gitter_msg_level_default():
@pytest.mark.parametrize('msg_level, except_msg_level', [
('', 'error'),
('error', 'error'),
('info', 'info')
])
def test_gitter_msg_level(msg_level, except_msg_level):
rule = {
'name': 'Test Gitter Rule',
'type': 'any',
'gitter_webhook_url': 'https://webhooks.gitter.im/e/xxxxx',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = GitterAlerter(rule)
match = {
'@timestamp': '2021-01-01T00:00:00',
'somefield': 'foobarbaz'
}
with mock.patch('requests.post') as mock_post_request:
alert.alert([match])

expected_data = {
'message': 'Test Gitter Rule\n\n@timestamp: 2021-01-01T00:00:00\nsomefield: foobarbaz\n',
'level': 'error'
}

mock_post_request.assert_called_once_with(
rule['gitter_webhook_url'],
mock.ANY,
headers={'content-type': 'application/json'},
proxies=None
)

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

if msg_level != '':
rule['gitter_msg_level'] = msg_level

def test_gitter_msg_level_info():
rule = {
'name': 'Test Gitter Rule',
'type': 'any',
'gitter_webhook_url': 'https://webhooks.gitter.im/e/xxxxx',
'gitter_msg_level': 'info',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = GitterAlerter(rule)
Expand All @@ -63,53 +37,18 @@ def test_gitter_msg_level_info():

expected_data = {
'message': 'Test Gitter Rule\n\n@timestamp: 2021-01-01T00:00:00\nsomefield: foobarbaz\n',
'level': 'info'
'level': except_msg_level
}

mock_post_request.assert_called_once_with(
rule['gitter_webhook_url'],
mock.ANY,
data=mock.ANY,
headers={'content-type': 'application/json'},
proxies=None
)

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


def test_gitter_msg_level_error():
rule = {
'name': 'Test Gitter Rule',
'type': 'any',
'gitter_webhook_url': 'https://webhooks.gitter.im/e/xxxxx',
'gitter_msg_level': 'error',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = GitterAlerter(rule)
match = {
'@timestamp': '2021-01-01T00:00:00',
'somefield': 'foobarbaz'
}
with mock.patch('requests.post') as mock_post_request:
alert.alert([match])
expected_data = {
'message': 'Test Gitter Rule\n\n@timestamp: 2021-01-01T00:00:00\nsomefield: foobarbaz\n',
'level': 'error'
}

mock_post_request.assert_called_once_with(
rule['gitter_webhook_url'],
mock.ANY,
headers={'content-type': 'application/json'},
proxies=None
)

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


def test_gitter_proxy():
Expand Down Expand Up @@ -137,14 +76,13 @@ def test_gitter_proxy():

mock_post_request.assert_called_once_with(
rule['gitter_webhook_url'],
mock.ANY,
data=mock.ANY,
headers={'content-type': 'application/json'},
proxies={'https': 'http://proxy.url'}
)

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


def test_gitter_ea_exception():
Expand All @@ -169,3 +107,51 @@ def test_gitter_ea_exception():
alert.alert([match])
except EAException:
assert True


def test_gitter_getinfo():
rule = {
'name': 'Test Gitter Rule',
'type': 'any',
'gitter_webhook_url': 'https://webhooks.gitter.im/e/xxxxx',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = GitterAlerter(rule)

expected_data = {
'type': 'gitter',
'gitter_webhook_url': 'https://webhooks.gitter.im/e/xxxxx'
}
actual_data = alert.get_info()
assert expected_data == actual_data


@pytest.mark.parametrize('gitter_webhook_url, expected_data', [
('', True),
('https://webhooks.gitter.im/e/xxxxx',
{
'type': 'gitter',
'gitter_webhook_url': 'https://webhooks.gitter.im/e/xxxxx'
})
])
def test_gitter_key_error(gitter_webhook_url, expected_data):
try:
rule = {
'name': 'Test Gitter Rule',
'type': 'any',
'alert': []
}

if gitter_webhook_url != '':
rule['gitter_webhook_url'] = gitter_webhook_url

rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = GitterAlerter(rule)

actual_data = alert.get_info()
assert expected_data == actual_data
except KeyError:
assert expected_data