Example #1
0
def load_records(app, filename, schema, tries=5):
    """Try to index records."""
    indexer = RecordIndexer()
    records = []
    with app.app_context():
        with mock.patch('invenio_records.api.Record.validate',
                        return_value=None):
            data_filename = pkg_resources.resource_filename(
                'invenio_records', filename)
            records_data = load(data_filename)
            with db.session.begin_nested():
                for item in records_data:
                    record_id = uuid.uuid4()
                    item_dict = dict(marc21.do(item))
                    item_dict['$schema'] = schema
                    recid_minter(record_id, item_dict)
                    oaiid_minter(record_id, item_dict)
                    record = Record.create(item_dict, id_=record_id)
                    indexer.index(record)
                    records.append(record.id)
            db.session.commit()

        # Wait for indexer to finish
        for i in range(tries):
            response = current_search_client.search()
            if response['hits']['total'] >= len(records):
                break
            current_search.flush_and_refresh('_all')

    return records
def test_getrecord(app):
    schema = {
        "type": "object",
        "properties": {"title": {"type": "string"}, "field": {"type": "boolean"}},
        "required": ["title"],
    }
    with app.test_request_context():
        with db.session.begin_nested():
            record = Record.create({"title": "Test0", "$schema": schema}).model
            recid_minter(record.id, record.json)
            pid = oaiid_minter(record.id, record.json)

        db.session.commit()

        pid_value = pid.pid_value
        pid_updated = pid.updated

        with app.test_client() as c:
            result = c.get("/oai2d?verb=GetRecord&identifier={0}&metadataPrefix=oai_dc".format(pid_value))
            assert 200 == result.status_code

            tree = etree.fromstring(result.data)

            namespaces = {"x": NS_OAIPMH}
            assert len(tree.xpath("/x:OAI-PMH", namespaces=namespaces)) == 1
            assert len(tree.xpath("/x:OAI-PMH/x:GetRecord", namespaces=namespaces)) == 1
            assert len(tree.xpath("/x:OAI-PMH/x:GetRecord/x:header", namespaces=namespaces)) == 1
            assert len(tree.xpath("/x:OAI-PMH/x:GetRecord/x:header/x:identifier", namespaces=namespaces)) == 1
            identifier = tree.xpath("/x:OAI-PMH/x:GetRecord/x:header/x:identifier/text()", namespaces=namespaces)
            assert identifier == [str(record.id)]
            datestamp = tree.xpath("/x:OAI-PMH/x:GetRecord/x:header/x:datestamp/text()", namespaces=namespaces)
            assert datestamp == [datetime_to_datestamp(pid_updated)]
            assert len(tree.xpath("/x:OAI-PMH/x:GetRecord/x:metadata", namespaces=namespaces)) == 1
Example #3
0
def load_records(app, filename, schema, tries=5):
    """Try to index records."""
    indexer = RecordIndexer()
    records = []
    with app.app_context():
        with mock.patch('invenio_records.api.Record.validate',
                        return_value=None):
            data_filename = pkg_resources.resource_filename(
                'invenio_records', filename)
            records_data = load(data_filename)
            with db.session.begin_nested():
                for item in records_data:
                    record_id = uuid.uuid4()
                    item_dict = dict(marc21.do(item))
                    item_dict['$schema'] = schema
                    recid_minter(record_id, item_dict)
                    oaiid_minter(record_id, item_dict)
                    record = Record.create(item_dict, id_=record_id)
                    indexer.index(record)
                    records.append(record.id)
            db.session.commit()

        # Wait for indexer to finish
        for i in range(tries):
            response = current_search_client.search()
            if response['hits']['total'] >= len(records):
                break
            sleep(5)

    return records
Example #4
0
def oaiserver(number):
    """Initialize OAI-PMH server."""
    from invenio_db import db
    from invenio_oaiserver.models import OAISet
    from invenio_records.api import Record

    # create a OAI Set
    with db.session.begin_nested():
        for i in range(number):
            db.session.add(OAISet(
                spec='test{0}'.format(i),
                name='Test{0}'.format(i),
                description='test desc {0}'.format(i),
                search_pattern='title:Test{0}'.format(i),
            ))

    # create a record
    schema = {
        'type': 'object',
        'properties': {
            'title': {'type': 'string'},
            'field': {'type': 'boolean'},
        },
        'required': ['title'],
    }

    with db.session.begin_nested():
        for i in range(number):
            record_id = uuid.uuid4()
            data = {'title': 'Test{0}'.format(i), '$schema': schema}
            recid_minter(record_id, data)
            oaiid_minter(record_id, data)
            Record.create(data, id_=record_id)

    db.session.commit()
Example #5
0
def test_listmetadataformats_record(app):
    """Test ListMetadataFormats for a record."""
    schema = {
        'type': 'object',
        'properties': {
            'title': {
                'type': 'string'
            },
            'field': {
                'type': 'boolean'
            },
        },
        'required': ['title'],
    }
    with app.test_request_context():
        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title': 'Test0', '$schema': schema}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            Record.create(data, id_=record_id)
            pid_value = pid.pid_value

        db.session.commit()

    _listmetadataformats(
        app=app,
        query='/oai2d?verb=ListMetadataFormats&identifier={0}'.format(
            pid_value))
def load_records(app, filename, schema, tries=5):
    """Try to index records."""
    indexer = RecordIndexer()
    records = []
    with app.app_context():
        with mock.patch("invenio_records.api.Record.validate",
                        return_value=None):
            data_filename = pkg_resources.resource_filename(
                "invenio_records", filename)
            records_data = load(data_filename)
            with db.session.begin_nested():
                for item in records_data:
                    record_id = uuid.uuid4()
                    item_dict = dict(marc21.do(item))
                    item_dict["$schema"] = schema
                    recid_minter(record_id, item_dict)
                    oaiid_minter(record_id, item_dict)
                    record = current_oaiserver.record_cls.create(item_dict,
                                                                 id_=record_id)
                    indexer.index(record)
                    records.append(record.id)
            db.session.commit()

        # Wait for indexer to finish
        for i in range(tries):
            response = current_search_client.search()
            if response["hits"]["total"] >= len(records):
                break
            current_search.flush_and_refresh("_all")

    return records
Example #7
0
def test_listmetadataformats_record(app):
    """Test ListMetadataFormats for a record."""
    schema = {
        'type': 'object',
        'properties': {
            'title': {'type': 'string'},
            'field': {'type': 'boolean'},
        },
        'required': ['title'],
    }
    with app.test_request_context():
        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title': 'Test0', '$schema': schema}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            Record.create(data, id_=record_id)
            pid_value = pid.pid_value

        db.session.commit()

    _listmetadataformats(
        app=app,
        query='/oai2d?verb=ListMetadataFormats&identifier={0}'.format(
            pid_value))
 def create_record(item_dict):
     """Create test record."""
     record_id = uuid.uuid4()
     recid_minter(record_id, item_dict)
     oaiid_minter(record_id, item_dict)
     record = Record.create(item_dict, id_=record_id)
     indexer.index(record)
     return record
Example #9
0
def test_listrecords(app):
    """Test ListRecords."""
    schema = {
        'type': 'object',
        'properties': {
            'title': {
                'type': 'string'
            },
            'field': {
                'type': 'boolean'
            },
        },
        'required': ['title'],
    }
    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title': 'Test0', '$schema': schema}
            recid_minter(record_id, data)
            oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        sleep(2)

        with app.test_client() as c:
            result = c.get('/oai2d?verb=ListRecords&metadataPrefix=oai_dc')

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1

        assert len(
            tree.xpath('/x:OAI-PMH/x:ListRecords', namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                       namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header',
                       namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath(
                '/x:OAI-PMH/x:ListRecords/x:record/x:header'
                '/x:identifier',
                namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath(
                '/x:OAI-PMH/x:ListRecords/x:record/x:header'
                '/x:datestamp',
                namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:metadata',
                       namespaces=NAMESPACES)) == 1
 def create_record(item_dict, mint_oaiid=True):
     """Create test record."""
     with app.test_request_context():
         record_id = uuid.uuid4()
         recid_minter(record_id, item_dict)
         if mint_oaiid:
             oaiid_minter(record_id, item_dict)
         record = Record.create(item_dict, id_=record_id)
         indexer.index(record)
         return record
Example #11
0
def oaiserver(sets, records):
    """Initialize OAI-PMH server."""
    from invenio_db import db
    from invenio_oaiserver.models import OAISet
    from invenio_records.api import Record

    # create a OAI Set
    with db.session.begin_nested():
        for i in range(sets):
            db.session.add(
                OAISet(
                    spec='test{0}'.format(i),
                    name='Test{0}'.format(i),
                    description='test desc {0}'.format(i),
                    search_pattern='title_statement.title:Test{0}'.format(i),
                ))

    # create a record
    schema = {
        'type': 'object',
        'properties': {
            'title_statement': {
                'type': 'object',
                'properties': {
                    'title': {
                        'type': 'string',
                    },
                },
            },
            'field': {
                'type': 'boolean'
            },
        },
    }

    search.client.indices.delete_alias('_all', '_all', ignore=[400, 404])
    search.client.indices.delete('*')

    with app.app_context():
        indexer = RecordIndexer()
        with db.session.begin_nested():
            for i in range(records):
                record_id = uuid.uuid4()
                data = {
                    'title_statement': {
                        'title': 'Test{0}'.format(i)
                    },
                    '$schema': schema,
                }
                recid_minter(record_id, data)
                oaiid_minter(record_id, data)
                record = Record.create(data, id_=record_id)
                indexer.index(record)

        db.session.commit()
def published_record(app, db, schemas, mappings, prepare_es):
    # let's create a record
    record_uuid = uuid.uuid4()
    data = {'title': 'blah', '$schema': schemas['published']}
    recid_minter(record_uuid, data)
    rec = Record.create(data, id_=record_uuid)
    RecordIndexer().index(rec)
    current_search_client.indices.refresh()
    current_search_client.indices.flush()

    return rec
Example #13
0
def create_record(app, item_dict, mint_oaiid=True):
    """Create test record."""
    indexer = RecordIndexer()
    with app.test_request_context():
        record_id = uuid.uuid4()
        recid_minter(record_id, item_dict)
        if mint_oaiid:
            oaiid_minter(record_id, item_dict)
        record = Record.create(item_dict, id_=record_id)
        indexer.index(record)
        return record
Example #14
0
def published_record(app, db, prepare_es):
    # let's create a record
    record_uuid = uuid.uuid4()
    SampleRecord._prepare_schemas()
    data = {'title': 'blah', '$schema': SampleRecord.PREFERRED_SCHEMA}
    recid_minter(record_uuid, data)
    rec = SampleRecord.create(data, id_=record_uuid)
    RecordIndexer().index(rec)
    current_search_client.indices.refresh()
    current_search_client.indices.flush()

    return rec
Example #15
0
def test_listidentifiers(app):
    """Test verb ListIdentifiers."""
    schema = {
        'type': 'object',
        'properties': {
            'title': {'type': 'string'},
            'field': {'type': 'boolean'},
        },
        'required': ['title'],
    }
    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title': 'Test0', '$schema': schema}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        sleep(1)

        pid_value = pid.pid_value

        with app.test_client() as c:
            result = c.get(
                "/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc"
            )

        tree = etree.fromstring(result.data)

        namespaces = {'x': NS_OAIPMH}
        assert len(tree.xpath('/x:OAI-PMH', namespaces=namespaces)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListIdentifiers',
                              namespaces=namespaces)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListIdentifiers/x:header',
                              namespaces=namespaces)) == 1
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=namespaces
        )
        assert len(identifier) == 1
        assert identifier[0].text == str(pid_value)
        datestamp = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:datestamp',
            namespaces=namespaces
        )
        assert len(datestamp) == 1
        assert datestamp[0].text == datetime_to_datestamp(record.updated)
Example #16
0
def test_getrecord(app):
    schema = {
        'type': 'object',
        'properties': {
            'title': {
                'type': 'string'
            },
            'field': {
                'type': 'boolean'
            },
        },
        'required': ['title'],
    }
    with app.test_request_context():
        with db.session.begin_nested():
            record = Record.create({'title': 'Test0', '$schema': schema}).model
            recid_minter(record.id, record.json)
            pid = oaiid_minter(record.id, record.json)

        db.session.commit()
        pid_value = pid.pid_value
        record_updated = record.updated
        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=GetRecord&identifier={0}&metadataPrefix=oai_dc'.
                format(pid_value))
            assert 200 == result.status_code

            tree = etree.fromstring(result.data)

            assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
            assert len(
                tree.xpath('/x:OAI-PMH/x:GetRecord',
                           namespaces=NAMESPACES)) == 1
            assert len(
                tree.xpath('/x:OAI-PMH/x:GetRecord/x:header',
                           namespaces=NAMESPACES)) == 1
            assert len(
                tree.xpath('/x:OAI-PMH/x:GetRecord/x:header/x:identifier',
                           namespaces=NAMESPACES)) == 1
            identifier = tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:header/x:identifier/text()',
                namespaces=NAMESPACES)
            assert identifier == [str(record.id)]
            datestamp = tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:header/x:datestamp/text()',
                namespaces=NAMESPACES)
            assert datestamp == [datetime_to_datestamp(record_updated)]
            assert len(
                tree.xpath('/x:OAI-PMH/x:GetRecord/x:metadata',
                           namespaces=NAMESPACES)) == 1
Example #17
0
def oaiserver(sets, records):
    """Initialize OAI-PMH server."""
    from invenio_db import db
    from invenio_oaiserver.models import OAISet
    from invenio_records.api import Record

    # create a OAI Set
    with db.session.begin_nested():
        for i in range(sets):
            db.session.add(OAISet(
                spec='test{0}'.format(i),
                name='Test{0}'.format(i),
                description='test desc {0}'.format(i),
                search_pattern='title_statement.title:Test{0}'.format(i),
            ))

    # create a record
    schema = {
        'type': 'object',
        'properties': {
            'title_statement': {
                'type': 'object',
                'properties': {
                    'title': {
                        'type': 'string',
                    },
                },
            },
            'field': {'type': 'boolean'},
        },
    }

    search.client.indices.delete_alias('_all', '_all', ignore=[400, 404])
    search.client.indices.delete('*')

    with app.app_context():
        indexer = RecordIndexer()
        with db.session.begin_nested():
            for i in range(records):
                record_id = uuid.uuid4()
                data = {
                    'title_statement': {'title': 'Test{0}'.format(i)},
                    '$schema': schema,
                }
                recid_minter(record_id, data)
                oaiid_minter(record_id, data)
                record = Record.create(data, id_=record_id)
                indexer.index(record)

        db.session.commit()
Example #18
0
def test_listmetadataformats_record(app):
    """Test ListMetadataFormats for a record."""
    with app.test_request_context():
        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title_statement': {'title': 'Test0'}}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            Record.create(data, id_=record_id)
            pid_value = pid.pid_value

        db.session.commit()

    _listmetadataformats(
        app=app,
        query='/oai?verb=ListMetadataFormats&identifier={0}'.format(pid_value))
Example #19
0
def test_listmetadataformats_record(app):
    """Test ListMetadataFormats for a record."""
    with app.test_request_context():
        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title_statement': {'title': 'Test0'}}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            Record.create(data, id_=record_id)
            pid_value = pid.pid_value

        db.session.commit()

    _listmetadataformats(
        app=app,
        query='/oai2d?verb=ListMetadataFormats&identifier={0}'.format(
            pid_value))
Example #20
0
def test_getrecord(app):
    schema = {
        'type': 'object',
        'properties': {
            'title': {'type': 'string'},
            'field': {'type': 'boolean'},
        },
        'required': ['title'],
    }
    with app.test_request_context():
        with db.session.begin_nested():
            record = Record.create({'title': 'Test0', '$schema': schema}).model
            recid_minter(record.id, record.json)
            pid = oaiid_minter(record.id, record.json)

        db.session.commit()
        pid_value = pid.pid_value
        record_updated = record.updated
        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=GetRecord&identifier={0}&metadataPrefix=oai_dc'
                .format(pid_value))
            assert 200 == result.status_code

            tree = etree.fromstring(result.data)

            assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
            assert len(tree.xpath('/x:OAI-PMH/x:GetRecord',
                                  namespaces=NAMESPACES)) == 1
            assert len(tree.xpath('/x:OAI-PMH/x:GetRecord/x:header',
                                  namespaces=NAMESPACES)) == 1
            assert len(tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:header/x:identifier',
                namespaces=NAMESPACES)) == 1
            identifier = tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:header/x:identifier/text()',
                namespaces=NAMESPACES)
            assert identifier == [str(record.id)]
            datestamp = tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:header/x:datestamp/text()',
                namespaces=NAMESPACES)
            assert datestamp == [datetime_to_datestamp(record_updated)]
            assert len(tree.xpath('/x:OAI-PMH/x:GetRecord/x:metadata',
                                  namespaces=NAMESPACES)) == 1
Example #21
0
def test_listrecords(app):
    """Test ListRecords."""
    schema = {
        'type': 'object',
        'properties': {
            'title': {'type': 'string'},
            'field': {'type': 'boolean'},
        },
        'required': ['title'],
    }
    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title': 'Test0', '$schema': schema}
            recid_minter(record_id, data)
            oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        sleep(2)

        with app.test_client() as c:
            result = c.get('/oai2d?verb=ListRecords&metadataPrefix=oai_dc')

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1

        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:identifier', namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:datestamp', namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:metadata',
                              namespaces=NAMESPACES)) == 1
Example #22
0
def test_recid_fetcher(app, db):
    """Test legacy recid fetcher."""
    with app.app_context():
        rec_uuid = uuid.uuid4()
        data = {}
        minted_pid = recid_minter(rec_uuid, data)
        fetched_pid = recid_fetcher(rec_uuid, data)
        assert minted_pid.pid_value == fetched_pid.pid_value
        assert fetched_pid.pid_type == fetched_pid.provider.pid_type
        assert fetched_pid.pid_type == 'recid'
def test_recid_fetcher(app):
    """Test base provider."""
    with app.app_context():
        rec_uuid = uuid.uuid4()
        data = {}
        minted_pid = recid_minter(rec_uuid, data)
        fetched_pid = recid_fetcher(rec_uuid, data)
        assert minted_pid.pid_value == fetched_pid.pid_value
        assert fetched_pid.pid_type == fetched_pid.provider.pid_type
        assert fetched_pid.pid_type == 'recid'
def test_listmetadataformats_record(app):
    """Test ListMetadataFormats for a record."""
    schema = {
        "type": "object",
        "properties": {"title": {"type": "string"}, "field": {"type": "boolean"}},
        "required": ["title"],
    }
    with app.test_request_context():
        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {"title": "Test0", "$schema": schema}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            Record.create(data, id_=record_id)
            pid_value = pid.pid_value

        db.session.commit()

    _listmetadataformats(app=app, query="/oai2d?verb=ListMetadataFormats&identifier={0}".format(pid_value))
def test_recid_minter(app):
    """Test base provider."""
    with app.app_context():

        rec_uuid = uuid.uuid4()
        data = {}
        pid = recid_minter(rec_uuid, data)
        assert pid
        assert data['control_number'] == pid.pid_value
        assert pid.object_type == 'rec'
        assert pid.object_uuid == rec_uuid
Example #26
0
def test_recid_minter(app, db):
    """Test base provider."""
    with app.app_context():

        rec_uuid = uuid.uuid4()
        data = {}
        pid = recid_minter(rec_uuid, data)
        assert pid
        assert data['control_number'] == pid.pid_value
        assert pid.object_type == 'rec'
        assert pid.object_uuid == rec_uuid
Example #27
0
def test_recid_minter(app, db):
    """Test base provider."""
    with app.app_context():

        rec_uuid = uuid.uuid4()
        data = {}
        pid = recid_minter(rec_uuid, data)
        assert pid
        assert data[app.config['PIDSTORE_RECID_FIELD']] == pid.pid_value
        assert pid.object_type == 'rec'
        assert pid.object_uuid == rec_uuid
Example #28
0
def oaiserver(number):
    """Initialize OAI-PMH server."""
    from invenio_db import db
    from invenio_oaiserver.models import OAISet
    from invenio_records.api import Record

    # create a OAI Set
    with db.session.begin_nested():
        for i in range(number):
            db.session.add(
                OAISet(
                    spec='test{0}'.format(i),
                    name='Test{0}'.format(i),
                    description='test desc {0}'.format(i),
                    search_pattern='title:Test{0}'.format(i),
                ))

    # create a record
    schema = {
        'type': 'object',
        'properties': {
            'title': {
                'type': 'string'
            },
            'field': {
                'type': 'boolean'
            },
        },
        'required': ['title'],
    }

    with db.session.begin_nested():
        for i in range(number):
            record_id = uuid.uuid4()
            data = {'title': 'Test{0}'.format(i), '$schema': schema}
            recid_minter(record_id, data)
            oaiid_minter(record_id, data)
            Record.create(data, id_=record_id)

    db.session.commit()
def test_listidentifiers(app):
    """Test verb ListIdentifiers."""
    schema = {
        "type": "object",
        "properties": {"title": {"type": "string"}, "field": {"type": "boolean"}},
        "required": ["title"],
    }
    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {"title": "Test0", "$schema": schema}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        sleep(1)

        pid_value = pid.pid_value

        with app.test_client() as c:
            result = c.get("/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc")

        tree = etree.fromstring(result.data)

        namespaces = {"x": NS_OAIPMH}
        assert len(tree.xpath("/x:OAI-PMH", namespaces=namespaces)) == 1
        assert len(tree.xpath("/x:OAI-PMH/x:ListIdentifiers", namespaces=namespaces)) == 1
        assert len(tree.xpath("/x:OAI-PMH/x:ListIdentifiers/x:header", namespaces=namespaces)) == 1
        identifier = tree.xpath("/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier", namespaces=namespaces)
        assert len(identifier) == 1
        assert identifier[0].text == str(pid_value)
        datestamp = tree.xpath("/x:OAI-PMH/x:ListIdentifiers/x:header/x:datestamp", namespaces=namespaces)
        assert len(datestamp) == 1
        assert datestamp[0].text == datetime_to_datestamp(record.updated)
def _create_record(data):
    # TODO: It might make sense to mark the new record as temporary
    import uuid
    import dateutil.parser as parser

    from invenio_db import db
    from invenio_pidstore.minters import recid_minter
    from invenio_indexer.api import RecordIndexer
    from invenio_records.api import Record

    def _get_keys(key_string):
        return [
            int(x) if x.isdigit() else unicode(x)
            for x in key_string.split('.')
        ]

    authors = data['circulation_compact_authors'].split(';')
    del data['circulation_compact_authors']

    fields = [(_get_keys(key), unicode(value)) for key, value in data.items()]

    rec = {}
    _prepare_record(rec, fields)

    rec[u'main_entry_personal_name'] = {u'personal_name': unicode(authors[0])}
    rec[u'added_entry_personal_name'] = [{
        u'personal_name': unicode(x)
    } for x in authors[1:]]

    # We need to check the year here
    year = (rec[u'publication_distribution_imprint'][0]
            [u'date_of_publication_distribution'][0])

    if year == u'':
        del (rec[u'publication_distribution_imprint'][0]
             [u'date_of_publication_distribution'])

    parser.parse(year)

    rec_uuid = uuid.uuid4()
    pid = recid_minter(rec_uuid, rec)
    rec[u'recid'] = int(pid.pid_value)
    rec[u'uuid'] = unicode(str(rec_uuid))

    r = Record.create(rec, id_=rec_uuid)
    RecordIndexer().index(r)

    db.session.commit()

    return r
Example #31
0
def test_listrecords(app):
    """Test ListRecords."""
    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title_statement': {'title': 'Test0'}}
            recid_minter(record_id, data)
            oaiid_minter(record_id, data)
            Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)

        sleep(2)

        with app.test_client() as c:
            result = c.get('/oai2d?verb=ListRecords&metadataPrefix=oai_dc')

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1

        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:identifier', namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:datestamp', namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:metadata',
                              namespaces=NAMESPACES)) == 1
Example #32
0
def make_sample_record(db, title, community_id, state='filling', secondary=None):
    rec = {
        'title': title,
        'oarepo:primaryCommunity': community_id,
        'oarepo:recordStatus': state,
        'oarepo:secondaryCommunities': secondary,
        'oarepo:ownedBy': 1
    }
    record_uuid = uuid.uuid4()
    pid = recid_minter(record_uuid, rec)
    rec = TestRecord.create(rec, id_=record_uuid)
    db.session.commit()
    indexer = RecordIndexer()
    indexer.index(rec)
    return PIDRecord(pid, rec)
def test_draft_endpoint_list(app, db, schemas, mappings, prepare_es,
                             client, draft_records_url):
    resp = client.get(draft_records_url)
    assert resp.status_code == 200
    server_name = app.config['SERVER_NAME']
    assert (server_name + draft_records_url) in header_links(resp)['self']

    # let's create a record
    record_uuid = uuid.uuid4()
    data = {
        # no title for draft record'title': 'blah',
        '$schema': current_jsonschemas.path_to_url('draft/records/record-v1.0.0.json')
    }
    recid_minter(record_uuid, data)
    rec = Record.create(data, id_=record_uuid)
    RecordIndexer().index(rec)
    current_search_client.indices.refresh()
    current_search_client.indices.flush()

    resp = client.get(draft_records_url)
    assert resp.status_code == 200
    assert len(resp.json['hits']['hits']) == 1
    first_hit = resp.json['hits']['hits'][0]
    assert first_hit['links']['self'] == f'http://{server_name}{draft_records_url}1'
Example #34
0
def create_record(ctx):
    """
    Creates the record in the database.
    :param ctx: The record metadata as a dictionary.
    :return: the recid and the uuid
    """
    record_information = create_data_structure(ctx)
    record_id = uuid.uuid4()
    pid = recid_minter(record_id, record_information)
    record_information['recid'] = int(pid.pid_value)
    record_information['uuid'] = str(record_id)

    Record.create(record_information, id_=record_id)
    db.session.commit()

    return record_information
Example #35
0
def create_record(ctx):
    """
    Creates the record in the database.
    :param ctx: The record metadata as a dictionary.
    :return: the recid and the uuid
    """
    record_information = create_data_structure(ctx)
    record_id = uuid.uuid4()
    pid = recid_minter(record_id, record_information)
    record_information['recid'] = int(pid.pid_value)
    record_information['uuid'] = str(record_id)

    Record.create(record_information, id_=record_id)
    db.session.commit()

    return record_information
def _create_record(data):
    # TODO: It might make sense to mark the new record as temporary
    import uuid
    import dateutil.parser as parser

    from invenio_db import db
    from invenio_pidstore.minters import recid_minter
    from invenio_indexer.api import RecordIndexer
    from invenio_records.api import Record

    def _get_keys(key_string):
        return [int(x) if x.isdigit() else unicode(x)
                for x in key_string.split('.')]

    authors = data['circulation_compact_authors'].split(';')
    del data['circulation_compact_authors']

    fields = [(_get_keys(key), unicode(value)) for key, value in data.items()]

    rec = {}
    _prepare_record(rec, fields)

    rec[u'main_entry_personal_name'] = {u'personal_name': unicode(authors[0])}
    rec[u'added_entry_personal_name'] = [{u'personal_name': unicode(x)}
                                         for x in authors[1:]]

    # We need to check the year here
    year = (rec[u'publication_distribution_imprint'][0]
               [u'date_of_publication_distribution'][0])

    if year == u'':
        del (rec[u'publication_distribution_imprint'][0]
                [u'date_of_publication_distribution'])

    parser.parse(year)

    rec_uuid = uuid.uuid4()
    pid = recid_minter(rec_uuid, rec)
    rec[u'recid'] = int(pid.pid_value)
    rec[u'uuid'] = unicode(str(rec_uuid))

    r = Record.create(rec, id_=rec_uuid)
    RecordIndexer().index(r)

    db.session.commit()

    return r
Example #37
0
def test_listidentifiers(app):
    """Test verb ListIdentifiers."""
    from invenio_oaiserver.models import OAISet

    with app.app_context():
        with db.session.begin_nested():
            db.session.add(OAISet(
                spec='test0',
                name='Test0',
                description='test desc 0',
                search_pattern='title_statement.title:Test0',
            ))
        db.session.commit()

    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title_statement': {'title': 'Test0'}}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        sleep(2)

        pid_value = pid.pid_value

        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc'
            )

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListIdentifiers',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListIdentifiers/x:header',
                              namespaces=NAMESPACES)) == 1
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=NAMESPACES
        )
        assert len(identifier) == 1
        assert identifier[0].text == str(pid_value)
        datestamp = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:datestamp',
            namespaces=NAMESPACES
        )
        assert len(datestamp) == 1
        assert datestamp[0].text == record['_oai']['updated']

        # Check from_:until range
        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc'
                '&from_={0}&until={1}&set=test0'.format(
                    datetime_to_datestamp(record.updated - datetime.timedelta(
                        1)),
                    datetime_to_datestamp(record.updated + datetime.timedelta(
                        1)),
                )
            )

        tree = etree.fromstring(result.data)
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=NAMESPACES
        )
        assert len(identifier) == 1
Example #38
0
def records():
    """Load fixtures."""
    with db.session.begin_nested():
        for record in RecordMetadata.query.all():
            recid_minter(record.id, record.json)
    db.session.commit()
Example #39
0
def pid(db, record):
    """File system location."""
    return recid_minter(record.id, record)
Example #40
0
def minter(record_uuid, data):
    rminter = recid_minter(record_uuid, data)
    oaiid_minter(record_uuid, data)

    return rminter
Example #41
0
def pid(db, record):
    """File system location."""
    return recid_minter(record.id, record)
Example #42
0
def test_listrecords(app):
    """Test ListRecords."""
    total = 12
    record_ids = []

    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            for idx in range(total):
                record_id = uuid.uuid4()
                data = {'title_statement': {'title': 'Test{0}'.format(idx)}}
                recid_minter(record_id, data)
                oaiid_minter(record_id, data)
                Record.create(data, id_=record_id)
                record_ids.append(record_id)

        db.session.commit()

        for record_id in record_ids:
            indexer.index_by_id(record_id)

        sleep(5)

        with app.test_client() as c:
            result = c.get('/oai2d?verb=ListRecords&metadataPrefix=oai_dc')

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1

        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                              namespaces=NAMESPACES)) == 10
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header',
                              namespaces=NAMESPACES)) == 10
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:identifier', namespaces=NAMESPACES)) == 10
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:datestamp', namespaces=NAMESPACES)) == 10
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:metadata',
                              namespaces=NAMESPACES)) == 10

        resumption_token = tree.xpath(
            '/x:OAI-PMH/x:ListRecords/x:resumptionToken', namespaces=NAMESPACES
        )[0]
        assert resumption_token.text

        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=ListRecords&resumptionToken={0}'.format(
                    resumption_token.text
                )
            )

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1

        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                              namespaces=NAMESPACES)) == 2
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header',
                              namespaces=NAMESPACES)) == 2
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:identifier', namespaces=NAMESPACES)) == 2
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:datestamp', namespaces=NAMESPACES)) == 2
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:metadata',
                              namespaces=NAMESPACES)) == 2

        resumption_token = tree.xpath(
            '/x:OAI-PMH/x:ListRecords/x:resumptionToken', namespaces=NAMESPACES
        )[0]
        assert not resumption_token.text
def test_listrecords(app):
    """Test ListRecords."""
    total = 12
    record_ids = []

    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            for idx in range(total):
                record_id = uuid.uuid4()
                data = {'title_statement': {'title': 'Test{0}'.format(idx)}}
                recid_minter(record_id, data)
                oaiid_minter(record_id, data)
                record = Record.create(data, id_=record_id)
                record_ids.append(record_id)

        db.session.commit()

        for record_id in record_ids:
            indexer.index_by_id(record_id)

        current_search.flush_and_refresh('_all')

        with app.test_client() as c:
            result = c.get('/oai2d?verb=ListRecords&metadataPrefix=oai_dc')

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1

        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                              namespaces=NAMESPACES)) == 10
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header',
                              namespaces=NAMESPACES)) == 10
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:identifier', namespaces=NAMESPACES)) == 10
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:datestamp', namespaces=NAMESPACES)) == 10
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:metadata',
                              namespaces=NAMESPACES)) == 10

        resumption_token = tree.xpath(
            '/x:OAI-PMH/x:ListRecords/x:resumptionToken', namespaces=NAMESPACES
        )[0]
        assert resumption_token.text

        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=ListRecords&resumptionToken={0}'.format(
                    resumption_token.text
                )
            )

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1

        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                              namespaces=NAMESPACES)) == 2
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header',
                              namespaces=NAMESPACES)) == 2
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:identifier', namespaces=NAMESPACES)) == 2
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:datestamp', namespaces=NAMESPACES)) == 2
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:metadata',
                              namespaces=NAMESPACES)) == 2

        resumption_token = tree.xpath(
            '/x:OAI-PMH/x:ListRecords/x:resumptionToken', namespaces=NAMESPACES
        )[0]
        assert not resumption_token.text

        # Check from:until range
        with app.test_client() as c:
            # Check date and datetime timestamps.
            for granularity in (False, True):
                result = c.get(
                    '/oai2d?verb=ListRecords&metadataPrefix=oai_dc'
                    '&from={0}&until={1}'.format(
                        datetime_to_datestamp(
                            record.updated - timedelta(days=1),
                            day_granularity=granularity),
                        datetime_to_datestamp(
                            record.updated + timedelta(days=1),
                            day_granularity=granularity),
                    )
                )
                assert result.status_code == 200

                tree = etree.fromstring(result.data)
                assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                           namespaces=NAMESPACES)) == 10
Example #44
0
def test_listrecords(app):
    """Test ListRecords."""
    total = 12
    record_ids = []

    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            for idx in range(total):
                record_id = uuid.uuid4()
                data = {'title_statement': {'title': 'Test{0}'.format(idx)}}
                recid_minter(record_id, data)
                oaiid_minter(record_id, data)
                Record.create(data, id_=record_id)
                record_ids.append(record_id)

        db.session.commit()

        for record_id in record_ids:
            indexer.index_by_id(record_id)

        sleep(5)

        with app.test_client() as c:
            result = c.get('/oai2d?verb=ListRecords&metadataPrefix=oai_dc')

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1

        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                              namespaces=NAMESPACES)) == 10
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header',
                              namespaces=NAMESPACES)) == 10
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:identifier', namespaces=NAMESPACES)) == 10
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:datestamp', namespaces=NAMESPACES)) == 10
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:metadata',
                              namespaces=NAMESPACES)) == 10

        resumption_token = tree.xpath(
            '/x:OAI-PMH/x:ListRecords/x:resumptionToken', namespaces=NAMESPACES
        )[0]
        assert resumption_token.text

        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=ListRecords&resumptionToken={0}'.format(
                    resumption_token.text
                )
            )

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1

        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                              namespaces=NAMESPACES)) == 2
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header',
                              namespaces=NAMESPACES)) == 2
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:identifier', namespaces=NAMESPACES)) == 2
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header'
                              '/x:datestamp', namespaces=NAMESPACES)) == 2
        assert len(tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:metadata',
                              namespaces=NAMESPACES)) == 2

        resumption_token = tree.xpath(
            '/x:OAI-PMH/x:ListRecords/x:resumptionToken', namespaces=NAMESPACES
        )[0]
        assert not resumption_token.text
def test_listidentifiers(app):
    """Test verb ListIdentifiers."""
    from invenio_oaiserver.models import OAISet

    with app.app_context():
        current_oaiserver.unregister_signals_oaiset()
        # create new OAI Set
        with db.session.begin_nested():
            oaiset = OAISet(
                spec='test0',
                name='Test0',
                description='test desc 0',
                search_pattern='title_statement.title:Test0',
            )
            db.session.add(oaiset)
        db.session.commit()

    run_after_insert_oai_set()

    with app.test_request_context():
        indexer = RecordIndexer()

        # create a new record (inside the OAI Set)
        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title_statement': {'title': 'Test0'}}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        current_search.flush_and_refresh('_all')

        pid_value = pid.pid_value

        # get the list of identifiers
        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc'
            )

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListIdentifiers',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListIdentifiers/x:header',
                              namespaces=NAMESPACES)) == 1
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=NAMESPACES
        )
        assert len(identifier) == 1
        assert identifier[0].text == str(pid_value)
        datestamp = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:datestamp',
            namespaces=NAMESPACES
        )
        assert len(datestamp) == 1
        assert datestamp[0].text == datetime_to_datestamp(record.updated)

        # Check from:until range
        with app.test_client() as c:
            # Check date and datetime timestamps.
            for granularity in (False, True):
                result = c.get(
                    '/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc'
                    '&from={0}&until={1}&set=test0'.format(
                        datetime_to_datestamp(
                            record.updated - timedelta(1),
                            day_granularity=granularity),
                        datetime_to_datestamp(
                            record.updated + timedelta(1),
                            day_granularity=granularity),
                    )
                )
                assert result.status_code == 200

                tree = etree.fromstring(result.data)
                identifier = tree.xpath(
                    '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
                    namespaces=NAMESPACES
                )
                assert len(identifier) == 1
def test_production_endpoint(app, db, schemas, mappings, prepare_es,
                             client, published_records_url, test_users):
    resp = client.get(published_records_url)
    assert resp.status_code == 200
    assert (app.config['SERVER_NAME'] + published_records_url) in header_links(resp)['self']

    login(client, test_users.u1)

    resp = client.post(published_records_url, json={'title': 'abc'})
    assert resp.status_code == 403
    assert resp.json == {
        'message': "You don't have the permission to access the requested resource. "
                   'It is either read-protected or not readable by the server.',
        'status': 403
    }

    # let's create a record
    record_uuid = uuid.uuid4()
    data = {
        'title': 'blah',
        '$schema': current_jsonschemas.path_to_url('records/record-v1.0.0.json')
    }
    recid_minter(record_uuid, data)
    rec = Record.create(data, id_=record_uuid)
    RecordIndexer().index(rec)
    current_search_client.indices.refresh()
    current_search_client.indices.flush()

    resp = client.get(published_records_url)
    assert resp.status_code == 200
    assert len(resp.json['hits']['hits']) == 1
    record_url = resp.json['hits']['hits'][0]['links']['self']
    assert record_url == 'http://localhost:5000/records/1'

    # try to update the record
    resp = client.put(record_url, json={'title': 'abc'})
    assert resp.status_code == 403
    assert resp.json == {
        'message': "You don't have the permission to access the requested resource. "
                   'It is either read-protected or not readable by the server.',
        'status': 403
    }

    # try to patch the record
    patch_ops = [{'op': 'replace', 'path': '/title', 'value': 'abc'}]
    resp = client.patch(record_url, json=patch_ops, headers={
        'Content-Type': 'application/json-patch+json'
    })
    assert resp.status_code == 403
    assert resp.json == {
        'message': "You don't have the permission to access the requested resource. "
                   'It is either read-protected or not readable by the server.',
        'status': 403
    }

    # deleting the record should pass as the default permission is allow_all
    resp = client.delete(record_url)
    assert resp.status_code == 204

    resp = client.get(record_url)
    assert resp.status_code == 410  # gone
Example #47
0
def test_aclrecordsearch_explicit_user(app, db, es, es_acl_prepare, test_users):
    current_explicit_acls.prepare(RECORD_SCHEMA)

    with db.session.begin_nested():
        acl1 = DefaultACL(name='test', schemas=[RECORD_SCHEMA],
                          priority=0, operation='get', originator=test_users.u1)
        actor1 = UserActor(name='auth', acl=acl1, users=[test_users.u1], originator=test_users.u1)
        db.session.add(acl1)
        db.session.add(actor1)

    current_explicit_acls.reindex_acl(acl1, delayed=False)

    record_uuid = uuid.uuid4()
    data = {'title': 'blah', 'contributors': [], 'keywords': ['blah']}
    recid_minter(record_uuid, data)
    rec = SchemaEnforcingRecord.create(data, id_=record_uuid)
    RecordIndexer().index(rec)

    current_search_client.indices.refresh()
    current_search_client.indices.flush()

    rs = ACLRecordsSearch(user=test_users.u1, context={
        'system_roles': ['authenticated_user']
    })
    rec_id = str(rec.id)
    print(json.dumps(rs.query(Ids(values=[rec_id])).query.to_dict(), indent=4))
    assert rs.query(Ids(values=[rec_id])).query.to_dict() == {
        "bool": {
            "minimum_should_match": "100%",
            "filter": [
                {
                    "bool": {
                        "should": [
                            {
                                "nested": {
                                    "path": "_invenio_explicit_acls",
                                    "_name": "invenio_explicit_acls_match_get",
                                    "query": {
                                        "bool": {
                                            "must": [
                                                {
                                                    "term": {
                                                        "_invenio_explicit_acls.operation": "get"
                                                    }
                                                },
                                                {
                                                    "bool": {
                                                        "minimum_should_match": 1,
                                                        "should": [
                                                            {
                                                                "terms": {
                                                                    "_invenio_explicit_acls.role": [
                                                                        1
                                                                    ]
                                                                }
                                                            },
                                                            {
                                                                "term": {
                                                                    "_invenio_explicit_acls.user": 1
                                                                }
                                                            },
                                                            {
                                                                "terms": {
                                                                    "_invenio_explicit_acls.role": [
                                                                        1
                                                                    ]
                                                                }
                                                            },
                                                            {
                                                                "terms": {
                                                                    "_invenio_explicit_acls.system_role": [
                                                                        "authenticated_user"
                                                                    ]
                                                                }
                                                            },
                                                            {
                                                                "term": {
                                                                    "_invenio_explicit_acls.user": 1
                                                                }
                                                            }
                                                        ]
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        ],
                        "minimum_should_match": 1
                    }
                }
            ],
            "must": [
                {
                    "ids": {
                        "values": [
                            rec_id
                        ]
                    }
                }
            ]
        }
    }

    hits = list(ACLRecordsSearch(user=test_users.u1, context={
        'system_roles': [authenticated_user]
    }).get_record(rec.id).execute())

    assert len(hits) == 1
    assert hits[0].meta.id == rec_id
    print(hits)

    hits = list(ACLRecordsSearch(user=test_users.u2, context={
        'system_roles': [authenticated_user]
    }).get_record(rec.id).execute())
    assert hits == []
Example #48
0
def records():
    """Load fixtures."""
    with db.session.begin_nested():
        for record in RecordMetadata.query.all():
            recid_minter(record.id, record.json)
    db.session.commit()
Example #49
0
def test_listidentifiers(app):
    """Test verb ListIdentifiers."""
    schema = {
        'type': 'object',
        'properties': {
            'title': {
                'type': 'string'
            },
            'field': {
                'type': 'boolean'
            },
        },
        'required': ['title'],
    }
    from invenio_oaiserver.models import OAISet

    with app.app_context():
        with db.session.begin_nested():
            db.session.add(
                OAISet(
                    spec='test0',
                    name='Test0',
                    description='test desc 0',
                    search_pattern='title:Test0',
                ))
        db.session.commit()

    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title': 'Test0', '$schema': schema}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        sleep(2)

        pid_value = pid.pid_value

        with app.test_client() as c:
            result = c.get('/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc')

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListIdentifiers',
                       namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListIdentifiers/x:header',
                       namespaces=NAMESPACES)) == 1
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=NAMESPACES)
        assert len(identifier) == 1
        assert identifier[0].text == str(pid_value)
        datestamp = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:datestamp',
            namespaces=NAMESPACES)
        assert len(datestamp) == 1
        assert datestamp[0].text == datetime_to_datestamp(record.updated)

        # Check from_:until range
        with app.test_client() as c:
            result = c.get('/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc'
                           '&from_={0}&until={1}&set=test0'.format(
                               datetime_to_datestamp(record.updated -
                                                     datetime.timedelta(1)),
                               datetime_to_datestamp(record.updated +
                                                     datetime.timedelta(1)),
                           ))

        tree = etree.fromstring(result.data)
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=NAMESPACES)
        assert len(identifier) == 1
Example #50
0
def test_listidentifiers(app):
    """Test verb ListIdentifiers."""
    from invenio_oaiserver.models import OAISet

    with app.app_context():
        current_oaiserver.unregister_signals_oaiset()
        # create new OAI Set
        with db.session.begin_nested():
            oaiset = OAISet(
                spec='test0',
                name='Test0',
                description='test desc 0',
                search_pattern='title_statement.title:Test0',
            )
            db.session.add(oaiset)
        db.session.commit()

    run_after_insert_oai_set()

    with app.test_request_context():
        indexer = RecordIndexer()

        # create a new record (inside the OAI Set)
        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title_statement': {'title': 'Test0'}}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        current_search.flush_and_refresh('_all')

        pid_value = pid.pid_value

        # get the list of identifiers
        with app.test_client() as c:
            result = c.get('/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc')

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListIdentifiers',
                       namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListIdentifiers/x:header',
                       namespaces=NAMESPACES)) == 1
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=NAMESPACES)
        assert len(identifier) == 1
        assert identifier[0].text == str(pid_value)
        datestamp = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:datestamp',
            namespaces=NAMESPACES)
        assert len(datestamp) == 1
        assert datestamp[0].text == datetime_to_datestamp(record.updated)

        # Check from:until range
        with app.test_client() as c:
            # Check date and datetime timestamps.
            for granularity in (False, True):
                result = c.get(
                    '/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc'
                    '&from={0}&until={1}&set=test0'.format(
                        datetime_to_datestamp(record.updated - timedelta(1),
                                              day_granularity=granularity),
                        datetime_to_datestamp(record.updated + timedelta(1),
                                              day_granularity=granularity),
                    ))
                assert result.status_code == 200

                tree = etree.fromstring(result.data)
                identifier = tree.xpath(
                    '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
                    namespaces=NAMESPACES)
                assert len(identifier) == 1
Example #51
0
def test_listrecords(app):
    """Test ListRecords."""
    total = 12
    record_ids = []

    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            for idx in range(total):
                record_id = uuid.uuid4()
                data = {'title_statement': {'title': 'Test{0}'.format(idx)}}
                recid_minter(record_id, data)
                oaiid_minter(record_id, data)
                record = Record.create(data, id_=record_id)
                record_ids.append(record_id)

        db.session.commit()

        for record_id in record_ids:
            indexer.index_by_id(record_id)

        current_search.flush_and_refresh('_all')

        with app.test_client() as c:
            result = c.get('/oai2d?verb=ListRecords&metadataPrefix=oai_dc')

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1

        assert len(
            tree.xpath('/x:OAI-PMH/x:ListRecords', namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                       namespaces=NAMESPACES)) == 10
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header',
                       namespaces=NAMESPACES)) == 10
        assert len(
            tree.xpath(
                '/x:OAI-PMH/x:ListRecords/x:record/x:header'
                '/x:identifier',
                namespaces=NAMESPACES)) == 10
        assert len(
            tree.xpath(
                '/x:OAI-PMH/x:ListRecords/x:record/x:header'
                '/x:datestamp',
                namespaces=NAMESPACES)) == 10
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:metadata',
                       namespaces=NAMESPACES)) == 10

        resumption_token = tree.xpath(
            '/x:OAI-PMH/x:ListRecords/x:resumptionToken',
            namespaces=NAMESPACES)[0]
        assert resumption_token.text
        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=ListRecords&resumptionToken={0}'.format(
                    resumption_token.text))

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1

        assert len(
            tree.xpath('/x:OAI-PMH/x:ListRecords', namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                       namespaces=NAMESPACES)) == 2
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:header',
                       namespaces=NAMESPACES)) == 2
        assert len(
            tree.xpath(
                '/x:OAI-PMH/x:ListRecords/x:record/x:header'
                '/x:identifier',
                namespaces=NAMESPACES)) == 2
        assert len(
            tree.xpath(
                '/x:OAI-PMH/x:ListRecords/x:record/x:header'
                '/x:datestamp',
                namespaces=NAMESPACES)) == 2
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListRecords/x:record/x:metadata',
                       namespaces=NAMESPACES)) == 2

        resumption_token = tree.xpath(
            '/x:OAI-PMH/x:ListRecords/x:resumptionToken',
            namespaces=NAMESPACES)[0]
        assert not resumption_token.text

        # Check from:until range
        with app.test_client() as c:
            # Check date and datetime timestamps.
            for granularity in (False, True):
                result = c.get('/oai2d?verb=ListRecords&metadataPrefix=oai_dc'
                               '&from={0}&until={1}'.format(
                                   datetime_to_datestamp(
                                       record.updated - timedelta(days=1),
                                       day_granularity=granularity),
                                   datetime_to_datestamp(
                                       record.updated + timedelta(days=1),
                                       day_granularity=granularity),
                               ))
                assert result.status_code == 200

                tree = etree.fromstring(result.data)
                assert len(
                    tree.xpath('/x:OAI-PMH/x:ListRecords/x:record',
                               namespaces=NAMESPACES)) == 10