Files
connexion/tests/test_json_validation.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

131 lines
4.3 KiB
Python

import json
import pathlib
import pytest
from connexion import App
from connexion.json_schema import Draft4RequestValidator
from connexion.spec import Specification
from connexion.validators import JSONRequestBodyValidator
from jsonschema.validators import _utils, extend
from conftest import build_app_from_fixture
SPECS = ["swagger.yaml", "openapi.yaml"]
@pytest.mark.parametrize("spec", SPECS)
def test_validator_map(json_validation_spec_dir, spec):
def validate_type(validator, types, instance, schema):
types = _utils.ensure_list(types)
errors = Draft4RequestValidator.VALIDATORS["type"](
validator, types, instance, schema
)
yield from errors
if "string" in types and "minLength" not in schema:
errors = Draft4RequestValidator.VALIDATORS["minLength"](
validator, 1, instance, schema
)
yield from errors
MinLengthRequestValidator = extend(Draft4RequestValidator, {"type": validate_type})
class MyJSONBodyValidator(JSONRequestBodyValidator):
def __init__(self, *args, **kwargs):
super().__init__(*args, validator=MinLengthRequestValidator, **kwargs)
validator_map = {"body": {"application/json": MyJSONBodyValidator}}
app = App(__name__, specification_dir=json_validation_spec_dir)
app.add_api(spec, validate_responses=True, validator_map=validator_map)
app_client = app.test_client()
res = app_client.post(
"/v1.0/minlength",
data=json.dumps({"foo": "bar"}),
content_type="application/json",
) # type: flask.Response
assert res.status_code == 200
res = app_client.post(
"/v1.0/minlength", data=json.dumps({"foo": ""}), content_type="application/json"
) # type: flask.Response
assert res.status_code == 400
@pytest.mark.parametrize("spec", SPECS)
def test_readonly(json_validation_spec_dir, spec):
app = build_app_from_fixture(
json_validation_spec_dir, spec, validate_responses=True
)
app_client = app.test_client()
res = app_client.get("/v1.0/user") # type: flask.Response
assert res.status_code == 200
assert json.loads(res.data.decode()).get("user_id") == 7
res = app_client.post(
"/v1.0/user",
data=json.dumps({"name": "max", "password": "1234"}),
content_type="application/json",
) # type: flask.Response
assert res.status_code == 200
assert json.loads(res.data.decode()).get("user_id") == 8
res = app_client.post(
"/v1.0/user",
data=json.dumps({"user_id": 9, "name": "max"}),
content_type="application/json",
) # type: flask.Response
assert res.status_code == 400
@pytest.mark.parametrize("spec", SPECS)
def test_writeonly(json_validation_spec_dir, spec):
app = build_app_from_fixture(
json_validation_spec_dir, spec, validate_responses=True
)
app_client = app.test_client()
res = app_client.post(
"/v1.0/user",
data=json.dumps({"name": "max", "password": "1234"}),
content_type="application/json",
) # type: flask.Response
assert res.status_code == 200
assert "password" not in json.loads(res.data.decode())
res = app_client.get("/v1.0/user") # type: flask.Response
assert res.status_code == 200
assert "password" not in json.loads(res.data.decode())
res = app_client.get("/v1.0/user_with_password") # type: flask.Response
assert res.status_code == 500
assert (
json.loads(res.data.decode())["title"]
== "Response body does not conform to specification"
)
@pytest.mark.parametrize("spec", SPECS)
def test_nullable_default(json_validation_spec_dir, spec):
spec_path = pathlib.Path(json_validation_spec_dir) / spec
Specification.load(spec_path)
@pytest.mark.parametrize("spec", ["openapi.yaml"])
def test_multipart_form_json(json_validation_spec_dir, spec):
app = build_app_from_fixture(
json_validation_spec_dir, spec, validate_responses=True
)
app_client = app.test_client()
res = app_client.post(
"/v1.0/multipart_form_json",
data={"x": json.dumps({"name": "joe", "age": 20})},
content_type="multipart/form-data",
)
assert res.status_code == 200
assert json.loads(res.data.decode())["name"] == "joe-reply"
assert json.loads(res.data.decode())["age"] == 30