Skip to content

Commit e280fe7

Browse files
committed
fix incorrect tests
1 parent 6b3992b commit e280fe7

File tree

4 files changed

+10
-32
lines changed

4 files changed

+10
-32
lines changed

routers/web/auth/auth.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,8 @@ func handleAccountActivation(ctx *context.Context, user *user_model.User) {
804804
return
805805
}
806806

807+
ctx.Csrf.PrepareForSessionUser(ctx)
808+
807809
if err := resetLocale(ctx, user); err != nil {
808810
ctx.ServerError("resetLocale", err)
809811
return

services/context/csrf.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func (c *csrfProtector) PrepareForSessionUser(ctx *Context) {
139139

140140
func (c *csrfProtector) validateToken(ctx *Context, token string) {
141141
if !ValidCsrfToken(token, c.opt.Secret, c.id, "POST", time.Now()) {
142+
c.DeleteCookie(ctx)
142143
// currently, there should be no access to the APIPath with CSRF token. because templates shouldn't use the `/api/` endpoints.
143144
// FIXME: distinguish what the response is for: HTML (web page) or JSON (fetch)
144145
http.Error(ctx.Resp, "Invalid CSRF token.", http.StatusBadRequest)

tests/integration/org_team_invite_test.go

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,13 @@ func TestOrgTeamEmailInviteRedirectsNewUserWithActivation(t *testing.T) {
233233
}
234234

235235
// enable email confirmation temporarily
236-
defer func(prevVal bool) {
237-
setting.Service.RegisterEmailConfirm = prevVal
238-
}(setting.Service.RegisterEmailConfirm)
239-
setting.Service.RegisterEmailConfirm = true
240-
236+
defer test.MockVariableValue(&setting.Service.RegisterEmailConfirm, true)()
241237
defer tests.PrepareTestEnv(t)()
242238

243239
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
244240
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2})
245241

246-
// create the invite
242+
// user1: create the invite
247243
session := loginUser(t, "user1")
248244

249245
teamURL := fmt.Sprintf("/org/%s/teams/%s", org.Name, team.Name)
@@ -261,47 +257,28 @@ func TestOrgTeamEmailInviteRedirectsNewUserWithActivation(t *testing.T) {
261257
assert.NoError(t, err)
262258
assert.Len(t, invites, 1)
263259

264-
// accept the invite
260+
// new user: accept the invite
261+
session = emptyTestSession(t)
262+
265263
inviteURL := fmt.Sprintf("/org/invite/%s", invites[0].Token)
266264
req = NewRequest(t, "GET", fmt.Sprintf("/user/sign_up?redirect_to=%s", url.QueryEscape(inviteURL)))
267-
inviteResp := MakeRequest(t, req, http.StatusOK)
268-
269-
doc := NewHTMLParser(t, resp.Body)
265+
session.MakeRequest(t, req, http.StatusOK)
270266
req = NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
271-
"_csrf": doc.GetCSRF(),
272267
"user_name": "doesnotexist",
273268
"email": "[email protected]",
274269
"password": "examplePassword!1",
275270
"retype": "examplePassword!1",
276271
})
277-
for _, c := range inviteResp.Result().Cookies() {
278-
req.AddCookie(c)
279-
}
280-
281-
resp = MakeRequest(t, req, http.StatusOK)
272+
resp = session.MakeRequest(t, req, http.StatusOK)
282273

283274
user, err := user_model.GetUserByName(db.DefaultContext, "doesnotexist")
284275
assert.NoError(t, err)
285276

286-
ch := http.Header{}
287-
ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
288-
cr := http.Request{Header: ch}
289-
290-
session = emptyTestSession(t)
291-
baseURL, err := url.Parse(setting.AppURL)
292-
assert.NoError(t, err)
293-
session.jar.SetCookies(baseURL, cr.Cookies())
294-
295277
activateURL := fmt.Sprintf("/user/activate?code=%s", user.GenerateEmailActivateCode("[email protected]"))
296278
req = NewRequestWithValues(t, "POST", activateURL, map[string]string{
297279
"password": "examplePassword!1",
298280
})
299281

300-
// use the cookies set by the signup request
301-
for _, c := range inviteResp.Result().Cookies() {
302-
req.AddCookie(c)
303-
}
304-
305282
resp = session.MakeRequest(t, req, http.StatusSeeOther)
306283
// should be redirected to accept the invite
307284
assert.Equal(t, inviteURL, test.RedirectURL(resp))

tests/integration/signin_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
func testLoginFailed(t *testing.T, username, password, message string) {
2222
session := emptyTestSession(t)
2323
req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{
24-
"_csrf": GetUserCSRFToken(t, session),
2524
"user_name": username,
2625
"password": password,
2726
})
@@ -68,7 +67,6 @@ func TestSigninWithRememberMe(t *testing.T) {
6867

6968
session := emptyTestSession(t)
7069
req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{
71-
"_csrf": GetUserCSRFToken(t, session),
7270
"user_name": user.Name,
7371
"password": userPassword,
7472
"remember": "on",

0 commit comments

Comments
 (0)