"""Functional tests for GET /image//location.""" import json import httpretty from images import config from images import response from images.constants import errors as error_constants from images.constants import header as header_constants from tests.factories import image_assets, image_status from tests.utils import db @httpretty.activate def test_get_image_location( client, import_asset_id, test_schema, expected_url, image_upc, valid_headers, ows_product_response_body, ): """Test response body from GET /image//location. Should contain the expected image location data. """ import_asset = image_status.ImportAssetFactory.build( id=import_asset_id, result='success') image_asset = image_assets.ImageAssetsFactory.build() release_cover_images = image_assets.ReleaseCoverImagesFactory.build() db.seed_models([import_asset, image_asset, release_cover_images]) httpretty.register_uri( httpretty.GET, config.OWS_PRODUCT_URL + '/product/upc/123456789012', body=json.dumps(ows_product_response_body), content_type='application/json') endpoint_response = client.get( '/image/{0}/location'.format(image_upc), headers=valid_headers) assert endpoint_response.status_code == 200 json_response = json.loads(endpoint_response.data.decode('utf-8')) assert json_response == {'path': expected_url} def test_get_image_location_when_not_found( client, import_asset_id, test_schema): """Test that the response body from GET /image//location. Should contain a not found message for non-existant image assets. """ endpoint_response = client.get('/image/{0}/location'.format(123123)) import_asset = json.loads(endpoint_response.data.decode('utf-8')) assert endpoint_response.status_code == 404 assert import_asset == { 'code': response.ERROR_CODE_NOT_FOUND, 'message': 'Image asset path not found.'} @httpretty.activate def test_get_image_location_when_not_authorized( client, import_asset_id, test_schema, expected_url, image_upc, not_valid_headers, ows_product_response_body, ): """Test response body from GET /image//location. Should contain the expected image location data. """ import_asset = image_status.ImportAssetFactory.build( id=import_asset_id, result='success') image_asset = image_assets.ImageAssetsFactory.build() release_cover_images = image_assets.ReleaseCoverImagesFactory.build() db.seed_models([import_asset, image_asset, release_cover_images]) httpretty.register_uri( httpretty.GET, config.OWS_PRODUCT_URL + '/product/upc/123456789012', body=json.dumps(ows_product_response_body), content_type='application/json') endpoint_response = client.get( '/image/{0}/location'.format(image_upc), headers=not_valid_headers) assert endpoint_response.status_code == 403 @httpretty.activate def test_get_image_location_with_incomplete_grass_creds( client, image_upc, import_asset_id, ows_product_response_body, test_account_type, test_schema): """Test that the request is rejected with incomplete Grass headers.""" import_asset = image_status.ImportAssetFactory.build( id=import_asset_id, result='success') image_asset = image_assets.ImageAssetsFactory.build() release_cover_images = image_assets.ReleaseCoverImagesFactory.build() db.seed_models([import_asset, image_asset, release_cover_images]) httpretty.register_uri( httpretty.GET, config.OWS_PRODUCT_URL + '/product/upc/{}'.format(image_upc), body=json.dumps(ows_product_response_body), content_type='application/json') headers_missing_id = { header_constants.GRASS_ACCOUNT_TYPE: test_account_type} endpoint_response = client.get( '/image/{}/location'.format(image_upc), headers=headers_missing_id) response_body = json.loads(endpoint_response.data.decode('utf-8')) assert response_body == { 'code': error_constants.ERROR_CODE_BAD_GRASS_REQUEST, 'message': error_constants.ERROR_MESSAGE_INCOMPLETE_GRASS_HEADERS} assert endpoint_response.status_code == 400 @httpretty.activate def test_get_image_location_with_no_grass_headers( client, image_upc, import_asset_id, ows_product_response_body, test_account_type, test_schema): """Test that the request is accepted if there are no Grass headers.""" import_asset = image_status.ImportAssetFactory.build( id=import_asset_id, result='success') image_asset = image_assets.ImageAssetsFactory.build() release_cover_images = image_assets.ReleaseCoverImagesFactory.build() db.seed_models([import_asset, image_asset, release_cover_images]) httpretty.register_uri( httpretty.GET, config.OWS_PRODUCT_URL + '/product/upc/{}'.format(image_upc), body=json.dumps(ows_product_response_body), content_type='application/json') endpoint_response = client.get('/image/{}/location'.format(image_upc)) assert endpoint_response.status_code == 200