Skip to content

Commit

Permalink
feat(study-search):improve the get_studies endpoint to better check `…
Browse files Browse the repository at this point in the history
…versions` and `users` parameters
  • Loading branch information
laurent-laporte-pro committed Jan 25, 2024
1 parent 529cab4 commit 0e54040
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
21 changes: 12 additions & 9 deletions antarest/study/web/studies_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,16 @@ def get_studies(
managed: t.Optional[bool] = Query(None, description="Filter studies based on their management status."),
archived: t.Optional[bool] = Query(None, description="Filter studies based on their archive status."),
variant: t.Optional[bool] = Query(None, description="Filter studies based on their variant status."),
versions: str = Query("", description="Comma-separated list of versions for filtering."),
users: str = Query("", description="Comma-separated list of group IDs for filtering."),
versions: str = Query(
"",
description="Comma-separated list of versions for filtering.",
regex=r"^\s*(?:\d+\s*(?:,\s*\d+\s*)*)?$",
),
users: str = Query(
"",
description="Comma-separated list of user IDs for filtering.",
regex=r"^\s*(?:\d+\s*(?:,\s*\d+\s*)*)?$",
),
groups: str = Query("", description="Comma-separated list of group IDs for filtering."),
tags: str = Query("", description="Comma-separated list of tags for filtering."),
study_ids: str = Query(
Expand Down Expand Up @@ -115,7 +123,7 @@ def get_studies(
- `archived`: Filter studies based on their archive status.
- `variant`: Filter studies based on their variant status.
- `versions`: Comma-separated list of versions for filtering.
- `users`: Comma-separated list of group IDs for filtering.
- `users`: Comma-separated list of user IDs for filtering.
- `groups`: Comma-separated list of group IDs for filtering.
- `tags`: Comma-separated list of tags for filtering.
- `studyIds`: Comma-separated list of study IDs for filtering.
Expand All @@ -134,12 +142,7 @@ def get_studies(
logger.info("Fetching for matching studies", extra={"user": current_user.id})
params = RequestParameters(user=current_user)

# todo: there must be another way to do this
# for instance by using a pydantic model with a custom validator
try:
user_list = [int(v) for v in _split_comma_separated_values(users)]
except ValueError:
raise HTTPException(status_code=422, detail="'users' must be a list of integers") from None
user_list = [int(v) for v in _split_comma_separated_values(users)]

study_filter = StudyFilter(
name=name,
Expand Down
8 changes: 7 additions & 1 deletion tests/integration/studies_blueprint/test_get_studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,11 +751,17 @@ def test_get_studies__invalid_parameters(
description = res.json()["description"]
assert re.search(r"could not be parsed to a boolean", description), f"{description=}"

# Invalid `versions` parameter (not a list of integers)
res = client.get(STUDIES_URL, headers=headers, params={"versions": "invalid"})
assert res.status_code == INVALID_PARAMS_STATUS_CODE, res.json()
description = res.json()["description"]
assert re.search(r"string does not match regex", description), f"{description=}"

# Invalid `users` parameter (not a list of integers)
res = client.get(STUDIES_URL, headers=headers, params={"users": "invalid"})
assert res.status_code == INVALID_PARAMS_STATUS_CODE, res.json()
description = res.json()["description"]
assert re.search(r"must be a list of integers", description), f"{description=}"
assert re.search(r"string does not match regex", description), f"{description=}"

# Invalid `exists` parameter (not a boolean)
res = client.get(STUDIES_URL, headers=headers, params={"exists": "invalid"})
Expand Down

0 comments on commit 0e54040

Please sign in to comment.