code.gitea.io/gitea@v1.21.7/tests/integration/org_count_test.go (about)

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