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