2020-01-12 13:11:17 +01:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-12 13:11:17 +01:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2022-08-25 04:31:57 +02:00
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2021-11-18 18:42:27 +01:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 08:29:02 +02:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 12:58:28 +01:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-06-06 10:01:49 +02:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 15:36:47 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-12 13:11:17 +01:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIncludesAllRepositoriesTeams(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-01-12 13:11:17 +01:00
|
|
|
|
|
|
|
testTeamRepositories := func(teamID int64, repoIds []int64) {
|
2022-08-16 04:22:25 +02:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
2022-12-03 03:48:26 +01:00
|
|
|
assert.NoError(t, team.LoadRepositories(db.DefaultContext), "%s: GetRepositories", team.Name)
|
2020-01-12 13:11:17 +01:00
|
|
|
assert.Len(t, team.Repos, team.NumRepos, "%s: len repo", team.Name)
|
2021-06-07 07:27:09 +02:00
|
|
|
assert.Len(t, team.Repos, len(repoIds), "%s: repo count", team.Name)
|
2020-01-12 13:11:17 +01:00
|
|
|
for i, rid := range repoIds {
|
|
|
|
if rid > 0 {
|
2022-03-29 08:29:02 +02:00
|
|
|
assert.True(t, models.HasRepository(team, rid), "%s: HasRepository(%d) %d", rid, i)
|
2020-01-12 13:11:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an admin user.
|
2022-12-03 03:48:26 +01:00
|
|
|
user, err := user_model.GetUserByID(db.DefaultContext, 1)
|
2020-01-12 13:11:17 +01:00
|
|
|
assert.NoError(t, err, "GetUserByID")
|
|
|
|
|
|
|
|
// Create org.
|
2022-03-29 08:29:02 +02:00
|
|
|
org := &organization.Organization{
|
2020-02-23 20:52:05 +01:00
|
|
|
Name: "All_repo",
|
2020-01-12 13:11:17 +01:00
|
|
|
IsActive: true,
|
2021-11-24 10:49:20 +01:00
|
|
|
Type: user_model.UserTypeOrganization,
|
2020-01-12 13:11:17 +01:00
|
|
|
Visibility: structs.VisibleTypePublic,
|
|
|
|
}
|
2022-03-29 08:29:02 +02:00
|
|
|
assert.NoError(t, organization.CreateOrganization(org, user), "CreateOrganization")
|
2020-01-12 13:11:17 +01:00
|
|
|
|
|
|
|
// Check Owner team.
|
2023-02-08 07:44:42 +01:00
|
|
|
ownerTeam, err := org.GetOwnerTeam(db.DefaultContext)
|
2020-01-12 13:11:17 +01:00
|
|
|
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++ {
|
2022-08-25 04:31:57 +02:00
|
|
|
r, err := CreateRepository(user, org.AsUser(), CreateRepoOptions{Name: fmt.Sprintf("repo-%d", i)})
|
2020-01-12 13:11:17 +01:00
|
|
|
assert.NoError(t, err, "CreateRepository %d", i)
|
|
|
|
if r != nil {
|
|
|
|
repoIds = append(repoIds, r.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get fresh copy of Owner team after creating repos.
|
2023-02-08 07:44:42 +01:00
|
|
|
ownerTeam, err = org.GetOwnerTeam(db.DefaultContext)
|
2020-01-12 13:11:17 +01:00
|
|
|
assert.NoError(t, err, "GetOwnerTeam")
|
|
|
|
|
|
|
|
// Create teams and check repositories.
|
2022-03-29 08:29:02 +02:00
|
|
|
teams := []*organization.Team{
|
2020-01-12 13:11:17 +01:00
|
|
|
ownerTeam,
|
|
|
|
{
|
|
|
|
OrgID: org.ID,
|
|
|
|
Name: "team one",
|
2022-01-05 04:37:00 +01:00
|
|
|
AccessMode: perm.AccessModeRead,
|
2020-01-12 13:11:17 +01:00
|
|
|
IncludesAllRepositories: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
OrgID: org.ID,
|
|
|
|
Name: "team 2",
|
2022-01-05 04:37:00 +01:00
|
|
|
AccessMode: perm.AccessModeRead,
|
2020-01-12 13:11:17 +01:00
|
|
|
IncludesAllRepositories: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
OrgID: org.ID,
|
|
|
|
Name: "team three",
|
2022-01-05 04:37:00 +01:00
|
|
|
AccessMode: perm.AccessModeWrite,
|
2020-01-12 13:11:17 +01:00
|
|
|
IncludesAllRepositories: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
OrgID: org.ID,
|
|
|
|
Name: "team 4",
|
2022-01-05 04:37:00 +01:00
|
|
|
AccessMode: perm.AccessModeWrite,
|
2020-01-12 13:11:17 +01:00
|
|
|
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.
|
2022-08-25 04:31:57 +02:00
|
|
|
r, err := CreateRepository(user, org.AsUser(), CreateRepoOptions{Name: "repo-last"})
|
2020-01-12 13:11:17 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-03-29 08:29:02 +02:00
|
|
|
assert.NoError(t, organization.DeleteOrganization(db.DefaultContext, org), "DeleteOrganization")
|
2020-01-12 13:11:17 +01:00
|
|
|
}
|
2022-06-06 10:01:49 +02:00
|
|
|
|
|
|
|
func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
// Get sample repo and change visibility
|
2022-12-03 03:48:26 +01:00
|
|
|
repo, err := repo_model.GetRepositoryByID(db.DefaultContext, 9)
|
2022-06-06 10:01:49 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
repo.IsPrivate = true
|
|
|
|
|
|
|
|
// Update it
|
|
|
|
err = UpdateRepository(db.DefaultContext, repo, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Check visibility of action has become private
|
2022-08-25 04:31:57 +02:00
|
|
|
act := activities_model.Action{}
|
2022-06-06 10:01:49 +02:00
|
|
|
_, err = db.GetEngine(db.DefaultContext).ID(3).Get(&act)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, act.IsPrivate)
|
|
|
|
}
|
2023-01-13 19:54:02 +01:00
|
|
|
|
|
|
|
func TestGetDirectorySize(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo, err := repo_model.GetRepositoryByID(db.DefaultContext, 1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
size, err := getDirectorySize(repo.RepoPath())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, size, repo.Size)
|
|
|
|
}
|