"""Test suite for SFTP asset delivery connector.""" from unittest import mock from src.connectors import sftp_asset_delivery @mock.patch('src.common.connectors.sftp.Connection') @mock.patch('config.secrets_manager_client.get_cred') @mock.patch('os.environ.get') @mock.patch('config.YOUTUBE_SFTP_USERNAME', 'test_username') @mock.patch('config.YOUTUBE_SFTP_PORT', 22) @mock.patch('config.YOUTUBE_SFTP_HOSTNAME', 'test.hostname.com') def test_get_sftp_connection(mock_environ_get, mock_get_cred, mock_connection): """Test getting SFTP connection.""" sftp_asset_delivery.sftp_connection = None mock_get_cred.return_value = 'FAKE_PRIVATE_KEY_CONTENT' mock_environ_get.return_value = 'qa' sftp_conn = sftp_asset_delivery._get_sftp_connection() mock_get_cred.assert_called_once_with('YOUTUBE_CMS_FP_PRIVATE_KEY') mock_connection.assert_called_once_with( hostname='test.hostname.com', port=22, username='test_username', pkey='FAKE_PRIVATE_KEY_CONTENT' ) assert sftp_conn is not None @mock.patch('src.connectors.sftp_asset_delivery.sftp_connection', {'existing': 'connection'}) def test_get_sftp_connection_cached(): """Test getting cached SFTP connection.""" sftp_conn = sftp_asset_delivery._get_sftp_connection() assert sftp_conn == {'existing': 'connection'} @mock.patch('src.common.connectors.sftp.Connection') @mock.patch('config.secrets_manager_client.get_cred') @mock.patch('os.environ.get', return_value='qa') def test_get_sftp_connection_exception(mock_environ_get, mock_get_cred, mock_connection): """Test exception handling when getting SFTP connection.""" sftp_asset_delivery.sftp_connection = None mock_environ_get.return_value = 'qa' mock_get_cred.return_value = { 'private_key': 'FAKE_PRIVATE_KEY_CONTENT' } mock_connection.side_effect = Exception('Connection error') try: sftp_asset_delivery._get_sftp_connection() except Exception as e: assert str(e) == 'Connection error' mock_get_cred.assert_called_once() mock_connection.assert_called_once() @mock.patch('src.common.connectors.sftp.Connection.upload_files') @mock.patch('src.connectors.sftp_asset_delivery._get_sftp_connection') @mock.patch('os.environ.get') def test_transfer_file_to_youtube(mock_environ_get, mock_get_sftp_connection, mock_upload_files): """Test transferring file to YouTube via SFTP.""" mock_sftp_conn = mock.Mock() mock_environ_get.side_effect = ['basedir/', 'qa'] mock_get_sftp_connection.return_value = mock_sftp_conn file_bytes = b'Test file content' subdir = 'test/subdir' step_function_execution_id = 'test_execution_id' sftp_asset_delivery.write_file( file_bytes, subdir, step_function_execution_id) mock_get_sftp_connection.assert_called_once() mock_sftp_conn.upload_files.assert_called_once_with( [(file_bytes, 'basedir/' + step_function_execution_id + '/' + subdir)] ) @mock.patch('src.common.connectors.sftp.Connection.upload_files') @mock.patch('src.connectors.sftp_asset_delivery._get_sftp_connection') @mock.patch('os.environ.get') def test_transfer_file_to_youtube_exception(mock_environ_get, mock_get_sftp_connection, mock_upload_files): """Test exception handling when transferring file to YouTube via SFTP.""" sftp_asset_delivery.sftp_connection = None mock_environ_get.side_effect = ['basedir', 'qa'] mock_sftp_conn = mock.Mock() mock_get_sftp_connection.return_value = mock_sftp_conn mock_sftp_conn.upload_files.side_effect = Exception('Upload error') file_bytes = b'Test file content' subdir = 'test/subdir' step_function_execution_id = 'test_execution_id' try: sftp_asset_delivery.write_file( file_bytes, subdir, step_function_execution_id) except Exception as e: assert str(e) == 'Upload error' mock_get_sftp_connection.assert_called_once() mock_sftp_conn.upload_files.assert_called_once_with( [(file_bytes, 'basedir/' + step_function_execution_id + '/' + subdir)] )