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.

140 lines
4.6 KiB

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