Skip to content

Commit

Permalink
Merge pull request #293 from ral-facilities/bugfix/api-extension-vali…
Browse files Browse the repository at this point in the history
…dation

Extension Validation Bugfix
  • Loading branch information
MRichards99 authored Nov 24, 2021
2 parents 210c5fe + f4339f2 commit 1a10fd4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
9 changes: 5 additions & 4 deletions datagateway_api/src/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ def validate_extension(extension):
"""
extension = extension.strip()

if not extension.startswith("/"):
raise ValueError("must start with '/'")
if extension.endswith("/"):
raise ValueError("must not end with '/'")
if extension:
if not extension.startswith("/"):
raise ValueError("must start with '/'")
if extension.endswith("/") and len(extension) != 1:
raise ValueError("must not end with '/'")

return extension

Expand Down
38 changes: 37 additions & 1 deletion test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from datagateway_api.src.common.config import APIConfig
from datagateway_api.src.common.config import APIConfig, validate_extension


@pytest.fixture()
Expand Down Expand Up @@ -103,3 +103,39 @@ def test_set_backend_type(self, test_config):
test_config.datagateway_api.set_backend_type("backend_name_changed")

assert test_config.datagateway_api.backend == "backend_name_changed"

@pytest.mark.parametrize(
"input_extension, expected_extension",
[
pytest.param("/", "/", id="Slash"),
pytest.param("", "", id="Empty string, implied slash"),
pytest.param("/datagateway-api", "/datagateway-api", id="DataGateway API"),
pytest.param(
" /datagateway-api ",
"/datagateway-api",
id="DataGateway API with trailing and leading spaces",
),
pytest.param("/search-api", "/search-api", id="Search API"),
pytest.param(
" /search-api ",
"/search-api",
id="Search API with trailing and leading spaces",
),
],
)
def test_valid_extension_validation(self, input_extension, expected_extension):
test_extension = validate_extension(input_extension)

assert test_extension == expected_extension

@pytest.mark.parametrize(
"input_extension",
[
pytest.param("datagateway-api", id="DataGateway API with no leading slash"),
pytest.param("search-api", id="Search API with no leading slash"),
pytest.param("/my-extension/", id="Extension with trailing slash"),
],
)
def test_invalid_extension_validation(self, input_extension):
with pytest.raises(ValueError):
validate_extension(input_extension)

0 comments on commit 1a10fd4

Please sign in to comment.