def test_jsonresolver_no_taxonomy_term(app):
    assert app.config.get('TAXONOMY_SERVER_NAME') == 'taxonomy-server.com'
    resolver = JSONResolver()
    resolver.pm.register(jsonresolver, 'taxonomies-resolver')
    with pytest.raises(ValueError, match='The taxonomy term does not exist.'):
        resolver.resolve(
            'https://taxonomy-server.com/api/taxonomies/code/slug')
def test_referenced_taxonomy_term(db, root_taxonomy, mkt):
    """Get terms  listassocitated with this taxonomy."""

    leaf = root_taxonomy.create_term(slug="leaf")
    nested = leaf.create_term(slug="nested")

    db.session.refresh(root_taxonomy)
    db.session.refresh(leaf)
    db.session.refresh(nested)

    jsonRes = JSONResolver(entry_point_group='invenio_records.jsonresolver')
    document = jsonRes.resolve(
        "http://taxonomy-server.com/api/taxonomies/root/leaf")
    assert document == {
        'descendants_count': 1.0,
        'id': leaf.id,
        'links': {
            'parent': 'http://localhost/taxonomies/root/',
            'parent_tree': 'http://localhost/taxonomies/root/?drilldown=True',
            'self': 'http://localhost/taxonomies/root/leaf/',
            'tree': 'http://localhost/taxonomies/root/leaf/?drilldown=True'
        },
        'path': '/leaf',
        'slug': 'leaf',
        'level': 1
    }
def test_jsonresolver_before_signal(app):
    assert app.config.get('TAXONOMY_SERVER_NAME') == 'taxonomy-server.com'

    def before(source, code=None, slug=None, **kwargs):
        return {'code': code, 'slug': slug}

    before_taxonomy_jsonresolve.connect(before)
    resolver = JSONResolver()
    resolver.pm.register(jsonresolver, 'taxonomies-resolver')
    assert resolver.resolve(
        'https://taxonomy-server.com/api/taxonomies/code/slug') == {
            'code': 'code',
            'slug': 'slug'
        }
예제 #4
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
예제 #5
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)
예제 #6
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)
예제 #7
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'
예제 #8
0
def json_resolver(schema):
    """Test ``json_resolver``."""
    json_resolver = JSONResolver(plugins=[
        'demo.json_resolver',
    ])
    resolver_cls = ref_resolver_factory(json_resolver)
    return resolver_cls.from_schema(schema)
예제 #9
0
def test_plugin_lazy_execution_hooks():
    """Test the lazy evaluation of loaded plugin through hooks."""
    # Plugin code should be called (i.e. raise exception) on resolve method
    resolver = JSONResolver(plugins=['demo.raising_hook'])
    with pytest.raises(HookRegistrationDetected) as exc_info:
        resolver.resolve('http://localhost:4000/test')
    assert exc_info.type is HookRegistrationDetected

    # Same as above but loaded using entry point
    resolver = JSONResolver(entry_point_group='raising_hook')
    with pytest.raises(HookRegistrationDetected) as exc_info:
        resolver.resolve('http://localhost:4000/test')
    assert exc_info.type is HookRegistrationDetected
예제 #10
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)
예제 #11
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'}
예제 #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']
예제 #13
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)
예제 #14
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'
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)
예제 #16
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)
예제 #17
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'}
예제 #18
0
def test_plugin_lazy_execution_hooks():
    """Test the lazy evaluation of loaded plugin through hooks."""
    # Plugin code should be called (i.e. raise exception) on resolve method
    resolver = JSONResolver(plugins=['demo.raising_hook'])
    with pytest.raises(HookRegistrationDetected) as exc_info:
        resolver.resolve('http://localhost:4000/test')
    assert exc_info.type is HookRegistrationDetected

    # Same as above but loaded using entry point
    resolver = JSONResolver(entry_point_group='raising_hook')
    with pytest.raises(HookRegistrationDetected) as exc_info:
        resolver.resolve('http://localhost:4000/test')
    assert exc_info.type is HookRegistrationDetected
def jsonschema(collection):
    collection = JSONSCHEMAS_VERSIONS.get(collection, collection)

    jsonschema_path = os.path.join(os.path.dirname(__file__), 'jsonschemas',
                                   'records', '{0}.json'.format(collection))
    json_resolver = JSONResolver(
        plugins=['cap.modules.records.resolvers.jsonschemas'])

    with open(jsonschema_path) as file:
        jsonschema_content = file.read()

    try:
        result = JsonRef.replace_refs(json.loads(
            jsonschema_content), loader=json_resolver.resolve)
        ren = render_template(
            'records/deposit-base-extended.html', schema=json.dumps(result, indent=4))
        return jsonify(json.loads(ren))
    except:
        return jsonify(json.loads(render_template('records/deposit-base-refs.html', collection=collection)))
예제 #20
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,
    }
    data = {'authors': [{'name': 'Ellis'}]}

    json_resolver = JSONResolver(plugins=['demo.schema'])
    resolver_cls = ref_resolver_factory(json_resolver)
    resolver = resolver_cls.from_schema(example_schema)
    with pytest.raises(RefResolutionError):
        list(validate(data, example_schema, resolver=resolver))
예제 #21
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 jsonschema_deposit(collection):
    collection = JSONSCHEMAS_VERSIONS.get(collection, collection)

    jsonschema_path = os.path.join(os.path.dirname(__file__), 'jsonschemas',
                                   'records', '{0}.json'.format(collection))
    json_resolver = JSONResolver(
        plugins=['cap.modules.records.resolvers.jsonschemas'])

    with open(jsonschema_path) as file:
        jsonschema_content = file.read()

    try:
        result = JsonRef.replace_refs(json.loads(
            jsonschema_content), loader=json_resolver.resolve)
        return jsonify(result)
    except:
        jsonschema_path = os.path.join(os.path.dirname(__file__), 'jsonschemas_gen',
                                       'records', '{0}.json'.format(collection))
        with open(jsonschema_path) as file:
            jsonschema_content = json.loads(file.read())

        return jsonify(jsonschema_content)
예제 #23
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,
    }
    data = {'authors': [{'name': 'Ellis'}]}

    json_resolver = JSONResolver(plugins=['demo.schema'])
    resolver_cls = ref_resolver_factory(json_resolver)
    resolver = resolver_cls.from_schema(example_schema)
    assert validate(data, example_schema, resolver=resolver) is None

    resolver = resolver_cls.from_schema(
        example_schema['properties']['authors'])
    assert validate(data['authors'],
                    example_schema['properties']['authors'],
                    resolver=resolver) is None
예제 #24
0
def test_plugins():
    """Test the plugins loading."""
    resolver = JSONResolver(plugins=['demo.simple'])
    assert resolver.resolve('http://localhost:4000/test') == {'test': 'test'}
예제 #25
0
def test_redis(db, root_taxonomy, mkt):
    """Get terms  listassocitated with this taxonomy."""

    leaf = root_taxonomy.create_term(slug="leaf")
    nested = leaf.create_term(slug="nested")

    db.session.refresh(root_taxonomy)
    db.session.refresh(leaf)
    db.session.refresh(nested)

    jsonRes = JSONResolver(entry_point_group='invenio_records.jsonresolver')
    document = jsonRes.resolve("http://taxonomy-server.com/api/taxonomies/root/leaf")
    assert document == {
        'descendants_count': 1.0,
        'id': leaf.id,
        'links': {'parent': 'http://localhost/taxonomies/root/',
                  'parent_tree': 'http://localhost/taxonomies/root/?drilldown=True',
                  'self': 'http://localhost/taxonomies/root/leaf/',
                  'tree': 'http://localhost/taxonomies/root/leaf/?drilldown=True'},
        'path': '/leaf',
        'slug': 'leaf',
        'level': 1
    }

    # change the document directly in the database
    leaf.extra_data = {
        'title': 'a title'
    }
    db.session.add(leaf)
    db.session.commit()

    # the document must be the original one - coming from redis
    document = jsonRes.resolve("http://taxonomy-server.com/api/taxonomies/root/leaf")
    assert document == {
        'descendants_count': 1.0,
        'id': leaf.id,
        'links': {'parent': 'http://localhost/taxonomies/root/',
                  'parent_tree': 'http://localhost/taxonomies/root/?drilldown=True',
                  'self': 'http://localhost/taxonomies/root/leaf/',
                  'tree': 'http://localhost/taxonomies/root/leaf/?drilldown=True'},
        'path': '/leaf',
        'slug': 'leaf',
        'level': 1
    }

    # change the document via api
    current_flask_taxonomies.update_term(root_taxonomy, leaf, {
        'extra_data': {'title': 'a title'}
    })
    document = jsonRes.resolve("http://taxonomy-server.com/api/taxonomies/root/leaf")
    assert document == {
        'descendants_count': 1.0,
        'id': leaf.id,
        'title': 'a title',
        'links': {'parent': 'http://localhost/taxonomies/root/',
                  'parent_tree': 'http://localhost/taxonomies/root/?drilldown=True',
                  'self': 'http://localhost/taxonomies/root/leaf/',
                  'tree': 'http://localhost/taxonomies/root/leaf/?drilldown=True'},
        'path': '/leaf',
        'slug': 'leaf',
        'level': 1
    }
예제 #26
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/'
JSONSCHEMAS_RECORDS_DIR = 'records/'
예제 #28
0
def test_plugins():
    resolver = JSONResolver(plugins=['demo.simple'])
    assert resolver.resolve('http://localhost:4000/test') == {'test': 'test'}
예제 #29
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)
예제 #30
0
def test_entry_point_group():
    resolver = JSONResolver(entry_point_group='espresso')
    assert resolver.resolve('http://localhost:4000/test') == {'test': 'test'}
예제 #31
0
def test_entry_point_group():
    """Test the entry point group loading."""
    resolver = JSONResolver(entry_point_group='espresso')
    assert resolver.resolve('http://localhost:4000/test') == {'test': 'test'}
예제 #32
0
def test_plugins():
    """Test the plugins loading."""
    resolver = JSONResolver(plugins=['demo.simple'])
    assert resolver.resolve('http://localhost:4000/test') == {'test': 'test'}
예제 #33
0
def test_entry_point_group():
    """Test the entry point group loading."""
    resolver = JSONResolver(entry_point_group='espresso')
    assert resolver.resolve('http://localhost:4000/test') == {'test': 'test'}
예제 #34
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
# ========