"""Lambda test module.""" import json import flexmock import pytest from urllib import request from urllib.error import URLError, HTTPError from common_config import logger # noqa from constants import errors # noqa import config # noqa import index # noqa @pytest.fixture def correct_lambda_event_object(): """Return correct lambda event object.""" return { 'Records': [ { 'Sns': { 'Message': '{"status": "ok", "foo": "bar"}', 'Timestamp': 'timestamp', 'TopicArn': 'Topic' } } ] } @pytest.fixture def correct_slack_message_object(correct_lambda_event_object): """Return correct slack message object""" message = json.loads( correct_lambda_event_object['Records'][0]['Sns']['Message']) return { 'channel': config.SLACK_CHANNEL, 'username': 'ALERT', 'text': correct_lambda_event_object['Records'][0]['Sns']['TopicArn'], 'icon_emoji': ':aws:', 'attachments': [{ 'text': json.dumps(message), 'color': config.SLACK_MESSAGE_COLOR }] } def test_handler_success( correct_lambda_event_object, correct_slack_message_object): """Test index.handler function success.""" response = flexmock(read=lambda: 'read') (flexmock(request) .should_receive('Request') .with_args( config.SLACK_HOOK_URL, json.dumps(correct_slack_message_object).encode('utf-8')) .and_return('Request') .once()) (flexmock(request) .should_receive('urlopen') .with_args('Request') .and_return(response) .once()) response.should_receive('read').once() result = index.handler(correct_lambda_event_object, None) assert result is True def test_handler_httperror_exception( correct_lambda_event_object, correct_slack_message_object): """Test index.handler function with http error.""" (flexmock(request) .should_receive('Request') .with_args( config.SLACK_HOOK_URL, json.dumps(correct_slack_message_object).encode('utf-8')) .and_return('Request') .once()) (flexmock(request) .should_receive('urlopen') .with_args('Request') .and_raise( HTTPError, url='some_url', code='code', msg='error_message', hdrs='hdrs', fp='fp') .once()) (flexmock(logger) .should_receive('error') .with_args( errors.SLACK_HTTP_ERROR_MESSAGE.format( code='code', reason='error_message')) .once()) with pytest.raises(HTTPError): index.handler(correct_lambda_event_object, None) def test_handler_urlerror_exception( correct_lambda_event_object, correct_slack_message_object): """Test index.handler function with Url error.""" (flexmock(request) .should_receive('Request') .with_args( config.SLACK_HOOK_URL, json.dumps(correct_slack_message_object).encode('utf-8')) .and_return('Request') .once()) (flexmock(request) .should_receive('urlopen') .with_args('Request') .and_raise(URLError, reason='error_message') .once()) (flexmock(logger) .should_receive('error') .with_args( errors.SLACK_URL_ERROR_MESSAGE.format(reason='error_message')) .once()) with pytest.raises(URLError): index.handler(correct_lambda_event_object, None) def test_handler_exception( correct_lambda_event_object, correct_slack_message_object): """Test index.handler function with exception.""" (flexmock(request) .should_receive('Request') .with_args( config.SLACK_HOOK_URL, json.dumps(correct_slack_message_object).encode('utf-8')) .and_return('Request')) (flexmock(request) .should_receive('urlopen') .with_args('Request') .and_raise(Exception, 'error_message')) with pytest.raises(Exception) as exception_info: index.handler(correct_lambda_event_object, None) assert exception_info.value.args[0] == 'error_message' def test_handler_show_filter_success( correct_lambda_event_object, correct_slack_message_object): """Test index.handler function success.""" config.MESSAGE_SHOW_FILTER = '{"foo": "bar"}' response = flexmock(read=lambda: 'read') (flexmock(request) .should_receive('Request') .with_args( config.SLACK_HOOK_URL, json.dumps(correct_slack_message_object).encode('utf-8')) .and_return('Request') .once()) (flexmock(request) .should_receive('urlopen') .with_args('Request') .and_return(response) .once()) response.should_receive('read').once() result = index.handler(correct_lambda_event_object, None) assert result is True def test_handler_show_filter_not_satisfied(correct_lambda_event_object): """Test index.handler function with show filter.""" config.MESSAGE_SHOW_FILTER = '{"some": "filter"}' response = flexmock(read=lambda: 'read') (flexmock(request) .should_receive('Request') .never()) (flexmock(request) .should_receive('urlopen') .never()) response.should_receive('read').never() result = index.handler(correct_lambda_event_object, None) assert result is True def test_handler_hide_filter_success(correct_lambda_event_object): """Test index.handler function success with hide filter.""" config.MESSAGE_SHOW_FILTER = None config.MESSAGE_HIDE_FILTER = '{"foo": "bar"}' response = flexmock(read=lambda: 'read') (flexmock(request) .should_receive('Request') .never()) (flexmock(request) .should_receive('urlopen') .never()) response.should_receive('read').never() result = index.handler(correct_lambda_event_object, None) assert result is True