gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "strings"
  6. "testing"
  7. auth_model "code.gitea.io/gitea/models/auth"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/models/organization"
  10. "code.gitea.io/gitea/models/unittest"
  11. user_model "code.gitea.io/gitea/models/user"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/tests"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func TestOrgCounts(t *testing.T) {
  17. defer tests.PrepareTestEnv(t)()
  18. testOrgCounts(t)
  19. }
  20. func testOrgCounts(t *testing.T) {
  21. orgOwner := "user2"
  22. orgName := "testOrg"
  23. orgCollaborator := "user4"
  24. ctx := NewAPITestContext(t, orgOwner, "repo1", auth_model.AccessTokenScopeWriteOrganization)
  25. var ownerCountRepos map[string]int
  26. var collabCountRepos map[string]int
  27. t.Run("GetTheOwnersNumRepos", doCheckOrgCounts(orgOwner, map[string]int{},
  28. false,
  29. func(_ *testing.T, calcOrgCounts map[string]int) {
  30. ownerCountRepos = calcOrgCounts
  31. },
  32. ))
  33. t.Run("GetTheCollaboratorsNumRepos", doCheckOrgCounts(orgCollaborator, map[string]int{},
  34. false,
  35. func(_ *testing.T, calcOrgCounts map[string]int) {
  36. collabCountRepos = calcOrgCounts
  37. },
  38. ))
  39. t.Run("CreatePublicTestOrganization", doAPICreateOrganization(ctx, &api.CreateOrgOption{
  40. UserName: orgName,
  41. Visibility: "public",
  42. }))
  43. // Following the creation of the organization, the orgName must appear in the counts with 0 repos
  44. ownerCountRepos[orgName] = 0
  45. t.Run("AssertNumRepos0ForTestOrg", doCheckOrgCounts(orgOwner, ownerCountRepos, true))
  46. // the collaborator is not a collaborator yet
  47. t.Run("AssertNoTestOrgReposForCollaborator", doCheckOrgCounts(orgCollaborator, collabCountRepos, true))
  48. t.Run("CreateOrganizationPrivateRepo", doAPICreateOrganizationRepository(ctx, orgName, &api.CreateRepoOption{
  49. Name: "privateTestRepo",
  50. AutoInit: true,
  51. Private: true,
  52. }))
  53. ownerCountRepos[orgName] = 1
  54. t.Run("AssertNumRepos1ForTestOrg", doCheckOrgCounts(orgOwner, ownerCountRepos, true))
  55. t.Run("AssertNoTestOrgReposForCollaborator", doCheckOrgCounts(orgCollaborator, collabCountRepos, true))
  56. var testTeam api.Team
  57. t.Run("CreateTeamForPublicTestOrganization", doAPICreateOrganizationTeam(ctx, orgName, &api.CreateTeamOption{
  58. Name: "test",
  59. Permission: "read",
  60. Units: []string{"repo.code", "repo.issues", "repo.wiki", "repo.pulls", "repo.releases"},
  61. CanCreateOrgRepo: true,
  62. }, func(_ *testing.T, team api.Team) {
  63. testTeam = team
  64. }))
  65. t.Run("AssertNoTestOrgReposForCollaborator", doCheckOrgCounts(orgCollaborator, collabCountRepos, true))
  66. t.Run("AddCollboratorToTeam", doAPIAddUserToOrganizationTeam(ctx, testTeam.ID, orgCollaborator))
  67. collabCountRepos[orgName] = 0
  68. t.Run("AssertNumRepos0ForTestOrgForCollaborator", doCheckOrgCounts(orgOwner, ownerCountRepos, true))
  69. // Now create a Public Repo
  70. t.Run("CreateOrganizationPublicRepo", doAPICreateOrganizationRepository(ctx, orgName, &api.CreateRepoOption{
  71. Name: "publicTestRepo",
  72. AutoInit: true,
  73. }))
  74. ownerCountRepos[orgName] = 2
  75. t.Run("AssertNumRepos2ForTestOrg", doCheckOrgCounts(orgOwner, ownerCountRepos, true))
  76. collabCountRepos[orgName] = 1
  77. t.Run("AssertNumRepos1ForTestOrgForCollaborator", doCheckOrgCounts(orgOwner, ownerCountRepos, true))
  78. // Now add the testTeam to the privateRepo
  79. t.Run("AddTestTeamToPrivateRepo", doAPIAddRepoToOrganizationTeam(ctx, testTeam.ID, orgName, "privateTestRepo"))
  80. t.Run("AssertNumRepos2ForTestOrg", doCheckOrgCounts(orgOwner, ownerCountRepos, true))
  81. collabCountRepos[orgName] = 2
  82. t.Run("AssertNumRepos2ForTestOrgForCollaborator", doCheckOrgCounts(orgOwner, ownerCountRepos, true))
  83. }
  84. func doCheckOrgCounts(username string, orgCounts map[string]int, strict bool, callback ...func(*testing.T, map[string]int)) func(t *testing.T) {
  85. canonicalCounts := make(map[string]int, len(orgCounts))
  86. for key, value := range orgCounts {
  87. newKey := strings.TrimSpace(strings.ToLower(key))
  88. canonicalCounts[newKey] = value
  89. }
  90. return func(t *testing.T) {
  91. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{
  92. Name: username,
  93. })
  94. orgs, err := db.Find[organization.Organization](t.Context(), organization.FindOrgOptions{
  95. UserID: user.ID,
  96. IncludeVisibility: api.VisibleTypePrivate,
  97. })
  98. assert.NoError(t, err)
  99. calcOrgCounts := map[string]int{}
  100. for _, org := range orgs {
  101. calcOrgCounts[org.LowerName] = org.NumRepos
  102. count, ok := canonicalCounts[org.LowerName]
  103. if ok {
  104. assert.Equal(t, count, org.NumRepos, "Number of Repos in %s is %d when we expected %d", org.Name, org.NumRepos, count)
  105. } else {
  106. assert.False(t, strict, "Did not expect to see %s with count %d", org.Name, org.NumRepos)
  107. }
  108. }
  109. for key, value := range orgCounts {
  110. _, seen := calcOrgCounts[strings.TrimSpace(strings.ToLower(key))]
  111. assert.True(t, seen, "Expected to see %s with %d but did not", key, value)
  112. }
  113. if len(callback) > 0 {
  114. callback[0](t, calcOrgCounts)
  115. }
  116. }
  117. }