def test_listdir(self): with mock.patch('main.models.User.get_clients', MockClients(self.usera).get_clients): fsa = get_fs(self.usera) with mock.patch('main.models.User.get_clients', MockClients(self.userb).get_clients): fsb = get_fs(self.userb) # User A creates two directories. dira0 = fsa.mkdir('/foo') dira1 = fsa.mkdir('/bar') # User B creates one. dirb0 = fsb.mkdir('/baz') # User A then shares them with User B. dira0.share(self.userb) dira1.share(self.userb, parent=dirb0) # Ensure shares were successful. self.assertTrue(fsb.isdir(dira0.path)) self.assertTrue(fsa.exists(dira1.path)) self.assertTrue(fsb.exists('/baz/bar')) listing = fsb.listdir('/') self.assertEqual(2, len(listing.dirs)) # Then User B moves one. fsb.move(dira0.path, '/foo-bar') self.assertTrue(fsa.exists(dira0.path)) self.assertTrue(fsb.exists('/foo-bar'))
def delete(self, request, uid, format=None): fs = get_fs(request.user) try: file = UserFile.objects.get(uid=uid, user=request.user) except UserFile.DoesNotExist: raise exceptions.NotFound(uid) return response.Response(fs.delete(file.path, file=file))
def test_move_fail(self): with mock.patch('main.models.User.get_clients', MockClients(self.user).get_clients): fs = get_fs(self.user) with self.assertRaises(PathNotFoundError): fs.move('/foo', '/bar') with BytesIO(TEST_FILE) as f: fs.upload('/foo', f) with self.assertRaises(FileConflictError): fs.mkdir('/foo') fs.mkdir('/bar/foo') with self.assertRaises(DirectoryConflictError): fs.move('/foo', '/bar') with self.assertRaises(DirectoryConflictError): fs.move('/bar', '/foo') with self.assertRaises(DirectoryNotFoundError): fs.move('/bar/foo', '/missing/bar') with self.assertRaises(DirectoryNotFoundError): fs.move('/foo', '/missing/bar')
def test_copy_file(self): with mock.patch('main.models.User.get_clients', MockClients(self.user).get_clients): fs = get_fs(self.user) with BytesIO(TEST_FILE) as f: fs.upload('/foo', f) # Dst directories are created automatically. fs.copy('/foo', '/miss') fs.copy('/foo', '/bar') self.assertTrue(fs.isfile('/bar')) self.assertTrue(fs.isfile('/foo')) with BytesIO() as o: with fs.download('/bar') as f: shutil.copyfileobj(f, o) self.assertEqual(TEST_FILE, o.getvalue()) with self.assertRaises(FileConflictError): fs.copy('/foo', '/bar') with BytesIO(TEST_FILE) as f: fs.upload('/bar/baz', f) with BytesIO(TEST_FILE) as f: fs.upload('/baz', f) with self.assertRaises(FileConflictError): fs.copy('/baz', '/bar') with self.assertRaises(PathNotFoundError): fs.copy('/missing', 'bar')
def get(self, request, path, version, format=None): fs = get_fs(request.user) # Find requested file. try: file = fs.info(path) except PathNotFoundError: raise exceptions.NotFound(path) # Find requested version. try: version = file.file.versions.get(uid=version) except Version.DoesNotExist: raise exceptions.NotFound(version) # Prepare response. try: response = StreamingHttpResponse(fs.download(path, file=file, version=version), content_type=version.mime) except PathNotFoundError: raise exceptions.NotFound(path) # Adjust headers. content_disposition = 'filename="%s"' % file.name if 'download' in request.GET: content_disposition = 'attachment; %s' % content_disposition response['Content-Disposition'] = content_disposition # Send the file. return response
def test_mkdir(self): fs = get_fs(self.user) dir = fs.mkdir('/foo') self.assertEqual('/foo', dir.path) fs.rmdir('/foo') with self.assertRaises(DirectoryNotFoundError): fs.rmdir('/foo')
def delete(self, request, path, format=None): fs = get_fs(request.user) if path == '/': raise exceptions.ValidationError('Cannot delete root') try: return response.Response(fs.rmdir(path)) except DirectoryNotFoundError: raise exceptions.NotFound(path)
def delete(self, request, path, format=None): fs = get_fs(request.user) try: info = fs.info(path) if info.isdir: raise exceptions.NotFound(path) return response.Response(fs.delete(path, file=info)) except PathNotFoundError: raise exceptions.NotFound(path)
def get(self, request, path, format=None): fs = get_fs(request.user) try: info = fs.info(path) if info.isdir: raise exceptions.NotFound(path) return response.Response(UserFileSerializer(info).data) except PathNotFoundError: raise exceptions.NotFound(path)
def handle_user(self, user): fs = get_fs(user) for f in File.objects.filter(owner=user): # Every one of their files (excluding dead files). try: self.handle_file(fs, f) except Exception as e: LOGGER.warning('%s:%s Error handling file', user.uid, f.uid) LOGGER.exception(e)
def get(self, request, path, format=None): fs = get_fs(request.user) try: file = fs.info(path) except PathNotFoundError: raise exceptions.NotFound(path) return super().get(request, path, file.file.version.uid, format)
def test_move(self): fs = get_fs(self.user) fs.mkdir('/foo') fs.mkdir('/bar') fs.move('/foo', '/bar') self.assertFalse(fs.exists('/foo')) self.assertTrue(fs.exists('/bar/foo')) self.assertTrue(fs.isdir('/bar/foo'))
def delete(self, request, uid, format=None): fs = get_fs(request.user) try: dir = UserDir.objects.get(uid=uid, user=request.user) except UserDir.DoesNotExist: raise exceptions.NotFound(uid) try: return response.Response(fs.rmdir(dir.path, dir=dir)) except DirectoryNotFoundError: raise exceptions.NotFound(uid)
def test_copy(self): fs = get_fs(self.user) fs.mkdir('/foo') fs.mkdir('/bar') fs.copy('/foo', '/bar') self.assertTrue(fs.exists('/foo')) self.assertTrue(fs.exists('/bar/foo')) self.assertTrue(fs.isdir('/foo')) self.assertTrue(fs.isdir('/bar/foo'))
def post(self, request, uid, format=None): fs = get_fs(request.user) try: file = UserFile.objects.get(uid=uid, user=request.user) except UserFile.DoesNotExist: raise exceptions.NotFound(uid) file = fs.upload(file.path, f=request.FILES['file']) return response.Response(UserFileSerializer(file).data)
def get(self, request, path, format=None): try: dir, dirs, files = get_fs(request.user).listdir(path) except DirectoryNotFoundError: raise exceptions.NotFound(path) return response.Response( UserDirListingSerializer({ 'info': dir, 'dirs': dirs, 'files': files }).data)
def test_info(self): with mock.patch('main.models.User.get_clients', MockClients(self.user).get_clients): fs = get_fs(self.user) with self.assertRaises(PathNotFoundError): fs.info('/foo') with BytesIO(TEST_FILE) as f: fs.upload('/foo', f) fs.info('/foo')
def test_copy_fail(self): with mock.patch('main.models.User.get_clients', MockClients(self.user).get_clients): fs = get_fs(self.user) with BytesIO(TEST_FILE) as f: fs.upload('/foo', f) with self.assertRaises(FileConflictError): fs.mkdir('/foo') fs.mkdir('/bar/foo')
def test_listdir(self): fs = get_fs(self.user) fs.mkdir('/foo') fs.mkdir('/foo/bar') fs.mkdir('/foo/baz') listing = fs.listdir('/foo') self.assertEqual(2, len(listing.dirs)) self.assertEqual(0, len(listing.files)) with self.assertRaises(DirectoryNotFoundError): fs.listdir('/missing')
def test_file_version(self): with mock.patch('main.models.User.get_clients', MockClients(self.user).get_clients): fs = get_fs(self.user) with BytesIO(TEST_FILE) as f: fi = fs.upload('/foo', f) with BytesIO(TEST_FILE) as f: fi = fs.upload('/foo', f) # Two versions of the file should be produced. self.assertEqual(2, fi.file.versions.count())
def get(self, request, uid, format=None): fs = get_fs(request.user) try: dir = UserDir.objects.get(uid=uid, user=request.user) except UserDir.DoesNotExist: raise exceptions.NotFound(uid) dir, dirs, files = fs.listdir(dir.path, dir=dir) return response.Response( UserDirListingSerializer({ 'info': dir, 'dirs': dirs, 'files': files }).data)
def test_move_file(self): with mock.patch('main.models.User.get_clients', MockClients(self.user).get_clients): fs = get_fs(self.user) with BytesIO(TEST_FILE) as f: fs.upload('/foo', f) # Dst directories are created automatically. fs.move('/foo', '/bar') self.assertTrue(fs.exists('/bar')) self.assertTrue(fs.isfile('/bar')) self.assertFalse(fs.exists('/foo'))
def test_fs(self): with mock.patch('main.models.User.get_clients', MockClients(self.user).get_clients): fs = get_fs(self.user) with BytesIO(TEST_FILE) as f: file = fs.upload('/foo', f) self.assertEqual('/foo', file.path) with fs.download('/foo') as f: self.assertEqual(TEST_FILE, f.read()) with self.assertRaises(FileNotFoundError): fs.download('/barfoo') fs.delete('/foo') with self.assertRaises(FileNotFoundError): fs.delete('/foo')
def test_fs_replicas(self): mock_clients = MockClients(self.user) with mock.patch('main.models.User.get_clients', mock_clients.get_clients): fs = get_fs(self.user, chunk_size=3, replicas=2) with BytesIO(TEST_FILE) as f: file = fs.upload('/foo', f) mock_clients.clients[2].data.clear() self.assertEqual('/foo', file.path) with BytesIO() as o: with fs.download('/foo') as f: shutil.copyfileobj(f, o) self.assertEqual(TEST_FILE, o.getvalue()) with self.assertRaises(FileNotFoundError): fs.download('/barfoo') fs.delete('/foo')
def post(self, request, path, format=None): fs = get_fs(request.user) file = fs.upload(path, f=request.FILES['file']) return response.Response(UserFileSerializer(file).data)
def test_is_dir_file(self): with mock.patch('main.models.User.get_clients', MockClients(self.user).get_clients): fs = get_fs(self.user) self.assertTrue(fs.isdir('/')) self.assertFalse(fs.isfile('/'))
def post(self, request, path, format=None): return response.Response( UserDirSerializer(get_fs(request.user).mkdir(path)).data)