|
|
|
@ -2,12 +2,13 @@ |
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package organization |
|
|
|
|
package organization_test |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db" |
|
|
|
|
"code.gitea.io/gitea/models/organization" |
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo" |
|
|
|
|
"code.gitea.io/gitea/models/unittest" |
|
|
|
|
user_model "code.gitea.io/gitea/models/user" |
|
|
|
@ -30,7 +31,7 @@ func TestUser_IsOwnedBy(t *testing.T) { |
|
|
|
|
{2, 2, false}, // user2 is not an organization
|
|
|
|
|
{2, 3, false}, |
|
|
|
|
} { |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: testCase.OrgID}).(*Organization) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: testCase.OrgID}).(*organization.Organization) |
|
|
|
|
isOwner, err := org.IsOwnedBy(testCase.UserID) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Equal(t, testCase.ExpectedOwner, isOwner) |
|
|
|
@ -51,7 +52,7 @@ func TestUser_IsOrgMember(t *testing.T) { |
|
|
|
|
{2, 2, false}, // user2 is not an organization
|
|
|
|
|
{2, 3, false}, |
|
|
|
|
} { |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: testCase.OrgID}).(*Organization) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: testCase.OrgID}).(*organization.Organization) |
|
|
|
|
isMember, err := org.IsOrgMember(testCase.UserID) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Equal(t, testCase.ExpectedMember, isMember) |
|
|
|
@ -60,35 +61,35 @@ func TestUser_IsOrgMember(t *testing.T) { |
|
|
|
|
|
|
|
|
|
func TestUser_GetTeam(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization) |
|
|
|
|
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") |
|
|
|
|
assert.True(t, IsErrTeamNotExist(err)) |
|
|
|
|
assert.True(t, organization.IsErrTeamNotExist(err)) |
|
|
|
|
|
|
|
|
|
nonOrg := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 2}).(*Organization) |
|
|
|
|
nonOrg := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 2}).(*organization.Organization) |
|
|
|
|
_, err = nonOrg.GetTeam("team") |
|
|
|
|
assert.True(t, IsErrTeamNotExist(err)) |
|
|
|
|
assert.True(t, organization.IsErrTeamNotExist(err)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestUser_GetOwnerTeam(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization) |
|
|
|
|
team, err := org.GetOwnerTeam() |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Equal(t, org.ID, team.OrgID) |
|
|
|
|
|
|
|
|
|
nonOrg := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 2}).(*Organization) |
|
|
|
|
nonOrg := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 2}).(*organization.Organization) |
|
|
|
|
_, err = nonOrg.GetOwnerTeam() |
|
|
|
|
assert.True(t, IsErrTeamNotExist(err)) |
|
|
|
|
assert.True(t, organization.IsErrTeamNotExist(err)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestUser_GetTeams(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization) |
|
|
|
|
teams, err := org.LoadTeams() |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
if assert.Len(t, teams, 4) { |
|
|
|
@ -101,7 +102,7 @@ func TestUser_GetTeams(t *testing.T) { |
|
|
|
|
|
|
|
|
|
func TestUser_GetMembers(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization) |
|
|
|
|
members, _, err := org.GetMembers() |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
if assert.Len(t, members, 3) { |
|
|
|
@ -114,23 +115,23 @@ func TestUser_GetMembers(t *testing.T) { |
|
|
|
|
func TestGetOrgByName(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
|
|
|
|
|
org, err := GetOrgByName("user3") |
|
|
|
|
org, err := organization.GetOrgByName("user3") |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.EqualValues(t, 3, org.ID) |
|
|
|
|
assert.Equal(t, "user3", org.Name) |
|
|
|
|
|
|
|
|
|
_, err = GetOrgByName("user2") // user2 is an individual
|
|
|
|
|
assert.True(t, IsErrOrgNotExist(err)) |
|
|
|
|
_, err = organization.GetOrgByName("user2") // user2 is an individual
|
|
|
|
|
assert.True(t, organization.IsErrOrgNotExist(err)) |
|
|
|
|
|
|
|
|
|
_, err = GetOrgByName("") // corner case
|
|
|
|
|
assert.True(t, IsErrOrgNotExist(err)) |
|
|
|
|
_, err = organization.GetOrgByName("") // corner case
|
|
|
|
|
assert.True(t, organization.IsErrOrgNotExist(err)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCountOrganizations(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
expected, err := db.GetEngine(db.DefaultContext).Where("type=?", user_model.UserTypeOrganization).Count(&Organization{}) |
|
|
|
|
expected, err := db.GetEngine(db.DefaultContext).Where("type=?", user_model.UserTypeOrganization).Count(&organization.Organization{}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
cnt, err := CountOrgs(FindOrgOptions{IncludePrivate: true}) |
|
|
|
|
cnt, err := organization.CountOrgs(organization.FindOrgOptions{IncludePrivate: true}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Equal(t, expected, cnt) |
|
|
|
|
} |
|
|
|
@ -138,7 +139,7 @@ func TestCountOrganizations(t *testing.T) { |
|
|
|
|
func TestIsOrganizationOwner(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
test := func(orgID, userID int64, expected bool) { |
|
|
|
|
isOwner, err := IsOrganizationOwner(db.DefaultContext, orgID, userID) |
|
|
|
|
isOwner, err := organization.IsOrganizationOwner(db.DefaultContext, orgID, userID) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.EqualValues(t, expected, isOwner) |
|
|
|
|
} |
|
|
|
@ -152,7 +153,7 @@ func TestIsOrganizationOwner(t *testing.T) { |
|
|
|
|
func TestIsOrganizationMember(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
test := func(orgID, userID int64, expected bool) { |
|
|
|
|
isMember, err := IsOrganizationMember(db.DefaultContext, orgID, userID) |
|
|
|
|
isMember, err := organization.IsOrganizationMember(db.DefaultContext, orgID, userID) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.EqualValues(t, expected, isMember) |
|
|
|
|
} |
|
|
|
@ -167,7 +168,7 @@ func TestIsOrganizationMember(t *testing.T) { |
|
|
|
|
func TestIsPublicMembership(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
test := func(orgID, userID int64, expected bool) { |
|
|
|
|
isMember, err := IsPublicMembership(orgID, userID) |
|
|
|
|
isMember, err := organization.IsPublicMembership(orgID, userID) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.EqualValues(t, expected, isMember) |
|
|
|
|
} |
|
|
|
@ -182,7 +183,7 @@ func TestIsPublicMembership(t *testing.T) { |
|
|
|
|
func TestFindOrgs(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
|
|
|
|
|
orgs, err := FindOrgs(FindOrgOptions{ |
|
|
|
|
orgs, err := organization.FindOrgs(organization.FindOrgOptions{ |
|
|
|
|
UserID: 4, |
|
|
|
|
IncludePrivate: true, |
|
|
|
|
}) |
|
|
|
@ -191,14 +192,14 @@ func TestFindOrgs(t *testing.T) { |
|
|
|
|
assert.EqualValues(t, 3, orgs[0].ID) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
orgs, err = FindOrgs(FindOrgOptions{ |
|
|
|
|
orgs, err = organization.FindOrgs(organization.FindOrgOptions{ |
|
|
|
|
UserID: 4, |
|
|
|
|
IncludePrivate: false, |
|
|
|
|
}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Len(t, orgs, 0) |
|
|
|
|
|
|
|
|
|
total, err := CountOrgs(FindOrgOptions{ |
|
|
|
|
total, err := organization.CountOrgs(organization.FindOrgOptions{ |
|
|
|
|
UserID: 4, |
|
|
|
|
IncludePrivate: true, |
|
|
|
|
}) |
|
|
|
@ -209,16 +210,16 @@ func TestFindOrgs(t *testing.T) { |
|
|
|
|
func TestGetOrgUsersByUserID(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
|
|
|
|
|
orgUsers, err := GetOrgUsersByUserID(5, &SearchOrganizationsOptions{All: true}) |
|
|
|
|
orgUsers, err := organization.GetOrgUsersByUserID(5, &organization.SearchOrganizationsOptions{All: true}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
if assert.Len(t, orgUsers, 2) { |
|
|
|
|
assert.Equal(t, OrgUser{ |
|
|
|
|
assert.Equal(t, organization.OrgUser{ |
|
|
|
|
ID: orgUsers[0].ID, |
|
|
|
|
OrgID: 6, |
|
|
|
|
UID: 5, |
|
|
|
|
IsPublic: true, |
|
|
|
|
}, *orgUsers[0]) |
|
|
|
|
assert.Equal(t, OrgUser{ |
|
|
|
|
assert.Equal(t, organization.OrgUser{ |
|
|
|
|
ID: orgUsers[1].ID, |
|
|
|
|
OrgID: 7, |
|
|
|
|
UID: 5, |
|
|
|
@ -226,12 +227,12 @@ func TestGetOrgUsersByUserID(t *testing.T) { |
|
|
|
|
}, *orgUsers[1]) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
publicOrgUsers, err := GetOrgUsersByUserID(5, &SearchOrganizationsOptions{All: false}) |
|
|
|
|
publicOrgUsers, err := organization.GetOrgUsersByUserID(5, &organization.SearchOrganizationsOptions{All: false}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Len(t, publicOrgUsers, 1) |
|
|
|
|
assert.Equal(t, *orgUsers[0], *publicOrgUsers[0]) |
|
|
|
|
|
|
|
|
|
orgUsers, err = GetOrgUsersByUserID(1, &SearchOrganizationsOptions{All: true}) |
|
|
|
|
orgUsers, err = organization.GetOrgUsersByUserID(1, &organization.SearchOrganizationsOptions{All: true}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Len(t, orgUsers, 0) |
|
|
|
|
} |
|
|
|
@ -239,20 +240,20 @@ func TestGetOrgUsersByUserID(t *testing.T) { |
|
|
|
|
func TestGetOrgUsersByOrgID(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
|
|
|
|
|
orgUsers, err := GetOrgUsersByOrgID(db.DefaultContext, &FindOrgMembersOpts{ |
|
|
|
|
orgUsers, err := organization.GetOrgUsersByOrgID(db.DefaultContext, &organization.FindOrgMembersOpts{ |
|
|
|
|
ListOptions: db.ListOptions{}, |
|
|
|
|
OrgID: 3, |
|
|
|
|
PublicOnly: false, |
|
|
|
|
}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
if assert.Len(t, orgUsers, 3) { |
|
|
|
|
assert.Equal(t, OrgUser{ |
|
|
|
|
assert.Equal(t, organization.OrgUser{ |
|
|
|
|
ID: orgUsers[0].ID, |
|
|
|
|
OrgID: 3, |
|
|
|
|
UID: 2, |
|
|
|
|
IsPublic: true, |
|
|
|
|
}, *orgUsers[0]) |
|
|
|
|
assert.Equal(t, OrgUser{ |
|
|
|
|
assert.Equal(t, organization.OrgUser{ |
|
|
|
|
ID: orgUsers[1].ID, |
|
|
|
|
OrgID: 3, |
|
|
|
|
UID: 4, |
|
|
|
@ -260,7 +261,7 @@ func TestGetOrgUsersByOrgID(t *testing.T) { |
|
|
|
|
}, *orgUsers[1]) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
orgUsers, err = GetOrgUsersByOrgID(db.DefaultContext, &FindOrgMembersOpts{ |
|
|
|
|
orgUsers, err = organization.GetOrgUsersByOrgID(db.DefaultContext, &organization.FindOrgMembersOpts{ |
|
|
|
|
ListOptions: db.ListOptions{}, |
|
|
|
|
OrgID: unittest.NonexistentID, |
|
|
|
|
PublicOnly: false, |
|
|
|
@ -273,20 +274,20 @@ func TestChangeOrgUserStatus(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
|
|
|
|
|
testSuccess := func(orgID, userID int64, public bool) { |
|
|
|
|
assert.NoError(t, ChangeOrgUserStatus(orgID, userID, public)) |
|
|
|
|
orgUser := unittest.AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID}).(*OrgUser) |
|
|
|
|
assert.NoError(t, organization.ChangeOrgUserStatus(orgID, userID, public)) |
|
|
|
|
orgUser := unittest.AssertExistsAndLoadBean(t, &organization.OrgUser{OrgID: orgID, UID: userID}).(*organization.OrgUser) |
|
|
|
|
assert.Equal(t, public, orgUser.IsPublic) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
testSuccess(3, 2, false) |
|
|
|
|
testSuccess(3, 2, false) |
|
|
|
|
testSuccess(3, 4, true) |
|
|
|
|
assert.NoError(t, ChangeOrgUserStatus(unittest.NonexistentID, unittest.NonexistentID, true)) |
|
|
|
|
assert.NoError(t, organization.ChangeOrgUserStatus(unittest.NonexistentID, unittest.NonexistentID, true)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestUser_GetUserTeamIDs(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization) |
|
|
|
|
testSuccess := func(userID int64, expected []int64) { |
|
|
|
|
teamIDs, err := org.GetUserTeamIDs(userID) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
@ -299,9 +300,9 @@ func TestUser_GetUserTeamIDs(t *testing.T) { |
|
|
|
|
|
|
|
|
|
func TestAccessibleReposEnv_CountRepos(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization) |
|
|
|
|
testSuccess := func(userID, expectedCount int64) { |
|
|
|
|
env, err := AccessibleReposEnv(db.DefaultContext, org, userID) |
|
|
|
|
env, err := organization.AccessibleReposEnv(db.DefaultContext, org, userID) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
count, err := env.CountRepos() |
|
|
|
|
assert.NoError(t, err) |
|
|
|
@ -313,9 +314,9 @@ func TestAccessibleReposEnv_CountRepos(t *testing.T) { |
|
|
|
|
|
|
|
|
|
func TestAccessibleReposEnv_RepoIDs(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization) |
|
|
|
|
testSuccess := func(userID, _, pageSize int64, expectedRepoIDs []int64) { |
|
|
|
|
env, err := AccessibleReposEnv(db.DefaultContext, org, userID) |
|
|
|
|
env, err := organization.AccessibleReposEnv(db.DefaultContext, org, userID) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
repoIDs, err := env.RepoIDs(1, 100) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
@ -327,9 +328,9 @@ func TestAccessibleReposEnv_RepoIDs(t *testing.T) { |
|
|
|
|
|
|
|
|
|
func TestAccessibleReposEnv_Repos(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization) |
|
|
|
|
testSuccess := func(userID int64, expectedRepoIDs []int64) { |
|
|
|
|
env, err := AccessibleReposEnv(db.DefaultContext, org, userID) |
|
|
|
|
env, err := organization.AccessibleReposEnv(db.DefaultContext, org, userID) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
repos, err := env.Repos(1, 100) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
@ -346,9 +347,9 @@ func TestAccessibleReposEnv_Repos(t *testing.T) { |
|
|
|
|
|
|
|
|
|
func TestAccessibleReposEnv_MirrorRepos(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization) |
|
|
|
|
testSuccess := func(userID int64, expectedRepoIDs []int64) { |
|
|
|
|
env, err := AccessibleReposEnv(db.DefaultContext, org, userID) |
|
|
|
|
env, err := organization.AccessibleReposEnv(db.DefaultContext, org, userID) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
repos, err := env.MirrorRepos() |
|
|
|
|
assert.NoError(t, err) |
|
|
|
@ -369,18 +370,18 @@ func TestHasOrgVisibleTypePublic(t *testing.T) { |
|
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User) |
|
|
|
|
|
|
|
|
|
const newOrgName = "test-org-public" |
|
|
|
|
org := &Organization{ |
|
|
|
|
org := &organization.Organization{ |
|
|
|
|
Name: newOrgName, |
|
|
|
|
Visibility: structs.VisibleTypePublic, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: org.Name, Type: user_model.UserTypeOrganization}) |
|
|
|
|
assert.NoError(t, CreateOrganization(org, owner)) |
|
|
|
|
assert.NoError(t, organization.CreateOrganization(org, owner)) |
|
|
|
|
org = unittest.AssertExistsAndLoadBean(t, |
|
|
|
|
&Organization{Name: org.Name, Type: user_model.UserTypeOrganization}).(*Organization) |
|
|
|
|
test1 := HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), owner) |
|
|
|
|
test2 := HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), user3) |
|
|
|
|
test3 := HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), nil) |
|
|
|
|
&organization.Organization{Name: org.Name, Type: user_model.UserTypeOrganization}).(*organization.Organization) |
|
|
|
|
test1 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), owner) |
|
|
|
|
test2 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), user3) |
|
|
|
|
test3 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), nil) |
|
|
|
|
assert.True(t, test1) // owner of org
|
|
|
|
|
assert.True(t, test2) // user not a part of org
|
|
|
|
|
assert.True(t, test3) // logged out user
|
|
|
|
@ -392,18 +393,18 @@ func TestHasOrgVisibleTypeLimited(t *testing.T) { |
|
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User) |
|
|
|
|
|
|
|
|
|
const newOrgName = "test-org-limited" |
|
|
|
|
org := &Organization{ |
|
|
|
|
org := &organization.Organization{ |
|
|
|
|
Name: newOrgName, |
|
|
|
|
Visibility: structs.VisibleTypeLimited, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: org.Name, Type: user_model.UserTypeOrganization}) |
|
|
|
|
assert.NoError(t, CreateOrganization(org, owner)) |
|
|
|
|
assert.NoError(t, organization.CreateOrganization(org, owner)) |
|
|
|
|
org = unittest.AssertExistsAndLoadBean(t, |
|
|
|
|
&Organization{Name: org.Name, Type: user_model.UserTypeOrganization}).(*Organization) |
|
|
|
|
test1 := HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), owner) |
|
|
|
|
test2 := HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), user3) |
|
|
|
|
test3 := HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), nil) |
|
|
|
|
&organization.Organization{Name: org.Name, Type: user_model.UserTypeOrganization}).(*organization.Organization) |
|
|
|
|
test1 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), owner) |
|
|
|
|
test2 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), user3) |
|
|
|
|
test3 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), nil) |
|
|
|
|
assert.True(t, test1) // owner of org
|
|
|
|
|
assert.True(t, test2) // user not a part of org
|
|
|
|
|
assert.False(t, test3) // logged out user
|
|
|
|
@ -415,18 +416,18 @@ func TestHasOrgVisibleTypePrivate(t *testing.T) { |
|
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User) |
|
|
|
|
|
|
|
|
|
const newOrgName = "test-org-private" |
|
|
|
|
org := &Organization{ |
|
|
|
|
org := &organization.Organization{ |
|
|
|
|
Name: newOrgName, |
|
|
|
|
Visibility: structs.VisibleTypePrivate, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: org.Name, Type: user_model.UserTypeOrganization}) |
|
|
|
|
assert.NoError(t, CreateOrganization(org, owner)) |
|
|
|
|
assert.NoError(t, organization.CreateOrganization(org, owner)) |
|
|
|
|
org = unittest.AssertExistsAndLoadBean(t, |
|
|
|
|
&Organization{Name: org.Name, Type: user_model.UserTypeOrganization}).(*Organization) |
|
|
|
|
test1 := HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), owner) |
|
|
|
|
test2 := HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), user3) |
|
|
|
|
test3 := HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), nil) |
|
|
|
|
&organization.Organization{Name: org.Name, Type: user_model.UserTypeOrganization}).(*organization.Organization) |
|
|
|
|
test1 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), owner) |
|
|
|
|
test2 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), user3) |
|
|
|
|
test3 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), nil) |
|
|
|
|
assert.True(t, test1) // owner of org
|
|
|
|
|
assert.False(t, test2) // user not a part of org
|
|
|
|
|
assert.False(t, test3) // logged out user
|
|
|
|
@ -435,7 +436,7 @@ func TestHasOrgVisibleTypePrivate(t *testing.T) { |
|
|
|
|
func TestGetUsersWhoCanCreateOrgRepo(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
|
|
|
|
|
users, err := GetUsersWhoCanCreateOrgRepo(db.DefaultContext, 3) |
|
|
|
|
users, err := organization.GetUsersWhoCanCreateOrgRepo(db.DefaultContext, 3) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Len(t, users, 2) |
|
|
|
|
var ids []int64 |
|
|
|
@ -444,8 +445,93 @@ func TestGetUsersWhoCanCreateOrgRepo(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
assert.ElementsMatch(t, ids, []int64{2, 28}) |
|
|
|
|
|
|
|
|
|
users, err = GetUsersWhoCanCreateOrgRepo(db.DefaultContext, 7) |
|
|
|
|
users, err = organization.GetUsersWhoCanCreateOrgRepo(db.DefaultContext, 7) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Len(t, users, 1) |
|
|
|
|
assert.EqualValues(t, 5, users[0].ID) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestUser_RemoveOrgRepo(t *testing.T) { |
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization) |
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: org.ID}).(*repo_model.Repository) |
|
|
|
|
|
|
|
|
|
// remove a repo that does belong to org
|
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamRepo{RepoID: repo.ID, OrgID: org.ID}) |
|
|
|
|
assert.NoError(t, organization.RemoveOrgRepo(db.DefaultContext, org.ID, repo.ID)) |
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.TeamRepo{RepoID: repo.ID, OrgID: org.ID}) |
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo.ID}) // repo should still exist
|
|
|
|
|
|
|
|
|
|
// remove a repo that does not belong to org
|
|
|
|
|
assert.NoError(t, organization.RemoveOrgRepo(db.DefaultContext, org.ID, repo.ID)) |
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.TeamRepo{RepoID: repo.ID, OrgID: org.ID}) |
|
|
|
|
|
|
|
|
|
assert.NoError(t, organization.RemoveOrgRepo(db.DefaultContext, org.ID, unittest.NonexistentID)) |
|
|
|
|
|
|
|
|
|
unittest.CheckConsistencyFor(t, |
|
|
|
|
&user_model.User{ID: org.ID}, |
|
|
|
|
&organization.Team{OrgID: org.ID}, |
|
|
|
|
&repo_model.Repository{ID: repo.ID}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCreateOrganization(t *testing.T) { |
|
|
|
|
// successful creation of org
|
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
|
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) |
|
|
|
|
const newOrgName = "neworg" |
|
|
|
|
org := &organization.Organization{ |
|
|
|
|
Name: newOrgName, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: newOrgName, Type: user_model.UserTypeOrganization}) |
|
|
|
|
assert.NoError(t, organization.CreateOrganization(org, owner)) |
|
|
|
|
org = unittest.AssertExistsAndLoadBean(t, |
|
|
|
|
&organization.Organization{Name: newOrgName, Type: user_model.UserTypeOrganization}).(*organization.Organization) |
|
|
|
|
ownerTeam := unittest.AssertExistsAndLoadBean(t, |
|
|
|
|
&organization.Team{Name: organization.OwnerTeamName, OrgID: org.ID}).(*organization.Team) |
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{UID: owner.ID, TeamID: ownerTeam.ID}) |
|
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCreateOrganization2(t *testing.T) { |
|
|
|
|
// unauthorized creation of org
|
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
|
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User) |
|
|
|
|
const newOrgName = "neworg" |
|
|
|
|
org := &organization.Organization{ |
|
|
|
|
Name: newOrgName, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.Organization{Name: newOrgName, Type: user_model.UserTypeOrganization}) |
|
|
|
|
err := organization.CreateOrganization(org, owner) |
|
|
|
|
assert.Error(t, err) |
|
|
|
|
assert.True(t, organization.IsErrUserNotAllowedCreateOrg(err)) |
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.Organization{Name: newOrgName, Type: user_model.UserTypeOrganization}) |
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Organization{}, &organization.Team{}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCreateOrganization3(t *testing.T) { |
|
|
|
|
// create org with same name as existent org
|
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
|
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) |
|
|
|
|
org := &organization.Organization{Name: "user3"} // should already exist
|
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: org.Name}) // sanity check
|
|
|
|
|
err := organization.CreateOrganization(org, owner) |
|
|
|
|
assert.Error(t, err) |
|
|
|
|
assert.True(t, user_model.IsErrUserAlreadyExist(err)) |
|
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCreateOrganization4(t *testing.T) { |
|
|
|
|
// create org with unusable name
|
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
|
|
|
|
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) |
|
|
|
|
err := organization.CreateOrganization(&organization.Organization{Name: "assets"}, owner) |
|
|
|
|
assert.Error(t, err) |
|
|
|
|
assert.True(t, db.IsErrNameReserved(err)) |
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Organization{}, &organization.Team{}) |
|
|
|
|
} |
|
|
|
|