Skip to content

Commit

Permalink
Add check to ensure status param in request is a valid JobStatus memb…
Browse files Browse the repository at this point in the history
…er. Also add corresponding test
  • Loading branch information
jewelltaylor committed Apr 17, 2024
1 parent 7d6ee49 commit f9978f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions florist/api/routes/server/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,15 @@ async def list_jobs_with_status(status: JobStatus, request: Request) -> List[Dic
:param request: (fastapi.Request) the FastAPI request object.
:return: (List[Dict[str, Any]]) A list where each entry is a dictionary with the attributes
of a Job instance with the specified status.
:raises: (HTTPException) status 400 if status is not a member of the JobStatus Enum.
"""
status = jsonable_encoder(status)

assert isinstance(status, str)
if status not in JobStatus._member_names_:
msg = f"status is not valid. status: {status}."
raise HTTPException(status_code=400, detail=msg)

job_db = request.app.database[JOB_DATABASE_NAME]
result = await job_db.find({"status": status}).to_list(MAX_RECORDS_TO_FETCH)
assert isinstance(result, list)
Expand Down
7 changes: 7 additions & 0 deletions florist/tests/integration/api/routes/server/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,10 @@ async def test_list_jobs_with_status(mock_request) -> None:
}
assert isinstance(result_finished_successfully[0]["clients_info"][0]["_id"], str)
assert isinstance(result_finished_successfully[0]["clients_info"][1]["_id"], str)

async def test_list_jobs_with_invalid_status(mock_request) -> None:
with raises(HTTPException) as exception_info:
await list_jobs_with_status("NON_EXISTENT_STATUS", mock_request)

assert exception_info.value.status_code == 400
assert "status is not valid. status: NON_EXISTENT_STATUS" in exception_info.value.detail

0 comments on commit f9978f5

Please sign in to comment.