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_resolver_named(mock_get, externals_file):
mock_get.side_effect = mock_get_petstore
import os.path
from prance.util import fs
res = resolver.RefResolver(externals_file,
fs.abspath('tests/specs/with_externals.yaml'))
res.resolve_references()
def test_issue_23_partial_resolution_all(mock_get):
mock_get.side_effect = mock_get_petstore
specs = get_specs('tests/specs/with_externals.yaml')
res = resolver.RefResolver(specs,
fs.abspath('tests/specs/with_externals.yaml'))
res.resolve_references()
# By default, all externals need to be resolved.
from prance.util.path import path_get
val = path_get(res.specs, ('paths', '/pets', 'get', 'responses', '200', 'schema'))
assert '$ref' not in val
val = path_get(res.specs, ('paths', '/pets', 'get', 'responses', 'default', 'schema'))
assert '$ref' not in val
val = path_get(res.specs, ('paths', '/pets', 'post', 'responses', 'default', 'schema'))
assert '$ref' not in val
val = path_get(res.specs, ('paths', '/pets/{petId}', 'get', 'parameters', 0))
assert '$ref' not in val
def test_abspath_basics():
testname = os.path.normpath('tests/specs/with_externals.yaml')
res = fs.abspath(testname)
expected = fs.to_posix(os.path.join(os.getcwd(), testname))
assert res == expected
def test_resolver_recursive_objects(mock_get, recursive_objs_file):
mock_get.side_effect = mock_get_petstore
# Recursive references to objects are a problem
import os.path
res = resolver.RefResolver(recursive_objs_file,
fs.abspath('tests/specs/recursive_objs.yaml'))
with pytest.raises(ResolutionError) as exc:
res.resolve_references()
assert str(exc.value).startswith('Recursion reached limit')
def test_issue_69_urlparse_error():
# XXX depending on python version, the error may not actually come from
# parsing the URL, but from trying to load a local file that does
# not exist. See test_issue_72_* for a test case that specifically
# tries for a nonexistent file url.
specs = {'$ref': "file://a\u2100b/bad/netloc"}
res = resolver.RefResolver(specs,
fs.abspath('tests/specs/with_externals.yaml'))
with pytest.raises(ResolutionError) as exinfo:
res.resolve_references()
assert 'bad/netloc' in str(exinfo.value)
def test_recursion_limit_do_not_recurse_raise(recursion_limit_file):
# Expect the default behaviour to raise.
import os.path
res = resolver.RefResolver(recursion_limit_file,
fs.abspath('tests/specs/recursion_limit.yaml'))
with pytest.raises(ResolutionError) as exc:
res.resolve_references()
assert str(exc.value).startswith('Recursion reached limit of 1')
def test_issue_23_partial_resolution_http(mock_get):
mock_get.side_effect = mock_get_petstore
specs = get_specs('tests/specs/with_externals.yaml')
res = resolver.RefResolver(specs,
fs.abspath('tests/specs/with_externals.yaml'),
resolve_types = resolver.RESOLVE_HTTP
)
res.resolve_references()
# By default, all externals need to be resolved.
from prance.util.path import path_get
val = path_get(res.specs, ('paths', '/pets', 'get', 'responses', '200', 'schema'))
assert '$ref' in val
val = path_get(res.specs, ('paths', '/pets', 'get', 'responses', 'default', 'schema'))
assert '$ref' in val
val = path_get(res.specs, ('paths', '/pets', 'post', 'responses', 'default', 'schema'))
assert '$ref' in val
def __parser_for_url(url, resolve, backend, strict, encoding): # noqa: N802
"""Return a parser instance for the URL and the given parameters."""
# Try the URL
formatted = click.format_filename(url)
click.echo('Processing "%s"...' % (formatted, ))
from prance.util import fs
fsurl = fs.abspath(url)
import os.path
if os.path.exists(fs.from_posix(fsurl)):
url = fsurl
# Create parser to use
parser = None
if resolve:
click.echo(' -> Resolving external references.')
parser = prance.ResolvingParser(url, lazy = True, backend = backend,
strict = strict, encoding = encoding)
else:
click.echo(' -> Not resolving external references.')
parser = prance.BaseParser(url, lazy = True, backend = backend,
strict = strict, encoding = encoding)
# XXX maybe enable tihs in debug mode or something.