aboutsummaryrefslogtreecommitdiffstats
path: root/models/org_team_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/org_team_test.go')
-rw-r--r--models/org_team_test.go137
1 files changed, 135 insertions, 2 deletions
diff --git a/models/org_team_test.go b/models/org_team_test.go
index 06ab4637d8..b7e2ef113d 100644
--- a/models/org_team_test.go
+++ b/models/org_team_test.go
@@ -5,9 +5,12 @@
package models
import (
+ "fmt"
"strings"
"testing"
+ "code.gitea.io/gitea/modules/structs"
+
"github.com/stretchr/testify/assert"
)
@@ -206,7 +209,7 @@ func TestUpdateTeam(t *testing.T) {
team.Name = "newName"
team.Description = strings.Repeat("A long description!", 100)
team.Authorize = AccessModeAdmin
- assert.NoError(t, UpdateTeam(team, true))
+ assert.NoError(t, UpdateTeam(team, true, false))
team = AssertExistsAndLoadBean(t, &Team{Name: "newName"}).(*Team)
assert.True(t, strings.HasPrefix(team.Description, "A long description!"))
@@ -225,7 +228,7 @@ func TestUpdateTeam2(t *testing.T) {
team.LowerName = "owners"
team.Name = "Owners"
team.Description = strings.Repeat("A long description!", 100)
- err := UpdateTeam(team, true)
+ err := UpdateTeam(team, true, false)
assert.True(t, IsErrTeamAlreadyExist(err))
CheckConsistencyFor(t, &Team{ID: team.ID})
@@ -374,3 +377,133 @@ func TestUsersInTeamsCount(t *testing.T) {
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
}
+
+func TestIncludesAllRepositoriesTeams(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+
+ testTeamRepositories := func(teamID int64, repoIds []int64) {
+ team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
+ assert.NoError(t, team.GetRepositories(), "%s: GetRepositories", team.Name)
+ assert.Len(t, team.Repos, team.NumRepos, "%s: len repo", team.Name)
+ assert.Equal(t, len(repoIds), len(team.Repos), "%s: repo count", team.Name)
+ for i, rid := range repoIds {
+ if rid > 0 {
+ assert.True(t, team.HasRepository(rid), "%s: HasRepository(%d) %d", rid, i)
+ }
+ }
+ }
+
+ // Get an admin user.
+ user, err := GetUserByID(1)
+ assert.NoError(t, err, "GetUserByID")
+
+ // Create org.
+ org := &User{
+ Name: "All repo",
+ IsActive: true,
+ Type: UserTypeOrganization,
+ Visibility: structs.VisibleTypePublic,
+ }
+ assert.NoError(t, CreateOrganization(org, user), "CreateOrganization")
+
+ // Check Owner team.
+ ownerTeam, err := org.GetOwnerTeam()
+ assert.NoError(t, err, "GetOwnerTeam")
+ assert.True(t, ownerTeam.IncludesAllRepositories, "Owner team includes all repositories")
+
+ // Create repos.
+ repoIds := make([]int64, 0)
+ for i := 0; i < 3; i++ {
+ r, err := CreateRepository(user, org, CreateRepoOptions{Name: fmt.Sprintf("repo-%d", i)})
+ assert.NoError(t, err, "CreateRepository %d", i)
+ if r != nil {
+ repoIds = append(repoIds, r.ID)
+ }
+ }
+ // Get fresh copy of Owner team after creating repos.
+ ownerTeam, err = org.GetOwnerTeam()
+ assert.NoError(t, err, "GetOwnerTeam")
+
+ // Create teams and check repositories.
+ teams := []*Team{
+ ownerTeam,
+ {
+ OrgID: org.ID,
+ Name: "team one",
+ Authorize: AccessModeRead,
+ IncludesAllRepositories: true,
+ },
+ {
+ OrgID: org.ID,
+ Name: "team 2",
+ Authorize: AccessModeRead,
+ IncludesAllRepositories: false,
+ },
+ {
+ OrgID: org.ID,
+ Name: "team three",
+ Authorize: AccessModeWrite,
+ IncludesAllRepositories: true,
+ },
+ {
+ OrgID: org.ID,
+ Name: "team 4",
+ Authorize: AccessModeWrite,
+ IncludesAllRepositories: false,
+ },
+ }
+ teamRepos := [][]int64{
+ repoIds,
+ repoIds,
+ {},
+ repoIds,
+ {},
+ }
+ for i, team := range teams {
+ if i > 0 { // first team is Owner.
+ assert.NoError(t, NewTeam(team), "%s: NewTeam", team.Name)
+ }
+ testTeamRepositories(team.ID, teamRepos[i])
+ }
+
+ // Update teams and check repositories.
+ teams[3].IncludesAllRepositories = false
+ teams[4].IncludesAllRepositories = true
+ teamRepos[4] = repoIds
+ for i, team := range teams {
+ assert.NoError(t, UpdateTeam(team, false, true), "%s: UpdateTeam", team.Name)
+ testTeamRepositories(team.ID, teamRepos[i])
+ }
+
+ // Create repo and check teams repositories.
+ org.Teams = nil // Reset teams to allow their reloading.
+ r, err := CreateRepository(user, org, CreateRepoOptions{Name: "repo-last"})
+ assert.NoError(t, err, "CreateRepository last")
+ if r != nil {
+ repoIds = append(repoIds, r.ID)
+ }
+ teamRepos[0] = repoIds
+ teamRepos[1] = repoIds
+ teamRepos[4] = repoIds
+ for i, team := range teams {
+ testTeamRepositories(team.ID, teamRepos[i])
+ }
+
+ // Remove repo and check teams repositories.
+ assert.NoError(t, DeleteRepository(user, org.ID, repoIds[0]), "DeleteRepository")
+ teamRepos[0] = repoIds[1:]
+ teamRepos[1] = repoIds[1:]
+ teamRepos[3] = repoIds[1:3]
+ teamRepos[4] = repoIds[1:]
+ for i, team := range teams {
+ testTeamRepositories(team.ID, teamRepos[i])
+ }
+
+ // Wipe created items.
+ for i, rid := range repoIds {
+ if i > 0 { // first repo already deleted.
+ assert.NoError(t, DeleteRepository(user, org.ID, rid), "DeleteRepository %d", i)
+ }
+ }
+ assert.NoError(t, DeleteOrganization(org), "DeleteOrganization")
+}