"""Lambda test module.""" from unittest import mock import pytest import index # noqa REMOVE_EVENT_BY_USER = {'Records': [ {'eventVersion': '2.0', 'eventSource': 'aws:s3', 'awsRegion': 'us-east-1', 'eventTime': '2018-03-12T15:35:26.133Z', 'eventName': 'ObjectRemoved:DeleteMarkerCreated', 'userIdentity': { 'principalId': 'AWS:AIDAJG7JCD5AVV4V3CKH2'}, 'requestParameters': { 'sourceIPAddress': '291.135.737.559'}, 'responseElements': { 'x-amz-request-id': 'E51D1AD9F7D3031D', 'x-amz-id-2': 'asd'}, 's3': { 's3SchemaVersion': '1.0', 'configurationId': 'asd', 'bucket': { 'name': 'dev-orchdbucket', 'ownerIdentity': {'principalId': 'asd'}, 'arn': 'arn:aws:s3:::dev-orchdbucket' }, 'object': { 'key': 'dev-statement-attachments/L25824_227_tst.doc', 'versionId': 'asd', 'sequencer': 'asd'}}}]} @mock.patch('index.util') @mock.patch('index.dynamodb') @mock.patch('index.common_config') def test_handler( common_config, dynamodb, util): """Test index.handler push an event to Kinesis.""" s3_object = REMOVE_EVENT_BY_USER['Records'][0]['s3']['object'] label_id = 4242 filename = 'test_file.jpg' filename_quarter = 'test_file.jpg(242)' period_id = 242 account_type = 'T' table_name = 'test_table_name' quarter_periods = (1, 242, 1) common_config.DYNAMODB_TABLE_SUCCESS = table_name # ensure that ignore all events is not a MagicMock object. common_config.IGNORE_ALL_EVENTS = None util.parse_file_key.return_value = { 'label_id': label_id, 'filename': filename, 'period_id': period_id, 'account_type': account_type } util.get_quarter_periods_by_period.return_value = quarter_periods util.get_attachment_primary_key_value.return_value = 'T4242_1_242_1' util.format_quarter_file_name.return_value = filename_quarter expected_key_single = { 'label_type_id_period_id': 'T{}_{}'.format(label_id, period_id), 'file_name': filename} expected_key_quarter = { 'label_type_id_period_id': 'T{}_{}'.format(label_id, '1_242_1'), 'file_name': filename_quarter} index.handler(REMOVE_EVENT_BY_USER, None) util.parse_file_key.assert_called_once_with(s3_object['key']) util.get_attachment_primary_key_value.assert_called_once_with( account_type, label_id, quarter_periods) dynamodb.batch_delete_data.assert_called_once_with( table_name=table_name, keys=(expected_key_single, expected_key_quarter)) @mock.patch('index.util') @mock.patch('index.dynamodb') @mock.patch('index.ignore_event') @mock.patch('index.common_config') def test_handler_ignores_events( common_config, ignore_event, dynamodb, util): """Test handler ignores events.""" ignore_event.return_value = True # ensure that ignore all events is not a MagicMock object. common_config.IGNORE_ALL_EVENTS = None assert index.handler(REMOVE_EVENT_BY_USER, None) is None funcs = [ util.parse_file_key, util.get_quarter_periods_by_period, util.get_attachment_primary_key_value, util.format_quarter_file_name, dynamodb.batch_delete_data] for func in funcs: func.assert_not_called() @mock.patch('index.util') @mock.patch('index.dynamodb') @mock.patch('index.ignore_event') @mock.patch('index.common_config') def test_handler_ignores_all_events( common_config, ignore_event, dynamodb, util): """Test handler ignores events.""" ignore_event.return_value = False # ensure that ignore comes from common config/env variable common_config.IGNORE_ALL_EVENTS = True assert index.handler(REMOVE_EVENT_BY_USER, None) is None funcs = [ util.parse_file_key, util.get_quarter_periods_by_period, util.get_attachment_primary_key_value, util.format_quarter_file_name, dynamodb.batch_delete_data] for func in funcs: func.assert_not_called() @pytest.mark.parametrize('event, expected_result', [ ({'Records': [{'userIdentity': {'principalId': 'do-not-ignore'}}]}, False), ({'Records': [{'userIdentity': {'principalId': 'please-ignore'}}]}, True), ({'Records': [{'userIdentity': {'principalId': 'ignore-please'}}]}, True) ]) @mock.patch('index.config') def test_ignore_event(config, event, expected_result): """Test ignore_event utility function.""" config.PRINCIPAL_IDS_TO_IGNORE = ['please-ignore', 'ignore-please'] assert index.ignore_event(event) == expected_result