Skip to content

Commit

Permalink
feat: introducing path prefix support for SchemaTester
Browse files Browse the repository at this point in the history
  • Loading branch information
Matias Cardenas committed Mar 12, 2024
1 parent b371571 commit 3c0f59d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
16 changes: 13 additions & 3 deletions openapi_tester/schema_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,19 @@ def __init__(
schema_file_path: str | None = None,
validators: list[Callable[[dict, Any], str | None]] | None = None,
field_key_map: dict[str, str] | None = None,
path_prefix: str | None = None,
) -> None:
"""
Iterates through an OpenAPI schema object and API response to check that they match at every level.
:param case_tester: An optional callable that validates schema and response keys
:param ignore_case: An optional list of keys for the case_tester to ignore
:schema_file_path: The file path to an OpenAPI yaml or json file. Only passed when using a static schema loader
:param path_prefix: An optional string to prefix the path of the schema file
:raises: openapi_tester.exceptions.DocumentationError or ImproperlyConfigured
"""
self.case_tester = case_tester
self._path_prefix = path_prefix
self.ignore_case = ignore_case or []
self.validators = validators or []

Expand Down Expand Up @@ -132,6 +135,14 @@ def get_schema_type(schema: dict[str, str]) -> str | None:
return "object"
return None

def get_paths_object(self) -> dict[str, Any]:
schema = self.loader.get_schema()
paths_object = self.get_key_value(schema, "paths")
if self._path_prefix:
paths_object = {f"{self._path_prefix}{key}": value for key, value in paths_object.items()}

return paths_object

def get_response_schema_section(self, response_handler: ResponseHandler) -> dict[str, Any]:
"""
Fetches the response section of a schema, wrt. the route, method, status code, and schema version.
Expand All @@ -146,7 +157,7 @@ def get_response_schema_section(self, response_handler: ResponseHandler) -> dict
parameterized_path, _ = self.loader.resolve_path(
response.request["PATH_INFO"], method=response_method # type: ignore
)
paths_object = self.get_key_value(schema, "paths")
paths_object = self.get_paths_object()

route_object = self.get_key_value(
paths_object,
Expand Down Expand Up @@ -212,11 +223,10 @@ def get_request_body_schema_section(self, request: dict[str, Any]) -> dict[str,
:param response: DRF Request Instance
:return dict
"""
schema = self.loader.get_schema()
request_method = request["REQUEST_METHOD"].lower()

parameterized_path, _ = self.loader.resolve_path(request["PATH_INFO"], method=request_method)
paths_object = self.get_key_value(schema, "paths")
paths_object = self.get_paths_object()

route_object = self.get_key_value(
paths_object,
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ disable = """
import-outside-toplevel,
fixme,
line-too-long,
too-many-arguments,
"""
enable = "useless-suppression"

Expand Down
14 changes: 14 additions & 0 deletions tests/test_schema_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,17 @@ def uuid_1_validator(schema_section: dict, data: Any) -> str | None: # pragma:
tester_with_custom_validator.test_schema_section(
uid1_schema, uid4, test_config=OpenAPITestConfig(validators=[uuid_1_validator])
)


def test_get_paths_object():
schema = tester.loader.get_schema()
paths = tester.get_paths_object()
assert paths == schema["paths"]


def test_get_paths_object_path_prefix(pets_api_schema: Path):
path_prefix = "/path/prefix"
schema_tester = SchemaTester(schema_file_path=str(pets_api_schema), path_prefix=path_prefix)
paths_object = schema_tester.get_paths_object()

assert list(paths_object.keys()) == [f"{path_prefix}/api/pets", f"{path_prefix}/api/pets/{{id}}"]

0 comments on commit 3c0f59d

Please sign in to comment.