From 70ddb7a4fd89ba10b06cd71c3ab2a98648cfb773 Mon Sep 17 00:00:00 2001 From: Matthew Richards Date: Tue, 23 Nov 2021 17:28:31 +0000 Subject: [PATCH 1/3] fix: allow blank extensions and slash extension to be valid --- datagateway_api/src/common/config.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/datagateway_api/src/common/config.py b/datagateway_api/src/common/config.py index f713c211..40cfe6ea 100644 --- a/datagateway_api/src/common/config.py +++ b/datagateway_api/src/common/config.py @@ -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 From 36b42c0899b298e4d7a546383dff3dbb06cce924 Mon Sep 17 00:00:00 2001 From: Matthew Richards Date: Tue, 23 Nov 2021 17:29:17 +0000 Subject: [PATCH 2/3] test: add tests for extension validation logic --- test/test_config.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/test/test_config.py b/test/test_config.py index 86c52750..9bd84875 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -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() @@ -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) From f4339f24d3f297323d215173a411873bd6920a84 Mon Sep 17 00:00:00 2001 From: Matthew Richards <32678030+MRichards99@users.noreply.github.com> Date: Wed, 24 Nov 2021 11:19:40 +0000 Subject: [PATCH 3/3] test: correct test data Co-authored-by: Viktor Bozhinov <45173816+VKTB@users.noreply.github.com> --- test/test_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_config.py b/test/test_config.py index 9bd84875..8b01bbbc 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -133,7 +133,7 @@ def test_valid_extension_validation(self, input_extension, expected_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"), + pytest.param("/my-extension/", id="Extension with trailing slash"), ], ) def test_invalid_extension_validation(self, input_extension):