"""Lambda test module for alert_notifier.""" import json from alert_notifier import notifier import config import httpretty from mock import MagicMock from mock import patch critical_response = { 'data': [{ 'metrics': [{ 'status': 'critical', 'only_business_days': False, 'name': 'current_status', 'threshold': 2}], 'status': {'2017-01-05': '--'}, 'feed_id': '11', 'delivery_date': None, 'feed_name': '24/7'}]} ok_response = { 'data': [{ 'metrics': [{ 'status': 'ok', 'only_business_days': False, 'name': 'current_status', 'threshold': 2}], 'status': {'2017-01-05': '--'}, 'feed_id': '11', 'delivery_date': None, 'feed_name': '24/7'}]} @httpretty.activate @patch('alert_notifier.notifier.send_alerts') def test_handler_empty_response(send_alerts): """Test notifier.handler with no response.""" httpretty.register_uri( httpretty.GET, config.MONTY_URL_BASE_PATH + config.INGESTION_STATUS_URI, body=json.dumps({}), content_type='application/json') httpretty.register_uri( httpretty.GET, config.MONTY_URL_BASE_PATH + config.OUTGOING_STATUS_URI, body=json.dumps({}), content_type='application/json') notifier.handler(None, None) send_alerts.assert_not_called() @httpretty.activate @patch('alert_notifier.notifier.send_alerts') def test_handler_critical_ingestion_response(send_alerts): """Test notifier.handler with critical ingestion.""" httpretty.register_uri( httpretty.GET, config.MONTY_URL_BASE_PATH + config.INGESTION_STATUS_URI, body=json.dumps(critical_response), content_type='application/json') httpretty.register_uri( httpretty.GET, config.MONTY_URL_BASE_PATH + config.OUTGOING_STATUS_URI, body=json.dumps({}), content_type='application/json') notifier.handler(None, None) send_alerts.assert_called_with([{ 'feed_id': '11', 'feed_name': '24/7', 'metric_name': 'current_status', 'status': 'critical'}]) @httpretty.activate @patch('alert_notifier.notifier.send_alerts') def test_handler_critical_outgoing_response(send_alerts): """Test notifier.handler with critical outgoing.""" httpretty.register_uri( httpretty.GET, config.MONTY_URL_BASE_PATH + config.INGESTION_STATUS_URI, body=json.dumps({}), content_type='application/json') httpretty.register_uri( httpretty.GET, config.MONTY_URL_BASE_PATH + config.OUTGOING_STATUS_URI, body=json.dumps(critical_response), content_type='application/json') notifier.handler(None, None) send_alerts.assert_called_with([{ 'feed_id': '11', 'feed_name': '24/7', 'metric_name': 'current_status', 'status': 'critical'}]) @httpretty.activate @patch('alert_notifier.notifier.send_alerts') def test_handler_ok_ingestion_response(send_alerts): """Test notifier.handler with ok ingestion.""" httpretty.register_uri( httpretty.GET, config.MONTY_URL_BASE_PATH + config.INGESTION_STATUS_URI, body=json.dumps(ok_response), content_type='application/json') httpretty.register_uri( httpretty.GET, config.MONTY_URL_BASE_PATH + config.OUTGOING_STATUS_URI, body=json.dumps({}), content_type='application/json') notifier.handler(None, None) send_alerts.assert_not_called() @httpretty.activate @patch('alert_notifier.notifier.send_alerts') def test_handler_ok_outgoing_response(send_alerts): """Test notifier.handler with ok outgoing.""" httpretty.register_uri( httpretty.GET, config.MONTY_URL_BASE_PATH + config.INGESTION_STATUS_URI, body=json.dumps({}), content_type='application/json') httpretty.register_uri( httpretty.GET, config.MONTY_URL_BASE_PATH + config.OUTGOING_STATUS_URI, body=json.dumps(ok_response), content_type='application/json') notifier.handler(None, None) send_alerts.assert_not_called() mock_alert_configuration = [ {'metric_name': 'current_status', 'target': 'slack', 'channel': 'data-alarm'} ] mock_slack_handler = MagicMock(return_value=None) mock_handlers_config = { 'slack': mock_slack_handler } @httpretty.activate @patch('config.ALERT_CONFIGURATION', mock_alert_configuration) @patch('alert_notifier.notifier.handlers_config', mock_handlers_config) def test_slack_handler(): """Test Slack handler.""" httpretty.register_uri( httpretty.GET, config.MONTY_URL_BASE_PATH + config.INGESTION_STATUS_URI, body=json.dumps(critical_response), content_type='application/json') httpretty.register_uri( httpretty.GET, config.MONTY_URL_BASE_PATH + config.OUTGOING_STATUS_URI, body=json.dumps({}), content_type='application/json') notifier.handler(None, None) mock_slack_handler.assert_called_with(mock_alert_configuration[0], [{ 'feed_id': '11', 'feed_name': '24/7', 'metric_name': 'current_status', 'status': 'critical'}])