Ejemplo n.º 1
0
def test_deposit_index(db, es):
    """Test update embargoed records."""
    deposit_index_name = 'deposits-records-record-v1.0.0'
    rec1 = Record.create({
        'title': 'One',
        '_deposit': {
            'status': 'published',
            'pid': {
                'type': 'recid',
                'value': '1'
            }
        }
    })
    PersistentIdentifier.create(pid_type='recid', pid_value='1',
                                status=PIDStatus.REGISTERED,
                                object_uuid=rec1.id, object_type='rec')
    Deposit.create({
        '_deposit': {
            'status': 'published',
            'pid': {
                'type': 'recid',
                'value': '1'
            }
        }
    })
    db.session.commit()
    current_search.flush_and_refresh(deposit_index_name)
    res = current_search.client.search(index=deposit_index_name)
    # Make sure the 'title' was indexed from record
    assert res['hits']['hits'][0]['_source']['title'] == 'One'
Ejemplo n.º 2
0
def test_deposit_index(db, es):
    """Test update embargoed records."""
    deposit_index_name = 'deposits-records-record-v1.0.0'
    rec1 = Record.create({
        'title': 'One',
        '_deposit': {
            'status': 'published',
            'pid': {
                'type': 'recid',
                'value': '1'
            }
        }
    })
    PersistentIdentifier.create(pid_type='recid',
                                pid_value='1',
                                status=PIDStatus.REGISTERED,
                                object_uuid=rec1.id,
                                object_type='rec')
    Deposit.create({
        '_deposit': {
            'status': 'published',
            'pid': {
                'type': 'recid',
                'value': '1'
            }
        }
    })
    db.session.commit()
    current_search.flush_and_refresh(deposit_index_name)
    res = current_search.client.search(index=deposit_index_name)
    # Make sure the 'title' was indexed from record
    assert res['hits']['hits'][0]['_source']['title'] == 'One'
def test_created_by_population(app, db, users):
    """Test created_by gets populated correctly."""
    record = {'title': 'fuu'}

    deposit = Deposit.create(record)
    assert 'created_by' not in deposit['_deposit']

    with app.test_request_context():
        login_user(users[0])
        deposit = Deposit.create(record)
        assert deposit['_deposit']['created_by'] == users[0].id
def test_created_by_population(api, users, location):
    """Test created_by gets populated correctly."""
    record = {'title': 'fuu'}

    deposit = Deposit.create(record)
    assert 'created_by' not in deposit['_deposit']

    with api.test_request_context():
        datastore = api.extensions['security'].datastore
        login_user(datastore.find_user(email=users[0]['email']))
        deposit = Deposit.create(record)
        assert deposit['_deposit']['created_by'] == users[0]['id']
def test_created_by_population(app, db, users):
    """Test created_by gets populated correctly."""
    record = {
        'title': 'fuu'
    }

    deposit = Deposit.create(record)
    assert 'created_by' not in deposit['_deposit']

    with app.test_request_context():
        login_user(users[0])
        deposit = Deposit.create(record)
        assert deposit['_deposit']['created_by'] == users[0].id
Ejemplo n.º 6
0
def test_schemas(app, db, fake_schemas):
    """Test schema URL transformations."""
    deposit = Deposit.create({})
    assert "http://localhost/schemas/deposits/deposit-v1.0.0.json" == deposit["$schema"]

    assert "http://localhost/schemas/deposit-v1.0.0.json" == deposit.record_schema

    assert "http://localhost/schemas/deposits/test-v1.0.0.json" == deposit.build_deposit_schema(
        {"$schema": "http://localhost/schemas/test-v1.0.0.json"}
    )

    with pytest.raises(RefResolutionError):
        Deposit.create({"$schema": "http://localhost/schemas/deposits/invalid.json"})
Ejemplo n.º 7
0
def records():
    """Load records."""
    from flask_login import login_user, logout_user
    from invenio_accounts.models import User
    from invenio_deposit.api import Deposit

    user = User.query.one()

    with current_app.test_request_context():
        login_user(user)
        with db.session.begin_nested():
            Deposit.create({'title': 'Test'})
        logout_user()
        db.session.commit()
def test_created_by_population(api, users):
    """Test created_by gets populated correctly."""
    record = {
        'title': 'fuu'
    }

    deposit = Deposit.create(record)
    assert 'created_by' not in deposit['_deposit']

    with api.test_request_context():
        datastore = api.extensions['security'].datastore
        login_user(datastore.find_user(email=users[0]['email']))
        deposit = Deposit.create(record)
        assert deposit['_deposit']['created_by'] == users[0]['id']
Ejemplo n.º 9
0
def test_delete(app, fake_schemas, location):
    """Test simple delete."""
    deposit = Deposit.create({})
    pid = deposit.pid
    assert deposit['_deposit']['id']
    assert 'draft' == deposit.status
    assert 0 == deposit.revision_id

    deposit.delete()

    with pytest.raises(NoResultFound):
        Deposit.get_record(deposit.id)

    with pytest.raises(PIDInvalidAction):
        deposit.publish(pid=pid)
Ejemplo n.º 10
0
def test_delete(app, db, fake_schemas, location):
    """Test simple delete."""
    deposit = Deposit.create({})
    pid = deposit.pid
    assert deposit["_deposit"]["id"]
    assert "draft" == deposit.status
    assert 0 == deposit.revision_id

    deposit.delete()

    with pytest.raises(NoResultFound):
        Deposit.get_record(deposit.id)

    with pytest.raises(PIDInvalidAction):
        deposit.publish(pid=pid)
Ejemplo n.º 11
0
def test_delete(app, db, fake_schemas):
    """Test simple delete."""
    deposit = Deposit.create({})
    pid = deposit.pid
    assert deposit['_deposit']['id']
    assert 'draft' == deposit['_deposit']['status']
    assert 0 == deposit.revision_id

    deposit.delete()

    with pytest.raises(NoResultFound):
        Deposit.get_record(deposit.id)

    with pytest.raises(PIDInvalidAction):
        deposit.publish(pid=pid)
Ejemplo n.º 12
0
def records():
    """Load records."""
    import pkg_resources
    import uuid
    from flask_login import login_user, logout_user
    from dojson.contrib.marc21 import marc21
    from dojson.contrib.marc21.utils import create_record, split_blob
    from invenio_accounts.models import User
    from invenio_deposit.api import Deposit

    users = User.query.all()

    # pkg resources the demodata
    data_path = pkg_resources.resource_filename(
        'invenio_records', 'data/marc21/bibliographic.xml'
    )
    with open(data_path) as source:
        with current_app.test_request_context():
            indexer = RecordIndexer()
            with db.session.begin_nested():
                for index, data in enumerate(split_blob(source.read()),
                                             start=1):
                    login_user(users[index % len(users)])
                    # do translate
                    record = marc21.do(create_record(data))
                    # create record
                    indexer.index(Deposit.create(record))
                    logout_user()
            db.session.commit()
def test_file_put(api, deposit, files, users):
    """PUT a deposit file."""
    with api.test_request_context():
        with api.test_client() as client:
            old_file_id = files[0].file_id
            old_filename = files[0].key
            new_filename = '{0}-new-name'.format(old_filename)
            # test rename file (without login)
            res = client.put(url_for('invenio_deposit_rest.depid_file',
                                     pid_value=deposit['_deposit']['id'],
                                     key=old_filename),
                             data=json.dumps({'filename': new_filename}))
            assert res.status_code == 401
            # login
            res = client.post(url_for_security('login'),
                              data=dict(email=users[0]['email'],
                                        password="******"))
            # test rename file
            res = client.put(url_for('invenio_deposit_rest.depid_file',
                                     pid_value=deposit['_deposit']['id'],
                                     key=old_filename),
                             data=json.dumps({'filename': new_filename}))
            deposit_id = deposit.id
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            files = list(deposit.files)
            assert res.status_code == 200
            assert new_filename == files[0].key
            assert old_file_id == files[0].file_id
            data = json.loads(res.data.decode('utf-8'))
            obj = files[0]
            assert data['filename'] == obj.key
            assert data['checksum'] == obj.file.checksum
            assert data['id'] == str(obj.file.id)
def test_files_put_oauth2(app, db, deposit, files, users, write_token_user_1):
    """Test put deposito files with oauth2."""
    with app.test_request_context():
        with app.test_client() as client:
            # fixture
            content = b'### Testing textfile 2 ###'
            stream = BytesIO(content)
            key = 'world.txt'
            deposit.files[key] = stream
            deposit.commit()
            db.session.commit()
            deposit_id = deposit.id
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            key0 = files[0].key
            files = list(deposit.files)
            assert files[0]['key'] == str(key0)
            assert files[1]['key'] == str(key)
            # order files
            res = client.put(
                url_for('invenio_deposit_rest.dep_files',
                        pid_value=deposit['_deposit']['id']),
                data=json.dumps([
                    {'id': key},
                    {'id': key0}
                ]),
                headers=[
                    ('Authorization',
                     'Bearer {0}'.format(write_token_user_1.access_token))
                ]
            )
            assert res.status_code == 200
            data = json.loads(res.data.decode('utf-8'))
            assert data[0]['filename'] == str(key)
            assert data[1]['filename'] == str(key0)
Ejemplo n.º 15
0
def test_publish_revision_changed_mergeable(app, db, location, fake_schemas):
    """Try to Publish and someone change the deposit in the while."""
    # create a deposit
    deposit = Deposit.create({"metadata": {"title": "title-1"}})
    deposit.commit()
    db.session.commit()
    # publish
    deposit.publish()
    db.session.commit()
    # edit
    deposit = deposit.edit()
    db.session.commit()
    # simulate a externally modification
    rid, record = deposit.fetch_published()
    rev_id = record.revision_id
    # try to change metadata
    record.update({
        'metadata': {"title": "title-1", 'poster': 'myposter'},
    })
    record.commit()
    db.session.commit()
    assert rev_id != record.revision_id
    # edit again and check the merging
    deposit.update({"metadata": {"title": "title-1", "description": "mydesc"}})
    deposit.commit()
    deposit.publish()
    db.session.commit()
    # check if is properly merged
    did, deposit = deposit.fetch_published()
    assert deposit['metadata']['title'] == 'title-1'
    assert deposit['metadata']['poster'] == 'myposter'
    assert deposit['metadata']['description'] == 'mydesc'
    assert deposit['$schema'] == 'http://localhost/schemas/deposit-v1.0.0.json'
Ejemplo n.º 16
0
def test_publish_revision_changed_not_mergeable(app, location,
                                                fake_schemas):
    """Try to Publish and someone change the deposit in the while."""
    # create a deposit
    deposit = Deposit.create({"metadata": {
        "title": "title-1",
    }})
    deposit.commit()
    db.session.commit()
    # publish
    deposit.publish()
    db.session.commit()
    # edit
    deposit = deposit.edit()
    db.session.commit()
    # simulate a externally modification
    rid, record = deposit.fetch_published()
    rev_id = record.revision_id
    record.update({'metadata': {
        "title": "title-2.1",
    }})
    record.commit()
    db.session.commit()
    assert rev_id != record.revision_id
    # edit again and check the merging
    deposit.update({"metadata": {
        "title": "title-2.2",
    }})
    deposit.commit()
    with pytest.raises(MergeConflict):
        deposit.publish()
Ejemplo n.º 17
0
def test_publish_revision_changed_not_mergeable(app, location, fake_schemas):
    """Try to Publish and someone change the deposit in the while."""
    # create a deposit
    deposit = Deposit.create({"metadata": {
        "title": "title-1",
    }})
    deposit.commit()
    db.session.commit()
    # publish
    deposit.publish()
    db.session.commit()
    # edit
    deposit = deposit.edit()
    db.session.commit()
    # simulate a externally modification
    rid, record = deposit.fetch_published()
    rev_id = record.revision_id
    record.update({'metadata': {
        "title": "title-2.1",
    }})
    record.commit()
    db.session.commit()
    assert rev_id != record.revision_id
    # edit again and check the merging
    deposit.update({"metadata": {
        "title": "title-2.2",
    }})
    deposit.commit()
    with pytest.raises(MergeConflict):
        deposit.publish()
def test_file_put_oauth2(api, deposit, files, users,
                         write_token_user_1):
    """PUT a deposit file with oauth2."""
    with api.test_request_context():
        with api.test_client() as client:
            old_file_id = files[0].file_id
            old_filename = files[0].key
            new_filename = '{0}-new-name'.format(old_filename)
            # test rename file
            res = client.put(
                url_for('invenio_deposit_rest.depid_file',
                        pid_value=deposit['_deposit']['id'],
                        key=old_filename),
                data=json.dumps({'filename': new_filename}),
                headers=[
                    ('Authorization',
                     'Bearer {0}'.format(write_token_user_1.access_token))
                ]
            )
            deposit_id = deposit.id
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            files = list(deposit.files)
            assert res.status_code == 200
            assert new_filename == files[0].key
            assert old_file_id == files[0].file_id
            data = json.loads(res.data.decode('utf-8'))
            obj = files[0]
            assert data['filename'] == obj.key
            assert data['checksum'] == obj.file.checksum
            assert data['id'] == str(obj.file.id)
def test_file_put_oauth2(api, deposit, files, users,
                         write_token_user_1):
    """PUT a deposit file with oauth2."""
    with api.test_request_context():
        with api.test_client() as client:
            old_file_id = files[0].file_id
            old_filename = files[0].key
            new_filename = '{0}-new-name'.format(old_filename)
            # test rename file
            res = client.put(
                url_for('invenio_deposit_rest.depid_file',
                        pid_value=deposit['_deposit']['id'],
                        key=old_filename),
                data=json.dumps({'filename': new_filename}),
                headers=[
                    ('Authorization',
                     'Bearer {0}'.format(write_token_user_1.access_token))
                ]
            )
            deposit_id = deposit.id
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            files = list(deposit.files)
            assert res.status_code == 200
            assert new_filename == files[0].key
            assert old_file_id == files[0].file_id
            data = json.loads(res.data.decode('utf-8'))
            obj = files[0]
            assert data['filename'] == obj.key
            assert data['checksum'] == obj.file.checksum
            assert data['id'] == str(obj.file.id)
Ejemplo n.º 20
0
def records():
    """Load records."""
    import pkg_resources
    from dojson.contrib.marc21 import marc21
    from dojson.contrib.marc21.utils import create_record, split_blob
    from flask_login import login_user, logout_user
    from invenio_accounts.models import User
    from invenio_deposit.api import Deposit

    users = User.query.all()

    # pkg resources the demodata
    data_path = pkg_resources.resource_filename(
        'invenio_records', 'data/marc21/bibliographic.xml')
    with open(data_path) as source:
        with current_app.test_request_context():
            indexer = RecordIndexer()
            with db.session.begin_nested():
                for index, data in enumerate(split_blob(source.read()),
                                             start=1):
                    login_user(users[index % len(users)])
                    # do translate
                    record = marc21.do(create_record(data))
                    # create record
                    indexer.index(Deposit.create(record))
                    logout_user()
            db.session.commit()
Ejemplo n.º 21
0
def test_publish_revision_changed_mergeable(app, db, location, fake_schemas):
    """Try to Publish and someone change the deposit in the while."""
    # create a deposit
    deposit = Deposit.create({"metadata": {"title": "title-1"}})
    deposit.commit()
    db.session.commit()
    # publish
    deposit.publish()
    db.session.commit()
    # edit
    deposit = deposit.edit()
    db.session.commit()
    # simulate a externally modification
    rid, record = deposit.fetch_published()
    rev_id = record.revision_id
    # try to change metadata
    record.update({"metadata": {"title": "title-1", "poster": "myposter"}})
    record.commit()
    db.session.commit()
    assert rev_id != record.revision_id
    # edit again and check the merging
    deposit.update({"metadata": {"title": "title-1", "description": "mydesc"}})
    deposit.commit()
    deposit.publish()
    db.session.commit()
    # check if is properly merged
    did, deposit = deposit.fetch_published()
    assert deposit["metadata"]["title"] == "title-1"
    assert deposit["metadata"]["poster"] == "myposter"
    assert deposit["metadata"]["description"] == "mydesc"
    assert deposit["$schema"] == "http://localhost/schemas/deposit-v1.0.0.json"
def test_file_delete(api, deposit, files, users):
    """Test delete file."""
    with api.test_request_context():
        # the user is the owner
        with api.test_client() as client:
            deposit_id = deposit.id
            res = client.delete(url_for(
                'invenio_deposit_rest.depid_file',
                pid_value=deposit['_deposit']['id'],
                key=files[0].key
            ))
            assert res.status_code == 401
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            assert files[0].key in deposit.files
            assert deposit.files[files[0].key] is not None
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[0]['email'],
                password="******"
            ))
            # delete resource
            res = client.delete(url_for(
                'invenio_deposit_rest.depid_file',
                pid_value=deposit['_deposit']['id'],
                key=files[0].key
            ))
            assert res.status_code == 204
            assert res.data == b''
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            assert files[0].key not in deposit.files

        # the user is NOT the owner
        with api.test_client() as client:
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[1]['email'],
                password="******"
            ))
            # delete resource
            res = client.delete(url_for(
                'invenio_deposit_rest.depid_file',
                pid_value=deposit['_deposit']['id'],
                key=files[0].key
            ))
            assert res.status_code == 403
def test_file_delete(api, deposit, files, users):
    """Test delete file."""
    with api.test_request_context():
        # the user is the owner
        with api.test_client() as client:
            deposit_id = deposit.id
            res = client.delete(url_for(
                'invenio_deposit_rest.depid_file',
                pid_value=deposit['_deposit']['id'],
                key=files[0].key
            ))
            assert res.status_code == 401
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            assert files[0].key in deposit.files
            assert deposit.files[files[0].key] is not None
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[0]['email'],
                password="******"
            ))
            # delete resource
            res = client.delete(url_for(
                'invenio_deposit_rest.depid_file',
                pid_value=deposit['_deposit']['id'],
                key=files[0].key
            ))
            assert res.status_code == 204
            assert res.data == b''
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            assert files[0].key not in deposit.files

        # the user is NOT the owner
        with api.test_client() as client:
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[1]['email'],
                password="******"
            ))
            # delete resource
            res = client.delete(url_for(
                'invenio_deposit_rest.depid_file',
                pid_value=deposit['_deposit']['id'],
                key=files[0].key
            ))
            assert res.status_code == 403
Ejemplo n.º 24
0
def test_schemas(app, fake_schemas):
    """Test schema URL transformations."""
    deposit = Deposit.create({})
    assert 'http://localhost/schemas/deposits/deposit-v1.0.0.json' == \
        deposit['$schema']

    assert 'http://localhost/schemas/deposit-v1.0.0.json' == \
        deposit.record_schema

    assert 'http://localhost/schemas/deposits/test-v1.0.0.json' == \
        deposit.build_deposit_schema({
            '$schema': 'http://localhost/schemas/test-v1.0.0.json',
        })

    with pytest.raises(RefResolutionError):
        Deposit.create({
            '$schema': 'http://localhost/schemas/deposits/invalid.json',
        })
Ejemplo n.º 25
0
def test_schemas(app, db, fake_schemas):
    """Test schema URL transformations."""
    deposit = Deposit.create({})
    assert 'http://localhost/schemas/deposits/deposit-v1.0.0.json' == \
        deposit['$schema']

    assert 'http://localhost/schemas/deposit-v1.0.0.json' == \
        deposit.record_schema

    assert 'http://localhost/schemas/deposits/test-v1.0.0.json' == \
        deposit.build_deposit_schema({
            '$schema': 'http://localhost/schemas/test-v1.0.0.json',
        })

    with pytest.raises(RefResolutionError):
        Deposit.create({
            '$schema': 'http://localhost/schemas/deposits/invalid.json',
        })
Ejemplo n.º 26
0
def depid(app, users, db):
    """New deposit with files."""
    record = {'title': {'title': 'fuu'}}
    with app.test_request_context():
        login_user(User.query.get(users[0]))
        deposit = Deposit.create(record)
        deposit.commit()
        db.session.commit()
    return deposit['_deposit']['id']
Ejemplo n.º 27
0
def test_simple_flow(app, fake_schemas, location):
    """Test simple flow of deposit states through its lifetime."""
    deposit = Deposit.create({})
    assert deposit['_deposit']['id']
    assert 'draft' == deposit.status
    assert 0 == deposit.revision_id

    deposit.publish()
    assert 'published' == deposit.status
    assert 1 == deposit.revision_id

    with pytest.raises(PIDInvalidAction):
        deposit.delete()
    with pytest.raises(PIDInvalidAction):
        deposit.clear()
    with pytest.raises(PIDInvalidAction):
        deposit.update(title='Revision 2')
    assert 'published' == deposit.status

    deposit = deposit.edit()
    assert 'draft' == deposit.status
    assert 2 == deposit.revision_id
    assert 0 == deposit['_deposit']['pid']['revision_id']

    with pytest.raises(PIDInvalidAction):
        deposit.edit()
    assert 'draft' == deposit.status

    deposit['title'] = 'Revision 1'
    deposit.publish()
    assert 'published' == deposit.status
    assert 3 == deposit.revision_id
    assert 0 == deposit['_deposit']['pid']['revision_id']

    deposit = deposit.edit()
    assert 'draft' == deposit.status
    assert 4 == deposit.revision_id
    assert 1 == deposit['_deposit']['pid']['revision_id']

    deposit['title'] = 'Revision 2'
    deposit.commit()
    assert 5 == deposit.revision_id

    (_, record) = deposit.fetch_published()
    record_schema_before = record['$schema']
    record_json = deepcopy(record.model.json)

    deposit = deposit.discard()
    assert 'published' == deposit.status
    assert 'Revision 1' == deposit['title']
    assert 6 == deposit.revision_id

    (_, record) = deposit.fetch_published()
    record_schema_after = record['$schema']
    assert record_schema_before == record_schema_after
    assert record_json == record.model.json
Ejemplo n.º 28
0
def test_simple_flow(app, fake_schemas, location):
    """Test simple flow of deposit states through its lifetime."""
    deposit = Deposit.create({})
    assert deposit['_deposit']['id']
    assert 'draft' == deposit.status
    assert 0 == deposit.revision_id

    deposit.publish()
    assert 'published' == deposit.status
    assert 1 == deposit.revision_id

    with pytest.raises(PIDInvalidAction):
        deposit.delete()
    with pytest.raises(PIDInvalidAction):
        deposit.clear()
    with pytest.raises(PIDInvalidAction):
        deposit.update(title='Revision 2')
    assert 'published' == deposit.status

    deposit = deposit.edit()
    assert 'draft' == deposit.status
    assert 2 == deposit.revision_id
    assert 0 == deposit['_deposit']['pid']['revision_id']

    with pytest.raises(PIDInvalidAction):
        deposit.edit()
    assert 'draft' == deposit.status

    deposit['title'] = 'Revision 1'
    deposit.publish()
    assert 'published' == deposit.status
    assert 3 == deposit.revision_id
    assert 0 == deposit['_deposit']['pid']['revision_id']

    deposit = deposit.edit()
    assert 'draft' == deposit.status
    assert 4 == deposit.revision_id
    assert 1 == deposit['_deposit']['pid']['revision_id']

    deposit['title'] = 'Revision 2'
    deposit.commit()
    assert 5 == deposit.revision_id

    (_, record) = deposit.fetch_published()
    record_schema_before = record['$schema']
    record_json = deepcopy(record.model.json)

    deposit = deposit.discard()
    assert 'published' == deposit.status
    assert 'Revision 1' == deposit['title']
    assert 6 == deposit.revision_id

    (_, record) = deposit.fetch_published()
    record_schema_after = record['$schema']
    assert record_schema_before == record_schema_after
    assert record_json == record.model.json
Ejemplo n.º 29
0
def test_simple_flow(app, db, fake_schemas, location):
    """Test simple flow of deposit states through its lifetime."""
    deposit = Deposit.create({})
    assert deposit["_deposit"]["id"]
    assert "draft" == deposit.status
    assert 0 == deposit.revision_id

    deposit.publish()
    assert "published" == deposit.status
    assert 1 == deposit.revision_id

    with pytest.raises(PIDInvalidAction):
        deposit.delete()
    with pytest.raises(PIDInvalidAction):
        deposit.clear()
    with pytest.raises(PIDInvalidAction):
        deposit.update(title="Revision 2")
    assert "published" == deposit.status

    deposit = deposit.edit()
    assert "draft" == deposit.status
    assert 2 == deposit.revision_id
    assert 0 == deposit["_deposit"]["pid"]["revision_id"]

    with pytest.raises(PIDInvalidAction):
        deposit.edit()
    assert "draft" == deposit.status

    deposit["title"] = "Revision 1"
    deposit.publish()
    assert "published" == deposit.status
    assert 3 == deposit.revision_id
    assert 0 == deposit["_deposit"]["pid"]["revision_id"]

    deposit = deposit.edit()
    assert "draft" == deposit.status
    assert 4 == deposit.revision_id
    assert 1 == deposit["_deposit"]["pid"]["revision_id"]

    deposit["title"] = "Revision 2"
    deposit.commit()
    assert 5 == deposit.revision_id

    (_, record) = deposit.fetch_published()
    record_schema_before = record["$schema"]
    record_json = deepcopy(record.model.json)

    deposit = deposit.discard()
    assert "published" == deposit.status
    assert "Revision 1" == deposit["title"]
    assert 6 == deposit.revision_id

    (_, record) = deposit.fetch_published()
    record_schema_after = record["$schema"]
    assert record_schema_before == record_schema_after
    assert record_json == record.model.json
Ejemplo n.º 30
0
def deposit(app_deposit, es, users, location, db):
    """Get new deposit with files."""
    record = {'title': 'fuu'}
    with app_deposit.test_request_context():
        login_user(users[0])
        deposit = Deposit.create(record)
        deposit.commit()
        db.session.commit()
    sleep(2)
    return deposit
def test_create_deposit_index(db, es):
    """Test if deposit index is created."""
    deposit_index_name = 'deposits-records-lhcb-v1.0.0'

    Deposit.create({
        '_deposit': {
            'status': 'draft',
            'pid': {
                'type': 'recid',
                'value': '1'
            },
            'id': 1
        }
    })
    db.session.commit()
    current_search.flush_and_refresh(deposit_index_name)
    res = current_search.client.search(index=deposit_index_name)
    # Make sure the '_deposit' is added in the record
    assert '_deposit' in res['hits']['hits'][0]['_source']
Ejemplo n.º 32
0
def deposit(app, es, users, location):
    """New deposit with files."""
    record = {"title": "fuu"}
    with app.test_request_context():
        datastore = app.extensions["security"].datastore
        login_user(datastore.find_user(email=users[0]["email"]))
        deposit = Deposit.create(record)
        deposit.commit()
        db.session.commit()
    sleep(2)
    return deposit
def test_create_deposit_cms_questionnaire_index(db, es, deposit_index):
    """Test if deposit cms questionnaire index is created."""
    deposit_index_name = deposit_index + '-' + 'cms-questionnaire-v0.0.1'

    Deposit.create({
        '$schema': 'https://localhost:5000/schemas/deposits/records/cms-questionnaire-v0.0.1.json',
        '_deposit': {
            'status': 'draft',
            'pid': {
                'type': 'recid',
                'value': '1'
            },
            'id': '0a7dac44ac234fd39d941fa14bae63c3'

        }
    })
    db.session.commit()
    current_search.flush_and_refresh(deposit_index_name)
    res = current_search.client.search(index=deposit_index_name)
    assert 'id' in res['hits']['hits'][0]['_source']['_deposit']
Ejemplo n.º 34
0
def deposit(app, es, users, location):
    """New deposit with files."""
    record = {
        'title': 'fuu'
    }
    with app.test_request_context():
        login_user(users[0])
        deposit = Deposit.create(record)
        deposit.commit()
        db_.session.commit()
    sleep(2)
    return deposit
Ejemplo n.º 35
0
def deposit(app, es, users, location):
    """New deposit with files."""
    record = {
        'title': 'fuu'
    }
    with app.test_request_context():
        datastore = app.extensions['security'].datastore
        login_user(datastore.find_user(email=users[0]['email']))
        deposit = Deposit.create(record)
        deposit.commit()
        db.session.commit()
    sleep(2)
    return deposit
def test_file_delete_oauth2(api, deposit, files, users, write_token_user_1):
    """Test delete file with oauth2."""
    with api.test_request_context():
        with api.test_client() as client:
            deposit_id = deposit.id
            # delete resource
            res = client.delete(url_for('invenio_deposit_rest.depid_file',
                                        pid_value=deposit['_deposit']['id'],
                                        key=files[0].key),
                                headers=[('Authorization', 'Bearer {0}'.format(
                                    write_token_user_1.access_token))])
            assert res.status_code == 204
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            assert files[0].key not in deposit.files
Ejemplo n.º 37
0
def test_simple_flow(app, db, fake_schemas, location):
    """Test simple flow of deposit states through its lifetime."""
    deposit = Deposit.create({})
    assert deposit['_deposit']['id']
    assert 'draft' == deposit['_deposit']['status']
    assert 0 == deposit.revision_id

    deposit.publish()
    assert 'published' == deposit['_deposit']['status']
    assert 1 == deposit.revision_id

    with pytest.raises(PIDInvalidAction):
        deposit.delete()
    with pytest.raises(PIDInvalidAction):
        deposit.clear()
    with pytest.raises(PIDInvalidAction):
        deposit.update(title='Revision 2')
    assert 'published' == deposit['_deposit']['status']

    deposit = deposit.edit()
    assert 'draft' == deposit['_deposit']['status']
    assert 2 == deposit.revision_id
    assert 0 == deposit['_deposit']['pid']['revision_id']

    with pytest.raises(PIDInvalidAction):
        deposit.edit()
    assert 'draft' == deposit['_deposit']['status']

    deposit['title'] = 'Revision 1'
    deposit.publish()
    assert 'published' == deposit['_deposit']['status']
    assert 3 == deposit.revision_id
    assert 0 == deposit['_deposit']['pid']['revision_id']

    deposit = deposit.edit()
    assert 'draft' == deposit['_deposit']['status']
    assert 4 == deposit.revision_id
    assert 1 == deposit['_deposit']['pid']['revision_id']

    deposit['title'] = 'Revision 2'
    deposit.commit()
    assert 5 == deposit.revision_id

    deposit = deposit.discard()
    assert 'Revision 1' == deposit['title']
    assert 6 == deposit.revision_id
Ejemplo n.º 38
0
def test_publish_merge_conflict(app, db, es, users, location, deposit,
                                json_headers, fake_schemas):
    """Test publish with merge conflicts."""
    with app.test_request_context():
        with app.test_client() as client:
            user_info = dict(email=users[0].email, password='******')
            # login
            res = client.post(url_for_security('login'), data=user_info)

            # create a deposit
            deposit = Deposit.create({"metadata": {
                "title": "title-1",
            }})
            deposit.commit()
            db.session.commit()
            # publish
            deposit.publish()
            db.session.commit()
            # edit
            deposit = deposit.edit()
            db.session.commit()
            # simulate a externally modification
            rid, record = deposit.fetch_published()
            rev_id = record.revision_id
            record.update({'metadata': {
                "title": "title-2.1",
            }})
            record.commit()
            db.session.commit()
            assert rev_id != record.revision_id
            # edit again and check the merging
            deposit.update({"metadata": {
                "title": "title-2.2",
            }})
            deposit.commit()

            current_search.flush_and_refresh('_all')

            deposit_id = deposit.pid.pid_value
            res = client.post(
                url_for('invenio_deposit_rest.depid_actions',
                        pid_value=deposit_id, action='publish'),
            )
            assert res.status_code == 409
def test_files_put_oauth2(api, deposit, files, users,
                          write_token_user_1):
    """Test put deposit files with oauth2."""
    with api.test_request_context():
        with api.test_client() as client:
            # fixture
            content = b'### Testing textfile 2 ###'
            stream = BytesIO(content)
            key = 'world.txt'
            deposit.files[key] = stream
            deposit.commit()
            db.session.commit()
            assert deposit['_files'][0]['key'] == files[0].key
            assert deposit['_files'][1]['key'] == key
            assert len(deposit.files) == 2
            assert len(deposit['_files']) == 2
            deposit_id = deposit.id
            file_ids = [f.file_id for f in deposit.files]
            # order files
            res = client.put(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data=json.dumps([
                    {'id': str(file_ids[1])},
                    {'id': str(file_ids[0])}
                ]),
                headers=[
                    ('Authorization',
                     'Bearer {0}'.format(write_token_user_1.access_token))
                ]
            )
            assert res.status_code == 200
            data = json.loads(res.data.decode('utf-8'))
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            assert deposit['_files'][0]['key'] == data[0]['filename']
            assert deposit['_files'][1]['key'] == data[1]['filename']
            assert data[0]['id'] == str(file_ids[1])
            assert data[1]['id'] == str(file_ids[0])
            assert len(deposit.files) == 2
            assert len(deposit['_files']) == 2
            assert len(data) == 2
def test_file_delete_oauth2(api, deposit, files, users,
                            write_token_user_1):
    """Test delete file with oauth2."""
    with api.test_request_context():
        with api.test_client() as client:
            deposit_id = deposit.id
            # delete resource
            res = client.delete(
                url_for(
                    'invenio_deposit_rest.depid_file',
                    pid_value=deposit['_deposit']['id'],
                    key=files[0].key
                ),
                headers=[
                    ('Authorization',
                     'Bearer {0}'.format(write_token_user_1.access_token))
                ]
            )
            assert res.status_code == 204
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            assert files[0].key not in deposit.files
def test_file_put(api, deposit, files, users):
    """PUT a deposit file."""
    with api.test_request_context():
        with api.test_client() as client:
            old_file_id = files[0].file_id
            old_filename = files[0].key
            new_filename = '{0}-new-name'.format(old_filename)
            # test rename file (without login)
            res = client.put(
                url_for('invenio_deposit_rest.depid_file',
                        pid_value=deposit['_deposit']['id'],
                        key=old_filename),
                data=json.dumps({'filename': new_filename}))
            assert res.status_code == 401
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[0]['email'],
                password="******"
            ))
            # test rename file
            res = client.put(
                url_for('invenio_deposit_rest.depid_file',
                        pid_value=deposit['_deposit']['id'],
                        key=old_filename),
                data=json.dumps({'filename': new_filename}))
            deposit_id = deposit.id
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            files = list(deposit.files)
            assert res.status_code == 200
            assert new_filename == files[0].key
            assert old_file_id == files[0].file_id
            data = json.loads(res.data.decode('utf-8'))
            obj = files[0]
            assert data['filename'] == obj.key
            assert data['checksum'] == obj.file.checksum
            assert data['id'] == str(obj.file.id)
def test_files_put(api, deposit, files, users):
    """Test put deposit files."""
    with api.test_request_context():
        # the user is the owner
        with api.test_client() as client:
            # fixture
            content = b'### Testing textfile 2 ###'
            stream = BytesIO(content)
            key = 'world.txt'
            deposit.files[key] = stream
            deposit.commit()
            db.session.commit()
            deposit_id = deposit.id
            file_ids = [f.file_id for f in deposit.files]
            assert deposit['_files'][0]['key'] == files[0].key
            assert deposit['_files'][1]['key'] == key
            assert len(deposit.files) == 2
            assert len(deposit['_files']) == 2
            # add new file (without login)
            res = client.put(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data=json.dumps([
                    {'id': str(file_ids[1])},
                    {'id': str(file_ids[0])}
                ])
            )
            assert res.status_code == 401
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[0]['email'],
                password="******"
            ))
            # order files
            res = client.put(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data=json.dumps([
                    {'id': str(file_ids[1])},
                    {'id': str(file_ids[0])}
                ])
            )
            data = json.loads(res.data.decode('utf-8'))
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            assert deposit['_files'][0]['key'] == data[0]['filename']
            assert deposit['_files'][1]['key'] == data[1]['filename']
            assert data[0]['id'] == str(file_ids[1])
            assert data[1]['id'] == str(file_ids[0])
            assert len(deposit.files) == 2
            assert len(deposit['_files']) == 2
            assert len(data) == 2

        # the user is NOT the owner
        with api.test_client() as client:
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[1]['email'],
                password="******"
            ))
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            order = [f.key for f in deposit.files]
            # test files post
            res = client.put(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data=json.dumps([
                    {'id': str(file_ids[1])},
                    {'id': str(file_ids[0])}
                ])
            )
            assert res.status_code == 403
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            assert deposit['_files'][0]['key'] == order[0]
            assert deposit['_files'][1]['key'] == order[1]
Ejemplo n.º 43
0
def test_files_property(app, db, fake_schemas, location):
    """Test deposit files property."""
    with pytest.raises(MissingModelError):
        Deposit({}).files

    deposit = Deposit.create({})

    assert 0 == len(deposit.files)
    assert "invalid" not in deposit.files

    with pytest.raises(KeyError):
        deposit.files["invalid"]

    bucket = deposit.files.bucket
    assert bucket

    # Create first file:
    deposit.files["hello.txt"] = BytesIO(b"Hello world!")

    file_0 = deposit.files["hello.txt"]
    assert "hello.txt" == file_0["key"]
    assert 1 == len(deposit.files)

    # Update first file with new content:
    deposit.files["hello.txt"] = BytesIO(b"Hola mundo!")
    file_1 = deposit.files["hello.txt"]
    assert "hello.txt" == file_1["key"]
    assert 1 == len(deposit.files)

    assert file_0["version_id"] != file_1["version_id"]

    # Create second file and check number of items in files.
    deposit.files["second.txt"] = BytesIO(b"Second file.")
    assert deposit.files["second.txt"]
    assert 2 == len(deposit.files)
    assert "hello.txt" in deposit.files
    assert "second.txt" in deposit.files

    # Check order of files.
    order_0 = [f["key"] for f in deposit.files]
    assert ["hello.txt", "second.txt"] == order_0

    deposit.files.sort_by(*reversed(order_0))
    order_1 = [f["key"] for f in deposit.files]
    assert ["second.txt", "hello.txt"] == order_1

    # Try to rename second file to 'hello.txt'.
    with pytest.raises(Exception):
        deposit.files.rename("second.txt", "hello.txt")

    # Remove the 'hello.txt' file.
    del deposit.files["hello.txt"]
    assert "hello.txt" not in deposit.files
    # Make sure that 'second.txt' is still there.
    assert "second.txt" in deposit.files

    with pytest.raises(KeyError):
        del deposit.files["hello.txt"]

    # Now you can rename 'second.txt' to 'hello.txt'.
    deposit.files.rename("second.txt", "hello.txt")
    assert "second.txt" not in deposit.files
    assert "hello.txt" in deposit.files
def test_files_post(api, deposit, users):
    """Post a deposit file."""
    real_filename = 'real_test.json'
    content = b'### Testing textfile ###'
    digest = 'md5:{0}'.format(hashlib.md5(content).hexdigest())
    filename = 'test.json'
    with api.test_request_context():
        # the user is the owner
        with api.test_client() as client:
            # test post without login
            file_to_upload = (BytesIO(content), filename)
            res = client.post(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data={'file': file_to_upload, 'name': real_filename},
                content_type='multipart/form-data'
            )
            assert res.status_code == 401
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[0]['email'],
                password="******"
            ))
            # test empty post
            res = client.post(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data={'name': real_filename},
                content_type='multipart/form-data'
            )
            assert res.status_code == 400
            # test post
            file_to_upload = (BytesIO(content), filename)
            res = client.post(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data={'file': file_to_upload, 'name': real_filename},
                content_type='multipart/form-data'
            )
            deposit_id = deposit.id
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            files = list(deposit.files)
            assert res.status_code == 201
            assert real_filename == files[0].key
            assert digest == files[0].file.checksum
            data = json.loads(res.data.decode('utf-8'))
            obj = files[0]
            assert data['filename'] == obj.key
            assert data['checksum'] == obj.file.checksum
            assert data['id'] == str(obj.file.id)

        # the user is NOT the owner
        with api.test_client() as client:
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[1]['email'],
                password="******"
            ))
            # test post
            file_to_upload = (BytesIO(content), filename)
            res = client.post(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data={'file': file_to_upload, 'name': real_filename},
                content_type='multipart/form-data'
            )
            assert res.status_code == 403
def test_files_post(api, deposit, users):
    """Post a deposit file."""
    real_filename = 'real_test.json'
    content = b'### Testing textfile ###'
    digest = 'md5:{0}'.format(hashlib.md5(content).hexdigest())
    filename = 'test.json'
    with api.test_request_context():
        # the user is the owner
        with api.test_client() as client:
            # test post without login
            file_to_upload = (BytesIO(content), filename)
            res = client.post(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data={'file': file_to_upload, 'name': real_filename},
                content_type='multipart/form-data'
            )
            assert res.status_code == 401
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[0]['email'],
                password="******"
            ))
            # test empty post
            res = client.post(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data={'name': real_filename},
                content_type='multipart/form-data'
            )
            assert res.status_code == 400
            # test post
            file_to_upload = (BytesIO(content), filename)
            res = client.post(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data={'file': file_to_upload, 'name': real_filename},
                content_type='multipart/form-data'
            )
            deposit_id = deposit.id
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            files = list(deposit.files)
            assert res.status_code == 201
            assert real_filename == files[0].key
            assert digest == files[0].file.checksum
            data = json.loads(res.data.decode('utf-8'))
            obj = files[0]
            assert data['filename'] == obj.key
            assert data['checksum'] == obj.file.checksum
            assert data['id'] == str(obj.file.id)

        # the user is NOT the owner
        with api.test_client() as client:
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[1]['email'],
                password="******"
            ))
            # test post
            file_to_upload = (BytesIO(content), filename)
            res = client.post(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data={'file': file_to_upload, 'name': real_filename},
                content_type='multipart/form-data'
            )
            assert res.status_code == 403
def test_files_put(api, deposit, files, users):
    """Test put deposit files."""
    with api.test_request_context():
        # the user is the owner
        with api.test_client() as client:
            # fixture
            content = b'### Testing textfile 2 ###'
            stream = BytesIO(content)
            key = 'world.txt'
            deposit.files[key] = stream
            deposit.commit()
            db.session.commit()
            deposit_id = deposit.id
            file_ids = [f.file_id for f in deposit.files]
            assert deposit['_files'][0]['key'] == files[0].key
            assert deposit['_files'][1]['key'] == key
            assert len(deposit.files) == 2
            assert len(deposit['_files']) == 2
            # add new file (without login)
            res = client.put(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data=json.dumps([
                    {'id': str(file_ids[1])},
                    {'id': str(file_ids[0])}
                ])
            )
            assert res.status_code == 401
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[0]['email'],
                password="******"
            ))
            # order files
            res = client.put(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data=json.dumps([
                    {'id': str(file_ids[1])},
                    {'id': str(file_ids[0])}
                ])
            )
            data = json.loads(res.data.decode('utf-8'))
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            assert deposit['_files'][0]['key'] == data[0]['filename']
            assert deposit['_files'][1]['key'] == data[1]['filename']
            assert data[0]['id'] == str(file_ids[1])
            assert data[1]['id'] == str(file_ids[0])
            assert len(deposit.files) == 2
            assert len(deposit['_files']) == 2
            assert len(data) == 2

        # the user is NOT the owner
        with api.test_client() as client:
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[1]['email'],
                password="******"
            ))
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            order = [f.key for f in deposit.files]
            # test files post
            res = client.put(
                url_for('invenio_deposit_rest.depid_files',
                        pid_value=deposit['_deposit']['id']),
                data=json.dumps([
                    {'id': str(file_ids[1])},
                    {'id': str(file_ids[0])}
                ])
            )
            assert res.status_code == 403
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            assert deposit['_files'][0]['key'] == order[0]
            assert deposit['_files'][1]['key'] == order[1]
Ejemplo n.º 47
0
def test_files_property(app, fake_schemas, location):
    """Test deposit files property."""
    with pytest.raises(MissingModelError):
        Deposit({}).files

    deposit = Deposit.create({})

    assert 0 == len(deposit.files)
    assert 'invalid' not in deposit.files

    with pytest.raises(KeyError):
        deposit.files['invalid']

    bucket = deposit.files.bucket
    assert bucket

    # Create first file:
    deposit.files['hello.txt'] = BytesIO(b'Hello world!')

    file_0 = deposit.files['hello.txt']
    assert 'hello.txt' == file_0['key']
    assert 1 == len(deposit.files)

    # Update first file with new content:
    deposit.files['hello.txt'] = BytesIO(b'Hola mundo!')
    file_1 = deposit.files['hello.txt']
    assert 'hello.txt' == file_1['key']
    assert 1 == len(deposit.files)

    assert file_0['version_id'] != file_1['version_id']

    # Create second file and check number of items in files.
    deposit.files['second.txt'] = BytesIO(b'Second file.')
    assert deposit.files['second.txt']
    assert 2 == len(deposit.files)
    assert 'hello.txt' in deposit.files
    assert 'second.txt' in deposit.files

    # Check order of files.
    order_0 = [f['key'] for f in deposit.files]
    assert ['hello.txt', 'second.txt'] == order_0

    deposit.files.sort_by(*reversed(order_0))
    order_1 = [f['key'] for f in deposit.files]
    assert ['second.txt', 'hello.txt'] == order_1

    # Try to rename second file to 'hello.txt'.
    with pytest.raises(Exception):
        deposit.files.rename('second.txt', 'hello.txt')

    # Remove the 'hello.txt' file.
    del deposit.files['hello.txt']
    assert 'hello.txt' not in deposit.files
    # Make sure that 'second.txt' is still there.
    assert 'second.txt' in deposit.files

    with pytest.raises(KeyError):
        del deposit.files['hello.txt']

    # Now you can rename 'second.txt' to 'hello.txt'.
    deposit.files.rename('second.txt', 'hello.txt')
    assert 'second.txt' not in deposit.files
    assert 'hello.txt' in deposit.files
Ejemplo n.º 48
0
def test_files_property(app, fake_schemas, location):
    """Test deposit files property."""
    with pytest.raises(MissingModelError):
        Deposit({}).files

    deposit = Deposit.create({})

    assert 0 == len(deposit.files)
    assert 'invalid' not in deposit.files

    with pytest.raises(KeyError):
        deposit.files['invalid']

    bucket = deposit.files.bucket
    assert bucket

    # Create first file:
    deposit.files['hello.txt'] = BytesIO(b'Hello world!')

    file_0 = deposit.files['hello.txt']
    assert 'hello.txt' == file_0['key']
    assert 1 == len(deposit.files)

    # Update first file with new content:
    deposit.files['hello.txt'] = BytesIO(b'Hola mundo!')
    file_1 = deposit.files['hello.txt']
    assert 'hello.txt' == file_1['key']
    assert 1 == len(deposit.files)

    assert file_0['version_id'] != file_1['version_id']

    # Create second file and check number of items in files.
    deposit.files['second.txt'] = BytesIO(b'Second file.')
    assert deposit.files['second.txt']
    assert 2 == len(deposit.files)
    assert 'hello.txt' in deposit.files
    assert 'second.txt' in deposit.files

    # Check order of files.
    order_0 = [f['key'] for f in deposit.files]
    assert ['hello.txt', 'second.txt'] == order_0

    deposit.files.sort_by(*reversed(order_0))
    order_1 = [f['key'] for f in deposit.files]
    assert ['second.txt', 'hello.txt'] == order_1

    # Try to rename second file to 'hello.txt'.
    with pytest.raises(Exception):
        deposit.files.rename('second.txt', 'hello.txt')

    # Remove the 'hello.txt' file.
    del deposit.files['hello.txt']
    assert 'hello.txt' not in deposit.files
    # Make sure that 'second.txt' is still there.
    assert 'second.txt' in deposit.files

    with pytest.raises(KeyError):
        del deposit.files['hello.txt']

    # Now you can rename 'second.txt' to 'hello.txt'.
    deposit.files.rename('second.txt', 'hello.txt')
    assert 'second.txt' not in deposit.files
    assert 'hello.txt' in deposit.files
def test_files_put(app, db, deposit, files, users):
    """Test put deposit files."""
    with app.test_request_context():
        # the user is the owner
        with app.test_client() as client:
            # fixture
            content = b'### Testing textfile 2 ###'
            stream = BytesIO(content)
            key = 'world.txt'
            deposit.files[key] = stream
            deposit.commit()
            db.session.commit()
            deposit_id = deposit.id
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            key0 = files[0].key
            files = list(deposit.files)
            assert files[0]['key'] == str(key0)
            assert files[1]['key'] == str(key)
            # add new file (without login)
            res = client.put(
                url_for('invenio_deposit_rest.dep_files',
                        pid_value=deposit['_deposit']['id']),
                data=json.dumps([
                    {'id': key},
                    {'id': key0}
                ])
            )
            assert res.status_code == 401
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[0].email,
                password="******"
            ))
            # order files
            res = client.put(
                url_for('invenio_deposit_rest.dep_files',
                        pid_value=deposit['_deposit']['id']),
                data=json.dumps([
                    {'id': key},
                    {'id': key0}
                ])
            )
            db.session.expunge(deposit.model)
            deposit = Deposit.get_record(deposit_id)
            files = list(deposit.files)
            assert len(deposit.files) == 2
            assert files[0]['key'] == str(key)
            assert files[1]['key'] == str(key0)
            data = json.loads(res.data.decode('utf-8'))
            assert data[0]['filename'] == str(key)
            assert data[1]['filename'] == str(key0)

        # the user is NOT the owner
        with app.test_client() as client:
            # login
            res = client.post(url_for_security('login'), data=dict(
                email=users[1].email,
                password="******"
            ))
            # test files post
            res = client.put(
                url_for('invenio_deposit_rest.dep_files',
                        pid_value=deposit['_deposit']['id']),
                data=json.dumps([
                    {'id': key},
                    {'id': key0}
                ])
            )
            assert res.status_code == 403