"""Logic layer test for image location.""" from unittest import mock from images import response from images.constants import errors from images.logic import image_location from images.models import image_assets as image_assets_model from images.models import ows_product as ows_product_model def test_get_image_location(monkeypatch, upc, expected_path, account_info): """Test that the logic layer calls image_assets model.""" image_location_response = response.Response( message={'path': expected_path}) ows_product_response = response.Response( message=account_info) monkeypatch.setattr( image_assets_model, 'get_image_path_by_upc', mock.MagicMock(return_value=image_location_response)) monkeypatch.setattr( ows_product_model, 'get_vendor_and_subaccount_by_upc', mock.MagicMock(return_value=ows_product_response)) image_location_response = image_location.get_image_location(upc) image_assets_model.get_image_path_by_upc.assert_called_with(upc) assert image_location_response.message.get('path') == expected_path assert image_location_response.message.get('account_info') == account_info def test_get_image_location_when_location_not_found(monkeypatch, upc): """Test that the logic returns 404 when location not found.""" image_location_response = response.create_error_response( code='not_found_error', message=errors.IMAGE_PATH_NOT_FOUND, status=404) monkeypatch.setattr( image_assets_model, 'get_image_path_by_upc', mock.MagicMock(return_value=image_location_response)) image_location_response = image_location.get_image_location(upc) image_assets_model.get_image_path_by_upc.assert_called_with(upc) assert image_location_response == image_location_response def test_get_image_location_when_account_info_not_found( monkeypatch, upc, expected_path): """Test that the logic layer returns 404 when acccount info not found.""" image_location_response = response.Response( message={'path': expected_path}) ows_product_response = response.create_error_response( code='not_found_error', message=errors.ERROR_CODE_NOT_FOUND, status=404) monkeypatch.setattr( image_assets_model, 'get_image_path_by_upc', mock.MagicMock(return_value=image_location_response)) monkeypatch.setattr( ows_product_model, 'get_vendor_and_subaccount_by_upc', mock.MagicMock(return_value=ows_product_response)) image_location_response = image_location.get_image_location(upc) image_assets_model.get_image_path_by_upc.assert_called_with(upc) assert image_location_response.status == 404 assert image_location_response.errors == { 'code': 'not_found_error', 'message': 'not_found'}