aboutsummaryrefslogtreecommitdiffstats
path: root/models/org_team_test.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-03-29 14:29:02 +0800
committerGitHub <noreply@github.com>2022-03-29 14:29:02 +0800
commitb06b9a056c0af751e576978f6ef3c914ee959b9c (patch)
treeaa0d11413038baa5d47af65fd435665c698fe456 /models/org_team_test.go
parentd4c789dfc1c341413b77a2f21fe7339982102bed (diff)
downloadgitea-b06b9a056c0af751e576978f6ef3c914ee959b9c.tar.gz
gitea-b06b9a056c0af751e576978f6ef3c914ee959b9c.zip
Move organization related structs into sub package (#18518)
* Move organization related structs into sub package * Fix test * Fix lint * Move more functions into sub packages * Fix bug * Fix test * Update models/organization/team_repo.go Co-authored-by: KN4CK3R <admin@oldschoolhack.me> * Apply suggestions from code review Co-authored-by: KN4CK3R <admin@oldschoolhack.me> * Fix fmt * Follow suggestion from @Gusted * Fix test * Fix test * Fix bug * Use ctx but db.DefaultContext on routers * Fix bug * Fix bug * fix bug * Update models/organization/team_user.go * Fix bug Co-authored-by: KN4CK3R <admin@oldschoolhack.me> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'models/org_team_test.go')
-rw-r--r--models/org_team_test.go278
1 files changed, 48 insertions, 230 deletions
diff --git a/models/org_team_test.go b/models/org_team_test.go
index cf3a797991..e125f3c65b 100644
--- a/models/org_team_test.go
+++ b/models/org_team_test.go
@@ -9,6 +9,7 @@ import (
"testing"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
@@ -17,68 +18,14 @@ import (
"github.com/stretchr/testify/assert"
)
-func TestTeam_IsOwnerTeam(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
- assert.True(t, team.IsOwnerTeam())
-
- team = unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team)
- assert.False(t, team.IsOwnerTeam())
-}
-
-func TestTeam_IsMember(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
- assert.True(t, team.IsMember(2))
- assert.False(t, team.IsMember(4))
- assert.False(t, team.IsMember(unittest.NonexistentID))
-
- team = unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team)
- assert.True(t, team.IsMember(2))
- assert.True(t, team.IsMember(4))
- assert.False(t, team.IsMember(unittest.NonexistentID))
-}
-
-func TestTeam_GetRepositories(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- test := func(teamID int64) {
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
- assert.NoError(t, team.GetRepositories(&SearchOrgTeamOptions{}))
- assert.Len(t, team.Repos, team.NumRepos)
- for _, repo := range team.Repos {
- unittest.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repo.ID})
- }
- }
- test(1)
- test(3)
-}
-
-func TestTeam_GetMembers(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- test := func(teamID int64) {
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
- assert.NoError(t, team.GetMembers(&SearchMembersOptions{}))
- assert.Len(t, team.Members, team.NumMembers)
- for _, member := range team.Members {
- unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID})
- }
- }
- test(1)
- test(3)
-}
-
func TestTeam_AddMember(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
test := func(teamID, userID int64) {
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
- assert.NoError(t, team.AddMember(userID))
- unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID})
- unittest.CheckConsistencyFor(t, &Team{ID: teamID}, &user_model.User{ID: team.OrgID})
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
+ assert.NoError(t, AddTeamMember(team, userID))
+ unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{UID: userID, TeamID: teamID})
+ unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID}, &user_model.User{ID: team.OrgID})
}
test(1, 2)
test(1, 4)
@@ -89,27 +36,27 @@ func TestTeam_RemoveMember(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
testSuccess := func(teamID, userID int64) {
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
- assert.NoError(t, team.RemoveMember(userID))
- unittest.AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID})
- unittest.CheckConsistencyFor(t, &Team{ID: teamID})
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
+ assert.NoError(t, RemoveTeamMember(team, userID))
+ unittest.AssertNotExistsBean(t, &organization.TeamUser{UID: userID, TeamID: teamID})
+ unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID})
}
testSuccess(1, 4)
testSuccess(2, 2)
testSuccess(3, 2)
testSuccess(3, unittest.NonexistentID)
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
- err := team.RemoveMember(2)
- assert.True(t, IsErrLastOrgOwner(err))
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 1}).(*organization.Team)
+ err := RemoveTeamMember(team, 2)
+ assert.True(t, organization.IsErrLastOrgOwner(err))
}
func TestTeam_HasRepository(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
test := func(teamID, repoID int64, expected bool) {
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
- assert.Equal(t, expected, team.HasRepository(repoID))
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
+ assert.Equal(t, expected, HasRepository(team, repoID))
}
test(1, 1, false)
test(1, 3, true)
@@ -124,29 +71,29 @@ func TestTeam_AddRepository(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
testSuccess := func(teamID, repoID int64) {
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository)
- assert.NoError(t, team.AddRepository(repo))
- unittest.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID})
- unittest.CheckConsistencyFor(t, &Team{ID: teamID}, &repo_model.Repository{ID: repoID})
+ assert.NoError(t, AddRepository(team, repo))
+ unittest.AssertExistsAndLoadBean(t, &organization.TeamRepo{TeamID: teamID, RepoID: repoID})
+ unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID}, &repo_model.Repository{ID: repoID})
}
testSuccess(2, 3)
testSuccess(2, 5)
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 1}).(*organization.Team)
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
- assert.Error(t, team.AddRepository(repo))
- unittest.CheckConsistencyFor(t, &Team{ID: 1}, &repo_model.Repository{ID: 1})
+ assert.Error(t, AddRepository(team, repo))
+ unittest.CheckConsistencyFor(t, &organization.Team{ID: 1}, &repo_model.Repository{ID: 1})
}
func TestTeam_RemoveRepository(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
testSuccess := func(teamID, repoID int64) {
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
- assert.NoError(t, team.RemoveRepository(repoID))
- unittest.AssertNotExistsBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID})
- unittest.CheckConsistencyFor(t, &Team{ID: teamID}, &repo_model.Repository{ID: repoID})
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
+ assert.NoError(t, RemoveRepository(team, repoID))
+ unittest.AssertNotExistsBean(t, &organization.TeamRepo{TeamID: teamID, RepoID: repoID})
+ unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID}, &repo_model.Repository{ID: repoID})
}
testSuccess(2, 3)
testSuccess(2, 5)
@@ -154,97 +101,62 @@ func TestTeam_RemoveRepository(t *testing.T) {
}
func TestIsUsableTeamName(t *testing.T) {
- assert.NoError(t, IsUsableTeamName("usable"))
- assert.True(t, db.IsErrNameReserved(IsUsableTeamName("new")))
+ assert.NoError(t, organization.IsUsableTeamName("usable"))
+ assert.True(t, db.IsErrNameReserved(organization.IsUsableTeamName("new")))
}
func TestNewTeam(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
const teamName = "newTeamName"
- team := &Team{Name: teamName, OrgID: 3}
+ team := &organization.Team{Name: teamName, OrgID: 3}
assert.NoError(t, NewTeam(team))
- unittest.AssertExistsAndLoadBean(t, &Team{Name: teamName})
- unittest.CheckConsistencyFor(t, &Team{}, &user_model.User{ID: team.OrgID})
-}
-
-func TestGetTeam(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- testSuccess := func(orgID int64, name string) {
- team, err := GetTeam(orgID, name)
- assert.NoError(t, err)
- assert.EqualValues(t, orgID, team.OrgID)
- assert.Equal(t, name, team.Name)
- }
- testSuccess(3, "Owners")
- testSuccess(3, "team1")
-
- _, err := GetTeam(3, "nonexistent")
- assert.Error(t, err)
- _, err = GetTeam(unittest.NonexistentID, "Owners")
- assert.Error(t, err)
-}
-
-func TestGetTeamByID(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- testSuccess := func(teamID int64) {
- team, err := GetTeamByID(teamID)
- assert.NoError(t, err)
- assert.EqualValues(t, teamID, team.ID)
- }
- testSuccess(1)
- testSuccess(2)
- testSuccess(3)
- testSuccess(4)
-
- _, err := GetTeamByID(unittest.NonexistentID)
- assert.Error(t, err)
+ unittest.AssertExistsAndLoadBean(t, &organization.Team{Name: teamName})
+ unittest.CheckConsistencyFor(t, &organization.Team{}, &user_model.User{ID: team.OrgID})
}
func TestUpdateTeam(t *testing.T) {
// successful update
assert.NoError(t, unittest.PrepareTestDatabase())
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team)
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2}).(*organization.Team)
team.LowerName = "newname"
team.Name = "newName"
team.Description = strings.Repeat("A long description!", 100)
team.AccessMode = perm.AccessModeAdmin
assert.NoError(t, UpdateTeam(team, true, false))
- team = unittest.AssertExistsAndLoadBean(t, &Team{Name: "newName"}).(*Team)
+ team = unittest.AssertExistsAndLoadBean(t, &organization.Team{Name: "newName"}).(*organization.Team)
assert.True(t, strings.HasPrefix(team.Description, "A long description!"))
access := unittest.AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: 3}).(*Access)
assert.EqualValues(t, perm.AccessModeAdmin, access.Mode)
- unittest.CheckConsistencyFor(t, &Team{ID: team.ID})
+ unittest.CheckConsistencyFor(t, &organization.Team{ID: team.ID})
}
func TestUpdateTeam2(t *testing.T) {
// update to already-existing team
assert.NoError(t, unittest.PrepareTestDatabase())
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team)
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2}).(*organization.Team)
team.LowerName = "owners"
team.Name = "Owners"
team.Description = strings.Repeat("A long description!", 100)
err := UpdateTeam(team, true, false)
- assert.True(t, IsErrTeamAlreadyExist(err))
+ assert.True(t, organization.IsErrTeamAlreadyExist(err))
- unittest.CheckConsistencyFor(t, &Team{ID: team.ID})
+ unittest.CheckConsistencyFor(t, &organization.Team{ID: team.ID})
}
func TestDeleteTeam(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team)
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2}).(*organization.Team)
assert.NoError(t, DeleteTeam(team))
- unittest.AssertNotExistsBean(t, &Team{ID: team.ID})
- unittest.AssertNotExistsBean(t, &TeamRepo{TeamID: team.ID})
- unittest.AssertNotExistsBean(t, &TeamUser{TeamID: team.ID})
+ unittest.AssertNotExistsBean(t, &organization.Team{ID: team.ID})
+ unittest.AssertNotExistsBean(t, &organization.TeamRepo{TeamID: team.ID})
+ unittest.AssertNotExistsBean(t, &organization.TeamUser{TeamID: team.ID})
// check that team members don't have "leftover" access to repos
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User)
@@ -254,78 +166,14 @@ func TestDeleteTeam(t *testing.T) {
assert.True(t, accessMode < perm.AccessModeWrite)
}
-func TestIsTeamMember(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
- test := func(orgID, teamID, userID int64, expected bool) {
- isMember, err := IsTeamMember(orgID, teamID, userID)
- assert.NoError(t, err)
- 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) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- test := func(teamID int64) {
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
- members, err := GetTeamMembers(teamID)
- assert.NoError(t, err)
- assert.Len(t, members, team.NumMembers)
- for _, member := range members {
- unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID})
- }
- }
- test(1)
- test(3)
-}
-
-func TestGetUserTeams(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
- test := func(userID int64) {
- teams, _, err := GetUserTeams(&GetUserTeamOptions{UserID: userID})
- assert.NoError(t, err)
- for _, team := range teams {
- unittest.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID})
- }
- }
- test(2)
- test(5)
- test(unittest.NonexistentID)
-}
-
-func TestGetUserOrgTeams(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
- test := func(orgID, userID int64) {
- teams, err := GetUserOrgTeams(orgID, userID)
- assert.NoError(t, err)
- for _, team := range teams {
- assert.EqualValues(t, orgID, team.OrgID)
- unittest.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID})
- }
- }
- test(3, 2)
- test(3, 4)
- test(3, unittest.NonexistentID)
-}
-
func TestAddTeamMember(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
test := func(teamID, userID int64) {
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
assert.NoError(t, AddTeamMember(team, userID))
- unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID})
- unittest.CheckConsistencyFor(t, &Team{ID: teamID}, &user_model.User{ID: team.OrgID})
+ unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{UID: userID, TeamID: teamID})
+ unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID}, &user_model.User{ID: team.OrgID})
}
test(1, 2)
test(1, 4)
@@ -336,47 +184,17 @@ func TestRemoveTeamMember(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
testSuccess := func(teamID, userID int64) {
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
assert.NoError(t, RemoveTeamMember(team, userID))
- unittest.AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID})
- unittest.CheckConsistencyFor(t, &Team{ID: teamID})
+ unittest.AssertNotExistsBean(t, &organization.TeamUser{UID: userID, TeamID: teamID})
+ unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID})
}
testSuccess(1, 4)
testSuccess(2, 2)
testSuccess(3, 2)
testSuccess(3, unittest.NonexistentID)
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
+ team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 1}).(*organization.Team)
err := RemoveTeamMember(team, 2)
- assert.True(t, IsErrLastOrgOwner(err))
-}
-
-func TestHasTeamRepo(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- test := func(teamID, repoID int64, expected bool) {
- team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
- assert.Equal(t, expected, HasTeamRepo(team.OrgID, teamID, repoID))
- }
- 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) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- test := func(teamIDs, userIDs []int64, expected int64) {
- count, err := UsersInTeamsCount(teamIDs, userIDs)
- assert.NoError(t, err)
- 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
+ assert.True(t, organization.IsErrLastOrgOwner(err))
}