2022-03-29 02:29:02 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-29 02:29:02 -04:00
|
|
|
|
2022-06-15 03:02:00 -04:00
|
|
|
package organization_test
|
2022-03-29 02:29:02 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-15 03:02:00 -04:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-03-29 02:29:02 -04:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-03-29 02:29:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUser_IsOwnedBy(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 02:29:02 -04: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},
|
|
|
|
} {
|
2022-08-15 22:22:25 -04:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: testCase.OrgID})
|
2023-10-03 06:30:41 -04:00
|
|
|
isOwner, err := org.IsOwnedBy(db.DefaultContext, testCase.UserID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.Equal(t, testCase.ExpectedOwner, isOwner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_IsOrgMember(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 02:29:02 -04: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},
|
|
|
|
} {
|
2022-08-15 22:22:25 -04:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: testCase.OrgID})
|
2023-10-03 06:30:41 -04:00
|
|
|
isMember, err := org.IsOrgMember(db.DefaultContext, testCase.UserID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.Equal(t, testCase.ExpectedMember, isMember)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetTeam(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2023-02-08 01:44:42 -05:00
|
|
|
team, err := org.GetTeam(db.DefaultContext, "team1")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.Equal(t, org.ID, team.OrgID)
|
|
|
|
assert.Equal(t, "team1", team.LowerName)
|
|
|
|
|
2023-02-08 01:44:42 -05:00
|
|
|
_, err = org.GetTeam(db.DefaultContext, "does not exist")
|
2022-06-15 03:02:00 -04:00
|
|
|
assert.True(t, organization.IsErrTeamNotExist(err))
|
2022-03-29 02:29:02 -04:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
nonOrg := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 2})
|
2023-02-08 01:44:42 -05:00
|
|
|
_, err = nonOrg.GetTeam(db.DefaultContext, "team")
|
2022-06-15 03:02:00 -04:00
|
|
|
assert.True(t, organization.IsErrTeamNotExist(err))
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetOwnerTeam(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2023-02-08 01:44:42 -05:00
|
|
|
team, err := org.GetOwnerTeam(db.DefaultContext)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.Equal(t, org.ID, team.OrgID)
|
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
nonOrg := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 2})
|
2023-02-08 01:44:42 -05:00
|
|
|
_, err = nonOrg.GetOwnerTeam(db.DefaultContext)
|
2022-06-15 03:02:00 -04:00
|
|
|
assert.True(t, organization.IsErrTeamNotExist(err))
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetTeams(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2023-10-03 06:30:41 -04:00
|
|
|
teams, err := org.LoadTeams(db.DefaultContext)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-01-30 05:12:45 -05:00
|
|
|
if assert.Len(t, teams, 5) {
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.Equal(t, int64(1), teams[0].ID)
|
|
|
|
assert.Equal(t, int64(2), teams[1].ID)
|
|
|
|
assert.Equal(t, int64(12), teams[2].ID)
|
2023-01-30 05:12:45 -05:00
|
|
|
assert.Equal(t, int64(14), teams[3].ID)
|
|
|
|
assert.Equal(t, int64(7), teams[4].ID)
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetMembers(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2023-09-14 13:09:32 -04:00
|
|
|
members, _, err := org.GetMembers(db.DefaultContext)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetOrgByName(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 02:29:02 -04:00
|
|
|
|
2023-09-13 22:59:53 -04:00
|
|
|
org, err := organization.GetOrgByName(db.DefaultContext, "org3")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.EqualValues(t, 3, org.ID)
|
2023-09-13 22:59:53 -04:00
|
|
|
assert.Equal(t, "org3", org.Name)
|
2022-03-29 02:29:02 -04:00
|
|
|
|
2023-02-08 01:44:42 -05:00
|
|
|
_, err = organization.GetOrgByName(db.DefaultContext, "user2") // user2 is an individual
|
2022-06-15 03:02:00 -04:00
|
|
|
assert.True(t, organization.IsErrOrgNotExist(err))
|
2022-03-29 02:29:02 -04:00
|
|
|
|
2023-02-08 01:44:42 -05:00
|
|
|
_, err = organization.GetOrgByName(db.DefaultContext, "") // corner case
|
2022-06-15 03:02:00 -04:00
|
|
|
assert.True(t, organization.IsErrOrgNotExist(err))
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCountOrganizations(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-15 03:02:00 -04:00
|
|
|
expected, err := db.GetEngine(db.DefaultContext).Where("type=?", user_model.UserTypeOrganization).Count(&organization.Organization{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-11-23 22:49:41 -05:00
|
|
|
cnt, err := db.Count[organization.Organization](db.DefaultContext, organization.FindOrgOptions{IncludePrivate: true})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-05-20 10:08:52 -04:00
|
|
|
assert.Equal(t, expected, cnt)
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsOrganizationOwner(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 02:29:02 -04:00
|
|
|
test := func(orgID, userID int64, expected bool) {
|
2022-06-15 03:02:00 -04:00
|
|
|
isOwner, err := organization.IsOrganizationOwner(db.DefaultContext, orgID, userID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.EqualValues(t, expected, isOwner)
|
|
|
|
}
|
|
|
|
test(3, 2, true)
|
|
|
|
test(3, 3, false)
|
|
|
|
test(6, 5, true)
|
|
|
|
test(6, 4, false)
|
|
|
|
test(unittest.NonexistentID, unittest.NonexistentID, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsOrganizationMember(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 02:29:02 -04:00
|
|
|
test := func(orgID, userID int64, expected bool) {
|
2022-06-15 03:02:00 -04:00
|
|
|
isMember, err := organization.IsOrganizationMember(db.DefaultContext, orgID, userID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
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)
|
|
|
|
test(unittest.NonexistentID, unittest.NonexistentID, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsPublicMembership(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 02:29:02 -04:00
|
|
|
test := func(orgID, userID int64, expected bool) {
|
2023-10-03 06:30:41 -04:00
|
|
|
isMember, err := organization.IsPublicMembership(db.DefaultContext, orgID, userID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
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)
|
|
|
|
test(unittest.NonexistentID, unittest.NonexistentID, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindOrgs(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 02:29:02 -04:00
|
|
|
|
2023-11-23 22:49:41 -05:00
|
|
|
orgs, err := db.Find[organization.Organization](db.DefaultContext, organization.FindOrgOptions{
|
2022-03-29 02:29:02 -04:00
|
|
|
UserID: 4,
|
|
|
|
IncludePrivate: true,
|
|
|
|
})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
if assert.Len(t, orgs, 1) {
|
|
|
|
assert.EqualValues(t, 3, orgs[0].ID)
|
|
|
|
}
|
|
|
|
|
2023-11-23 22:49:41 -05:00
|
|
|
orgs, err = db.Find[organization.Organization](db.DefaultContext, organization.FindOrgOptions{
|
2022-03-29 02:29:02 -04:00
|
|
|
UserID: 4,
|
|
|
|
IncludePrivate: false,
|
|
|
|
})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, orgs)
|
2022-03-29 02:29:02 -04:00
|
|
|
|
2023-11-23 22:49:41 -05:00
|
|
|
total, err := db.Count[organization.Organization](db.DefaultContext, organization.FindOrgOptions{
|
2022-03-29 02:29:02 -04:00
|
|
|
UserID: 4,
|
|
|
|
IncludePrivate: true,
|
|
|
|
})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.EqualValues(t, 1, total)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetOrgUsersByOrgID(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 02:29:02 -04:00
|
|
|
|
2022-06-15 03:02:00 -04:00
|
|
|
orgUsers, err := organization.GetOrgUsersByOrgID(db.DefaultContext, &organization.FindOrgMembersOpts{
|
2022-03-29 02:29:02 -04:00
|
|
|
ListOptions: db.ListOptions{},
|
|
|
|
OrgID: 3,
|
|
|
|
PublicOnly: false,
|
|
|
|
})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
if assert.Len(t, orgUsers, 3) {
|
2022-06-15 03:02:00 -04:00
|
|
|
assert.Equal(t, organization.OrgUser{
|
2022-03-29 02:29:02 -04:00
|
|
|
ID: orgUsers[0].ID,
|
|
|
|
OrgID: 3,
|
|
|
|
UID: 2,
|
|
|
|
IsPublic: true,
|
|
|
|
}, *orgUsers[0])
|
2022-06-15 03:02:00 -04:00
|
|
|
assert.Equal(t, organization.OrgUser{
|
2022-03-29 02:29:02 -04:00
|
|
|
ID: orgUsers[1].ID,
|
|
|
|
OrgID: 3,
|
|
|
|
UID: 4,
|
|
|
|
IsPublic: false,
|
|
|
|
}, *orgUsers[1])
|
2023-05-24 15:06:04 -04:00
|
|
|
assert.Equal(t, organization.OrgUser{
|
|
|
|
ID: orgUsers[2].ID,
|
|
|
|
OrgID: 3,
|
|
|
|
UID: 28,
|
|
|
|
IsPublic: true,
|
|
|
|
}, *orgUsers[2])
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
2022-06-15 03:02:00 -04:00
|
|
|
orgUsers, err = organization.GetOrgUsersByOrgID(db.DefaultContext, &organization.FindOrgMembersOpts{
|
2022-03-29 02:29:02 -04:00
|
|
|
ListOptions: db.ListOptions{},
|
|
|
|
OrgID: unittest.NonexistentID,
|
|
|
|
PublicOnly: false,
|
|
|
|
})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, orgUsers)
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangeOrgUserStatus(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 02:29:02 -04:00
|
|
|
|
|
|
|
testSuccess := func(orgID, userID int64, public bool) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, organization.ChangeOrgUserStatus(db.DefaultContext, orgID, userID, public))
|
2022-08-15 22:22:25 -04:00
|
|
|
orgUser := unittest.AssertExistsAndLoadBean(t, &organization.OrgUser{OrgID: orgID, UID: userID})
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.Equal(t, public, orgUser.IsPublic)
|
|
|
|
}
|
|
|
|
|
|
|
|
testSuccess(3, 2, false)
|
|
|
|
testSuccess(3, 2, false)
|
|
|
|
testSuccess(3, 4, true)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, organization.ChangeOrgUserStatus(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID, true))
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetUserTeamIDs(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2022-03-29 02:29:02 -04:00
|
|
|
testSuccess := func(userID int64, expected []int64) {
|
2023-10-03 06:30:41 -04:00
|
|
|
teamIDs, err := org.GetUserTeamIDs(db.DefaultContext, userID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.Equal(t, expected, teamIDs)
|
|
|
|
}
|
2023-01-30 05:12:45 -05:00
|
|
|
testSuccess(2, []int64{1, 2, 14})
|
2022-03-29 02:29:02 -04:00
|
|
|
testSuccess(4, []int64{2})
|
|
|
|
testSuccess(unittest.NonexistentID, []int64{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessibleReposEnv_CountRepos(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2022-03-29 02:29:02 -04:00
|
|
|
testSuccess := func(userID, expectedCount int64) {
|
2022-06-15 03:02:00 -04:00
|
|
|
env, err := organization.AccessibleReposEnv(db.DefaultContext, org, userID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
count, err := env.CountRepos()
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.EqualValues(t, expectedCount, count)
|
|
|
|
}
|
|
|
|
testSuccess(2, 3)
|
|
|
|
testSuccess(4, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessibleReposEnv_RepoIDs(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2024-04-29 04:47:56 -04:00
|
|
|
testSuccess := func(userID int64, expectedRepoIDs []int64) {
|
2022-06-15 03:02:00 -04:00
|
|
|
env, err := organization.AccessibleReposEnv(db.DefaultContext, org, userID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
repoIDs, err := env.RepoIDs(1, 100)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.Equal(t, expectedRepoIDs, repoIDs)
|
|
|
|
}
|
2024-10-19 08:11:38 -04:00
|
|
|
testSuccess(2, []int64{32, 5, 3})
|
|
|
|
testSuccess(4, []int64{32, 3})
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessibleReposEnv_Repos(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2022-03-29 02:29:02 -04:00
|
|
|
testSuccess := func(userID int64, expectedRepoIDs []int64) {
|
2022-06-15 03:02:00 -04:00
|
|
|
env, err := organization.AccessibleReposEnv(db.DefaultContext, org, userID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
repos, err := env.Repos(1, 100)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-06-05 03:25:47 -04:00
|
|
|
expectedRepos := make(repo_model.RepositoryList, len(expectedRepoIDs))
|
2022-03-29 02:29:02 -04:00
|
|
|
for i, repoID := range expectedRepoIDs {
|
|
|
|
expectedRepos[i] = unittest.AssertExistsAndLoadBean(t,
|
2022-08-15 22:22:25 -04:00
|
|
|
&repo_model.Repository{ID: repoID})
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
|
|
|
assert.Equal(t, expectedRepos, repos)
|
|
|
|
}
|
2024-10-19 08:11:38 -04:00
|
|
|
testSuccess(2, []int64{32, 5, 3})
|
|
|
|
testSuccess(4, []int64{32, 3})
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessibleReposEnv_MirrorRepos(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2022-03-29 02:29:02 -04:00
|
|
|
testSuccess := func(userID int64, expectedRepoIDs []int64) {
|
2022-06-15 03:02:00 -04:00
|
|
|
env, err := organization.AccessibleReposEnv(db.DefaultContext, org, userID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
repos, err := env.MirrorRepos()
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-06-05 03:25:47 -04:00
|
|
|
expectedRepos := make(repo_model.RepositoryList, len(expectedRepoIDs))
|
2022-03-29 02:29:02 -04:00
|
|
|
for i, repoID := range expectedRepoIDs {
|
|
|
|
expectedRepos[i] = unittest.AssertExistsAndLoadBean(t,
|
2022-08-15 22:22:25 -04:00
|
|
|
&repo_model.Repository{ID: repoID})
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
|
|
|
assert.Equal(t, expectedRepos, repos)
|
|
|
|
}
|
|
|
|
testSuccess(2, []int64{5})
|
|
|
|
testSuccess(4, []int64{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasOrgVisibleTypePublic(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2023-09-13 22:59:53 -04:00
|
|
|
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
2022-03-29 02:29:02 -04:00
|
|
|
|
|
|
|
const newOrgName = "test-org-public"
|
2022-06-15 03:02:00 -04:00
|
|
|
org := &organization.Organization{
|
2022-03-29 02:29:02 -04:00
|
|
|
Name: newOrgName,
|
|
|
|
Visibility: structs.VisibleTypePublic,
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: org.Name, Type: user_model.UserTypeOrganization})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, organization.CreateOrganization(db.DefaultContext, org, owner))
|
2022-03-29 02:29:02 -04:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t,
|
2022-08-15 22:22:25 -04:00
|
|
|
&organization.Organization{Name: org.Name, Type: user_model.UserTypeOrganization})
|
2022-06-15 03:02:00 -04:00
|
|
|
test1 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), owner)
|
2023-09-13 22:59:53 -04:00
|
|
|
test2 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), org3)
|
2022-06-15 03:02:00 -04:00
|
|
|
test3 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), nil)
|
2022-03-29 02:29:02 -04: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasOrgVisibleTypeLimited(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2023-09-13 22:59:53 -04:00
|
|
|
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
2022-03-29 02:29:02 -04:00
|
|
|
|
|
|
|
const newOrgName = "test-org-limited"
|
2022-06-15 03:02:00 -04:00
|
|
|
org := &organization.Organization{
|
2022-03-29 02:29:02 -04:00
|
|
|
Name: newOrgName,
|
|
|
|
Visibility: structs.VisibleTypeLimited,
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: org.Name, Type: user_model.UserTypeOrganization})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, organization.CreateOrganization(db.DefaultContext, org, owner))
|
2022-03-29 02:29:02 -04:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t,
|
2022-08-15 22:22:25 -04:00
|
|
|
&organization.Organization{Name: org.Name, Type: user_model.UserTypeOrganization})
|
2022-06-15 03:02:00 -04:00
|
|
|
test1 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), owner)
|
2023-09-13 22:59:53 -04:00
|
|
|
test2 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), org3)
|
2022-06-15 03:02:00 -04:00
|
|
|
test3 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), nil)
|
2022-03-29 02:29:02 -04: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasOrgVisibleTypePrivate(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2023-09-13 22:59:53 -04:00
|
|
|
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
2022-03-29 02:29:02 -04:00
|
|
|
|
|
|
|
const newOrgName = "test-org-private"
|
2022-06-15 03:02:00 -04:00
|
|
|
org := &organization.Organization{
|
2022-03-29 02:29:02 -04:00
|
|
|
Name: newOrgName,
|
|
|
|
Visibility: structs.VisibleTypePrivate,
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: org.Name, Type: user_model.UserTypeOrganization})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, organization.CreateOrganization(db.DefaultContext, org, owner))
|
2022-03-29 02:29:02 -04:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t,
|
2022-08-15 22:22:25 -04:00
|
|
|
&organization.Organization{Name: org.Name, Type: user_model.UserTypeOrganization})
|
2022-06-15 03:02:00 -04:00
|
|
|
test1 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), owner)
|
2023-09-13 22:59:53 -04:00
|
|
|
test2 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), org3)
|
2022-06-15 03:02:00 -04:00
|
|
|
test3 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), nil)
|
2022-03-29 02:29:02 -04: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUsersWhoCanCreateOrgRepo(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 02:29:02 -04:00
|
|
|
|
2022-06-15 03:02:00 -04:00
|
|
|
users, err := organization.GetUsersWhoCanCreateOrgRepo(db.DefaultContext, 3)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
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})
|
|
|
|
|
2022-06-15 03:02:00 -04:00
|
|
|
users, err = organization.GetUsersWhoCanCreateOrgRepo(db.DefaultContext, 7)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-03-29 02:29:02 -04:00
|
|
|
assert.Len(t, users, 1)
|
2023-01-30 05:12:45 -05:00
|
|
|
assert.NotNil(t, users[5])
|
2022-03-29 02:29:02 -04:00
|
|
|
}
|
2022-06-15 03:02:00 -04:00
|
|
|
|
|
|
|
func TestUser_RemoveOrgRepo(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: org.ID})
|
2022-06-15 03:02:00 -04:00
|
|
|
|
|
|
|
// remove a repo that does belong to org
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamRepo{RepoID: repo.ID, OrgID: org.ID})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, organization.RemoveOrgRepo(db.DefaultContext, org.ID, repo.ID))
|
2022-06-15 03:02:00 -04:00
|
|
|
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
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, organization.RemoveOrgRepo(db.DefaultContext, org.ID, repo.ID))
|
2022-06-15 03:02:00 -04:00
|
|
|
unittest.AssertNotExistsBean(t, &organization.TeamRepo{RepoID: repo.ID, OrgID: org.ID})
|
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, organization.RemoveOrgRepo(db.DefaultContext, org.ID, unittest.NonexistentID))
|
2022-06-15 03:02:00 -04:00
|
|
|
|
|
|
|
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
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-15 03:02:00 -04:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2022-06-15 03:02:00 -04:00
|
|
|
const newOrgName = "neworg"
|
|
|
|
org := &organization.Organization{
|
|
|
|
Name: newOrgName,
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: newOrgName, Type: user_model.UserTypeOrganization})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, organization.CreateOrganization(db.DefaultContext, org, owner))
|
2022-06-15 03:02:00 -04:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t,
|
2022-08-15 22:22:25 -04:00
|
|
|
&organization.Organization{Name: newOrgName, Type: user_model.UserTypeOrganization})
|
2022-06-15 03:02:00 -04:00
|
|
|
ownerTeam := unittest.AssertExistsAndLoadBean(t,
|
2022-08-15 22:22:25 -04:00
|
|
|
&organization.Team{Name: organization.OwnerTeamName, OrgID: org.ID})
|
2022-06-15 03:02:00 -04:00
|
|
|
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
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-15 03:02:00 -04:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
|
2022-06-15 03:02:00 -04:00
|
|
|
const newOrgName = "neworg"
|
|
|
|
org := &organization.Organization{
|
|
|
|
Name: newOrgName,
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.Organization{Name: newOrgName, Type: user_model.UserTypeOrganization})
|
2023-10-03 06:30:41 -04:00
|
|
|
err := organization.CreateOrganization(db.DefaultContext, org, owner)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.Error(t, err)
|
2022-06-15 03:02:00 -04:00
|
|
|
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
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-15 03:02:00 -04:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2023-09-13 22:59:53 -04:00
|
|
|
org := &organization.Organization{Name: "org3"} // should already exist
|
2022-06-15 03:02:00 -04:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: org.Name}) // sanity check
|
2023-10-03 06:30:41 -04:00
|
|
|
err := organization.CreateOrganization(db.DefaultContext, org, owner)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.Error(t, err)
|
2022-06-15 03:02:00 -04:00
|
|
|
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
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-15 03:02:00 -04:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2023-10-03 06:30:41 -04:00
|
|
|
err := organization.CreateOrganization(db.DefaultContext, &organization.Organization{Name: "assets"}, owner)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.Error(t, err)
|
2022-06-15 03:02:00 -04:00
|
|
|
assert.True(t, db.IsErrNameReserved(err))
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Organization{}, &organization.Team{})
|
|
|
|
}
|