1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-04-28 03:10:45 +00:00
seahub/tests/api/test_repos.py

261 lines
9.7 KiB
Python
Raw Permalink Normal View History

2014-09-05 02:07:33 +00:00
#coding: UTF-8
"""
Test repos api.
"""
import json
import time
2015-07-23 03:46:56 +00:00
import pytest
import uuid
import pytest
pytestmark = pytest.mark.django_db
2014-09-05 02:07:33 +00:00
2020-07-27 06:59:18 +00:00
from django.urls import reverse
2015-08-28 08:04:37 +00:00
from seaserv import seafile_api
2014-09-06 03:38:20 +00:00
from tests.api.apitestbase import ApiTestBase
from tests.api.urls import (
2018-12-06 03:24:20 +00:00
REPOS_URL, GET_REPO_TOKENS_URL
)
2014-09-05 02:07:33 +00:00
from tests.common.utils import apiurl, urljoin, randstring
from tests.common.common import SEAFILE_BASE_URL
2014-09-05 02:07:33 +00:00
from seahub.test_utils import BaseTestCase
2014-09-06 03:38:20 +00:00
# TODO: all tests should be run on an encrypted repo
2014-09-05 02:07:33 +00:00
class ReposApiTest(ApiTestBase):
def test_list_repos(self):
repos = self.get(REPOS_URL).json()
# self.assertHasLen(repos, 1)
2014-09-06 03:38:20 +00:00
2014-09-05 02:07:33 +00:00
for repo in repos:
self.assertIsNotNone(repo['permission'])
self.assertIsNotNone(repo['encrypted'])
self.assertGreater(repo['mtime'], 0)
self.assertIsNotNone(repo['mtime_relative'])
2014-09-05 02:07:33 +00:00
self.assertIsNotNone(repo['owner'])
self.assertIsNotNone(repo['id'])
self.assertIsNotNone(repo['size'])
self.assertIsNotNone(repo['name'])
self.assertIsNotNone(repo['type'])
self.assertIsNotNone(repo['head_commit_id'])
assert len(repo['modifier_email']) > 0
assert len(repo['modifier_name']) > 0
assert len(repo['modifier_contact_email']) > 0
2014-09-05 02:07:33 +00:00
def test_get_repo_info(self):
2014-09-06 03:38:20 +00:00
with self.get_tmp_repo() as repo:
rinfo = self.get(repo.repo_url).json()
self.assertFalse(rinfo['encrypted'])
self.assertIsNotNone(rinfo['mtime'])
self.assertIsNotNone(rinfo['owner'])
self.assertIsNotNone(rinfo['id'])
self.assertIsNotNone(rinfo['size'])
self.assertIsNotNone(rinfo['name'])
self.assertIsNotNone(rinfo['root'])
self.assertIsNotNone(rinfo['type'])
assert len(rinfo['modifier_email']) > 0
assert len(rinfo['modifier_name']) > 0
assert len(rinfo['modifier_contact_email']) > 0
2014-09-05 02:07:33 +00:00
def test_get_repo_history(self):
2014-09-06 03:38:20 +00:00
with self.get_tmp_repo() as repo:
self.create_file(repo)
self.create_dir(repo)
repo_history_url = urljoin(repo.repo_url, '/history/')
history = self.get(repo_history_url).json()
commits = history['commits']
self.assertHasLen(commits, 3)
for commit in commits:
self.assertIsNotNone(commit['rev_file_size'])
#self.assertIsNotNone(commit['rev_file_id']) #allow null
self.assertIsNotNone(commit['ctime'])
self.assertIsNotNone(commit['creator_name'])
self.assertIsNotNone(commit['creator'])
self.assertIsNotNone(commit['root_id'])
#self.assertIsNotNone(commit['rev_renamed_old_path']) #allow null
#self.assertIsNotNone(commit['parent_id']) #allow null
self.assertIsNotNone(commit['new_merge'])
self.assertIsNotNone(commit['repo_id'])
self.assertIsNotNone(commit['id'])
self.assertIsNotNone(commit['conflict'])
#self.assertIsNotNone(commit['second_parent_id']) #allow null
2014-09-05 02:07:33 +00:00
def test_create_repo(self):
data = {'name': 'test'}
res = self.post(REPOS_URL, data=data)
repo = res.json()
repo_id = repo['repo_id']
2014-09-06 03:38:20 +00:00
try:
self.assertIsNotNone(repo['encrypted'])
self.assertIsNotNone(repo['enc_version'])
self.assertIsNotNone(repo['repo_id'])
self.assertIsNotNone(repo['magic'])
self.assertIsNotNone(repo['relay_id'])
self.assertIsNotNone(repo['repo_version'])
self.assertIsNotNone(repo['relay_addr'])
self.assertIsNotNone(repo['token'])
self.assertIsNotNone(repo['relay_port'])
self.assertIsNotNone(repo['random_key'])
self.assertIsNotNone(repo['email'])
self.assertIsNotNone(repo['repo_name'])
finally:
self.remove_repo(repo_id)
# Check the repo is really removed
self.get(urljoin(REPOS_URL, repo_id), expected=404)
def test_create_repo_with_invalid_name(self):
data = {'name': 'test/test'}
self.post(REPOS_URL, data=data, expected=400)
2014-09-06 03:38:20 +00:00
def test_check_or_create_sub_repo(self):
# TODO: create a sub folder and use it as a sub repo
2014-09-05 02:07:33 +00:00
pass
2015-07-23 03:46:56 +00:00
@pytest.mark.xfail
def test_generate_repo_tokens(self):
with self.get_tmp_repo() as ra:
with self.get_tmp_repo() as rb:
fake_repo_id = str(uuid.uuid4())
repo_ids = ','.join([ra.repo_id, rb.repo_id, fake_repo_id])
tokens = self.get(GET_REPO_TOKENS_URL + '?repos=%s' % repo_ids).json()
assert ra.repo_id in tokens
assert rb.repo_id in tokens
assert fake_repo_id not in tokens
Python3 master (#4076) * 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
2019-09-11 03:46:43 +00:00
for repo_id, token in tokens.items():
self._get_repo_info(token, repo_id)
def test_generate_repo_tokens_reject_invalid_params(self):
self.get(GET_REPO_TOKENS_URL, expected=400)
self.get(GET_REPO_TOKENS_URL + '?repos=badxxx', expected=400)
def _get_repo_info(self, sync_token, repo_id, **kwargs):
headers = {
'Seafile-Repo-Token': sync_token
}
url = urljoin(SEAFILE_BASE_URL,
'repo/%s/permission-check/?op=upload' % repo_id)
self.get(url, use_token=False, headers=headers, **kwargs)
2015-08-28 08:04:37 +00:00
class NewReposApiTest(BaseTestCase):
# @pytest.mark.xfail
def setUp(self):
self.clear_cache()
self.login_as(self.admin)
from constance import config
self.config = config
self.config.ENABLE_ENCRYPTED_LIBRARY = True
def test_create_encrypted_repo(self):
2015-08-28 08:04:37 +00:00
"""Test create an encrypted repo with the secure keys generated on client
side.
"""
repo_id = str(uuid.uuid4())
password = randstring(16)
enc_version = 2
enc_info = seafile_api.generate_magic_and_random_key(enc_version, repo_id, password)
2015-08-28 08:04:37 +00:00
data = {
'name': 'enc-test',
'repo_id': repo_id,
'enc_version': enc_version,
'magic': enc_info.magic,
'random_key': enc_info.random_key,
2015-08-28 08:04:37 +00:00
}
repo = self.client.post(REPOS_URL, data=data)
repo = json.loads(repo.content)
2015-08-28 08:04:37 +00:00
assert repo['repo_id'] == repo_id
assert repo['encrypted']
assert repo['magic'] == enc_info.magic
assert repo['random_key'] == enc_info.random_key
2015-08-28 08:04:37 +00:00
# validate the password on server
set_password_url = apiurl('/api2/repos/{}/'.format(repo['repo_id']))
self.client.post(set_password_url, data={'password': password})
# do some file operation
self.create_file(repo_id=repo['repo_id'], parent_dir='/', filename='repo-test', username=self.admin.username)
def test_create_encrypted_repo_with_disable_create_enc_library(self):
"""Test create an encrypted repo with the secure keys generated on client
side.
"""
self.config.ENABLE_ENCRYPTED_LIBRARY = False
repo_id = str(uuid.uuid4())
password = randstring(16)
enc_version = 2
enc_info = seafile_api.generate_magic_and_random_key(enc_version, repo_id, password)
data = {
'name': 'enc-test-with-disable-encry',
'repo_id': repo_id,
'enc_version': enc_version,
'magic': enc_info.magic,
'random_key': enc_info.random_key,
}
res = self.client.post(REPOS_URL, data=data)
assert res.status_code == 403
2015-08-28 08:04:37 +00:00
def test_create_encrypted_repo_with_invalid_repoid(self):
"""Test create an encrypted repo with the secure keys generated on client
side.
"""
repo_id = 'qwer'
password = randstring(16)
enc_version = 2
enc_info = seafile_api.generate_magic_and_random_key(enc_version, repo_id, password)
data = {
'name': 'enc-test-with-disable-encry',
'repo_id': repo_id,
'enc_version': enc_version,
'magic': enc_info.magic,
'random_key': enc_info.random_key,
}
res = self.client.post(REPOS_URL, data=data)
Python3 master (#4076) * 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
2019-09-11 03:46:43 +00:00
print(res)
assert res.status_code == 400
# repo = res.json()
# assert repo['repo_id'] == repo_id
# assert repo['encrypted']
# assert repo['magic'] == enc_info.magic
# assert repo['random_key'] == enc_info.random_key
#
# # validate the password on server
# set_password_url = apiurl('/api2/repos/{}/'.format(repo['repo_id']))
# self.post(set_password_url, data={'password': password})
#
2015-08-28 08:04:37 +00:00
# do some file operation
# Uncomment following to test api performance.
# class ReposApiTest2(BaseTestCase):
# def setUp(self):
# self.num_repos = 500
# self.tmp_user = self.create_user()
# self.login_as(self.tmp_user)
# self.repo_ids = []
# for i in range(self.num_repos):
# r = self.create_repo(name='test-repo%d' % i, desc='',
# username=self.tmp_user.username, passwd=None)
# self.repo_ids.append(r)
# time.sleep(0.01)
# assert len(self.repo_ids) == self.num_repos
# def tearDown(self):
# self.remove_user(self.tmp_user.username)
# for e in self.repo_ids:
# self.remove_repo(e)
# def test_list_repos(self):
# time.sleep(1)
# print '--------> start list repos...'
# resp = self.client.get(reverse('api2-repos') + '?type=mine')
# json_resp = json.loads(resp.content)
# assert len(json_resp) == self.num_repos
# print '--------> end list repos.'