def test_SIP_create(app, db, mocker): """Test the create method from SIP API.""" # we setup a file storage tmppath = tempfile.mkdtemp() db.session.add(Location(name='default', uri=tmppath, default=True)) db.session.commit() # we create a file content = b'test lol\n' bucket = Bucket.create() obj = ObjectVersion.create(bucket, 'test.txt', stream=BytesIO(content)) db.session.commit() files = [obj] # setup metadata mjson = SIPMetadataType(title='JSON Test', name='json-test', format='json', schema='url') marcxml = SIPMetadataType(title='MARC XML Test', name='marcxml-test', format='xml', schema='uri') db.session.add(mjson) db.session.add(marcxml) metadata = { 'json-test': json.dumps({ 'this': 'is', 'not': 'sparta' }), 'marcxml-test': '<record></record>' } # Let's create a SIP user = create_test_user('*****@*****.**') agent = {'email': '*****@*****.**', 'ip_address': '1.1.1.1'} sip = SIP.create(True, files=files, metadata=metadata, user_id=user.id, agent=agent) db.session.commit() assert SIP_.query.count() == 1 assert len(sip.files) == 1 assert len(sip.metadata) == 2 assert SIPFile.query.count() == 1 assert SIPMetadata.query.count() == 2 assert sip.user.id == user.id assert sip.agent == agent # we mock the user and the agent to test if the creation works app.config['SIPSTORE_AGENT_JSONSCHEMA_ENABLED'] = False mock_current_user = mocker.patch('invenio_sipstore.api.current_user') type(mock_current_user).is_anonymous = mocker.PropertyMock( return_value=True) sip = SIP.create(True, files=files, metadata=metadata) assert sip.model.user_id is None assert sip.user is None assert sip.agent == {} # finalization rmtree(tmppath)
def test_Archive(db): """Test the Archive model class.""" assert Archive.query.count() == 0 # we create an SIP, it will automatically create an Archive via signals user = create_test_user('*****@*****.**') sip = SIP.create(True, user_id=user.id, agent={'test': 'test'}) db.session.commit() assert Archive.query.count() == 1 ark = Archive.get_from_sip(sip.id) assert ark.sip.user.id == sip.user.id assert ark.status == ArchiveStatus.NEW assert ark.accession_id is None assert ark.archivematica_id is None # let's change the object ark.status = ArchiveStatus.REGISTERED ark.accession_id = '08' ark.archivematica_id = sip.id db.session.commit() ark = Archive.get_from_accession_id('08') assert Archive.query.count() == 1 assert ark.status == ArchiveStatus.REGISTERED assert ark.archivematica_id == sip.id # we try to get a non existing record assert Archive.get_from_sip(uuid.uuid4()) is None
def test_SIP_create(app, db, mocker): """Test the create method from SIP API.""" # we setup a file storage tmppath = tempfile.mkdtemp() db.session.add(Location(name='default', uri=tmppath, default=True)) db.session.commit() # we create a file content = b'test lol\n' bucket = Bucket.create() obj = ObjectVersion.create(bucket, 'test.txt', stream=BytesIO(content)) db.session.commit() files = [obj] # setup metadata mjson = SIPMetadataType(title='JSON Test', name='json-test', format='json', schema='url') marcxml = SIPMetadataType(title='MARC XML Test', name='marcxml-test', format='xml', schema='uri') db.session.add(mjson) db.session.add(marcxml) metadata = { 'json-test': json.dumps({'this': 'is', 'not': 'sparta'}), 'marcxml-test': '<record></record>' } # Let's create a SIP user = create_test_user('*****@*****.**') agent = {'email': '*****@*****.**', 'ip_address': '1.1.1.1'} sip = SIP.create(True, files=files, metadata=metadata, user_id=user.id, agent=agent) db.session.commit() assert SIP_.query.count() == 1 assert len(sip.files) == 1 assert len(sip.metadata) == 2 assert SIPFile.query.count() == 1 assert SIPMetadata.query.count() == 2 assert sip.user.id == user.id assert sip.agent == agent # we mock the user and the agent to test if the creation works app.config['SIPSTORE_AGENT_JSONSCHEMA_ENABLED'] = False mock_current_user = mocker.patch('invenio_sipstore.api.current_user') type(mock_current_user).is_anonymous = mocker.PropertyMock( return_value=True) sip = SIP.create(True, files=files, metadata=metadata) assert sip.model.user_id is None assert sip.user is None assert sip.agent == {} # finalization rmtree(tmppath)
def test_listeners(conf, expected_status, app, db): """Test listener_sip_created and listener_record_updated functions.""" # first we change the is_archivable function app.config['ARCHIVEMATICA_ISARCHIVABLE_FACTORY'] = conf assert Archive.query.count() == 0 # let's create an SIP user = create_test_user('*****@*****.**') sip = SIP.create(True, user_id=user.id, agent={'test': 'test'}) db.session.commit() assert Archive.query.count() == 1 ark = Archive.get_from_sip(sip.id) assert ark.sip.user.id == sip.user.id assert ark.status == expected_status
def test_RecordSIP(db): """Test RecordSIP API class.""" user = create_test_user('*****@*****.**') agent = {'email': '*****@*****.**', 'ip_address': '1.1.1.1'} # we create a record recid = uuid.uuid4() pid = PersistentIdentifier.create('recid', '1337', object_type='rec', object_uuid=recid, status=PIDStatus.REGISTERED) title = {'title': 'record test'} record = Record.create(title, recid) # we create the models sip = SIP.create(True, user_id=user.id, agent=agent) recordsip = RecordSIP_(sip_id=sip.id, pid_id=pid.id) db.session.commit() # We create an API SIP on top of it api_recordsip = RecordSIP(recordsip, sip) assert api_recordsip.model is recordsip assert api_recordsip.sip.id == sip.id
def test_RecordSIP(db): """Test RecordSIP API class.""" user = create_test_user('*****@*****.**') agent = {'email': '*****@*****.**', 'ip_address': '1.1.1.1'} # we create a record recid = uuid.uuid4() pid = PersistentIdentifier.create( 'recid', '1337', object_type='rec', object_uuid=recid, status=PIDStatus.REGISTERED) title = {'title': 'record test'} record = Record.create(title, recid) # we create the models sip = SIP.create(True, user_id=user.id, agent=agent) recordsip = RecordSIP_(sip_id=sip.id, pid_id=pid.id) db.session.commit() # We create an API SIP on top of it api_recordsip = RecordSIP(recordsip, sip) assert api_recordsip.model is recordsip assert api_recordsip.sip.id == sip.id