"""Test handle_video_asset handler.""" from unittest.mock import ANY, MagicMock, patch from ddex_ingester_common.schemas.state_machine_schema import ProductSchema from constants import queries from index import (get_thumbnail_token, handler, ingest_custom_thumbnail, update_video_with_thumbnail) @patch('index.config', return_value=MagicMock( name=['VIDEO_ASSET_BUCKET', 'VIDEO_ASSET_INGEST_DIR'])) @patch('index.logger') @patch('index.copy_asset') @patch('index.create_video_workflow') @patch('index.attach_workflow_to_video_product') @patch('index.ingest_custom_thumbnail') def test_handler( mock_ingest_custom_thumbnail, mock_attach_workflow, mock_create_workflow, mock_copy_asset, mock_logger, mock_config, lambda_input): """Test handler.""" workflow_id = 3453453 product_id = 12345 mock_config.VIDEO_ASSET_BUCKET = 'video_bucket' mock_config.VIDEO_ASSET_INGEST_DIR = 'video_ingest_path' mock_create_workflow.return_value = {'id': workflow_id} handler(lambda_input, None) mock_copy_asset.assert_called_once() mock_create_workflow.assert_called_with( 'video_ingest_path/incoming/video1.mp4', product_id ) mock_attach_workflow.assert_called_with(workflow_id, product_id) mock_ingest_custom_thumbnail.assert_called_with( ANY, workflow_id ) @patch('index.write_rc_json') @patch('index.create_blank_rc_json') @patch('index.config', return_value=MagicMock( name=['VIDEO_ASSET_BUCKET', 'VIDEO_ASSET_INGEST_DIR'])) @patch('index.logger') @patch('index.copy_asset') @patch('index.create_video_workflow') @patch('index.attach_workflow_to_video_product') @patch('index.ingest_custom_thumbnail') def test_handler_unsupported_video_workflow( mock_ingest_custom_thumbnail, mock_attach_workflow, mock_create_workflow, mock_copy_asset, mock_logger, mock_config, mock_create_blank_rc_json, mock_write_rc_json, lambda_input): """Test handler.""" lambda_input['context']['product']['status'] = 'in_content' mock_config.VIDEO_ASSET_BUCKET = 'video_bucket' mock_config.VIDEO_ASSET_INGEST_DIR = 'video_ingest_path' handler(lambda_input, None) mock_copy_asset.assert_not_called() mock_create_workflow.assert_not_called() mock_attach_workflow.assert_not_called() mock_ingest_custom_thumbnail.assert_not_called() mock_write_rc_json.assert_called_once() @patch('index.get_thumbnail_token') @patch('index.copy_asset') @patch('index.update_video_with_thumbnail') def test_ingest_custom_thumbnail( mock_update_video_with_thumbnail, mock_copy_asset, mock_get_thumbnail_token, lambda_input): """Test ingest_custom_thumbnail.""" product = ProductSchema().load(lambda_input['context']['product']) artwork = product.artwork workflow_id = 3453453 mock_token_response = { 'bucket': 'qa-orcd-video-assets', 'filename': 'thumbnails/custom/119365/test_thumbnail.jpg' } mock_get_thumbnail_token.return_value = mock_token_response ingest_custom_thumbnail(product, workflow_id) mock_get_thumbnail_token.assert_called_with( 'A10301A0004280500R_T-1205814372821_Image.tif', workflow_id ) mock_copy_asset.assert_called_with( {}, artwork, 'qa-orcd-video-assets', 'thumbnails/custom/119365/test_thumbnail.jpg' ) mock_update_video_with_thumbnail.assert_called_with( product.product_id, 'thumbnails/custom/119365/test_thumbnail.jpg' ) @patch('index.graphql_gateway.execute') def test_get_thumbnail_token(mock_graphql_execute): """Test get_thumbnail_token.""" filename = 'test_thumbnail.jpg' workflow_id = 3453453 payload = { 'filename': filename, 'workflowJobId': workflow_id } mock_token_response = { 'data': { 'createVideoCustomThumbnailToken': { 'key': 'value' } } } mock_graphql_execute.return_value = mock_token_response response = get_thumbnail_token(filename, workflow_id) assert response == mock_token_response['data'][ 'createVideoCustomThumbnailToken'] mock_graphql_execute.assert_called_with( queries.get_custom_thumbnail_token, {'data': payload} ) @patch('index.graphql_gateway.execute') def test_update_video_with_thumbnail(mock_graphql_execute): """Test update_video_with_thumbnail.""" product_id = 12345 target_key = 'thumbnails/custom/119365/test_thumbnail.jpg' payload = { 'update': { 'productId': str(product_id), 'customThumbnailPath': '119365/test_thumbnail.jpg' } } update_video_with_thumbnail(product_id, target_key) mock_graphql_execute.assert_called_with( queries.save_video_single_product, {'data': payload} )