Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

org_count_test.go 4.7KB

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