"""Module with tests for post_asset method.""" import json from unittest.mock import patch from src import app as index from src.common import lambda_exceptions import pytest @pytest.fixture def fixture_correct_post_asset_args(): """Fixture with correct post_asset call args.""" return { 'asset_type': 'WAV', 'object_type': 'episode', 'object_id': '1', 'filename': 'unique_filename.wav', 'original_filename': 'original_filename.ext' } @patch('owsrequest.request.process') def test_post_asset_success(process, fixture_correct_post_asset_args): """Test for post_asset success.""" process.return_value.status_code = 200 process.return_value.json.return_value = { 'config': { 'output_bucket_name': 'test-output-bucket', 'preview_bucket_name': 'test-output-bucket', 'elastic_transcoder_id': '123' } } result = index.post_asset(fixture_correct_post_asset_args) assert result @patch('owsrequest.request.process') def test_post_asset_request_validation_error(process, fixture_correct_post_asset_args): """Test for post_asset with a 400 response.""" process.return_value.status_code = 400 process.return_value.text = 'Validation error from microserivce' with pytest.raises(lambda_exceptions.PostAssetError) as err: index.post_asset(fixture_correct_post_asset_args) assert err.value.error_code == 'post_asset_error' assert str(err.value) == 'post asset failed with code 400 Validation error from microserivce' @patch('owsrequest.request.process') def test_post_asset_request_not_found_error(process, fixture_correct_post_asset_args): """Test for post_asset with a 404 response.""" process.return_value.status_code = 404 with pytest.raises(lambda_exceptions.PostAssetNotFoundError) as err: index.post_asset(fixture_correct_post_asset_args) assert err.value.error_code == 'post_asset_error' @patch('owsrequest.request.process') def test_post_asset_fails_with_no_config(process, fixture_correct_post_asset_args): """Test for post_asset fails with missing config.""" process.return_value.status_code = 200 process.return_value.json.return_value = { 'asset-type': 'wav', 'filename': 'file' } with pytest.raises(lambda_exceptions.AssetConfigError) as err: index.post_asset(fixture_correct_post_asset_args) assert err.value.error_code == 'asset_config_error' assert str(err.value) == 'The asset does not have a proper configuration. Config: {message}'.format( message=json.dumps({'config': ['Missing data for required field.']})) @patch('owsrequest.request.process') def test_post_asset_fails_with_incomplete_config(process, fixture_correct_post_asset_args): """Test for post_asset fails with missing config.""" process.return_value.status_code = 200 process.return_value.json.return_value = { 'asset-type': 'wav', 'filename': 'file', 'config': { 'output_bucket_name': 'test-output-bucket', 'preview_bucket_name': 'test-output-bucket', } } with pytest.raises(lambda_exceptions.AssetConfigError) as err: index.post_asset(fixture_correct_post_asset_args) assert err.value.error_code == 'asset_config_error' assert str(err.value) == 'The asset does not have a proper configuration. Config: {message}'.format( message=json.dumps({'config': {'elastic_transcoder_id': ['Missing data for required field.']}}))