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

feat: add hook for schemathesis tests #513

Merged
merged 8 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,6 @@ dev-catalyst-voice-9f78f27c6bc5.json
# Specifically exclude it in the directory it appears, if its required.
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

#Hypothesis
.hypothesis
4 changes: 3 additions & 1 deletion catalyst-gateway/tests/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ package-schemathesis:
RUN apk add --no-cache gcc musl-dev
RUN python -m pip install schemathesis==3.27.1
RUN mkdir /results
COPY ./api_tests/api_tests/hooks.py .

VOLUME /results
ENTRYPOINT st run --checks all $openapi_spec \
ENTRYPOINT export SCHEMATHESIS_HOOKS=hooks \
&& st run --checks all $openapi_spec \
--workers=$workers \
--wait-for-schema=$wait_for_schema \
--max-response-time=$max_response_time \
Expand Down
3 changes: 2 additions & 1 deletion catalyst-gateway/tests/api_tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__
junit-report.xml
junit-report.xml
.hypothesis/
36 changes: 36 additions & 0 deletions catalyst-gateway/tests/api_tests/api_tests/hooks.py
kukkok3 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import copy
import schemathesis
import schemathesis.schemas
from typing import Any, Dict

@schemathesis.hook
def before_load_schema(
context: schemathesis.hooks.HookContext,
raw_schema: Dict[str, Any],
) -> None:
paths = dict(raw_schema["paths"])
for endpoint in paths.keys():
endpoint_data = dict(raw_schema["paths"][endpoint])

# Get first method data as reference
ref_info = next(iter(endpoint_data.values()))

# Create new method data using reference data, replacing 'responses' field
new_info = copy.deepcopy(ref_info)
new_info["responses"] = {"405": {"description": "method not allowed"}}

# Update endpoint if a method is not declared
if "get" not in endpoint_data:
endpoint_data.update([("get", new_info),])
if "post" not in endpoint_data:
endpoint_data.update([("post", new_info),])
if "put" not in endpoint_data:
endpoint_data.update([("put", new_info),])
if "patch" not in endpoint_data:
endpoint_data.update([("patch", new_info),])
if "delete" not in endpoint_data:
endpoint_data.update([("patch", new_info),])

# Update endpoint
raw_schema["paths"][endpoint] = endpoint_data

Loading