Skip to content

Commit

Permalink
Fix CheckWronglyVersionedBaseUrls middleware (for landing pages) (#752)
Browse files Browse the repository at this point in the history
Ensure the version part is properly checked.
Don't use for-else for middleware.
Add test for bare versioned base URLs.

Ensure `version` is correctly set for test client.

Co-authored-by: Matthew Evans <[email protected]>
  • Loading branch information
CasperWA and ml-evs authored Mar 25, 2021
1 parent 0675ebb commit 9079ec1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
11 changes: 4 additions & 7 deletions optimade/server/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,12 @@ def check_url(url: StarletteURL):
"""
base_url = get_base_url(url)
optimade_path = f"{url.scheme}://{url.netloc}{url.path}"[len(base_url) :]
if re.match(r"^/v[0-9]+", optimade_path):
for version_prefix in BASE_URL_PREFIXES.values():
if optimade_path.startswith(f"{version_prefix}/"):
break
else:
version_prefix = re.findall(r"(/v[0-9]+(\.[0-9]+){0,2})", optimade_path)
match = re.match(r"^(?P<version>/v[0-9]+(\.[0-9]+){0,2}).*", optimade_path)
if match is not None:
if match.group("version") not in BASE_URL_PREFIXES.values():
raise VersionNotSupported(
detail=(
f"The parsed versioned base URL {version_prefix[0][0]!r} from "
f"The parsed versioned base URL {match.group('version')!r} from "
f"{url} is not supported by this implementation. "
f"Supported versioned base URLs are: {', '.join(BASE_URL_PREFIXES.values())}"
)
Expand Down
15 changes: 13 additions & 2 deletions tests/server/middleware/test_versioned_url.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Test CheckWronglyVersionedBaseUrls middleware"""
import urllib
import urllib.parse

import pytest

Expand Down Expand Up @@ -63,7 +63,7 @@ def test_multiple_versions_in_path(both_clients):
urllib.parse.urlparse(url)
)

# Test also that the a non-valid OPTIMADE version raises
# Test also that a non-valid OPTIMADE version raises
url = f"{CONFIG.base_url}/v0/info"
with pytest.raises(VersionNotSupported):
CheckWronglyVersionedBaseUrls(both_clients.app).check_url(
Expand All @@ -74,3 +74,14 @@ def test_multiple_versions_in_path(both_clients):
CONFIG.base_url = org_base_url
else:
CONFIG.base_url = None


def test_versioned_base_urls(both_clients):
"""Test the middleware does not wrongly catch requests to versioned base URLs"""
from optimade.server.config import CONFIG
from optimade.server.routers.utils import BASE_URL_PREFIXES

for request in BASE_URL_PREFIXES.values():
CheckWronglyVersionedBaseUrls(both_clients.app).check_url(
urllib.parse.urlparse(f"{CONFIG.base_url}{request}")
)
2 changes: 2 additions & 0 deletions tests/server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def __init__(
if version:
if not version.startswith("v") and not version.startswith("/v"):
version = f"/v{version}"
if version.startswith("v"):
version = f"/{version}"
if re.match(r"/v[0-9](.[0-9]){0,2}", version) is None:
warnings.warn(
f"Invalid version passed to client: {version!r}. "
Expand Down

0 comments on commit 9079ec1

Please sign in to comment.