Skip to content

Commit

Permalink
Merge pull request #332 from ral-facilities/bugfix/return-404-on-endp…
Browse files Browse the repository at this point in the history
…oints-with-non-existent-dataset-pid-#327

Return 404 on /files and /files/count endpoints with non-existent dataset pid
  • Loading branch information
VKTB authored Feb 17, 2022
2 parents 263ad34 + 8e0cfc1 commit e23f0c4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
6 changes: 6 additions & 0 deletions datagateway_api/src/search_api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ def get_files(entity_name, pid, filters):
"Entity Name: %s, Filters: %s", entity_name, filters,
)

# Check if dataset with such pid exists before proceeding
get_with_pid("Dataset", pid, [])

filters.append(SearchAPIWhereFilter("dataset.pid", pid, "eq"))
return get_search(entity_name, filters)

Expand All @@ -171,5 +174,8 @@ def get_files_count(entity_name, filters, pid):
"Entity Name: %s, Filters: %s", entity_name, filters,
)

# Check if dataset with such pid exists before proceeding
get_with_pid("Dataset", pid, [])

filters.append(SearchAPIWhereFilter("dataset.pid", pid, "eq"))
return get_count(entity_name, filters)
13 changes: 6 additions & 7 deletions test/search_api/endpoints/test_count_dataset_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ class TestSearchAPICountDatasetFilesEndpoint:
{"count": 0},
id="Count dataset files with filter to return zero count",
),
pytest.param(
"unknown pid", "{}", {"count": 0}, id="Non-existent dataset pid",
),
],
)
def test_valid_count_dataset_files_endpoint(
Expand All @@ -54,22 +51,24 @@ def test_valid_count_dataset_files_endpoint(
assert test_response.json == expected_json

@pytest.mark.parametrize(
"pid, request_filter",
"pid, request_filter, expected_status_code",
[
pytest.param("0-8401-1070-7", '{"bad filter"}', id="Bad filter"),
pytest.param("0-8401-1070-7", '{"bad filter"}', 400, id="Bad filter"),
pytest.param(
"0-8401-1070-7",
'{"where": {"name": "FILE 4"}}',
400,
id="Where filter inside where query param",
),
pytest.param("my 404 test pid", "{}", 404, id="Non-existent dataset pid"),
],
)
def test_invalid_count_dataset_files_endpoint(
self, flask_test_app_search_api, pid, request_filter,
self, flask_test_app_search_api, pid, request_filter, expected_status_code,
):
test_response = flask_test_app_search_api.get(
f"{Config.config.search_api.extension}/datasets/{pid}/files/count"
f"?where={request_filter}",
)

assert test_response.status_code == 400
assert test_response.status_code == expected_status_code
9 changes: 1 addition & 8 deletions test/search_api/endpoints/test_get_dataset_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,7 @@ def test_valid_get_dataset_files_endpoint(
pytest.param(
"0-8401-1070-7", '{"include": ""}', 400, id="Bad include filter",
),
pytest.param(
"my 404 test pid",
"{}",
404,
id="Non-existent dataset pid",
# Skipped because this actually returns 200
marks=pytest.mark.skip,
),
pytest.param("my 404 test pid", "{}", 404, id="Non-existent dataset pid"),
],
)
def test_invalid_get_dataset_files_endpoint(
Expand Down

0 comments on commit e23f0c4

Please sign in to comment.