2023-09-06 14:08:51 +02:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
"code.gitea.io/gitea/models/organization"
|
|
|
|
"code.gitea.io/gitea/models/perm"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIncludesAllRepositoriesTeams(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
testTeamRepositories := func(teamID int64, repoIds []int64) {
|
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
|
|
|
assert.NoError(t, team.LoadRepositories(db.DefaultContext), "%s: GetRepositories", team.Name)
|
|
|
|
assert.Len(t, team.Repos, team.NumRepos, "%s: len repo", team.Name)
|
|
|
|
assert.Len(t, team.Repos, len(repoIds), "%s: repo count", team.Name)
|
|
|
|
for i, rid := range repoIds {
|
|
|
|
if rid > 0 {
|
2023-09-15 08:13:19 +02:00
|
|
|
assert.True(t, HasRepository(db.DefaultContext, team, rid), "%s: HasRepository(%d) %d", rid, i)
|
2023-09-06 14:08:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an admin user.
|
|
|
|
user, err := user_model.GetUserByID(db.DefaultContext, 1)
|
|
|
|
assert.NoError(t, err, "GetUserByID")
|
|
|
|
|
|
|
|
// Create org.
|
|
|
|
org := &organization.Organization{
|
|
|
|
Name: "All_repo",
|
|
|
|
IsActive: true,
|
|
|
|
Type: user_model.UserTypeOrganization,
|
|
|
|
Visibility: structs.VisibleTypePublic,
|
|
|
|
}
|
2023-10-03 12:30:41 +02:00
|
|
|
assert.NoError(t, organization.CreateOrganization(db.DefaultContext, org, user), "CreateOrganization")
|
2023-09-06 14:08:51 +02:00
|
|
|
|
|
|
|
// Check Owner team.
|
|
|
|
ownerTeam, err := org.GetOwnerTeam(db.DefaultContext)
|
|
|
|
assert.NoError(t, err, "GetOwnerTeam")
|
|
|
|
assert.True(t, ownerTeam.IncludesAllRepositories, "Owner team includes all repositories")
|
|
|
|
|
|
|
|
// Create repos.
|
2024-02-15 16:19:36 +01:00
|
|
|
repoIDs := make([]int64, 0)
|
2023-09-06 14:08:51 +02:00
|
|
|
for i := 0; i < 3; i++ {
|
2023-09-08 06:51:15 +02:00
|
|
|
r, err := CreateRepositoryDirectly(db.DefaultContext, user, org.AsUser(), CreateRepoOptions{Name: fmt.Sprintf("repo-%d", i)})
|
2023-09-06 14:08:51 +02:00
|
|
|
assert.NoError(t, err, "CreateRepository %d", i)
|
|
|
|
if r != nil {
|
2024-02-15 16:19:36 +01:00
|
|
|
repoIDs = append(repoIDs, r.ID)
|
2023-09-06 14:08:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get fresh copy of Owner team after creating repos.
|
|
|
|
ownerTeam, err = org.GetOwnerTeam(db.DefaultContext)
|
|
|
|
assert.NoError(t, err, "GetOwnerTeam")
|
|
|
|
|
|
|
|
// Create teams and check repositories.
|
|
|
|
teams := []*organization.Team{
|
|
|
|
ownerTeam,
|
|
|
|
{
|
|
|
|
OrgID: org.ID,
|
|
|
|
Name: "team one",
|
|
|
|
AccessMode: perm.AccessModeRead,
|
|
|
|
IncludesAllRepositories: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
OrgID: org.ID,
|
|
|
|
Name: "team 2",
|
|
|
|
AccessMode: perm.AccessModeRead,
|
|
|
|
IncludesAllRepositories: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
OrgID: org.ID,
|
|
|
|
Name: "team three",
|
|
|
|
AccessMode: perm.AccessModeWrite,
|
|
|
|
IncludesAllRepositories: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
OrgID: org.ID,
|
|
|
|
Name: "team 4",
|
|
|
|
AccessMode: perm.AccessModeWrite,
|
|
|
|
IncludesAllRepositories: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
teamRepos := [][]int64{
|
2024-02-15 16:19:36 +01:00
|
|
|
repoIDs,
|
|
|
|
repoIDs,
|
2023-09-06 14:08:51 +02:00
|
|
|
{},
|
2024-02-15 16:19:36 +01:00
|
|
|
repoIDs,
|
2023-09-06 14:08:51 +02:00
|
|
|
{},
|
|
|
|
}
|
|
|
|
for i, team := range teams {
|
|
|
|
if i > 0 { // first team is Owner.
|
2023-09-14 19:09:32 +02:00
|
|
|
assert.NoError(t, models.NewTeam(db.DefaultContext, team), "%s: NewTeam", team.Name)
|
2023-09-06 14:08:51 +02:00
|
|
|
}
|
|
|
|
testTeamRepositories(team.ID, teamRepos[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update teams and check repositories.
|
|
|
|
teams[3].IncludesAllRepositories = false
|
|
|
|
teams[4].IncludesAllRepositories = true
|
2024-02-15 16:19:36 +01:00
|
|
|
teamRepos[4] = repoIDs
|
2023-09-06 14:08:51 +02:00
|
|
|
for i, team := range teams {
|
2023-09-14 19:09:32 +02:00
|
|
|
assert.NoError(t, models.UpdateTeam(db.DefaultContext, team, false, true), "%s: UpdateTeam", team.Name)
|
2023-09-06 14:08:51 +02:00
|
|
|
testTeamRepositories(team.ID, teamRepos[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create repo and check teams repositories.
|
2023-09-08 06:51:15 +02:00
|
|
|
r, err := CreateRepositoryDirectly(db.DefaultContext, user, org.AsUser(), CreateRepoOptions{Name: "repo-last"})
|
2023-09-06 14:08:51 +02:00
|
|
|
assert.NoError(t, err, "CreateRepository last")
|
|
|
|
if r != nil {
|
2024-02-15 16:19:36 +01:00
|
|
|
repoIDs = append(repoIDs, r.ID)
|
2023-09-06 14:08:51 +02:00
|
|
|
}
|
2024-02-15 16:19:36 +01:00
|
|
|
teamRepos[0] = repoIDs
|
|
|
|
teamRepos[1] = repoIDs
|
|
|
|
teamRepos[4] = repoIDs
|
2023-09-06 14:08:51 +02:00
|
|
|
for i, team := range teams {
|
|
|
|
testTeamRepositories(team.ID, teamRepos[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove repo and check teams repositories.
|
2024-02-15 16:19:36 +01:00
|
|
|
assert.NoError(t, DeleteRepositoryDirectly(db.DefaultContext, user, repoIDs[0]), "DeleteRepository")
|
|
|
|
teamRepos[0] = repoIDs[1:]
|
|
|
|
teamRepos[1] = repoIDs[1:]
|
|
|
|
teamRepos[3] = repoIDs[1:3]
|
|
|
|
teamRepos[4] = repoIDs[1:]
|
2023-09-06 14:08:51 +02:00
|
|
|
for i, team := range teams {
|
|
|
|
testTeamRepositories(team.ID, teamRepos[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wipe created items.
|
2024-02-15 16:19:36 +01:00
|
|
|
for i, rid := range repoIDs {
|
2023-09-06 14:08:51 +02:00
|
|
|
if i > 0 { // first repo already deleted.
|
2023-10-18 17:03:10 +02:00
|
|
|
assert.NoError(t, DeleteRepositoryDirectly(db.DefaultContext, user, rid), "DeleteRepository %d", i)
|
2023-09-06 14:08:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.NoError(t, organization.DeleteOrganization(db.DefaultContext, org), "DeleteOrganization")
|
|
|
|
}
|