"""Test handler.""" import datetime from unittest.mock import ANY, MagicMock, patch import pytest from botocore.errorfactory import ClientError from ddex_ingester_common.schemas.state_machine_schema import \ StateMachineSchema import index @patch('index.generate_file_name', return_value='new_name') @patch('index.rename_video_file') @patch('index.get_video_file_url', return_value='http://www.video-url.com') @patch('index.fix_video_resolution', return_value=b'video bytes') @patch('index.upload_video_file') @patch('index.trigger_new_state_machine_execution') def test_handler( mock_trigger_new_state_machine_execution, mock_upload_video_file, mock_fix_video_resolution, mock_get_video_file_url, mock_rename_video_file, mock_generate_file_name, mock_context): """Test the main handler.""" new_name = 'new_name' local_path = '/tmp/' video_url = 'http://www.video-url.com' video_data = b'video bytes' index.handler(mock_context, None) mock_generate_file_name.assert_called() mock_rename_video_file.assert_called_with(ANY, new_name) mock_get_video_file_url.assert_called_with(ANY, new_name) mock_fix_video_resolution.assert_called_with(local_path, video_url) mock_upload_video_file.assert_called_with(ANY, video_data) mock_trigger_new_state_machine_execution.assert_called() def test_fix_video_resolution(): """Test that the video resolution fix handler returns the video bytes.""" input_filepath = '/tmp/data/input2.mp4' video_bytes = index.fix_video_resolution('/tmp/', input_filepath) assert video_bytes assert len(video_bytes) > 2360000 assert len(video_bytes) < 2470000 @pytest.mark.skip(reason='Only for running locally') def test_fix_video_resolution_local(): """Test that the video resolution fix handler writes the file to disk.""" input_filepath = 'tests/data/input3.mov' output_filepath = 'tests/data/output.mov' index.fix_video_resolution( '', input_filepath, output_file_name=output_filepath, ) f = open(output_filepath, 'rb') video_bytes = f.read() assert video_bytes assert len(video_bytes) > 24352700 assert len(video_bytes) < 24353200 @patch('index.datetime') def test_generate_file_name(mock_datetime, mock_context): """Test generate_file_name.""" context = StateMachineSchema().load(mock_context) mock_datetime.datetime.now.return_value =\ datetime.datetime.fromtimestamp(1000000) result = index.generate_file_name(context) assert result == 'EXPV494614_RECV2091549_Original_1970-01-12_13-46-40.mov' @patch('index.s3_client.copy') def test_rename_video_file(mock_s3_copy, mock_context): """Test rename_video_file.""" context = StateMachineSchema().load(mock_context) new_name = 'new_name.mp4' old_name = 'EXPV494614_RECV2091549.mov' bucket = 'qa-ddex-ingester' key_no_file = 'sme_ddex/Video_Folder/resources/' index.rename_video_file(context, new_name) mock_s3_copy.assert_called_with( { 'Bucket': bucket, 'Key': key_no_file + old_name }, bucket, key_no_file + new_name ) @patch('index.s3_client') def test_get_video_file_url( mock_s3_client, mock_context): """Test get_video_file_url.""" context = StateMachineSchema().load(mock_context) new_file_name = 'new_file.mp4' bucket = 'qa-ddex-ingester' key_no_file = 'sme_ddex/Video_Folder/resources/' index.get_video_file_url(context, new_file_name) mock_s3_client.generate_presigned_url.assert_called_with( 'get_object', Params={'Bucket': bucket, 'Key': key_no_file + new_file_name}, ExpiresIn=900 ) @patch('index.s3_client') def test_upload_video_file( mock_s3_client, mock_context): """Test upload_video_file.""" context = StateMachineSchema().load(mock_context) video_data = b'video bytes' bucket = 'qa-ddex-ingester' key = 'sme_ddex/Video_Folder/resources/EXPV494614_RECV2091549.mov' index.upload_video_file(context, video_data) mock_s3_client.put_object.assert_called_with( Body=video_data, Bucket=bucket, Key=key, ) @patch('index.s3_client') def test_trigger_new_state_machine_execution( mock_s3_client, mock_context): """Test trigger_new_state_machine_execution.""" context = StateMachineSchema().load(mock_context) bucket = 'qa-ddex-ingester' key_no_file = mock_context['key'] xml_file = 'A10301A0003545560O.xml' file_data = b'XML file data' s3_file_data = MagicMock() s3_file_data.read.return_value = file_data mock_s3_client.get_object.return_value = {'Body': s3_file_data} mock_head_object = MagicMock( side_effect=ClientError(error_response={}, operation_name='')) mock_s3_client.head_object.side_effect = mock_head_object index.trigger_new_state_machine_execution(context) mock_s3_client.get_object.assert_called_with( Bucket=bucket, Key=(key_no_file + xml_file), ) mock_s3_client.put_object.assert_called_with( Bucket=bucket, Key=key_no_file + 'reupload_' + xml_file, Body=file_data, ) @patch('index.s3_client') def test_trigger_new_state_machine_execution_file_exists( mock_s3_client, mock_context): """Test trigger_new_state_machine_... skips the trigger if file exists.""" context = StateMachineSchema().load(mock_context) reupload_key = mock_context['key'] + 'reupload_A10301A0003545560O.xml' index.trigger_new_state_machine_execution(context) mock_s3_client.head_object.assert_called_with( Bucket=mock_context['bucket'], Key=reupload_key) mock_s3_client.get_object.assert_not_called() mock_s3_client.put_object.assert_not_called()