diff --git a/tests/config.py b/tests/config.py index c70841c9..030382bc 100644 --- a/tests/config.py +++ b/tests/config.py @@ -7,3 +7,6 @@ INACTIVE_USER = 'inactiveuser@test.seafile.com' INACTIVE_PASSWORD = 'inactiveuser' + +ORG_PREFIX = 'seafile_org' +ORG_CREATOR = 'org_user' diff --git a/tests/conftest.py b/tests/conftest.py index 6fff38c0..ed2bc852 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,9 +7,9 @@ from tenacity import retry, stop_after_attempt, wait_fixed from tests.config import ( ADMIN_PASSWORD, ADMIN_USER, INACTIVE_PASSWORD, INACTIVE_USER, PASSWORD, - PASSWORD2, USER, USER2 + PASSWORD2, USER, USER2, ORG_PREFIX, ORG_CREATOR ) -from tests.utils import create_and_get_repo, randstring, create_and_get_group +from tests.utils import create_and_get_repo, randstring, create_and_get_group, create_and_get_org from seaserv import ccnet_api, seafile_api @@ -60,6 +60,22 @@ def group(): finally: if ccnet_api.get_group(group.id): ccnet_api.remove_group(group.id) + assert ccnet_api.get_group(group.id) is None + +@pytest.yield_fixture(scope='function') +def org(): + org = create_and_get_org( + 'testorg测试-{}'.format(randstring(10)), ORG_PREFIX, ORG_CREATOR + ) + try: + yield org + finally: + if ccnet_api.get_org_by_id(org.org_id): + ccnet_api.remove_org(org.org_id) + + + + diff --git a/tests/test_sharing.py b/tests/test_sharing.py index 67b0af22..0d3077cd 100644 --- a/tests/test_sharing.py +++ b/tests/test_sharing.py @@ -14,8 +14,10 @@ def test_share_repo_to_user(repo, permission): api.share_repo(repo.id, USER, USER2, permission) assert api.check_permission(repo.id, USER2) == permission - repos = api.get_share_in_repo_list(USER2, 0, 1) - assert_repo_with_permission(repo, repos, permission) + t_repos_list = 0 + t_repos_list = api.get_share_in_repo_list(USER2, 0, 1) + + assert_repo_with_permission(repo, t_repos_list, permission) api.remove_share(repo.id, USER, USER2) assert api.check_permission(repo.id, USER2) is None @@ -26,8 +28,8 @@ def test_share_repo_to_group(repo, group, permission): assert api.check_permission(repo.id, USER) == 'rw' assert api.check_permission(repo.id, USER2) is None - repos = api.get_repos_by_group(group.id) - assert len(repos) == 0 + t_repos = api.get_repos_by_group(group.id) + assert len(t_repos) == 0 group_list = ccnet_api.get_groups(USER) assert len(group_list) == 1 @@ -35,22 +37,137 @@ def test_share_repo_to_group(repo, group, permission): assert len(group_list) == 0 api.group_share_repo(repo.id, group.id, USER, permission) - repos = api.get_repos_by_group(group.id) - assert_repo_with_permission(repo, repos, permission) + t_repos = api.get_repos_by_group(group.id) + assert_repo_with_permission(repo, t_repos, permission) + + group_list = ccnet_api.get_groups(USER2) + assert len(group_list) == 0 ccnet_api.group_add_member(group.id, USER, USER2) group_list = ccnet_api.get_groups(USER2) assert len(group_list) == 1 - group = group_list[0] - assert group.id == group.id + t_group = group_list[0] + assert t_group.id == group.id repos2 = api.get_repos_by_group(group.id) assert_repo_with_permission(repo, repos2, permission) assert api.check_permission(repo.id, USER2) == permission - api.group_unshare_repo(repo.id, group.id, USER); + api.group_unshare_repo(repo.id, group.id, USER) repos = api.get_repos_by_group(group.id) assert len(repos) == 0 assert api.check_permission(repo.id, USER2) is None + +@pytest.mark.parametrize('permission', ['r', 'rw']) +def test_share_org_repo_to_user(repo, org, permission): + t_org_list = ccnet_api.get_all_orgs(0, 1) + assert len(t_org_list) == 1 + + t_orgs = t_org_list[0] + assert t_orgs.org_id == org.org_id + + t_org_count = ccnet_api.count_orgs() + assert t_org_count == 1 + + t_user_orgs = ccnet_api.get_orgs_by_user(USER) + assert len(t_user_orgs) == 0 + + ccnet_api.add_org_user(org.org_id, USER, False) + t_user_orgs = ccnet_api.get_orgs_by_user(USER) + assert len(t_user_orgs) == 1 + t_user_org = t_user_orgs[0] + assert t_user_org.org_id == org.org_id + + assert api.check_permission(repo.id, USER) == 'rw' + assert api.check_permission(repo.id, USER2) is None + + api.share_repo(repo.id, USER, USER2, permission) + + t_user_orgs = ccnet_api.get_orgs_by_user(USER2) + assert len(t_user_orgs) == 0 + + assert api.check_permission(repo.id, USER2) == permission + + api.remove_share(repo.id, USER, USER2) + assert api.check_permission(repo.id, USER2) is None + + ccnet_api.add_org_user(org.org_id, USER2, False) + t_user_orgs = ccnet_api.get_orgs_by_user(USER2) + assert len(t_user_orgs) == 1 + t_user_org = t_user_orgs[0] + assert t_user_org.org_id == org.org_id + + assert api.check_permission(repo.id, USER2) is None + api.share_repo(repo.id, USER, USER2, permission) + assert api.check_permission(repo.id, USER2) == permission + + api.remove_share(repo.id, USER, USER2) + assert api.check_permission(repo.id, USER2) is None + + ccnet_api.remove_org_user(org.org_id, USER) + orgs = ccnet_api.get_orgs_by_user(USER) + assert len(orgs) ==0 + + ccnet_api.remove_org_user(org.org_id, USER2) + orgs = ccnet_api.get_orgs_by_user(USER2) + assert len(orgs) ==0 + + +@pytest.mark.parametrize('permission', ['r', 'rw']) +def test_share_org_repo_to_group(repo, org, permission): + ccnet_api.add_org_user(org.org_id, USER, False) + + assert api.check_permission(repo.id, USER) == 'rw' + assert api.check_permission(repo.id, USER2) is None + + t_group_list = ccnet_api.get_groups(USER) + assert len(t_group_list) == 0 + + t_org_group_id = ccnet_api.create_org_group(org.org_id, 'org_group_test1', USER) + t_group_list = ccnet_api.get_groups(USER) + assert len(t_group_list) == 1 + + t_group_list = ccnet_api.get_groups(USER2) + assert len(t_group_list) == 0 + + api.group_share_repo(repo.id, t_org_group_id, USER, permission) + t_repos = api.get_repos_by_group(t_org_group_id) + assert_repo_with_permission(repo, t_repos, permission) + + ccnet_api.group_add_member(t_org_group_id, USER, USER2) + t_group_list = ccnet_api.get_groups(USER2) + assert len(t_group_list) == 1 + t_group = t_group_list[0] + assert t_group.id == t_org_group_id + + t_group_repo = api.get_repos_by_group(t_org_group_id) + assert_repo_with_permission(repo, t_group_repo, permission) + + assert api.check_permission(repo.id, USER2) == permission + + api.group_unshare_repo(repo.id, t_org_group_id, USER) + t_repos_list = api.get_repos_by_group(t_org_group_id) + assert len(t_repos_list) == 0 + + assert api.check_permission(repo.id, USER2) is None + + ccnet_api.group_remove_member(t_org_group_id, USER, USER2) + group_list = ccnet_api.get_groups(USER2) + assert len(group_list) == 0 + + ccnet_api.add_org_user(org.org_id, USER2, False) + + group_list = ccnet_api.get_groups(USER2) + assert len(group_list) == 0 + + ccnet_api.group_add_member(t_org_group_id, USER, USER2) + group_list = ccnet_api.get_groups(USER2) + assert len(group_list) == 1 + t_group = t_group_list[0] + assert t_group.id == t_org_group_id + + ccnet_api.group_remove_member(t_org_group_id, USER, USER2) + group_list = ccnet_api.get_groups(USER2) + assert len(group_list) == 0 diff --git a/tests/utils.py b/tests/utils.py index bb3350f8..414252a7 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -6,8 +6,15 @@ def create_and_get_repo(*a, **kw): + repo_id = 0 repo_id = seafile_api.create_repo(*a, **kw) + assert repo_id != 0 + + repo = 0 repo = seafile_api.get_repo(repo_id) + assert repo != 0 + assert repo.id == repo_id + return repo @@ -25,3 +32,8 @@ def assert_repo_with_permission(r1, r2, permission): r2 = r2[0] assert r2.id == r1.id assert r2.permission == permission + +def create_and_get_org(*a, **kw): + org_id = ccnet_api.create_org(*a, **kw) + org = ccnet_api.get_org_by_id(org_id) + return org