aboutsummaryrefslogtreecommitdiffstats
path: root/tests/integration/integration_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'tests/integration/integration_test.go')
-rw-r--r--tests/integration/integration_test.go80
1 files changed, 29 insertions, 51 deletions
diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go
index 46b93b0a10..21126b63c5 100644
--- a/tests/integration/integration_test.go
+++ b/tests/integration/integration_test.go
@@ -1,7 +1,7 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
-//nolint:forbidigo
+//nolint:forbidigo // use of print functions is allowed in tests
package integration
import (
@@ -29,6 +29,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
+ "code.gitea.io/gitea/modules/web/middleware"
"code.gitea.io/gitea/routers"
gitea_context "code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/tests"
@@ -118,12 +119,11 @@ type TestSession struct {
jar http.CookieJar
}
-func (s *TestSession) GetCookie(name string) *http.Cookie {
+func (s *TestSession) GetRawCookie(name string) *http.Cookie {
baseURL, err := url.Parse(setting.AppURL)
if err != nil {
return nil
}
-
for _, c := range s.jar.Cookies(baseURL) {
if c.Name == name {
return c
@@ -132,6 +132,20 @@ func (s *TestSession) GetCookie(name string) *http.Cookie {
return nil
}
+func (s *TestSession) GetSiteCookie(name string) string {
+ c := s.GetRawCookie(name)
+ if c != nil {
+ v, _ := url.QueryUnescape(c.Value)
+ return v
+ }
+ return ""
+}
+
+func (s *TestSession) GetCookieFlashMessage() *middleware.Flash {
+ cookie := s.GetSiteCookie(gitea_context.CookieNameFlash)
+ return middleware.ParseCookieFlashMessage(cookie)
+}
+
func (s *TestSession) MakeRequest(t testing.TB, rw *RequestWrapper, expectedStatus int) *httptest.ResponseRecorder {
t.Helper()
req := rw.Request
@@ -235,55 +249,19 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
// token has to be unique this counter take care of
var tokenCounter int64
-// getTokenForLoggedInUser returns a token for a logged in user.
-// The scope is an optional list of snake_case strings like the frontend form fields,
-// but without the "scope_" prefix.
+// getTokenForLoggedInUser returns a token for a logged-in user.
func getTokenForLoggedInUser(t testing.TB, session *TestSession, scopes ...auth.AccessTokenScope) string {
t.Helper()
- var token string
- req := NewRequest(t, "GET", "/user/settings/applications")
- resp := session.MakeRequest(t, req, http.StatusOK)
- var csrf string
- for _, cookie := range resp.Result().Cookies() {
- if cookie.Name != "_csrf" {
- continue
- }
- csrf = cookie.Value
- break
- }
- if csrf == "" {
- doc := NewHTMLParser(t, resp.Body)
- csrf = doc.GetCSRF()
- }
- assert.NotEmpty(t, csrf)
urlValues := url.Values{}
- urlValues.Add("_csrf", csrf)
+ urlValues.Add("_csrf", GetUserCSRFToken(t, session))
urlValues.Add("name", fmt.Sprintf("api-testing-token-%d", atomic.AddInt64(&tokenCounter, 1)))
for _, scope := range scopes {
- urlValues.Add("scope", string(scope))
+ urlValues.Add("scope-dummy", string(scope)) // it only needs to start with "scope-" to be accepted
}
- req = NewRequestWithURLValues(t, "POST", "/user/settings/applications", urlValues)
- resp = session.MakeRequest(t, req, http.StatusSeeOther)
-
- // Log the flash values on failure
- if !assert.Equal(t, []string{"/user/settings/applications"}, resp.Result().Header["Location"]) {
- for _, cookie := range resp.Result().Cookies() {
- if cookie.Name != gitea_context.CookieNameFlash {
- continue
- }
- flash, _ := url.ParseQuery(cookie.Value)
- for key, value := range flash {
- t.Logf("Flash %q: %q", key, value)
- }
- }
- }
-
- req = NewRequest(t, "GET", "/user/settings/applications")
- resp = session.MakeRequest(t, req, http.StatusOK)
- htmlDoc := NewHTMLParser(t, resp.Body)
- token = htmlDoc.doc.Find(".ui.info p").Text()
- assert.NotEmpty(t, token)
- return token
+ req := NewRequestWithURLValues(t, "POST", "/user/settings/applications", urlValues)
+ session.MakeRequest(t, req, http.StatusSeeOther)
+ flashes := session.GetCookieFlashMessage()
+ return flashes.InfoMsg
}
type RequestWrapper struct {
@@ -332,7 +310,7 @@ func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string
func NewRequestWithURLValues(t testing.TB, method, urlStr string, urlValues url.Values) *RequestWrapper {
t.Helper()
- return NewRequestWithBody(t, method, urlStr, bytes.NewBufferString(urlValues.Encode())).
+ return NewRequestWithBody(t, method, urlStr, strings.NewReader(urlValues.Encode())).
SetHeader("Content-Type", "application/x-www-form-urlencoded")
}
@@ -382,7 +360,7 @@ func MakeRequestNilResponseRecorder(t testing.TB, rw *RequestWrapper, expectedSt
recorder := NewNilResponseRecorder()
testWebRoutes.ServeHTTP(recorder, req)
if expectedStatus != NoExpectedStatus {
- if !assert.EqualValues(t, expectedStatus, recorder.Code,
+ if !assert.Equal(t, expectedStatus, recorder.Code,
"Request: %s %s", req.Method, req.URL.String()) {
logUnexpectedResponse(t, &recorder.ResponseRecorder)
}
@@ -396,7 +374,7 @@ func MakeRequestNilResponseHashSumRecorder(t testing.TB, rw *RequestWrapper, exp
recorder := NewNilResponseHashSumRecorder()
testWebRoutes.ServeHTTP(recorder, req)
if expectedStatus != NoExpectedStatus {
- if !assert.EqualValues(t, expectedStatus, recorder.Code,
+ if !assert.Equal(t, expectedStatus, recorder.Code,
"Request: %s %s", req.Method, req.URL.String()) {
logUnexpectedResponse(t, &recorder.ResponseRecorder)
}
@@ -458,9 +436,9 @@ func VerifyJSONSchema(t testing.TB, resp *httptest.ResponseRecorder, schemaFile
// GetUserCSRFToken returns CSRF token for current user
func GetUserCSRFToken(t testing.TB, session *TestSession) string {
t.Helper()
- cookie := session.GetCookie("_csrf")
+ cookie := session.GetSiteCookie("_csrf")
require.NotEmpty(t, cookie)
- return cookie.Value
+ return cookie
}
// GetUserCSRFToken returns CSRF token for anonymous user (not logged in)