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

75 lines
2.3 KiB
Python

import math
from unittest.mock import MagicMock
import connexion.apps
import pytest
from connexion import utils
def test_get_function_from_name():
function = utils.get_function_from_name("math.ceil")
assert function == math.ceil
assert function(2.7) == 3
def test_get_function_from_name_no_module():
with pytest.raises(ValueError):
utils.get_function_from_name("math")
def test_get_function_from_name_attr_error(monkeypatch):
"""
Test attribute error without import error on get_function_from_name.
Attribute errors due to import errors are tested on
test_api.test_invalid_operation_does_stop_application_to_setup
"""
deep_attr_mock = MagicMock()
deep_attr_mock.side_effect = AttributeError
monkeypatch.setattr("connexion.utils.deep_getattr", deep_attr_mock)
with pytest.raises(AttributeError):
utils.get_function_from_name("math.ceil")
def test_get_function_from_name_for_class_method():
function = utils.get_function_from_name("connexion.FlaskApp.add_error_handler")
assert function == connexion.FlaskApp.add_error_handler
def test_boolean():
assert utils.boolean("true")
assert utils.boolean("True")
assert utils.boolean("TRUE")
assert utils.boolean(True)
assert not utils.boolean("false")
assert not utils.boolean("False")
assert not utils.boolean("FALSE")
assert not utils.boolean(False)
with pytest.raises(ValueError):
utils.boolean("foo")
with pytest.raises(ValueError):
utils.boolean(None)
def test_deep_get_dict():
obj = {"type": "object", "properties": {"id": {"type": "string"}}}
assert utils.deep_get(obj, ["properties", "id"]) == {"type": "string"}
def test_deep_get_list():
obj = [{"type": "object", "properties": {"id": {"type": "string"}}}]
assert utils.deep_get(obj, ["0", "properties", "id"]) == {"type": "string"}
def test_is_json_mimetype():
assert utils.is_json_mimetype("application/json")
assert utils.is_json_mimetype("application/vnd.com.myEntreprise.v6+json")
assert utils.is_json_mimetype(
"application/vnd.scanner.adapter.vuln.report.harbor+json; version=1.0"
)
assert utils.is_json_mimetype(
"application/vnd.com.myEntreprise.v6+json; charset=UTF-8"
)
assert not utils.is_json_mimetype("text/html")