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