summaryrefslogtreecommitdiffstats
path: root/tests/integration/repo_flags_test.go
blob: 8b64776a5ab498d1695f334b83a3160485f7b2f3 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"slices"
	"testing"

	auth_model "code.gitea.io/gitea/models/auth"
	"code.gitea.io/gitea/models/db"
	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/setting"
	api "code.gitea.io/gitea/modules/structs"
	"code.gitea.io/gitea/modules/test"
	"code.gitea.io/gitea/routers"
	"code.gitea.io/gitea/tests"

	"github.com/stretchr/testify/assert"
)

func TestRepositoryFlagsUIDisabled(t *testing.T) {
	defer tests.PrepareTestEnv(t)()
	defer test.MockVariableValue(&setting.Repository.EnableFlags, false)()
	defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()

	admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{IsAdmin: true})
	session := loginUser(t, admin.Name)

	// With the repo flags feature disabled, the /flags route is 404
	req := NewRequest(t, "GET", "/user2/repo1/flags")
	session.MakeRequest(t, req, http.StatusNotFound)

	// With the repo flags feature disabled, the "Modify flags" tab does not
	// appear for instance admins
	req = NewRequest(t, "GET", "/user2/repo1")
	resp := session.MakeRequest(t, req, http.StatusOK)
	doc := NewHTMLParser(t, resp.Body)
	flagsLinkCount := doc.Find(fmt.Sprintf(`a[href="%s/flags"]`, "/user2/repo1")).Length()
	assert.Equal(t, 0, flagsLinkCount)
}

func TestRepositoryFlagsAPI(t *testing.T) {
	defer tests.PrepareTestEnv(t)()
	defer test.MockVariableValue(&setting.Repository.EnableFlags, true)()
	defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()

	// *************
	// ** Helpers **
	// *************

	adminUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{IsAdmin: true}).Name
	normalUserBean := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
	assert.False(t, normalUserBean.IsAdmin)
	normalUser := normalUserBean.Name

	assertAccess := func(t *testing.T, user, method, uri string, expectedStatus int) {
		session := loginUser(t, user)
		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeReadAdmin)

		req := NewRequestf(t, method, "/api/v1/repos/user2/repo1/flags%s", uri).AddTokenAuth(token)
		MakeRequest(t, req, expectedStatus)
	}

	// ***********
	// ** Tests **
	// ***********

	t.Run("API access", func(t *testing.T) {
		t.Run("as admin", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			assertAccess(t, adminUser, "GET", "", http.StatusOK)
		})

		t.Run("as normal user", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			assertAccess(t, normalUser, "GET", "", http.StatusForbidden)
		})
	})

	t.Run("token scopes", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()

		// Trying to access the API with a token that lacks permissions, will
		// fail, even if the token owner is an instance admin.
		session := loginUser(t, adminUser)
		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)

		req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/flags").AddTokenAuth(token)
		MakeRequest(t, req, http.StatusForbidden)
	})

	t.Run("setting.Repository.EnableFlags is respected", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()
		defer test.MockVariableValue(&setting.Repository.EnableFlags, false)()
		defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()

		t.Run("as admin", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			assertAccess(t, adminUser, "GET", "", http.StatusNotFound)
		})

		t.Run("as normal user", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			assertAccess(t, normalUser, "GET", "", http.StatusNotFound)
		})
	})

	t.Run("API functionality", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()
		repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
		defer func() {
			repo.ReplaceAllFlags(db.DefaultContext, []string{})
		}()

		baseURLFmtStr := "/api/v1/repos/user5/repo4/flags%s"

		session := loginUser(t, adminUser)
		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteAdmin)

		// Listing flags
		req := NewRequestf(t, "GET", baseURLFmtStr, "").AddTokenAuth(token)
		resp := MakeRequest(t, req, http.StatusOK)
		var flags []string
		DecodeJSON(t, resp, &flags)
		assert.Empty(t, flags)

		// Replacing all tags works, twice in a row
		for i := 0; i < 2; i++ {
			req = NewRequestWithJSON(t, "PUT", fmt.Sprintf(baseURLFmtStr, ""), &api.ReplaceFlagsOption{
				Flags: []string{"flag-1", "flag-2", "flag-3"},
			}).AddTokenAuth(token)
			MakeRequest(t, req, http.StatusNoContent)
		}

		// The list now includes all three flags
		req = NewRequestf(t, "GET", baseURLFmtStr, "").AddTokenAuth(token)
		resp = MakeRequest(t, req, http.StatusOK)
		DecodeJSON(t, resp, &flags)
		assert.Len(t, flags, 3)
		for _, flag := range []string{"flag-1", "flag-2", "flag-3"} {
			assert.True(t, slices.Contains(flags, flag))
		}

		// Check a flag that is on the repo
		req = NewRequestf(t, "GET", baseURLFmtStr, "/flag-1").AddTokenAuth(token)
		MakeRequest(t, req, http.StatusNoContent)

		// Check a flag that isn't on the repo
		req = NewRequestf(t, "GET", baseURLFmtStr, "/no-such-flag").AddTokenAuth(token)
		MakeRequest(t, req, http.StatusNotFound)

		// We can add the same flag twice
		for i := 0; i < 2; i++ {
			req = NewRequestf(t, "PUT", baseURLFmtStr, "/brand-new-flag").AddTokenAuth(token)
			MakeRequest(t, req, http.StatusNoContent)
		}

		// The new flag is there
		req = NewRequestf(t, "GET", baseURLFmtStr, "/brand-new-flag").AddTokenAuth(token)
		MakeRequest(t, req, http.StatusNoContent)

		// We can delete a flag, twice
		for i := 0; i < 2; i++ {
			req = NewRequestf(t, "DELETE", baseURLFmtStr, "/flag-3").AddTokenAuth(token)
			MakeRequest(t, req, http.StatusNoContent)
		}

		// We can delete a flag that wasn't there
		req = NewRequestf(t, "DELETE", baseURLFmtStr, "/no-such-flag").AddTokenAuth(token)
		MakeRequest(t, req, http.StatusNoContent)

		// We can delete all of the flags in one go, too
		req = NewRequestf(t, "DELETE", baseURLFmtStr, "").AddTokenAuth(token)
		MakeRequest(t, req, http.StatusNoContent)

		// ..once all flags are deleted, none are listed, either
		req = NewRequestf(t, "GET", baseURLFmtStr, "").AddTokenAuth(token)
		resp = MakeRequest(t, req, http.StatusOK)
		DecodeJSON(t, resp, &flags)
		assert.Empty(t, flags)
	})
}

func TestRepositoryFlagsUI(t *testing.T) {
	defer tests.PrepareTestEnv(t)()
	defer test.MockVariableValue(&setting.Repository.EnableFlags, true)()
	defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()

	// *******************
	//  ** Preparations **
	// *******************
	flaggedRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
	unflaggedRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})

	// **************
	//  ** Helpers **
	// **************

	adminUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{IsAdmin: true}).Name
	flaggedOwner := "user2"
	flaggedRepoURLStr := "/user2/repo1"
	unflaggedOwner := "user5"
	unflaggedRepoURLStr := "/user5/repo4"
	otherUser := "user4"

	ensureFlags := func(repo *repo_model.Repository, flags []string) func() {
		repo.ReplaceAllFlags(db.DefaultContext, flags)

		return func() {
			repo.ReplaceAllFlags(db.DefaultContext, flags)
		}
	}

	// Tests:
	// - Presence of the link
	// - Number of flags listed in the admin-only message box
	// - Whether there's a link to /user/repo/flags
	// - Whether /user/repo/flags is OK or Forbidden
	assertFlagAccessAndCount := func(t *testing.T, user, repoURL string, hasAccess bool, expectedFlagCount int) {
		t.Helper()

		var expectedLinkCount int
		var expectedStatus int
		if hasAccess {
			expectedLinkCount = 1
			expectedStatus = http.StatusOK
		} else {
			expectedLinkCount = 0
			if user != "" {
				expectedStatus = http.StatusForbidden
			} else {
				expectedStatus = http.StatusSeeOther
			}
		}

		var resp *httptest.ResponseRecorder
		var session *TestSession
		req := NewRequest(t, "GET", repoURL)
		if user != "" {
			session = loginUser(t, user)
			resp = session.MakeRequest(t, req, http.StatusOK)
		} else {
			resp = MakeRequest(t, req, http.StatusOK)
		}
		doc := NewHTMLParser(t, resp.Body)

		flagsLinkCount := doc.Find(fmt.Sprintf(`a[href="%s/flags"]`, repoURL)).Length()
		assert.Equal(t, expectedLinkCount, flagsLinkCount)

		flagCount := doc.Find(".ui.info.message .ui.label").Length()
		assert.Equal(t, expectedFlagCount, flagCount)

		req = NewRequest(t, "GET", fmt.Sprintf("%s/flags", repoURL))
		if user != "" {
			session.MakeRequest(t, req, expectedStatus)
		} else {
			MakeRequest(t, req, expectedStatus)
		}
	}

	// Ensures that given a repo owner and a repo:
	// - An instance admin has access to flags, and sees the list on the repo home
	// - A repo admin does not have access to either, and does not see the list
	// - A passer by has no access to either, and does not see the list
	runTests := func(t *testing.T, ownerUser, repoURL string, expectedFlagCount int) {
		t.Run("as instance admin", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			assertFlagAccessAndCount(t, adminUser, repoURL, true, expectedFlagCount)
		})
		t.Run("as owner", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			assertFlagAccessAndCount(t, ownerUser, repoURL, false, 0)
		})
		t.Run("as other user", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			assertFlagAccessAndCount(t, otherUser, repoURL, false, 0)
		})
		t.Run("as non-logged in user", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			assertFlagAccessAndCount(t, "", repoURL, false, 0)
		})
	}

	// **************************
	// ** The tests themselves **
	// **************************
	t.Run("unflagged repo", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()
		defer ensureFlags(unflaggedRepo, []string{})()

		runTests(t, unflaggedOwner, unflaggedRepoURLStr, 0)
	})

	t.Run("flagged repo", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()
		defer ensureFlags(flaggedRepo, []string{"test-flag"})()

		runTests(t, flaggedOwner, flaggedRepoURLStr, 1)
	})

	t.Run("modifying flags", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()

		session := loginUser(t, adminUser)
		flaggedRepoManageURL := fmt.Sprintf("%s/flags", flaggedRepoURLStr)
		unflaggedRepoManageURL := fmt.Sprintf("%s/flags", unflaggedRepoURLStr)

		assertUIFlagStates := func(t *testing.T, url string, flagStates map[string]bool) {
			t.Helper()

			req := NewRequest(t, "GET", url)
			resp := session.MakeRequest(t, req, http.StatusOK)

			doc := NewHTMLParser(t, resp.Body)
			flagBoxes := doc.Find(`input[name="flags"]`)
			assert.Equal(t, len(flagStates), flagBoxes.Length())

			for name, state := range flagStates {
				_, checked := doc.Find(fmt.Sprintf(`input[value="%s"]`, name)).Attr("checked")
				assert.Equal(t, state, checked)
			}
		}

		t.Run("flag presence on the UI", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()
			defer ensureFlags(flaggedRepo, []string{"test-flag"})()

			assertUIFlagStates(t, flaggedRepoManageURL, map[string]bool{"test-flag": true})
		})

		t.Run("setting.Repository.SettableFlags is respected", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()
			defer test.MockVariableValue(&setting.Repository.SettableFlags, []string{"featured", "no-license"})()
			defer ensureFlags(flaggedRepo, []string{"test-flag"})()

			assertUIFlagStates(t, flaggedRepoManageURL, map[string]bool{
				"test-flag":  true,
				"featured":   false,
				"no-license": false,
			})
		})

		t.Run("removing flags", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()
			defer ensureFlags(flaggedRepo, []string{"test-flag"})()

			flagged := flaggedRepo.IsFlagged(db.DefaultContext)
			assert.True(t, flagged)

			req := NewRequestWithValues(t, "POST", flaggedRepoManageURL, map[string]string{
				"_csrf": GetCSRF(t, session, flaggedRepoManageURL),
			})
			session.MakeRequest(t, req, http.StatusSeeOther)

			flagged = flaggedRepo.IsFlagged(db.DefaultContext)
			assert.False(t, flagged)

			assertUIFlagStates(t, flaggedRepoManageURL, map[string]bool{})
		})

		t.Run("adding flags", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()
			defer ensureFlags(unflaggedRepo, []string{})()

			flagged := unflaggedRepo.IsFlagged(db.DefaultContext)
			assert.False(t, flagged)

			req := NewRequestWithValues(t, "POST", unflaggedRepoManageURL, map[string]string{
				"_csrf": GetCSRF(t, session, unflaggedRepoManageURL),
				"flags": "test-flag",
			})
			session.MakeRequest(t, req, http.StatusSeeOther)

			assertUIFlagStates(t, unflaggedRepoManageURL, map[string]bool{"test-flag": true})
		})
	})
}