Example #1
0
def cds_jsonresolver(app):
    """Configure a jsonresolver for cds-dojson."""
    resolver = JSONResolver(plugins=['demo.json_resolver'])
    app.extensions['invenio-records'].ref_resolver_cls = ref_resolver_factory(
        resolver)
    app.extensions['invenio-records'].loader_cls = json_loader_factory(
        resolver)
Example #2
0
def test_record_replace_refs(app):
    """Test the replacement of JSON references using JSONResolver."""
    with app.app_context():
        record = Record.create({
            'one': {'$ref': 'http://nest.ed/A'},
            'three': {'$ref': 'http://nest.ed/ABC'}
        })
        app.extensions['invenio-records'].loader_cls = json_loader_factory(
            JSONResolver(plugins=['demo.json_resolver']))
        out_json = record.replace_refs()
        expected_json = {
            'one': {
                'letter': 'A',
                'next': '.',
            },
            'three': {
                'letter': 'A',
                'next': {
                    'letter': 'B',
                    'next': {
                        'letter': 'C',
                        'next': '.',
                    },
                },
            },
        }
        assert out_json == expected_json
Example #3
0
def test_funders_json_resolving(app, db):
    """Test the loadef for the FundRef dataset."""
    # Test loading the real FundRef dataset.
    # 'grant': {'$ref': 'https://zenodo.org/funders/10.19/11/grants/22'}
    load_funders_testdata()  # Load test data
    example_funder = {
        'doi': 'http://dx.doi.org/10.13039/003',
        'name': 'Some funder',
        'acronyms': [
            'SF',
        ],
        'parent': {
            '$ref': 'http://dx.doi.org/10.13039/002'
        },
        'country': "US",
    }
    json_resolver = JSONResolver(plugins=[
        'invenio_openaire.resolvers.funders',
        'invenio_openaire.resolvers.grants'
    ])
    loader_cls = json_loader_factory(json_resolver)
    loader = loader_cls()
    print(PID.query.all())
    out_json = JsonRef.replace_refs(example_funder, loader=loader)
    assert out_json['parent']['name'] == 'Department of Bar'
    assert out_json['parent']['parent']['name'] == 'University of Foo'
Example #4
0
 def __init__(self, app, entry_point_group=None):
     """Initialize state."""
     self.app = app
     self.app.register_blueprint(blueprint)
     self.resolver = JSONResolver(entry_point_group=entry_point_group)
     self.ref_resolver_cls = ref_resolver_factory(self.resolver)
     self.loader_cls = json_loader_factory(self.resolver)
Example #5
0
def test_record_replace_refs(app, db):
    """Test the replacement of JSON references using JSONResolver."""
    record = Record.create({
        'one': {
            '$ref': 'http://nest.ed/A'
        },
        'three': {
            '$ref': 'http://nest.ed/ABC'
        }
    })
    app.extensions['invenio-records'].loader_cls = json_loader_factory(
        JSONResolver(plugins=['demo.json_resolver']))
    out_json = record.replace_refs()
    expected_json = {
        'one': {
            'letter': 'A',
            'next': '.',
        },
        'three': {
            'letter': 'A',
            'next': {
                'letter': 'B',
                'next': {
                    'letter': 'C',
                    'next': '.',
                },
            },
        },
    }
    assert out_json == expected_json
Example #6
0
def test_record_replace_refs(app, db):
    """Test the replacement of JSON references using JSONResolver."""
    record = Record.create({"one": {"$ref": "http://nest.ed/A"}, "three": {"$ref": "http://nest.ed/ABC"}})
    app.extensions["invenio-records"].loader_cls = json_loader_factory(JSONResolver(plugins=["demo.json_resolver"]))
    out_json = record.replace_refs()
    expected_json = {
        "one": {"letter": "A", "next": "."},
        "three": {"letter": "A", "next": {"letter": "B", "next": {"letter": "C", "next": "."}}},
    }
    assert out_json == expected_json
def test_grants_json_resolving(app):
    """Test the loadef for the FundRef dataset."""
    with app.app_context():
        load_grants_testdata()
        grant_ref = {'$ref': 'http://inveniosoftware.org/grants/'
                             '10.13039/501100000923::DP0667033'}
        json_resolver = JSONResolver(
                plugins=['invenio_openaire.resolvers.grants'])
        loader_cls = json_loader_factory(json_resolver)
        loader = loader_cls()
        data = JsonRef.replace_refs(grant_ref, loader=loader)
        assert data['title'].startswith('Dispersal and colonisation')
Example #8
0
    def __init__(self, app, entry_point_group=None):
        """Initialize state."""
        self.app = app
        self.resolver = JSONResolver(entry_point_group=entry_point_group)
        self.refresolver_cls = ref_resolver_factory(self.resolver)
        self.refresolver_store = None
        if (self.app.config.get("RECORDS_REFRESOLVER_CLS")):
            self.refresolver_cls = obj_or_import_string(
                self.app.config.get("RECORDS_REFRESOLVER_CLS"), )
            self.refresolver_store = obj_or_import_string(
                self.app.config.get("RECORDS_REFRESOLVER_STORE"))

        self.loader_cls = json_loader_factory(self.resolver)
def test_same_route_different_hosts():
    """Test orignal resolver."""
    example = {
        'host1': {'$ref': 'http://localhost:4000/test'},
        'host2': {'$ref': 'http://inveniosoftware.org/test'},
    }

    json_resolver = JSONResolver(plugins=['demo.simple', 'demo.hosts'])
    loader_cls = json_loader_factory(json_resolver)
    loader = loader_cls()
    data = JsonRef.replace_refs(example, loader=loader)
    assert data['host1']['test'] == 'test'
    assert data['host2']['test'] == 'inveniosoftware.org'
def test_grants_json_resolving(app, db):
    """Test the loadef for the FundRef dataset."""
    load_grants_testdata()
    grant_ref = {"$ref": "http://inveniosoftware.org/grants/" "10.13039/501100000923::DP0667033"}
    json_resolver = JSONResolver(plugins=["invenio_openaire.resolvers.grants"])
    loader_cls = json_loader_factory(json_resolver)
    loader = loader_cls()
    data = JsonRef.replace_refs(grant_ref, loader=loader)
    assert data["title"].startswith("Dispersal and colonisation")

    # Invalid grant reference
    grant_ref = {"$ref": "http://inveniosoftware.org/grants/" "10.13039/invalid"}
    data = JsonRef.replace_refs(grant_ref, loader=loader)
    pytest.raises(JsonRefError, dict, data)
def test_license_jsonref_resolver(app, loaded_example_licenses):
    """Test resolver."""
    with app.app_context():
        example_license = {
            'license': {'$ref': 'http://inveniosoftware.org/licenses/mit'}
        }

        json_resolver = JSONResolver(plugins=[
            'invenio_opendefinition.resolvers'
        ])
        loader_cls = json_loader_factory(json_resolver)
        loader = loader_cls()
        out_json = JsonRef.replace_refs(example_license, loader=loader)
        assert out_json['license'] == loaded_example_licenses['MIT']
Example #12
0
def test_license_jsonref_resolver(
        app, loaded_example_licenses):
    """Test resolver."""
    with app.app_context():
        example_license = {
            'license': {'$ref': 'http://inveniosoftware.org/licenses/MIT'}
        }

        json_resolver = JSONResolver(plugins=[
            'invenio_opendefinition.resolvers'
        ])
        loader_cls = json_loader_factory(json_resolver)
        loader = loader_cls()
        out_json = JsonRef.replace_refs(example_license, loader=loader)
        assert out_json['license'] == loaded_example_licenses['MIT']
Example #13
0
def test_key_resolver():
    """Test JSONSchema validation with custom reference resolver."""
    example_schema = {
        '$schema': 'http://json-schema.org/draft-04/schema#',
        'type': 'object',
        'properties': {
            'authors': {'$ref': 'http://localhost:4000/schema/authors.json#'}
        },
        'additionalProperties': False,
    }

    json_resolver = JSONResolver(plugins=['demo.schema'])
    loader_cls = json_loader_factory(json_resolver)
    loader = loader_cls()
    data = JsonRef.replace_refs(example_schema, loader=loader)
    assert data['properties']['authors'] == {'type': 'array'}
Example #14
0
def test_replace_refs(app):
    """Test replace refs."""
    app.config['INDEXER_REPLACE_REFS'] = False
    app.extensions['invenio-records'].loader_cls = json_loader_factory(
            JSONResolver(plugins=['demo.json_resolver']))

    with app.app_context():
        record = Record({'$ref': 'http://dx.doi.org/10.1234/foo'})
        data = RecordIndexer._prepare_record(record, 'records', 'record')
        assert '$ref' in data

    app.config['INDEXER_REPLACE_REFS'] = True
    with app.app_context():
        record = Record({'$ref': 'http://dx.doi.org/10.1234/foo'})
        data = RecordIndexer._prepare_record(record, 'records', 'record')
        assert '$ref' not in data
        assert json.dumps(data)
def test_grants_json_resolving(app, db, es, funders):
    """Test the loadef for the FundRef dataset."""
    load_grants_testdata()
    grant_ref = {'$ref': 'http://inveniosoftware.org/grants/'
                         '10.13039/501100000923::DP0667033'}
    json_resolver = JSONResolver(
        plugins=['invenio_openaire.resolvers.grants'])
    loader_cls = json_loader_factory(json_resolver)
    loader = loader_cls()
    data = JsonRef.replace_refs(grant_ref, loader=loader)
    assert data['title'].startswith('Dispersal and colonisation')

    # Invalid grant reference
    grant_ref = {'$ref': 'http://inveniosoftware.org/grants/'
                         '10.13039/invalid'}
    data = JsonRef.replace_refs(grant_ref, loader=loader)
    pytest.raises(JsonRefError, dict, data)
Example #16
0
def test_missing_route():
    """Test orignal resolver."""
    example_schema = {
        '$schema': 'http://json-schema.org/draft-04/schema#',
        'type': 'object',
        'properties': {
            'authors': {'$ref': 'file:///missing.json#'}
        },
        'additionalProperties': False,
    }

    json_resolver = JSONResolver(plugins=['demo.schema'])
    loader_cls = json_loader_factory(json_resolver)
    loader = loader_cls()
    data = JsonRef.replace_refs(example_schema, loader=loader)
    with pytest.raises(JsonRefError):
        data['properties']['authors']['type']
Example #17
0
def test_same_route_different_hosts():
    """Test orignal resolver."""
    example = {
        'host1': {
            '$ref': 'http://localhost:4000/test'
        },
        'host2': {
            '$ref': 'http://inveniosoftware.org/test'
        },
    }

    json_resolver = JSONResolver(plugins=['demo.simple', 'demo.hosts'])
    loader_cls = json_loader_factory(json_resolver)
    loader = loader_cls()
    data = JsonRef.replace_refs(example, loader=loader)
    assert data['host1']['test'] == 'test'
    assert data['host2']['test'] == 'inveniosoftware.org'
Example #18
0
def test_replace_refs(app):
    """Test replace refs."""
    app.config['INDEXER_REPLACE_REFS'] = False
    app.extensions['invenio-records'].loader_cls = json_loader_factory(
            JSONResolver(plugins=['demo.json_resolver']))

    with app.app_context():
        record = Record({'$ref': 'http://dx.doi.org/10.1234/foo'})
        data = RecordIndexer._prepare_record(record, 'records', 'record')
        assert '$ref' in data

    app.config['INDEXER_REPLACE_REFS'] = True
    with app.app_context():
        record = Record({'$ref': 'http://dx.doi.org/10.1234/foo'})
        data = RecordIndexer._prepare_record(record, 'records', 'record')
        assert '$ref' not in data
        assert json.dumps(data)
def test_grants_json_resolving(app, db, es, funders):
    """Test the loadef for the FundRef dataset."""
    load_grants_testdata()
    grant_ref = {'$ref': 'http://inveniosoftware.org/grants/'
                         '10.13039/501100000923::DP0667033'}
    json_resolver = JSONResolver(
        plugins=['invenio_openaire.resolvers.grants'])
    loader_cls = json_loader_factory(json_resolver)
    loader = loader_cls()
    data = JsonRef.replace_refs(grant_ref, loader=loader)
    assert data['title'].startswith('Dispersal and colonisation')

    # Invalid grant reference
    grant_ref = {'$ref': 'http://inveniosoftware.org/grants/'
                         '10.13039/invalid'}
    data = JsonRef.replace_refs(grant_ref, loader=loader)
    pytest.raises(JsonRefError, dict, data)
Example #20
0
def test_key_resolver():
    """Test JSONSchema validation with custom reference resolver."""
    example_schema = {
        '$schema': 'http://json-schema.org/draft-04/schema#',
        'type': 'object',
        'properties': {
            'authors': {
                '$ref': 'http://localhost:4000/schema/authors.json#'
            }
        },
        'additionalProperties': False,
    }

    json_resolver = JSONResolver(plugins=['demo.schema'])
    loader_cls = json_loader_factory(json_resolver)
    loader = loader_cls()
    data = JsonRef.replace_refs(example_schema, loader=loader)
    assert data['properties']['authors'] == {'type': 'array'}
Example #21
0
def test_replace_refs(app):
    """Test replace refs."""
    app.config["INDEXER_REPLACE_REFS"] = False
    app.extensions["invenio-records"].loader_cls = json_loader_factory(
        JSONResolver(plugins=["demo.json_resolver"])
    )

    with app.app_context():
        record = Record({"$ref": "http://dx.doi.org/10.1234/foo"})
        data = RecordIndexer()._prepare_record(record, "records", "record")
        assert "$ref" in data

    app.config["INDEXER_REPLACE_REFS"] = True
    with app.app_context():
        record = Record({"$ref": "http://dx.doi.org/10.1234/foo"})
        data = RecordIndexer()._prepare_record(record, "records", "record")
        assert "$ref" not in data
        assert json.dumps(data)
def test_funders_json_resolving(doi_host, app, db):
    """Test the loadef for the FundRef dataset."""
    # Test loading the real FundRef dataset.
    # 'grant': {'$ref': 'https://zenodo.org/funders/10.19/11/grants/22'}
    load_funders_testdata()  # Load test data
    example_funder = {
        "doi": "http://{doi_host}/10.13039/003".format(doi_host=doi_host),
        "name": "Some funder",
        "acronyms": ["SF"],
        "parent": {"$ref": "http://{doi_host}/10.13039/002".format(doi_host=doi_host)},
        "country": "US",
    }
    json_resolver = JSONResolver(plugins=["invenio_openaire.resolvers.funders", "invenio_openaire.resolvers.grants"])
    loader_cls = json_loader_factory(json_resolver)
    loader = loader_cls()
    print(PID.query.all())
    out_json = JsonRef.replace_refs(example_funder, loader=loader)
    assert out_json["parent"]["name"] == "Department of Bar"
    assert out_json["parent"]["parent"]["name"] == "University of Foo"
Example #23
0
def test_missing_route():
    """Test orignal resolver."""
    example_schema = {
        '$schema': 'http://json-schema.org/draft-04/schema#',
        'type': 'object',
        'properties': {
            'authors': {
                '$ref': 'file:///missing.json#'
            }
        },
        'additionalProperties': False,
    }

    json_resolver = JSONResolver(plugins=['demo.schema'])
    loader_cls = json_loader_factory(json_resolver)
    loader = loader_cls()
    data = JsonRef.replace_refs(example_schema, loader=loader)
    with pytest.raises(JsonRefError):
        data['properties']['authors']['type']
def test_funders_json_resolving(app, db):
    """Test the loadef for the FundRef dataset."""
    # Test loading the real FundRef dataset.
    # 'grant': {'$ref': 'https://zenodo.org/funders/10.19/11/grants/22'}
    load_funders_testdata()  # Load test data
    example_funder = {
        'doi': 'http://dx.doi.org/10.13039/003',
        'name': 'Some funder',
        'acronyms': ['SF', ],
        'parent': {'$ref': 'http://dx.doi.org/10.13039/002'},
        'country': "US",
    }
    json_resolver = JSONResolver(
        plugins=['invenio_openaire.resolvers.funders',
                 'invenio_openaire.resolvers.grants'])
    loader_cls = json_loader_factory(json_resolver)
    loader = loader_cls()
    print(PID.query.all())
    out_json = JsonRef.replace_refs(example_funder, loader=loader)
    assert out_json['parent']['name'] == 'Department of Bar'
    assert out_json['parent']['parent']['name'] == 'University of Foo'
Example #25
0
 def __init__(self, app, entry_point_group=None):
     """Initialize state."""
     self.app = app
     self.resolver = JSONResolver(entry_point_group=entry_point_group)
     self.ref_resolver_cls = ref_resolver_factory(self.resolver)
     self.loader_cls = json_loader_factory(self.resolver)

class DatabaseJsonLoader(AbstractRecordLoader):

    def get_record(self, pid_type, recid):
        try:
            return record_getter.get_db_record(pid_type, recid)
        except record_getter.RecordGetterError:
            return None


es_record_loader = ESJsonLoader()
db_record_loader = DatabaseJsonLoader()
SCHEMA_LOADER_CLS = json_loader_factory(
    jsonresolver.JSONResolver(
        plugins=['invenio_jsonschemas.jsonresolver']
    )
)
"""Used in invenio-jsonschemas to resolve relative $ref."""


def load_resolved_schema(name):
    """Load a JSON schema with all references resolved.

    Args:
        name(str): name of the schema to load.

    Returns:
        dict: the JSON schema with resolved references.

    Examples:
Example #27
0
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016-2018 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""Test helpers."""

from jsonresolver import JSONResolver
from jsonresolver.contrib.jsonref import json_loader_factory

LOADER_CLS = json_loader_factory(
    JSONResolver(plugins=['invenio_jsonschemas.jsonresolver']))
"""Test loader class."""
#: OAuth login template.
# OAUTHCLIENT_LOGIN_USER_TEMPLATE = 'access/login_user.html'

# JSON Schemas
# ============
#: Hostname for JSON Schemas.
JSONSCHEMAS_HOST = os.environ.get('APP_JSONSCHEMAS_HOST', 'localhost:5000')
#: Path to where JSON metadata exist
JSON_METADATA_PATH = "/_metadata"
JSONSCHEMAS_ENDPOINT = '/schemas'

JSONSCHEMAS_RESOLVE_SCHEMA = True

JSONSCHEMAS_LOADER_CLS = json_loader_factory(
    JSONResolver(plugins=[
        'cap.modules.records.resolvers.local',
        'cap.modules.records.resolvers.cap',
    ], ))

# JSONSCHEMAS_VERSIONS = {
#     "ATLASAnalysis": "ATLASAnalysis-v0.0.1",
#     "ATLASWorkflows": "ATLASWorkflows-v0.0.1",
#     "CMSAnalysis": "CMSAnalysis-v0.0.1",
#     "CMSQuestionnaire": "CMSQuestionnaire-v0.0.1",
#     "LHCbAnalysis": "LHCbAnalysis-v0.0.1",
# }

JSONSCHEMAS_ROOT = os.path.join(APP_ROOT, 'jsonschemas')

# directories with jsonschemas
JSONSCHEMAS_DEPOSIT_DIR = 'deposits/records/'
OAUTHCLIENT_REMOTE_APPS = dict(
    cern=CERN_REMOTE_APP,
)


# JSON Schemas
# ============
#: Path to where JSON metadata exist
JSON_METADATA_PATH = "/_metadata"
JSONSCHEMAS_ENDPOINT = '/schemas'

JSONSCHEMAS_RESOLVE_SCHEMA = True

JSONSCHEMAS_LOADER_CLS = json_loader_factory(JSONResolver(
    plugins=[
        'cap.modules.records.resolvers.local',
        'cap.modules.records.resolvers.cap',
    ],
))

JSONSCHEMAS_ROOT = os.path.join(APP_ROOT, 'jsonschemas')

# directories with jsonschemas
JSONSCHEMAS_DEPOSIT_DIR = 'deposits/records/'
JSONSCHEMAS_RECORDS_DIR = 'records/'

# WARNING: Do not share the secret key - especially do not commit it to
# version control.
SECRET_KEY = "changeme"

# Ana's database
LHCB_ANA_DB = 'http://datadependency.cern.ch'
Example #30
0
CERN_REMOTE_APP['signup_handler']['view'] = signup_handler

#: Defintion of OAuth client applications.
OAUTHCLIENT_REMOTE_APPS = dict(cern=CERN_REMOTE_APP, )

# JSON Schemas
# ============
#: Path to where JSON metadata exist
JSON_METADATA_PATH = "/_metadata"
JSONSCHEMAS_ENDPOINT = '/schemas'

JSONSCHEMAS_RESOLVE_SCHEMA = True

JSONSCHEMAS_LOADER_CLS = json_loader_factory(
    JSONResolver(plugins=[
        'cap.modules.schemas.resolvers', 'cap.modules.schemas.resolvers_api'
    ], ))

# WARNING: Do not share the secret key - especially do not commit it to
# version control.
SECRET_KEY = "changeme"

# LHCb
# ========
#: Ana's database
LHCB_ANA_DB = 'http://datadependency.cern.ch'
LHCB_GETCOLLISIONDATA_URL = '{0}/getRecoStripSoft?propass='******'{0}/getPlatform?app='.format(LHCB_ANA_DB)

# CMS
# ========