schemathesis 3.15.4__py3-none-any.whl → 4.4.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- schemathesis/__init__.py +53 -25
- schemathesis/auths.py +507 -0
- schemathesis/checks.py +190 -25
- schemathesis/cli/__init__.py +27 -1219
- schemathesis/cli/__main__.py +4 -0
- schemathesis/cli/commands/__init__.py +133 -0
- schemathesis/cli/commands/data.py +10 -0
- schemathesis/cli/commands/run/__init__.py +602 -0
- schemathesis/cli/commands/run/context.py +228 -0
- schemathesis/cli/commands/run/events.py +60 -0
- schemathesis/cli/commands/run/executor.py +157 -0
- schemathesis/cli/commands/run/filters.py +53 -0
- schemathesis/cli/commands/run/handlers/__init__.py +46 -0
- schemathesis/cli/commands/run/handlers/base.py +45 -0
- schemathesis/cli/commands/run/handlers/cassettes.py +464 -0
- schemathesis/cli/commands/run/handlers/junitxml.py +60 -0
- schemathesis/cli/commands/run/handlers/output.py +1750 -0
- schemathesis/cli/commands/run/loaders.py +118 -0
- schemathesis/cli/commands/run/validation.py +256 -0
- schemathesis/cli/constants.py +5 -0
- schemathesis/cli/core.py +19 -0
- schemathesis/cli/ext/fs.py +16 -0
- schemathesis/cli/ext/groups.py +203 -0
- schemathesis/cli/ext/options.py +81 -0
- schemathesis/config/__init__.py +202 -0
- schemathesis/config/_auth.py +51 -0
- schemathesis/config/_checks.py +268 -0
- schemathesis/config/_diff_base.py +101 -0
- schemathesis/config/_env.py +21 -0
- schemathesis/config/_error.py +163 -0
- schemathesis/config/_generation.py +157 -0
- schemathesis/config/_health_check.py +24 -0
- schemathesis/config/_operations.py +335 -0
- schemathesis/config/_output.py +171 -0
- schemathesis/config/_parameters.py +19 -0
- schemathesis/config/_phases.py +253 -0
- schemathesis/config/_projects.py +543 -0
- schemathesis/config/_rate_limit.py +17 -0
- schemathesis/config/_report.py +120 -0
- schemathesis/config/_validator.py +9 -0
- schemathesis/config/_warnings.py +89 -0
- schemathesis/config/schema.json +975 -0
- schemathesis/core/__init__.py +72 -0
- schemathesis/core/adapter.py +34 -0
- schemathesis/core/compat.py +32 -0
- schemathesis/core/control.py +2 -0
- schemathesis/core/curl.py +100 -0
- schemathesis/core/deserialization.py +210 -0
- schemathesis/core/errors.py +588 -0
- schemathesis/core/failures.py +316 -0
- schemathesis/core/fs.py +19 -0
- schemathesis/core/hooks.py +20 -0
- schemathesis/core/jsonschema/__init__.py +13 -0
- schemathesis/core/jsonschema/bundler.py +183 -0
- schemathesis/core/jsonschema/keywords.py +40 -0
- schemathesis/core/jsonschema/references.py +222 -0
- schemathesis/core/jsonschema/types.py +41 -0
- schemathesis/core/lazy_import.py +15 -0
- schemathesis/core/loaders.py +107 -0
- schemathesis/core/marks.py +66 -0
- schemathesis/core/media_types.py +79 -0
- schemathesis/core/output/__init__.py +46 -0
- schemathesis/core/output/sanitization.py +54 -0
- schemathesis/core/parameters.py +45 -0
- schemathesis/core/rate_limit.py +60 -0
- schemathesis/core/registries.py +34 -0
- schemathesis/core/result.py +27 -0
- schemathesis/core/schema_analysis.py +17 -0
- schemathesis/core/shell.py +203 -0
- schemathesis/core/transforms.py +144 -0
- schemathesis/core/transport.py +223 -0
- schemathesis/core/validation.py +73 -0
- schemathesis/core/version.py +7 -0
- schemathesis/engine/__init__.py +28 -0
- schemathesis/engine/context.py +152 -0
- schemathesis/engine/control.py +44 -0
- schemathesis/engine/core.py +201 -0
- schemathesis/engine/errors.py +446 -0
- schemathesis/engine/events.py +284 -0
- schemathesis/engine/observations.py +42 -0
- schemathesis/engine/phases/__init__.py +108 -0
- schemathesis/engine/phases/analysis.py +28 -0
- schemathesis/engine/phases/probes.py +172 -0
- schemathesis/engine/phases/stateful/__init__.py +68 -0
- schemathesis/engine/phases/stateful/_executor.py +364 -0
- schemathesis/engine/phases/stateful/context.py +85 -0
- schemathesis/engine/phases/unit/__init__.py +220 -0
- schemathesis/engine/phases/unit/_executor.py +459 -0
- schemathesis/engine/phases/unit/_pool.py +82 -0
- schemathesis/engine/recorder.py +254 -0
- schemathesis/errors.py +47 -0
- schemathesis/filters.py +395 -0
- schemathesis/generation/__init__.py +25 -0
- schemathesis/generation/case.py +478 -0
- schemathesis/generation/coverage.py +1528 -0
- schemathesis/generation/hypothesis/__init__.py +121 -0
- schemathesis/generation/hypothesis/builder.py +992 -0
- schemathesis/generation/hypothesis/examples.py +56 -0
- schemathesis/generation/hypothesis/given.py +66 -0
- schemathesis/generation/hypothesis/reporting.py +285 -0
- schemathesis/generation/meta.py +227 -0
- schemathesis/generation/metrics.py +93 -0
- schemathesis/generation/modes.py +20 -0
- schemathesis/generation/overrides.py +127 -0
- schemathesis/generation/stateful/__init__.py +37 -0
- schemathesis/generation/stateful/state_machine.py +294 -0
- schemathesis/graphql/__init__.py +15 -0
- schemathesis/graphql/checks.py +109 -0
- schemathesis/graphql/loaders.py +285 -0
- schemathesis/hooks.py +270 -91
- schemathesis/openapi/__init__.py +13 -0
- schemathesis/openapi/checks.py +467 -0
- schemathesis/openapi/generation/__init__.py +0 -0
- schemathesis/openapi/generation/filters.py +72 -0
- schemathesis/openapi/loaders.py +315 -0
- schemathesis/pytest/__init__.py +5 -0
- schemathesis/pytest/control_flow.py +7 -0
- schemathesis/pytest/lazy.py +341 -0
- schemathesis/pytest/loaders.py +36 -0
- schemathesis/pytest/plugin.py +357 -0
- schemathesis/python/__init__.py +0 -0
- schemathesis/python/asgi.py +12 -0
- schemathesis/python/wsgi.py +12 -0
- schemathesis/schemas.py +682 -257
- schemathesis/specs/graphql/__init__.py +0 -1
- schemathesis/specs/graphql/nodes.py +26 -2
- schemathesis/specs/graphql/scalars.py +77 -12
- schemathesis/specs/graphql/schemas.py +367 -148
- schemathesis/specs/graphql/validation.py +33 -0
- schemathesis/specs/openapi/__init__.py +9 -1
- schemathesis/specs/openapi/_hypothesis.py +555 -318
- schemathesis/specs/openapi/adapter/__init__.py +10 -0
- schemathesis/specs/openapi/adapter/parameters.py +729 -0
- schemathesis/specs/openapi/adapter/protocol.py +59 -0
- schemathesis/specs/openapi/adapter/references.py +19 -0
- schemathesis/specs/openapi/adapter/responses.py +368 -0
- schemathesis/specs/openapi/adapter/security.py +144 -0
- schemathesis/specs/openapi/adapter/v2.py +30 -0
- schemathesis/specs/openapi/adapter/v3_0.py +30 -0
- schemathesis/specs/openapi/adapter/v3_1.py +30 -0
- schemathesis/specs/openapi/analysis.py +96 -0
- schemathesis/specs/openapi/checks.py +748 -82
- schemathesis/specs/openapi/converter.py +176 -37
- schemathesis/specs/openapi/definitions.py +599 -4
- schemathesis/specs/openapi/examples.py +581 -165
- schemathesis/specs/openapi/expressions/__init__.py +52 -5
- schemathesis/specs/openapi/expressions/extractors.py +25 -0
- schemathesis/specs/openapi/expressions/lexer.py +34 -31
- schemathesis/specs/openapi/expressions/nodes.py +97 -46
- schemathesis/specs/openapi/expressions/parser.py +35 -13
- schemathesis/specs/openapi/formats.py +122 -0
- schemathesis/specs/openapi/media_types.py +75 -0
- schemathesis/specs/openapi/negative/__init__.py +93 -73
- schemathesis/specs/openapi/negative/mutations.py +294 -103
- schemathesis/specs/openapi/negative/utils.py +0 -9
- schemathesis/specs/openapi/patterns.py +458 -0
- schemathesis/specs/openapi/references.py +60 -81
- schemathesis/specs/openapi/schemas.py +647 -666
- schemathesis/specs/openapi/serialization.py +53 -30
- schemathesis/specs/openapi/stateful/__init__.py +403 -68
- schemathesis/specs/openapi/stateful/control.py +87 -0
- schemathesis/specs/openapi/stateful/dependencies/__init__.py +232 -0
- schemathesis/specs/openapi/stateful/dependencies/inputs.py +428 -0
- schemathesis/specs/openapi/stateful/dependencies/models.py +341 -0
- schemathesis/specs/openapi/stateful/dependencies/naming.py +491 -0
- schemathesis/specs/openapi/stateful/dependencies/outputs.py +34 -0
- schemathesis/specs/openapi/stateful/dependencies/resources.py +339 -0
- schemathesis/specs/openapi/stateful/dependencies/schemas.py +447 -0
- schemathesis/specs/openapi/stateful/inference.py +254 -0
- schemathesis/specs/openapi/stateful/links.py +219 -78
- schemathesis/specs/openapi/types/__init__.py +3 -0
- schemathesis/specs/openapi/types/common.py +23 -0
- schemathesis/specs/openapi/types/v2.py +129 -0
- schemathesis/specs/openapi/types/v3.py +134 -0
- schemathesis/specs/openapi/utils.py +7 -6
- schemathesis/specs/openapi/warnings.py +75 -0
- schemathesis/transport/__init__.py +224 -0
- schemathesis/transport/asgi.py +26 -0
- schemathesis/transport/prepare.py +126 -0
- schemathesis/transport/requests.py +278 -0
- schemathesis/transport/serialization.py +329 -0
- schemathesis/transport/wsgi.py +175 -0
- schemathesis-4.4.2.dist-info/METADATA +213 -0
- schemathesis-4.4.2.dist-info/RECORD +192 -0
- {schemathesis-3.15.4.dist-info → schemathesis-4.4.2.dist-info}/WHEEL +1 -1
- schemathesis-4.4.2.dist-info/entry_points.txt +6 -0
- {schemathesis-3.15.4.dist-info → schemathesis-4.4.2.dist-info/licenses}/LICENSE +1 -1
- schemathesis/_compat.py +0 -57
- schemathesis/_hypothesis.py +0 -123
- schemathesis/auth.py +0 -214
- schemathesis/cli/callbacks.py +0 -240
- schemathesis/cli/cassettes.py +0 -351
- schemathesis/cli/context.py +0 -38
- schemathesis/cli/debug.py +0 -21
- schemathesis/cli/handlers.py +0 -11
- schemathesis/cli/junitxml.py +0 -41
- schemathesis/cli/options.py +0 -70
- schemathesis/cli/output/__init__.py +0 -1
- schemathesis/cli/output/default.py +0 -521
- schemathesis/cli/output/short.py +0 -40
- schemathesis/constants.py +0 -88
- schemathesis/exceptions.py +0 -257
- schemathesis/extra/_aiohttp.py +0 -27
- schemathesis/extra/_flask.py +0 -10
- schemathesis/extra/_server.py +0 -16
- schemathesis/extra/pytest_plugin.py +0 -251
- schemathesis/failures.py +0 -145
- schemathesis/fixups/__init__.py +0 -29
- schemathesis/fixups/fast_api.py +0 -30
- schemathesis/graphql.py +0 -5
- schemathesis/internal.py +0 -6
- schemathesis/lazy.py +0 -301
- schemathesis/models.py +0 -1113
- schemathesis/parameters.py +0 -91
- schemathesis/runner/__init__.py +0 -470
- schemathesis/runner/events.py +0 -242
- schemathesis/runner/impl/__init__.py +0 -3
- schemathesis/runner/impl/core.py +0 -791
- schemathesis/runner/impl/solo.py +0 -85
- schemathesis/runner/impl/threadpool.py +0 -367
- schemathesis/runner/serialization.py +0 -206
- schemathesis/serializers.py +0 -253
- schemathesis/service/__init__.py +0 -18
- schemathesis/service/auth.py +0 -10
- schemathesis/service/client.py +0 -62
- schemathesis/service/constants.py +0 -25
- schemathesis/service/events.py +0 -39
- schemathesis/service/handler.py +0 -46
- schemathesis/service/hosts.py +0 -74
- schemathesis/service/metadata.py +0 -42
- schemathesis/service/models.py +0 -21
- schemathesis/service/serialization.py +0 -184
- schemathesis/service/worker.py +0 -39
- schemathesis/specs/graphql/loaders.py +0 -215
- schemathesis/specs/openapi/constants.py +0 -7
- schemathesis/specs/openapi/expressions/context.py +0 -12
- schemathesis/specs/openapi/expressions/pointers.py +0 -29
- schemathesis/specs/openapi/filters.py +0 -44
- schemathesis/specs/openapi/links.py +0 -303
- schemathesis/specs/openapi/loaders.py +0 -453
- schemathesis/specs/openapi/parameters.py +0 -430
- schemathesis/specs/openapi/security.py +0 -129
- schemathesis/specs/openapi/validation.py +0 -24
- schemathesis/stateful.py +0 -358
- schemathesis/targets.py +0 -32
- schemathesis/types.py +0 -38
- schemathesis/utils.py +0 -475
- schemathesis-3.15.4.dist-info/METADATA +0 -202
- schemathesis-3.15.4.dist-info/RECORD +0 -99
- schemathesis-3.15.4.dist-info/entry_points.txt +0 -7
- /schemathesis/{extra → cli/ext}/__init__.py +0 -0
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
"""Open API links support.
|
|
2
|
-
|
|
3
|
-
Based on https://swagger.io/docs/specification/links/
|
|
4
|
-
"""
|
|
5
|
-
from copy import deepcopy
|
|
6
|
-
from difflib import get_close_matches
|
|
7
|
-
from typing import Any, Dict, Generator, List, NoReturn, Optional, Sequence, Tuple, Union
|
|
8
|
-
|
|
9
|
-
import attr
|
|
10
|
-
|
|
11
|
-
from ...models import APIOperation, Case
|
|
12
|
-
from ...parameters import ParameterSet
|
|
13
|
-
from ...stateful import Direction, ParsedData, StatefulTest
|
|
14
|
-
from ...types import NotSet
|
|
15
|
-
from ...utils import NOT_SET, GenericResponse
|
|
16
|
-
from . import expressions
|
|
17
|
-
from .constants import LOCATION_TO_CONTAINER
|
|
18
|
-
from .parameters import OpenAPI20Body, OpenAPI30Body, OpenAPIParameter
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
@attr.s(slots=True, repr=False) # pragma: no mutate
|
|
22
|
-
class Link(StatefulTest):
|
|
23
|
-
operation: APIOperation = attr.ib() # pragma: no mutate
|
|
24
|
-
parameters: Dict[str, Any] = attr.ib() # pragma: no mutate
|
|
25
|
-
request_body: Any = attr.ib(default=NOT_SET) # pragma: no mutate
|
|
26
|
-
|
|
27
|
-
@request_body.validator
|
|
28
|
-
def is_defined(self, attribute: attr.Attribute, value: Any) -> None:
|
|
29
|
-
if value is not NOT_SET and not self.operation.body:
|
|
30
|
-
# Link defines `requestBody` for a parameter that does not accept one
|
|
31
|
-
raise ValueError(
|
|
32
|
-
f"Request body is not defined in API operation {self.operation.method.upper()} {self.operation.path}"
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
@classmethod
|
|
36
|
-
def from_definition(
|
|
37
|
-
cls, name: str, definition: Dict[str, Dict[str, Any]], source_operation: APIOperation
|
|
38
|
-
) -> "Link":
|
|
39
|
-
# Links can be behind a reference
|
|
40
|
-
_, definition = source_operation.schema.resolver.resolve_in_scope( # type: ignore
|
|
41
|
-
definition, source_operation.definition.scope
|
|
42
|
-
)
|
|
43
|
-
if "operationId" in definition:
|
|
44
|
-
# source_operation.schema is `BaseOpenAPISchema` and has this method
|
|
45
|
-
operation = source_operation.schema.get_operation_by_id(definition["operationId"]) # type: ignore
|
|
46
|
-
else:
|
|
47
|
-
operation = source_operation.schema.get_operation_by_reference(definition["operationRef"]) # type: ignore
|
|
48
|
-
return cls(
|
|
49
|
-
# Pylint can't detect that the API operation is always defined at this point
|
|
50
|
-
# E.g. if there is no matching operation or no operations at all, then a ValueError will be risen
|
|
51
|
-
name=name,
|
|
52
|
-
operation=operation, # pylint: disable=undefined-loop-variable
|
|
53
|
-
parameters=definition.get("parameters", {}),
|
|
54
|
-
request_body=definition.get("requestBody", NOT_SET), # `None` might be a valid value - `null`
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
def parse(self, case: Case, response: GenericResponse) -> ParsedData:
|
|
58
|
-
"""Parse data into a structure expected by links definition."""
|
|
59
|
-
context = expressions.ExpressionContext(case=case, response=response)
|
|
60
|
-
parameters = {
|
|
61
|
-
parameter: expressions.evaluate(expression, context) for parameter, expression in self.parameters.items()
|
|
62
|
-
}
|
|
63
|
-
return ParsedData(
|
|
64
|
-
parameters=parameters,
|
|
65
|
-
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#link-object
|
|
66
|
-
# > A literal value or {expression} to use as a request body when calling the target operation.
|
|
67
|
-
# In this case all literals will be passed as is, and expressions will be evaluated
|
|
68
|
-
body=expressions.evaluate(self.request_body, context),
|
|
69
|
-
)
|
|
70
|
-
|
|
71
|
-
def make_operation(self, collected: List[ParsedData]) -> APIOperation:
|
|
72
|
-
"""Create a modified version of the original API operation with additional data merged in."""
|
|
73
|
-
# We split the gathered data among all locations & store the original parameter
|
|
74
|
-
containers = {
|
|
75
|
-
location: {
|
|
76
|
-
parameter.name: {"options": [], "parameter": parameter}
|
|
77
|
-
for parameter in getattr(self.operation, container_name)
|
|
78
|
-
}
|
|
79
|
-
for location, container_name in LOCATION_TO_CONTAINER.items()
|
|
80
|
-
}
|
|
81
|
-
# There might be duplicates in the data
|
|
82
|
-
for item in set(collected):
|
|
83
|
-
for name, value in item.parameters.items():
|
|
84
|
-
container = self._get_container_by_parameter_name(name, containers)
|
|
85
|
-
container.append(value)
|
|
86
|
-
if "body" in containers["body"] and item.body is not NOT_SET:
|
|
87
|
-
containers["body"]["body"]["options"].append(item.body)
|
|
88
|
-
# These are the final `path_parameters`, `query`, and other API operation components
|
|
89
|
-
components: Dict[str, ParameterSet] = {
|
|
90
|
-
container_name: getattr(self.operation, container_name).__class__()
|
|
91
|
-
for location, container_name in LOCATION_TO_CONTAINER.items()
|
|
92
|
-
}
|
|
93
|
-
# Here are all components that are filled with parameters
|
|
94
|
-
for location, parameters in containers.items():
|
|
95
|
-
for name, parameter_data in parameters.items():
|
|
96
|
-
parameter = parameter_data["parameter"]
|
|
97
|
-
if parameter_data["options"]:
|
|
98
|
-
definition = deepcopy(parameter.definition)
|
|
99
|
-
if "schema" in definition:
|
|
100
|
-
# The actual schema doesn't matter since we have a list of allowed values
|
|
101
|
-
definition["schema"] = {"enum": parameter_data["options"]}
|
|
102
|
-
else:
|
|
103
|
-
# Other schema-related keywords will be ignored later, during the canonicalisation step
|
|
104
|
-
# inside `hypothesis-jsonschema`
|
|
105
|
-
definition["enum"] = parameter_data["options"]
|
|
106
|
-
new_parameter: OpenAPIParameter
|
|
107
|
-
if isinstance(parameter, OpenAPI30Body):
|
|
108
|
-
new_parameter = parameter.__class__(
|
|
109
|
-
definition, media_type=parameter.media_type, required=parameter.required
|
|
110
|
-
)
|
|
111
|
-
elif isinstance(parameter, OpenAPI20Body):
|
|
112
|
-
new_parameter = parameter.__class__(definition, media_type=parameter.media_type)
|
|
113
|
-
else:
|
|
114
|
-
new_parameter = parameter.__class__(definition)
|
|
115
|
-
components[LOCATION_TO_CONTAINER[location]].add(new_parameter)
|
|
116
|
-
else:
|
|
117
|
-
# No options were gathered for this parameter - use the original one
|
|
118
|
-
components[LOCATION_TO_CONTAINER[location]].add(parameter)
|
|
119
|
-
return self.operation.clone(**components)
|
|
120
|
-
|
|
121
|
-
def _get_container_by_parameter_name(self, full_name: str, templates: Dict[str, Dict[str, Dict[str, Any]]]) -> List:
|
|
122
|
-
"""Detect in what request part the parameters is defined."""
|
|
123
|
-
location: Optional[str]
|
|
124
|
-
try:
|
|
125
|
-
# The parameter name is prefixed with its location. Example: `path.id`
|
|
126
|
-
location, name = full_name.split(".")
|
|
127
|
-
except ValueError:
|
|
128
|
-
location, name = None, full_name
|
|
129
|
-
if location:
|
|
130
|
-
try:
|
|
131
|
-
parameters = templates[location]
|
|
132
|
-
except KeyError:
|
|
133
|
-
self._unknown_parameter(full_name)
|
|
134
|
-
else:
|
|
135
|
-
for parameters in templates.values():
|
|
136
|
-
if name in parameters:
|
|
137
|
-
break
|
|
138
|
-
else:
|
|
139
|
-
self._unknown_parameter(full_name)
|
|
140
|
-
if not parameters:
|
|
141
|
-
self._unknown_parameter(full_name)
|
|
142
|
-
return parameters[name]["options"]
|
|
143
|
-
|
|
144
|
-
def _unknown_parameter(self, name: str) -> NoReturn:
|
|
145
|
-
raise ValueError(
|
|
146
|
-
f"Parameter `{name}` is not defined in API operation {self.operation.method.upper()} {self.operation.path}"
|
|
147
|
-
)
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
def get_links(response: GenericResponse, operation: APIOperation, field: str) -> Sequence[Link]:
|
|
151
|
-
"""Get `x-links` / `links` definitions from the schema."""
|
|
152
|
-
responses = operation.definition.resolved["responses"]
|
|
153
|
-
if str(response.status_code) in responses:
|
|
154
|
-
response_definition = responses[str(response.status_code)]
|
|
155
|
-
elif response.status_code in responses:
|
|
156
|
-
response_definition = responses[response.status_code]
|
|
157
|
-
else:
|
|
158
|
-
response_definition = responses.get("default", {})
|
|
159
|
-
links = response_definition.get(field, {})
|
|
160
|
-
return [Link.from_definition(name, definition, operation) for name, definition in links.items()]
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
@attr.s(slots=True, repr=False) # pragma: no mutate
|
|
164
|
-
class OpenAPILink(Direction):
|
|
165
|
-
"""Alternative approach to link processing.
|
|
166
|
-
|
|
167
|
-
NOTE. This class will replace `Link` in the future.
|
|
168
|
-
"""
|
|
169
|
-
|
|
170
|
-
name: str = attr.ib() # pragma: no mutate
|
|
171
|
-
status_code: str = attr.ib() # pragma: no mutate
|
|
172
|
-
definition: Dict[str, Any] = attr.ib() # pragma: no mutate
|
|
173
|
-
operation: APIOperation = attr.ib() # pragma: no mutate
|
|
174
|
-
parameters: List[Tuple[Optional[str], str, str]] = attr.ib(init=False) # pragma: no mutate
|
|
175
|
-
body: Union[Dict[str, Any], NotSet] = attr.ib(init=False) # pragma: no mutate
|
|
176
|
-
|
|
177
|
-
def __attrs_post_init__(self) -> None:
|
|
178
|
-
self.parameters = [
|
|
179
|
-
normalize_parameter(parameter, expression)
|
|
180
|
-
for parameter, expression in self.definition.get("parameters", {}).items()
|
|
181
|
-
]
|
|
182
|
-
self.body = self.definition.get("requestBody", NOT_SET)
|
|
183
|
-
|
|
184
|
-
def set_data(self, case: Case, elapsed: float, **kwargs: Any) -> None:
|
|
185
|
-
"""Assign all linked definitions to the new case instance."""
|
|
186
|
-
context = kwargs["context"]
|
|
187
|
-
self.set_parameters(case, context)
|
|
188
|
-
self.set_body(case, context)
|
|
189
|
-
case.set_source(context.response, context.case, elapsed)
|
|
190
|
-
|
|
191
|
-
def set_parameters(self, case: Case, context: expressions.ExpressionContext) -> None:
|
|
192
|
-
for location, name, expression in self.parameters:
|
|
193
|
-
container = get_container(case, location, name)
|
|
194
|
-
# Might happen if there is directly specified container,
|
|
195
|
-
# but the schema has no parameters of such type at all.
|
|
196
|
-
# Therefore the container is empty, otherwise it will be at least an empty object
|
|
197
|
-
if container is None:
|
|
198
|
-
message = f"No such parameter in `{case.operation.method.upper()} {case.operation.path}`: `{name}`."
|
|
199
|
-
possibilities = [param.name for param in case.operation.definition.parameters]
|
|
200
|
-
matches = get_close_matches(name, possibilities)
|
|
201
|
-
if matches:
|
|
202
|
-
message += f" Did you mean `{matches[0]}`?"
|
|
203
|
-
raise ValueError(message)
|
|
204
|
-
container[name] = expressions.evaluate(expression, context)
|
|
205
|
-
|
|
206
|
-
def set_body(self, case: Case, context: expressions.ExpressionContext) -> None:
|
|
207
|
-
if self.body is not NOT_SET:
|
|
208
|
-
case.body = expressions.evaluate(self.body, context)
|
|
209
|
-
|
|
210
|
-
def get_target_operation(self) -> APIOperation:
|
|
211
|
-
if "operationId" in self.definition:
|
|
212
|
-
return self.operation.schema.get_operation_by_id(self.definition["operationId"]) # type: ignore
|
|
213
|
-
return self.operation.schema.get_operation_by_reference(self.definition["operationRef"]) # type: ignore
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
def get_container(case: Case, location: Optional[str], name: str) -> Optional[Dict[str, Any]]:
|
|
217
|
-
"""Get a container that suppose to store the given parameter."""
|
|
218
|
-
if location:
|
|
219
|
-
container_name = LOCATION_TO_CONTAINER[location]
|
|
220
|
-
else:
|
|
221
|
-
for param in case.operation.definition.parameters:
|
|
222
|
-
if param.name == name:
|
|
223
|
-
container_name = LOCATION_TO_CONTAINER[param.location]
|
|
224
|
-
break
|
|
225
|
-
else:
|
|
226
|
-
raise ValueError(f"Parameter `{name}` is not defined in API operation `{case.operation.verbose_name}`")
|
|
227
|
-
return getattr(case, container_name)
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
def normalize_parameter(parameter: str, expression: str) -> Tuple[Optional[str], str, str]:
|
|
231
|
-
"""Normalize runtime expressions.
|
|
232
|
-
|
|
233
|
-
Runtime expressions may have parameter names prefixed with their location - `path.id`.
|
|
234
|
-
At the same time, parameters could be defined without a prefix - `id`.
|
|
235
|
-
We need to normalize all parameters to the same form to simplify working with them.
|
|
236
|
-
"""
|
|
237
|
-
try:
|
|
238
|
-
# The parameter name is prefixed with its location. Example: `path.id`
|
|
239
|
-
location, name = tuple(parameter.split("."))
|
|
240
|
-
return location, name, expression
|
|
241
|
-
except ValueError:
|
|
242
|
-
return None, parameter, expression
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
def get_all_links(operation: APIOperation) -> Generator[Tuple[str, OpenAPILink], None, None]:
|
|
246
|
-
for status_code, definition in operation.definition.resolved["responses"].items():
|
|
247
|
-
for name, link_definition in definition.get(operation.schema.links_field, {}).items(): # type: ignore
|
|
248
|
-
yield status_code, OpenAPILink(name, status_code, link_definition, operation)
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
StatusCode = Union[str, int]
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
def _get_response_by_status_code(responses: Dict[StatusCode, Dict[str, Any]], status_code: Union[str, int]) -> Dict:
|
|
255
|
-
if isinstance(status_code, int):
|
|
256
|
-
# Invalid schemas may contain status codes as integers
|
|
257
|
-
if status_code in responses:
|
|
258
|
-
return responses[status_code]
|
|
259
|
-
# Passed here as an integer, but there is no such status code as int
|
|
260
|
-
# We cast it to a string because it is either there already and we'll get relevant responses, otherwise
|
|
261
|
-
# a new dict will be created because there is no such status code in the schema (as an int or a string)
|
|
262
|
-
return responses.setdefault(str(status_code), {})
|
|
263
|
-
if status_code.isnumeric():
|
|
264
|
-
# Invalid schema but the status code is passed as a string
|
|
265
|
-
numeric_status_code = int(status_code)
|
|
266
|
-
if numeric_status_code in responses:
|
|
267
|
-
return responses[numeric_status_code]
|
|
268
|
-
# All status codes as strings, including `default` and patterned values like `5XX`
|
|
269
|
-
return responses.setdefault(status_code, {})
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
def add_link(
|
|
273
|
-
responses: Dict[StatusCode, Dict[str, Any]],
|
|
274
|
-
links_field: str,
|
|
275
|
-
parameters: Optional[Dict[str, str]],
|
|
276
|
-
request_body: Any,
|
|
277
|
-
status_code: StatusCode,
|
|
278
|
-
target: Union[str, APIOperation],
|
|
279
|
-
name: Optional[str] = None,
|
|
280
|
-
) -> None:
|
|
281
|
-
response = _get_response_by_status_code(responses, status_code)
|
|
282
|
-
links_definition = response.setdefault(links_field, {})
|
|
283
|
-
new_link: Dict[str, Union[str, Dict[str, str]]] = {}
|
|
284
|
-
if parameters is not None:
|
|
285
|
-
new_link["parameters"] = parameters
|
|
286
|
-
if request_body is not None:
|
|
287
|
-
new_link["requestBody"] = request_body
|
|
288
|
-
if isinstance(target, str):
|
|
289
|
-
name = name or target
|
|
290
|
-
new_link["operationRef"] = target
|
|
291
|
-
else:
|
|
292
|
-
name = name or f"{target.method.upper()} {target.path}"
|
|
293
|
-
# operationId is a dict lookup which is more efficient than using `operationRef`, since it
|
|
294
|
-
# doesn't involve reference resolving when we will look up for this target during testing.
|
|
295
|
-
if "operationId" in target.definition.resolved:
|
|
296
|
-
new_link["operationId"] = target.definition.resolved["operationId"]
|
|
297
|
-
else:
|
|
298
|
-
new_link["operationRef"] = target.operation_reference
|
|
299
|
-
# The name is arbitrary, so we don't really case what it is,
|
|
300
|
-
# but it should not override existing links
|
|
301
|
-
while name in links_definition:
|
|
302
|
-
name += "_new"
|
|
303
|
-
links_definition[name] = new_link
|