Skip to content

Commit

Permalink
fix incorrect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Oct 8, 2024
1 parent 6b3992b commit e280fe7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 32 deletions.
2 changes: 2 additions & 0 deletions routers/web/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,8 @@ func handleAccountActivation(ctx *context.Context, user *user_model.User) {
return
}

ctx.Csrf.PrepareForSessionUser(ctx)

if err := resetLocale(ctx, user); err != nil {
ctx.ServerError("resetLocale", err)
return
Expand Down
1 change: 1 addition & 0 deletions services/context/csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func (c *csrfProtector) PrepareForSessionUser(ctx *Context) {

func (c *csrfProtector) validateToken(ctx *Context, token string) {
if !ValidCsrfToken(token, c.opt.Secret, c.id, "POST", time.Now()) {
c.DeleteCookie(ctx)
// currently, there should be no access to the APIPath with CSRF token. because templates shouldn't use the `/api/` endpoints.
// FIXME: distinguish what the response is for: HTML (web page) or JSON (fetch)
http.Error(ctx.Resp, "Invalid CSRF token.", http.StatusBadRequest)
Expand Down
37 changes: 7 additions & 30 deletions tests/integration/org_team_invite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,13 @@ func TestOrgTeamEmailInviteRedirectsNewUserWithActivation(t *testing.T) {
}

// enable email confirmation temporarily
defer func(prevVal bool) {
setting.Service.RegisterEmailConfirm = prevVal
}(setting.Service.RegisterEmailConfirm)
setting.Service.RegisterEmailConfirm = true

defer test.MockVariableValue(&setting.Service.RegisterEmailConfirm, true)()
defer tests.PrepareTestEnv(t)()

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

// create the invite
// user1: create the invite
session := loginUser(t, "user1")

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

// accept the invite
// new user: accept the invite
session = emptyTestSession(t)

inviteURL := fmt.Sprintf("/org/invite/%s", invites[0].Token)
req = NewRequest(t, "GET", fmt.Sprintf("/user/sign_up?redirect_to=%s", url.QueryEscape(inviteURL)))
inviteResp := MakeRequest(t, req, http.StatusOK)

doc := NewHTMLParser(t, resp.Body)
session.MakeRequest(t, req, http.StatusOK)
req = NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
"_csrf": doc.GetCSRF(),
"user_name": "doesnotexist",
"email": "doesnotexist@example.com",
"password": "examplePassword!1",
"retype": "examplePassword!1",
})
for _, c := range inviteResp.Result().Cookies() {
req.AddCookie(c)
}

resp = MakeRequest(t, req, http.StatusOK)
resp = session.MakeRequest(t, req, http.StatusOK)

Check failure on line 272 in tests/integration/org_team_invite_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

ineffectual assignment to resp (ineffassign)

Check failure on line 272 in tests/integration/org_team_invite_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

ineffectual assignment to resp (ineffassign)

Check failure on line 272 in tests/integration/org_team_invite_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

ineffectual assignment to resp (ineffassign)

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

ch := http.Header{}
ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
cr := http.Request{Header: ch}

session = emptyTestSession(t)
baseURL, err := url.Parse(setting.AppURL)
assert.NoError(t, err)
session.jar.SetCookies(baseURL, cr.Cookies())

activateURL := fmt.Sprintf("/user/activate?code=%s", user.GenerateEmailActivateCode("doesnotexist@example.com"))
req = NewRequestWithValues(t, "POST", activateURL, map[string]string{
"password": "examplePassword!1",
})

// use the cookies set by the signup request
for _, c := range inviteResp.Result().Cookies() {
req.AddCookie(c)
}

resp = session.MakeRequest(t, req, http.StatusSeeOther)
// should be redirected to accept the invite
assert.Equal(t, inviteURL, test.RedirectURL(resp))
Expand Down
2 changes: 0 additions & 2 deletions tests/integration/signin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
func testLoginFailed(t *testing.T, username, password, message string) {
session := emptyTestSession(t)
req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{
"_csrf": GetUserCSRFToken(t, session),
"user_name": username,
"password": password,
})
Expand Down Expand Up @@ -68,7 +67,6 @@ func TestSigninWithRememberMe(t *testing.T) {

session := emptyTestSession(t)
req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{
"_csrf": GetUserCSRFToken(t, session),
"user_name": user.Name,
"password": userPassword,
"remember": "on",
Expand Down

0 comments on commit e280fe7

Please sign in to comment.