summaryrefslogtreecommitdiffstats
path: root/tests/integration/user_count_test.go
blob: e76c30c1d4cd312c551624e5786e6c01ec76f12d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"fmt"
	"net/http"
	"strconv"
	"testing"

	"code.gitea.io/gitea/models/db"
	"code.gitea.io/gitea/models/organization"
	packages_model "code.gitea.io/gitea/models/packages"
	project_model "code.gitea.io/gitea/models/project"
	repo_model "code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/optional"
	"code.gitea.io/gitea/tests"

	"github.com/PuerkitoBio/goquery"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type userCountTest struct {
	doer         *user_model.User
	user         *user_model.User
	session      *TestSession
	repoCount    int64
	projectCount int64
	packageCount int64
	memberCount  int64
	teamCount    int64
}

func (countTest *userCountTest) Init(t *testing.T, doerID, userID int64) {
	countTest.doer = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doerID})
	countTest.user = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID})
	countTest.session = loginUser(t, countTest.doer.Name)

	var err error

	countTest.repoCount, err = repo_model.CountRepository(db.DefaultContext, &repo_model.SearchRepoOptions{
		Actor:       countTest.doer,
		OwnerID:     countTest.user.ID,
		Private:     true,
		Collaborate: optional.Some(false),
	})
	require.NoError(t, err)

	var projectType project_model.Type
	if countTest.user.IsOrganization() {
		projectType = project_model.TypeOrganization
	} else {
		projectType = project_model.TypeIndividual
	}
	countTest.projectCount, err = db.Count[project_model.Project](db.DefaultContext, &project_model.SearchOptions{
		OwnerID:  countTest.user.ID,
		IsClosed: optional.Some(false),
		Type:     projectType,
	})
	require.NoError(t, err)
	countTest.packageCount, err = packages_model.CountOwnerPackages(db.DefaultContext, countTest.user.ID)
	require.NoError(t, err)

	if !countTest.user.IsOrganization() {
		return
	}

	org := (*organization.Organization)(countTest.user)

	isMember, err := org.IsOrgMember(db.DefaultContext, countTest.doer.ID)
	require.NoError(t, err)

	countTest.memberCount, err = organization.CountOrgMembers(db.DefaultContext, &organization.FindOrgMembersOpts{
		OrgID:      org.ID,
		PublicOnly: !isMember,
	})
	require.NoError(t, err)

	teams, err := org.LoadTeams(db.DefaultContext)
	require.NoError(t, err)

	countTest.teamCount = int64(len(teams))
}

func (countTest *userCountTest) getCount(doc *goquery.Document, name string) (int64, error) {
	selection := doc.Find(fmt.Sprintf("[test-name=\"%s\"]", name))

	if selection.Length() != 1 {
		return 0, fmt.Errorf("%s was not found", name)
	}

	return strconv.ParseInt(selection.Text(), 10, 64)
}

func (countTest *userCountTest) TestPage(t *testing.T, page string, orgLink bool) {
	t.Run(page, func(t *testing.T) {
		var userLink string

		if orgLink {
			userLink = countTest.user.OrganisationLink()
		} else {
			userLink = countTest.user.HomeLink()
		}

		req := NewRequestf(t, "GET", "%s/%s", userLink, page)
		resp := countTest.session.MakeRequest(t, req, http.StatusOK)
		htmlDoc := NewHTMLParser(t, resp.Body)

		repoCount, err := countTest.getCount(htmlDoc.doc, "repository-count")
		require.NoError(t, err)
		assert.Equal(t, countTest.repoCount, repoCount)

		projectCount, err := countTest.getCount(htmlDoc.doc, "project-count")
		require.NoError(t, err)
		assert.Equal(t, countTest.projectCount, projectCount)

		packageCount, err := countTest.getCount(htmlDoc.doc, "package-count")
		require.NoError(t, err)
		assert.Equal(t, countTest.packageCount, packageCount)

		if !countTest.user.IsOrganization() {
			return
		}

		memberCount, err := countTest.getCount(htmlDoc.doc, "member-count")
		require.NoError(t, err)
		assert.Equal(t, countTest.memberCount, memberCount)

		teamCount, err := countTest.getCount(htmlDoc.doc, "team-count")
		require.NoError(t, err)
		assert.Equal(t, countTest.teamCount, teamCount)
	})
}

func TestFrontendHeaderCountUser(t *testing.T) {
	defer tests.PrepareTestEnv(t)()

	countTest := new(userCountTest)
	countTest.Init(t, 2, 2)

	countTest.TestPage(t, "", false)
	countTest.TestPage(t, "?tab=repositories", false)
	countTest.TestPage(t, "-/projects", false)
	countTest.TestPage(t, "-/packages", false)
	countTest.TestPage(t, "?tab=activity", false)
	countTest.TestPage(t, "?tab=stars", false)
}

func TestFrontendHeaderCountOrg(t *testing.T) {
	defer tests.PrepareTestEnv(t)()

	countTest := new(userCountTest)
	countTest.Init(t, 15, 17)

	countTest.TestPage(t, "", false)
	countTest.TestPage(t, "-/projects", false)
	countTest.TestPage(t, "-/packages", false)
	countTest.TestPage(t, "members", true)
	countTest.TestPage(t, "teams", true)

	countTest.TestPage(t, "settings", true)
	countTest.TestPage(t, "settings/hooks", true)
	countTest.TestPage(t, "settings/labels", true)
	countTest.TestPage(t, "settings/applications", true)
	countTest.TestPage(t, "settings/packages", true)
	countTest.TestPage(t, "settings/actions/runners", true)
	countTest.TestPage(t, "settings/actions/secrets", true)
	countTest.TestPage(t, "settings/actions/variables", true)
	countTest.TestPage(t, "settings/blocked_users", true)
	countTest.TestPage(t, "settings/delete", true)
}