8000 fix · go-gitea/gitea@2f6d496 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f6d496

Browse files
committed
fix
1 parent 340d9ec commit 2f6d496

File tree

8 files changed

+61
-41
lines changed

8 files changed

+61
-41
lines changed

modules/web/middleware/flash.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package middleware
66
import (
77
"fmt"
88
"html/template"
9+
"net/http"
910
"net/url"
1011

1112
"code.gitea.io/gitea/modules/reqctx"
@@ -65,3 +66,27 @@ func (f *Flash) Success(msg any, current ...bool) {
6566
f.SuccessMsg = flashMsgStringOrHTML(msg)
6667
f.set("success", f.SuccessMsg, current...)
6768
}
69+
70+
func ParseCookieFlashMessage(val string) *Flash {
71+
if vals, _ := url.ParseQuery(val); len(vals) > 0 {
72+
return &Flash{
73+
Values: vals,
74+
ErrorMsg: vals.Get("error"),
75+
SuccessMsg: vals.Get("success"),
76+
InfoMsg: vals.Get("info"),
77+
WarningMsg: vals.Get("warning"),
78+
}
79+
}
80+
return nil
81+
}
82+
83+
func GetSiteCookieFlashMessage(dataStore reqctx.RequestDataStore, req *http.Request, cookieName string) (string, *Flash) {
84+
// Get the last flash message from cookie
85+
lastFlashCookie := GetSiteCookie(req, cookieName)
86+
lastFlashMsg := ParseCookieFlashMessage(lastFlashCookie)
87+
if lastFlashMsg != nil {
88+
lastFlashMsg.DataStore = dataStore
89+
return lastFlashCookie, lastFlashMsg
90+
}
91+
return lastFlashCookie, nil
92+
}

services/context/context.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,10 @@ func Contexter() func(next http.Handler) http.Handler {
165165
ctx.Base.SetContextValue(WebContextKey, ctx)
166166
ctx.Csrf = NewCSRFProtector(csrfOpts)
167167

168-
// Get the last flash message from cookie
169-
lastFlashCookie := middleware.GetSiteCookie(ctx.Req, CookieNameFlash)
168+
// get the last flash message from cookie
169+
lastFlashCookie, lastFlashMsg := middleware.GetSiteCookieFlashMessage(ctx, ctx.Req, CookieNameFlash)
170170
if vals, _ := url.ParseQuery(lastFlashCookie); len(vals) > 0 {
171-
// store last Flash message into the template data, to render it
172-
ctx.Data["Flash"] = &middleware.Flash{
173-
DataStore: ctx,
174-
Values: vals,
175-
ErrorMsg: vals.Get("error"),
176-
SuccessMsg: vals.Get("success"),
177-
InfoMsg: vals.Get("info"),
178-
WarningMsg: vals.Get("warning"),
179-
}
171+
ctx.Data["Flash"] = lastFlashMsg // store last Flash message into the template data, to render it
180172
}
181173

182174
// if there are new messages in the ctx.Flash, write them into cookie

tests/integration/editor_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"testing"
1313

1414
"code.gitea.io/gitea/modules/json"
15-
gitea_context "code.gitea.io/gitea/services/context"
1615

1716
"github.com/stretchr/testify/assert"
1817
)
@@ -58,9 +57,8 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
5857
})
5958
session.MakeRequest(t, req, http.StatusSeeOther)
6059
// Check if master branch has been locked successfully
61-
flashCookie := session.GetCookie(gitea_context.CookieNameFlash)
62-
assert.NotNil(t, flashCookie)
63-
assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Brule%2B%2522master%2522%2Bhas%2Bbeen%2Bupdated.", flashCookie.Value)
60+
flashMsg := session.GetCookieFlashMessage()
61+
assert.EqualValues(t, `Branch protection for rule "master" has been updated.`, flashMsg.SuccessMsg)
6462

6563
// Request editor page
6664
req = NewRequest(t, "GET", "/user2/repo1/_new/master/")
@@ -98,9 +96,8 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
9896
assert.EqualValues(t, "/user2/repo1/settings/branches", res["redirect"])
9997

10098
// Check if master branch has been locked successfully
101-
flashCookie = session.GetCookie(gitea_context.CookieNameFlash)
102-
assert.NotNil(t, flashCookie)
103-
assert.EqualValues(t, "error%3DRemoving%2Bbranch%2Bprotection%2Brule%2B%25221%2522%2Bfailed.", flashCookie.Value)
99+
flashMsg = session.GetCookieFlashMessage()
100+
assert.EqualValues(t, `Removing branch protection rule "1" failed.`, flashMsg.ErrorMsg)
104101
})
105102
}
106103

tests/integration/git_general_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"code.gitea.io/gitea/modules/lfs"
2929
"code.gitea.io/gitea/modules/setting"
3030
api "code.gitea.io/gitea/modules/structs"
31-
gitea_context "code.gitea.io/gitea/services/context"
3231
"code.gitea.io/gitea/tests"
3332

3433
"github.com/stretchr/testify/assert"
@@ -464,9 +463,8 @@ func doProtectBranch(ctx APITestContext, branch, userToWhitelistPush, userToWhit
464463
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
465464

466465
// Check if master branch has been locked successfully
467-
flashCookie := ctx.Session.GetCookie(gitea_context.CookieNameFlash)
468-
assert.NotNil(t, flashCookie)
469-
assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Brule%2B%2522"+url.QueryEscape(branch)+"%2522%2Bhas%2Bbeen%2Bupdated.", flashCookie.Value)
466+
flashMsg := ctx.Session.GetCookieFlashMessage()
467+
assert.EqualValues(t, `Branch protection for rule "`+url.QueryEscape(branch)+`" has been updated.`, flashMsg.SuccessMsg)
470468
}
471469
}
472470

tests/integration/integration_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"code.gitea.io/gitea/modules/setting"
3030
"code.gitea.io/gitea/modules/util"
3131
"code.gitea.io/gitea/modules/web"
32+
"code.gitea.io/gitea/modules/web/middleware"
3233
"code.gitea.io/gitea/routers"
3334
gitea_context "code.gitea.io/gitea/services/context"
3435
"code.gitea.io/gitea/tests"
@@ -118,12 +119,11 @@ type TestSession struct {
118119
jar http.CookieJar
119120
}
120121

121-
func (s *TestSession) GetCookie(name string) *http.Cookie {
122+
func (s *TestSession) GetRawCookie(name string) *http.Cookie {
122123
baseURL, err := url.Parse(setting.AppURL)
123124
if err != nil {
124125
return nil
125126
}
126-
127127
for _, c := range s.jar.Cookies(baseURL) {
128128
if c.Name == name {
129129
return c
@@ -132,6 +132,20 @@ func (s *TestSession) GetCookie(name string) *http.Cookie {
132132
return nil
133133
}
134134

135+
func (s *TestSession) GetSiteCookie(name string) string {
136+
c := s.GetRawCookie(name)
137+
if c != nil {
138+
v, _ := url.QueryUnescape(c.Value)
139+
return v
140+
}
141+
return ""
142+
}
143+
144+
func (s *TestSession) GetCookieFlashMessage() *middleware.Flash {
145+
cookie := s.GetSiteCookie(gitea_context.CookieNameFlash)
146+
return middleware.ParseCookieFlashMessage(cookie)
147+
}
148+
135149
func (s *TestSession) MakeRequest(t testing.TB, rw *RequestWrapper, expectedStatus int) *httptest.ResponseRecorder {
136150
t.Helper()
137151
req := rw.Request
@@ -458,9 +472,9 @@ func VerifyJSONSchema(t testing.TB, resp *httptest.ResponseRecorder, schemaFile
458472
// GetUserCSRFToken returns CSRF token for current user
459473
func GetUserCSRFToken(t testing.TB, session *TestSession) string {
460474
t.Helper()
461-
cookie := session.GetCookie("_csrf")
475+
cookie := session.GetSiteCookie("_csrf")
462476
require.NotEmpty(t, cookie)
463-
return cookie.Value
477+
return cookie
464478
}
465479

466480
// GetUserCSRFToken returns CSRF token for anonymous user (not logged in)

tests/integration/mirror_push_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"net/http"
1010
"net/url"
1111
"strconv"
12-
"strings"
1312
"testing"
1413
"time"
1514

@@ -20,7 +19,6 @@ import (
2019
"code.gitea.io/gitea/modules/git"
2120
"code.gitea.io/gitea/modules/gitrepo"
2221
"code.gitea.io/gitea/modules/setting"
23-
gitea_context "code.gitea.io/gitea/services/context"
2422
"code.gitea.io/gitea/services/migrations"
2523
mirror_service "code.gitea.io/gitea/services/mirror"
2624
repo_service "code.gitea.io/gitea/services/repository"
@@ -92,9 +90,8 @@ func testCreatePushMirror(t *testing.T, session *TestSession, owner, repo, addre
9290
})
9391
session.MakeRequest(t, req, http.StatusSeeOther)
9492

95-
flashCookie := session.GetCookie(gitea_context.CookieNameFlash)
96-
assert.NotNil(t, flashCookie)
97-
assert.Contains(t, flashCookie.Value, "success")
93+
flashMsg := session.GetCookieFlashMessage()
94+
assert.NotEmpty(t, flashMsg.SuccessMsg)
9895
}
9996

10097
func doRemovePushMirror(t *testing.T, session *TestSession, owner, repo string, pushMirrorID int64) bool {
@@ -104,8 +101,8 @@ func doRemovePushMirror(t *testing.T, session *TestSession, owner, repo string,
104101
"push_mirror_id": strconv.FormatInt(pushMirrorID, 10),
105102
})
106103
resp := session.MakeRequest(t, req, NoExpectedStatus)
107-
flashCookie := session.GetCookie(gitea_context.CookieNameFlash)
108-
return resp.Code == http.StatusSeeOther && flashCookie != nil && strings.Contains(flashCookie.Value, "success")
104+
flashMsg := session.GetCookieFlashMessage()
105+
return resp.Code == http.StatusSeeOther && assert.NotEmpty(t, flashMsg.SuccessMsg)
109106
}
110107

111108
func doUpdatePushMirror(t *testing.T, session *TestSession, owner, repo string, pushMirrorID int64, interval string) bool {

tests/integration/rename_branch_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
git_model "code.gitea.io/gitea/models/git"
1212
repo_model "code.gitea.io/gitea/models/repo"
1313
"code.gitea.io/gitea/models/unittest"
14-
gitea_context "code.gitea.io/gitea/services/context"
1514
"code.gitea.io/gitea/tests"
1615

1716
"github.com/stretchr/testify/assert"
@@ -82,9 +81,8 @@ func testRenameBranch(t *testing.T, u *url.URL) {
8281
"to": "branch1",
8382
})
8483
session.MakeRequest(t, req, http.StatusSeeOther)
85-
flashCookie := session.GetCookie(gitea_context.CookieNameFlash)
86-
assert.NotNil(t, flashCookie)
87-
assert.Contains(t, flashCookie.Value, "error")
84+
flashMsg := session.GetCookieFlashMessage()
85+
assert.NotEmpty(t, flashMsg.ErrorMsg)
8886

8987
branch2 = unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch2"})
9088
assert.Equal(t, "branch2", branch2.Name)
@@ -110,9 +108,8 @@ func testRenameBranch(t *testing.T, u *url.URL) {
110108
})
111109
session.MakeRequest(t, req, http.StatusSeeOther)
112110

113-
flashCookie = session.GetCookie(gitea_context.CookieNameFlash)
114-
assert.NotNil(t, flashCookie)
115-
assert.Contains(t, flashCookie.Value, "success")
111+
flashMsg = session.GetCookieFlashMessage()
112+
assert.NotEmpty(t, flashMsg.SuccessMsg)
116113

117114
unittest.AssertNotExistsBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch2"})
118115
branch1 = unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch1"})

tests/integration/signin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestSigninWithRememberMe(t *testing.T) {
7979
})
8080
session.MakeRequest(t, req, http.StatusSeeOther)
8181

82-
c := session.GetCookie(setting.CookieRememberName)
82+
c := session.GetRawCookie(setting.CookieRememberName)
8383
assert.NotNil(t, c)
8484

8585
session = emptyTestSession(t)

0 commit comments

Comments
 (0)
0