Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct response codes for bad/unusable bboxes #235

Merged
merged 5 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion stac_fastapi/pgstac/stac_fastapi/pgstac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import attr
import orjson
from buildpg import render
from fastapi import HTTPException
from pydantic import ValidationError
from starlette.requests import Request

from stac_fastapi.pgstac.models.links import CollectionLinks, ItemLinks, PagingLinks
Expand Down Expand Up @@ -250,5 +252,8 @@ async def get_search(
base_args["fields"] = {"include": includes, "exclude": excludes}

# Do the request
search_request = PgstacSearch(**base_args)
try:
search_request = PgstacSearch(**base_args)
except ValidationError:
raise HTTPException(status_code=400, detail="Invalid parameters provided")
return await self.post_search(search_request, request=kwargs["request"])
15 changes: 15 additions & 0 deletions stac_fastapi/pgstac/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,3 +928,18 @@ async def test_relative_link_construction():
)
links = CollectionLinks(collection_id="naip", request=req)
assert links.link_items()["href"] == "http://test/stac/collections/naip/items"


@pytest.mark.asyncio
async def test_search_bbox_errors(app_client):
body = {"query": {"bbox": [0]}}
resp = await app_client.post("/search", json=body)
assert resp.status_code == 400

body = {"query": {"bbox": [100.0, 0.0, 0.0, 105.0, 1.0, 1.0]}}
resp = await app_client.post("/search", json=body)
assert resp.status_code == 400

params = {"bbox": "100.0,0.0,0.0,105.0"}
resp = await app_client.get("/search", params=params)
assert resp.status_code == 400
19 changes: 17 additions & 2 deletions stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import geoalchemy2 as ga
import sqlalchemy as sa
import stac_pydantic
from fastapi import HTTPException
from pydantic import ValidationError
from shapely.geometry import Polygon as ShapelyPolygon
from shapely.geometry import shape
from sqlakeyset import get_page
Expand Down Expand Up @@ -207,7 +209,10 @@ def get_search(
base_args["fields"] = {"include": includes, "exclude": excludes}

# Do the request
search_request = SQLAlchemySTACSearch(**base_args)
try:
search_request = SQLAlchemySTACSearch(**base_args)
except ValidationError:
raise HTTPException(status_code=400, detail="Invalid parameters provided")
resp = self.post_search(search_request, request=kwargs["request"])

# Pagination
Expand Down Expand Up @@ -293,7 +298,17 @@ def post_search(
if search_request.intersects is not None:
poly = shape(search_request.intersects)
elif search_request.bbox:
poly = ShapelyPolygon.from_bounds(*search_request.bbox)
if len(search_request.bbox) == 4:
poly = ShapelyPolygon.from_bounds(*search_request.bbox)
elif len(search_request.bbox) == 6:
"""Shapely doesn't support 3d bounding boxes we'll just use the 2d portion"""
bbox_2d = [
search_request.bbox[0],
search_request.bbox[1],
search_request.bbox[3],
search_request.bbox[4],
]
poly = ShapelyPolygon.from_bounds(*bbox_2d)

if poly:
filter_geom = ga.shape.from_shape(poly, srid=4326)
Expand Down
14 changes: 14 additions & 0 deletions stac_fastapi/sqlalchemy/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,20 @@ def test_search_invalid_query_field(app_client):
assert resp.status_code == 400


def test_search_bbox_errors(app_client):
body = {"query": {"bbox": [0]}}
resp = app_client.post("/search", json=body)
assert resp.status_code == 400

body = {"query": {"bbox": [100.0, 0.0, 0.0, 105.0, 1.0, 1.0]}}
resp = app_client.post("/search", json=body)
assert resp.status_code == 400

params = {"bbox": "100.0,0.0,0.0,105.0"}
resp = app_client.get("/search", params=params)
assert resp.status_code == 400


def test_conformance_classes_configurable():
"""Test conformance class configurability"""
landing = LandingPageMixin()
Expand Down