2022-03-29 09:29:02 +03:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-29 09:29:02 +03:00
|
|
|
|
2022-06-15 10:02:00 +03:00
|
|
|
package organization_test
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
import (
|
2024-10-11 15:48:47 +03:00
|
|
|
"path/filepath"
|
2022-03-29 09:29:02 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-15 10:02:00 +03:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2024-10-11 15:48:47 +03:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-03-29 09:29:02 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2024-10-11 15:48:47 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 22:41:10 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-03-29 09:29:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTeam_IsOwnerTeam(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
|
2022-08-16 05:22:25 +03:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 1})
|
2022-03-29 09:29:02 +03:00
|
|
|
assert.True(t, team.IsOwnerTeam())
|
|
|
|
|
2022-08-16 05:22:25 +03:00
|
|
|
team = unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2})
|
2022-03-29 09:29:02 +03:00
|
|
|
assert.False(t, team.IsOwnerTeam())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTeam_IsMember(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
|
2022-08-16 05:22:25 +03:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 1})
|
2023-10-03 13:30:41 +03:00
|
|
|
assert.True(t, team.IsMember(db.DefaultContext, 2))
|
|
|
|
assert.False(t, team.IsMember(db.DefaultContext, 4))
|
|
|
|
assert.False(t, team.IsMember(db.DefaultContext, unittest.NonexistentID))
|
2022-03-29 09:29:02 +03:00
|
|
|
|
2022-08-16 05:22:25 +03:00
|
|
|
team = unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2})
|
2023-10-03 13:30:41 +03:00
|
|
|
assert.True(t, team.IsMember(db.DefaultContext, 2))
|
|
|
|
assert.True(t, team.IsMember(db.DefaultContext, 4))
|
|
|
|
assert.False(t, team.IsMember(db.DefaultContext, unittest.NonexistentID))
|
2022-03-29 09:29:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTeam_GetRepositories(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
test := func(teamID int64) {
|
2022-08-16 05:22:25 +03:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, team.LoadRepositories(db.DefaultContext))
|
2022-03-29 09:29:02 +03:00
|
|
|
assert.Len(t, team.Repos, team.NumRepos)
|
|
|
|
for _, repo := range team.Repos {
|
2022-06-15 10:02:00 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamRepo{TeamID: teamID, RepoID: repo.ID})
|
2022-03-29 09:29:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
test(1)
|
|
|
|
test(3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTeam_GetMembers(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
test := func(teamID int64) {
|
2022-08-16 05:22:25 +03:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, team.LoadMembers(db.DefaultContext))
|
2022-03-29 09:29:02 +03:00
|
|
|
assert.Len(t, team.Members, team.NumMembers)
|
|
|
|
for _, member := range team.Members {
|
2022-06-15 10:02:00 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{UID: member.ID, TeamID: teamID})
|
2022-03-29 09:29:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
test(1)
|
|
|
|
test(3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTeam(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
testSuccess := func(orgID int64, name string) {
|
2022-06-15 10:02:00 +03:00
|
|
|
team, err := organization.GetTeam(db.DefaultContext, orgID, name)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-03-29 09:29:02 +03:00
|
|
|
assert.EqualValues(t, orgID, team.OrgID)
|
|
|
|
assert.Equal(t, name, team.Name)
|
|
|
|
}
|
|
|
|
testSuccess(3, "Owners")
|
|
|
|
testSuccess(3, "team1")
|
|
|
|
|
2022-06-15 10:02:00 +03:00
|
|
|
_, err := organization.GetTeam(db.DefaultContext, 3, "nonexistent")
|
2024-07-30 22:41:10 +03:00
|
|
|
require.Error(t, err)
|
2022-06-15 10:02:00 +03:00
|
|
|
_, err = organization.GetTeam(db.DefaultContext, unittest.NonexistentID, "Owners")
|
2024-07-30 22:41:10 +03:00
|
|
|
require.Error(t, err)
|
2022-03-29 09:29:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTeamByID(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
testSuccess := func(teamID int64) {
|
2022-06-15 10:02:00 +03:00
|
|
|
team, err := organization.GetTeamByID(db.DefaultContext, teamID)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-03-29 09:29:02 +03:00
|
|
|
assert.EqualValues(t, teamID, team.ID)
|
|
|
|
}
|
|
|
|
testSuccess(1)
|
|
|
|
testSuccess(2)
|
|
|
|
testSuccess(3)
|
|
|
|
testSuccess(4)
|
|
|
|
|
2022-06-15 10:02:00 +03:00
|
|
|
_, err := organization.GetTeamByID(db.DefaultContext, unittest.NonexistentID)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.Error(t, err)
|
2022-03-29 09:29:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsTeamMember(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
test := func(orgID, teamID, userID int64, expected bool) {
|
2022-06-15 10:02:00 +03:00
|
|
|
isMember, err := organization.IsTeamMember(db.DefaultContext, orgID, teamID, userID)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-03-29 09:29:02 +03:00
|
|
|
assert.Equal(t, expected, isMember)
|
|
|
|
}
|
|
|
|
|
|
|
|
test(3, 1, 2, true)
|
|
|
|
test(3, 1, 4, false)
|
|
|
|
test(3, 1, unittest.NonexistentID, false)
|
|
|
|
|
|
|
|
test(3, 2, 2, true)
|
|
|
|
test(3, 2, 4, true)
|
|
|
|
|
|
|
|
test(3, unittest.NonexistentID, unittest.NonexistentID, false)
|
|
|
|
test(unittest.NonexistentID, unittest.NonexistentID, unittest.NonexistentID, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTeamMembers(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
test := func(teamID int64) {
|
2022-08-16 05:22:25 +03:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
2022-06-15 10:02:00 +03:00
|
|
|
members, err := organization.GetTeamMembers(db.DefaultContext, &organization.SearchMembersOptions{
|
2022-03-29 09:29:02 +03:00
|
|
|
TeamID: teamID,
|
|
|
|
})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-03-29 09:29:02 +03:00
|
|
|
assert.Len(t, members, team.NumMembers)
|
|
|
|
for _, member := range members {
|
2022-06-15 10:02:00 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{UID: member.ID, TeamID: teamID})
|
2022-03-29 09:29:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
test(1)
|
|
|
|
test(3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUserTeams(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
test := func(userID int64) {
|
2023-09-25 16:17:37 +03:00
|
|
|
teams, _, err := organization.SearchTeam(db.DefaultContext, &organization.SearchTeamOptions{UserID: userID})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-03-29 09:29:02 +03:00
|
|
|
for _, team := range teams {
|
2022-06-15 10:02:00 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{TeamID: team.ID, UID: userID})
|
2022-03-29 09:29:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
test(2)
|
|
|
|
test(5)
|
|
|
|
test(unittest.NonexistentID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUserOrgTeams(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
test := func(orgID, userID int64) {
|
2022-06-15 10:02:00 +03:00
|
|
|
teams, err := organization.GetUserOrgTeams(db.DefaultContext, orgID, userID)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-03-29 09:29:02 +03:00
|
|
|
for _, team := range teams {
|
|
|
|
assert.EqualValues(t, orgID, team.OrgID)
|
2022-06-15 10:02:00 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{TeamID: team.ID, UID: userID})
|
2022-03-29 09:29:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
test(3, 2)
|
|
|
|
test(3, 4)
|
|
|
|
test(3, unittest.NonexistentID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasTeamRepo(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
test := func(teamID, repoID int64, expected bool) {
|
2022-08-16 05:22:25 +03:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
2022-06-15 10:02:00 +03:00
|
|
|
assert.Equal(t, expected, organization.HasTeamRepo(db.DefaultContext, team.OrgID, teamID, repoID))
|
2022-03-29 09:29:02 +03:00
|
|
|
}
|
|
|
|
test(1, 1, false)
|
|
|
|
test(1, 3, true)
|
|
|
|
test(1, 5, true)
|
|
|
|
test(1, unittest.NonexistentID, false)
|
|
|
|
|
|
|
|
test(2, 3, true)
|
|
|
|
test(2, 5, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUsersInTeamsCount(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
test := func(teamIDs, userIDs []int64, expected int64) {
|
2023-10-03 13:30:41 +03:00
|
|
|
count, err := organization.UsersInTeamsCount(db.DefaultContext, teamIDs, userIDs)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-03-29 09:29:02 +03:00
|
|
|
assert.Equal(t, expected, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
test([]int64{2}, []int64{1, 2, 3, 4}, 1) // only userid 2
|
|
|
|
test([]int64{1, 2, 3, 4, 5}, []int64{2, 5}, 2) // userid 2,4
|
|
|
|
test([]int64{1, 2, 3, 4, 5}, []int64{2, 3, 5}, 3) // userid 2,4,5
|
|
|
|
}
|
2024-10-11 15:48:47 +03:00
|
|
|
|
|
|
|
func TestInconsistentOwnerTeam(t *testing.T) {
|
|
|
|
defer unittest.OverrideFixtures(
|
|
|
|
unittest.FixturesOptions{
|
|
|
|
Dir: filepath.Join(setting.AppWorkPath, "models/fixtures/"),
|
|
|
|
Base: setting.AppWorkPath,
|
|
|
|
Dirs: []string{"models/organization/TestInconsistentOwnerTeam/"},
|
|
|
|
},
|
|
|
|
)()
|
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1000, TeamID: 1000, AccessMode: perm.AccessModeNone})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1001, TeamID: 1000, AccessMode: perm.AccessModeNone})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1002, TeamID: 1000, AccessMode: perm.AccessModeNone})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1003, TeamID: 1000, AccessMode: perm.AccessModeNone})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1004, TeamID: 1000, AccessMode: perm.AccessModeNone})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1005, TeamID: 1000, AccessMode: perm.AccessModeNone})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1006, TeamID: 1000, AccessMode: perm.AccessModeNone})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1007, TeamID: 1000, AccessMode: perm.AccessModeNone})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1008, TeamID: 1000, AccessMode: perm.AccessModeNone})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1009, TeamID: 1000, AccessMode: perm.AccessModeNone})
|
|
|
|
|
|
|
|
count, err := organization.CountInconsistentOwnerTeams(db.DefaultContext)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 1, count)
|
|
|
|
|
|
|
|
count, err = organization.FixInconsistentOwnerTeams(db.DefaultContext)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 1, count)
|
|
|
|
|
|
|
|
count, err = organization.CountInconsistentOwnerTeams(db.DefaultContext)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 0, count)
|
|
|
|
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1000, AccessMode: perm.AccessModeOwner})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1001, AccessMode: perm.AccessModeOwner})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1002, AccessMode: perm.AccessModeOwner})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1003, AccessMode: perm.AccessModeOwner})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1004, AccessMode: perm.AccessModeOwner})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1007, AccessMode: perm.AccessModeOwner})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1008, AccessMode: perm.AccessModeOwner})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1009, AccessMode: perm.AccessModeOwner})
|
|
|
|
|
|
|
|
// External wiki and issue
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1005, AccessMode: perm.AccessModeRead})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUnit{ID: 1006, AccessMode: perm.AccessModeRead})
|
|
|
|
}
|