def test_rocketchat_uses_list_of_custom_rocket_chat_channel(): rule = { 'name': 'Test Rule', 'type': 'any', 'rocket_chat_webhook_url': ['http://please.dontgohere.rocketchat'], 'rocket_chat_channel_override': ['#test-alert', '#test-alert2'], 'alert': [] } rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = RocketChatAlerter(rule) match = {'@timestamp': '2021-01-01T00:00:00', 'somefield': 'foobarbaz'} with mock.patch('requests.post') as mock_post_request: alert.alert([match]) expected_data1 = { 'username': '******', 'channel': '#test-alert', 'emoji': ':ghost:', 'attachments': [{ 'color': 'danger', 'title': rule['name'], 'text': BasicMatchString(rule, match).__str__(), 'fields': [] }], 'text': '' } expected_data2 = { 'username': '******', 'channel': '#test-alert2', 'emoji': ':ghost:', 'attachments': [{ 'color': 'danger', 'title': rule['name'], 'text': BasicMatchString(rule, match).__str__(), 'fields': [] }], 'text': '' } mock_post_request.assert_called_with( rule['rocket_chat_webhook_url'][0], data=mock.ANY, headers={'content-type': 'application/json'}, proxies=None, timeout=10, verify=True) assert expected_data1 == json.loads( mock_post_request.call_args_list[0][1]['data']) assert expected_data2 == json.loads( mock_post_request.call_args_list[1][1]['data'])
def test_rocket_chat_kibana_discover_color(): rule = { 'name': 'Test Rule', 'type': 'any', 'alert_text_type': 'alert_text_only', 'alert': [], 'rocket_chat_webhook_url': 'http://please.dontgohere.rocket_chat', 'rocket_chat_attach_kibana_discover_url': True, 'rocket_chat_kibana_discover_color': 'blue' } rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = RocketChatAlerter(rule) match = { 'somefield': 'foobarbaz', '@timestamp': '2021-01-01T00:00:00', 'kibana_discover_url': 'http://localhost:5601/app/discover#/' } with mock.patch('requests.post') as mock_post_request: alert.alert([match]) expected_data = { 'username': '******', 'channel': '', 'emoji': ':ghost:', 'attachments': [{ 'color': 'danger', 'title': 'Test Rule', 'text': BasicMatchString(rule, match).__str__(), 'fields': [] }, { 'color': 'blue', 'title': 'Discover in Kibana', 'title_link': 'http://localhost:5601/app/discover#/' }], 'text': '' } mock_post_request.assert_called_once_with( rule['rocket_chat_webhook_url'], data=mock.ANY, headers={'content-type': 'application/json'}, proxies=None, timeout=10, verify=True) assert expected_data == json.loads( mock_post_request.call_args_list[0][1]['data'])
def test_rocket_chat_ca_certs(ca_certs, ignore_ssl_errors, excpet_verify): rule = { 'name': 'Test Rule', 'type': 'any', 'rocket_chat_webhook_url': 'http://please.dontgohere.rocketchat', 'alert_subject': 'Cool subject', 'alert': [] } if ca_certs: rule['rocket_chat_ca_certs'] = ca_certs if ignore_ssl_errors: rule['rocket_chat_ignore_ssl_errors'] = ignore_ssl_errors rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = RocketChatAlerter(rule) match = {'@timestamp': '2017-01-01T00:00:00', 'somefield': 'foobarbaz'} with mock.patch('requests.post') as mock_post_request: alert.alert([match]) expected_data = { 'username': '******', 'channel': '', 'emoji': ':ghost:', 'attachments': [{ 'color': 'danger', 'title': 'Cool subject', 'text': BasicMatchString(rule, match).__str__(), 'fields': [] }], 'text': '' } mock_post_request.assert_called_once_with( rule['rocket_chat_webhook_url'], data=mock.ANY, headers={'content-type': 'application/json'}, proxies=None, verify=excpet_verify, timeout=10) assert expected_data == json.loads( mock_post_request.call_args_list[0][1]['data'])
def test_rocketchat_msg_color_required_error(): try: rule = { 'name': 'Test Rule', 'type': 'any', 'rocket_chat_webhook_url': 'http://please.dontgohere.rocketchat', 'rocket_chat_msg_color': 'abc', 'alert_subject': 'Cool subject', 'alert': [] } rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = RocketChatAlerter(rule) match = {'@timestamp': '2021-01-01T00:00:00', 'somefield': 'foobarbaz'} with mock.patch('requests.post'): alert.alert([match]) except KeyError: assert True
def test_rocketchat_uses_custom_title(caplog): caplog.set_level(logging.INFO) rule = { 'name': 'Test Rule', 'type': 'any', 'rocket_chat_webhook_url': 'http://please.dontgohere.rocketchat', 'alert_subject': 'Cool subject', 'alert': [] } rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = RocketChatAlerter(rule) match = {'@timestamp': '2021-01-01T00:00:00', 'somefield': 'foobarbaz'} with mock.patch('requests.post') as mock_post_request: alert.alert([match]) expected_data = { 'username': '******', 'channel': '', 'emoji': ':ghost:', 'attachments': [{ 'color': 'danger', 'title': rule['alert_subject'], 'text': BasicMatchString(rule, match).__str__(), 'fields': [] }], 'text': '' } mock_post_request.assert_called_once_with( rule['rocket_chat_webhook_url'], data=mock.ANY, headers={'content-type': 'application/json'}, proxies=None, timeout=10, verify=True) assert expected_data == json.loads( mock_post_request.call_args_list[0][1]['data']) assert ('elastalert', logging.INFO, 'Alert sent to Rocket.Chat') == caplog.record_tuples[0]
def test_rocketchat_ea_exception(): with pytest.raises(EAException) as ea: rule = { 'name': 'Test Rule', 'type': 'any', 'rocket_chat_webhook_url': 'http://please.dontgohere.rocketchat', 'rocket_chat_username_override': 'elastalert2', 'rocket_chat_msg_pretext': 'pretext value', 'alert_subject': 'Cool subject', 'alert': [] } rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = RocketChatAlerter(rule) match = {'@timestamp': '2021-01-01T00:00:00', 'somefield': 'foobarbaz'} mock_run = mock.MagicMock(side_effect=RequestException) with mock.patch('requests.post', mock_run), pytest.raises(RequestException): alert.alert([match]) assert 'Error posting to Rocket.Chat: ' in str(ea)