2017-01-24 19:16:36 +03:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 14:49:59 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 04:27:50 +03:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 17:36:47 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 12:49:20 +03:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-08-24 15:28:59 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-02-18 19:00:27 +03:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
|
2017-01-24 19:16:36 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUser_IsOwnedBy(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-21 10:43:26 +03:00
|
|
|
for _, testCase := range []struct {
|
|
|
|
OrgID int64
|
|
|
|
UserID int64
|
|
|
|
ExpectedOwner bool
|
|
|
|
}{
|
|
|
|
{3, 2, true},
|
|
|
|
{3, 1, false},
|
|
|
|
{3, 3, false},
|
|
|
|
{3, 4, false},
|
|
|
|
{2, 2, false}, // user2 is not an organization
|
|
|
|
{2, 3, false},
|
|
|
|
} {
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: testCase.OrgID}).(*Organization)
|
2017-12-21 10:43:26 +03:00
|
|
|
isOwner, err := org.IsOwnedBy(testCase.UserID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testCase.ExpectedOwner, isOwner)
|
|
|
|
}
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_IsOrgMember(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-21 10:43:26 +03:00
|
|
|
for _, testCase := range []struct {
|
|
|
|
OrgID int64
|
|
|
|
UserID int64
|
|
|
|
ExpectedMember bool
|
|
|
|
}{
|
|
|
|
{3, 2, true},
|
|
|
|
{3, 4, true},
|
|
|
|
{3, 1, false},
|
|
|
|
{3, 3, false},
|
|
|
|
{2, 2, false}, // user2 is not an organization
|
|
|
|
{2, 3, false},
|
|
|
|
} {
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: testCase.OrgID}).(*Organization)
|
2017-12-21 10:43:26 +03:00
|
|
|
isMember, err := org.IsOrgMember(testCase.UserID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testCase.ExpectedMember, isMember)
|
|
|
|
}
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetTeam(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
team, err := org.GetTeam("team1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, org.ID, team.OrgID)
|
|
|
|
assert.Equal(t, "team1", team.LowerName)
|
|
|
|
|
|
|
|
_, err = org.GetTeam("does not exist")
|
2019-09-23 23:08:03 +03:00
|
|
|
assert.True(t, IsErrTeamNotExist(err))
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2021-11-19 14:41:40 +03:00
|
|
|
nonOrg := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 2}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
_, err = nonOrg.GetTeam("team")
|
2019-09-23 23:08:03 +03:00
|
|
|
assert.True(t, IsErrTeamNotExist(err))
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetOwnerTeam(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
team, err := org.GetOwnerTeam()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, org.ID, team.OrgID)
|
|
|
|
|
2021-11-19 14:41:40 +03:00
|
|
|
nonOrg := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 2}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
_, err = nonOrg.GetOwnerTeam()
|
2019-09-23 23:08:03 +03:00
|
|
|
assert.True(t, IsErrTeamNotExist(err))
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetTeams(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
|
|
|
teams, err := org.LoadTeams()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, teams, 4) {
|
|
|
|
assert.Equal(t, int64(1), teams[0].ID)
|
|
|
|
assert.Equal(t, int64(2), teams[1].ID)
|
|
|
|
assert.Equal(t, int64(12), teams[2].ID)
|
|
|
|
assert.Equal(t, int64(7), teams[3].ID)
|
2017-08-28 12:17:45 +03:00
|
|
|
}
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetMembers(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
|
|
|
members, _, err := org.GetMembers()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, members, 3) {
|
|
|
|
assert.Equal(t, int64(2), members[0].ID)
|
|
|
|
assert.Equal(t, int64(28), members[1].ID)
|
|
|
|
assert.Equal(t, int64(4), members[2].ID)
|
2017-08-28 12:17:45 +03:00
|
|
|
}
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_AddMember(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
|
|
|
|
// add a user that is not a member
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
|
2017-01-24 19:16:36 +03:00
|
|
|
prevNumMembers := org.NumMembers
|
|
|
|
assert.NoError(t, org.AddMember(5))
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 5, OrgID: 3})
|
2021-11-19 14:41:40 +03:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.Equal(t, prevNumMembers+1, org.NumMembers)
|
|
|
|
|
|
|
|
// add a user that is already a member
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
|
2017-01-24 19:16:36 +03:00
|
|
|
prevNumMembers = org.NumMembers
|
|
|
|
assert.NoError(t, org.AddMember(4))
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
|
2021-11-19 14:41:40 +03:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.Equal(t, prevNumMembers, org.NumMembers)
|
2017-02-07 14:47:55 +03:00
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{})
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_RemoveMember(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
|
|
|
|
// remove a user that is a member
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
|
2017-01-24 19:16:36 +03:00
|
|
|
prevNumMembers := org.NumMembers
|
|
|
|
assert.NoError(t, org.RemoveMember(4))
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &OrgUser{UID: 4, OrgID: 3})
|
2021-11-19 14:41:40 +03:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.Equal(t, prevNumMembers-1, org.NumMembers)
|
|
|
|
|
|
|
|
// remove a user that is not a member
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
|
2017-01-24 19:16:36 +03:00
|
|
|
prevNumMembers = org.NumMembers
|
|
|
|
assert.NoError(t, org.RemoveMember(5))
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
|
2021-11-19 14:41:40 +03:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.Equal(t, prevNumMembers, org.NumMembers)
|
2017-02-07 14:47:55 +03:00
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &Team{})
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_RemoveOrgRepo(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2021-12-10 04:27:50 +03:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: org.ID}).(*repo_model.Repository)
|
2017-01-24 19:16:36 +03:00
|
|
|
|
|
|
|
// remove a repo that does belong to org
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.NoError(t, org.RemoveOrgRepo(repo.ID))
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
|
2021-12-10 04:27:50 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo.ID}) // repo should still exist
|
2017-01-24 19:16:36 +03:00
|
|
|
|
|
|
|
// remove a repo that does not belong to org
|
|
|
|
assert.NoError(t, org.RemoveOrgRepo(repo.ID))
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
assert.NoError(t, org.RemoveOrgRepo(unittest.NonexistentID))
|
2017-02-07 14:47:55 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.CheckConsistencyFor(t,
|
2021-11-24 12:49:20 +03:00
|
|
|
&user_model.User{ID: org.ID},
|
2017-02-07 14:47:55 +03:00
|
|
|
&Team{OrgID: org.ID},
|
2021-12-10 04:27:50 +03:00
|
|
|
&repo_model.Repository{ID: repo.ID})
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateOrganization(t *testing.T) {
|
|
|
|
// successful creation of org
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2017-01-24 19:16:36 +03:00
|
|
|
const newOrgName = "neworg"
|
2021-11-19 14:41:40 +03:00
|
|
|
org := &Organization{
|
2017-01-24 19:16:36 +03:00
|
|
|
Name: newOrgName,
|
|
|
|
}
|
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: newOrgName, Type: user_model.UserTypeOrganization})
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.NoError(t, CreateOrganization(org, owner))
|
2021-11-16 11:53:21 +03:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t,
|
2021-11-24 12:49:20 +03:00
|
|
|
&Organization{Name: newOrgName, Type: user_model.UserTypeOrganization}).(*Organization)
|
2021-11-16 11:53:21 +03:00
|
|
|
ownerTeam := unittest.AssertExistsAndLoadBean(t,
|
2017-01-24 19:16:36 +03:00
|
|
|
&Team{Name: ownerTeamName, OrgID: org.ID}).(*Team)
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: owner.ID, TeamID: ownerTeam.ID})
|
2021-11-24 12:49:20 +03:00
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &Team{})
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateOrganization2(t *testing.T) {
|
|
|
|
// unauthorized creation of org
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
2017-01-24 19:16:36 +03:00
|
|
|
const newOrgName = "neworg"
|
2021-11-19 14:41:40 +03:00
|
|
|
org := &Organization{
|
2017-01-24 19:16:36 +03:00
|
|
|
Name: newOrgName,
|
|
|
|
}
|
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &Organization{Name: newOrgName, Type: user_model.UserTypeOrganization})
|
2017-01-24 19:16:36 +03:00
|
|
|
err := CreateOrganization(org, owner)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrUserNotAllowedCreateOrg(err))
|
2021-11-24 12:49:20 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &Organization{Name: newOrgName, Type: user_model.UserTypeOrganization})
|
2021-11-19 14:41:40 +03:00
|
|
|
unittest.CheckConsistencyFor(t, &Organization{}, &Team{})
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateOrganization3(t *testing.T) {
|
|
|
|
// create org with same name as existent org
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
|
|
org := &Organization{Name: "user3"} // should already exist
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: org.Name}) // sanity check
|
2017-01-24 19:16:36 +03:00
|
|
|
err := CreateOrganization(org, owner)
|
|
|
|
assert.Error(t, err)
|
2021-11-24 12:49:20 +03:00
|
|
|
assert.True(t, user_model.IsErrUserAlreadyExist(err))
|
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &Team{})
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateOrganization4(t *testing.T) {
|
|
|
|
// create org with unusable name
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2021-11-19 14:41:40 +03:00
|
|
|
err := CreateOrganization(&Organization{Name: "assets"}, owner)
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.Error(t, err)
|
2021-11-24 12:49:20 +03:00
|
|
|
assert.True(t, db.IsErrNameReserved(err))
|
2021-11-19 14:41:40 +03:00
|
|
|
unittest.CheckConsistencyFor(t, &Organization{}, &Team{})
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetOrgByName(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-24 19:16:36 +03:00
|
|
|
|
|
|
|
org, err := GetOrgByName("user3")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 3, org.ID)
|
|
|
|
assert.Equal(t, "user3", org.Name)
|
|
|
|
|
2019-06-12 22:41:28 +03:00
|
|
|
_, err = GetOrgByName("user2") // user2 is an individual
|
2017-07-06 16:30:19 +03:00
|
|
|
assert.True(t, IsErrOrgNotExist(err))
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2019-06-12 22:41:28 +03:00
|
|
|
_, err = GetOrgByName("") // corner case
|
2017-07-06 16:30:19 +03:00
|
|
|
assert.True(t, IsErrOrgNotExist(err))
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCountOrganizations(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-24 12:49:20 +03:00
|
|
|
expected, err := db.GetEngine(db.DefaultContext).Where("type=?", user_model.UserTypeOrganization).Count(&user_model.User{})
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, CountOrganizations())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsOrganizationOwner(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-21 10:43:26 +03:00
|
|
|
test := func(orgID, userID int64, expected bool) {
|
|
|
|
isOwner, err := IsOrganizationOwner(orgID, userID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, expected, isOwner)
|
|
|
|
}
|
|
|
|
test(3, 2, true)
|
|
|
|
test(3, 3, false)
|
|
|
|
test(6, 5, true)
|
|
|
|
test(6, 4, false)
|
2021-11-16 11:53:21 +03:00
|
|
|
test(unittest.NonexistentID, unittest.NonexistentID, false)
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsOrganizationMember(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-21 10:43:26 +03:00
|
|
|
test := func(orgID, userID int64, expected bool) {
|
|
|
|
isMember, err := IsOrganizationMember(orgID, userID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, expected, isMember)
|
|
|
|
}
|
|
|
|
test(3, 2, true)
|
|
|
|
test(3, 3, false)
|
|
|
|
test(3, 4, true)
|
|
|
|
test(6, 5, true)
|
|
|
|
test(6, 4, false)
|
2021-11-16 11:53:21 +03:00
|
|
|
test(unittest.NonexistentID, unittest.NonexistentID, false)
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsPublicMembership(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-12-21 10:43:26 +03:00
|
|
|
test := func(orgID, userID int64, expected bool) {
|
|
|
|
isMember, err := IsPublicMembership(orgID, userID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, expected, isMember)
|
|
|
|
}
|
|
|
|
test(3, 2, true)
|
|
|
|
test(3, 3, false)
|
|
|
|
test(3, 4, false)
|
|
|
|
test(6, 5, true)
|
|
|
|
test(6, 4, false)
|
2021-11-16 11:53:21 +03:00
|
|
|
test(unittest.NonexistentID, unittest.NonexistentID, false)
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
2021-11-22 16:51:45 +03:00
|
|
|
func TestFindOrgs(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2021-11-22 16:51:45 +03:00
|
|
|
orgs, err := FindOrgs(FindOrgOptions{
|
|
|
|
UserID: 4,
|
|
|
|
IncludePrivate: true,
|
|
|
|
})
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.NoError(t, err)
|
2017-08-28 12:17:45 +03:00
|
|
|
if assert.Len(t, orgs, 1) {
|
|
|
|
assert.EqualValues(t, 3, orgs[0].ID)
|
|
|
|
}
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2021-11-22 16:51:45 +03:00
|
|
|
orgs, err = FindOrgs(FindOrgOptions{
|
|
|
|
UserID: 4,
|
|
|
|
IncludePrivate: false,
|
|
|
|
})
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, orgs, 0)
|
2021-11-22 16:51:45 +03:00
|
|
|
|
|
|
|
total, err := CountOrgs(FindOrgOptions{
|
|
|
|
UserID: 4,
|
|
|
|
IncludePrivate: true,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 1, total)
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetOwnedOrgsByUserID(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-24 19:16:36 +03:00
|
|
|
|
|
|
|
orgs, err := GetOwnedOrgsByUserID(2)
|
|
|
|
assert.NoError(t, err)
|
2017-08-28 12:17:45 +03:00
|
|
|
if assert.Len(t, orgs, 1) {
|
|
|
|
assert.EqualValues(t, 3, orgs[0].ID)
|
|
|
|
}
|
2017-01-24 19:16:36 +03:00
|
|
|
|
|
|
|
orgs, err = GetOwnedOrgsByUserID(4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, orgs, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetOwnedOrgsByUserIDDesc(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-24 19:16:36 +03:00
|
|
|
|
|
|
|
orgs, err := GetOwnedOrgsByUserIDDesc(5, "id")
|
|
|
|
assert.NoError(t, err)
|
2017-08-28 12:17:45 +03:00
|
|
|
if assert.Len(t, orgs, 2) {
|
|
|
|
assert.EqualValues(t, 7, orgs[0].ID)
|
|
|
|
assert.EqualValues(t, 6, orgs[1].ID)
|
|
|
|
}
|
2017-01-24 19:16:36 +03:00
|
|
|
|
|
|
|
orgs, err = GetOwnedOrgsByUserIDDesc(4, "id")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, orgs, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetOrgUsersByUserID(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2020-01-24 22:00:29 +03:00
|
|
|
orgUsers, err := GetOrgUsersByUserID(5, &SearchOrganizationsOptions{All: true})
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.NoError(t, err)
|
2017-08-28 12:17:45 +03:00
|
|
|
if assert.Len(t, orgUsers, 2) {
|
|
|
|
assert.Equal(t, OrgUser{
|
|
|
|
ID: orgUsers[0].ID,
|
|
|
|
OrgID: 6,
|
|
|
|
UID: 5,
|
2021-03-14 21:52:12 +03:00
|
|
|
IsPublic: true,
|
|
|
|
}, *orgUsers[0])
|
2017-08-28 12:17:45 +03:00
|
|
|
assert.Equal(t, OrgUser{
|
|
|
|
ID: orgUsers[1].ID,
|
|
|
|
OrgID: 7,
|
|
|
|
UID: 5,
|
2021-03-14 21:52:12 +03:00
|
|
|
IsPublic: false,
|
|
|
|
}, *orgUsers[1])
|
2017-08-28 12:17:45 +03:00
|
|
|
}
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2020-01-24 22:00:29 +03:00
|
|
|
publicOrgUsers, err := GetOrgUsersByUserID(5, &SearchOrganizationsOptions{All: false})
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, publicOrgUsers, 1)
|
|
|
|
assert.Equal(t, *orgUsers[0], *publicOrgUsers[0])
|
|
|
|
|
2020-01-24 22:00:29 +03:00
|
|
|
orgUsers, err = GetOrgUsersByUserID(1, &SearchOrganizationsOptions{All: true})
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, orgUsers, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetOrgUsersByOrgID(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2020-01-24 22:00:29 +03:00
|
|
|
orgUsers, err := GetOrgUsersByOrgID(&FindOrgMembersOpts{
|
2021-09-24 14:32:56 +03:00
|
|
|
ListOptions: db.ListOptions{},
|
2020-01-24 22:00:29 +03:00
|
|
|
OrgID: 3,
|
|
|
|
PublicOnly: false,
|
|
|
|
})
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.NoError(t, err)
|
2019-11-20 14:27:49 +03:00
|
|
|
if assert.Len(t, orgUsers, 3) {
|
2017-08-28 12:17:45 +03:00
|
|
|
assert.Equal(t, OrgUser{
|
|
|
|
ID: orgUsers[0].ID,
|
|
|
|
OrgID: 3,
|
|
|
|
UID: 2,
|
2021-03-14 21:52:12 +03:00
|
|
|
IsPublic: true,
|
|
|
|
}, *orgUsers[0])
|
2017-08-28 12:17:45 +03:00
|
|
|
assert.Equal(t, OrgUser{
|
|
|
|
ID: orgUsers[1].ID,
|
|
|
|
OrgID: 3,
|
|
|
|
UID: 4,
|
2021-03-14 21:52:12 +03:00
|
|
|
IsPublic: false,
|
|
|
|
}, *orgUsers[1])
|
2017-08-28 12:17:45 +03:00
|
|
|
}
|
2017-01-24 19:16:36 +03:00
|
|
|
|
2020-01-24 22:00:29 +03:00
|
|
|
orgUsers, err = GetOrgUsersByOrgID(&FindOrgMembersOpts{
|
2021-09-24 14:32:56 +03:00
|
|
|
ListOptions: db.ListOptions{},
|
2021-11-16 11:53:21 +03:00
|
|
|
OrgID: unittest.NonexistentID,
|
2020-01-24 22:00:29 +03:00
|
|
|
PublicOnly: false,
|
|
|
|
})
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, orgUsers, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangeOrgUserStatus(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-24 19:16:36 +03:00
|
|
|
|
|
|
|
testSuccess := func(orgID, userID int64, public bool) {
|
|
|
|
assert.NoError(t, ChangeOrgUserStatus(orgID, userID, public))
|
2021-11-16 11:53:21 +03:00
|
|
|
orgUser := unittest.AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID}).(*OrgUser)
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.Equal(t, public, orgUser.IsPublic)
|
|
|
|
}
|
|
|
|
|
|
|
|
testSuccess(3, 2, false)
|
|
|
|
testSuccess(3, 2, false)
|
|
|
|
testSuccess(3, 4, true)
|
2021-11-16 11:53:21 +03:00
|
|
|
assert.NoError(t, ChangeOrgUserStatus(unittest.NonexistentID, unittest.NonexistentID, true))
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddOrgUser(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-08-24 15:28:59 +03:00
|
|
|
testSuccess := func(orgID, userID int64, isPublic bool) {
|
2021-11-24 12:49:20 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID}).(*user_model.User)
|
2017-01-24 19:16:36 +03:00
|
|
|
expectedNumMembers := org.NumMembers
|
2021-11-16 11:53:21 +03:00
|
|
|
if !unittest.BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
|
2017-01-24 19:16:36 +03:00
|
|
|
expectedNumMembers++
|
|
|
|
}
|
|
|
|
assert.NoError(t, AddOrgUser(orgID, userID))
|
2019-08-24 15:28:59 +03:00
|
|
|
ou := &OrgUser{OrgID: orgID, UID: userID}
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, ou)
|
2021-06-07 08:27:09 +03:00
|
|
|
assert.Equal(t, isPublic, ou.IsPublic)
|
2021-11-24 12:49:20 +03:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID}).(*user_model.User)
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.EqualValues(t, expectedNumMembers, org.NumMembers)
|
|
|
|
}
|
2019-08-24 15:28:59 +03:00
|
|
|
|
|
|
|
setting.Service.DefaultOrgMemberVisible = false
|
|
|
|
testSuccess(3, 5, false)
|
|
|
|
testSuccess(3, 5, false)
|
|
|
|
testSuccess(6, 2, false)
|
|
|
|
|
|
|
|
setting.Service.DefaultOrgMemberVisible = true
|
|
|
|
testSuccess(6, 3, true)
|
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &Team{})
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveOrgUser(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-24 19:16:36 +03:00
|
|
|
testSuccess := func(orgID, userID int64) {
|
2021-11-24 12:49:20 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID}).(*user_model.User)
|
2017-01-24 19:16:36 +03:00
|
|
|
expectedNumMembers := org.NumMembers
|
2021-11-16 11:53:21 +03:00
|
|
|
if unittest.BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
|
2017-01-24 19:16:36 +03:00
|
|
|
expectedNumMembers--
|
|
|
|
}
|
|
|
|
assert.NoError(t, RemoveOrgUser(orgID, userID))
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &OrgUser{OrgID: orgID, UID: userID})
|
2021-11-24 12:49:20 +03:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID}).(*user_model.User)
|
2017-01-24 19:16:36 +03:00
|
|
|
assert.EqualValues(t, expectedNumMembers, org.NumMembers)
|
|
|
|
}
|
|
|
|
testSuccess(3, 4)
|
|
|
|
testSuccess(3, 4)
|
|
|
|
|
|
|
|
err := RemoveOrgUser(7, 5)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrLastOrgOwner(err))
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &OrgUser{OrgID: 7, UID: 5})
|
2021-11-24 12:49:20 +03:00
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &Team{})
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetUserTeamIDs(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
testSuccess := func(userID int64, expected []int64) {
|
|
|
|
teamIDs, err := org.GetUserTeamIDs(userID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, teamIDs)
|
|
|
|
}
|
|
|
|
testSuccess(2, []int64{1, 2})
|
|
|
|
testSuccess(4, []int64{2})
|
2021-11-16 11:53:21 +03:00
|
|
|
testSuccess(unittest.NonexistentID, []int64{})
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessibleReposEnv_CountRepos(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
testSuccess := func(userID, expectedCount int64) {
|
|
|
|
env, err := org.AccessibleReposEnv(userID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
count, err := env.CountRepos()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, expectedCount, count)
|
|
|
|
}
|
2018-06-21 19:00:13 +03:00
|
|
|
testSuccess(2, 3)
|
|
|
|
testSuccess(4, 2)
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessibleReposEnv_RepoIDs(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2019-06-12 22:41:28 +03:00
|
|
|
testSuccess := func(userID, _, pageSize int64, expectedRepoIDs []int64) {
|
2017-01-24 19:16:36 +03:00
|
|
|
env, err := org.AccessibleReposEnv(userID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
repoIDs, err := env.RepoIDs(1, 100)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedRepoIDs, repoIDs)
|
|
|
|
}
|
2018-06-21 19:00:13 +03:00
|
|
|
testSuccess(2, 1, 100, []int64{3, 5, 32})
|
|
|
|
testSuccess(4, 0, 100, []int64{3, 32})
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessibleReposEnv_Repos(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
testSuccess := func(userID int64, expectedRepoIDs []int64) {
|
|
|
|
env, err := org.AccessibleReposEnv(userID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
repos, err := env.Repos(1, 100)
|
|
|
|
assert.NoError(t, err)
|
2021-12-10 04:27:50 +03:00
|
|
|
expectedRepos := make([]*repo_model.Repository, len(expectedRepoIDs))
|
2017-01-24 19:16:36 +03:00
|
|
|
for i, repoID := range expectedRepoIDs {
|
2021-11-16 11:53:21 +03:00
|
|
|
expectedRepos[i] = unittest.AssertExistsAndLoadBean(t,
|
2021-12-10 04:27:50 +03:00
|
|
|
&repo_model.Repository{ID: repoID}).(*repo_model.Repository)
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
assert.Equal(t, expectedRepos, repos)
|
|
|
|
}
|
2018-06-21 19:00:13 +03:00
|
|
|
testSuccess(2, []int64{3, 5, 32})
|
|
|
|
testSuccess(4, []int64{3, 32})
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessibleReposEnv_MirrorRepos(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-19 14:41:40 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization)
|
2017-01-24 19:16:36 +03:00
|
|
|
testSuccess := func(userID int64, expectedRepoIDs []int64) {
|
|
|
|
env, err := org.AccessibleReposEnv(userID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
repos, err := env.MirrorRepos()
|
|
|
|
assert.NoError(t, err)
|
2021-12-10 04:27:50 +03:00
|
|
|
expectedRepos := make([]*repo_model.Repository, len(expectedRepoIDs))
|
2017-01-24 19:16:36 +03:00
|
|
|
for i, repoID := range expectedRepoIDs {
|
2021-11-16 11:53:21 +03:00
|
|
|
expectedRepos[i] = unittest.AssertExistsAndLoadBean(t,
|
2021-12-10 04:27:50 +03:00
|
|
|
&repo_model.Repository{ID: repoID}).(*repo_model.Repository)
|
2017-01-24 19:16:36 +03:00
|
|
|
}
|
|
|
|
assert.Equal(t, expectedRepos, repos)
|
|
|
|
}
|
|
|
|
testSuccess(2, []int64{5})
|
|
|
|
testSuccess(4, []int64{})
|
|
|
|
}
|
2019-02-18 19:00:27 +03:00
|
|
|
|
|
|
|
func TestHasOrgVisibleTypePublic(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-24 12:49:20 +03:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User)
|
2019-02-18 19:00:27 +03:00
|
|
|
|
|
|
|
const newOrgName = "test-org-public"
|
2021-11-19 14:41:40 +03:00
|
|
|
org := &Organization{
|
2019-02-18 19:00:27 +03:00
|
|
|
Name: newOrgName,
|
|
|
|
Visibility: structs.VisibleTypePublic,
|
|
|
|
}
|
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: org.Name, Type: user_model.UserTypeOrganization})
|
2019-02-18 19:00:27 +03:00
|
|
|
assert.NoError(t, CreateOrganization(org, owner))
|
2021-11-16 11:53:21 +03:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t,
|
2021-11-24 12:49:20 +03:00
|
|
|
&Organization{Name: org.Name, Type: user_model.UserTypeOrganization}).(*Organization)
|
2021-11-19 14:41:40 +03:00
|
|
|
test1 := HasOrgOrUserVisible(org.AsUser(), owner)
|
|
|
|
test2 := HasOrgOrUserVisible(org.AsUser(), user3)
|
|
|
|
test3 := HasOrgOrUserVisible(org.AsUser(), nil)
|
2021-06-07 08:27:09 +03:00
|
|
|
assert.True(t, test1) // owner of org
|
|
|
|
assert.True(t, test2) // user not a part of org
|
|
|
|
assert.True(t, test3) // logged out user
|
2019-02-18 19:00:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasOrgVisibleTypeLimited(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-24 12:49:20 +03:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User)
|
2019-02-18 19:00:27 +03:00
|
|
|
|
|
|
|
const newOrgName = "test-org-limited"
|
2021-11-19 14:41:40 +03:00
|
|
|
org := &Organization{
|
2019-02-18 19:00:27 +03:00
|
|
|
Name: newOrgName,
|
|
|
|
Visibility: structs.VisibleTypeLimited,
|
|
|
|
}
|
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: org.Name, Type: user_model.UserTypeOrganization})
|
2019-02-18 19:00:27 +03:00
|
|
|
assert.NoError(t, CreateOrganization(org, owner))
|
2021-11-16 11:53:21 +03:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t,
|
2021-11-24 12:49:20 +03:00
|
|
|
&Organization{Name: org.Name, Type: user_model.UserTypeOrganization}).(*Organization)
|
2021-11-19 14:41:40 +03:00
|
|
|
test1 := HasOrgOrUserVisible(org.AsUser(), owner)
|
|
|
|
test2 := HasOrgOrUserVisible(org.AsUser(), user3)
|
|
|
|
test3 := HasOrgOrUserVisible(org.AsUser(), nil)
|
2021-06-07 08:27:09 +03:00
|
|
|
assert.True(t, test1) // owner of org
|
|
|
|
assert.True(t, test2) // user not a part of org
|
|
|
|
assert.False(t, test3) // logged out user
|
2019-02-18 19:00:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasOrgVisibleTypePrivate(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-24 12:49:20 +03:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User)
|
2019-02-18 19:00:27 +03:00
|
|
|
|
|
|
|
const newOrgName = "test-org-private"
|
2021-11-19 14:41:40 +03:00
|
|
|
org := &Organization{
|
2019-02-18 19:00:27 +03:00
|
|
|
Name: newOrgName,
|
|
|
|
Visibility: structs.VisibleTypePrivate,
|
|
|
|
}
|
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: org.Name, Type: user_model.UserTypeOrganization})
|
2019-02-18 19:00:27 +03:00
|
|
|
assert.NoError(t, CreateOrganization(org, owner))
|
2021-11-16 11:53:21 +03:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t,
|
2021-11-24 12:49:20 +03:00
|
|
|
&Organization{Name: org.Name, Type: user_model.UserTypeOrganization}).(*Organization)
|
2021-11-19 14:41:40 +03:00
|
|
|
test1 := HasOrgOrUserVisible(org.AsUser(), owner)
|
|
|
|
test2 := HasOrgOrUserVisible(org.AsUser(), user3)
|
|
|
|
test3 := HasOrgOrUserVisible(org.AsUser(), nil)
|
2021-06-07 08:27:09 +03:00
|
|
|
assert.True(t, test1) // owner of org
|
|
|
|
assert.False(t, test2) // user not a part of org
|
|
|
|
assert.False(t, test3) // logged out user
|
2019-02-18 19:00:27 +03:00
|
|
|
}
|
2021-03-01 03:47:30 +03:00
|
|
|
|
|
|
|
func TestGetUsersWhoCanCreateOrgRepo(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-03-01 03:47:30 +03:00
|
|
|
|
|
|
|
users, err := GetUsersWhoCanCreateOrgRepo(3)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, users, 2)
|
|
|
|
var ids []int64
|
|
|
|
for i := range users {
|
|
|
|
ids = append(ids, users[i].ID)
|
|
|
|
}
|
|
|
|
assert.ElementsMatch(t, ids, []int64{2, 28})
|
|
|
|
|
|
|
|
users, err = GetUsersWhoCanCreateOrgRepo(7)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, users, 1)
|
|
|
|
assert.EqualValues(t, 5, users[0].ID)
|
|
|
|
}
|