"""Lambda test module.""" from flexmock import flexmock import pytest from src import app from src import status from src.config import logger from constants import general @pytest.fixture def correct_lambda_event_object(): """Return correct lambda event object.""" return { 'InitialInput': 'Something', 'detail': { 'requestParameters': { 'key': 'test_key.ext' } }, 'Error': { 'Error': 'error_code', 'Cause': 'some error cause' }, 'Function': 'acknowledge' } def test_handler_success(correct_lambda_event_object): """Test app.handler function success.""" expected_input_params = correct_lambda_event_object.copy() del expected_input_params['Error'] del expected_input_params['Function'] expected_filename = expected_input_params['detail']['requestParameters']['key'] expected_error_status = 'acknowledge_error' (flexmock(status).should_receive('send_general_status').with_args( correct_lambda_event_object.get('Function'), expected_error_status, expected_filename, correct_lambda_event_object.get('Error').get('Error'), correct_lambda_event_object.get('Error').get('Cause'), None, expected_input_params)) result = app.handler(correct_lambda_event_object, None) assert result def test_handler_success_without_mapping(correct_lambda_event_object): """Test app.handler function success.""" expected_input_params = correct_lambda_event_object.copy() del expected_input_params['Error'] del expected_input_params['Function'] expected_filename = expected_input_params['detail']['requestParameters']['key'] correct_lambda_event_object['Function'] = 'some_function' expected_error_status = general.GENERAL_ERROR_STATUS (flexmock(status) .should_receive('send_general_status') .with_args( correct_lambda_event_object.get('Function'), expected_error_status, expected_filename, correct_lambda_event_object.get('Error').get('Error'), correct_lambda_event_object.get('Error').get('Cause'), None, expected_input_params)) result = app.handler(correct_lambda_event_object, None) assert result def test_handler_failure(correct_lambda_event_object): """Test app.handler function failure.""" expected_error_message = 'error_message' expected_input_params = correct_lambda_event_object.copy() del expected_input_params['Error'] del expected_input_params['Function'] expected_filename = expected_input_params['detail']['requestParameters']['key'] expected_error_status = 'acknowledge_error' expected_exception = Exception(expected_error_message) (flexmock(status).should_receive('send_general_status').with_args( correct_lambda_event_object.get('Function'), expected_error_status, expected_filename, correct_lambda_event_object.get('Error').get('Error'), correct_lambda_event_object.get('Error').get('Cause'), None, expected_input_params) .and_raise(expected_exception)) (flexmock(logger).should_receive('exception').with_args(str(expected_exception))) with pytest.raises(Exception) as e: app.handler(correct_lambda_event_object, None) assert e.value.args[0] == expected_error_message def test_handler_prefers_top_level_key(correct_lambda_event_object): """Test app.handler uses top-level key before detail fallbacks.""" correct_lambda_event_object['key'] = 'test_key.ext' correct_lambda_event_object['detail']['object'] = {'key': 'test_key.ext'} expected_input_params = correct_lambda_event_object.copy() del expected_input_params['Error'] del expected_input_params['Function'] expected_error_status = 'acknowledge_error' expected_filename = 'test_key.ext' (flexmock(status).should_receive('send_general_status').with_args( correct_lambda_event_object.get('Function'), expected_error_status, expected_filename, correct_lambda_event_object.get('Error').get('Error'), correct_lambda_event_object.get('Error').get('Cause'), None, expected_input_params)) result = app.handler(correct_lambda_event_object, None) assert result def test_handler_uses_detail_object_key_when_top_level_missing(correct_lambda_event_object): """Test app.handler uses detail.object.key when top-level key is not present.""" correct_lambda_event_object['detail']['object'] = {'key': 'test_key.ext'} expected_input_params = correct_lambda_event_object.copy() del expected_input_params['Error'] del expected_input_params['Function'] expected_error_status = 'acknowledge_error' expected_filename = 'test_key.ext' (flexmock(status).should_receive('send_general_status').with_args( correct_lambda_event_object.get('Function'), expected_error_status, expected_filename, correct_lambda_event_object.get('Error').get('Error'), correct_lambda_event_object.get('Error').get('Cause'), None, expected_input_params)) result = app.handler(correct_lambda_event_object, None) assert result def test_handler_uses_guardduty_s3_object_details(correct_lambda_event_object): """Test app.handler extracts key from GuardDuty malware scan result event.""" correct_lambda_event_object['detail'] = { 's3ObjectDetails': { 'bucketName': 'test_bucket', 'objectKey': 'test_key.ext', } } expected_input_params = correct_lambda_event_object.copy() del expected_input_params['Error'] del expected_input_params['Function'] expected_error_status = 'acknowledge_error' expected_filename = 'test_key.ext' (flexmock(status).should_receive('send_general_status').with_args( correct_lambda_event_object.get('Function'), expected_error_status, expected_filename, correct_lambda_event_object.get('Error').get('Error'), correct_lambda_event_object.get('Error').get('Cause'), None, expected_input_params)) result = app.handler(correct_lambda_event_object, None) assert result