def test_view(app, pkg_factory, mock_entry_points):
    """Test view."""
    schema_files_1 = build_schemas(1)
    schema_files_2 = build_schemas(2)
    schema_files_3 = build_schemas(3)

    all_schemas = dict()
    all_schemas.update(schema_files_1)
    all_schemas.update(schema_files_2)
    all_schemas.update(schema_files_3)

    entry_point_group = 'invenio_jsonschema_test_entry_point'
    endpoint = '/testschemas'
    app.config[InvenioJSONSchemas.CONFIG_ENDPOINT] = endpoint
    with pkg_factory(schema_files_1) as pkg1, \
            pkg_factory(schema_files_2) as pkg2, \
            pkg_factory(schema_files_3) as pkg3:
        mock_entry_points.add(entry_point_group, 'entry1', pkg1)
        mock_entry_points.add(entry_point_group, 'entry2', pkg2)
        mock_entry_points.add(entry_point_group, 'entry3', pkg3)
        # Test an alternative way of initializing the app
        # with InvenioJSONSchemas
        ext = InvenioJSONSchemas(entry_point_group=entry_point_group)
        ext = ext.init_app(app)
        # Test if all the schemas are correctly found
        assert set(ext.list_schemas()) == set(all_schemas.keys())

        with app.test_client() as client:
            for name, schema in all_schemas.items():
                res = client.get("{0}/{1}".format(endpoint, name))
                assert res.status_code == 200
                assert json.loads(schema) == \
                    json.loads(res.get_data(as_text=True))
            res = client.get("/nonexisting")
            assert res.status_code == 404
def test_view(app, pkg_factory, mock_entry_points):
    """Test view."""
    schema_files_1 = build_schemas(1)
    schema_files_2 = build_schemas(2)
    schema_files_3 = build_schemas(3)

    all_schemas = dict()
    all_schemas.update(schema_files_1)
    all_schemas.update(schema_files_2)
    all_schemas.update(schema_files_3)

    entry_point_group = 'invenio_jsonschema_test_entry_point'
    endpoint = '/testschemas'
    app.config[InvenioJSONSchemas.CONFIG_ENDPOINT] = endpoint
    with pkg_factory(schema_files_1) as pkg1, \
            pkg_factory(schema_files_2) as pkg2, \
            pkg_factory(schema_files_3) as pkg3:
        mock_entry_points.add(entry_point_group, 'entry1', pkg1)
        mock_entry_points.add(entry_point_group, 'entry2', pkg2)
        mock_entry_points.add(entry_point_group, 'entry3', pkg3)
        # Test an alternative way of initializing the app
        # with InvenioJSONSchemas
        ext = InvenioJSONSchemas(entry_point_group=entry_point_group)
        ext = ext.init_app(app)
        # Test if all the schemas are correctly found
        assert set(ext.list_schemas()) == set(all_schemas.keys())

        with app.test_client() as client:
            for name, schema in all_schemas.items():
                res = client.get("{0}/{1}".format(endpoint, name))
                assert res.status_code == 200
                assert json.loads(schema) == \
                    json.loads(res.get_data(as_text=True))
            res = client.get("/nonexisting")
            assert res.status_code == 404
def test_register_schema(app, dir_factory):
    ext = InvenioJSONSchemas(app, entry_point_group=None)
    schema_files = build_schemas(1)
    with dir_factory(schema_files) as directory:
        registered_schemas = set(list(schema_files.keys())[:1])
        nonregistered_schema = [s for s in schema_files if s not in
                                registered_schemas]
        for schema in registered_schemas:
            ext.register_schema(directory, schema)
        assert set(ext.list_schemas()) == registered_schemas

        for schema in nonregistered_schema:
            with pytest.raises(JSONSchemaNotFound):
                ext.get_schema(schema)
def test_register_schema(app, dir_factory):
    ext = InvenioJSONSchemas(app, entry_point_group=None)
    schema_files = build_schemas(1)
    with dir_factory(schema_files) as directory:
        registered_schemas = set(list(schema_files.keys())[:1])
        nonregistered_schema = [
            s for s in schema_files if s not in registered_schemas
        ]
        for schema in registered_schemas:
            ext.register_schema(directory, schema)
        assert set(ext.list_schemas()) == registered_schemas

        for schema in nonregistered_schema:
            with pytest.raises(JSONSchemaNotFound):
                ext.get_schema(schema)
def test_alternative_entry_point_group_init(app, pkg_factory,
                                            mock_entry_points):
    """Test initializing the entry_point_group after creating the extension."""
    schema_files_1 = build_schemas(1)
    schema_files_2 = build_schemas(2)

    all_schemas = dict()
    all_schemas.update(schema_files_1)
    all_schemas.update(schema_files_2)

    entry_point_group = 'invenio_jsonschema_test_entry_point'
    with pkg_factory(schema_files_1) as pkg1, \
            pkg_factory(schema_files_2) as pkg2:
        mock_entry_points.add(entry_point_group, 'entry1', pkg1)
        mock_entry_points.add(entry_point_group, 'entry2', pkg2)
        # Test an alternative way of initializing the app and entry_point_group
        # with InvenioJSONSchemas
        ext = InvenioJSONSchemas()
        ext = ext.init_app(app, entry_point_group=entry_point_group)
        # Test if all the schemas are correctly found
        assert set(ext.list_schemas()) == set(all_schemas.keys())
def test_alternative_entry_point_group_init(app, pkg_factory,
                                            mock_entry_points):
    """Test initializing the entry_point_group after creating the extension."""
    schema_files_1 = build_schemas(1)
    schema_files_2 = build_schemas(2)

    all_schemas = dict()
    all_schemas.update(schema_files_1)
    all_schemas.update(schema_files_2)

    entry_point_group = 'invenio_jsonschema_test_entry_point'
    with pkg_factory(schema_files_1) as pkg1, \
            pkg_factory(schema_files_2) as pkg2:
        mock_entry_points.add(entry_point_group, 'entry1', pkg1)
        mock_entry_points.add(entry_point_group, 'entry2', pkg2)
        # Test an alternative way of initializing the app and entry_point_group
        # with InvenioJSONSchemas
        ext = InvenioJSONSchemas()
        ext = ext.init_app(app, entry_point_group=entry_point_group)
        # Test if all the schemas are correctly found
        assert set(ext.list_schemas()) == set(all_schemas.keys())
def test_api(app, dir_factory):
    ext = InvenioJSONSchemas(app, entry_point_group=None)
    schema_files = build_schemas(1)
    with dir_factory(schema_files) as directory:
        ext.register_schemas_dir(directory)
        for path in schema_files.keys():
            # test get_schema_dir
            assert ext.get_schema_dir(path) == directory
            # test get_schema_path
            assert ext.get_schema_path(path) == \
                os.path.join(directory, path)
            # test get_schema
            assert ext.get_schema(path) == json.loads(schema_files[path])
        # test list_schemas
        assert set(schema_files.keys()) == set(ext.list_schemas())
        # test failure when asking for non existing schemas fails
        with pytest.raises(JSONSchemaNotFound) as exc_info:
            ext.get_schema('not_existing_schema.json')
        assert exc_info.value.schema == 'not_existing_schema.json'
        # test failure when asking for non existing schemas' path
        with pytest.raises(JSONSchemaNotFound) as exc_info:
            ext.get_schema_path('not_existing_schema.json')
        assert exc_info.value.schema == 'not_existing_schema.json'
def test_api(app, dir_factory):
    ext = InvenioJSONSchemas(app, entry_point_group=None)
    schema_files = build_schemas(1)
    with dir_factory(schema_files) as directory:
        ext.register_schemas_dir(directory)
        for path in schema_files.keys():
            # test get_schema_dir
            assert ext.get_schema_dir(path) == directory
            # test get_schema_path
            assert ext.get_schema_path(path) == \
                os.path.join(directory, path)
            # test get_schema
            assert ext.get_schema(path) == json.loads(schema_files[path])
        # test list_schemas
        assert set(schema_files.keys()) == set(ext.list_schemas())
        # test failure when asking for non existing schemas fails
        with pytest.raises(JSONSchemaNotFound) as exc_info:
            ext.get_schema('not_existing_schema.json')
        assert exc_info.value.schema == 'not_existing_schema.json'
        # test failure when asking for non existing schemas' path
        with pytest.raises(JSONSchemaNotFound) as exc_info:
            ext.get_schema_path('not_existing_schema.json')
        assert exc_info.value.schema == 'not_existing_schema.json'
def test_whitelisting_entry_points(app, pkg_factory, mock_entry_points,
                                   whitelisted, expected):
    """Test whitelisting entry points."""
    app.config['JSONSCHEMAS_SCHEMAS'] = whitelisted

    entries = dict(entry1=build_schemas(1), entry2=build_schemas(2))

    all_schemas = dict()
    all_schemas.update(entries['entry1'])
    all_schemas.update(entries['entry2'])

    entry_point_group = 'invenio_jsonschema_test_entry_point'
    with pkg_factory(entries['entry1']) as pkg1, \
            pkg_factory(entries['entry2']) as pkg2:
        mock_entry_points.add(entry_point_group, 'entry1', pkg1)
        mock_entry_points.add(entry_point_group, 'entry2', pkg2)
        ext = InvenioJSONSchemas()
        ext = ext.init_app(app, entry_point_group=entry_point_group)
        # Test if all whitelisted schemas are correctly found
        expected_schemas = set()
        for name in expected:
            for schema in entries[name].keys():
                expected_schemas.add(schema)
        assert set(ext.list_schemas()) == expected_schemas
Example #10
0
import json

from flask import Flask

from invenio_jsonschemas import InvenioJSONSchemas

# Create Flask application
app = Flask(__name__)

# set the endpoint serving the JSON schemas
app.config[InvenioJSONSchemas.CONFIG_ENDPOINT] = '/schemas'

# Initialize the application with the InvenioJSONSchema extension.
# This registers the jsonschemas from examples/samplepkg/jsonschemas as
# samplepkg's setup.py has the "invenio_jsonschemas.schemas" entrypoint.
ext = InvenioJSONSchemas(app)

# list all registered schemas
print('SCHEMAS >> {}'.format(ext.list_schemas()))
for schema in ext.list_schemas():
    print('=' * 50)
    print('SCHEMA {}'.format(schema))
    # retrieve the schema content
    print(json.dumps(ext.get_schema(schema), indent=4))

# InvenioJSONSchemas registers a blueprint serving the JSON schemas
print('>> You can retrieve the schemas using the url in their "id".')

if __name__ == "__main__":
    app.run()
Example #11
0
import json

from flask import Flask

from invenio_jsonschemas import InvenioJSONSchemas

# Create Flask application
app = Flask(__name__)

# set the endpoint serving the JSON schemas
app.config['JSONSCHEMAS_ENDPOINT'] = '/schemas'

# Initialize the application with the InvenioJSONSchema extension.
# This registers the jsonschemas from examples/samplepkg/jsonschemas as
# samplepkg's setup.py has the "invenio_jsonschemas.schemas" entrypoint.
ext = InvenioJSONSchemas(app)

# list all registered schemas
print('SCHEMAS >> {}'.format(ext.list_schemas()))
for schema in ext.list_schemas():
    print('=' * 50)
    print('SCHEMA {}'.format(schema))
    # retrieve the schema content
    print(json.dumps(ext.get_schema(schema), indent=4))

# InvenioJSONSchemas registers a blueprint serving the JSON schemas
print('>> You can retrieve the schemas using the url in their "id".')

if __name__ == "__main__":
    app.run()