def test_find(self): models.StoredFileNode.remove_one( self.node.get_addon('osfstorage').root_node) models.StoredFileNode( path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).save() models.StoredFileNode( path='afolder', name='name', is_file=False, node=self.node, provider='test', materialized_path='/long/path/to/name2/', ).save() expected = ['afile', 'afolder'] select = lambda y: [x.path for x in y.find()] assert_equal(expected, select(models.FileNode)) assert_equal(expected, select(TestFileNode)) assert_equal(['afile'], select(TestFile)) assert_equal(['afile'], select(models.File)) assert_equal(['afolder'], select(TestFolder)) assert_equal(['afolder'], select(models.Folder))
def test_restore_folder(self): root = models.StoredFileNode( path='root', name='rootfolder', is_file=False, node=self.node, provider='test', materialized_path='/long/path/to/', ).wrapped() root.save() fn = models.StoredFileNode( parent=root._id, path='afolder', name='folder_name', is_file=False, node=self.node, provider='test', materialized_path='/long/path/to/folder_name', ).wrapped() before = fn.to_storage() trashed = fn.delete(user=self.user) assert_equal(trashed.restore().to_storage(), before) assert_equal(models.TrashedFileNode.load(trashed._id), None)
def test_parent(self): parent = models.StoredFileNode( path='afolder', name='name', is_file=False, node=self.node, provider='test', materialized_path='/long/path/to/name2/', ).wrapped() parent.save() child = models.StoredFileNode( path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped() child.save() assert_is(child.parent, None) assert_false(isinstance(parent, models.StoredFileNode)) child.parent = parent assert_true(isinstance(child.parent, models.FileNode)) child.parent = parent.stored_object assert_true(isinstance(child.parent, models.FileNode))
def test_restore_file(self): root = models.StoredFileNode( path='root', name='rootfolder', is_file=False, node=self.node, provider='test', materialized_path='/long/path/to', ).wrapped() root.save() fn = models.StoredFileNode( parent=root._id, path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped() guid = Guid.generate(fn) before = fn.to_storage() trashed = fn.delete(user=self.user) restored = trashed.restore() assert_equal(restored.to_storage(), before) assert_equal(models.TrashedFileNode.load(trashed._id), None) # Guid is repointed guid.reload() assert_equal(guid.referent, restored)
def test_delete(self): child = models.StoredFileNode( path='afile', name='child', is_file=True, node=self.node, parent=self.parent._id, provider='test', materialized_path='/long/path/to/name', ).wrapped() child.save() guid = self.parent.get_guid(create=True) child_guid = child.get_guid(create=True) trashed_parent = self.parent.delete(user=self.user) guid.reload() child_guid.reload() assert_equal(trashed_parent, models.TrashedFileNode.load(child._id).parent) assert_equal(trashed_parent, guid.referent) assert_equal(child_guid.referent, models.TrashedFileNode.load(child._id))
def test_touch(self, mock_requests): file = models.StoredFileNode( path='/afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped() mock_requests.return_value = mock.Mock(status_code=400) assert_is(file.touch(None), None) mock_response = mock.Mock(status_code=200) mock_response.json.return_value = { 'data': { 'name': 'fairly', 'modified': '2015', 'size': 0xDEADBEEF, 'materialized': 'ephemeral', } } mock_requests.return_value = mock_response v = file.touch(None) assert_equals(v.size, 0xDEADBEEF) assert_equals(len(file.versions), 0)
def test_repr(self): child = models.StoredFileNode( path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped() assert_true(isinstance(child.__repr__(), basestring))
def setUp(self): super(TestFolderObj, self).setUp() self.parent = models.StoredFileNode( path='aparent', name='parent', is_file=False, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped() self.parent.save()
def setUp(self): super(TestStoredFileNode, self).setUp() self.sfn = models.StoredFileNode( path='anid', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ) self.sfn.save()
def test_find_one(self): models.StoredFileNode( path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).save() found = TestFile.find_one(Q('path', 'eq', 'afile')) assert_true(isinstance(found, TestFile)) assert_equal(found.materialized_path, '/long/path/to/name')
def test_delete(self): models.StoredFileNode( path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped().delete() trashed = models.TrashedFileNode.find_one() assert_equal(trashed.path, 'afile') assert_equal(trashed.node, self.node) assert_equal(trashed.materialized_path, '/long/path/to/name')
def test_delete_with_user(self): fn = models.StoredFileNode( path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped() fn.delete(user=self.user) trashed = models.TrashedFileNode.find_one() assert_equal(trashed.deleted_by, self.user) assert_equal(models.StoredFileNode.load(fn._id), None)
def test_restore(self): fn = models.StoredFileNode( path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped() before = fn.to_storage() trashed = fn.delete(user=self.user) assert_equal(trashed.restore().to_storage(), before) assert_equal(models.TrashedFileNode.load(trashed._id), None)
def test_children(self): models.StoredFileNode( path='afile', name='child', is_file=True, node=self.node, parent=self.parent._id, provider='test', materialized_path='/long/path/to/name', ).save() assert_equal(len(list(self.parent.children)), 1) models.StoredFileNode( path='afile2', name='child2', is_file=True, node=self.node, parent=self.parent._id, provider='test', materialized_path='/long/path/to/name', ).save() assert_equal(len(list(self.parent.children)), 2)
def test_save(self): child = models.StoredFileNode( path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped() assert_false(child._is_loaded) assert_false(child.stored_object._is_loaded) child.save() assert_true(child._is_loaded) assert_true(child.stored_object._is_loaded)
def test_load(self): item = models.StoredFileNode( path='afolder', name='name', is_file=False, node=self.node, provider='test', materialized_path='/long/path/to/name2/', ) item.save() assert_is(models.FileNode.load('notanid'), None) assert_true(isinstance(TestFolder.load(item._id), TestFolder)) assert_true(isinstance(models.FileNode.load(item._id), TestFolder)) with assert_raises(AssertionError): TestFile.load(item._id)
def test_attr_passthrough(self): stored = models.StoredFileNode( path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ) stored.test = 'Foo' stored.bar = ['wat'] wrapped = stored.wrapped() wrapped.bar.append('wat') assert_equal(stored.bar, wrapped.bar) assert_equal(stored.test, wrapped.test)
def test_touch_auth(self, mock_requests): file = models.StoredFileNode( path='/afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped() mock_response = mock.Mock(status_code=404) mock_requests.return_value = mock_response file.touch('Bearer bearer', revision='foo') assert_equal(mock_requests.call_args[1]['headers'], {'Authorization': 'Bearer bearer'})
def migrate_filenodes(): for node_settings in paginated(osfstorage_model.OsfStorageNodeSettings): if node_settings.owner is None: logger.warning( 'OsfStorageNodeSettings {} has no parent; skipping'.format( node_settings._id)) continue logger.info('Migrating files for {!r}'.format(node_settings.owner)) listing = [] for filenode in osfstorage_model.OsfStorageFileNode.find( Q('node_settings', 'eq', node_settings._id)): logger.debug('Migrating OsfStorageFileNode {}'.format( filenode._id)) versions = translate_versions(filenode.versions) if filenode.is_file and not filenode.node.is_deleted: if not filenode.versions: logger.warning( 'File {!r} has no versions'.format(filenode)) elif not versions: logger.warning( '{!r} is a file with no translatable versions'.format( filenode)) new_node = models.StoredFileNode( _id=filenode._id, versions=versions, node=node_settings.owner, parent=None if not filenode.parent else filenode.parent._id, is_file=filenode.kind == 'file', provider='osfstorage', name=filenode.name, last_touched=NOW) # Wrapped's save will populate path and materialized_path new_node.wrapped().save() listing.append(new_node) assert node_settings.get_root() for x in listing: # Make sure everything transfered properly if x.to_storage()['parent']: assert x.parent, '{!r}\'s parent {} does not exist'.format( x.wrapped(), x.to_storage()['parent'])
def test_update_version_metadata(self): v1 = models.FileVersion(identifier='1') v1.save() file = models.StoredFileNode( path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped() file.versions.append(v1) file.update_version_metadata(None, {'size': 1337}) with assert_raises(exceptions.VersionNotFoundError): file.update_version_metadata('3', {}) assert_equal(v1.size, 1337)
def test_delete_with_guid(self): fn = models.StoredFileNode( path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped() guid = fn.get_guid(create=True) fn.delete() trashed = models.TrashedFileNode.find_one() guid.reload() assert_equal(guid.referent, trashed) assert_equal(trashed.path, 'afile') assert_equal(trashed.node, self.node) assert_equal(trashed.materialized_path, '/long/path/to/name') assert_less((trashed.deleted_on - datetime.datetime.utcnow()).total_seconds(), 5)
def build_tree(acc=None, parent=None, atleastone=False): import random acc = acc or [] if len(acc) > 50: return acc is_folder = atleastone for i in range(random.randrange(3, 15)): fn = models.StoredFileNode( path='name{}'.format(i), name='name{}'.format(i), is_file=not is_folder, node=self.node, parent=parent._id, provider='test', materialized_path='{}/{}'.format(parent.materialized_path, 'name{}'.format(i)), ).wrapped() fn.save() random.randint(0, 5) == 1 if is_folder: build_tree(acc, fn) acc.append(fn) is_folder = random.randint(0, 5) == 1 return acc
def test_get_version(self): v1 = models.FileVersion(identifier='1') v2 = models.FileVersion(identifier='2') v1.save() v2.save() file = models.StoredFileNode( path='afile', name='name', is_file=True, node=self.node, provider='test', materialized_path='/long/path/to/name', ).wrapped() file.versions.extend([v1, v2]) assert_equals(file.get_version('1'), v1) assert_equals(file.get_version('2', required=True), v2) assert_is(file.get_version('3'), None) with assert_raises(exceptions.VersionNotFoundError): file.get_version('3', required=True)
def test_restore_folder_nested(self): def build_tree(acc=None, parent=None, atleastone=False): import random acc = acc or [] if len(acc) > 50: return acc is_folder = atleastone for i in range(random.randrange(3, 15)): fn = models.StoredFileNode( path='name{}'.format(i), name='name{}'.format(i), is_file=not is_folder, node=self.node, parent=parent._id, provider='test', materialized_path='{}/{}'.format(parent.materialized_path, 'name{}'.format(i)), ).wrapped() fn.save() random.randint(0, 5) == 1 if is_folder: build_tree(acc, fn) acc.append(fn) is_folder = random.randint(0, 5) == 1 return acc root = models.StoredFileNode( path='root', name='rootfolder', is_file=False, node=self.node, provider='test', materialized_path='/long/path/to/', ).wrapped() root.save() parent = models.StoredFileNode( parent=root._id, path='afolder', name='folder_name', is_file=False, node=self.node, provider='test', materialized_path='/long/path/to/folder_name', ).wrapped() parent.save() branch = models.StoredFileNode( path='afolder', name='folder_name', is_file=False, node=self.node, provider='test', parent=parent._id, materialized_path='/long/path/to/folder_name', ).wrapped() branch.save() round1 = build_tree(parent=branch, atleastone=True) round2 = build_tree(parent=parent, atleastone=True) stay_deleted = [branch.to_storage() ] + [child.to_storage() for child in round1] get_restored = [parent.to_storage() ] + [child.to_storage() for child in round2] branch.delete() for data in stay_deleted: assert_true(models.TrashedFileNode.load(data['_id'])) assert_is(models.StoredFileNode.load(data['_id']), None) trashed = parent.delete() for data in get_restored: assert_true(models.TrashedFileNode.load(data['_id'])) assert_is(models.StoredFileNode.load(data['_id']), None) trashed.restore() for data in stay_deleted: assert_true(models.TrashedFileNode.load(data['_id'])) assert_is(models.StoredFileNode.load(data['_id']), None) for data in get_restored: assert_is(models.TrashedFileNode.load(data['_id']), None) assert_equals( models.StoredFileNode.load(data['_id']).to_storage(), data)