feat: vendor gitea 1.16.2
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
//nolint:forbidigo // use of print functions is allowed in tests
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"hash"
|
||||
"hash/fnv"
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/testlogger"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
"code.gitea.io/gitea/modules/web/middleware"
|
||||
@@ -79,14 +80,14 @@ func NewNilResponseHashSumRecorder() *NilResponseHashSumRecorder {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
func testMain(m *testing.M) int {
|
||||
defer log.GetManager().Close()
|
||||
|
||||
managerCtx, cancel := context.WithCancel(context.Background())
|
||||
graceful.InitManager(managerCtx)
|
||||
defer cancel()
|
||||
|
||||
tests.InitTest(true)
|
||||
tests.InitTest()
|
||||
testWebRoutes = routers.NormalRoutes()
|
||||
|
||||
err := unittest.InitFixtures(
|
||||
@@ -95,8 +96,7 @@ func TestMain(m *testing.M) {
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("Error initializing test database: %v\n", err)
|
||||
os.Exit(1)
|
||||
testlogger.Panicf("InitFixtures: %v", err)
|
||||
}
|
||||
|
||||
// FIXME: the console logger is deleted by mistake, so if there is any `log.Fatal`, developers won't see any error message.
|
||||
@@ -104,15 +104,16 @@ func TestMain(m *testing.M) {
|
||||
exitCode := m.Run()
|
||||
|
||||
if err = util.RemoveAll(setting.Indexer.IssuePath); err != nil {
|
||||
fmt.Printf("util.RemoveAll: %v\n", err)
|
||||
os.Exit(1)
|
||||
log.Error("Failed to remove indexer path: %v", err)
|
||||
}
|
||||
if err = util.RemoveAll(setting.Indexer.RepoPath); err != nil {
|
||||
fmt.Printf("Unable to remove repo indexer: %v\n", err)
|
||||
os.Exit(1)
|
||||
log.Error("Failed to remove indexer path: %v", err)
|
||||
}
|
||||
return exitCode
|
||||
}
|
||||
|
||||
os.Exit(exitCode)
|
||||
func TestMain(m *testing.M) {
|
||||
os.Exit(testMain(m))
|
||||
}
|
||||
|
||||
type TestSession struct {
|
||||
@@ -225,16 +226,11 @@ func loginUser(t testing.TB, userName string) *TestSession {
|
||||
|
||||
func loginUserWithPassword(t testing.TB, userName, password string) *TestSession {
|
||||
t.Helper()
|
||||
req := NewRequest(t, "GET", "/user/login")
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
doc := NewHTMLParser(t, resp.Body)
|
||||
req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{
|
||||
"_csrf": doc.GetCSRF(),
|
||||
req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{
|
||||
"user_name": userName,
|
||||
"password": password,
|
||||
})
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
|
||||
ch := http.Header{}
|
||||
ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
|
||||
@@ -250,14 +246,13 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
|
||||
}
|
||||
|
||||
// token has to be unique this counter take care of
|
||||
var tokenCounter int64
|
||||
var tokenCounter atomic.Int64
|
||||
|
||||
// getTokenForLoggedInUser returns a token for a logged-in user.
|
||||
func getTokenForLoggedInUser(t testing.TB, session *TestSession, scopes ...auth.AccessTokenScope) string {
|
||||
t.Helper()
|
||||
urlValues := url.Values{}
|
||||
urlValues.Add("_csrf", GetUserCSRFToken(t, session))
|
||||
urlValues.Add("name", fmt.Sprintf("api-testing-token-%d", atomic.AddInt64(&tokenCounter, 1)))
|
||||
urlValues.Add("name", fmt.Sprintf("api-testing-token-%d", tokenCounter.Add(1)))
|
||||
for _, scope := range scopes {
|
||||
urlValues.Add("scope-dummy", string(scope)) // it only needs to start with "scope-" to be accepted
|
||||
}
|
||||
@@ -332,9 +327,10 @@ func NewRequestWithBody(t testing.TB, method, urlStr string, body io.Reader) *Re
|
||||
urlStr = "/" + urlStr
|
||||
}
|
||||
req, err := http.NewRequest(method, urlStr, body)
|
||||
assert.NoError(t, err)
|
||||
req.RequestURI = urlStr
|
||||
|
||||
require.NoError(t, err)
|
||||
if req.URL.User != nil {
|
||||
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(req.URL.User.String())))
|
||||
}
|
||||
return &RequestWrapper{req}
|
||||
}
|
||||
|
||||
@@ -347,6 +343,10 @@ func MakeRequest(t testing.TB, rw *RequestWrapper, expectedStatus int) *httptest
|
||||
if req.RemoteAddr == "" {
|
||||
req.RemoteAddr = "test-mock:12345"
|
||||
}
|
||||
// Ensure unknown contentLength is seen as -1
|
||||
if req.Body != nil && req.ContentLength == 0 {
|
||||
req.ContentLength = -1
|
||||
}
|
||||
testWebRoutes.ServeHTTP(recorder, req)
|
||||
if expectedStatus != NoExpectedStatus {
|
||||
if expectedStatus != recorder.Code {
|
||||
@@ -410,11 +410,13 @@ func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
|
||||
}
|
||||
}
|
||||
|
||||
func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v any) {
|
||||
func DecodeJSON[T any](t testing.TB, resp *httptest.ResponseRecorder, v T) (ret T) {
|
||||
t.Helper()
|
||||
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
require.NoError(t, decoder.Decode(v))
|
||||
// FIXME: JSON-KEY-CASE: for testing purpose only, because many structs don't provide `json` tags, they just use capitalized field names
|
||||
decoder := json.NewDecoderCaseInsensitive(resp.Body)
|
||||
require.NoError(t, decoder.Decode(&v))
|
||||
return v
|
||||
}
|
||||
|
||||
func VerifyJSONSchema(t testing.TB, resp *httptest.ResponseRecorder, schemaFile string) {
|
||||
@@ -435,20 +437,3 @@ func VerifyJSONSchema(t testing.TB, resp *httptest.ResponseRecorder, schemaFile
|
||||
assert.Empty(t, result.Errors())
|
||||
assert.True(t, result.Valid())
|
||||
}
|
||||
|
||||
// GetUserCSRFToken returns CSRF token for current user
|
||||
func GetUserCSRFToken(t testing.TB, session *TestSession) string {
|
||||
t.Helper()
|
||||
cookie := session.GetSiteCookie("_csrf")
|
||||
require.NotEmpty(t, cookie)
|
||||
return cookie
|
||||
}
|
||||
|
||||
// GetUserCSRFToken returns CSRF token for anonymous user (not logged in)
|
||||
func GetAnonymousCSRFToken(t testing.TB, session *TestSession) string {
|
||||
t.Helper()
|
||||
resp := session.MakeRequest(t, NewRequest(t, "GET", "/user/login"), http.StatusOK)
|
||||
csrfToken := NewHTMLParser(t, resp.Body).GetCSRF()
|
||||
require.NotEmpty(t, csrfToken)
|
||||
return csrfToken
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user