2015-12-03 08:45:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import json
|
|
|
|
from mock import patch
|
2019-06-26 10:19:00 +00:00
|
|
|
import random
|
2015-12-03 08:45:08 +00:00
|
|
|
|
2020-07-27 06:59:18 +00:00
|
|
|
from django.urls import reverse
|
2015-12-03 08:45:08 +00:00
|
|
|
from seaserv import seafile_api
|
|
|
|
|
|
|
|
from seahub.test_utils import BaseTestCase
|
|
|
|
from seahub.api2.endpoints.groups import Groups
|
2015-12-07 03:33:49 +00:00
|
|
|
from tests.common.utils import randstring
|
2015-12-03 08:45:08 +00:00
|
|
|
|
2020-12-15 05:18:57 +00:00
|
|
|
|
2015-12-03 08:45:08 +00:00
|
|
|
class GroupsTest(BaseTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.login_as(self.user)
|
|
|
|
self.group_id = self.group.id
|
|
|
|
self.group_name = self.group.group_name
|
|
|
|
self.repo_id = self.repo.id
|
|
|
|
|
|
|
|
self.url = reverse('api-v2.1-groups')
|
|
|
|
|
|
|
|
# share repo to group
|
|
|
|
seafile_api.set_group_repo(self.repo_id,
|
2020-12-15 05:18:57 +00:00
|
|
|
self.group_id, self.user.email, 'rw')
|
2015-12-03 08:45:08 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.remove_group()
|
|
|
|
self.remove_repo()
|
|
|
|
|
|
|
|
def test_get_group_info(self):
|
|
|
|
resp = self.client.get(self.url)
|
|
|
|
self.assertEqual(200, resp.status_code)
|
|
|
|
|
|
|
|
json_resp = json.loads(resp.content)
|
2020-12-15 05:18:57 +00:00
|
|
|
assert len(json_resp[0]) == 8
|
2015-12-03 08:45:08 +00:00
|
|
|
|
|
|
|
group_ids = []
|
|
|
|
for group in json_resp:
|
|
|
|
group_ids.append(group['id'])
|
|
|
|
|
|
|
|
assert self.group_id in group_ids
|
|
|
|
|
|
|
|
def test_get_group_info_with_repos(self):
|
|
|
|
resp = self.client.get(self.url + '?with_repos=1')
|
|
|
|
self.assertEqual(200, resp.status_code)
|
|
|
|
|
|
|
|
json_resp = json.loads(resp.content)
|
2020-12-15 05:18:57 +00:00
|
|
|
assert len(json_resp[0]) == 9
|
2015-12-03 08:45:08 +00:00
|
|
|
|
|
|
|
group_ids = []
|
|
|
|
group_repos = []
|
|
|
|
for group in json_resp:
|
|
|
|
group_ids.append(group['id'])
|
|
|
|
for repo in group['repos']:
|
|
|
|
group_repos.append(repo)
|
|
|
|
|
|
|
|
group_repo_ids = []
|
|
|
|
for repo in group_repos:
|
|
|
|
group_repo_ids.append(repo['id'])
|
|
|
|
|
|
|
|
assert self.repo_id in group_repo_ids
|
|
|
|
assert self.group_id in group_ids
|
|
|
|
|
|
|
|
def test_create_group(self):
|
2015-12-07 03:33:49 +00:00
|
|
|
new_group_name = 'new-group-' + randstring(6)
|
2015-12-03 08:45:08 +00:00
|
|
|
|
2016-01-06 11:12:24 +00:00
|
|
|
resp = self.client.post(self.url, {'name': new_group_name})
|
2015-12-03 08:45:08 +00:00
|
|
|
self.assertEqual(201, resp.status_code)
|
|
|
|
|
|
|
|
json_resp = json.loads(resp.content)
|
2020-12-15 05:18:57 +00:00
|
|
|
assert len(json_resp) == 8
|
2015-12-03 08:45:08 +00:00
|
|
|
assert json_resp['name'] == new_group_name
|
2016-01-06 11:12:24 +00:00
|
|
|
assert json_resp['owner'] == self.user.email
|
2015-12-03 08:45:08 +00:00
|
|
|
|
|
|
|
self.remove_group(json_resp['id'])
|
|
|
|
|
|
|
|
def test_create_group_with_cn_name(self):
|
2019-09-11 03:46:43 +00:00
|
|
|
new_group_name = '中文' + randstring(6)
|
2016-01-06 11:12:24 +00:00
|
|
|
resp = self.client.post(self.url, {'name': new_group_name})
|
2015-12-03 08:45:08 +00:00
|
|
|
self.assertEqual(201, resp.status_code)
|
|
|
|
|
|
|
|
json_resp = json.loads(resp.content)
|
2020-12-15 05:18:57 +00:00
|
|
|
assert len(json_resp) == 8
|
2015-12-03 08:45:08 +00:00
|
|
|
assert json_resp['name'] == new_group_name
|
2016-01-06 11:12:24 +00:00
|
|
|
assert json_resp['owner'] == self.user.email
|
2015-12-03 08:45:08 +00:00
|
|
|
|
|
|
|
self.remove_group(json_resp['id'])
|
|
|
|
|
|
|
|
def test_can_not_create_group_with_same_name(self):
|
|
|
|
resp = self.client.post(self.url, {'group_name': self.group_name})
|
|
|
|
self.assertEqual(400, resp.status_code)
|
|
|
|
|
2019-06-26 10:19:00 +00:00
|
|
|
def test_can_create_by_limit_punctuation(self):
|
|
|
|
limit_punctuation = """-'_."""
|
|
|
|
new_group_name = randstring(2) + random.choice(limit_punctuation) + randstring(2)
|
|
|
|
|
|
|
|
resp = self.client.post(self.url, {'name': new_group_name})
|
|
|
|
self.assertEqual(201, resp.status_code)
|
|
|
|
|
|
|
|
json_resp = json.loads(resp.content)
|
|
|
|
assert json_resp['name'] == new_group_name
|
|
|
|
assert json_resp['owner'] == self.user.email
|
|
|
|
|
|
|
|
self.remove_group(json_resp['id'])
|
|
|
|
|
|
|
|
def test_can_not_create_by_other_punctuation(self):
|
|
|
|
other_punctuation = """!"#$%&()*+,/:;<=>?@[\]^`{|}~"""
|
|
|
|
new_group_name = randstring(2) + random.choice(other_punctuation) + randstring(2)
|
|
|
|
|
|
|
|
resp = self.client.post(self.url, {'name': new_group_name})
|
|
|
|
self.assertEqual(400, resp.status_code)
|
|
|
|
|
2015-12-03 08:45:08 +00:00
|
|
|
def test_can_not_create_group_with_invalid_name(self):
|
2015-12-07 03:33:49 +00:00
|
|
|
new_group_name = 'new%group-' + randstring(6)
|
2015-12-03 08:45:08 +00:00
|
|
|
|
2015-12-07 03:33:49 +00:00
|
|
|
resp = self.client.post(self.url, {'group_name': new_group_name})
|
2015-12-03 08:45:08 +00:00
|
|
|
self.assertEqual(400, resp.status_code)
|
|
|
|
|
|
|
|
@patch.object(Groups, '_can_add_group')
|
|
|
|
def test_can_not_create_group_with_invalid_permission(self, mock_can_add_group):
|
|
|
|
mock_can_add_group.return_value = False
|
2015-12-07 03:33:49 +00:00
|
|
|
new_group_name = 'new-group-' + randstring(6)
|
2015-12-03 08:45:08 +00:00
|
|
|
|
2015-12-07 03:33:49 +00:00
|
|
|
resp = self.client.post(self.url, {'group_name': new_group_name})
|
2015-12-03 08:45:08 +00:00
|
|
|
self.assertEqual(403, resp.status_code)
|
2015-12-07 03:33:49 +00:00
|
|
|
|
|
|
|
def test_can_rename_group(self):
|
|
|
|
new_group_name = 'new-group-' + randstring(6)
|
|
|
|
url = reverse('api-v2.1-group', args=[self.group_id])
|
2016-01-06 11:12:24 +00:00
|
|
|
data = 'name=%s' % new_group_name
|
2015-12-07 03:33:49 +00:00
|
|
|
|
|
|
|
resp = self.client.put(url, data, 'application/x-www-form-urlencoded')
|
|
|
|
self.assertEqual(200, resp.status_code)
|
|
|
|
|
|
|
|
json_resp = json.loads(resp.content)
|
|
|
|
assert json_resp['name'] == new_group_name
|
|
|
|
|
|
|
|
def test_can_transfer_group(self):
|
|
|
|
new_creator = self.admin.email
|
|
|
|
url = reverse('api-v2.1-group', args=[self.group_id])
|
2016-01-06 11:12:24 +00:00
|
|
|
data = 'owner=%s' % new_creator
|
2015-12-07 03:33:49 +00:00
|
|
|
|
|
|
|
resp = self.client.put(url, data, 'application/x-www-form-urlencoded')
|
|
|
|
self.assertEqual(200, resp.status_code)
|
|
|
|
|
|
|
|
json_resp = json.loads(resp.content)
|
2016-05-06 06:52:28 +00:00
|
|
|
|
2016-01-06 11:12:24 +00:00
|
|
|
assert json_resp['owner'] == new_creator
|
2016-05-06 06:52:28 +00:00
|
|
|
assert new_creator in json_resp['admins']
|
|
|
|
assert self.user.email not in json_resp['admins']
|
2015-12-07 03:33:49 +00:00
|
|
|
|
2015-12-08 10:43:32 +00:00
|
|
|
def test_can_not_transfer_group_to_group_owner(self):
|
2015-12-08 08:40:17 +00:00
|
|
|
new_creator = self.user.email
|
|
|
|
url = reverse('api-v2.1-group', args=[self.group_id])
|
2016-01-06 11:12:24 +00:00
|
|
|
data = 'owner=%s' % new_creator
|
2015-12-08 08:40:17 +00:00
|
|
|
|
|
|
|
resp = self.client.put(url, data, 'application/x-www-form-urlencoded')
|
|
|
|
self.assertEqual(400, resp.status_code)
|
|
|
|
|
2015-12-07 03:33:49 +00:00
|
|
|
def test_can_delete_group(self):
|
|
|
|
url = reverse('api-v2.1-group', args=[self.group_id])
|
|
|
|
resp = self.client.delete(url)
|
|
|
|
self.assertEqual(200, resp.status_code)
|
|
|
|
|
|
|
|
json_resp = json.loads(resp.content)
|
|
|
|
assert json_resp['success'] is True
|