diff --git a/seahub/api2/views.py b/seahub/api2/views.py index 4b8c492583..3432bb1b3d 100644 --- a/seahub/api2/views.py +++ b/seahub/api2/views.py @@ -2007,6 +2007,7 @@ class FileView(APIView): parent_dir = os.path.dirname(path) operation = request.POST.get('operation', '') + file_info = {} if operation.lower() == 'rename': if check_folder_permission(request, repo_id, parent_dir) != 'rw': return api_error(status.HTTP_403_FORBIDDEN, @@ -2090,7 +2091,10 @@ class FileView(APIView): if request.GET.get('reloaddir', '').lower() == 'true': return reloaddir(request, dst_repo, dst_dir) else: - resp = Response('success', status=status.HTTP_301_MOVED_PERMANENTLY) + file_info['repo_id'] = dst_repo_id + file_info['parent_dir'] = dst_dir + file_info['obj_name'] = new_filename_utf8 + resp = Response(file_info, status=status.HTTP_301_MOVED_PERMANENTLY) uri = reverse('FileView', args=[dst_repo_id], request=request) resp['Location'] = uri + '?p=' + quote(dst_dir_utf8) + quote(new_filename_utf8) return resp @@ -2140,7 +2144,10 @@ class FileView(APIView): if request.GET.get('reloaddir', '').lower() == 'true': return reloaddir(request, dst_repo, dst_dir) else: - resp = Response('success', status=status.HTTP_200_OK) + file_info['repo_id'] = dst_repo_id + file_info['parent_dir'] = dst_dir + file_info['obj_name'] = new_filename_utf8 + resp = Response(file_info, status=status.HTTP_200_OK) uri = reverse('FileView', args=[dst_repo_id], request=request) resp['Location'] = uri + '?p=' + quote(dst_dir_utf8) + quote(new_filename_utf8) return resp diff --git a/tests/api/test_files.py b/tests/api/test_files.py index 7002d2575d..f82c24b42c 100644 --- a/tests/api/test_files.py +++ b/tests/api/test_files.py @@ -8,6 +8,7 @@ import pytest import urllib from urllib import urlencode, quote import urlparse +from nose.tools import assert_in from tests.common.utils import randstring, urljoin from tests.api.apitestbase import ApiTestBase @@ -32,15 +33,70 @@ class FilesApiTest(ApiTestBase): def test_move_file(self): with self.get_tmp_repo() as repo: - _, furl = self.create_file(repo) # TODO: create another repo here, and use it as dst_repo + + # create sub folder(dpath) + dpath, _ = self.create_dir(repo) + + # create tmp file in sub folder(dpath) + tmp_file = 'tmp_file.txt' + file_path = dpath + '/' + tmp_file + furl = repo.get_filepath_url(file_path) + data = {'operation': 'create'} + res = self.post(furl, data=data, expected=201) + + # copy tmp file from sub folder(dpath) to dst dir('/') data = { - 'operation': 'move', 'dst_repo': repo.repo_id, 'dst_dir': '/', + 'operation': 'copy', } - res = self.post(furl, data=data) - self.assertEqual(res.text, '"success"') + u = urlparse.urlparse(furl) + parsed_furl = urlparse.urlunparse((u.scheme, u.netloc, u.path, '', '', '')) + res = self.post(parsed_furl+ '?p=' + quote(file_path), data=data) + assert_in(tmp_file, res.text) + + # get info of copied file in dst dir('/') + fdurl = repo.file_url + u'detail/?p=/%s' % quote(tmp_file) + detail = self.get(fdurl).json() + self.assertIsNotNone(detail) + self.assertIsNotNone(detail['id']) + + # copy tmp file from sub folder(dpath) to dst dir('/') again + # for test can rename file if a file with the same name is dst dir + data = { + 'dst_repo': repo.repo_id, + 'dst_dir': '/', + 'operation': 'copy', + } + u = urlparse.urlparse(furl) + parsed_furl = urlparse.urlunparse((u.scheme, u.netloc, u.path, '', '', '')) + res = self.post(parsed_furl+ '?p=' + quote(file_path), data=data) + assert_in('tmp_file (1).txt', res.text) + + # copy tmp file from sub folder(dpath) to dst dir('/') again + # for test can rename file if a file with the same name is dst dir + data = { + 'dst_repo': repo.repo_id, + 'dst_dir': '/', + 'operation': 'copy', + } + u = urlparse.urlparse(furl) + parsed_furl = urlparse.urlunparse((u.scheme, u.netloc, u.path, '', '', '')) + res = self.post(parsed_furl+ '?p=' + quote(file_path), data=data) + assert_in('tmp_file (2).txt', res.text) + + # then move file to dst dir + data = { + 'dst_repo': repo.repo_id, + 'dst_dir': '/', + 'operation': 'move', + } + u = urlparse.urlparse(furl) + parsed_furl = urlparse.urlunparse((u.scheme, u.netloc, u.path, '', '', '')) + res = self.post(parsed_furl+ '?p=' + quote(file_path), data=data) + assert_in('tmp_file%20%283%29.txt', res.text) + def test_copy_file(self): with self.get_tmp_repo() as repo: @@ -65,7 +121,7 @@ class FilesApiTest(ApiTestBase): u = urlparse.urlparse(furl) parsed_furl = urlparse.urlunparse((u.scheme, u.netloc, u.path, '', '', '')) res = self.post(parsed_furl+ '?p=' + quote(file_path), data=data) - self.assertEqual(res.text, '"success"') + assert_in(tmp_file, res.text) # get info of copied file in dst dir('/') fdurl = repo.file_url + u'detail/?p=/%s' % quote(tmp_file) @@ -73,6 +129,30 @@ class FilesApiTest(ApiTestBase): self.assertIsNotNone(detail) self.assertIsNotNone(detail['id']) + # copy tmp file from sub folder(dpath) to dst dir('/') again + # for test can rename file if a file with the same name is dst dir + data = { + 'dst_repo': repo.repo_id, + 'dst_dir': '/', + 'operation': 'copy', + } + u = urlparse.urlparse(furl) + parsed_furl = urlparse.urlunparse((u.scheme, u.netloc, u.path, '', '', '')) + res = self.post(parsed_furl+ '?p=' + quote(file_path), data=data) + assert_in('tmp_file (1).txt', res.text) + + # copy tmp file from sub folder(dpath) to dst dir('/') again + # for test can rename file if a file with the same name is dst dir + data = { + 'dst_repo': repo.repo_id, + 'dst_dir': '/', + 'operation': 'copy', + } + u = urlparse.urlparse(furl) + parsed_furl = urlparse.urlunparse((u.scheme, u.netloc, u.path, '', '', '')) + res = self.post(parsed_furl+ '?p=' + quote(file_path), data=data) + assert_in('tmp_file (2).txt', res.text) + def test_download_file(self): with self.get_tmp_repo() as repo: fname, furl = self.create_file(repo)