Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CheckWronglyVersionedBaseUrls middleware (for landing pages) #752

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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