code.gitea.io/gitea@v1.21.7/services/repository/create_test.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package repository 5 6 import ( 7 "fmt" 8 "testing" 9 10 "code.gitea.io/gitea/models" 11 "code.gitea.io/gitea/models/db" 12 "code.gitea.io/gitea/models/organization" 13 "code.gitea.io/gitea/models/perm" 14 "code.gitea.io/gitea/models/unittest" 15 user_model "code.gitea.io/gitea/models/user" 16 "code.gitea.io/gitea/modules/structs" 17 18 "github.com/stretchr/testify/assert" 19 ) 20 21 func TestIncludesAllRepositoriesTeams(t *testing.T) { 22 assert.NoError(t, unittest.PrepareTestDatabase()) 23 24 testTeamRepositories := func(teamID int64, repoIds []int64) { 25 team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}) 26 assert.NoError(t, team.LoadRepositories(db.DefaultContext), "%s: GetRepositories", team.Name) 27 assert.Len(t, team.Repos, team.NumRepos, "%s: len repo", team.Name) 28 assert.Len(t, team.Repos, len(repoIds), "%s: repo count", team.Name) 29 for i, rid := range repoIds { 30 if rid > 0 { 31 assert.True(t, HasRepository(db.DefaultContext, team, rid), "%s: HasRepository(%d) %d", rid, i) 32 } 33 } 34 } 35 36 // Get an admin user. 37 user, err := user_model.GetUserByID(db.DefaultContext, 1) 38 assert.NoError(t, err, "GetUserByID") 39 40 // Create org. 41 org := &organization.Organization{ 42 Name: "All_repo", 43 IsActive: true, 44 Type: user_model.UserTypeOrganization, 45 Visibility: structs.VisibleTypePublic, 46 } 47 assert.NoError(t, organization.CreateOrganization(org, user), "CreateOrganization") 48 49 // Check Owner team. 50 ownerTeam, err := org.GetOwnerTeam(db.DefaultContext) 51 assert.NoError(t, err, "GetOwnerTeam") 52 assert.True(t, ownerTeam.IncludesAllRepositories, "Owner team includes all repositories") 53 54 // Create repos. 55 repoIds := make([]int64, 0) 56 for i := 0; i < 3; i++ { 57 r, err := CreateRepositoryDirectly(db.DefaultContext, user, org.AsUser(), CreateRepoOptions{Name: fmt.Sprintf("repo-%d", i)}) 58 assert.NoError(t, err, "CreateRepository %d", i) 59 if r != nil { 60 repoIds = append(repoIds, r.ID) 61 } 62 } 63 // Get fresh copy of Owner team after creating repos. 64 ownerTeam, err = org.GetOwnerTeam(db.DefaultContext) 65 assert.NoError(t, err, "GetOwnerTeam") 66 67 // Create teams and check repositories. 68 teams := []*organization.Team{ 69 ownerTeam, 70 { 71 OrgID: org.ID, 72 Name: "team one", 73 AccessMode: perm.AccessModeRead, 74 IncludesAllRepositories: true, 75 }, 76 { 77 OrgID: org.ID, 78 Name: "team 2", 79 AccessMode: perm.AccessModeRead, 80 IncludesAllRepositories: false, 81 }, 82 { 83 OrgID: org.ID, 84 Name: "team three", 85 AccessMode: perm.AccessModeWrite, 86 IncludesAllRepositories: true, 87 }, 88 { 89 OrgID: org.ID, 90 Name: "team 4", 91 AccessMode: perm.AccessModeWrite, 92 IncludesAllRepositories: false, 93 }, 94 } 95 teamRepos := [][]int64{ 96 repoIds, 97 repoIds, 98 {}, 99 repoIds, 100 {}, 101 } 102 for i, team := range teams { 103 if i > 0 { // first team is Owner. 104 assert.NoError(t, models.NewTeam(db.DefaultContext, team), "%s: NewTeam", team.Name) 105 } 106 testTeamRepositories(team.ID, teamRepos[i]) 107 } 108 109 // Update teams and check repositories. 110 teams[3].IncludesAllRepositories = false 111 teams[4].IncludesAllRepositories = true 112 teamRepos[4] = repoIds 113 for i, team := range teams { 114 assert.NoError(t, models.UpdateTeam(db.DefaultContext, team, false, true), "%s: UpdateTeam", team.Name) 115 testTeamRepositories(team.ID, teamRepos[i]) 116 } 117 118 // Create repo and check teams repositories. 119 r, err := CreateRepositoryDirectly(db.DefaultContext, user, org.AsUser(), CreateRepoOptions{Name: "repo-last"}) 120 assert.NoError(t, err, "CreateRepository last") 121 if r != nil { 122 repoIds = append(repoIds, r.ID) 123 } 124 teamRepos[0] = repoIds 125 teamRepos[1] = repoIds 126 teamRepos[4] = repoIds 127 for i, team := range teams { 128 testTeamRepositories(team.ID, teamRepos[i]) 129 } 130 131 // Remove repo and check teams repositories. 132 assert.NoError(t, DeleteRepositoryDirectly(db.DefaultContext, user, org.ID, repoIds[0]), "DeleteRepository") 133 teamRepos[0] = repoIds[1:] 134 teamRepos[1] = repoIds[1:] 135 teamRepos[3] = repoIds[1:3] 136 teamRepos[4] = repoIds[1:] 137 for i, team := range teams { 138 testTeamRepositories(team.ID, teamRepos[i]) 139 } 140 141 // Wipe created items. 142 for i, rid := range repoIds { 143 if i > 0 { // first repo already deleted. 144 assert.NoError(t, DeleteRepositoryDirectly(db.DefaultContext, user, org.ID, rid), "DeleteRepository %d", i) 145 } 146 } 147 assert.NoError(t, organization.DeleteOrganization(db.DefaultContext, org), "DeleteOrganization") 148 }