Skip to content

Commit 1f77ce3

Browse files
committed
fix test
1 parent f9601ec commit 1f77ce3

File tree

4 files changed

+11
-71
lines changed

4 files changed

+11
-71
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/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)