1
0
mirror of https://github.com/haiwen/seafile-server.git synced 2025-04-28 19:35:10 +00:00
seafile-server/tests/test_file_operation/test_zip_download.py

168 lines
6.3 KiB
Python
Raw Permalink Normal View History

2019-08-28 04:13:44 +00:00
import pytest
import requests
import os
import time
import zipfile
import json
from tests.config import USER
from seaserv import seafile_api as api
file1_name = 'file1.txt'
file2_name = 'file2.txt'
file1_path = os.getcwd() + '/' + file1_name
file2_path = os.getcwd() + '/' + file2_name
file1_content ='File1 content'
file2_content ='File2 content'
download_dir_path = os.getcwd() + '/download_dir'
def create_test_files():
os.mkdir(download_dir_path)
with open(file1_path, 'w') as fp1:
fp1.write(file1_content)
with open(file2_path, 'w') as fp2:
fp2.write(file2_content)
def remove_test_files():
os.rmdir(download_dir_path)
os.remove(file1_path)
os.remove(file2_path)
def test_zip_download():
create_test_files()
t_repo_id = api.create_repo('test_zip_download', '', USER)
base_url = 'http://127.0.0.1:8082/'
#test zip download dir
dir_name = 'dir'
api.post_dir(t_repo_id, '/', dir_name, USER)
api.post_file(t_repo_id, file1_path, '/dir', file1_name, USER)
api.post_file(t_repo_id, file2_path, '/dir', file2_name, USER)
dir_id = api.get_dir_id_by_path(t_repo_id, '/dir')
obj_id = {'obj_id': dir_id, 'dir_name': dir_name, 'is_windows': 0}
obj_id_json_str = json.dumps(obj_id)
token = api.get_fileserver_access_token(t_repo_id, obj_id_json_str,
'download-dir', USER)
Go fileserver (#437) * Initial commit for fileserver written in golang. [gofileserver] Fix some syntaxt errors. Add fs backend and objstore test (#352) * Add fs backend and objstore test * modify test case and optimize fs backend * Modify function name and first write temporary files * Don't need to reopen the temp files Add comment for objstore (#354) * Add comment for objstore * Modify comment Add commitmgr and test case (#356) * Add commitmgr and test case * Redefine the interface * Modify comment and interface * Modify parameter and del unused method * Add comment for FromData and ToData Add blockmgr and test case (#357) * Add blockmgr and test case * Modify comment and interface Add fsmgr and test case (#358) * Add fsmgr and test case * Add save interface and error details * Modify errors and comments Add searpc package and test case (#360) * Add searpc package * Add searpc test case * Add return error and add Request struct * Modify returned error * Modify comments add checkPerm (#369) Add file and block download (#363) * Add file and block download * Modify init and use aes algorithm * Get block by offset and add stat method * Modify objID's type * Fix reset pos after add start * Add http error handing and record log when failed to read block or write block to response * Modify http return code and value names * Modify http return code and add log info * Block read add comment and only repeat once load ccnetdb and support sqlite (#371) Add zip download (#372) * Add zip download * Modify pack dir and log info * Modify http return code and use Deflate zip compression methods add /repo/<repo-id>/permission-check (#375) add /<repo-id>/commit/HEAD (#377) add /repo/<repo-id>/commit/<id> (#379) add /repo/<repo-id>/block/<id> (#380) add /repo/<repo-id>/fs-id-list (#383) add /repo/head-commits-multi (#388) Add file upload api (#378) * Add file upload api * Upload api implements post multi files and create relative path * Modify handle error and save files directly * Fix rebase conflict * index block use channel and optimize mkdir with parents * Handle jobs and results in a loop * Mkdir with parents use postMultiFiles and use pointer of SeafDirent * Del diff_simple size_sched virtual_repo * Need to check the path with and without slash * Modify merge trees and add merge test case * Del postFile and don't close results channel * Close the file and remove multipart temp file * Modify merge test case and compare the first name of path * Use pointer of Entries for SeafDir * Add test cases for different situations add /repo/<repo-id>/pack-fs (#389) add POST /<repo-id>/check-fs and /<repo-id>/check-blocks (#396) Merge compute repo (#397) * Add update repo size and merge virtual repo * Eliminate lint warnings * Uncomment merge virtual repo and compute repo size * Need init the dents * Use interface{} param and modify removeElems * Move update dir to file.go and modify logs * Del sync pkg add PUT /<repo-id>/commit/<commit-id> (#400) add PUT /<repo-id>/block/<id> (#401) add POST /<repo-id>/recv-fs (#398) add PUT /<repo-id>/commit/HEAD (#402) Add http return code (#403) Add file update API (#399) * Add file update API * Add GetObjIDByPath and fix change size error * Add traffic statistics for update api add diffTrees unit test (#391) add GET /accessible-repos (#406) add GET /<repo-id>/block-map/<file-id> (#405) Add test update repo size and merge virtual repo (#409) * Update dir need update repo size * Add test update repo size and merge virtual repo * Add delay for test ajax * Add delay before get repo size and modify comment Use go fileserver for unit test (#410) * Use go fileserver for unit test * Blocking scheduling update repo size * Add delay because of sqlite doesn't support concurrency * Post use multipart form encode * Del mysql database when test finished * Fix merge virtual repo failed when use sqlite3 Add upload block API (#412) fixed error Add quota-check API (#426) use diff package * Use central conf for go fileserver (#428) * Use central conf for go fileserver * Fix log error * use store id and remove share get repo owner (#430) * Fix permission error (#432) Co-authored-by: feiniks <36756310+feiniks@users.noreply.github.com> Co-authored-by: Xiangyue Cai <caixiangyue007@gmail.com>
2021-01-04 03:41:53 +00:00
time.sleep(1)
2019-08-28 04:13:44 +00:00
download_url = base_url + 'zip/' + token
response = requests.get(download_url)
assert response.status_code == 200
download_zipfile_path = download_dir_path + '/dir.zip'
with open(download_zipfile_path, 'wb') as fp:
fp.write(response.content)
zipFile = zipfile.ZipFile(download_zipfile_path)
for name in zipFile.namelist():
zipFile.extract(name, download_dir_path)
zipFile.close()
assert os.path.exists(download_dir_path + '/dir.zip')
assert os.path.exists(download_dir_path + '/dir')
assert os.path.exists(download_dir_path + '/dir' + '/file1.txt')
assert os.path.exists(download_dir_path + '/dir' + '/file2.txt')
with open(download_dir_path + '/dir' + '/file1.txt', 'r') as fp1:
line = fp1.read()
assert line == file1_content
with open(download_dir_path + '/dir' + '/file2.txt', 'r') as fp2:
line = fp2.read()
assert line == file2_content
os.remove(download_dir_path + '/dir' + '/file1.txt')
os.remove(download_dir_path + '/dir' + '/file2.txt')
os.rmdir(download_dir_path + '/dir')
os.remove(download_dir_path + '/dir.zip')
#test zip download empty dir
empty_dir_name = 'empty_dir'
api.post_dir(t_repo_id, '/', empty_dir_name, USER)
dir_id = api.get_dir_id_by_path(t_repo_id, '/empty_dir')
obj_id = {'obj_id': dir_id, 'dir_name': empty_dir_name, 'is_windows': 0}
obj_id_json_str = json.dumps(obj_id)
token = api.get_fileserver_access_token(t_repo_id, obj_id_json_str,
'download-dir', USER)
Go fileserver (#437) * Initial commit for fileserver written in golang. [gofileserver] Fix some syntaxt errors. Add fs backend and objstore test (#352) * Add fs backend and objstore test * modify test case and optimize fs backend * Modify function name and first write temporary files * Don't need to reopen the temp files Add comment for objstore (#354) * Add comment for objstore * Modify comment Add commitmgr and test case (#356) * Add commitmgr and test case * Redefine the interface * Modify comment and interface * Modify parameter and del unused method * Add comment for FromData and ToData Add blockmgr and test case (#357) * Add blockmgr and test case * Modify comment and interface Add fsmgr and test case (#358) * Add fsmgr and test case * Add save interface and error details * Modify errors and comments Add searpc package and test case (#360) * Add searpc package * Add searpc test case * Add return error and add Request struct * Modify returned error * Modify comments add checkPerm (#369) Add file and block download (#363) * Add file and block download * Modify init and use aes algorithm * Get block by offset and add stat method * Modify objID's type * Fix reset pos after add start * Add http error handing and record log when failed to read block or write block to response * Modify http return code and value names * Modify http return code and add log info * Block read add comment and only repeat once load ccnetdb and support sqlite (#371) Add zip download (#372) * Add zip download * Modify pack dir and log info * Modify http return code and use Deflate zip compression methods add /repo/<repo-id>/permission-check (#375) add /<repo-id>/commit/HEAD (#377) add /repo/<repo-id>/commit/<id> (#379) add /repo/<repo-id>/block/<id> (#380) add /repo/<repo-id>/fs-id-list (#383) add /repo/head-commits-multi (#388) Add file upload api (#378) * Add file upload api * Upload api implements post multi files and create relative path * Modify handle error and save files directly * Fix rebase conflict * index block use channel and optimize mkdir with parents * Handle jobs and results in a loop * Mkdir with parents use postMultiFiles and use pointer of SeafDirent * Del diff_simple size_sched virtual_repo * Need to check the path with and without slash * Modify merge trees and add merge test case * Del postFile and don't close results channel * Close the file and remove multipart temp file * Modify merge test case and compare the first name of path * Use pointer of Entries for SeafDir * Add test cases for different situations add /repo/<repo-id>/pack-fs (#389) add POST /<repo-id>/check-fs and /<repo-id>/check-blocks (#396) Merge compute repo (#397) * Add update repo size and merge virtual repo * Eliminate lint warnings * Uncomment merge virtual repo and compute repo size * Need init the dents * Use interface{} param and modify removeElems * Move update dir to file.go and modify logs * Del sync pkg add PUT /<repo-id>/commit/<commit-id> (#400) add PUT /<repo-id>/block/<id> (#401) add POST /<repo-id>/recv-fs (#398) add PUT /<repo-id>/commit/HEAD (#402) Add http return code (#403) Add file update API (#399) * Add file update API * Add GetObjIDByPath and fix change size error * Add traffic statistics for update api add diffTrees unit test (#391) add GET /accessible-repos (#406) add GET /<repo-id>/block-map/<file-id> (#405) Add test update repo size and merge virtual repo (#409) * Update dir need update repo size * Add test update repo size and merge virtual repo * Add delay for test ajax * Add delay before get repo size and modify comment Use go fileserver for unit test (#410) * Use go fileserver for unit test * Blocking scheduling update repo size * Add delay because of sqlite doesn't support concurrency * Post use multipart form encode * Del mysql database when test finished * Fix merge virtual repo failed when use sqlite3 Add upload block API (#412) fixed error Add quota-check API (#426) use diff package * Use central conf for go fileserver (#428) * Use central conf for go fileserver * Fix log error * use store id and remove share get repo owner (#430) * Fix permission error (#432) Co-authored-by: feiniks <36756310+feiniks@users.noreply.github.com> Co-authored-by: Xiangyue Cai <caixiangyue007@gmail.com>
2021-01-04 03:41:53 +00:00
time.sleep(1)
2019-08-28 04:13:44 +00:00
download_url = base_url + 'zip/' + token
response = requests.get(download_url)
assert response.status_code == 200
download_zipfile_path = download_dir_path + '/empty_dir.zip'
with open(download_zipfile_path, 'wb') as fp:
fp.write(response.content)
zipFile = zipfile.ZipFile(download_zipfile_path)
for name in zipFile.namelist():
zipFile.extract(name, download_dir_path)
zipFile.close()
assert os.path.exists(download_dir_path + '/empty_dir')
assert not os.listdir(download_dir_path + '/empty_dir')
os.rmdir(download_dir_path + '/empty_dir')
os.remove(download_dir_path + '/empty_dir.zip')
#test zip download mutliple files
api.post_file(t_repo_id, file1_path, '/', file1_name, USER)
api.post_file(t_repo_id, file2_path, '/', file2_name, USER)
obj_id = {'parent_dir': '/', 'file_list': [file1_name, file2_name], 'is_windows' : 0}
obj_id_json_str = json.dumps(obj_id)
token = api.get_fileserver_access_token(t_repo_id, obj_id_json_str,
'download-multi', USER)
time.sleep(1)
download_url = base_url + 'zip/' + token
response = requests.get(download_url)
assert response.status_code == 200
download_zipfile_path = download_dir_path + '/multi_files.zip'
with open(download_zipfile_path, 'wb') as fp:
fp.write(response.content)
zipFile = zipfile.ZipFile(download_zipfile_path)
for name in zipFile.namelist():
zipFile.extract(name, download_dir_path)
zipFile.close()
assert os.path.exists(download_dir_path + '/file1.txt')
assert os.path.exists(download_dir_path + '/file2.txt')
with open(download_dir_path + '/file1.txt', 'r') as fp1:
line = fp1.read()
assert line == file1_content
with open(download_dir_path + '/file2.txt', 'r') as fp2:
line = fp2.read()
assert line == file2_content
os.remove(download_dir_path + '/file1.txt')
os.remove(download_dir_path + '/file2.txt')
os.remove(download_dir_path + '/multi_files.zip')
#test zip download mutliple files in multi-level
api.post_file(t_repo_id, file2_path, '/dir', file2_name, USER)
obj_id = {'parent_dir': '/', 'file_list': [file1_name, 'dir/'+file2_name], 'is_windows' : 0}
obj_id_json_str = json.dumps(obj_id)
token = api.get_fileserver_access_token(t_repo_id, obj_id_json_str,
'download-multi', USER)
2019-08-28 04:13:44 +00:00
Go fileserver (#437) * Initial commit for fileserver written in golang. [gofileserver] Fix some syntaxt errors. Add fs backend and objstore test (#352) * Add fs backend and objstore test * modify test case and optimize fs backend * Modify function name and first write temporary files * Don't need to reopen the temp files Add comment for objstore (#354) * Add comment for objstore * Modify comment Add commitmgr and test case (#356) * Add commitmgr and test case * Redefine the interface * Modify comment and interface * Modify parameter and del unused method * Add comment for FromData and ToData Add blockmgr and test case (#357) * Add blockmgr and test case * Modify comment and interface Add fsmgr and test case (#358) * Add fsmgr and test case * Add save interface and error details * Modify errors and comments Add searpc package and test case (#360) * Add searpc package * Add searpc test case * Add return error and add Request struct * Modify returned error * Modify comments add checkPerm (#369) Add file and block download (#363) * Add file and block download * Modify init and use aes algorithm * Get block by offset and add stat method * Modify objID's type * Fix reset pos after add start * Add http error handing and record log when failed to read block or write block to response * Modify http return code and value names * Modify http return code and add log info * Block read add comment and only repeat once load ccnetdb and support sqlite (#371) Add zip download (#372) * Add zip download * Modify pack dir and log info * Modify http return code and use Deflate zip compression methods add /repo/<repo-id>/permission-check (#375) add /<repo-id>/commit/HEAD (#377) add /repo/<repo-id>/commit/<id> (#379) add /repo/<repo-id>/block/<id> (#380) add /repo/<repo-id>/fs-id-list (#383) add /repo/head-commits-multi (#388) Add file upload api (#378) * Add file upload api * Upload api implements post multi files and create relative path * Modify handle error and save files directly * Fix rebase conflict * index block use channel and optimize mkdir with parents * Handle jobs and results in a loop * Mkdir with parents use postMultiFiles and use pointer of SeafDirent * Del diff_simple size_sched virtual_repo * Need to check the path with and without slash * Modify merge trees and add merge test case * Del postFile and don't close results channel * Close the file and remove multipart temp file * Modify merge test case and compare the first name of path * Use pointer of Entries for SeafDir * Add test cases for different situations add /repo/<repo-id>/pack-fs (#389) add POST /<repo-id>/check-fs and /<repo-id>/check-blocks (#396) Merge compute repo (#397) * Add update repo size and merge virtual repo * Eliminate lint warnings * Uncomment merge virtual repo and compute repo size * Need init the dents * Use interface{} param and modify removeElems * Move update dir to file.go and modify logs * Del sync pkg add PUT /<repo-id>/commit/<commit-id> (#400) add PUT /<repo-id>/block/<id> (#401) add POST /<repo-id>/recv-fs (#398) add PUT /<repo-id>/commit/HEAD (#402) Add http return code (#403) Add file update API (#399) * Add file update API * Add GetObjIDByPath and fix change size error * Add traffic statistics for update api add diffTrees unit test (#391) add GET /accessible-repos (#406) add GET /<repo-id>/block-map/<file-id> (#405) Add test update repo size and merge virtual repo (#409) * Update dir need update repo size * Add test update repo size and merge virtual repo * Add delay for test ajax * Add delay before get repo size and modify comment Use go fileserver for unit test (#410) * Use go fileserver for unit test * Blocking scheduling update repo size * Add delay because of sqlite doesn't support concurrency * Post use multipart form encode * Del mysql database when test finished * Fix merge virtual repo failed when use sqlite3 Add upload block API (#412) fixed error Add quota-check API (#426) use diff package * Use central conf for go fileserver (#428) * Use central conf for go fileserver * Fix log error * use store id and remove share get repo owner (#430) * Fix permission error (#432) Co-authored-by: feiniks <36756310+feiniks@users.noreply.github.com> Co-authored-by: Xiangyue Cai <caixiangyue007@gmail.com>
2021-01-04 03:41:53 +00:00
time.sleep(1)
2019-08-28 04:13:44 +00:00
download_url = base_url + 'zip/' + token
response = requests.get(download_url)
assert response.status_code == 200
download_zipfile_path = download_dir_path + '/multi_files.zip'
with open(download_zipfile_path, 'wb') as fp:
fp.write(response.content)
zipFile = zipfile.ZipFile(download_zipfile_path)
for name in zipFile.namelist():
zipFile.extract(name, download_dir_path)
zipFile.close()
assert os.path.exists(download_dir_path + '/file1.txt')
assert os.path.exists(download_dir_path + '/file2.txt')
with open(download_dir_path + '/file1.txt', 'r') as fp1:
line = fp1.read()
assert line == file1_content
with open(download_dir_path + '/file2.txt', 'r') as fp2:
line = fp2.read()
assert line == file2_content
os.remove(download_dir_path + '/file1.txt')
os.remove(download_dir_path + '/file2.txt')
os.remove(download_dir_path + '/multi_files.zip')
remove_test_files()
api.remove_repo(t_repo_id)