You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.2 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repository
  5. import (
  6. "fmt"
  7. "testing"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/structs"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestIncludesAllRepositoriesTeams(t *testing.T) {
  13. assert.NoError(t, models.PrepareTestDatabase())
  14. testTeamRepositories := func(teamID int64, repoIds []int64) {
  15. team := models.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team)
  16. assert.NoError(t, team.GetRepositories(&models.SearchTeamOptions{}), "%s: GetRepositories", team.Name)
  17. assert.Len(t, team.Repos, team.NumRepos, "%s: len repo", team.Name)
  18. assert.Equal(t, len(repoIds), len(team.Repos), "%s: repo count", team.Name)
  19. for i, rid := range repoIds {
  20. if rid > 0 {
  21. assert.True(t, team.HasRepository(rid), "%s: HasRepository(%d) %d", rid, i)
  22. }
  23. }
  24. }
  25. // Get an admin user.
  26. user, err := models.GetUserByID(1)
  27. assert.NoError(t, err, "GetUserByID")
  28. // Create org.
  29. org := &models.User{
  30. Name: "All_repo",
  31. IsActive: true,
  32. Type: models.UserTypeOrganization,
  33. Visibility: structs.VisibleTypePublic,
  34. }
  35. assert.NoError(t, models.CreateOrganization(org, user), "CreateOrganization")
  36. // Check Owner team.
  37. ownerTeam, err := org.GetOwnerTeam()
  38. assert.NoError(t, err, "GetOwnerTeam")
  39. assert.True(t, ownerTeam.IncludesAllRepositories, "Owner team includes all repositories")
  40. // Create repos.
  41. repoIds := make([]int64, 0)
  42. for i := 0; i < 3; i++ {
  43. r, err := CreateRepository(user, org, models.CreateRepoOptions{Name: fmt.Sprintf("repo-%d", i)})
  44. assert.NoError(t, err, "CreateRepository %d", i)
  45. if r != nil {
  46. repoIds = append(repoIds, r.ID)
  47. }
  48. }
  49. // Get fresh copy of Owner team after creating repos.
  50. ownerTeam, err = org.GetOwnerTeam()
  51. assert.NoError(t, err, "GetOwnerTeam")
  52. // Create teams and check repositories.
  53. teams := []*models.Team{
  54. ownerTeam,
  55. {
  56. OrgID: org.ID,
  57. Name: "team one",
  58. Authorize: models.AccessModeRead,
  59. IncludesAllRepositories: true,
  60. },
  61. {
  62. OrgID: org.ID,
  63. Name: "team 2",
  64. Authorize: models.AccessModeRead,
  65. IncludesAllRepositories: false,
  66. },
  67. {
  68. OrgID: org.ID,
  69. Name: "team three",
  70. Authorize: models.AccessModeWrite,
  71. IncludesAllRepositories: true,
  72. },
  73. {
  74. OrgID: org.ID,
  75. Name: "team 4",
  76. Authorize: models.AccessModeWrite,
  77. IncludesAllRepositories: false,
  78. },
  79. }
  80. teamRepos := [][]int64{
  81. repoIds,
  82. repoIds,
  83. {},
  84. repoIds,
  85. {},
  86. }
  87. for i, team := range teams {
  88. if i > 0 { // first team is Owner.
  89. assert.NoError(t, models.NewTeam(team), "%s: NewTeam", team.Name)
  90. }
  91. testTeamRepositories(team.ID, teamRepos[i])
  92. }
  93. // Update teams and check repositories.
  94. teams[3].IncludesAllRepositories = false
  95. teams[4].IncludesAllRepositories = true
  96. teamRepos[4] = repoIds
  97. for i, team := range teams {
  98. assert.NoError(t, models.UpdateTeam(team, false, true), "%s: UpdateTeam", team.Name)
  99. testTeamRepositories(team.ID, teamRepos[i])
  100. }
  101. // Create repo and check teams repositories.
  102. org.Teams = nil // Reset teams to allow their reloading.
  103. r, err := CreateRepository(user, org, models.CreateRepoOptions{Name: "repo-last"})
  104. assert.NoError(t, err, "CreateRepository last")
  105. if r != nil {
  106. repoIds = append(repoIds, r.ID)
  107. }
  108. teamRepos[0] = repoIds
  109. teamRepos[1] = repoIds
  110. teamRepos[4] = repoIds
  111. for i, team := range teams {
  112. testTeamRepositories(team.ID, teamRepos[i])
  113. }
  114. // Remove repo and check teams repositories.
  115. assert.NoError(t, models.DeleteRepository(user, org.ID, repoIds[0]), "DeleteRepository")
  116. teamRepos[0] = repoIds[1:]
  117. teamRepos[1] = repoIds[1:]
  118. teamRepos[3] = repoIds[1:3]
  119. teamRepos[4] = repoIds[1:]
  120. for i, team := range teams {
  121. testTeamRepositories(team.ID, teamRepos[i])
  122. }
  123. // Wipe created items.
  124. for i, rid := range repoIds {
  125. if i > 0 { // first repo already deleted.
  126. assert.NoError(t, models.DeleteRepository(user, org.ID, rid), "DeleteRepository %d", i)
  127. }
  128. }
  129. assert.NoError(t, models.DeleteOrganization(org), "DeleteOrganization")
  130. }