Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_validation_error_on_completely_invalid_swagger_spec():
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write('[1]\n'.encode())
with pytest.raises(InvalidSpecification):
FlaskApi(pathlib.Path(f.name), base_path="/api/v1.0")
os.unlink(f.name)
def test_api():
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'
api2 = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml")
assert api2.blueprint.name == '/v1_0'
assert api2.blueprint.url_prefix == '/v1.0'
api3 = FlaskApi(TEST_FOLDER / "fixtures/simple/openapi.yaml", base_path="/api/v1.0")
assert api3.blueprint.name == '/api/v1_0'
assert api3.blueprint.url_prefix == '/api/v1.0'
api4 = FlaskApi(TEST_FOLDER / "fixtures/simple/openapi.yaml")
assert api4.blueprint.name == '/v1_0'
assert api4.blueprint.url_prefix == '/v1.0'
def test_invalid_operation_does_not_stop_application_in_debug_mode():
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",
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",
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",
base_path="/api/v1.0", arguments={'title': 'OK'}, debug=True)
assert api.specification['info']['title'] == 'OK'
def test_api():
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'
api2 = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml")
assert api2.blueprint.name == '/v1_0'
assert api2.blueprint.url_prefix == '/v1.0'
api3 = FlaskApi(TEST_FOLDER / "fixtures/simple/openapi.yaml", base_path="/api/v1.0")
assert api3.blueprint.name == '/api/v1_0'
assert api3.blueprint.url_prefix == '/api/v1.0'
api4 = FlaskApi(TEST_FOLDER / "fixtures/simple/openapi.yaml")
assert api4.blueprint.name == '/v1_0'
assert api4.blueprint.url_prefix == '/v1.0'
def test_use_of_safe_load_for_yaml_swagger_specs():
with pytest.raises(YAMLError):
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write('!!python/object:object {}\n'.encode())
try:
FlaskApi(pathlib.Path(f.name), base_path="/api/v1.0")
os.unlink(f.name)
except InvalidSpecification:
pytest.fail("Could load invalid YAML file, use yaml.safe_load!")
def test_api():
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'
api2 = FlaskApi(TEST_FOLDER / "fixtures/simple/swagger.yaml")
assert api2.blueprint.name == '/v1_0'
assert api2.blueprint.url_prefix == '/v1.0'
api3 = FlaskApi(TEST_FOLDER / "fixtures/simple/openapi.yaml", base_path="/api/v1.0")
assert api3.blueprint.name == '/api/v1_0'
assert api3.blueprint.url_prefix == '/api/v1.0'
api4 = FlaskApi(TEST_FOLDER / "fixtures/simple/openapi.yaml")
assert api4.blueprint.name == '/v1_0'
assert api4.blueprint.url_prefix == '/v1.0'
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",
base_path="/api/v1.0", arguments={'title': 'test'})
assert api1.specification['info']['title'] == '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_other_errors_stop_application_to_setup():
# Errors should still result exceptions!
with pytest.raises(InvalidSpecification):
FlaskApi(TEST_FOLDER / "fixtures/bad_specs/swagger.yaml",
base_path="/api/v1.0", arguments={'title': 'OK'})
from connexion import FlaskApi, AbstractApp, RestyResolver, problem # type: ignore[import]
from connexion.apps.flask_app import FlaskJSONEncoder # type: ignore[import]
from connexion.exceptions import ProblemException # type: ignore[import]
from cmk.gui.wsgi.auth import with_user
from cmk.utils import paths, crash_reporting
logger = logging.getLogger('cmk.gui.wsgi.rest_api')
def openapi_spec_dir():
return paths.web_dir + "/htdocs/openapi"
class CheckmkApi(FlaskApi):
pass
def wrap_result(function_resolver, result_wrap):
"""Wrap the result of a resolver with another function.
"""
@functools.wraps(function_resolver)
def wrapper(*args, **kw):
return result_wrap(function_resolver(*args, **kw))
return wrapper
class APICrashReport(crash_reporting.ABCCrashReport):
"""API specific crash reporting class.