"""Lambda test module.""" from io import BytesIO from unittest.mock import MagicMock, patch from moto import mock_s3 import boto3 import paramiko import index import config class LambdaContext: """This is for passing around the invoke context to logger.""" aws_request_id = '1234-1234' sample_event = { 'upcs': '[1,2,3]', 'asset_type': '1', 'message': 'TESTING', 'physical_location_id': '[1, 2]' } sample_context = None @patch('lambdacommon.util.dd_connection') @patch('index.process_direct_delivery_records') def test_index_handler(mock_process_direct_delivery_records, mock_connect): """Test index handler.""" index.handler(sample_event, sample_context) mock_cursor = MagicMock() mock_cursor.fetchall.return_value = None mock_connect.assert_called_with(config.DD_MYSQL_CONN_INFO) mock_process_direct_delivery_records.assert_called() @patch('index.transfer_file_from_sftp_to_s3') def test_process_direct_delivery_records(mock_transfer_file_from_sftp_to_s3): """Test process_direct_delivery_records function.""" physical_location_id = 2 is_downloadable = True test_data = [{'file_path': '\\test\\file1.txt', 'ip': '0.0.0.0', 'upc': 1234567890, 'physical_location_id': 2}] index.process_direct_delivery_records(test_data, physical_location_id, is_downloadable) mock_transfer_file_from_sftp_to_s3.assert_called() def test_open_sftp_connection(monkeypatch): """Test open_sftp_connection function.""" sftp_obj = MagicMock() monkeypatch.setattr( paramiko.SFTPClient, 'from_transport', MagicMock(return_value=sftp_obj)) # Mock transport object transport_obj = MagicMock() monkeypatch.setattr(transport_obj, 'connect', MagicMock(return_value='')) monkeypatch.setattr(transport_obj, 'close', MagicMock(return_value='')) monkeypatch.setattr( paramiko, 'Transport', MagicMock(return_value=transport_obj)) index.open_sftp_connection('host', 22, 'username', 'pwd') assert paramiko.Transport('host', 22) == transport_obj assert paramiko.SFTPClient.from_transport(transport_obj) == sftp_obj @mock_s3 @patch('index.open_sftp_connection') def test_transfer_file_from_sftp_to_s3_small_file(mock_open_sftp_connection): """Test transfer_file_from_sftp_to_s3_small_file function.""" sftp_obj = MagicMock() file_obj = MagicMock() sftp_obj.file.return_value = file_obj file_obj._get_size.return_value = 2000 sftp_obj.file.return_value = file_obj file_obj.read.return_value = BytesIO(b'hello') index.open_sftp_connection.return_value = sftp_obj s3 = boto3.resource('s3') s3.create_bucket( ACL='private', Bucket=config.RAW_BUCKET) s3_file_path = 'test_folder/test.txt' index.transfer_file_from_sftp_to_s3( config.RAW_BUCKET, 'test.txt', s3_file_path, s3_file_path, '0.0.0.0', 'user', 'pwd', config.CHUNK_SIZE, s3) @mock_s3 def test_transfer_chunk_from_sftp_to_s3(): """Test transfer_chunk_from_sftp_to_s3 function.""" multipart_upload = { 'UploadId': '1234' } client = boto3.client( config.S3, region_name=config.REGION_NAME, aws_access_key_id='fake_access_key', aws_secret_access_key='fake_secret_key', ) client.upload_part = MagicMock() part = {'ETag': 'a54357aff0632cce46d942af68356b38', 'PartNumber': 1} client.upload_part.return_value = part result = index.transfer_chunk_from_sftp_to_s3( BytesIO(b'hello'), client, multipart_upload, config.RAW_BUCKET, '/test_folder/test.txt', 'test_folder/test.txt', 1, config.CHUNK_SIZE) assert part == result @mock_s3 @patch('index.open_sftp_connection') def test_transfer_file_from_sftp_to_s3_large_file( mock_open_sftp_connection): """Test transfer_file_from_sftp_to_s3_large_file function.""" sftp_obj = MagicMock() file_obj = MagicMock() sftp_obj.file.return_value = file_obj file_obj._get_size.return_value = 7291456 sftp_obj.file.return_value = file_obj file_obj.read.return_value = BytesIO(b'hello') index.open_sftp_connection.return_value = sftp_obj index.transfer_chunk_from_sftp_to_s3 = MagicMock() s3 = boto3.resource('s3') s3.create_bucket( ACL='private', Bucket=config.RAW_BUCKET) client = boto3.client( config.S3, region_name='eu-west-1', aws_access_key_id='fake_access_key', aws_secret_access_key='fake_secret_key', ) client.create_multipart_upload = MagicMock( Bucket=config.RAW_BUCKET, Key='test.txt', UploadId='123456', MultipartUpload=None) client.complete_multipart_upload = MagicMock( Bucket=config.RAW_BUCKET, Key='test.txt', UploadId='123456', MultipartUpload=None) index.transfer_file_from_sftp_to_s3( config.RAW_BUCKET, 'test.txt', 'test.txt', 'test.txt', '0.0.0.0', 'user', 'pwd', config.CHUNK_SIZE, client ) @patch('index.transfer_file_from_sftp_to_s3') def test_process_direct_delivery_records_fornyc_storage( mock_transfer_file_from_sftp_to_s3): """Test process_direct_delivery_records function.""" physical_location_id = 2 is_downloadable = False test_data = [{'file_path': '\\test\\file1.txt', 'ip': '0.0.0.0', 'upc': 1234567890, 'physical_location_id': 2}] index.process_direct_delivery_records(test_data, physical_location_id, is_downloadable) mock_transfer_file_from_sftp_to_s3.assert_called()