def test_file_download_ui(app, objects): """Test get buckets.""" app.config.update(dict( FILES_REST_PERMISSION_FACTORY=lambda *a, **kw: type( 'Allow', (object, ), {'can': lambda self: True} )(), RECORDS_UI_DEFAULT_PERMISSION_FACTORY=None, # No permission checking RECORDS_UI_ENDPOINTS=dict( recid=dict( pid_type='recid', route='/records/<pid_value>', ), recid_files=dict( pid_type='recid', route='/records/<pid_value>/files/<filename>', view_imp='invenio_records_files.utils:file_download_ui', record_class='invenio_records_files.api:Record', ), ) )) InvenioRecordsUI(app) obj1 = objects[0] with app.test_request_context(): # Record 1 - Live record rec_uuid = uuid.uuid4() PersistentIdentifier.create( 'recid', '1', object_type='rec', object_uuid=rec_uuid, status=PIDStatus.REGISTERED) record = Record.create({ 'title': 'Registered', 'recid': 1, '_files': [ {'key': obj1.key, 'bucket': str(obj1.bucket_id), 'checksum': 'invalid'}, ] }, id_=rec_uuid) RecordsBuckets.create(record=record.model, bucket=obj1.bucket) db.session.commit() main_url = url_for('invenio_records_ui.recid', pid_value='1') file_url = url_for( 'invenio_records_ui.recid_files', pid_value='1', filename=obj1.key) no_file_url = url_for( 'invenio_records_ui.recid_files', pid_value='1', filename='') invalid_file_url = url_for( 'invenio_records_ui.recid_files', pid_value='1', filename='no') with app.test_client() as client: res = client.get(main_url) assert res.status_code == 200 res = client.get(file_url) assert res.status_code == 200 res = client.get(no_file_url) assert res.status_code == 404 res = client.get(invalid_file_url) assert res.status_code == 404
def record_with_bucket(full_record, bucket, db): """Create a bucket.""" record = RecordFile.create(full_record) RecordsBuckets.create(bucket=bucket, record=record.model) pid = PersistentIdentifier.create( pid_type='recid', pid_value=12345, object_type='rec', object_uuid=record.id, status='R') db.session.commit() return pid, record
def record_with_bucket(db, full_record, bucket, sip_metadata_types): """Create a bucket.""" record = ZenodoRecord.create(full_record) RecordsBuckets.create(bucket=bucket, record=record.model) pid = PersistentIdentifier.create( pid_type='recid', pid_value=12345, object_type='rec', object_uuid=record.id, status='R') db.session.commit() return pid, record
def record_with_bucket(full_record, bucket, db): """Create a bucket.""" record = RecordFile.create(full_record) RecordsBuckets.create(bucket=bucket, record=record.model) pid = PersistentIdentifier.create(pid_type='recid', pid_value=1, object_type='rec', object_uuid=record.id, status='R') db.session.commit() return pid, record
def record_with_bucket(db, full_record, bucket, sip_metadata_types): """Create a bucket.""" record = ZenodoRecord.create(full_record) record['_buckets']['record'] = str(bucket.id) record['_files'][0]['bucket'] = str(bucket.id) record.commit() RecordsBuckets.create(bucket=bucket, record=record.model) pid = PersistentIdentifier.create( pid_type='recid', pid_value=12345, object_type='rec', object_uuid=record.id, status='R') db.session.commit() return pid, record
def test_bucket_link_factory_has_bucket(app, db, location, bucket): """Test bucket link factory retrieval of a bucket.""" with app.test_request_context(): with db.session.begin_nested(): record = RecordMetadata() RecordsBuckets.create(record, bucket) db.session.add(record) pid = mock.Mock() pid.get_assigned_object.return_value = record.id assert default_bucket_link_factory(pid) == url_for( 'invenio_files_rest.bucket_api', bucket_id=bucket.id, _external=True)
def test_record_files_link_factory(app, db, location, bucket): """Test record files link factory.""" with app.test_request_context(): with db.session.begin_nested(): record = RecordMetadata() RecordsBuckets.create(record, bucket) db.session.add(record) pid = mock.Mock() pid.pid_value = 1 pid.pid_type = "recid" pid.get_assigned_object.return_value = record.id links = default_record_files_links_factory(pid) assert links == { "files": "http://localhost/records/1/files", "self": "http://localhost/records/1", }
def record_with_bucket(record, bucket, db): """Create a bucket.""" RecordsBuckets.create(bucket=bucket, record=record.model) db.session.commit() return record
def test_files_property(app, db, location, bucket): """Test record files property.""" with pytest.raises(MissingModelError): Record({}).files record = Record.create({}) RecordsBuckets.create(bucket=bucket, record=record.model) assert 0 == len(record.files) assert 'invalid' not in record.files # make sure that _files key is not added after accessing record.files assert '_files' not in record with pytest.raises(KeyError): record.files['invalid'] bucket = record.files.bucket assert bucket # Create first file: record.files['hello.txt'] = BytesIO(b'Hello world!') file_0 = record.files['hello.txt'] assert 'hello.txt' == file_0['key'] assert 1 == len(record.files) assert 1 == len(record['_files']) # Update first file with new content: record.files['hello.txt'] = BytesIO(b'Hola mundo!') file_1 = record.files['hello.txt'] assert 'hello.txt' == file_1['key'] assert 1 == len(record.files) assert 1 == len(record['_files']) assert file_0['version_id'] != file_1['version_id'] # Create second file and check number of items in files. record.files['second.txt'] = BytesIO(b'Second file.') record.files['second.txt'] assert 2 == len(record.files) assert 'hello.txt' in record.files assert 'second.txt' in record.files # Check order of files. order_0 = [f['key'] for f in record.files] assert ['hello.txt', 'second.txt'] == order_0 record.files.sort_by(*reversed(order_0)) order_1 = [f['key'] for f in record.files] assert ['second.txt', 'hello.txt'] == order_1 # Try to rename second file to 'hello.txt'. with pytest.raises(Exception): record.files.rename('second.txt', 'hello.txt') # Remove the 'hello.txt' file. del record.files['hello.txt'] assert 'hello.txt' not in record.files # Make sure that 'second.txt' is still there. assert 'second.txt' in record.files with pytest.raises(KeyError): del record.files['hello.txt'] # Now you can rename 'second.txt' to 'hello.txt'. record.files.rename('second.txt', 'hello.txt') assert 'second.txt' not in record.files assert 'hello.txt' in record.files
def test_file_download_ui(base_app, objects, db): """Test get buckets.""" app = base_app app.config.update( dict( RECORDS_UI_DEFAULT_PERMISSION_FACTORY=None, # No permission checking RECORDS_UI_ENDPOINTS=dict( recid=dict( pid_type='recid', route='/records/<pid_value>', ), recid_files=dict( pid_type='recid', route='/records/<pid_value>/files/<filename>', view_imp='invenio_files_rest.views.file_download_ui', record_class='invenio_records_files.api:Record', ), ))) InvenioRecords(app) InvenioPIDStore(app) InvenioRecordsUI(app) obj1 = objects[0] with app.app_context(): # Record 1 - Live record rec_uuid = uuid.uuid4() PersistentIdentifier.create('recid', '1', object_type='rec', object_uuid=rec_uuid, status=PIDStatus.REGISTERED) record = Record.create( { 'title': 'Registered', 'recid': 1, '_files': [ { 'key': obj1.key, 'bucket': str(obj1.bucket_id), 'checksum': 'invalid' }, ] }, id_=rec_uuid) RecordsBuckets.create(record=record.model, bucket=obj1.bucket) db.session.commit() main_url = url_for('invenio_records_ui.recid', pid_value='1') file_url = url_for('invenio_records_ui.recid_files', pid_value='1', filename=obj1.key) no_file_url = url_for('invenio_records_ui.recid_files', pid_value='1', filename='') invalid_file_url = url_for('invenio_records_ui.recid_files', pid_value='1', filename='no') with app.test_client() as client: res = client.get(main_url) assert res.status_code == 200 res = client.get(file_url) assert res.status_code == 200 res = client.get(no_file_url) assert res.status_code == 404 res = client.get(invalid_file_url) assert res.status_code == 404
def minted_record_with_bucket(minted_record, bucket, db): """Create a bucket.""" pid, record = minted_record RecordsBuckets.create(bucket=bucket, record=record.model) db.session.commit() return pid, record
record = Record.create( { '$schema': 'https://zenodo.org/schemas/deposits/records/record-v1.0.0.json', '_deposit': { 'status': 'draft' }, 'title': 'demo' }, recid) record.commit() db.session.commit() # put a file in the record stream = BytesIO(b'head crab\n') b = Bucket.create() RecordsBuckets.create(bucket=b, record=record.model) db.session.commit() record.files['crab.txt'] = stream record.files.dumps() record.commit() db.session.commit() # create the archive sip = RecordSIP.create(pid, record, True, user_id=1, agent={'demo': 'archivematica'}) db.session.commit() # archive it