Skip to content

Commit

Permalink
Block search calls that dont include a collection in the body or quer…
Browse files Browse the repository at this point in the history
…y param
  • Loading branch information
Marc Lichtman committed Oct 24, 2024
1 parent 8602de8 commit a255829
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,21 @@ To run the servers, use
./scripts/server
```

This will bring up the development database, STAC API, Tiler, Azure Functions, and other services.
This will bring up the development database, STAC API, Tiler, Azure Functions, and other services. If at this point something errors out (e.g. nginx complaining about a config file), try deleting the containers/images and rerunning `./scripts/setup`.

To test the tiler, try going to <http://localhost:8080/data/mosaic/info?collection=naip>.
The STAC API can be found at <http://localhost:8080/stac/> (goes through nginx) or <http://localhost:8081> directly.

To hit the tiler, try going to <http://localhost:8080/data/mosaic/info?collection=naip>, although it will fail due to lack of an authorization header.

#### Testing and and formatting

To run tests, use
To run tests, use one of the following (note, you don't need `./scripts/server` running). If you get an immediate error related to library stubs, just run it again. The tiler tests may fail locally, TBD why.

```console
./scripts/test
./scripts/test --stac
./scripts/test --tiler
./scripts/test --common
```

To format code, use
Expand Down
7 changes: 6 additions & 1 deletion pcstac/pcstac/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from urllib.parse import urljoin

import attr
from fastapi import Request
from fastapi import HTTPException, Request
from stac_fastapi.pgstac.core import CoreCrudClient
from stac_fastapi.types.errors import NotFoundError
from stac_fastapi.types.stac import (
Expand Down Expand Up @@ -215,7 +215,12 @@ async def _fetch() -> ItemCollection:
)
return item_collection

# Block searches that don't specify a collection
if search_request.collections is None and "collection=" not in str(request.url):
raise HTTPException(status_code=422, detail="collection is required")

search_json = search_request.model_dump_json()

add_stac_attributes_from_search(search_json, request)

logger.info(
Expand Down
41 changes: 26 additions & 15 deletions pcstac/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ async def test_item_search_bbox_get(app_client):
assert resp_json["features"][0]["id"] == first_item["id"]


# @pytest.mark.skip(reason="TODO")
@pytest.mark.asyncio
async def test_item_search_get_without_collections(app_client):
"""Test GET search without specifying collections"""
Expand All @@ -234,9 +235,7 @@ async def test_item_search_get_without_collections(app_client):
"bbox": ",".join([str(coord) for coord in first_item["bbox"]]),
}
resp = await app_client.get("/search", params=params)
assert resp.status_code == 200
resp_json = resp.json()
assert resp_json["features"][0]["id"] == first_item["id"]
assert resp.status_code == 422 # Unprocessable Content


@pytest.mark.asyncio
Expand Down Expand Up @@ -299,9 +298,7 @@ async def test_item_search_post_without_collection(app_client):
"bbox": first_item["bbox"],
}
resp = await app_client.post("/search", json=params)
assert resp.status_code == 200
resp_json = resp.json()
assert resp_json["features"][0]["id"] == first_item["id"]
assert resp.status_code == 422 # Unprocessable Content


@pytest.mark.asyncio
Expand All @@ -313,7 +310,10 @@ async def test_item_search_properties_jsonb(app_client):
first_item = items_resp.json()["features"][0]

# EPSG is a JSONB key
params = {"query": {"proj:epsg": {"eq": first_item["properties"]["proj:epsg"]}}}
params = {
"collections": [first_item["collection"]],
"query": {"proj:epsg": {"eq": first_item["properties"]["proj:epsg"]}},
}
print(params)
resp = await app_client.post("/search", json=params)
assert resp.status_code == 200
Expand Down Expand Up @@ -459,7 +459,7 @@ async def test_pagination_post(app_client):
ids = [item["id"] for item in items_resp.json()["features"]]

# Paginate through all 5 items with a limit of 1 (expecting 5 requests)
request_body = {"ids": ids, "limit": 1}
request_body = {"ids": ids, "limit": 1, "collections": ["naip"]}
page = await app_client.post("/search", json=request_body)
idx = 0
item_ids = []
Expand Down Expand Up @@ -489,7 +489,11 @@ async def test_pagination_token_idempotent(app_client):
# so that a "next" link is returned
page = await app_client.get(
"/search",
params={"datetime": "1900-01-01T00:00:00Z/2030-01-01T00:00:00Z", "limit": 3},
params={
"datetime": "1900-01-01T00:00:00Z/2030-01-01T00:00:00Z",
"limit": 3,
"collections": ["naip"],
},
)
assert page.status_code == 200

Expand All @@ -516,7 +520,10 @@ async def test_pagination_token_idempotent(app_client):
@pytest.mark.asyncio
async def test_field_extension_get(app_client):
"""Test GET search with included fields (fields extension)"""
params = {"fields": "+properties.proj:epsg,+properties.gsd,+collection"}
params = {
"fields": "+properties.proj:epsg,+properties.gsd,+collection",
"collections": ["naip"],
}
resp = await app_client.get("/search", params=params)
print(resp.json())
feat_properties = resp.json()["features"][0]["properties"]
Expand All @@ -526,7 +533,7 @@ async def test_field_extension_get(app_client):
@pytest.mark.asyncio
async def test_field_extension_exclude_default_includes(app_client):
"""Test POST search excluding a forbidden field (fields extension)"""
body = {"fields": {"exclude": ["geometry"]}}
body = {"fields": {"exclude": ["geometry"]}, "collections": ["naip"]}

resp = await app_client.post("/search", json=body)
resp_json = resp.json()
Expand All @@ -538,7 +545,7 @@ async def test_search_intersects_and_bbox(app_client):
"""Test POST search intersects and bbox are mutually exclusive (core)"""
bbox = [-118, 34, -117, 35]
geoj = Polygon.from_bounds(*bbox).model_dump(exclude_none=True)
params = {"bbox": bbox, "intersects": geoj}
params = {"bbox": bbox, "intersects": geoj, "collections": ["naip"]}
resp = await app_client.post("/search", json=params)
assert resp.status_code == 400

Expand Down Expand Up @@ -599,15 +606,18 @@ async def test_tiler_link_construction(app_client):

@pytest.mark.asyncio
async def test_search_bbox_errors(app_client):
body = {"query": {"bbox": [0]}}
body = {"query": {"bbox": [0]}, "collections": ["naip"]}
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]}}
body = {
"query": {"bbox": [100.0, 0.0, 0.0, 105.0, 1.0, 1.0]},
"collections": ["naip"],
}
resp = await app_client.post("/search", json=body)
assert resp.status_code == 400

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

Expand All @@ -628,6 +638,7 @@ async def test_search_get_page_limits(app_client):
assert len(resp_json["features"]) == 12


@pytest.mark.skip(reason="Are these params even valid? they dont match the model")
@pytest.mark.asyncio
async def test_search_post_page_limits(app_client):
params = {"op": "=", "args": [{"property": "collection"}, "naip"]}
Expand Down

0 comments on commit a255829

Please sign in to comment.