integration tests: Use t.Helper() (#7654)
This commit is contained in:
parent
0c927b1606
commit
d02566a8ea
2 changed files with 18 additions and 0 deletions
|
@ -19,6 +19,7 @@ type HTMLDoc struct {
|
||||||
|
|
||||||
// NewHTMLParser parse html file
|
// NewHTMLParser parse html file
|
||||||
func NewHTMLParser(t testing.TB, body *bytes.Buffer) *HTMLDoc {
|
func NewHTMLParser(t testing.TB, body *bytes.Buffer) *HTMLDoc {
|
||||||
|
t.Helper()
|
||||||
doc, err := goquery.NewDocumentFromReader(body)
|
doc, err := goquery.NewDocumentFromReader(body)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
return &HTMLDoc{doc: doc}
|
return &HTMLDoc{doc: doc}
|
||||||
|
|
|
@ -169,6 +169,7 @@ func initIntegrationTest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareTestEnv(t testing.TB, skip ...int) {
|
func prepareTestEnv(t testing.TB, skip ...int) {
|
||||||
|
t.Helper()
|
||||||
ourSkip := 2
|
ourSkip := 2
|
||||||
if len(skip) > 0 {
|
if len(skip) > 0 {
|
||||||
ourSkip += skip[0]
|
ourSkip += skip[0]
|
||||||
|
@ -201,6 +202,7 @@ func (s *TestSession) GetCookie(name string) *http.Cookie {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestSession) MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.ResponseRecorder {
|
func (s *TestSession) MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.ResponseRecorder {
|
||||||
|
t.Helper()
|
||||||
baseURL, err := url.Parse(setting.AppURL)
|
baseURL, err := url.Parse(setting.AppURL)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
for _, c := range s.jar.Cookies(baseURL) {
|
for _, c := range s.jar.Cookies(baseURL) {
|
||||||
|
@ -217,6 +219,7 @@ func (s *TestSession) MakeRequest(t testing.TB, req *http.Request, expectedStatu
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestSession) MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
|
func (s *TestSession) MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
|
||||||
|
t.Helper()
|
||||||
baseURL, err := url.Parse(setting.AppURL)
|
baseURL, err := url.Parse(setting.AppURL)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
for _, c := range s.jar.Cookies(baseURL) {
|
for _, c := range s.jar.Cookies(baseURL) {
|
||||||
|
@ -237,6 +240,7 @@ const userPassword = "password"
|
||||||
var loginSessionCache = make(map[string]*TestSession, 10)
|
var loginSessionCache = make(map[string]*TestSession, 10)
|
||||||
|
|
||||||
func emptyTestSession(t testing.TB) *TestSession {
|
func emptyTestSession(t testing.TB) *TestSession {
|
||||||
|
t.Helper()
|
||||||
jar, err := cookiejar.New(nil)
|
jar, err := cookiejar.New(nil)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
@ -244,6 +248,7 @@ func emptyTestSession(t testing.TB) *TestSession {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loginUser(t testing.TB, userName string) *TestSession {
|
func loginUser(t testing.TB, userName string) *TestSession {
|
||||||
|
t.Helper()
|
||||||
if session, ok := loginSessionCache[userName]; ok {
|
if session, ok := loginSessionCache[userName]; ok {
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
@ -253,6 +258,7 @@ func loginUser(t testing.TB, userName string) *TestSession {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loginUserWithPassword(t testing.TB, userName, password string) *TestSession {
|
func loginUserWithPassword(t testing.TB, userName, password string) *TestSession {
|
||||||
|
t.Helper()
|
||||||
req := NewRequest(t, "GET", "/user/login")
|
req := NewRequest(t, "GET", "/user/login")
|
||||||
resp := MakeRequest(t, req, http.StatusOK)
|
resp := MakeRequest(t, req, http.StatusOK)
|
||||||
|
|
||||||
|
@ -278,6 +284,7 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTokenForLoggedInUser(t testing.TB, session *TestSession) string {
|
func getTokenForLoggedInUser(t testing.TB, session *TestSession) string {
|
||||||
|
t.Helper()
|
||||||
req := NewRequest(t, "GET", "/user/settings/applications")
|
req := NewRequest(t, "GET", "/user/settings/applications")
|
||||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||||
doc := NewHTMLParser(t, resp.Body)
|
doc := NewHTMLParser(t, resp.Body)
|
||||||
|
@ -294,14 +301,17 @@ func getTokenForLoggedInUser(t testing.TB, session *TestSession) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequest(t testing.TB, method, urlStr string) *http.Request {
|
func NewRequest(t testing.TB, method, urlStr string) *http.Request {
|
||||||
|
t.Helper()
|
||||||
return NewRequestWithBody(t, method, urlStr, nil)
|
return NewRequestWithBody(t, method, urlStr, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequestf(t testing.TB, method, urlFormat string, args ...interface{}) *http.Request {
|
func NewRequestf(t testing.TB, method, urlFormat string, args ...interface{}) *http.Request {
|
||||||
|
t.Helper()
|
||||||
return NewRequest(t, method, fmt.Sprintf(urlFormat, args...))
|
return NewRequest(t, method, fmt.Sprintf(urlFormat, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string]string) *http.Request {
|
func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string]string) *http.Request {
|
||||||
|
t.Helper()
|
||||||
urlValues := url.Values{}
|
urlValues := url.Values{}
|
||||||
for key, value := range values {
|
for key, value := range values {
|
||||||
urlValues[key] = []string{value}
|
urlValues[key] = []string{value}
|
||||||
|
@ -312,6 +322,7 @@ func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *http.Request {
|
func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *http.Request {
|
||||||
|
t.Helper()
|
||||||
jsonBytes, err := json.Marshal(v)
|
jsonBytes, err := json.Marshal(v)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
req := NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes))
|
req := NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes))
|
||||||
|
@ -320,6 +331,7 @@ func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *htt
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequestWithBody(t testing.TB, method, urlStr string, body io.Reader) *http.Request {
|
func NewRequestWithBody(t testing.TB, method, urlStr string, body io.Reader) *http.Request {
|
||||||
|
t.Helper()
|
||||||
request, err := http.NewRequest(method, urlStr, body)
|
request, err := http.NewRequest(method, urlStr, body)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
request.RequestURI = urlStr
|
request.RequestURI = urlStr
|
||||||
|
@ -334,6 +346,7 @@ func AddBasicAuthHeader(request *http.Request, username string) *http.Request {
|
||||||
const NoExpectedStatus = -1
|
const NoExpectedStatus = -1
|
||||||
|
|
||||||
func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.ResponseRecorder {
|
func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.ResponseRecorder {
|
||||||
|
t.Helper()
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
mac.ServeHTTP(recorder, req)
|
mac.ServeHTTP(recorder, req)
|
||||||
if expectedStatus != NoExpectedStatus {
|
if expectedStatus != NoExpectedStatus {
|
||||||
|
@ -346,6 +359,7 @@ func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
|
func MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
|
||||||
|
t.Helper()
|
||||||
recorder := NewNilResponseRecorder()
|
recorder := NewNilResponseRecorder()
|
||||||
mac.ServeHTTP(recorder, req)
|
mac.ServeHTTP(recorder, req)
|
||||||
if expectedStatus != NoExpectedStatus {
|
if expectedStatus != NoExpectedStatus {
|
||||||
|
@ -359,6 +373,7 @@ func MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedSta
|
||||||
|
|
||||||
// logUnexpectedResponse logs the contents of an unexpected response.
|
// logUnexpectedResponse logs the contents of an unexpected response.
|
||||||
func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
|
func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
|
||||||
|
t.Helper()
|
||||||
respBytes := recorder.Body.Bytes()
|
respBytes := recorder.Body.Bytes()
|
||||||
if len(respBytes) == 0 {
|
if len(respBytes) == 0 {
|
||||||
return
|
return
|
||||||
|
@ -381,11 +396,13 @@ func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v interface{}) {
|
func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v interface{}) {
|
||||||
|
t.Helper()
|
||||||
decoder := json.NewDecoder(resp.Body)
|
decoder := json.NewDecoder(resp.Body)
|
||||||
assert.NoError(t, decoder.Decode(v))
|
assert.NoError(t, decoder.Decode(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
|
func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
|
||||||
|
t.Helper()
|
||||||
req := NewRequest(t, "GET", urlStr)
|
req := NewRequest(t, "GET", urlStr)
|
||||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||||
doc := NewHTMLParser(t, resp.Body)
|
doc := NewHTMLParser(t, resp.Body)
|
||||||
|
|
Loading…
Reference in a new issue