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_convert_parser_lazy_swagger_backend():
from prance import BaseParser, ResolvingParser, ValidationError
parser = BaseParser('tests/specs/petstore.yaml')
# Conversion should fail with the default backend.
with pytest.raises(ValidationError):
converted = convert.convert_spec(parser)
# However, with the lazy flag it should work.
converted = convert.convert_spec(parser, lazy = True)
assert isinstance(converted, BaseParser)
# Passing a ResolvingParser class should also work.
converted = convert.convert_spec(parser, ResolvingParser, lazy = True)
assert isinstance(converted, ResolvingParser)
def test_convert_spec():
from prance import BaseParser, ResolvingParser, ValidationError
parser = BaseParser('tests/specs/petstore.yaml')
# Conversion should fail with the default backend.
with pytest.raises(ValidationError):
converted = convert.convert_spec(parser.specification)
# However, with the lazy flag it should work.
converted = convert.convert_spec(parser.specification, lazy = True)
assert isinstance(converted, BaseParser)
# Passing a ResolvingParser class should also work.
converted = convert.convert_spec(parser.specification, ResolvingParser, lazy = True)
assert isinstance(converted, ResolvingParser)
def test_flex_validate_failure():
with pytest.raises(ValidationError):
parser = BaseParser('tests/specs/missing_reference.yaml', backend = 'flex')
def test_reraise_with_value_extra_message():
with pytest.raises(ValidationError) as caught:
try:
raise RuntimeError("foo")
except RuntimeError as inner:
exceptions.raise_from(ValidationError, inner, 'asdf')
# The first is obvious from pytest.raises. The rest tests
# known attributes
assert caught.type == ValidationError
assert str(caught.value) == 'foo -- asdf'
def test_parse_fail():
with pytest.raises(ValidationError):
BaseParser(spec_string = """---
invalid 'a'sda YAML""")
def test_reraise_without_value_no_extra_message():
with pytest.raises(ValidationError) as caught:
exceptions.raise_from(ValidationError, None)
# The first is obvious from pytest.raises. The rest tests
# known attributes
assert caught.type == ValidationError
assert str(caught.value) == ''
def test_openapi_spec_validator_issue_36_error_reporting():
with pytest.raises(ValidationError, match = r'Strict mode enabled'):
BaseParser('tests/specs/issue_36.yaml')
def __validate(parser, name): # noqa: N802
"""Validate a spec using this parser."""
from prance.util.url import ResolutionError
from prance import ValidationError
try:
parser.parse()
except (ResolutionError, ValidationError) as err:
msg = 'ERROR in "%s" [%s]: %s' % (name, type(err).__name__,
str(err))
click.secho(msg, err = True, fg = 'red')
import sys
sys.exit(1)
# All good, next file.
click.echo('Validates OK as %s!' % (parser.version,))
"""
try:
import prance
except ImportError as error: # re-raise with a more verbose message
exc_class = type(error)
raise exc_class(
"validate_spec requires prance to be installed. "
"You can install all validation requirements using:\n"
" pip install 'apispec[validation]'"
)
parser_kwargs = {}
if spec.openapi_version.version[0] == 3:
parser_kwargs["backend"] = "openapi-spec-validator"
try:
prance.BaseParser(spec_string=json.dumps(spec.to_dict()), **parser_kwargs)
except prance.ValidationError as err:
raise exceptions.OpenAPIError(*err.args)
else:
return True
def _parse_file(path, base=None):
try:
parser = ResolvingParser(str(path))
return parser.specification
except (
AssertionError,
AttributeError,
ComposerError,
FileNotFoundError,
ResolutionError,
ScannerError,
UnicodeDecodeError,
ValidationError,
) as err:
log.info(
"repos.views.openapi.invalid", path=str(path.relative_to(base)), error=err
)