"""Functional tests for GET /product//spatial endpoint.""" import json from unittest.mock import MagicMock from flexmock import flexmock from owsrequest import request import pytest from product_digital.constants import services from tests.factories import release as release_factory from tests.factories import release_spatial as release_spatial_factory from tests.testutils import db PRODUCT_ID = 12345 SPATIAL_UPC = 9876543210123 @pytest.fixture def product_id(): """Return a fake product id for testing.""" return PRODUCT_ID @pytest.fixture def owner_ok_response(): """Return a mock 200 ownership response from ows-product.""" return MagicMock(status_code=200) @pytest.fixture def not_owner_response(): """Return a mock 403 ownership response from ows-product.""" return MagicMock(status_code=403) @db.test_schema def test_get_product_spatial_success( client, product_id, valid_headers_for_vendor, owner_ok_response): """Test successful retrieval of a spatial record.""" existing_release = release_factory.ReleaseFactory.build(release_id=product_id) existing_spatial = release_spatial_factory.ReleaseSpatialFactory.build( release_id=product_id, upc=SPATIAL_UPC) db.seed_models([existing_release, existing_spatial]) (flexmock(request) .should_receive('head') .with_args(services.OWS_PRODUCT, '/vendor/{}/product/{}'.format( valid_headers_for_vendor['Grass-Account-Id'], product_id)) .and_return(owner_ok_response)) response = client.get( '/product/{}/spatial'.format(product_id), headers=valid_headers_for_vendor) body = json.loads(response.data.decode()) assert response.status_code == 200 assert body['release_id'] == product_id assert body['upc'] == SPATIAL_UPC @db.test_schema def test_get_product_spatial_not_found( client, product_id, valid_headers_for_vendor, owner_ok_response): """Test 404 when no spatial record exists for the product.""" existing_release = release_factory.ReleaseFactory.build(release_id=product_id) db.seed_models(existing_release) (flexmock(request) .should_receive('head') .and_return(owner_ok_response)) response = client.get( '/product/{}/spatial'.format(product_id), headers=valid_headers_for_vendor) assert response.status_code == 404 def test_get_product_spatial_not_owner( client, product_id, account_id, valid_headers_for_vendor, not_owner_response): """Test 403 when the requesting account does not own the product.""" (flexmock(request) .should_receive('head') .with_args(services.OWS_PRODUCT, '/vendor/{}/product/{}'.format( account_id, product_id)) .and_return(not_owner_response)) response = client.get( '/product/{}/spatial'.format(product_id), headers=valid_headers_for_vendor) assert response.status_code == 403 def test_get_product_spatial_incomplete_grass_headers( client, product_id, valid_headers, account_id): """Test 400 when only one Grass header is present (incomplete pair).""" valid_headers['Grass-Account-Id'] = account_id response = client.get( '/product/{}/spatial'.format(product_id), headers=valid_headers) assert response.status_code == 400