Files
connexion/tests/test_references.py
Robbe Sneyders edb0381af3 Implement user facing interface for ConnexionMiddleware (#1621)
This PR adds an interface for the ConnexionMiddleware, similar to the
interface of the Connexion Apps.

The Connexion Apps are now a simple wrapper around the
ConnexionMiddleware and framework app, delegating the work to the
middleware. This enables a similar interface and behavior for users when
using either the middleware or apps.

The arguments are repeated everywhere there is a user interface, but are
parsed in a central place. Repeating the arguments is not DRY, but
needed to provide users with IDE autocomplete, typing, etc. They are
parsed in a single `_Options` class, which also provides a mechanism to
set default options on an App level, and override them on the more
granular API level.

This makes the long list of provided parameters a lot more manageable,
so I would like to use it for the `Jsonifier` as well, and re-add the
`debug` and `extra_files` arguments which I have dropped in previous
PRs. I'll submit a separate PR for this.

I renamed the `options` parameter to `swagger_ui_options` since it only
contains swagger UI options. This is a breaking change though, and we'll
need to highlight this upon release.

We still have quite a lot of `App`, `MiddlewareApp`, and abstract
classes. It would be great if we could find a way to reduce those
further, or at least find better naming to make it more clear what each
one does 🙂 .

Finally, I added examples on how the middleware can be used with third
party frameworks under `examples/frameworks`. Currently there's an
example for Starlette and Quart, but this should be easy to extend. They
also show how the `ASGIDecorator` and `StarletteDecorator` from my
previous PR can be used.
2023-01-26 14:40:29 +01:00

121 lines
3.4 KiB
Python

from unittest import mock
import pytest
from connexion.json_schema import RefResolutionError, resolve_refs
from connexion.jsonifier import Jsonifier
DEFINITIONS = {
"new_stack": {
"required": ["image_version", "keep_stacks", "new_traffic", "senza_yaml"],
"type": "object",
"properties": {
"keep_stacks": {
"type": "integer",
"description": "Number of older stacks to keep",
},
"image_version": {
"type": "string",
"description": "Docker image version to deploy",
},
"senza_yaml": {"type": "string", "description": "YAML to provide to senza"},
"new_traffic": {
"type": "integer",
"description": "Percentage of the traffic",
},
},
},
"composed": {
"required": ["test"],
"type": "object",
"properties": {"test": {"schema": {"$ref": "#/definitions/new_stack"}}},
},
"problem": {"some": "thing"},
}
PARAMETER_DEFINITIONS = {"myparam": {"in": "path", "type": "integer"}}
@pytest.fixture
def api():
return mock.MagicMock(jsonifier=Jsonifier)
def test_non_existent_reference(api):
op_spec = {
"parameters": [
{
"in": "body",
"name": "new_stack",
"required": True,
"schema": {"$ref": "#/definitions/new_stack"},
}
]
}
with pytest.raises(RefResolutionError) as exc_info: # type: py.code.ExceptionInfo
resolve_refs(op_spec, {})
exception = exc_info.value
assert "definitions/new_stack" in str(exception)
def test_invalid_reference(api):
op_spec = {
"parameters": [
{
"in": "body",
"name": "new_stack",
"required": True,
"schema": {"$ref": "#/notdefinitions/new_stack"},
}
]
}
with pytest.raises(RefResolutionError) as exc_info: # type: py.code.ExceptionInfo
resolve_refs(
op_spec, {"definitions": DEFINITIONS, "parameters": PARAMETER_DEFINITIONS}
)
exception = exc_info.value
assert "notdefinitions/new_stack" in str(exception)
def test_resolve_invalid_reference(api):
op_spec = {
"operationId": "fakeapi.hello.post_greeting",
"parameters": [{"$ref": "/parameters/fail"}],
}
with pytest.raises(RefResolutionError) as exc_info:
resolve_refs(op_spec, {"parameters": PARAMETER_DEFINITIONS})
exception = exc_info.value
assert "parameters/fail" in str(exception)
def test_resolve_web_reference(api):
op_spec = {"parameters": [{"$ref": "https://reallyfake.asd/parameters.json"}]}
store = {"https://reallyfake.asd/parameters.json": {"name": "test"}}
spec = resolve_refs(op_spec, store=store)
assert spec["parameters"][0]["name"] == "test"
def test_resolve_ref_referring_to_another_ref(api):
expected = {"type": "string"}
op_spec = {
"parameters": [
{
"schema": {"$ref": "#/definitions/A"},
}
],
"definitions": {
"A": {
"$ref": "#/definitions/B",
},
"B": expected,
},
}
spec = resolve_refs(op_spec)
assert spec["parameters"][0]["schema"] == expected
assert spec["definitions"]["A"] == expected