Skip to content

Commit

Permalink
Change PUT requests route paths
Browse files Browse the repository at this point in the history
- Require filename path parameter
  • Loading branch information
zuhdil committed Sep 23, 2024
1 parent 4d88b24 commit 091c5d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
21 changes: 13 additions & 8 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def get_config_for(instance: str) -> dict[str, str]:
async def upload(
instance: str,
form_id: str,
filename: str,
file: UploadFile,
folder: str,
make_bucket: Callable[[dict[str, str]], S3Bucket],
Expand All @@ -47,7 +48,7 @@ async def upload(
extra_args = {"ContentType": file.content_type}
if folder == "images":
extra_args["ACL"] = "public-read"
file_key = f"{folder}/{str(file.filename)}"
file_key = f"{folder}/{str(filename)}"
bucket.upload(file.file, file_key, extra_args)
return ResultMessage.success("OK!")

Expand All @@ -59,36 +60,40 @@ def provide_upload(
make_form_validator: Annotated[
Callable[[dict[str, str]], FormValidator], Depends(provide_make_form_validator)
],
) -> Callable[[str, str, UploadFile, str], Awaitable[ResultMessage]]:
) -> Callable[[str, str, str, UploadFile, str], Awaitable[ResultMessage]]:
return partial(
upload, make_bucket=make_bucket, make_form_validator=make_form_validator
)


@app.put("/{instance}/devicezip/{form_id}/", status_code=status.HTTP_201_CREATED)
@app.put(
"/{instance}/devicezip/{form_id}/{filename}", status_code=status.HTTP_201_CREATED
)
async def put_devicezip(
instance: str,
form_id: FormIdParam,
filename: str,
file: UploadFile,
upload: Annotated[
Callable[[str, str, UploadFile, str], Awaitable[ResultMessage]],
Callable[[str, str, str, UploadFile, str], Awaitable[ResultMessage]],
Depends(provide_upload),
],
) -> ResultMessage:
return await upload(instance, form_id, file, "devicezip")
return await upload(instance, form_id, filename, file, "devicezip")


@app.put("/{instance}/images/{form_id}/", status_code=status.HTTP_201_CREATED)
@app.put("/{instance}/images/{form_id}/{filename}", status_code=status.HTTP_201_CREATED)
async def put_images(
instance: str,
form_id: FormIdParam,
filename: str,
file: UploadFile,
upload: Annotated[
Callable[[str, str, UploadFile, str], Awaitable[ResultMessage]],
Callable[[str, str, str, UploadFile, str], Awaitable[ResultMessage]],
Depends(provide_upload),
],
) -> ResultMessage:
return await upload(instance, form_id, file, "images")
return await upload(instance, form_id, filename, file, "images")


@app.get("/{instance}/surveys/{versioned_form_id}.zip")
Expand Down
8 changes: 4 additions & 4 deletions backend/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_put_devicezip_should_call_bucket_upload(fake_bucket, fake_validator):
with patch("app.main.get_config", return_value=fake_config):
fake_validator.validate.return_value = True
client.put(
"/instance1/devicezip/123/",
f"/instance1/devicezip/123/{file_name}",
files={"file": (file_name, file_content, file_content_type)},
)

Expand All @@ -59,7 +59,7 @@ def test_put_devicezip_with_invalid_instance_returns_404(fake_bucket):
fake_config = {}
with patch("app.main.get_config", return_value=fake_config):
response = client.put(
"/instance1/devicezip/123/",
"/instance1/devicezip/123/test.txt",
files={"file": io.BytesIO(b"<file content>")},
)

Expand All @@ -75,7 +75,7 @@ def test_put_images_should_call_bucket_upload(fake_bucket, fake_validator):
with patch("app.main.get_config", return_value=fake_config):
fake_validator.validate.return_value = True
client.put(
"/instance1/images/123/",
f"/instance1/images/123/{file_name}",
files={"file": (file_name, file_content, file_content_type)},
)

Expand All @@ -90,7 +90,7 @@ def test_put_images_with_invalid_instance_returns_404(fake_bucket):
fake_config = {}
with patch("app.main.get_config", return_value=fake_config):
response = client.put(
"/instance1/images/123/",
"/instance1/images/123/test.jpg",
files={"file": io.BytesIO(b"<file content>")},
)

Expand Down

0 comments on commit 091c5d9

Please sign in to comment.