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_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
예제 #5
0
def test_url_mapping(app, dir_factory, url_scheme):
    """Test register schema."""
    app.config['SERVER_NAME'] = 'example.org'
    app.config['JSONSCHEMAS_HOST'] = 'inveniosoftware.org'
    if url_scheme is not None:
        app.config['JSONSCHEMAS_URL_SCHEME'] = url_scheme
    else:
        # test with default url scheme configuration
        url_scheme = JSONSCHEMAS_URL_SCHEME

    ext = InvenioJSONSchemas(app, entry_point_group=None)
    schema_files = build_schemas(1)

    with dir_factory(schema_files) as directory:
        ext.register_schemas_dir(directory)
        with app.app_context():
            assert 'sub1/subschema_1.json' == ext.url_to_path(
                '{0}://inveniosoftware.org/schemas/sub1/subschema_1.json'
                .format(url_scheme))
            assert ext.url_to_path(
                '{0}://inveniosoftware.org/schemas/invalid.json'
                .format(url_scheme)) is None
            assert ext.url_to_path(
                '{0}://example.org/schemas/sub1/subschema_1.json'
                .format(url_scheme)) is None

            assert (
                '{0}://inveniosoftware.org/schemas/sub1/subschema_1.json'
                .format(url_scheme)
            ) == ext.path_to_url('sub1/subschema_1.json')
            assert ext.path_to_url('invalid.json') is None
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_redefine(app, dir_factory):
    ext = InvenioJSONSchemas(app, entry_point_group=None)
    schema_files = build_schemas(1)
    with dir_factory(schema_files) as dir1, \
            dir_factory(schema_files) as dir2:
        ext.register_schemas_dir(dir1)
        # register schemas from a directory which have the same relative
        # paths
        with pytest.raises(JSONSchemaDuplicate) as exc_info:
            ext.register_schemas_dir(dir2)
        assert exc_info.value.schema in schema_files.keys()
def test_url_mapping(app, dir_factory, url_scheme):
    """Test register schema."""
    app.config['SERVER_NAME'] = 'example.org'
    app.config['JSONSCHEMAS_HOST'] = 'inveniosoftware.org'
    if url_scheme is not None:
        app.config['JSONSCHEMAS_URL_SCHEME'] = url_scheme
    else:
        # test with default url scheme configuration
        url_scheme = JSONSCHEMAS_URL_SCHEME

    ext = InvenioJSONSchemas(app, entry_point_group=None)
    schema_files = build_schemas(1)

    with dir_factory(schema_files) as directory:
        ext.register_schemas_dir(directory)
        with app.app_context():
            assert 'sub1/subschema_1.json' == ext.url_to_path(
                '{0}://inveniosoftware.org/schemas/sub1/subschema_1.json'
                .format(url_scheme))
            assert ext.url_to_path(
                '{0}://inveniosoftware.org/schemas/invalid.json'
                .format(url_scheme)) is None
            assert ext.url_to_path(
                '{0}://example.org/schemas/sub1/subschema_1.json'
                .format(url_scheme)) is None

            assert (
                '{0}://inveniosoftware.org/schemas/sub1/subschema_1.json'
                .format(url_scheme)
            ) == ext.path_to_url('sub1/subschema_1.json')
            assert ext.path_to_url('invalid.json') is None
예제 #9
0
def base_app():
    """Flask application fixture."""
    app_ = Flask('testapp')
    app_.config.update(
        TESTING=True,
        JSON_AS_ASCII=True,
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        SQLALCHEMY_DATABASE_URI=os.environ.get(
            'SQLALCHEMY_DATABASE_URI',
            'sqlite:///:memory:'),
        SERVER_NAME='localhost',
        SECURITY_PASSWORD_SALT='TEST_SECURITY_PASSWORD_SALT',
        SECRET_KEY='TEST_SECRET_KEY',
        FILES_REST_MULTIPART_CHUNKSIZE_MIN=2,
        FILES_REST_MULTIPART_CHUNKSIZE_MAX=20,
        FILES_REST_MULTIPART_MAX_PARTS=100,
        FILES_REST_TASK_WAIT_INTERVAL=0.1,
        FILES_REST_TASK_WAIT_MAX_SECONDS=1,
    )
    app.test_client_class = JsonClient

    InvenioDB(app_)
    InvenioAccounts(app_)
    InvenioAccess(app_)
    InvenioJSONSchemas(app_)

    return app_
예제 #10
0
def base_app():
    """Flask application fixture."""
    app_ = Flask('testapp')
    app_.config.update(
        TESTING=True,
        JSON_AS_ASCII=True,
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        SQLALCHEMY_DATABASE_URI=os.environ.get(
            'SQLALCHEMY_DATABASE_URI',
            'sqlite:///:memory:'),
        SERVER_NAME='localhost',
        SECURITY_PASSWORD_SALT='TEST_SECURITY_PASSWORD_SALT',
        SECRET_KEY='TEST_SECRET_KEY',
        FILES_REST_MULTIPART_CHUNKSIZE_MIN=2,
        FILES_REST_MULTIPART_CHUNKSIZE_MAX=20,
        FILES_REST_MULTIPART_MAX_PARTS=100,
        FILES_REST_TASK_WAIT_INTERVAL=0.1,
        FILES_REST_TASK_WAIT_MAX_SECONDS=1,
        TAXONOMY_SERVER_NAME='taxonomy-server.com',
        TAXONOMY_REDIS_URL='redis://localhost:6379/15',
    )
    app.test_client_class = JsonClient

    InvenioDB(app_)
    InvenioAccounts(app_)
    InvenioAccess(app_)
    InvenioJSONSchemas(app_)
    OARepoReferences(app_)
    app_.extensions['oarepo-references'] = RecordReferencesStateMock(app_)

    return app_
예제 #11
0
def app(request):
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app_ = Flask(__name__, instance_path=instance_path)
    app_.config.update(
        CELERY_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND='memory',
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_RESULT_BACKEND='cache',
        SECRET_KEY='CHANGE_ME',
        SECURITY_PASSWORD_SALT='CHANGE_ME_ALSO',
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///test.db'),
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        TESTING=True,
    )
    FlaskCLI(app_)
    FlaskCeleryExt(app_)
    InvenioDB(app_)
    InvenioAccounts(app_)
    InvenioJSONSchemas(app_)
    InvenioSearch(app_)
    InvenioRecords(app_)
    InvenioRecordsREST(app_)
    InvenioPIDStore(app_)
    InvenioDeposit(app_)

    with app_.app_context():
        yield app_

    shutil.rmtree(instance_path)
예제 #12
0
def es_app(request):
    """Flask application with records fixture."""
    app = Flask(__name__)
    app.config.update(
        JSONSCHEMAS_HOST='http://localhost:5000',
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///test.db'),
    )
    app.config[InvenioJSONSchemas.CONFIG_ENDPOINT] = '/'

    Babel(app)
    FlaskCLI(app)
    InvenioDB(app)
    InvenioRecords(app)
    InvenioMARC21(app)
    search = InvenioSearch(app)
    InvenioIndexer(app)
    InvenioJSONSchemas(app)

    with app.app_context():
        db.create_all()
        list(search.create())
        sleep(10)

    def teardown():
        with app.app_context():
            db.drop_all()
            list(search.delete())

    request.addfinalizer(teardown)

    return app
예제 #13
0
def app():
    instance_path = tempfile.mkdtemp()
    app = Flask('testapp', instance_path=instance_path)

    app.config.update(
        JSONSCHEMAS_HOST="nusl.cz",
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        SQLALCHEMY_DATABASE_URI=os.environ.get(
            'SQLALCHEMY_DATABASE_URI',
            'postgresql+psycopg2://{user}:{pw}@{url}/{db}'.format(user="******", pw="oarepo",
                                                                  url="127.0.0.1",
                                                                  db="oarepo")),
        SERVER_NAME='127.0.0.1:5000',
    )
    InvenioRecordsDraft(app)
    InvenioJSONSchemas(app)
    InvenioRecords(app)
    InvenioSearch(app)
    InvenioIndexer(app)
    InvenioDB(app)
    FlaskTaxonomies(app)
    FlaskTaxonomiesES(app)
    InvenioNUSLTheses(app)
    with app.app_context():
        app.register_blueprint(taxonomies_blueprint)
        yield app

    shutil.rmtree(instance_path)
예제 #14
0
def app(request):
    """Flask application fixture."""
    # Set temporary instance path for sqlite
    instance_path = tempfile.mkdtemp()
    app = Flask('testapp', instance_path=instance_path)
    app.config.update(
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///test.db'),
        INDEXER_REPLACE_REFS=True,
        CELERY_ALWAYS_EAGER=True,
        CELERY_RESULT_BACKEND="cache",
        CELERY_CACHE_BACKEND="memory",
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        JSONSCHEMAS_HOST='inveniosoftware.org',
        OPENAIRE_OAI_LOCAL_SOURCE='invenio_openaire/data/oaire_local.sqlite',
        TESTING=True,
    )

    app.url_map.converters['pid'] = PIDConverter
    app.url_map.converters['pidpath'] = PIDPathConverter

    LoginManager(app)
    InvenioDB(app)
    InvenioIndexer(app)
    InvenioRecords(app)
    InvenioCelery(app)
    InvenioPIDStore(app)
    InvenioOpenAIRE(app)
    InvenioSearch(app)
    InvenioJSONSchemas(app)

    with app.app_context():
        yield app

    shutil.rmtree(instance_path)
예제 #15
0
def app():
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app = Flask('testapp', instance_path=instance_path)
    app.config.update(
        CELERY_ALWAYS_EAGER=True,
        CELERY_TASK_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND='memory',
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_TASK_EAGER_PROPAGATES=True,
        CELERY_RESULT_BACKEND='cache',
        JSONSCHEMAS_HOST='inveniosoftware.org',
        TESTING=True,
        SECRET_KEY='CHANGE_ME',
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///test.db'),
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        SERVER_NAME='app',
        OAISERVER_ID_PREFIX='oai:inveniosoftware.org:recid/',
        OAISERVER_QUERY_PARSER_FIELDS=["title_statement"],
        OAISERVER_RECORD_INDEX='_all',
        OAISERVER_REGISTER_SET_SIGNALS=True,
    )
    if not hasattr(app, 'cli'):
        from flask_cli import FlaskCLI
        FlaskCLI(app)
    InvenioDB(app)
    FlaskCeleryExt(app)
    InvenioJSONSchemas(app)
    InvenioRecords(app)
    InvenioPIDStore(app)
    InvenioMARC21(app)
    client = Elasticsearch(hosts=[os.environ.get('ES_HOST', 'localhost')])
    search = InvenioSearch(app, client=client)
    search.register_mappings('records', 'data')
    InvenioIndexer(app)
    InvenioOAIServer(app)

    app.register_blueprint(blueprint)

    with app.app_context():
        if str(db.engine.url) != 'sqlite://' and \
           not database_exists(str(db.engine.url)):
            create_database(str(db.engine.url))
        db.create_all()
        list(search.delete(ignore=[404]))
        list(search.create())
        search.flush_and_refresh('_all')

    with app.app_context():
        yield app

    with app.app_context():
        db.session.close()
        if str(db.engine.url) != 'sqlite://':
            drop_database(str(db.engine.url))
        list(search.delete(ignore=[404]))
        search.client.indices.delete("*-percolators")
    shutil.rmtree(instance_path)
예제 #16
0
def app(base_app):
    """Flask application fixture."""
    InvenioDB(base_app)
    InvenioAccounts(base_app)
    InvenioFilesREST(base_app)
    InvenioJSONSchemas(base_app)
    with base_app.app_context():
        yield base_app
예제 #17
0
def app(request):
    """Test multilingual."""
    instance_path = tempfile.mkdtemp()
    app = Flask('testapp', instance_path=instance_path)

    app.config.update(
        ACCOUNTS_JWT_ENABLE=False,
        INDEXER_DEFAULT_DOC_TYPE='record-v1.0.0',
        RECORDS_REST_ENDPOINTS={},
        RECORDS_REST_DEFAULT_CREATE_PERMISSION_FACTORY=None,
        RECORDS_REST_DEFAULT_DELETE_PERMISSION_FACTORY=None,
        RECORDS_REST_DEFAULT_READ_PERMISSION_FACTORY=None,
        RECORDS_REST_DEFAULT_UPDATE_PERMISSION_FACTORY=None,
        SERVER_NAME='localhost:5000',
        JSONSCHEMAS_HOST='localhost:5000',
        CELERY_ALWAYS_EAGER=True,
        CELERY_RESULT_BACKEND='cache',
        CELERY_CACHE_BACKEND='memory',
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        SQLALCHEMY_DATABASE_URI=os.environ.get(
            'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'
        ),
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        SUPPORTED_LANGUAGES=["cs", "en"],
        BABEL_DEFAULT_LOCALE='cs',
        I18N_LANGUAGES=(
            ('en', _('English')),
        ),
        TESTING=True,
        ELASTICSEARCH_DEFAULT_LANGUAGE_TEMPLATE={
            "type": "text",
            "fields": {
                "raw": {
                    "type": "keyword"
                }
            }
        }

    )

    app.secret_key = 'changeme'

    InvenioDB(app)
    InvenioRecords(app)
    InvenioJSONSchemas(app)
    InvenioPIDStore(app)
    InvenioSearch(app)
    InvenioIndexer(app)
    OARepoMappingIncludesExt(app)
    OARepoMultilingualExt(app)
    #
    app_loaded.send(app, app=app)

    with app.app_context():
        yield app

    # Teardown instance path.
    shutil.rmtree(instance_path)
예제 #18
0
def app():
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app_ = Flask(__name__, instance_path=instance_path)
    app_.config.update(
        CELERY_ALWAYS_EAGER=True,
        CELERY_CACHE_BACKEND='memory',
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
        CELERY_RESULT_BACKEND='cache',
        JSONSCHEMAS_URL_SCHEME='http',
        SECRET_KEY='CHANGE_ME',
        SECURITY_PASSWORD_SALT='CHANGE_ME_ALSO',
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite:///test.db'),
        SQLALCHEMY_TRACK_MODIFICATIONS=True,
        SQLALCHEMY_ECHO=False,
        TESTING=True,
        WTF_CSRF_ENABLED=False,
        DEPOSIT_SEARCH_API='/api/search',
        SECURITY_PASSWORD_HASH='plaintext',
        SECURITY_PASSWORD_SCHEMES=['plaintext'],
        SECURITY_DEPRECATED_PASSWORD_SCHEMES=[],
        OAUTHLIB_INSECURE_TRANSPORT=True,
        OAUTH2_CACHE_TYPE='simple',
    )
    app_.url_map.converters['pid'] = PIDConverter
    FlaskCLI(app_)
    Babel(app_)
    FlaskCeleryExt(app_)
    InvenioDB(app_)
    Breadcrumbs(app_)
    InvenioAccounts(app_)
    InvenioAccess(app_)
    app_.register_blueprint(accounts_blueprint)
    InvenioAssets(app_)
    InvenioJSONSchemas(app_)
    InvenioSearch(app_)
    InvenioRecords(app_)
    app_.url_map.converters['pid'] = PIDConverter
    InvenioRecordsREST(app_)
    InvenioPIDStore(app_)
    InvenioIndexer(app_)
    InvenioDeposit(app_)
    InvenioSearchUI(app_)
    InvenioRecordsUI(app_)
    InvenioFilesREST(app_)
    OAuth2Provider(app_)
    InvenioOAuth2Server(app_)
    InvenioOAuth2ServerREST(app_)
    app_.register_blueprint(oauth2server_settings_blueprint)
    InvenioDepositREST(app_)

    with app_.app_context():
        yield app_

    shutil.rmtree(instance_path)
예제 #19
0
def app():
    """Flask application fixture."""
    app_ = Flask(__name__)
    app_.config.update(
        TESTING=True,
        JSONSCHEMAS_HOST='cds.cern.ch',
    )

    app_.config.update(TESTING=True)
    InvenioJSONSchemas(app_)
    return app_
예제 #20
0
def app(instance_path, config):
    """Flask application fixture."""
    app = Flask('testapp', instance_path=instance_path)
    app.config.update(config)
    InvenioDB(app)
    InvenioAccounts(app)
    InvenioJSONSchemas(app)
    InvenioSIPStore(app)

    with app.app_context():
        yield app
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())
예제 #23
0
def app(request):
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()

    app_ = Flask(__name__, instance_path=instance_path)
    app_.config.update(
        SQLALCHEMY_DATABASE_URI=os.getenv(
            'SQLALCHEMY_DATABASE_URI',
            'postgresql+psycopg2://localhost/circulation_test'),
        OAUTH2SERVER_CLIENT_ID_SALT_LEN=40,
        OAUTH2SERVER_CLIENT_SECRET_SALT_LEN=60,
        OAUTH2SERVER_TOKEN_PERSONAL_SALT_LEN=60,
        SECRET_KEY='changeme',
        SERVER_NAME='localhost:5000',
        REPLACE_REFS=False,
        TESTING=True,
        CIRCULATION_ACTION_LOAN_URL=(
            '/hooks/receivers/circulation_loan/events/'),
        CIRCULATION_ACTION_REQUEST_URL=(
            '/hooks/receivers/circulation_request/events/'),
        CIRCULATION_ACTION_RETURN_URL=(
            '/hooks/receivers/circulation_return/events/'),
    )

    app_.url_map.converters['pid'] = PIDConverter

    Babel(app_)
    Menu(app_)
    Breadcrumbs(app_)
    InvenioAccounts(app_)
    InvenioAssets(app_)
    InvenioDB(app_)
    InvenioIndexer(app_)
    InvenioJSONSchemas(app_)
    InvenioPIDStore(app_)
    InvenioRecords(app_)
    InvenioRecordsREST(app_)
    InvenioWebhooks(app_)
    InvenioOAuth2Server(app_)
    InvenioCirculation(app_)
    InvenioCirculationREST(app_)
    InvenioSearch(app_)

    app_.register_blueprint(server_blueprint)
    app_.register_blueprint(settings_blueprint)
    app_.register_blueprint(webhooks_blueprint)
    app_.register_blueprint(circulation_blueprint)

    with app_.app_context():
        yield app_

    shutil.rmtree(instance_path)
예제 #24
0
def base_app(base_app):
    """Flask base application fixture."""
    base_app.url_map.converters['pid'] = PIDConverter
    InvenioDB(base_app)
    InvenioRecords(base_app)
    InvenioRecordsREST(base_app)
    InvenioPIDStore(base_app)
    InvenioIndexer(base_app)
    InvenioPIDStore(base_app)
    InvenioSearch(base_app)
    InvenioCirculation(base_app)
    InvenioJSONSchemas(base_app)
    base_app.register_blueprint(create_blueprint_from_app(base_app))
    yield base_app
예제 #25
0
def app(base_app):
    """Flask application fixture."""
    base_app._internal_jsonschemas = InvenioJSONSchemas(base_app)
    InvenioREST(base_app)
    InvenioRecordsREST(base_app)
    InvenioRecords(base_app)
    InvenioPIDStore(base_app)
    base_app.url_map.converters['pid'] = PIDConverter
    base_app.register_blueprint(create_blueprint_from_app(base_app))

    app_loaded.send(None, app=base_app)

    with base_app.app_context():
        yield base_app
def testapp(base_app, database):
    """Application with just a database.

    Pytest-Invenio also initialises ES with the app fixture.
    """
    location_obj = Location(
        name="marctest-location", uri=tempfile.mkdtemp(), default=True
    )

    database.session.add(location_obj)
    database.session.commit()
    InvenioRecords(base_app)
    InvenioJSONSchemas(base_app)
    yield base_app
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
예제 #28
0
def test_jsonresolver():
    """Test extension initialization."""
    app = Flask('testapp')
    InvenioJSONSchemas(app)
    assert 'invenio-jsonschemas' in app.extensions
    with app.app_context():
        json_resolver = JSONResolver(
            plugins=['invenio_jsonschemas.jsonresolver', ])
        schema = {'$ref': 'http://localhost/schemas/some_schema.json'}
        resolver_cls = ref_resolver_factory(json_resolver)
        resolver = resolver_cls.from_schema(schema)
        with pytest.raises(ValidationError) as exc_info:
            validate({'foo': 'foo_value', 'bar': "not_an_int"}, schema,
                     resolver=resolver)
        assert exc_info.value.schema == {'type': 'integer'}
def app(od_licenses_json):
    """Flask application fixture."""
    instance_path = tempfile.mkdtemp()
    app = Flask('testapp', instance_path=instance_path)
    app.config.update(
        JSONSCHEMAS_HOST='localhost',
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               'sqlite://'),
        TESTING=True,
        RECORDS_REST_DEFAULT_READ_PERMISSION_FACTORY=None,
        CELERY_ALWAYS_EAGER=True,
        CELERY_RESULT_BACKEND="cache",
        CELERY_CACHE_BACKEND="memory",
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
    )

    app.url_map.converters['pid'] = PIDConverter

    FlaskCeleryExt(app)
    InvenioDB(app)
    InvenioJSONSchemas(app)
    InvenioRecords(app)
    InvenioIndexer(app)
    InvenioPIDStore(app)
    InvenioOpenDefinition(app)

    with app.app_context():
        if str(db.engine.url) != "sqlite://" and \
           not database_exists(str(db.engine.url)):
            create_database(str(db.engine.url))

        # MySQL has case-sensitivity issues with its default collation. To fix
        # this, we alter the created database's charset and collation.
        if str(db.engine.url).startswith('mysql'):
            conn = db.engine.connect()
            conn.execute('COMMIT')  # close the current transaction
            conn.execute('ALTER DATABASE invenio '
                         'CHARACTER SET utf8 COLLATE utf8_bin')
            conn.close()
        db.drop_all()
        db.create_all()

    def teardown():
        drop_database(str(db.engine.url))

    yield app

    shutil.rmtree(instance_path)
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_redefine(app, dir_factory):
    ext = InvenioJSONSchemas(app, entry_point_group=None)
    schema_files = build_schemas(1)
    with dir_factory(schema_files) as dir1, \
            dir_factory(schema_files) as dir2:
        ext.register_schemas_dir(dir1)
        # register schemas from a directory which have the same relative
        # paths
        with pytest.raises(JSONSchemaDuplicate) as exc_info:
            ext.register_schemas_dir(dir2)
        assert exc_info.value.schema in schema_files.keys()
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)
예제 #33
0
def app(base_app):
    """Flask application fixture."""

    base_app._internal_jsonschemas = InvenioJSONSchemas(base_app)

    InvenioREST(base_app)
    InvenioRecordsREST(base_app)

    InvenioRecords(base_app)
    InvenioPIDStore(base_app)
    base_app.url_map.converters['pid'] = PIDConverter
    SampleExt(base_app)
    OARepoMappingIncludesExt(base_app)
    LoginManager(base_app)
    Permission(base_app)
    InvenioAccess(base_app)
    Principal(base_app)
    OARepoValidate(base_app)
    Actions(base_app)
    base_app.register_blueprint(
        invenio_records_rest.views.create_blueprint_from_app(base_app))
    login_manager = LoginManager()
    login_manager.init_app(base_app)
    login_manager.login_view = 'login'

    @login_manager.user_loader
    def basic_user_loader(user_id):
        user_obj = User.query.get(int(user_id))
        return user_obj

    @base_app.route('/test/login/<int:id>', methods=['GET', 'POST'])
    def test_login(id):
        print("test: logging user with id", id)
        response = make_response()
        user = User.query.get(id)
        login_user(user)
        set_identity(user)
        return response

    app_loaded.send(None, app=base_app)

    with base_app.app_context():
        yield base_app
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_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)
예제 #36
0
 def init_app(app_):
     app_.config.update(
         CELERY_ALWAYS_EAGER=False,
         CELERY_CACHE_BACKEND="memory",
         CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
         CELERY_RESULT_BACKEND="cache",
         JSONSCHEMAS_URL_SCHEME="http",
         SECRET_KEY="CHANGE_ME",
         SECURITY_PASSWORD_SALT="CHANGE_ME_ALSO",
         SQLALCHEMY_DATABASE_URI=os.environ.get("SQLALCHEMY_DATABASE_URI",
                                                "sqlite:///test.db"),
         SQLALCHEMY_TRACK_MODIFICATIONS=True,
         SQLALCHEMY_ECHO=False,
         TESTING=True,
         WTF_CSRF_ENABLED=False,
         DEPOSIT_SEARCH_API="/api/search",
         SECURITY_PASSWORD_HASH="plaintext",
         SECURITY_PASSWORD_SCHEMES=["plaintext"],
         SECURITY_DEPRECATED_PASSWORD_SCHEMES=[],
         THEME_SITENAME="Test Site",
         OAUTHLIB_INSECURE_TRANSPORT=True,
         OAUTH2_CACHE_TYPE="simple",
         ACCOUNTS_JWT_ENABLE=False,
         # This allows access to files across all of invenio-files-rest
         FILES_REST_PERMISSION_FACTORY=lambda *a, **kw: type(
             "Allow", (object, ), {"can": lambda self: True})(),
         FILES_REST_MULTIPART_CHUNKSIZE_MIN=10,
     )
     Babel(app_)
     FlaskCeleryExt(app_)
     Breadcrumbs(app_)
     OAuth2Provider(app_)
     InvenioDB(app_)
     InvenioAccounts(app_)
     InvenioAccess(app_)
     InvenioIndexer(app_)
     InvenioJSONSchemas(app_)
     InvenioOAuth2Server(app_)
     InvenioPIDStore(app_)
     InvenioRecords(app_)
     search = InvenioSearch(app_)
     search.register_mappings("deposits", "invenio_deposit.mappings")
def base_app(instance_path, editor_config):
    """Flask application fixture."""
    app = Flask("testapp", instance_path=instance_path)

    app.config.update(
        SECRET_KEY="SECRET_KEY", TESTING=True, SERVER_NAME="localhost"
    )
    app.config.update(RECORDS_EDITOR_UI_CONFIG=editor_config)

    InvenioAccounts(app)
    InvenioAssets(app)
    InvenioJSONSchemas(app)
    InvenioRecordsEditor(app)
    Menu(app)

    from invenio_accounts.views.settings import blueprint
    app.register_blueprint(blueprint)

    app.register_blueprint(create_editor_blueprint(app))
    return app
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_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_cache(app, dir_factory):
    """Test cached schema loading."""
    m = mock_open
    with mock.patch('invenio_jsonschemas.ext.open', m):
        ext = InvenioJSONSchemas(app, entry_point_group=None)
        schema_files = build_schemas(1)

        with dir_factory(schema_files) as directory:
            ext.register_schemas_dir(directory)
            assert m.counter == 0
            ext.get_schema('rootschema_1.json')
            assert m.counter == 1
            ext.get_schema('rootschema_1.json')
            ext.get_schema('rootschema_1.json')
            assert m.counter == 1
            ext.get_schema('sub1/subschema_1.json')
            assert m.counter == 2
            ext.get_schema('sub1/subschema_1.json')
            assert m.counter == 2
예제 #41
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()