"""Tests for image location logic layer.""" import flexmock from oto import response import pytest from assets import config from assets.constants import error from assets.logic import image_location from assets.models import s3_file @pytest.fixture def fixture_filename(): """Fixture for a s3 filename.""" return 'product/cover/e10adc3949ba59abbe56e057f20f883e.jpg' @pytest.fixture def fixture_file_path(): """Fixture for a s3 file_path.""" return 'images/product/cover/e10adc3949ba59abbe56e057f20f883e.jpg' @pytest.fixture def image_location_data(): """Data for use in the image location call.""" return { 'entity_id': 123456, 'image_type': 'product', 'image_format': 'cover' } def test_get_image_location_success( image_location_data, fixture_filename, fixture_file_path): """Test for successful image location call.""" config.ASSET_STORAGE_BUCKET_NAME = 'dev-asset-storage' config.CDN_URL = 'https://fakesite.cloudfront.net' (flexmock(s3_file) .should_receive('check_s3_file_exists') .with_args('dev-asset-storage', fixture_file_path) .and_return(response.Response('ok'))) result = image_location.get_image_location( **image_location_data) assert result.status == 200 assert result.message == '{cdn}/{filename}'.format( cdn=config.CDN_URL, filename=fixture_filename) def test_get_image_location_file_not_found(image_location_data): """Test for failing image location call when the file is not found.""" failure_response = response.create_fatal_response( message='File not found' ) (flexmock(s3_file) .should_receive('check_s3_file_exists') .and_return(failure_response)) result = image_location.get_image_location( **image_location_data) assert result.status == 404 assert result.errors['code'] == error.ERROR_CODE_NOT_FOUND assert result.errors['message'] == error.ERROR_IMAGE_NOT_FOUND