"""Unit tests for error correction.""" import flexmock from oto import response import pytest from assets import config from assets.models import error_correction from assets.models import s3_file from assets.models import image_location @pytest.fixture def product_id(): """Return a product id.""" return 123 @pytest.fixture def cover_correction_path(product_id): """Return a cover correction path.""" return image_location.get_backend_image_filename( product_id, 'product', 'cover_correction').message @pytest.fixture def cover_path(product_id): """Return a cover path.""" return image_location.get_backend_image_filename( product_id, 'product', 'cover').message @pytest.fixture def large_cover_correction_path(product_id): """Return a large cover correction path.""" return image_location.get_backend_image_filename( product_id, 'product', 'large_cover_correction').message @pytest.fixture def large_cover_path(product_id): """Return a large cover path.""" return image_location.get_backend_image_filename( product_id, 'product', 'large_cover').message def test_rename_image_assets_success( cover_path, cover_correction_path, large_cover_correction_path, large_cover_path, product_id): """Test result when both images are successfully renamed.""" (flexmock(s3_file) .should_receive('rename_s3_file') .with_args( config.ASSET_STORAGE_BUCKET_NAME, cover_correction_path, cover_path) .once() .and_return(response.Response())) (flexmock(s3_file) .should_receive('rename_s3_file') .with_args( config.ASSET_STORAGE_BUCKET_NAME, large_cover_correction_path, large_cover_path) .once() .and_return(response.Response())) result = error_correction.rename_image_assets(product_id) assert result.status == 200 def test_rename_image_assets_not_found( cover_path, cover_correction_path, large_cover_correction_path, large_cover_path, product_id): """Test result when neither image is found.""" (flexmock(s3_file) .should_receive('rename_s3_file') .with_args( config.ASSET_STORAGE_BUCKET_NAME, cover_correction_path, cover_path) .and_return(response.create_not_found_response(message='nobody home'))) (flexmock(s3_file) .should_receive('rename_s3_file') .with_args( config.ASSET_STORAGE_BUCKET_NAME, large_cover_correction_path, large_cover_path) .and_return(response.create_not_found_response(message='not here'))) result = error_correction.rename_image_assets(product_id) assert result.status == 404 assert result.errors == { 'code': 'not_found_error', 'message': { 'cover': { 'code': 'not_found_error', 'message': 'nobody home'}, 'large_cover': { 'code': 'not_found_error', 'message': 'not here'}}} def test_rename_image_assets_error( cover_path, cover_correction_path, large_cover_correction_path, large_cover_path, product_id): """Test result when an image rename returns a 500.""" (flexmock(s3_file) .should_receive('rename_s3_file') .with_args( config.ASSET_STORAGE_BUCKET_NAME, cover_correction_path, cover_path) .and_return(response.create_fatal_response(message='facepalm'))) (flexmock(s3_file) .should_receive('rename_s3_file') .with_args( config.ASSET_STORAGE_BUCKET_NAME, large_cover_correction_path, large_cover_path) .and_return(response.Response(message='that went ok'))) result = error_correction.rename_image_assets(product_id) assert result.status == 500 assert result.errors == { 'code': 'internal_error', 'message': { 'cover': { 'code': 'internal_error', 'message': 'facepalm'}}} def test_rename_image_assets_partial_404( cover_path, cover_correction_path, large_cover_correction_path, large_cover_path, product_id): """Test result when one image is not found but the other succeeds.""" (flexmock(s3_file) .should_receive('rename_s3_file') .with_args( config.ASSET_STORAGE_BUCKET_NAME, cover_correction_path, cover_path) .and_return(response.create_not_found_response(message='nobody home'))) (flexmock(s3_file) .should_receive('rename_s3_file') .with_args( config.ASSET_STORAGE_BUCKET_NAME, large_cover_correction_path, large_cover_path) .and_return(response.Response(message='whee, that worked'))) result = error_correction.rename_image_assets(product_id) assert result.status == 404 assert result.errors == { 'code': 'not_found_error', 'message': { 'cover': { 'code': 'not_found_error', 'message': 'nobody home'}}} def test_rename_image_assets_mixed_errors( cover_path, cover_correction_path, large_cover_correction_path, large_cover_path, product_id): """Test result when one rename returns 404 and the other returns 500.""" (flexmock(s3_file) .should_receive('rename_s3_file') .with_args( config.ASSET_STORAGE_BUCKET_NAME, cover_correction_path, cover_path) .and_return(response.create_not_found_response(message='nobody home'))) (flexmock(s3_file) .should_receive('rename_s3_file') .with_args( config.ASSET_STORAGE_BUCKET_NAME, large_cover_correction_path, large_cover_path) .and_return(response.create_fatal_response(message='s3 fail'))) result = error_correction.rename_image_assets(product_id) assert result.status == 500 assert result.errors == { 'code': 'internal_error', 'message': { 'cover': { 'code': 'not_found_error', 'message': 'nobody home'}, 'large_cover': { 'code': 'internal_error', 'message': 's3 fail'}}}