mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
408 lines
11 KiB
408 lines
11 KiB
8 years ago
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
2 years ago
|
package integration
|
||
8 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
5 years ago
|
"context"
|
||
8 years ago
|
"fmt"
|
||
4 years ago
|
"hash"
|
||
|
"hash/fnv"
|
||
8 years ago
|
"io"
|
||
|
"net/http"
|
||
8 years ago
|
"net/http/cookiejar"
|
||
7 years ago
|
"net/http/httptest"
|
||
8 years ago
|
"net/url"
|
||
8 years ago
|
"os"
|
||
7 years ago
|
"path/filepath"
|
||
8 years ago
|
"strings"
|
||
8 years ago
|
"testing"
|
||
5 years ago
|
"time"
|
||
8 years ago
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/graceful"
|
||
3 years ago
|
"code.gitea.io/gitea/modules/json"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/log"
|
||
8 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/util"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/web"
|
||
8 years ago
|
"code.gitea.io/gitea/routers"
|
||
2 years ago
|
"code.gitea.io/gitea/tests"
|
||
8 years ago
|
|
||
7 years ago
|
"github.com/PuerkitoBio/goquery"
|
||
8 years ago
|
"github.com/stretchr/testify/assert"
|
||
8 years ago
|
)
|
||
|
|
||
4 years ago
|
var c *web.Route
|
||
8 years ago
|
|
||
6 years ago
|
type NilResponseRecorder struct {
|
||
|
httptest.ResponseRecorder
|
||
|
Length int
|
||
|
}
|
||
|
|
||
|
func (n *NilResponseRecorder) Write(b []byte) (int, error) {
|
||
6 years ago
|
n.Length += len(b)
|
||
6 years ago
|
return len(b), nil
|
||
|
}
|
||
|
|
||
|
// NewRecorder returns an initialized ResponseRecorder.
|
||
|
func NewNilResponseRecorder() *NilResponseRecorder {
|
||
|
return &NilResponseRecorder{
|
||
|
ResponseRecorder: *httptest.NewRecorder(),
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
type NilResponseHashSumRecorder struct {
|
||
|
httptest.ResponseRecorder
|
||
|
Hash hash.Hash
|
||
|
Length int
|
||
|
}
|
||
|
|
||
|
func (n *NilResponseHashSumRecorder) Write(b []byte) (int, error) {
|
||
|
_, _ = n.Hash.Write(b)
|
||
|
n.Length += len(b)
|
||
|
return len(b), nil
|
||
|
}
|
||
|
|
||
|
// NewRecorder returns an initialized ResponseRecorder.
|
||
|
func NewNilResponseHashSumRecorder() *NilResponseHashSumRecorder {
|
||
|
return &NilResponseHashSumRecorder{
|
||
|
Hash: fnv.New32(),
|
||
|
ResponseRecorder: *httptest.NewRecorder(),
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func TestMain(m *testing.M) {
|
||
4 years ago
|
defer log.Close()
|
||
|
|
||
5 years ago
|
managerCtx, cancel := context.WithCancel(context.Background())
|
||
|
graceful.InitManager(managerCtx)
|
||
|
defer cancel()
|
||
|
|
||
2 years ago
|
tests.InitTest(true)
|
||
2 years ago
|
c = routers.NormalRoutes(context.TODO())
|
||
8 years ago
|
|
||
5 years ago
|
// integration test settings...
|
||
|
if setting.Cfg != nil {
|
||
|
testingCfg := setting.Cfg.Section("integration-tests")
|
||
2 years ago
|
tests.SlowTest = testingCfg.Key("SLOW_TEST").MustDuration(tests.SlowTest)
|
||
|
tests.SlowFlush = testingCfg.Key("SLOW_FLUSH").MustDuration(tests.SlowFlush)
|
||
5 years ago
|
}
|
||
|
|
||
|
if os.Getenv("GITEA_SLOW_TEST_TIME") != "" {
|
||
|
duration, err := time.ParseDuration(os.Getenv("GITEA_SLOW_TEST_TIME"))
|
||
|
if err == nil {
|
||
2 years ago
|
tests.SlowTest = duration
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if os.Getenv("GITEA_SLOW_FLUSH_TIME") != "" {
|
||
|
duration, err := time.ParseDuration(os.Getenv("GITEA_SLOW_FLUSH_TIME"))
|
||
|
if err == nil {
|
||
2 years ago
|
tests.SlowFlush = duration
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
os.Unsetenv("GIT_AUTHOR_NAME")
|
||
|
os.Unsetenv("GIT_AUTHOR_EMAIL")
|
||
|
os.Unsetenv("GIT_AUTHOR_DATE")
|
||
|
os.Unsetenv("GIT_COMMITTER_NAME")
|
||
|
os.Unsetenv("GIT_COMMITTER_EMAIL")
|
||
|
os.Unsetenv("GIT_COMMITTER_DATE")
|
||
|
|
||
3 years ago
|
err := unittest.InitFixtures(
|
||
|
unittest.FixturesOptions{
|
||
3 years ago
|
Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
|
||
|
},
|
||
8 years ago
|
)
|
||
|
if err != nil {
|
||
|
fmt.Printf("Error initializing test database: %v\n", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
7 years ago
|
exitCode := m.Run()
|
||
|
|
||
2 years ago
|
tests.WriterCloser.Reset()
|
||
6 years ago
|
|
||
4 years ago
|
if err = util.RemoveAll(setting.Indexer.IssuePath); err != nil {
|
||
|
fmt.Printf("util.RemoveAll: %v\n", err)
|
||
7 years ago
|
os.Exit(1)
|
||
|
}
|
||
4 years ago
|
if err = util.RemoveAll(setting.Indexer.RepoPath); err != nil {
|
||
7 years ago
|
fmt.Printf("Unable to remove repo indexer: %v\n", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
7 years ago
|
|
||
|
os.Exit(exitCode)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
type TestSession struct {
|
||
|
jar http.CookieJar
|
||
|
}
|
||
|
|
||
|
func (s *TestSession) GetCookie(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
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
7 years ago
|
func (s *TestSession) MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.ResponseRecorder {
|
||
5 years ago
|
t.Helper()
|
||
8 years ago
|
baseURL, err := url.Parse(setting.AppURL)
|
||
|
assert.NoError(t, err)
|
||
|
for _, c := range s.jar.Cookies(baseURL) {
|
||
|
req.AddCookie(c)
|
||
|
}
|
||
7 years ago
|
resp := MakeRequest(t, req, expectedStatus)
|
||
8 years ago
|
|
||
|
ch := http.Header{}
|
||
6 years ago
|
ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
|
||
8 years ago
|
cr := http.Request{Header: ch}
|
||
|
s.jar.SetCookies(baseURL, cr.Cookies())
|
||
|
|
||
|
return resp
|
||
|
}
|
||
|
|
||
6 years ago
|
func (s *TestSession) MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
|
||
5 years ago
|
t.Helper()
|
||
6 years ago
|
baseURL, err := url.Parse(setting.AppURL)
|
||
|
assert.NoError(t, err)
|
||
|
for _, c := range s.jar.Cookies(baseURL) {
|
||
|
req.AddCookie(c)
|
||
|
}
|
||
|
resp := MakeRequestNilResponseRecorder(t, req, expectedStatus)
|
||
|
|
||
|
ch := http.Header{}
|
||
6 years ago
|
ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
|
||
6 years ago
|
cr := http.Request{Header: ch}
|
||
|
s.jar.SetCookies(baseURL, cr.Cookies())
|
||
|
|
||
|
return resp
|
||
|
}
|
||
|
|
||
4 years ago
|
func (s *TestSession) MakeRequestNilResponseHashSumRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseHashSumRecorder {
|
||
|
t.Helper()
|
||
|
baseURL, err := url.Parse(setting.AppURL)
|
||
|
assert.NoError(t, err)
|
||
|
for _, c := range s.jar.Cookies(baseURL) {
|
||
|
req.AddCookie(c)
|
||
|
}
|
||
|
resp := MakeRequestNilResponseHashSumRecorder(t, req, expectedStatus)
|
||
|
|
||
|
ch := http.Header{}
|
||
|
ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
|
||
|
cr := http.Request{Header: ch}
|
||
|
s.jar.SetCookies(baseURL, cr.Cookies())
|
||
|
|
||
|
return resp
|
||
|
}
|
||
|
|
||
8 years ago
|
const userPassword = "password"
|
||
|
|
||
7 years ago
|
var loginSessionCache = make(map[string]*TestSession, 10)
|
||
|
|
||
7 years ago
|
func emptyTestSession(t testing.TB) *TestSession {
|
||
5 years ago
|
t.Helper()
|
||
7 years ago
|
jar, err := cookiejar.New(nil)
|
||
|
assert.NoError(t, err)
|
||
|
|
||
|
return &TestSession{jar: jar}
|
||
|
}
|
||
|
|
||
3 years ago
|
func getUserToken(t testing.TB, userName string) string {
|
||
|
return getTokenForLoggedInUser(t, loginUser(t, userName))
|
||
|
}
|
||
|
|
||
8 years ago
|
func loginUser(t testing.TB, userName string) *TestSession {
|
||
5 years ago
|
t.Helper()
|
||
7 years ago
|
if session, ok := loginSessionCache[userName]; ok {
|
||
|
return session
|
||
|
}
|
||
|
session := loginUserWithPassword(t, userName, userPassword)
|
||
|
loginSessionCache[userName] = session
|
||
|
return session
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func loginUserWithPassword(t testing.TB, userName, password string) *TestSession {
|
||
5 years ago
|
t.Helper()
|
||
8 years ago
|
req := NewRequest(t, "GET", "/user/login")
|
||
7 years ago
|
resp := MakeRequest(t, req, http.StatusOK)
|
||
8 years ago
|
|
||
8 years ago
|
doc := NewHTMLParser(t, resp.Body)
|
||
8 years ago
|
req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{
|
||
|
"_csrf": doc.GetCSRF(),
|
||
|
"user_name": userName,
|
||
|
"password": password,
|
||
|
})
|
||
3 years ago
|
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||
8 years ago
|
|
||
|
ch := http.Header{}
|
||
6 years ago
|
ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
|
||
8 years ago
|
cr := http.Request{Header: ch}
|
||
|
|
||
7 years ago
|
session := emptyTestSession(t)
|
||
|
|
||
8 years ago
|
baseURL, err := url.Parse(setting.AppURL)
|
||
|
assert.NoError(t, err)
|
||
7 years ago
|
session.jar.SetCookies(baseURL, cr.Cookies())
|
||
8 years ago
|
|
||
7 years ago
|
return session
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
// token has to be unique this counter take care of
|
||
5 years ago
|
var tokenCounter int64
|
||
|
|
||
6 years ago
|
func getTokenForLoggedInUser(t testing.TB, session *TestSession) string {
|
||
5 years ago
|
t.Helper()
|
||
5 years ago
|
tokenCounter++
|
||
6 years ago
|
req := NewRequest(t, "GET", "/user/settings/applications")
|
||
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||
|
doc := NewHTMLParser(t, resp.Body)
|
||
|
req = NewRequestWithValues(t, "POST", "/user/settings/applications", map[string]string{
|
||
|
"_csrf": doc.GetCSRF(),
|
||
5 years ago
|
"name": fmt.Sprintf("api-testing-token-%d", tokenCounter),
|
||
6 years ago
|
})
|
||
2 years ago
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
||
6 years ago
|
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()
|
||
|
return token
|
||
|
}
|
||
|
|
||
8 years ago
|
func NewRequest(t testing.TB, method, urlStr string) *http.Request {
|
||
5 years ago
|
t.Helper()
|
||
8 years ago
|
return NewRequestWithBody(t, method, urlStr, nil)
|
||
|
}
|
||
|
|
||
8 years ago
|
func NewRequestf(t testing.TB, method, urlFormat string, args ...interface{}) *http.Request {
|
||
5 years ago
|
t.Helper()
|
||
8 years ago
|
return NewRequest(t, method, fmt.Sprintf(urlFormat, args...))
|
||
|
}
|
||
|
|
||
8 years ago
|
func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string]string) *http.Request {
|
||
5 years ago
|
t.Helper()
|
||
8 years ago
|
urlValues := url.Values{}
|
||
|
for key, value := range values {
|
||
|
urlValues[key] = []string{value}
|
||
|
}
|
||
8 years ago
|
req := NewRequestWithBody(t, method, urlStr, bytes.NewBufferString(urlValues.Encode()))
|
||
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||
|
return req
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *http.Request {
|
||
5 years ago
|
t.Helper()
|
||
4 years ago
|
|
||
8 years ago
|
jsonBytes, err := json.Marshal(v)
|
||
|
assert.NoError(t, err)
|
||
8 years ago
|
req := NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes))
|
||
|
req.Header.Add("Content-Type", "application/json")
|
||
|
return req
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func NewRequestWithBody(t testing.TB, method, urlStr string, body io.Reader) *http.Request {
|
||
5 years ago
|
t.Helper()
|
||
4 years ago
|
if !strings.HasPrefix(urlStr, "http") && !strings.HasPrefix(urlStr, "/") {
|
||
|
urlStr = "/" + urlStr
|
||
|
}
|
||
8 years ago
|
request, err := http.NewRequest(method, urlStr, body)
|
||
8 years ago
|
assert.NoError(t, err)
|
||
8 years ago
|
request.RequestURI = urlStr
|
||
8 years ago
|
return request
|
||
|
}
|
||
|
|
||
6 years ago
|
func AddBasicAuthHeader(request *http.Request, username string) *http.Request {
|
||
|
request.SetBasicAuth(username, userPassword)
|
||
|
return request
|
||
|
}
|
||
|
|
||
7 years ago
|
const NoExpectedStatus = -1
|
||
|
|
||
7 years ago
|
func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.ResponseRecorder {
|
||
5 years ago
|
t.Helper()
|
||
7 years ago
|
recorder := httptest.NewRecorder()
|
||
4 years ago
|
c.ServeHTTP(recorder, req)
|
||
7 years ago
|
if expectedStatus != NoExpectedStatus {
|
||
7 years ago
|
if !assert.EqualValues(t, expectedStatus, recorder.Code,
|
||
|
"Request: %s %s", req.Method, req.URL.String()) {
|
||
|
logUnexpectedResponse(t, recorder)
|
||
|
}
|
||
7 years ago
|
}
|
||
7 years ago
|
return recorder
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
6 years ago
|
func MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
|
||
5 years ago
|
t.Helper()
|
||
6 years ago
|
recorder := NewNilResponseRecorder()
|
||
4 years ago
|
c.ServeHTTP(recorder, req)
|
||
6 years ago
|
if expectedStatus != NoExpectedStatus {
|
||
4 years ago
|
if !assert.EqualValues(t, expectedStatus, recorder.Code,
|
||
|
"Request: %s %s", req.Method, req.URL.String()) {
|
||
|
logUnexpectedResponse(t, &recorder.ResponseRecorder)
|
||
|
}
|
||
|
}
|
||
|
return recorder
|
||
|
}
|
||
|
|
||
|
func MakeRequestNilResponseHashSumRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseHashSumRecorder {
|
||
|
t.Helper()
|
||
|
recorder := NewNilResponseHashSumRecorder()
|
||
|
c.ServeHTTP(recorder, req)
|
||
|
if expectedStatus != NoExpectedStatus {
|
||
6 years ago
|
if !assert.EqualValues(t, expectedStatus, recorder.Code,
|
||
|
"Request: %s %s", req.Method, req.URL.String()) {
|
||
|
logUnexpectedResponse(t, &recorder.ResponseRecorder)
|
||
|
}
|
||
|
}
|
||
|
return recorder
|
||
|
}
|
||
|
|
||
7 years ago
|
// logUnexpectedResponse logs the contents of an unexpected response.
|
||
|
func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
|
||
5 years ago
|
t.Helper()
|
||
7 years ago
|
respBytes := recorder.Body.Bytes()
|
||
|
if len(respBytes) == 0 {
|
||
|
return
|
||
|
} else if len(respBytes) < 500 {
|
||
|
// if body is short, just log the whole thing
|
||
|
t.Log("Response:", string(respBytes))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// log the "flash" error message, if one exists
|
||
|
// we must create a new buffer, so that we don't "use up" resp.Body
|
||
|
htmlDoc, err := goquery.NewDocumentFromReader(bytes.NewBuffer(respBytes))
|
||
|
if err != nil {
|
||
|
return // probably a non-HTML response
|
||
|
}
|
||
|
errMsg := htmlDoc.Find(".ui.negative.message").Text()
|
||
|
if len(errMsg) > 0 {
|
||
|
t.Log("A flash error message was found:", errMsg)
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v interface{}) {
|
||
5 years ago
|
t.Helper()
|
||
4 years ago
|
|
||
7 years ago
|
decoder := json.NewDecoder(resp.Body)
|
||
8 years ago
|
assert.NoError(t, decoder.Decode(v))
|
||
|
}
|
||
7 years ago
|
|
||
|
func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
|
||
5 years ago
|
t.Helper()
|
||
7 years ago
|
req := NewRequest(t, "GET", urlStr)
|
||
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||
|
doc := NewHTMLParser(t, resp.Body)
|
||
|
return doc.GetCSRF()
|
||
|
}
|