def test_init():
    """Test extension initialization."""
    app = Flask('testapp')
    ext = InvenioJSONSchemas(app)
    assert 'invenio-jsonschemas' in app.extensions

    app = Flask('testapp')
    ext = InvenioJSONSchemas()
    assert 'invenio-jsonschemas' not in app.extensions
    ext.init_app(app)
    assert 'invenio-jsonschemas' in app.extensions
def test_init():
    """Test extension initialization."""
    app = Flask('testapp')
    ext = InvenioJSONSchemas(app)
    assert 'invenio-jsonschemas' in app.extensions

    app = Flask('testapp')
    ext = InvenioJSONSchemas()
    assert 'invenio-jsonschemas' not in app.extensions
    ext.init_app(app)
    assert 'invenio-jsonschemas' in app.extensions
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_replace_refs_in_view(app, pkg_factory, mock_entry_points):
    """Test replace refs config in view."""
    schemas = {
        'root.json': '{"$ref": "sub/schema.json"}',
        'sub/schema.json': schema_template.format('test')
    }

    entry_point_group = 'invenio_jsonschema_test_entry_point'
    endpoint = '/testschemas'
    app.config['JSONSCHEMAS_ENDPOINT'] = endpoint
    with pkg_factory(schemas) as pkg1:
        mock_entry_points.add(entry_point_group, 'entry1', pkg1)
        ext = InvenioJSONSchemas(entry_point_group=entry_point_group)
        ext = ext.init_app(app)

        with app.test_client() as client:
            res = client.get('{0}/{1}'.format(endpoint, 'root.json'))
            assert res.status_code == 200
            assert json.loads(schemas['root.json']) == \
                json.loads(res.get_data(as_text=True))

            app.config['JSONSCHEMAS_REPLACE_REFS'] = True
            app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True

            res = client.get('{0}/{1}'.format(endpoint, 'root.json'))
            assert res.status_code == 200
            assert json.loads(schemas['sub/schema.json']) == \
                json.loads(res.get_data(as_text=True))

            app.config['JSONSCHEMAS_REPLACE_REFS'] = False

            res = client.get('{0}/{1}?refs=1'.format(endpoint, 'root.json'))
            assert res.status_code == 200
            assert json.loads(schemas['sub/schema.json']) == \
                json.loads(res.get_data(as_text=True))
def test_replace_refs_in_view(app, pkg_factory, mock_entry_points):
    """Test replace refs config in view."""
    schemas = {
        'root.json': '{"$ref": "sub/schema.json"}',
        'sub/schema.json': schema_template.format('test')
    }

    entry_point_group = 'invenio_jsonschema_test_entry_point'
    endpoint = '/testschemas'
    app.config['JSONSCHEMAS_ENDPOINT'] = endpoint
    with pkg_factory(schemas) as pkg1:
        mock_entry_points.add(entry_point_group, 'entry1', pkg1)
        ext = InvenioJSONSchemas(entry_point_group=entry_point_group)
        ext = ext.init_app(app)

        with app.test_client() as client:
            res = client.get('{0}/{1}'.format(endpoint, 'root.json'))
            assert res.status_code == 200
            assert json.loads(schemas['root.json']) == \
                json.loads(res.get_data(as_text=True))

            app.config['JSONSCHEMAS_REPLACE_REFS'] = True
            app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True

            res = client.get('{0}/{1}'.format(endpoint, 'root.json'))
            assert res.status_code == 200
            assert json.loads(schemas['sub/schema.json']) == \
                json.loads(res.get_data(as_text=True))

            app.config['JSONSCHEMAS_REPLACE_REFS'] = False

            res = client.get('{0}/{1}?refs=1'.format(endpoint, 'root.json'))
            assert res.status_code == 200
            assert json.loads(schemas['sub/schema.json']) == \
                json.loads(res.get_data(as_text=True))
def test_init(app):
    """Test extension initialization."""
    app = Flask('testapp')
    ext = InvenioJSONSchemas(app)
    assert 'invenio-jsonschemas' in app.extensions

    app = Flask('testapp')
    app.config['JSONSCHEMAS_REGISTER_ENDPOINTS_UI'] = True
    ext = InvenioJSONSchemasUI(app)
    assert 'invenio-jsonschemas' in app.extensions

    app = Flask('testapp')
    app.config['JSONSCHEMAS_REGISTER_ENDPOINTS_API'] = True
    ext = InvenioJSONSchemasAPI(app)
    assert 'invenio-jsonschemas' in app.extensions

    app = Flask('testapp')
    ext = InvenioJSONSchemas()
    assert 'invenio-jsonschemas' not in app.extensions
    ext.init_app(app)
    assert 'invenio-jsonschemas' in app.extensions
def test_init(app):
    """Test extension initialization."""
    app = Flask('testapp')
    ext = InvenioJSONSchemas(app)
    assert 'invenio-jsonschemas' in app.extensions

    app = Flask('testapp')
    app.config['JSONSCHEMAS_REGISTER_ENDPOINTS_UI'] = True
    ext = InvenioJSONSchemasUI(app)
    assert 'invenio-jsonschemas' in app.extensions

    app = Flask('testapp')
    app.config['JSONSCHEMAS_REGISTER_ENDPOINTS_API'] = True
    ext = InvenioJSONSchemasAPI(app)
    assert 'invenio-jsonschemas' in app.extensions

    app = Flask('testapp')
    ext = InvenioJSONSchemas()
    assert 'invenio-jsonschemas' not in app.extensions
    ext.init_app(app)
    assert 'invenio-jsonschemas' in app.extensions
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_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