Skip to content
Snippets Groups Projects
Commit e75739d2 authored by DAVID Axel's avatar DAVID Axel
Browse files

fix: :bug: Select routes only for the good api version :bug:

parent bdb43025
Branches
No related tags found
No related merge requests found
...@@ -20,11 +20,10 @@ from typing import final, Final ...@@ -20,11 +20,10 @@ from typing import final, Final
from blacksheep.server.openapi.ui import ReDocUIProvider from blacksheep.server.openapi.ui import ReDocUIProvider
from blacksheep.server.openapi.v3 import OpenAPIHandler from blacksheep.server.openapi.v3 import OpenAPIHandler
from openapidocs.common import Format from openapidocs.common import Format
from openapidocs.v3 import Contact, ExternalDocs, Info, License, OpenAPI, Tag from openapidocs.v3 import Contact, ExternalDocs, Info, License, OpenAPI, Tag
from vigenere_api.version import Version
from vigenere_api.version import Version
from .errors import VersionTypeError from .errors import VersionTypeError
from .open_api_route_filter import get_route_filter from .open_api_route_filter import get_route_filter
...@@ -81,7 +80,10 @@ class VigenereAPIOpenAPIHandler(OpenAPIHandler): ...@@ -81,7 +80,10 @@ class VigenereAPIOpenAPIHandler(OpenAPIHandler):
) )
self.ui_providers.append(ReDocUIProvider(ui_path=f"{api_route}/redocs")) self.ui_providers.append(ReDocUIProvider(ui_path=f"{api_route}/redocs"))
self.include = get_route_filter((api_route, json_spec_path, yaml_spec_path)) self.include = get_route_filter(
(api_route, json_spec_path, yaml_spec_path),
version,
)
@property @property
def version(self) -> Version: def version(self) -> Version:
......
...@@ -20,10 +20,19 @@ from collections.abc import Callable, Collection ...@@ -20,10 +20,19 @@ from collections.abc import Callable, Collection
from blacksheep import Route from blacksheep import Route
from .errors import ExcludedPathsTypeError, ExcludedPathTypeError, PathTypeError from vigenere_api.version import Version
from .errors import (
ExcludedPathsTypeError,
def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]: ExcludedPathTypeError,
PathTypeError,
VersionTypeError,
)
def get_route_filter(
excluded: Collection[str],
api_version: Version,
) -> Callable[[str, Route], bool]:
""" """
Get the route filter. Get the route filter.
...@@ -31,6 +40,8 @@ def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]: ...@@ -31,6 +40,8 @@ def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]:
---------- ----------
excluded : Collection[str] excluded : Collection[str]
All routes that should not be exposed. All routes that should not be exposed.
api_version : Version
The API version.
Raises Raises
------ ------
...@@ -38,6 +49,8 @@ def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]: ...@@ -38,6 +49,8 @@ def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]:
Thrown if 'excluded' is not a Collection. Thrown if 'excluded' is not a Collection.
ExcludedPathTypeError ExcludedPathTypeError
Thrown if 'excluded' does not contain only str. Thrown if 'excluded' does not contain only str.
VersionTypeError
Thrown if 'api_version' is not a Version.
Returns Returns
------- -------
...@@ -51,6 +64,9 @@ def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]: ...@@ -51,6 +64,9 @@ def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]:
if not isinstance(excluded_path, str): if not isinstance(excluded_path, str):
raise ExcludedPathTypeError(excluded_path, excluded) raise ExcludedPathTypeError(excluded_path, excluded)
if not isinstance(api_version, Version):
raise VersionTypeError(api_version)
def _route_filter(path: str, _: Route) -> bool: def _route_filter(path: str, _: Route) -> bool:
""" """
Exclude routes that should not be exposed. Exclude routes that should not be exposed.
...@@ -80,6 +96,6 @@ def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]: ...@@ -80,6 +96,6 @@ def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]:
if not isinstance(path, str): if not isinstance(path, str):
raise PathTypeError(path) raise PathTypeError(path)
return path not in excluded return path not in excluded and path.startswith(f"/api/v{api_version.major}")
return _route_filter return _route_filter
...@@ -22,13 +22,16 @@ from vigenere_api.api.helpers.errors import ( ...@@ -22,13 +22,16 @@ from vigenere_api.api.helpers.errors import (
ExcludedPathsTypeError, ExcludedPathsTypeError,
ExcludedPathTypeError, ExcludedPathTypeError,
PathTypeError, PathTypeError,
VersionTypeError,
) )
from vigenere_api.api.helpers.open_api_route_filter import get_route_filter from vigenere_api.api.helpers.open_api_route_filter import get_route_filter
from vigenere_api.version import Version
def test_get_filter() -> None: def test_get_filter() -> None:
excluded = [] excluded = []
filter = get_route_filter(excluded) version = Version(major=1, minor=1, patch=10)
filter = get_route_filter(excluded, version)
assert callable(filter) assert callable(filter)
s = signature(filter) s = signature(filter)
...@@ -45,31 +48,42 @@ def test_get_filter() -> None: ...@@ -45,31 +48,42 @@ def test_get_filter() -> None:
@pytest.mark.raises(exception=ExcludedPathsTypeError) @pytest.mark.raises(exception=ExcludedPathsTypeError)
def test_bad_type_excluded() -> None: def test_bad_type_excluded() -> None:
excluded = 10 excluded = 10
_ignored = get_route_filter(excluded) version = Version(major=1, minor=1, patch=10)
_ignored = get_route_filter(excluded, version)
@pytest.mark.raises(exception=ExcludedPathTypeError) @pytest.mark.raises(exception=ExcludedPathTypeError)
def test_bad_type_path_in_excluded() -> None: def test_bad_type_path_in_excluded() -> None:
excluded = [b"test"] excluded = [b"test"]
_ignored = get_route_filter(excluded) version = Version(major=1, minor=1, patch=10)
_ignored = get_route_filter(excluded, version)
@pytest.mark.raises(exception=VersionTypeError)
def test_bad_type_version() -> None:
_ignored = get_route_filter((), "1.0.0")
@pytest.mark.raises(exception=PathTypeError) @pytest.mark.raises(exception=PathTypeError)
def test_filter_bad_type_path() -> None: def test_filter_bad_type_path() -> None:
route_filter = get_route_filter(["/api"]) version = Version(major=1, minor=1, patch=10)
route_filter = get_route_filter(["/api"], version)
_ignored = route_filter(b"/http", Route("http", {})) _ignored = route_filter(b"/http", Route("http", {}))
def test_filter_bad_type_route() -> None: def test_filter_bad_type_route() -> None:
route_filter = get_route_filter(["/api"]) version = Version(major=1, minor=1, patch=10)
assert route_filter("/http", {}) route_filter = get_route_filter(["/api"], version)
assert route_filter(f"/api/v{version.major}/test", {})
def test_filter_route_excluded() -> None: def test_filter_route_excluded() -> None:
route_filter = get_route_filter(["/api"]) version = Version(major=1, minor=1, patch=10)
route_filter = get_route_filter(["/api"], version)
assert not route_filter("/api", Route("http", {})) assert not route_filter("/api", Route("http", {}))
def test_filter_route_not_excluded() -> None: def test_filter_route_not_excluded() -> None:
route_filter = get_route_filter(["/api"]) version = Version(major=1, minor=1, patch=10)
assert route_filter("/http", Route("http", {})) route_filter = get_route_filter(["/api"], version)
assert route_filter(f"/api/v{version.major}/test", Route("http", {}))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment