"""Functional tests for posting to error correction images.""" from moto import mock_s3 import pytest from assets import config from assets.connectors import s3 from assets.models import image_location @pytest.fixture() def all_image_types(): """Return a list of all the asset types for cover art.""" return [ 'cover', 'cover_correction', 'large_cover', 'large_cover_correction'] @pytest.fixture def product_id(): """Create a product id for testing.""" return 123 def seed_assets(product_id, image_types): """Create objects in s3 for the given image types.""" s3_connection = s3.connect_to_s3() s3_bucket = s3_connection.create_bucket(config.ASSET_STORAGE_BUCKET_NAME) for image_type in image_types: image_path = image_location.get_backend_image_filename( product_id, 'product', image_type).message s3_key = s3_bucket.new_key(image_path) s3_key.set_contents_from_string('fake {} content'.format(image_type)) @mock_s3 def test_post_approval_success( all_image_types, fixture_client, product_id, valid_oa_headers): """Test result when images are successfully renamed.""" seed_assets(product_id, all_image_types) result = fixture_client.post( '/correction/images/approval/{}'.format(product_id), headers=valid_oa_headers) assert result.status_code == 200 @mock_s3 def test_post_approval_not_oa( all_image_types, fixture_client, product_id, valid_alw_headers): """Test that request is rejected if not from OA.""" seed_assets(product_id, all_image_types) result = fixture_client.post( '/correction/images/approval/{}'.format(product_id), headers=valid_alw_headers) assert result.status_code == 403 @mock_s3 def test_post_approval_not_found(fixture_client, product_id, valid_oa_headers): """Test that response is 404 if correction assets not found.""" seed_assets(product_id, ['large_cover', 'large_cover_correction']) result = fixture_client.post( '/correction/images/approval/{}'.format(product_id), headers=valid_oa_headers) assert result.status_code == 404