mirror of
https://github.com/haiwen/seahub.git
synced 2025-07-06 03:36:46 +00:00
* delete thridpart/social_django * delete social_django in seahub/urls.py * delete social_django in seahub/settings.py * delete seahub/notifications/management/commands/send_wxwork_notices.py * delete social_django in code annotation * delete seahub/social_core * delete tests/seahub/social_core * delete social_core in seahub/urls.py * delete social_core in seahub/settings.py * change app_label to auth in SocialAuthUser model * 2to3 asserts * 2to3 basestring * 2to3 dict * 2to3 except * 2to3 filter * 2to3 future * 2to3 has_key * 2to3 idioms * 2to3 import * 2to3 imports * 2to3 long * 2to3 map * 2to3 next * 2to3 numliterals * 2to3 print * 2to3 raise * 2to3 raw_input * 2to3 reduce * 2to3 reload * 2to3 set_literal * 2to3 unicode * 2to3 urllib * 2to3 ws_comma * 2to3 xrange * 2to3 zip * add pymysql in __init__.py * fix encode and decode in seahub/cconvert.py * fix seafserv_rpc.is_passwd_set in seahub/views/__init__.py * fix smart_unicode to smart_text * fix force_unicode to force_text * delete seaserv.get_session_info * delete seaserv.ccnet_rpc * fix indent error in seahub/auth/middleware.py * update dev-requirements * update test-requirements * update requirements * fix StringIO to BytesIO in thumbnail * fix seaserv.list_inner_pub_repos to seafile_api.get_inner_pub_repo_list * fix seaserv.list_org_inner_pub_repos to seafile_api.list_org_inner_pub_repos * add logger in seahub/utils/__init__.py * fix sort cmp in seahub/views/__init__.py * fix sort cmp in seahub/base/management/commands/export_file_access_log.py * fix sort cmp in seahub/api2/endpoints/repo_trash.py * fix sort cmp in seahub/api2/endpoints/shared_repos.py * fix sort cmp in seahub/api2/endpoints/shared_folders.py * fix sort cmp in seahub/wiki/views.py * fix sort cmp in seahub/api2/endpoints/wiki_pages.py * fix sort cmp in seahub/api2/endpoints/group_libraries.py * fix sort cmp in seahub/base/models.py * fix sort cmp in seahub/api2/endpoints/upload_links.py * fix sort cmp in seahub/views/ajax.py * fix sort cmp in seahub/api2/views.py * fix sort cmp in seahub/views/wiki.py * fix sort cmp in seahub/api2/endpoints/repos.py * fix sort cmp in seahub/api2/endpoints/starred_items.py * fix sort cmp in seahub/views/file.py * fix sort cmp in seahub/api2/endpoints/dir.py * fix sort cmp in seahub/api2/endpoints/share_links.py * fix cmp to cmp_to_key in seahub/api2/endpoints/admin/device_trusted_ip.py * fix cmp to cmp_to_key in tests/api/endpoints/admin/test_device_trusted_ip.py * delete encode('utf-8') in seafile_api.list_dir_by_commit_and_path * delete encode('utf-8') in is_file_starred * delete encode('utf-8') in seafile_api.list_dir_by_path * delete path.encode('utf-8') in seahub/views/file.py * fix os.write to add encode('utf-8') * add encode('utf-8') for hashlib * add encode('utf-8') for hmac * fix with open(file, 'wb') for binary file * fix encode and decode in seahub/utils/hasher.py * fix next in thirdpart/shibboleth/views.py * fix next in seahub/profile/views.py * fix next in seahub/notifications/views.py * fix next in seahub/institutions/views.py * fix next in seahub/options/views.py * fix next in seahub/share/views.py * fix next in seahub/avatar/views.py * fix next in seahub/views/__init__.py * fix next in seahub/group/views.py * fix next in seahub/views/wiki.py * fix next in seahub/views/sysadmin.py * fix next in seahub/views/file.py * fix string.lowercase to string.ascii_lowercase in test * fix open file add 'rb' in test * fix self.user.username in test * add migrations in file_participants * fix list_org_inner_pub_repos to list_org_inner_pub_repos_by_owner * fix from seaserv import is_passwd_set to seafile_api.is_password_set * fix assert bytes resp.content in test * fix seafile_api.get_inner_pub_repo_list to seafile_api.list_inner_pub_repos_by_owner * fix seafile_api.is_passwd_set to seafile_api.is_password_set * fix AccountsApiTest assert length * rewrite sort_devices cmp to operator.lt * fix bytes + str in seahub/api2/views.py * fix assert bytes resp.content in test * fix hashlib encode in seahub/thirdpart/registration/models.py * change app_label to base in SocialAuthUser * fix base64 encode in seahub/base/database_storage/database_storage.py * fix assert bytes resp.content * remove path.decode in def mkstemp() * remove path.decode in FpathToLinkTest * remove str decode in FileTagTest * remove mock_write_xls.assert_called_once() in SysUserAdminExportExcelTest * fix urllib assert in FilesApiTest * fix link fields in FileCommentsTest * fix get_related_users_by_repo() * fix assert list in GetRepoSharedUsersTest * fix create user in AccountTest * fix repeated key in dict seahub/api2/views.py * add drone.yml * update nginx conf in test * update test conf in test * update dist and push after test success * update drone conf to dist and push * fix assert in BeSharedReposTest * fix seafile_api.list_org_inner_pub_repos_by_owner(org_id, username) to seafile_api.list_org_inner_pub_repos(org_id) * fix seafile_api.list_inner_pub_repos_by_owner(username) to seafile_api.get_inner_pub_repo_list() * update pyjwt requirement * update dist branch in drone * add SKIP in dist and push * fix StringIO to BytesIO in seahub/avatar/models.py * fix if org_id > 0 to if org_id and org_id > 0 * remove payment * fix StringIO to BytesIO in seahub/base/database_storage/database_storage.py * fix send_message to seafile_api.publish_event in seahub/drafts/utils.py * fix send_message to seafile_api.publish_event in seahub/api2/views.py * fix send_message to seafile_api.publish_event in seahub/api2/endpoints/repos.py * fix send_message to seafile_api.publish_event in seahub/views/file.py * fix send_message to seafile_api.publish_event in seahub/utils/__init__.py * fix image_file.read encode in seahub/base/database_storage/database_storage.py * fix DatabaseStorageTest * remove .travis.yml * drone branch include master
245 lines
7.5 KiB
Python
245 lines
7.5 KiB
Python
#coding: UTF-8
|
|
|
|
import requests
|
|
import unittest
|
|
from contextlib import contextmanager
|
|
from nose.tools import assert_equal, assert_in # pylint: disable=E0611
|
|
from urllib.parse import quote
|
|
|
|
from tests.common.common import USERNAME, PASSWORD, \
|
|
ADMIN_USERNAME, ADMIN_PASSWORD
|
|
|
|
from tests.common.utils import apiurl, urljoin, randstring
|
|
from tests.api.urls import TOKEN_URL, GROUPS_URL, ACCOUNTS_URL, REPOS_URL
|
|
|
|
class ApiTestBase(unittest.TestCase):
|
|
_token = None
|
|
_admin_token = None
|
|
|
|
username = USERNAME
|
|
password = PASSWORD
|
|
admin_username = ADMIN_USERNAME
|
|
admin_password = ADMIN_PASSWORD
|
|
|
|
@classmethod
|
|
def get(cls, *args, **kwargs):
|
|
return cls._req('GET', *args, **kwargs)
|
|
|
|
@classmethod
|
|
def post(cls, *args, **kwargs):
|
|
return cls._req('POST', *args, **kwargs)
|
|
|
|
@classmethod
|
|
def put(cls, *args, **kwargs):
|
|
return cls._req('PUT', *args, **kwargs)
|
|
|
|
@classmethod
|
|
def delete(cls, *args, **kwargs):
|
|
return cls._req('DELETE', *args, **kwargs)
|
|
|
|
@classmethod
|
|
def admin_get(cls, *args, **kwargs):
|
|
kwargs['admin'] = True
|
|
return cls.get(*args, **kwargs)
|
|
|
|
@classmethod
|
|
def admin_post(cls, *args, **kwargs):
|
|
kwargs['admin'] = True
|
|
return cls.post(*args, **kwargs)
|
|
|
|
@classmethod
|
|
def admin_put(cls, *args, **kwargs):
|
|
kwargs['admin'] = True
|
|
return cls.put(*args, **kwargs)
|
|
|
|
@classmethod
|
|
def admin_delete(cls, *args, **kwargs):
|
|
kwargs['admin'] = True
|
|
return cls.delete(*args, **kwargs)
|
|
|
|
@classmethod
|
|
def _req(cls, method, *args, **kwargs):
|
|
use_token = kwargs.pop('use_token', True)
|
|
token = kwargs.pop('token', None)
|
|
if use_token and token is None:
|
|
admin = kwargs.pop('admin', False)
|
|
if admin:
|
|
if cls._admin_token is None:
|
|
cls._admin_token = get_auth_token(ADMIN_USERNAME,
|
|
ADMIN_PASSWORD)
|
|
token = cls._admin_token
|
|
else:
|
|
if cls._token is None:
|
|
cls._token = get_auth_token(USERNAME, PASSWORD)
|
|
token = cls._token
|
|
|
|
if use_token:
|
|
headers = kwargs.get('headers', {})
|
|
headers.setdefault('Authorization', 'Token ' + token)
|
|
kwargs['headers'] = headers
|
|
|
|
expected = kwargs.pop('expected', 200)
|
|
resp = requests.request(method, *args, **kwargs)
|
|
if expected is not None:
|
|
if hasattr(expected, '__iter__'):
|
|
assert_in(resp.status_code, expected,
|
|
"Expected http status in %s, received %s" % (expected,
|
|
resp.status_code))
|
|
else:
|
|
assert_equal(resp.status_code, expected,
|
|
"Expected http status %s, received %s" % (expected,
|
|
resp.status_code))
|
|
return resp
|
|
|
|
def assertHasLen(self, lst, length):
|
|
"""
|
|
Assert a list/tuple/string has exact `length`
|
|
"""
|
|
msg = 'Expected to have length %s, but length is %s' \
|
|
% (length, len(lst))
|
|
self.assertEqual(len(lst), length, msg)
|
|
|
|
def assertNotEmpty(self, lst):
|
|
"""
|
|
Assert a list/tuple/string is not empty
|
|
"""
|
|
msg = 'Expected not empty, but it is'
|
|
self.assertGreater(len(lst), 0, msg)
|
|
|
|
@contextmanager
|
|
def get_tmp_repo(self):
|
|
"""
|
|
Context manager to create a tmp repo, and automatically delete it after use
|
|
|
|
with self.tmp_repo() as repo:
|
|
self.get(repo.file_url + '?p=/')
|
|
"""
|
|
repo = self.create_repo()
|
|
try:
|
|
yield repo
|
|
finally:
|
|
self.remove_repo(repo.repo_id)
|
|
|
|
@contextmanager
|
|
def get_tmp_group(self):
|
|
"""
|
|
Context manager to create a tmp group, and automatically delete it after use
|
|
|
|
with self.tmp_repo() as repo:
|
|
self.get(repo.file_url + '?p=/')
|
|
"""
|
|
group = self.create_group()
|
|
try:
|
|
yield group
|
|
finally:
|
|
self.remove_group(group.group_id)
|
|
|
|
@contextmanager
|
|
def get_tmp_user(self):
|
|
"""
|
|
Context manager to create a tmp user, and automatically delete it after use
|
|
|
|
with self.get_tmp_user() as user:
|
|
...
|
|
"""
|
|
user = self.create_user()
|
|
try:
|
|
yield user
|
|
finally:
|
|
self.remove_user(user.user_name)
|
|
|
|
def create_repo(self):
|
|
repo_name = '测试-test-repo-%s' % randstring(6)
|
|
data = {
|
|
'name': repo_name,
|
|
'desc': 'just for test - 测试用资料库',
|
|
}
|
|
repo = self.post(REPOS_URL, data=data).json()
|
|
repo_id = repo['repo_id']
|
|
return _Repo(repo_id)
|
|
|
|
def remove_repo(self, repo_id):
|
|
repo_url = urljoin(REPOS_URL, repo_id)
|
|
self.delete(repo_url)
|
|
|
|
def create_group(self):
|
|
group_name = '测试群组-%s' % randstring(16)
|
|
data = {'group_name': group_name}
|
|
group_id = self.put(GROUPS_URL, data=data).json()['group_id']
|
|
return _Group(group_name, group_id)
|
|
|
|
def remove_group(self, group_id):
|
|
group_url = urljoin(GROUPS_URL, str(group_id))
|
|
self.delete(group_url)
|
|
|
|
def create_user(self):
|
|
username = '%s@test.com' % randstring(20)
|
|
password = randstring(20)
|
|
data = {'password': password}
|
|
self.admin_put(urljoin(ACCOUNTS_URL, username), data=data, expected=201)
|
|
return _User(username, password)
|
|
|
|
def remove_user(self, username):
|
|
user_url = urljoin(ACCOUNTS_URL, username)
|
|
self.admin_delete(user_url)
|
|
|
|
def create_file(self, repo, fname=None):
|
|
if isinstance(repo, str):
|
|
repo = _Repo(repo)
|
|
fname = fname or ('文件 %s.txt' % randstring())
|
|
furl = repo.get_filepath_url('/' + fname)
|
|
data = {'operation': 'create'}
|
|
res = self.post(furl, data=data, expected=201)
|
|
self.assertEqual(res.text, '"success"')
|
|
return fname, furl
|
|
|
|
def create_dir(self, repo):
|
|
data = {'operation': 'mkdir'}
|
|
dpath = '/目录 %s' % randstring()
|
|
durl = repo.get_dirpath_url(dpath)
|
|
res = self.post(durl, data=data, expected=201)
|
|
self.assertEqual(res.text, '"success"')
|
|
return dpath, durl
|
|
|
|
|
|
def get_auth_token(username, password):
|
|
data = {
|
|
'username': username,
|
|
'password': password,
|
|
'platform': 'linux',
|
|
'device_id': '701143c1238e6736b61c20e73de82fc95989c413',
|
|
'device_name': 'test',
|
|
}
|
|
res = requests.post(TOKEN_URL, data=data)
|
|
assert_equal(res.status_code, 200)
|
|
token = res.json()['token']
|
|
assert_equal(len(token), 40)
|
|
return token
|
|
|
|
class _Repo(object):
|
|
def __init__(self, repo_id):
|
|
self.repo_id = repo_id
|
|
self.repo_url = urljoin(REPOS_URL, self.repo_id)
|
|
self.file_url = urljoin(self.repo_url, 'file')
|
|
self.dir_url = urljoin(self.repo_url, 'dir')
|
|
|
|
def get_filepath_url(self, path):
|
|
query = '?p=%s' % quote(path)
|
|
return self.file_url + query
|
|
|
|
def get_dirpath_url(self, path):
|
|
query = '?p=%s' % quote(path)
|
|
return self.dir_url + query
|
|
|
|
class _Group(object):
|
|
def __init__(self, group_name, group_id):
|
|
self.group_name = group_name
|
|
self.group_id = group_id
|
|
self.group_url = urljoin(GROUPS_URL, str(self.group_id))
|
|
|
|
class _User(object):
|
|
def __init__(self, username, password):
|
|
self.user_name = username
|
|
self.password = password
|
|
self.user_url = urljoin(ACCOUNTS_URL, username)
|