From 5365a292924c2bb1739177aa96fcd35ae7b55ac7 Mon Sep 17 00:00:00 2001 From: Markus Scheidgen Date: Tue, 5 Jan 2021 15:27:26 +0100 Subject: [PATCH 1/6] Reflect CONFIG.root_path in get_base_url --- optimade/server/routers/utils.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/optimade/server/routers/utils.py b/optimade/server/routers/utils.py index 45ecdefcd..3faf05e26 100644 --- a/optimade/server/routers/utils.py +++ b/optimade/server/routers/utils.py @@ -185,10 +185,11 @@ def get_base_url( if isinstance(parsed_url_request, str) else parsed_url_request ) + root_path = CONFIG.root_path.rstrip("/") if CONFIG.root_path else "" return ( CONFIG.base_url.rstrip("/") if CONFIG.base_url - else f"{parsed_url_request.scheme}://{parsed_url_request.netloc}" + else f"{parsed_url_request.scheme}://{parsed_url_request.netloc}{root_path}" ) @@ -213,9 +214,12 @@ def get_entries( query = urllib.parse.parse_qs(request.url.query) query["page_offset"] = int(query.get("page_offset", [0])[0]) + len(results) urlencoded = urllib.parse.urlencode(query, doseq=True) - base_url = get_base_url(request.url) + root_path = CONFIG.root_path.rstrip("/") if CONFIG.root_path else "" + base_url = ( + f"{get_base_url(request.url)}{request.url.path.replace(root_path, '')}" + ) - links = ToplevelLinks(next=f"{base_url}{request.url.path}?{urlencoded}") + links = ToplevelLinks(next=f"{base_url}?{urlencoded}") else: links = ToplevelLinks(next=None) From cce260218b96adb784d2741567844936e386a696 Mon Sep 17 00:00:00 2001 From: Casper Welzel Andersen Date: Mon, 8 Mar 2021 20:24:57 +0100 Subject: [PATCH 2/6] Rename variable to be more representative --- optimade/server/routers/utils.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/optimade/server/routers/utils.py b/optimade/server/routers/utils.py index 3faf05e26..e1c12a5e2 100644 --- a/optimade/server/routers/utils.py +++ b/optimade/server/routers/utils.py @@ -215,11 +215,9 @@ def get_entries( query["page_offset"] = int(query.get("page_offset", [0])[0]) + len(results) urlencoded = urllib.parse.urlencode(query, doseq=True) root_path = CONFIG.root_path.rstrip("/") if CONFIG.root_path else "" - base_url = ( - f"{get_base_url(request.url)}{request.url.path.replace(root_path, '')}" - ) + url = f"{get_base_url(request.url)}{request.url.path.replace(root_path, '')}" - links = ToplevelLinks(next=f"{base_url}?{urlencoded}") + links = ToplevelLinks(next=f"{url}?{urlencoded}") else: links = ToplevelLinks(next=None) From 83e1d90ca4e283796da601383f8cd59413c5dfb7 Mon Sep 17 00:00:00 2001 From: Casper Welzel Andersen Date: Tue, 9 Mar 2021 11:35:51 +0100 Subject: [PATCH 3/6] Add root_path to test config --- tests/test_config.json | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_config.json b/tests/test_config.json index 683fd248d..e18c4c9b9 100644 --- a/tests/test_config.json +++ b/tests/test_config.json @@ -2,6 +2,7 @@ "debug": false, "default_db": "test_server", "base_url": "http://example.org", + "root_path": "/custom_root_path/", "implementation": { "name": "Example implementation", "source_url": "https://github.com/Materials-Consortia/optimade-python-tools", From c2aa29bc4caf32099e38b2a3a7ebb427f8c1ec69 Mon Sep 17 00:00:00 2001 From: Casper Welzel Andersen Date: Tue, 9 Mar 2021 11:45:20 +0100 Subject: [PATCH 4/6] Use quotation marks for all id value tests --- tests/server/middleware/test_api_hint.py | 8 ++++---- tests/server/query_params/test_filter.py | 2 +- tests/server/routers/test_structures.py | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/server/middleware/test_api_hint.py b/tests/server/middleware/test_api_hint.py index bcda58174..2e89b0acf 100644 --- a/tests/server/middleware/test_api_hint.py +++ b/tests/server/middleware/test_api_hint.py @@ -11,7 +11,7 @@ def test_correct_api_hint(both_clients, check_response): links_id = "index" major_version = BASE_URL_PREFIXES["major"][1:] # Remove prefixed `/` - query_url = f"/links?api_hint={major_version}&filter=id={links_id}" + query_url = f'/links?api_hint={major_version}&filter=id="{links_id}"' check_response( request=query_url, @@ -27,7 +27,7 @@ def test_incorrect_api_hint(both_clients, check_error_response): links_id = "index" incorrect_version = int(BASE_URL_PREFIXES["major"][len("/v") :]) + 1 incorrect_version = f"v{incorrect_version}" - query_url = f"/links?api_hint={incorrect_version}&filter=id={links_id}" + query_url = f'/links?api_hint={incorrect_version}&filter=id="{links_id}"' with pytest.raises(VersionNotSupported): check_error_response( @@ -52,7 +52,7 @@ def test_url_changes(both_clients, get_good_response): links_id = "index" major_version = BASE_URL_PREFIXES["major"][1:] # Remove prefixed `/` - query_url = f"/links?filter=id={links_id}&api_hint={major_version}" + query_url = f'/links?filter=id="{links_id}"&api_hint={major_version}' response = get_good_response(query_url, server=both_clients, return_json=False) @@ -62,7 +62,7 @@ def test_url_changes(both_clients, get_good_response): ) # Now to make sure the redirect would not happen when leaving out `api_hint` - query_url = f"/links?filter=id={links_id}" + query_url = f'/links?filter=id="{links_id}"' response = get_good_response(query_url, server=both_clients, return_json=False) diff --git a/tests/server/query_params/test_filter.py b/tests/server/query_params/test_filter.py index 285145dcb..e5070dbb8 100644 --- a/tests/server/query_params/test_filter.py +++ b/tests/server/query_params/test_filter.py @@ -9,7 +9,7 @@ def test_custom_field(check_response): def test_id(check_response): - request = "/structures?filter=id=mpf_2" + request = '/structures?filter=id="mpf_2"' expected_ids = ["mpf_2"] check_response(request, expected_ids) diff --git a/tests/server/routers/test_structures.py b/tests/server/routers/test_structures.py index dd6b9096b..a1a601ac2 100644 --- a/tests/server/routers/test_structures.py +++ b/tests/server/routers/test_structures.py @@ -111,7 +111,7 @@ def test_structures_endpoint_data(self): class TestMultiStructureWithSharedRelationships(RegularEndpointTests): """Tests for /structures for entries with shared relationships""" - request_str = "/structures?filter=id=mpf_1 OR id=mpf_2" + request_str = '/structures?filter=id="mpf_1" OR id="mpf_2"' response_cls = StructureResponseMany def test_structures_endpoint_data(self): @@ -126,7 +126,7 @@ def test_structures_endpoint_data(self): class TestMultiStructureWithRelationships(RegularEndpointTests): """Tests for /structures for mixed entries with and without relationships""" - request_str = "/structures?filter=id=mpf_1 OR id=mpf_23" + request_str = '/structures?filter=id="mpf_1" OR id="mpf_23"' response_cls = StructureResponseMany def test_structures_endpoint_data(self): @@ -145,7 +145,7 @@ class TestMultiStructureWithOverlappingRelationships(RegularEndpointTests): some of these relationships overlap between the entries, others don't. """ - request_str = "/structures?filter=id=mpf_1 OR id=mpf_3" + request_str = '/structures?filter=id="mpf_1" OR id="mpf_3"' response_cls = StructureResponseMany def test_structures_endpoint_data(self): From 123532618176d70b29ac091d65e8180854fdc9f9 Mon Sep 17 00:00:00 2001 From: Casper Welzel Andersen Date: Tue, 9 Mar 2021 11:46:20 +0100 Subject: [PATCH 5/6] Remove trailing slashes (/) from root_path --- optimade/server/config.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/optimade/server/config.py b/optimade/server/config.py index 7ed0f4f71..c5c5be7e7 100644 --- a/optimade/server/config.py +++ b/optimade/server/config.py @@ -146,6 +146,14 @@ def set_implementation_version(cls, v): res.update(v) return res + @validator("root_path", pre=False) + def remove_end_slashes(cls, value: str) -> str: + """Remove ending slashes from root_path""" + if isinstance(value, str): + while value.endswith("/"): + value = value[:-1] + return value + @root_validator(pre=True) def load_settings(cls, values): """ From 863c0bb812ec16114c51c967c33de07d51a34bd8 Mon Sep 17 00:00:00 2001 From: Casper Welzel Andersen Date: Tue, 9 Mar 2021 11:54:09 +0100 Subject: [PATCH 6/6] Ensure starting slash for root_path --- optimade/server/config.py | 3 +++ tests/test_config.json | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/optimade/server/config.py b/optimade/server/config.py index c5c5be7e7..3c20a7a3a 100644 --- a/optimade/server/config.py +++ b/optimade/server/config.py @@ -152,6 +152,9 @@ def remove_end_slashes(cls, value: str) -> str: if isinstance(value, str): while value.endswith("/"): value = value[:-1] + while value.startswith("/"): + value = value[1:] + value = f"/{value}" return value @root_validator(pre=True) diff --git a/tests/test_config.json b/tests/test_config.json index e18c4c9b9..7bc725eca 100644 --- a/tests/test_config.json +++ b/tests/test_config.json @@ -2,7 +2,7 @@ "debug": false, "default_db": "test_server", "base_url": "http://example.org", - "root_path": "/custom_root_path/", + "root_path": "custom_root_path/", "implementation": { "name": "Example implementation", "source_url": "https://github.com/Materials-Consortia/optimade-python-tools",