Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Sep 30, 2024
1 parent 0715010 commit e5a3a8e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
26 changes: 18 additions & 8 deletions chord_metadata_service/chord/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,25 @@ def test_dats_as_attachment(self):
self.assertEqual(r.status_code, status.HTTP_201_CREATED)
dataset_id = Dataset.objects.first().identifier

response = self.client.get(f"/api/datasets/{dataset_id}/dats?attachment=true")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertDictEqual(response.data, payload['dats_file'])
self.assertEqual(response.headers["Content-Disposition"], f"attachment; filename=\"{dataset_id}_dats.json\"")
subtest_params = [
("?attachment=true", True),
("?attachment=false", False),
("?attachment=", False),
("", False),
]

response = self.client.get(f"/api/datasets/{dataset_id}/dats?attachment=false")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertDictEqual(response.data, payload['dats_file'])
self.assertNotIn("Content-Disposition", response.headers)
for params in subtest_params:
with self.subTest(params=params):
response = self.client.get(f"/api/datasets/{dataset_id}/dats{params[0]}")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertDictEqual(response.data, payload['dats_file'])
if params[1]:
self.assertEqual(
response.headers["Content-Disposition"],
f"attachment; filename=\"{dataset_id}_dats.json\""
)
else:
self.assertNotIn("Content-Disposition", response.headers)

def test_resources(self):
resource = {
Expand Down
6 changes: 1 addition & 5 deletions chord_metadata_service/restapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from chord_metadata_service.phenopackets.models import Biosample

__all__ = [
"COMPUTED_PROPERTY_PREFIX",
"camel_case_field_names",
"transform_keys",
"computed_property",
"remove_computed_properties",
Expand All @@ -25,6 +23,7 @@


COMPUTED_PROPERTY_PREFIX = "__"
FILENAME_REPLACE_PATTERN = re.compile(r"[\\/:*?\"<>|]")


def camel_case_field_names(string) -> str:
Expand Down Expand Up @@ -117,9 +116,6 @@ def response_as_attachment(request: DrfRequest) -> bool:
return request.query_params.get("attachment", "").strip().lower() in ("1", "true", "yes")


FILENAME_REPLACE_PATTERN = re.compile(r"[\\/:*?\"<>|]")


def attachment_content_disposition(filename: str) -> dict[str, str]:
filename_safe = FILENAME_REPLACE_PATTERN.sub("_", filename)
return {"Content-Disposition": f"attachment; filename=\"{filename_safe}\""}
Expand Down

0 comments on commit e5a3a8e

Please sign in to comment.