def test_copy_file(self): with self.get_tmp_repo() as repo: fname, _ = self.create_file(repo) # TODO: create another repo here, and use it as dst_repo dpath, _ = self.create_dir(repo) fopurl = urljoin(repo.repo_url, 'fileops/copy/') + '?p=/' data = { 'file_names': fname, 'dst_repo': repo.repo_id, 'dst_dir': dpath, } res = self.post(fopurl, data=data) self.assertEqual(res.text, '"success"') # create tmp file in sub folder(dpath) tmp_file = 'tmp_file.txt' furl = repo.get_filepath_url(dpath + '/' + tmp_file) data = {'operation': 'create'} res = self.post(furl, data=data, expected=201) # copy tmp file(in dpath) to dst dir fopurl = urljoin(repo.repo_url, 'fileops/copy/') + '?p=' + quote(dpath) data = { 'file_names': tmp_file, 'dst_repo': repo.repo_id, 'dst_dir': dpath, } res = self.post(fopurl, data=data) self.assertEqual(res.text, '"success"')
def test_create_user_avatar(self): # update user avatar avatar_url = reverse('api-v2.1-user-avatar') avatar_url = urljoin(BASE_URL, avatar_url) avatar_file = os.path.join(os.getcwd(), 'media/img/seafile-logo.png') random_avatar_size = random.randint(12, 128) with open(avatar_file) as f: json_resp = self.post(avatar_url, files={ 'avatar': f }, data={ 'avatar_size': random_avatar_size }).json() assert json_resp.has_key('avatar_url') response_url = json_resp['avatar_url'] list_url = response_url.split('/') assert str(random_avatar_size) in list_url # assert is NOT default avatar avatar_url = urljoin(AVATAR_BASE_URL, 'user', self.username, '/resized/80/') info = self.get(avatar_url).json() assert 'resized' in info['url'] assert info['is_default'] == False
def test_user_avatar(self): avatar_url = urljoin(AVATAR_BASE_URL, 'user', self.username, '/resized/80/') info = self.get(avatar_url).json() self.assertIsNotNone(info['url']) self.assertIsNotNone(info['is_default']) self.assertIsNotNone(info['mtime'])
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)
def test_download_dir(self): with self.get_tmp_repo() as repo: dpath, _ = self.create_dir(repo) query = '?p=%s' % quote(dpath) ddurl = urljoin(repo.dir_url, 'download') + query res = self.get(ddurl) self.assertRegexpMatches(res.text, r'"http(.*)/files/[^/]+/%s"' % quote(dpath[1:]))
def test_generate_client_login_token(self): url = self._get_client_login_url() r = requests.get(url) assert r.url == BASE_URL r = requests.get(url) assert r.url == urljoin(BASE_URL, 'accounts/login/?next=/'), \ 'a client login token can only be used once'
def test_create_user_avatar(self): # update user avatar avatar_url = reverse('api-v2.1-user-avatar') avatar_url = urljoin(BASE_URL, avatar_url) avatar_file = os.path.join(os.getcwd(), 'media/img/seafile-logo.png') with open(avatar_file) as f: json_resp = self.post(avatar_url, files={'avatar': f}).json() assert json_resp['success'] == True # assert is NOT default avatar avatar_url = urljoin(AVATAR_BASE_URL, 'user', self.username, '/resized/80/') info = self.get(avatar_url).json() assert 'resized' in info['url'] assert info['is_default'] == False
def test_download_dir(self): with self.get_tmp_repo() as repo: dpath, _ = self.create_dir(repo) query = '?p=%s' % quote(dpath) ddurl = urljoin(repo.dir_url, 'download') + query res = self.get(ddurl) self.assertRegexpMatches( res.text, r'"http(.*)/files/[^/]+/%s"' % quote(dpath[1:]))
def create_user(self): username = '******' % randstring(20) password = randstring(20) data = {'password': password} self.admin_put(urljoin(ACCOUNTS_URL, username), data=data, expected=201) return _User(username, password)
def test_update_favicon_with_invalid_user_permission(self): # update user avatar logo_url = reverse('api-v2.1-admin-favicon') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'media/img/seafile-logo.png') with open(logo_file) as f: json_resp = self.post(logo_url, files={'logo': f}, expected=403).json()
def test_add_remove_group_member(self): with self.get_tmp_user() as user: with self.get_tmp_group() as group: test_group_members_url = urljoin(group.group_url, '/members/') data = {'user_name': user.user_name} res = self.put(test_group_members_url, data=data).json() self.assertTrue(res['success']) res = self.delete(test_group_members_url, data=data).json() self.assertTrue(res['success'])
def test_group_avatar(self): gname = randstring(16) data = {'group_name': gname} res = self.put(GROUPS_URL, data=data) gid = res.json()['group_id'] avatar_url = urljoin(AVATAR_BASE_URL, 'group', str(gid), '/resized/80/') info = self.get(avatar_url).json() self.assertIsNotNone(info) self.assertIsNotNone(info['url']) self.assertIsNotNone(info['is_default']) self.assertIsNotNone(info['mtime'])
def test_update_favicon_with_invalid_user_permission(self): self.logout() self.login_as(self.user) # update user avatar logo_url = reverse('api-v2.1-admin-favicon') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'media/img/seafile-logo.png') with open(logo_file) as f: resp = self.client.post(logo_url, {'favicon': f}) assert resp.status_code == 403
def test_update_license(self, mock_ccnet_api): mock_ccnet_api.return_val = {} url = reverse('api-v2.1-admin-license') url = urljoin(BASE_URL, url) with open( os.path.join(os.getcwd(), 'tests/seahub/utils/seafile-license.txt')) as f: json_resp = self.client.post(url, {'license': f}) json_resp = json.loads(json_resp.content) assert json_resp['success'] is True assert os.path.exists(LICENSE_PATH)
def test_fetch_repo_download_info(self): with self.get_tmp_repo() as repo: download_info_repo_url = urljoin(repo.repo_url, '/download-info/') info = self.get(download_info_repo_url).json() self.assertIsNotNone(info['relay_addr']) self.assertIsNotNone(info['token']) self.assertIsNotNone(info['repo_id']) self.assertIsNotNone(info['relay_port']) self.assertIsNotNone(info['encrypted']) self.assertIsNotNone(info['repo_name']) self.assertIsNotNone(info['relay_id']) self.assertIsNotNone(info['email'])
def test_update_logo_with_invalid_user_permission(self): self.logout() # update user avatar logo_url = reverse('api-v2.1-admin-logo') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'media/img/seafile-logo.png') with open(logo_file, 'rb') as f: resp = self.client.post(logo_url, {'logo': f}) assert 403 == resp.status_code
def test_fetch_repo_download_info(self): with self.get_tmp_repo() as repo: download_info_repo_url = urljoin(repo.repo_url, "/download-info/") info = self.get(download_info_repo_url).json() self.assertIsNotNone(info["relay_addr"]) self.assertIsNotNone(info["token"]) self.assertIsNotNone(info["repo_id"]) self.assertIsNotNone(info["relay_port"]) self.assertIsNotNone(info["encrypted"]) self.assertIsNotNone(info["repo_name"]) self.assertIsNotNone(info["relay_id"]) self.assertIsNotNone(info["email"])
def test_update_favicon_with_invalid_filetype(self): with open('test.noico', 'w') as f: f.write('hello') logo_url = reverse('api-v2.1-admin-favicon') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'test.noico') with open(logo_file) as f: resp = self.client.post(logo_url, {'favicon': f}) json_resp = json.loads(resp.content) assert resp.status_code == 400 assert json_resp['error_msg'] == file_type_error_msg('noico', PREVIEW_FILEEXT.get(IMAGE))
def test_add_remove_group_with_blank_and_hyphen(self): data = {'group_name': randstring(4) + '-' + randstring(4) + ' ' + randstring(4)} info = self.put(GROUPS_URL, data=data).json() self.assertTrue(info['success']) group_id = info['group_id'] self.assertGreater(group_id, 0) url = urljoin(GROUPS_URL, str(group_id)) self.delete(url) # check group is really removed groups = self.get(GROUPS_URL).json()['groups'] for group in groups: self.assertNotEqual(group['id'], group_id)
def test_copy_file(self): with self.get_tmp_repo() as repo: fname, _ = self.create_file(repo) # TODO: create another repo here, and use it as dst_repo dpath, _ = self.create_dir(repo) fopurl = urljoin(repo.repo_url, 'fileops/copy/') + '?p=/' data = { 'file_names': fname, 'dst_repo': repo.repo_id, 'dst_dir': dpath, } res = self.post(fopurl, data=data) self.assertEqual(res.text, '"success"')
def test_update_favicon_with_invalid_filetype(self): with open('test.noico', 'w') as f: f.write('hello') logo_url = reverse('api-v2.1-admin-favicon') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'test.noico') with open(logo_file, 'rb') as f: resp = self.client.post(logo_url, {'favicon': f}) json_resp = json.loads(resp.content) assert resp.status_code == 400 assert json_resp['error_msg'] == file_type_error_msg('noico', PREVIEW_FILEEXT.get(IMAGE))
def test_update_license_with_invalid_type(self, mock_ccnet_api): mock_ccnet_api.return_val = {} url = reverse('api-v2.1-admin-license') url = urljoin(BASE_URL, url) with open('temp.notxt', 'w') as f: f.write('1') with open(os.path.join(os.getcwd(), 'temp.notxt')) as f: resp = self.client.post(url, {'license': f}) json_resp = json.loads(resp.content) assert 400 == resp.status_code assert file_type_error_msg('notxt', 'txt') == json_resp['error_msg']
def test_share_dir(self): with self.get_tmp_repo() as repo: dpath, _ = self.create_dir(repo) query = '?p=%s' % quote(dpath) share_dir_url = urljoin(repo.dir_url, 'share/') + query with self.get_tmp_user() as user: data = { 'emails': user.user_name, 's_type': 'd', 'path': '/', 'perm': 'r' } res = self.post(share_dir_url, data=data) self.assertEqual(res.text, u'{}')
def test_download_file_from_history(self): with self.get_tmp_repo() as repo: fname, _ = self.create_file(repo) file_history_url = urljoin(repo.repo_url, 'history/') + \ '?p=/%s' % quote(fname) res = self.get(file_history_url).json() commit_id = res['commits'][0]['id'] self.assertEqual(len(commit_id), 40) data = { 'p': fname, 'commit_id': commit_id, } query = '?' + urlencode(data) res = self.get(repo.file_url + query) self.assertRegexpMatches(res.text, r'"http(.*)/%s"' % quote(fname))
def test_update_logo_with_invalid_file_type(self): with open('test.noimage', 'w') as f: f.write('1') logo_url = reverse('api-v2.1-admin-logo') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'test.noimage') with open(logo_file, 'rb') as f: resp = self.client.post(logo_url, {'logo': f}) json_resp = json.loads(resp.content) os.remove(logo_file) assert 400 == resp.status_code assert json_resp['error_msg'] == file_type_error_msg( 'noimage', PREVIEW_FILEEXT.get(IMAGE))
def test_update_logo_with_invalid_file_type(self): with open('test.noimage', 'w') as f: f.write('hello') image_url = reverse('api-v2.1-admin-login-background-image') image_url = urljoin(BASE_URL, image_url) image_file = os.path.join(os.getcwd(), 'test.noimage') with open(image_file, 'rb') as f: resp = self.client.post(image_url, {'login_bg_image': f}) json_resp = json.loads(resp.content) os.remove(image_file) assert 400 == resp.status_code assert json_resp['error_msg'] == file_type_error_msg("noimage", PREVIEW_FILEEXT.get('Image'))
def test_update_license(self, mock_ccnet_api): mock_ccnet_api.return_val = {} url = reverse('api-v2.1-admin-license') url = urljoin(BASE_URL, url) with open( os.path.join(os.getcwd(), 'tests/seahub/utils/seafile-license.txt'), 'rb') as f: resp = self.client.post(url, {'license': f}) json_resp = json.loads(resp.content) assert json_resp['license_expiration'] is not None assert json_resp['license_mode'] is not None assert json_resp['license_maxusers'] is not None assert json_resp['license_to'] is not None assert os.path.exists(LICENSE_PATH)
def test_update_favicon(self): custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(CUSTOM_FAVICON_PATH)) if os.path.exists(custom_symlink): os.remove(custom_symlink) assert not os.path.exists(custom_symlink) # update user avatar logo_url = reverse('api-v2.1-admin-logo') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'media/img/seafile-logo.png') with open(logo_file) as f: json_resp = self.admin_post(logo_url, files={'logo': f}).json() assert json_resp['success'] == True assert os.path.exists(custom_symlink) assert os.path.islink(custom_symlink)
def test_update_logo(self): custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(CUSTOM_LOGIN_BG_PATH)) if os.path.exists(custom_symlink): os.remove(custom_symlink) assert not os.path.exists(custom_symlink) # update user avatar image_url = reverse('api-v2.1-admin-login-background-image') image_url = urljoin(BASE_URL, image_url) image_file = os.path.join(os.getcwd(), 'media/img/seafile-logo.png') with open(image_file, 'rb') as f: resp = self.client.post(image_url, {'login_bg_image': f}) json_resp = json.loads(resp.content) assert 200 == resp.status_code assert json_resp['login_bg_image_path'] == get_service_url() + MEDIA_URL + CUSTOM_LOGIN_BG_PATH assert os.path.exists(custom_symlink) assert os.path.islink(custom_symlink)
def test_update_favicon(self): custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(CUSTOM_FAVICON_PATH)) if os.path.exists(custom_symlink): os.remove(custom_symlink) assert not os.path.exists(custom_symlink) # update user avatar logo_url = reverse('api-v2.1-admin-favicon') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'media/img/seafile-logo.png') with open(logo_file) as f: resp = self.client.post(logo_url, {'favicon': f}) assert resp.status_code == 200 json_resp = json.loads(resp.content) assert json_resp['success'] == True assert os.path.exists(custom_symlink) assert os.path.islink(custom_symlink)
def test_get_repo_history(self): 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'])
def test_update_favicon(self): custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(CUSTOM_FAVICON_PATH)) if os.path.exists(custom_symlink): os.remove(custom_symlink) assert not os.path.exists(custom_symlink) # update user avatar logo_url = reverse('api-v2.1-admin-favicon') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'media/img/seafile-logo.png') with open(logo_file, 'rb') as f: resp = self.client.post(logo_url, {'favicon': f}) assert resp.status_code == 200 json_resp = json.loads(resp.content) assert json_resp['favicon_path'] == get_service_url() + MEDIA_URL + CUSTOM_FAVICON_PATH assert os.path.exists(custom_symlink) assert os.path.islink(custom_symlink)
def test_create_repo(self): data = {'name': 'test'} res = self.post(REPOS_URL, data=data) repo = res.json() repo_id = repo['repo_id'] 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_update_logo(self): custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(CUSTOM_LOGO_PATH)) if os.path.exists(custom_symlink): os.remove(custom_symlink) assert not os.path.exists(custom_symlink) # update user avatar logo_url = reverse('api-v2.1-admin-logo') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'media/img/seafile-logo.png') with open(logo_file, 'rb') as f: resp = self.client.post(logo_url, {'logo': f}) json_resp = json.loads(resp.content) assert 200 == resp.status_code assert json_resp['success'] == True assert os.path.exists(custom_symlink) assert os.path.islink(custom_symlink)
def test_create_file_shared_link(self): with self.get_tmp_repo() as repo: fname, _ = self.create_file(repo) fsurl = urljoin(repo.file_url, 'shared-link') data = { 'type': 'f', 'p': '/' + fname, } res = self.put(fsurl, data=data, expected=201) self.assertRegexpMatches(res.headers['Location'], \ r'http(.*)/f/(\w{10,10})/') res = self.get(SHARED_LINKS_URL).json() self.assertNotEmpty(res) for fileshare in res['fileshares']: self.assertIsNotNone(fileshare['username']) self.assertIsNotNone(fileshare['repo_id']) #self.assertIsNotNone(fileshare['ctime']) self.assertIsNotNone(fileshare['s_type']) self.assertIsNotNone(fileshare['token']) self.assertIsNotNone(fileshare['view_cnt']) self.assertIsNotNone(fileshare['path'])
def test_get_repo_history(self): 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['desc']) self.assertIsNotNone(commit['id']) self.assertIsNotNone(commit['conflict'])
def test_get_repo_history(self): 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["desc"]) self.assertIsNotNone(commit["id"]) self.assertIsNotNone(commit["conflict"])
def test_create_repo(self): data = {"name": "test"} res = self.post(REPOS_URL, data=data) repo = res.json() repo_id = repo["repo_id"] 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 __init__(self, username, password): self.user_name = username self.password = password self.user_url = urljoin(ACCOUNTS_URL, username)
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))
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 remove_user(self, username): user_url = urljoin(ACCOUNTS_URL, username) self.admin_delete(user_url)
def remove_group(self, group_id): group_url = urljoin(GROUPS_URL, str(group_id)) self.delete(group_url)
def remove_repo(self, repo_id): repo_url = urljoin(REPOS_URL, repo_id) self.delete(repo_url)
def test_client_login_token_wont_enter_sudo_mode(self): url = self._get_client_login_url(admin=True) url += '&next=/sys/useradmin' r = requests.get(url) assert r.url == urljoin(BASE_URL, '/sys/sudo/?next=/sys/useradmin/')
import requests import unittest from tests.common.utils import apiurl, urljoin, randstring from tests.api.apitestbase import ApiTestBase from tests.api.urls import ACCOUNTS_URL, ACCOUNT_INFO_URL, PING_URL, \ AUTH_PING_URL test_account_username = '******' % randstring(10) test_account_password = randstring(20) test_account_password2 = randstring(20) test_account_url = urljoin(ACCOUNTS_URL, test_account_username) class AccountsApiTest(ApiTestBase): def test_check_account_info(self): info = self.get(ACCOUNT_INFO_URL).json() self.assertIsNotNone(info) self.assertEqual(info['email'], self.username) self.assertIsNotNone(info['total']) self.assertIsNotNone(info['usage']) def test_list_accounts(self): # Normal user can not list accounts self.get(ACCOUNTS_URL, expected=403) accounts = self.admin_get(ACCOUNTS_URL).json() self.assertGreaterEqual(accounts, 2) # TODO: check returned json, test start/limit param def test_create_delete_account(self): data = {'password': test_account_password} # non-admin user can not create new user
def test_get_update_blocks_link(self): with self.get_tmp_repo() as repo: update_blks_url = urljoin(repo.repo_url, 'update-blks-link') res = self.get(update_blks_url) self.assertRegexpMatches(res.text, r'"http(.*)/update-blks-api/[^/]+"')