def test_openapi_spec():
    """Test OpenAPI spec validation."""

    current_dir = os.path.abspath(os.path.dirname(__file__))
    with open(os.path.join(current_dir, "../docs/openapi.json")) as f:
        reana_job_controller_spec = json.load(f)

    validate_json(reana_job_controller_spec, "schemas/v2.0/schema.json")
Пример #2
0
def build_openapi_spec(publish):
    """Creates an OpenAPI definition of Flask application,
    check conformity of generated definition against OpenAPI 2.0 specification
    and writes it into a file."""

    package = __title__
    desc = __api_description__
    ver = __version__

    # Create OpenAPI specification object
    spec = APISpec(title=package,
                   version=ver,
                   info=dict(description=desc),
                   plugins=('apispec.ext.flask', 'apispec.ext.marshmallow'))

    # Add marshmallow schemas to the specification here
    # spec.definition('Example', schema=Example_schema)

    # Collect OpenAPI docstrings from Flask endpoints
    for key in current_app.view_functions:
        if key != 'static' and key != 'get_openapi_spec':
            spec.add_path(view=current_app.view_functions[key])

    spec_json = json.dumps(spec.to_dict(),
                           indent=2,
                           separators=(',', ': '),
                           sort_keys=True)

    # Output spec to JSON file
    with click.open_file(__output_path__,
                         mode='w+',
                         encoding=None,
                         errors='strict',
                         lazy=False,
                         atomic=False) as output_file:

        output_file.write(spec_json)

        click.echo(
            click.style('OpenAPI specification written to {}'.format(
                output_file.name),
                        fg='green'))

    # Check that generated spec passes validation. Done after writing to file
    # in order to give user easy way to access the possible erroneous spec.
    with open(os.path.join(os.getcwd(), __output_path__)) as output_file:

        validate_json(json.load(output_file), 'schemas/v2.0/schema.json')

        click.echo(
            click.style('OpenAPI specification validated successfully',
                        fg='green'))
        if publish:
            copy_openapi_specs(__output_path__, 'reana-workflow-controller')

    return spec.to_dict()
def get_deref(spec_dict):
    swagger_resolver = validate_json(
        spec_dict,
        'schemas/v2.0/schema.json',
    )
    return functools.partial(deref, resolver=swagger_resolver)
def get_deref(spec_dict):
    swagger_resolver = validate_json(
        spec_dict,
        'schemas/v2.0/schema.json',
    )
    return functools.partial(deref, resolver=swagger_resolver)
def test_failure():
    with pytest.raises(SwaggerValidationError) as excinfo:
        validate_json({}, 'schemas/v2.0/schema.json')
    assert "'swagger' is a required property" in str(excinfo.value)
def test_success():
    with open('./tests/data/v2.0/petstore.json') as f:
        petstore_spec = json.load(f)
    validate_json(petstore_spec, 'schemas/v2.0/schema.json')
def test_success():
    my_dir = os.path.abspath(os.path.dirname(__file__))
    with open(os.path.join(my_dir, '../data/v2.0/petstore.json')) as f:
        petstore_spec = json.load(f)
    validate_json(petstore_spec, 'schemas/v2.0/schema.json')
def test_success():
    my_dir = os.path.abspath(os.path.dirname(__file__))
    with open(os.path.join(my_dir, '../data/v2.0/petstore.json')) as f:
        petstore_spec = json.load(f)
    validate_json(petstore_spec, 'schemas/v2.0/schema.json')