2020-01-12 17:41:17 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-11-18 23:12:27 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-28 17:28:28 +05:30
|
|
|
"code.gitea.io/gitea/models/perm"
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-12 17:41:17 +05:30
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIncludesAllRepositoriesTeams(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-01-12 17:41:17 +05:30
|
|
|
|
|
|
|
testTeamRepositories := func(teamID int64, repoIds []int64) {
|
2021-11-16 14:23:21 +05:30
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team)
|
2022-02-02 00:39:24 +05:30
|
|
|
assert.NoError(t, team.GetRepositories(&models.SearchOrgTeamOptions{}), "%s: GetRepositories", team.Name)
|
2020-01-12 17:41:17 +05:30
|
|
|
assert.Len(t, team.Repos, team.NumRepos, "%s: len repo", team.Name)
|
2021-06-07 10:57:09 +05:30
|
|
|
assert.Len(t, team.Repos, len(repoIds), "%s: repo count", team.Name)
|
2020-01-12 17:41:17 +05:30
|
|
|
for i, rid := range repoIds {
|
|
|
|
if rid > 0 {
|
|
|
|
assert.True(t, team.HasRepository(rid), "%s: HasRepository(%d) %d", rid, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an admin user.
|
2021-11-24 15:19:20 +05:30
|
|
|
user, err := user_model.GetUserByID(1)
|
2020-01-12 17:41:17 +05:30
|
|
|
assert.NoError(t, err, "GetUserByID")
|
|
|
|
|
|
|
|
// Create org.
|
2021-11-19 17:11:40 +05:30
|
|
|
org := &models.Organization{
|
2020-02-24 01:22:05 +05:30
|
|
|
Name: "All_repo",
|
2020-01-12 17:41:17 +05:30
|
|
|
IsActive: true,
|
2021-11-24 15:19:20 +05:30
|
|
|
Type: user_model.UserTypeOrganization,
|
2020-01-12 17:41:17 +05:30
|
|
|
Visibility: structs.VisibleTypePublic,
|
|
|
|
}
|
|
|
|
assert.NoError(t, models.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++ {
|
2021-11-19 17:11:40 +05:30
|
|
|
r, err := CreateRepository(user, org.AsUser(), models.CreateRepoOptions{Name: fmt.Sprintf("repo-%d", i)})
|
2020-01-12 17:41:17 +05:30
|
|
|
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 := []*models.Team{
|
|
|
|
ownerTeam,
|
|
|
|
{
|
|
|
|
OrgID: org.ID,
|
|
|
|
Name: "team one",
|
2022-01-05 09:07:00 +05:30
|
|
|
AccessMode: perm.AccessModeRead,
|
2020-01-12 17:41:17 +05:30
|
|
|
IncludesAllRepositories: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
OrgID: org.ID,
|
|
|
|
Name: "team 2",
|
2022-01-05 09:07:00 +05:30
|
|
|
AccessMode: perm.AccessModeRead,
|
2020-01-12 17:41:17 +05:30
|
|
|
IncludesAllRepositories: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
OrgID: org.ID,
|
|
|
|
Name: "team three",
|
2022-01-05 09:07:00 +05:30
|
|
|
AccessMode: perm.AccessModeWrite,
|
2020-01-12 17:41:17 +05:30
|
|
|
IncludesAllRepositories: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
OrgID: org.ID,
|
|
|
|
Name: "team 4",
|
2022-01-05 09:07:00 +05:30
|
|
|
AccessMode: perm.AccessModeWrite,
|
2020-01-12 17:41:17 +05:30
|
|
|
IncludesAllRepositories: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
teamRepos := [][]int64{
|
|
|
|
repoIds,
|
|
|
|
repoIds,
|
|
|
|
{},
|
|
|
|
repoIds,
|
|
|
|
{},
|
|
|
|
}
|
|
|
|
for i, team := range teams {
|
|
|
|
if i > 0 { // first team is Owner.
|
|
|
|
assert.NoError(t, models.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, models.UpdateTeam(team, false, true), "%s: UpdateTeam", team.Name)
|
|
|
|
testTeamRepositories(team.ID, teamRepos[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create repo and check teams repositories.
|
2021-11-19 17:11:40 +05:30
|
|
|
r, err := CreateRepository(user, org.AsUser(), models.CreateRepoOptions{Name: "repo-last"})
|
2020-01-12 17:41:17 +05:30
|
|
|
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, models.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, models.DeleteRepository(user, org.ID, rid), "DeleteRepository %d", i)
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 23:12:27 +05:30
|
|
|
assert.NoError(t, models.DeleteOrganization(db.DefaultContext, org), "DeleteOrganization")
|
2020-01-12 17:41:17 +05:30
|
|
|
}
|