Skip to content

Commit

Permalink
Merge branch 'use_app_factory' of https://github.com/opendatacube/dat…
Browse files Browse the repository at this point in the history
…acube-explorer into use_app_factory
  • Loading branch information
Ariana Barzinpour committed Jul 1, 2024
2 parents fd79a93 + dbd2a53 commit 5d80889
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
23 changes: 18 additions & 5 deletions cubedash/_pages.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import decimal
import re
from datetime import datetime, timedelta
from typing import List, Tuple

import datacube
import dateutil.parser
import flask
import structlog
from datacube.model import DatasetType, Range
Expand All @@ -17,6 +19,7 @@
request,
url_for,
)
from sqlalchemy.exc import DataError
from werkzeug.datastructures import MultiDict

import cubedash
Expand Down Expand Up @@ -163,7 +166,14 @@ def search_page(
)

args = MultiDict(flask.request.args)
query = utils.query_to_search(args, product=product)
try:
query = utils.query_to_search(args, product=product)
except ValueError as e: # invalid query val
abort(400, str(e))
except dateutil.parser._parser.ParserError:
abort(400, "Invalid datetime format")
except decimal.InvalidOperation:
abort(400, "Could not convert value to decimal")

# Always add time range, selected product to query
if product_name:
Expand Down Expand Up @@ -206,10 +216,13 @@ def search_page(
_LOG.info("query", query=query)

# TODO: Add sort option to index API
datasets = sorted(
_model.STORE.index.datasets.search(**query, limit=_HARD_SEARCH_LIMIT + 1),
key=lambda d: default_utc(d.center_time),
)
try:
datasets = sorted(
_model.STORE.index.datasets.search(**query, limit=_HARD_SEARCH_LIMIT + 1),
key=lambda d: default_utc(d.center_time),
)
except DataError:
abort(400, "Invalid field value provided in query")

more_datasets_exist = False
if len(datasets) > _HARD_SEARCH_LIMIT:
Expand Down
17 changes: 17 additions & 0 deletions integration_tests/test_page_loads.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,3 +1014,20 @@ def expect_404(url: str, message_contains: str = None):

expect_404(f"/dataset/{dataset_id}")
expect_404(f"/dataset/{dataset_id}.odc-metadata.yaml")


def test_invalid_query_gives_400(client: FlaskClient):
"""
We should get 400 errors, not errors, for an invalid field values in a query
"""

def expect_400(url: str):
__tracebackhide__ = True
get_text_response(client, url, expect_status_code=400)

# errors that are caught when parsing query args
expect_400("/products/ga_ls8c_ard_3/datasets?time=asdf")
expect_400("/products/ga_ls8c_ard_3/datasets?lat=asdf")
# errors that aren't caught until the db query
expect_400("/products/ga_ls8c_ard_3/datasets?indexed_time=asdf")
expect_400("/products/ga_ls8c_ard_3/datasets?id=asdf")

0 comments on commit 5d80889

Please sign in to comment.