mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-09 20:37:46 +00:00
New style of passing options to Connexion (#436)
* Order classes by relevance in module * Order definitions by relevance within module * Swagger UI options extracted * New style options * Use new-style options * Reuse code * Sort imports * Ignore typing imports * Warn users about parameter name change * Add back isort check * Fix isort check
This commit is contained in:
committed by
Henning Jacobs
parent
19e0b37194
commit
93c06711ed
@@ -8,112 +8,113 @@ from yaml import YAMLError
|
||||
|
||||
import pytest
|
||||
from connexion import FlaskApi
|
||||
from connexion.apis.abstract import canonical_base_url
|
||||
from connexion.apis.abstract import canonical_base_path
|
||||
from connexion.exceptions import InvalidSpecification, ResolverError
|
||||
from mock import MagicMock
|
||||
|
||||
TEST_FOLDER = pathlib.Path(__file__).parent
|
||||
|
||||
|
||||
def test_canonical_base_url():
|
||||
assert canonical_base_url('') == ''
|
||||
assert canonical_base_url('/') == ''
|
||||
assert canonical_base_url('/api') == '/api'
|
||||
assert canonical_base_url('/api/') == '/api'
|
||||
def test_canonical_base_path():
|
||||
assert canonical_base_path('') == ''
|
||||
assert canonical_base_path('/') == ''
|
||||
assert canonical_base_path('/api') == '/api'
|
||||
assert canonical_base_path('/api/') == '/api'
|
||||
|
||||
|
||||
def test_api():
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml", "/api/v1.0", {})
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml", base_path="/api/v1.0")
|
||||
assert api.blueprint.name == '/api/v1_0'
|
||||
assert api.blueprint.url_prefix == '/api/v1.0'
|
||||
# TODO test base_url in spec
|
||||
|
||||
api2 = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml")
|
||||
assert api2.blueprint.name == '/v1_0'
|
||||
assert api2.blueprint.url_prefix == '/v1.0'
|
||||
|
||||
|
||||
def test_api_basepath_slash():
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/simple/basepath-slash.yaml", None, {})
|
||||
def test_api_base_path_slash():
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/simple/basepath-slash.yaml")
|
||||
assert api.blueprint.name == ''
|
||||
assert api.blueprint.url_prefix == ''
|
||||
|
||||
|
||||
def test_template():
|
||||
api1 = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml", "/api/v1.0", {'title': 'test'})
|
||||
api1 = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'test'})
|
||||
assert api1.specification['info']['title'] == 'test'
|
||||
|
||||
api2 = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml", "/api/v1.0", {'title': 'other test'})
|
||||
api2 = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'other test'})
|
||||
assert api2.specification['info']['title'] == 'other test'
|
||||
|
||||
|
||||
def test_invalid_operation_does_stop_application_to_setup():
|
||||
with pytest.raises(ImportError):
|
||||
FlaskApi(TEST_FOLDER / "fixtures/op_error_api/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'})
|
||||
FlaskApi(TEST_FOLDER / "fixtures/op_error_api/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'})
|
||||
|
||||
with pytest.raises(ResolverError):
|
||||
FlaskApi(TEST_FOLDER / "fixtures/missing_op_id/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'})
|
||||
FlaskApi(TEST_FOLDER / "fixtures/missing_op_id/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'})
|
||||
|
||||
with pytest.raises(ImportError):
|
||||
FlaskApi(TEST_FOLDER / "fixtures/module_not_implemented/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'})
|
||||
FlaskApi(TEST_FOLDER / "fixtures/module_not_implemented/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'})
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
FlaskApi(TEST_FOLDER / "fixtures/user_module_loading_error/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'})
|
||||
FlaskApi(TEST_FOLDER / "fixtures/user_module_loading_error/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'})
|
||||
|
||||
with pytest.raises(ResolverError):
|
||||
FlaskApi(TEST_FOLDER / "fixtures/missing_op_id/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'})
|
||||
FlaskApi(TEST_FOLDER / "fixtures/missing_op_id/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'})
|
||||
|
||||
|
||||
def test_invalid_operation_does_not_stop_application_in_debug_mode():
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/op_error_api/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'}, debug=True)
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/op_error_api/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
|
||||
assert api.specification['info']['title'] == 'OK'
|
||||
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/missing_op_id/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'}, debug=True)
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/missing_op_id/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
|
||||
assert api.specification['info']['title'] == 'OK'
|
||||
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/module_not_implemented/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'}, debug=True)
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/module_not_implemented/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
|
||||
assert api.specification['info']['title'] == 'OK'
|
||||
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/user_module_loading_error/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'}, debug=True)
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/user_module_loading_error/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
|
||||
assert api.specification['info']['title'] == 'OK'
|
||||
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/missing_op_id/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'}, debug=True)
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/missing_op_id/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
|
||||
assert api.specification['info']['title'] == 'OK'
|
||||
|
||||
|
||||
def test_other_errors_stop_application_to_setup():
|
||||
# The previous tests were just about operationId not being resolvable.
|
||||
# Other errors should still result exceptions!
|
||||
# Errors should still result exceptions!
|
||||
with pytest.raises(InvalidSpecification):
|
||||
FlaskApi(TEST_FOLDER / "fixtures/bad_specs/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'})
|
||||
FlaskApi(TEST_FOLDER / "fixtures/bad_specs/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'})
|
||||
|
||||
# Debug mode should ignore the error
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/bad_specs/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'}, debug=True)
|
||||
api = FlaskApi(TEST_FOLDER / "fixtures/bad_specs/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
|
||||
assert api.specification['info']['title'] == 'OK'
|
||||
|
||||
|
||||
def test_invalid_schema_file_structure():
|
||||
with pytest.raises(SwaggerValidationError):
|
||||
FlaskApi(TEST_FOLDER / "fixtures/invalid_schema/swagger.yaml", "/api/v1.0",
|
||||
{'title': 'OK'}, debug=True)
|
||||
FlaskApi(TEST_FOLDER / "fixtures/invalid_schema/swagger.yaml",
|
||||
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
|
||||
|
||||
|
||||
def test_invalid_encoding():
|
||||
with tempfile.NamedTemporaryFile(mode='wb') as f:
|
||||
f.write(u"swagger: '2.0'\ninfo:\n title: Foo 整\n version: v1\npaths: {}".encode('gbk'))
|
||||
f.flush()
|
||||
FlaskApi(pathlib.Path(f.name), "/api/v1.0")
|
||||
FlaskApi(pathlib.Path(f.name), base_path="/api/v1.0")
|
||||
|
||||
|
||||
def test_use_of_safe_load_for_yaml_swagger_specs():
|
||||
@@ -122,7 +123,7 @@ def test_use_of_safe_load_for_yaml_swagger_specs():
|
||||
f.write('!!python/object:object {}\n'.encode())
|
||||
f.flush()
|
||||
try:
|
||||
FlaskApi(pathlib.Path(f.name), "/api/v1.0")
|
||||
FlaskApi(pathlib.Path(f.name), base_path="/api/v1.0")
|
||||
except SwaggerValidationError:
|
||||
pytest.fail("Could load invalid YAML file, use yaml.safe_load!")
|
||||
|
||||
@@ -132,4 +133,17 @@ def test_validation_error_on_completely_invalid_swagger_spec():
|
||||
with tempfile.NamedTemporaryFile() as f:
|
||||
f.write('[1]\n'.encode())
|
||||
f.flush()
|
||||
FlaskApi(pathlib.Path(f.name), "/api/v1.0")
|
||||
FlaskApi(pathlib.Path(f.name), base_path="/api/v1.0")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_api_logger(monkeypatch):
|
||||
mocked_logger = MagicMock(name='mocked_logger')
|
||||
monkeypatch.setattr('connexion.apis.abstract.logger', mocked_logger)
|
||||
return mocked_logger
|
||||
|
||||
|
||||
def test_warn_users_about_base_url_parameter_name_change(mock_api_logger):
|
||||
FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml", base_url="/api/v1")
|
||||
mock_api_logger.warning.assert_called_with(
|
||||
'Parameter base_url should be no longer used. Use base_path instead.')
|
||||
|
||||
Reference in New Issue
Block a user