Skip to content

Commit

Permalink
Throw 400s for invalid datetime formats (#323)
Browse files Browse the repository at this point in the history
* Throw 400s for bad datetime formats

* update changelog
  • Loading branch information
moradology authored Jan 14, 2022
1 parent caf6a14 commit a7437c1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
6 changes: 4 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
* The minimum `limit` value for searches is now 1 ([#296](https://github.com/stac-utils/stac-fastapi/pull/296))
* Links stored with Collections and Items (e.g. license links) are now returned with those STAC objects ([#282](https://github.com/stac-utils/stac-fastapi/pull/282))
* Content-type response headers for the /api endpoint now reflect those expected in the STAC api spec ([#287](https://github.com/stac-utils/stac-fastapi/pull/287))
* Changed type options for datetime in BaseSearchGetRequest ([#315](https://github.com/stac-utils/stac-fastapi/pull/318))
* Expanded on tests to ensure properly testing get and post searches ([#315](https://github.com/stac-utils/stac-fastapi/pull/318))
* Changed type options for datetime in BaseSearchGetRequest ([#318](https://github.com/stac-utils/stac-fastapi/pull/318))
* Expanded on tests to ensure properly testing get and post searches ([#318](https://github.com/stac-utils/stac-fastapi/pull/318))
* Ensure invalid datetimes result in 400s ([#323](https://github.com/stac-utils/stac-fastapi/pull/323))


## [2.2.0]

Expand Down
1 change: 1 addition & 0 deletions stac_fastapi/pgstac/stac_fastapi/pgstac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ async def get_search(
"token": token,
"query": orjson.loads(query) if query else query,
}

if datetime:
base_args["datetime"] = datetime

Expand Down
6 changes: 0 additions & 6 deletions stac_fastapi/pgstac/stac_fastapi/pgstac/types/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ class PgstacSearch(BaseSearchPostRequest):
Overrides the validation for datetime from the base request model.
"""

datetime: Optional[str] = None
conf: Optional[Dict] = None

@validator("datetime")
def validate_datetime(cls, v):
"""Pgstac does not require the base validator for datetime."""
return v

@validator("filter_lang", pre=False, check_fields=False, always=True)
def validate_query_uses_cql(cls, v, values):
"""If using query syntax, forces cql-json."""
Expand Down
21 changes: 21 additions & 0 deletions stac_fastapi/pgstac/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -1232,3 +1232,24 @@ async def test_item_search_get_filter_extension_cql2(
resp_json["features"][0]["properties"]["proj:epsg"]
== test_item["properties"]["proj:epsg"]
)


@pytest.mark.asyncio
async def test_search_datetime_validation_errors(app_client):
bad_datetimes = [
"37-01-01T12:00:27.87Z",
"1985-13-12T23:20:50.52Z",
"1985-12-32T23:20:50.52Z",
"1985-12-01T25:20:50.52Z",
"1985-12-01T00:60:50.52Z",
"1985-12-01T00:06:61.52Z",
"1990-12-31T23:59:61Z",
"1986-04-12T23:20:50.52Z/1985-04-12T23:20:50.52Z",
]
for dt in bad_datetimes:
body = {"query": {"datetime": dt}}
resp = await app_client.post("/search", json=body)
assert resp.status_code == 400

resp = await app_client.get("/search?datetime={}".format(dt))
assert resp.status_code == 400
2 changes: 2 additions & 0 deletions stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,10 @@ def get_search(
"token": token,
"query": json.loads(query) if query else query,
}

if datetime:
base_args["datetime"] = datetime

if sortby:
# https://github.com/radiantearth/stac-spec/tree/master/api-spec/extensions/sort#http-get-or-post-form
sort_param = []
Expand Down
20 changes: 20 additions & 0 deletions stac_fastapi/sqlalchemy/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,3 +926,23 @@ def test_conformance_classes_configurable():
os.environ["WRITER_CONN_STRING"] = "testing"
client = CoreCrudClient(base_conformance_classes=["this is a test"])
assert client.conformance_classes()[0] == "this is a test"


def test_search_datetime_validation_errors(app_client):
bad_datetimes = [
"37-01-01T12:00:27.87Z",
"1985-13-12T23:20:50.52Z",
"1985-12-32T23:20:50.52Z",
"1985-12-01T25:20:50.52Z",
"1985-12-01T00:60:50.52Z",
"1985-12-01T00:06:61.52Z",
"1990-12-31T23:59:61Z",
"1986-04-12T23:20:50.52Z/1985-04-12T23:20:50.52Z",
]
for dt in bad_datetimes:
body = {"query": {"datetime": dt}}
resp = app_client.post("/search", json=body)
assert resp.status_code == 400

resp = app_client.get("/search?datetime={}".format(dt))
assert resp.status_code == 400

0 comments on commit a7437c1

Please sign in to comment.