Skip to content

Commit

Permalink
Test for filters being well formed #2714
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed Jun 22, 2022
1 parent 10bd84a commit f892805
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def list_cases(self, page: int = None, limit: int = None, filter: str = None):
if validation_error is not None:
return jsonify(validation_error), 400
predicate = CaseController.parse_filter(filter)
if predicate is None:
validation_error = {"message:" "cannot understand query"}
return jsonify(validation_error), 422
cases = self.store.fetch_cases(page, limit, predicate)
count = self.store.count_cases(predicate)
response = {"cases": cases, "total": count}
Expand All @@ -55,6 +58,8 @@ def parse_filter(filter: str) -> Filter:
# split query on spaces
components = filter.split(" ")
filters = [CaseController.individual_filter(c) for c in components]
if None in filters:
return None
if len(filters) == 1:
return filters[0]
else:
Expand All @@ -65,6 +70,8 @@ def individual_filter(term: str) -> Filter:
"""Turn a single property:value filter request into a filter object"""
# keyword value pairs separated by colon
(keyword, value) = term.split(":")
if len(keyword) == 0 or len(value) == 0:
return None
# special case dateconfirmedbefore, dateconfirmedafter
if keyword == "dateconfirmedbefore":
return PropertyFilter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,10 @@ def test_list_cases_no_matching_results(client_with_patched_mongo):
assert response.status_code == 200
assert len(response.json["cases"]) == 0
assert response.json["total"] == 0


def test_list_cases_with_bad_filter_rejected(client_with_patched_mongo):
response = client_with_patched_mongo.get(
f"/api/cases?q=country%3A"
)
assert response.status_code == 422

0 comments on commit f892805

Please sign in to comment.