예제 #1
0
def test_publish_deleted_published(app, db, schemas):
    TestDraftRecord.schema = schemas['draft']
    TestPublishedRecord.schema = schemas['published']
    with db.session.begin_nested():
        published_uuid = uuid.uuid4()
        published_record = TestPublishedRecord.create(
            {
                'id': '1',
                'title': '11',
                '$schema': 'records/record-v1.0.0.json'
            },
            id_=published_uuid)
        published_pid = PersistentIdentifier.create(
            pid_type='recid',
            pid_value='1',
            status=PIDStatus.REGISTERED,
            object_type='rec',
            object_uuid=published_uuid)
        assert published_record.revision_id == 0

        draft_uuid = uuid.uuid4()
        rec = TestDraftRecord.create({
            'id': '1',
            'title': '22'
        },
                                     id_=draft_uuid)
        draft_pid = PersistentIdentifier.create(pid_type='drecid',
                                                pid_value='1',
                                                status=PIDStatus.REGISTERED,
                                                object_type='rec',
                                                object_uuid=draft_uuid)

    with db.session.begin_nested():
        published_record.delete()
        published_pid.status = PIDStatus.DELETED
        db.session.add(published_pid)

    with db.session.begin_nested():
        rec = TestDraftRecord.get_record(draft_uuid)
        draft_pid = PersistentIdentifier.get(pid_type='drecid', pid_value='1')
        with disable_test_authenticated():
            current_drafts.publish(
                RecordContext(record=rec, record_pid=draft_pid))

    with db.session.begin_nested():
        # draft should be gone
        draft_pid = PersistentIdentifier.get(pid_type='drecid', pid_value='1')
        assert draft_pid.status == PIDStatus.DELETED
        rec = TestDraftRecord.get_record(draft_uuid, with_deleted=True)
        assert rec.model.json is None

        published_pid = PersistentIdentifier.get(pid_type='recid',
                                                 pid_value='1')
        assert published_pid.status == PIDStatus.REGISTERED
        rec = TestPublishedRecord.get_record(published_pid.object_uuid)
        assert rec['title'] == '22'
        # revision 0 original, 1 deleted, 2 temporarily reverted to orig, 3 published
        assert rec.revision_id == 3
예제 #2
0
def test_publish_record_marshmallow(app, db, schemas):
    TestDraftRecord.schema = schemas['draft']
    TestPublishedRecord.schema = schemas['published']
    with db.session.begin_nested():
        draft_uuid = uuid.uuid4()

        rec = TestDraftRecord.create({'id': '1'}, id_=draft_uuid)
        draft_pid = PersistentIdentifier.create(pid_type='drecid',
                                                pid_value='1',
                                                status=PIDStatus.REGISTERED,
                                                object_type='rec',
                                                object_uuid=draft_uuid)

        with pytest.raises(InvalidRecordException):
            # title is required but not in rec, so should fail
            with disable_test_authenticated():
                current_drafts.publish(
                    RecordContext(record=rec, record_pid=draft_pid))

        with pytest.raises(PIDDoesNotExistError):
            # no record should be created
            PersistentIdentifier.get(pid_type='recid', pid_value='1')

        # make the record valid
        rec['title'] = 'blah'
        rec.commit()

        assert rec['invenio_draft_validation']['valid']

        # and publish it again
        with disable_test_authenticated():
            current_drafts.publish(
                RecordContext(record=rec, record_pid=draft_pid))

        # draft should be gone
        draft_pid = PersistentIdentifier.get(pid_type='drecid', pid_value='1')
        assert draft_pid.status == PIDStatus.DELETED
        rec = TestDraftRecord.get_record(draft_uuid, with_deleted=True)
        assert rec.model.json is None

        published_pid = PersistentIdentifier.get(pid_type='recid',
                                                 pid_value='1')
        assert published_pid.status == PIDStatus.REGISTERED
        rec = TestPublishedRecord.get_record(published_pid.object_uuid)
        assert rec.model.json is not None
예제 #3
0
def test_draft_record_deleted_draft(app, db, schemas):
    TestDraftRecord.schema = schemas['draft']
    TestPublishedRecord.schema = schemas['published']
    with db.session.begin_nested():
        published_uuid = uuid.uuid4()
        published_record = TestPublishedRecord.create(
            {
                'id': '1',
                'title': '11'
            }, id_=published_uuid)
        published_pid = PersistentIdentifier.create(
            pid_type='recid',
            pid_value='1',
            status=PIDStatus.REGISTERED,
            object_type='rec',
            object_uuid=published_uuid)
        assert published_record.revision_id == 0

        draft_uuid = uuid.uuid4()
        draft_record = TestDraftRecord.create({
            'id': '1',
            'title': '22'
        },
                                              id_=draft_uuid)
        draft_pid = PersistentIdentifier.create(pid_type='drecid',
                                                pid_value='1',
                                                status=PIDStatus.REGISTERED,
                                                object_type='rec',
                                                object_uuid=draft_uuid)
        assert draft_record.revision_id == 0

    with db.session.begin_nested():
        draft_record.delete()
        draft_pid.status = PIDStatus.DELETED
        db.session.add(draft_pid)

    with db.session.begin_nested():
        with disable_test_authenticated():
            current_drafts.edit(
                RecordContext(record=published_record,
                              record_pid=published_pid))

        # published version should be there unchanged
        published_pid = PersistentIdentifier.get(pid_type='recid',
                                                 pid_value='1')
        assert published_pid.status == PIDStatus.REGISTERED
        rec = TestDraftRecord.get_record(published_uuid, with_deleted=True)
        assert rec['title'] == '11'
        assert rec.revision_id == 0

        # draft version should be there unchanged
        draft_pid = PersistentIdentifier.get(pid_type='drecid', pid_value='1')
        assert draft_pid.status == PIDStatus.REGISTERED
        rec = TestDraftRecord.get_record(draft_pid.object_uuid)
        assert rec.model.json is not None
        assert rec['title'] == '11'
        assert rec.revision_id == 4
예제 #4
0
def test_publish_record_with_previous_version(app, db, schemas):
    TestDraftRecord.schema = schemas['draft']
    TestPublishedRecord.schema = schemas['published']
    with db.session.begin_nested():
        published_uuid = uuid.uuid4()
        PersistentIdentifier.create(pid_type='recid',
                                    pid_value='1',
                                    status=PIDStatus.REGISTERED,
                                    object_type='rec',
                                    object_uuid=published_uuid)
        published_record = TestPublishedRecord.create(
            {
                'id': '1',
                'title': '11'
            }, id_=published_uuid)
        assert published_record.revision_id == 0

        draft_uuid = uuid.uuid4()
        draft_pid = PersistentIdentifier.create(pid_type='drecid',
                                                pid_value='1',
                                                status=PIDStatus.REGISTERED,
                                                object_type='rec',
                                                object_uuid=draft_uuid)
        draft_record = TestDraftRecord.create({
            'id': '1',
            'title': '22'
        },
                                              id_=draft_uuid)
        assert draft_record.revision_id == 0

        print(draft_record['invenio_draft_validation'])

        # and publish it again
        with disable_test_authenticated():
            current_drafts.publish(
                RecordContext(record=draft_record, record_pid=draft_pid))

        # draft should be gone
        draft_pid = PersistentIdentifier.get(pid_type='drecid', pid_value='1')
        assert draft_pid.status == PIDStatus.DELETED
        rec = TestDraftRecord.get_record(draft_uuid, with_deleted=True)
        assert rec.model.json is None

        published_pid = PersistentIdentifier.get(pid_type='recid',
                                                 pid_value='1')
        assert published_pid.status == PIDStatus.REGISTERED
        rec = TestPublishedRecord.get_record(published_pid.object_uuid)
        assert rec.model.json is not None
        assert rec['title'] == '22'
        assert rec.revision_id == 1
예제 #5
0
def test_unpublish_record_redirected_draft(app, db, schemas):
    TestDraftRecord.schema = schemas['draft']
    TestPublishedRecord.schema = schemas['published']
    with db.session.begin_nested():
        published_uuid = uuid.uuid4()
        published_record = TestPublishedRecord.create(
            {
                'id': '1',
                'title': '11'
            }, id_=published_uuid)
        published_pid = PersistentIdentifier.create(
            pid_type='recid',
            pid_value='1',
            status=PIDStatus.REGISTERED,
            object_type='rec',
            object_uuid=published_uuid)
        assert published_record.revision_id == 0

        draft_uuid = uuid.uuid4()
        draft_record = TestDraftRecord.create({
            'id': '1',
            'title': '22'
        },
                                              id_=draft_uuid)
        draft_pid = PersistentIdentifier.create(pid_type='drecid',
                                                pid_value='1',
                                                status=PIDStatus.REGISTERED,
                                                object_type='rec',
                                                object_uuid=draft_uuid)
        assert draft_record.revision_id == 0

    with db.session.begin_nested():
        draft_record.delete()
        draft_pid.status = PIDStatus.REDIRECTED
        db.session.add(draft_pid)

    with db.session.begin_nested():
        with pytest.raises(NotImplementedError):
            with disable_test_authenticated():
                current_drafts.unpublish(
                    RecordContext(record=published_record,
                                  record_pid=published_pid))
예제 #6
0
def test_publish(app, db, schemas, mappings, prepare_es):
    with disable_test_authenticated():
        with db.session.begin_nested():
            draft_uuid = uuid.uuid4()

            rec1 = DraftRecord.create({
                'id': '1',
                'title': 'rec1'
            }, id_=draft_uuid)
            draft1_pid = PersistentIdentifier.create(
                pid_type='drecid', pid_value='1', status=PIDStatus.REGISTERED,
                object_type='rec', object_uuid=draft_uuid
            )

            published_uuid = uuid.uuid4()
            published = PublishedRecord.create({
                'id': '3',
                'title': 'rec1a'
            }, id_=published_uuid)
            published_pid = PersistentIdentifier.create(
                pid_type='recid', pid_value='3', status=PIDStatus.REGISTERED,
                object_type='rec', object_uuid=published_uuid
            )

            draft2_uuid = uuid.uuid4()
            rec2 = DraftRecord.create({
                'id': '2',
                'title': 'rec2',
                'ref': {'$ref': 'http://localhost/drafts/records/1'},
                'ref_pub': {'$ref': 'http://localhost/records/3'}
            }, id_=draft2_uuid)
            draft2_pid = PersistentIdentifier.create(
                pid_type='drecid', pid_value='2', status=PIDStatus.REGISTERED,
                object_type='rec', object_uuid=draft2_uuid
            )
            RecordIndexer().index(rec2)

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

        es_draft2 = RecordsSearch(index='draft-records-record-v1.0.0').\
            get_record(draft2_pid.object_uuid).execute()
        assert len(es_draft2.hits) == 1

        current_drafts.publish(RecordContext(record=rec2, record_pid=draft2_pid))

        published2_pid = PersistentIdentifier.get(pid_type='recid', pid_value=draft2_pid.pid_value)
        pr = PublishedRecord.get_record(published2_pid.object_uuid)
        assert pr.dumps() == {
            '$schema': 'https://localhost/schemas/records/record-v1.0.0.json',
            'id': '2',
            'ref': {'$ref': 'http://localhost/records/1'},
            'ref_pub': {'$ref': 'http://localhost/records/3'},
            'title': 'rec2'
        }

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

        es_published2 = RecordsSearch(index='records-record-v1.0.0').\
            get_record(published2_pid.object_uuid).execute()
        assert len(es_published2.hits) == 1
        es_published2 = es_published2.hits[0].to_dict()
        es_published2.pop('_created')
        es_published2.pop('_updated')
        assert es_published2 == {
            '$schema': 'https://localhost/schemas/records/record-v1.0.0.json',
            'id': '2',
            'ref': {'published': '1'},
            'ref_pub': {'published': '3'},
            'title': 'rec2'}

        es_draft2 = RecordsSearch(index='draft-records-record-v1.0.0').\
            get_record(draft2_pid.object_uuid).execute()
        assert len(es_draft2.hits) == 0
예제 #7
0
def test_unpublish(app, db, schemas, mappings, prepare_es):
    with disable_test_authenticated():
        with db.session.begin_nested():
            published_uuid1 = uuid.uuid4()

            published_rec1 = PublishedRecord.create(
                {
                    'id': '1',
                    'title': 'rec1'
                }, id_=published_uuid1)
            published_pid1 = PersistentIdentifier.create(
                pid_type='recid',
                pid_value='1',
                status=PIDStatus.REGISTERED,
                object_type='rec',
                object_uuid=published_uuid1)

            published_uuid2 = uuid.uuid4()
            published_rec2 = PublishedRecord.create(
                {
                    'id': '2',
                    'title': 'rec2'
                }, id_=published_uuid2)
            published_pid2 = PersistentIdentifier.create(
                pid_type='recid',
                pid_value='2',
                status=PIDStatus.REGISTERED,
                object_type='rec',
                object_uuid=published_uuid2)

            published_uuid3 = uuid.uuid4()
            published_rec3 = PublishedRecord.create(
                {
                    'id': '3',
                    'title': 'rec3',
                    'ref': {
                        '$ref': 'http://localhost/records/1'
                    },
                    'ref_pub': {
                        '$ref': 'http://localhost/records/2'
                    }
                },
                id_=published_uuid3)
            published_pid3 = PersistentIdentifier.create(
                pid_type='recid',
                pid_value='3',
                status=PIDStatus.REGISTERED,
                object_type='rec',
                object_uuid=published_uuid3)
            RecordIndexer().index(published_rec1)
            RecordIndexer().index(published_rec2)
            RecordIndexer().index(published_rec3)

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

        es = RecordsSearch(index='draft-records-record-v1.0.0'). \
            get_record(published_pid3.object_uuid).execute()
        assert len(es.hits) == 0

        es = RecordsSearch(index='records-record-v1.0.0'). \
            get_record(published_pid3.object_uuid).execute()
        assert len(es.hits) == 1

        current_drafts.unpublish(
            RecordContext(record=published_rec3, record_pid=published_pid3))

        assert PersistentIdentifier.get(
            pid_type='recid',
            pid_value=published_pid3.pid_value).status == PIDStatus.DELETED

        assert PersistentIdentifier.get(
            pid_type='recid',
            pid_value=published_pid2.pid_value).status == PIDStatus.DELETED

        assert PersistentIdentifier.get(
            pid_type='recid',
            pid_value=published_pid1.pid_value).status == PIDStatus.DELETED

        draft_pid3 = PersistentIdentifier.get(
            pid_type='drecid', pid_value=published_pid3.pid_value)
        pr = DraftRecord.get_record(draft_pid3.object_uuid)
        assert pr.dumps() == {
            '$schema':
            'https://localhost/schemas/draft/records/record-v1.0.0.json',
            'id': '3',
            'invenio_draft_validation': {
                'valid': True
            },
            'ref': {
                '$ref': 'http://localhost/drafts/records/1'
            },
            'ref_pub': {
                '$ref': 'http://localhost/drafts/records/2'
            },
            'title': 'rec3'
        }

        draft_pid2 = PersistentIdentifier.get(
            pid_type='drecid', pid_value=published_pid2.pid_value)
        pr = DraftRecord.get_record(draft_pid2.object_uuid)
        assert pr.dumps() == {
            '$schema':
            'https://localhost/schemas/draft/records/record-v1.0.0.json',
            'id': '2',
            'invenio_draft_validation': {
                'valid': True
            },
            'title': 'rec2'
        }

        draft_pid1 = PersistentIdentifier.get(
            pid_type='drecid', pid_value=published_pid1.pid_value)
        pr = DraftRecord.get_record(draft_pid1.object_uuid)
        assert pr.dumps() == {
            '$schema':
            'https://localhost/schemas/draft/records/record-v1.0.0.json',
            'id': '1',
            'invenio_draft_validation': {
                'valid': True
            },
            'title': 'rec1'
        }

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

        es = RecordsSearch(index='draft-records-record-v1.0.0'). \
            get_record(draft_pid3.object_uuid).execute()
        assert len(es.hits) == 1
        es = es.hits[0].to_dict()
        es.pop('_created')
        es.pop('_updated')
        assert es == {
            '$schema':
            'https://localhost/schemas/draft/records/record-v1.0.0.json',
            'id': '3',
            'invenio_draft_validation': {
                'valid': True
            },
            'ref': {
                'draft': '1'
            },
            'ref_pub': {
                'draft': '2'
            },
            'title': 'rec3'
        }

        es = RecordsSearch(index='records-record-v1.0.0'). \
            get_record(published_pid3.object_uuid).execute()
        assert len(es.hits) == 0

        es = RecordsSearch(index='draft-records-record-v1.0.0'). \
            get_record(draft_pid1.object_uuid).execute()
        assert len(es.hits) == 1
        es = es.hits[0].to_dict()
        es.pop('_created')
        es.pop('_updated')
        assert es == {
            '$schema':
            'https://localhost/schemas/draft/records/record-v1.0.0.json',
            'id': '1',
            'invenio_draft_validation': {
                'valid': True
            },
            'title': 'rec1'
        }

        es = RecordsSearch(index='records-record-v1.0.0'). \
            get_record(published_pid1.object_uuid).execute()
        assert len(es.hits) == 0

        es = RecordsSearch(index='records-record-v1.0.0'). \
            get_record(published_pid3.object_uuid).execute()
        assert len(es.hits) == 0

        es = RecordsSearch(index='draft-records-record-v1.0.0'). \
            get_record(draft_pid2.object_uuid).execute()
        assert len(es.hits) == 1
        es = es.hits[0].to_dict()
        es.pop('_created')
        es.pop('_updated')
        assert es == {
            '$schema':
            'https://localhost/schemas/draft/records/record-v1.0.0.json',
            'id': '2',
            'invenio_draft_validation': {
                'valid': True
            },
            'title': 'rec2'
        }

        es = RecordsSearch(index='records-record-v1.0.0'). \
            get_record(published_pid2.object_uuid).execute()
        assert len(es.hits) == 0