Skip to content

Commit 35aa33d

Browse files
committed
fix tests
1 parent f9601ec commit 35aa33d

9 files changed

+20
-76
lines changed

models/auth/access_token_scope_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type scopeTestNormalize struct {
1717
}
1818

1919
func TestAccessTokenScope_Normalize(t *testing.T) {
20-
assert.Equal(t, []string([]string{"activitypub", "admin", "issue", "misc", "notification", "organization", "package", "repository", "user"}), GetAccessTokenCategories())
20+
assert.Equal(t, []string{"activitypub", "admin", "issue", "misc", "notification", "organization", "package", "repository", "user"}, GetAccessTokenCategories())
2121
tests := []scopeTestNormalize{
2222
{"", "", nil},
2323
{"write:misc,write:notification,read:package,write:notification,public-only", "public-only,write:misc,write:notification,read:package", nil},

services/context/context.go

+3
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ func Contexter() func(next http.Handler) http.Handler {
215215
func (ctx *Context) HasError() bool {
216216
hasErr, _ := ctx.Data["HasError"].(bool)
217217
hasErr = hasErr || ctx.Flash.ErrorMsg != ""
218+
if !hasErr {
219+
return false
220+
}
218221
if ctx.Flash.ErrorMsg == "" {
219222
ctx.Flash.ErrorMsg = ctx.GetErrMsg()
220223
}

services/forms/user_form_test.go

-27
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
package forms
55

66
import (
7-
"strconv"
87
"testing"
98

10-
auth_model "code.gitea.io/gitea/models/auth"
119
"code.gitea.io/gitea/modules/setting"
1210

1311
"github.com/gobwas/glob"
@@ -104,28 +102,3 @@ func TestRegisterForm_IsDomainAllowed_BlockedEmail(t *testing.T) {
104102
assert.Equal(t, v.valid, form.IsEmailDomainAllowed())
105103
}
106104
}
107-
108-
func TestNewAccessTokenForm_GetScope(t *testing.T) {
109-
tests := []struct {
110-
form NewAccessTokenForm
111-
scope auth_model.AccessTokenScope
112-
expectedErr error
113-
}{
114-
{
115-
form: NewAccessTokenForm{Name: "test", Scope: []string{"read:repository"}},
116-
scope: "read:repository",
117-
},
118-
{
119-
form: NewAccessTokenForm{Name: "test", Scope: []string{"read:repository", "write:user"}},
120-
scope: "read:repository,write:user",
121-
},
122-
}
123-
124-
for i, test := range tests {
125-
t.Run(strconv.Itoa(i), func(t *testing.T) {
126-
scope, err := test.form.GetScope()
127-
assert.Equal(t, test.expectedErr, err)
128-
assert.Equal(t, test.scope, scope)
129-
})
130-
}
131-
}

tests/integration/api_admin_org_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestAPIAdminOrgCreateNotAdmin(t *testing.T) {
7676
defer tests.PrepareTestEnv(t)()
7777
nonAdminUsername := "user2"
7878
session := loginUser(t, nonAdminUsername)
79-
token := getTokenForLoggedInUser(t, session)
79+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll)
8080
org := api.CreateOrgOption{
8181
UserName: "user2_org",
8282
FullName: "User2's organization",

tests/integration/api_admin_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestAPIAdminDeleteUnauthorizedKey(t *testing.T) {
7676
var newPublicKey api.PublicKey
7777
DecodeJSON(t, resp, &newPublicKey)
7878

79-
token = getUserToken(t, normalUsername)
79+
token = getUserToken(t, normalUsername, auth_model.AccessTokenScopeAll)
8080
req = NewRequestf(t, "DELETE", "/api/v1/admin/users/%s/keys/%d", adminUsername, newPublicKey.ID).
8181
AddTokenAuth(token)
8282
MakeRequest(t, req, http.StatusForbidden)
@@ -139,7 +139,7 @@ func TestAPIListUsersNotLoggedIn(t *testing.T) {
139139
func TestAPIListUsersNonAdmin(t *testing.T) {
140140
defer tests.PrepareTestEnv(t)()
141141
nonAdminUsername := "user2"
142-
token := getUserToken(t, nonAdminUsername)
142+
token := getUserToken(t, nonAdminUsername, auth_model.AccessTokenScopeAll)
143143
req := NewRequest(t, "GET", "/api/v1/admin/users").
144144
AddTokenAuth(token)
145145
MakeRequest(t, req, http.StatusForbidden)

tests/integration/api_helper_for_declarative_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ type APITestContext struct {
3333

3434
func NewAPITestContext(t *testing.T, username, reponame string, scope ...auth.AccessTokenScope) APITestContext {
3535
session := loginUser(t, username)
36+
if len(scope) == 0 {
37+
// FIXME: legacy logic: no scope means all
38+
scope = []auth.AccessTokenScope{auth.AccessTokenScopeAll}
39+
}
3640
token := getTokenForLoggedInUser(t, session, scope...)
3741
return APITestContext{
3842
Session: session,

tests/integration/api_repo_git_blobs_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestAPIReposGitBlobs(t *testing.T) {
7272

7373
// Login as User4.
7474
session = loginUser(t, user4.Name)
75-
token4 := getTokenForLoggedInUser(t, session)
75+
token4 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll)
7676

7777
// Test using org repo "org3/repo3" where user4 is a NOT collaborator
7878
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/blobs/d56a3073c1dbb7b15963110a049d50cdb5db99fc?access=%s", org3.Name, repo3.Name, token4)

tests/integration/api_repo_git_trees_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestAPIReposGitTrees(t *testing.T) {
6969

7070
// Login as User4.
7171
session = loginUser(t, user4.Name)
72-
token4 := getTokenForLoggedInUser(t, session)
72+
token4 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll)
7373

7474
// Test using org repo "org3/repo3" where user4 is a NOT collaborator
7575
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/trees/d56a3073c1dbb7b15963110a049d50cdb5db99fc?access=%s", org3.Name, repo3.Name, token4)

tests/integration/integration_test.go

+7-43
Original file line numberDiff line numberDiff line change
@@ -249,55 +249,19 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
249249
// token has to be unique this counter take care of
250250
var tokenCounter int64
251251

252-
// getTokenForLoggedInUser returns a token for a logged in user.
253-
// The scope is an optional list of snake_case strings like the frontend form fields,
254-
// but without the "scope_" prefix.
252+
// getTokenForLoggedInUser returns a token for a logged-in user.
255253
func getTokenForLoggedInUser(t testing.TB, session *TestSession, scopes ...auth.AccessTokenScope) string {
256254
t.Helper()
257-
var token string
258-
req := NewRequest(t, "GET", "/user/settings/applications")
259-
resp := session.MakeRequest(t, req, http.StatusOK)
260-
var csrf string
261-
for _, cookie := range resp.Result().Cookies() {
262-
if cookie.Name != "_csrf" {
263-
continue
264-
}
265-
csrf = cookie.Value
266-
break
267-
}
268-
if csrf == "" {
269-
doc := NewHTMLParser(t, resp.Body)
270-
csrf = doc.GetCSRF()
271-
}
272-
assert.NotEmpty(t, csrf)
273255
urlValues := url.Values{}
274-
urlValues.Add("_csrf", csrf)
256+
urlValues.Add("_csrf", GetUserCSRFToken(t, session))
275257
urlValues.Add("name", fmt.Sprintf("api-testing-token-%d", atomic.AddInt64(&tokenCounter, 1)))
276258
for _, scope := range scopes {
277-
urlValues.Add("scope", string(scope))
259+
urlValues.Add("scope-dummy", string(scope)) // it only needs to start with "scope-" to be accepted
278260
}
279-
req = NewRequestWithURLValues(t, "POST", "/user/settings/applications", urlValues)
280-
resp = session.MakeRequest(t, req, http.StatusSeeOther)
281-
282-
// Log the flash values on failure
283-
if !assert.Equal(t, []string{"/user/settings/applications"}, resp.Result().Header["Location"]) {
284-
for _, cookie := range resp.Result().Cookies() {
285-
if cookie.Name != gitea_context.CookieNameFlash {
286-
continue
287-
}
288-
flash, _ := url.ParseQuery(cookie.Value)
289-
for key, value := range flash {
290-
t.Logf("Flash %q: %q", key, value)
291-
}
292-
}
293-
}
294-
295-
req = NewRequest(t, "GET", "/user/settings/applications")
296-
resp = session.MakeRequest(t, req, http.StatusOK)
297-
htmlDoc := NewHTMLParser(t, resp.Body)
298-
token = htmlDoc.doc.Find(".ui.info p").Text()
299-
assert.NotEmpty(t, token)
300-
return token
261+
req := NewRequestWithURLValues(t, "POST", "/user/settings/applications", urlValues)
262+
session.MakeRequest(t, req, http.StatusSeeOther)
263+
flashes := session.GetCookieFlashMessage()
264+
return flashes.InfoMsg
301265
}
302266

303267
type RequestWrapper struct {

0 commit comments

Comments
 (0)