From f1adaef45849fc260b07be39a39b9c1ee5146923 Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Fri, 7 Jul 2017 15:36:47 -0400 Subject: [PATCH] Less verbose integration tests (#2123) * Helper functions for intergration test boilerplate --- integrations/api_branch_test.go | 2 +- integrations/api_comment_test.go | 3 +- integrations/api_issue_label_test.go | 6 +-- integrations/api_issue_test.go | 6 +-- integrations/api_pull_test.go | 3 +- integrations/api_repo_test.go | 8 +--- integrations/api_team_test.go | 3 +- integrations/change_default_branch_test.go | 26 ++++-------- integrations/delete_user_test.go | 15 ++----- integrations/editor_test.go | 47 +++++++-------------- integrations/integration_test.go | 30 ++++++++++---- integrations/internal_test.go | 4 +- integrations/issue_test.go | 24 ++++------- integrations/pull_compare_test.go | 5 +-- integrations/pull_create_test.go | 9 ++-- integrations/pull_merge_test.go | 27 ++++-------- integrations/release_test.go | 18 +++----- integrations/repo_commits_test.go | 12 ++---- integrations/repo_fork_test.go | 15 +++---- integrations/repo_migrate_test.go | 6 +-- integrations/repo_test.go | 17 +++----- integrations/signup_test.go | 8 +--- integrations/user_test.go | 48 +++++++--------------- integrations/version_test.go | 3 +- 24 files changed, 122 insertions(+), 223 deletions(-) diff --git a/integrations/api_branch_test.go b/integrations/api_branch_test.go index 79adfaa4f..8701229b0 100644 --- a/integrations/api_branch_test.go +++ b/integrations/api_branch_test.go @@ -18,7 +18,7 @@ func testAPIGetBranch(t *testing.T, branchName string, exists bool) { session := loginUser(t, "user2") req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/branches/%s", branchName) - resp := session.MakeRequest(t, req) + resp := session.MakeRequest(t, req, NoExpectedStatus) if !exists { assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode) return diff --git a/integrations/api_comment_test.go b/integrations/api_comment_test.go index cdf411036..74983b2c8 100644 --- a/integrations/api_comment_test.go +++ b/integrations/api_comment_test.go @@ -26,8 +26,7 @@ func TestAPIListComments(t *testing.T) { session := loginUser(t, repoOwner.Name) req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/comments", repoOwner.Name, repo.Name, issue.Index) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) var comments []*api.Comment DecodeJSON(t, resp, &comments) diff --git a/integrations/api_issue_label_test.go b/integrations/api_issue_label_test.go index 2014d38e7..195ec24ca 100644 --- a/integrations/api_issue_label_test.go +++ b/integrations/api_issue_label_test.go @@ -29,8 +29,7 @@ func TestAPIAddIssueLabels(t *testing.T) { Labels: []int64{label.ID}, }) session := loginUser(t, owner.Name) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) var apiLabels []*api.Label DecodeJSON(t, resp, &apiLabels) assert.Len(t, apiLabels, models.GetCount(t, &models.IssueLabel{IssueID: issue.ID})) @@ -52,8 +51,7 @@ func TestAPIReplaceIssueLabels(t *testing.T) { Labels: []int64{label.ID}, }) session := loginUser(t, owner.Name) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) var apiLabels []*api.Label DecodeJSON(t, resp, &apiLabels) assert.Len(t, apiLabels, 1) diff --git a/integrations/api_issue_test.go b/integrations/api_issue_test.go index 63dd9459b..74436ffe9 100644 --- a/integrations/api_issue_test.go +++ b/integrations/api_issue_test.go @@ -24,8 +24,7 @@ func TestAPIListIssues(t *testing.T) { session := loginUser(t, owner.Name) req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues?state=all", owner.Name, repo.Name) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) var apiIssues []*api.Issue DecodeJSON(t, resp, &apiIssues) assert.Len(t, apiIssues, models.GetCount(t, &models.Issue{RepoID: repo.ID})) @@ -49,8 +48,7 @@ func TestAPICreateIssue(t *testing.T) { Title: title, Assignee: owner.Name, }) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusCreated, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusCreated) var apiIssue api.Issue DecodeJSON(t, resp, &apiIssue) assert.Equal(t, apiIssue.Body, body) diff --git a/integrations/api_pull_test.go b/integrations/api_pull_test.go index 605f51ef9..b9bab920e 100644 --- a/integrations/api_pull_test.go +++ b/integrations/api_pull_test.go @@ -21,8 +21,7 @@ func TestAPIViewPulls(t *testing.T) { session := loginUser(t, "user2") req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/pulls?state=all", owner.Name, repo.Name) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) var pulls []*api.PullRequest DecodeJSON(t, resp, &pulls) diff --git a/integrations/api_repo_test.go b/integrations/api_repo_test.go index 4de0c52ab..180105d4c 100644 --- a/integrations/api_repo_test.go +++ b/integrations/api_repo_test.go @@ -7,22 +7,18 @@ package integrations import ( "net/http" "testing" - - "github.com/stretchr/testify/assert" ) func TestAPIUserReposNotLogin(t *testing.T) { prepareTestEnv(t) req := NewRequest(t, "GET", "/api/v1/users/user2/repos") - resp := MakeRequest(req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + MakeRequest(t, req, http.StatusOK) } func TestAPISearchRepoNotLogin(t *testing.T) { prepareTestEnv(t) req := NewRequest(t, "GET", "/api/v1/repos/search?q=Test") - resp := MakeRequest(req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + MakeRequest(t, req, http.StatusOK) } diff --git a/integrations/api_team_test.go b/integrations/api_team_test.go index 279014c6c..b8d21be53 100644 --- a/integrations/api_team_test.go +++ b/integrations/api_team_test.go @@ -22,8 +22,7 @@ func TestAPITeam(t *testing.T) { session := loginUser(t, user.Name) req := NewRequestf(t, "GET", "/api/v1/teams/%d", teamUser.TeamID) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) var apiTeam api.Team DecodeJSON(t, resp, &apiTeam) diff --git a/integrations/change_default_branch_test.go b/integrations/change_default_branch_test.go index 07bc41a18..dbd189cd2 100644 --- a/integrations/change_default_branch_test.go +++ b/integrations/change_default_branch_test.go @@ -10,8 +10,6 @@ import ( "testing" "code.gitea.io/gitea/models" - - "github.com/stretchr/testify/assert" ) func TestChangeDefaultBranch(t *testing.T) { @@ -22,29 +20,19 @@ func TestChangeDefaultBranch(t *testing.T) { session := loginUser(t, owner.Name) branchesURL := fmt.Sprintf("/%s/%s/settings/branches", owner.Name, repo.Name) - req := NewRequest(t, "GET", branchesURL) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc := NewHTMLParser(t, resp.Body) - - req = NewRequestWithValues(t, "POST", branchesURL, map[string]string{ - "_csrf": doc.GetCSRF(), + csrf := GetCSRF(t, session, branchesURL) + req := NewRequestWithValues(t, "POST", branchesURL, map[string]string{ + "_csrf": csrf, "action": "default_branch", "branch": "DefaultBranch", }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) - - req = NewRequest(t, "GET", branchesURL) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc = NewHTMLParser(t, resp.Body) + session.MakeRequest(t, req, http.StatusFound) + csrf = GetCSRF(t, session, branchesURL) req = NewRequestWithValues(t, "POST", branchesURL, map[string]string{ - "_csrf": doc.GetInputValueByName("_csrf"), + "_csrf": csrf, "action": "default_branch", "branch": "does_not_exist", }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode) + session.MakeRequest(t, req, http.StatusNotFound) } diff --git a/integrations/delete_user_test.go b/integrations/delete_user_test.go index f766a29a1..889f8dae2 100644 --- a/integrations/delete_user_test.go +++ b/integrations/delete_user_test.go @@ -9,8 +9,6 @@ import ( "testing" "code.gitea.io/gitea/models" - - "github.com/stretchr/testify/assert" ) func TestDeleteUser(t *testing.T) { @@ -18,16 +16,11 @@ func TestDeleteUser(t *testing.T) { session := loginUser(t, "user1") - req := NewRequest(t, "GET", "/admin/users/8") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - - doc := NewHTMLParser(t, resp.Body) - req = NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{ - "_csrf": doc.GetCSRF(), + csrf := GetCSRF(t, session, "/admin/users/8") + req := NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{ + "_csrf": csrf, }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + session.MakeRequest(t, req, http.StatusOK) models.AssertNotExistsBean(t, &models.User{ID: 8}) models.CheckConsistencyFor(t, &models.User{}) diff --git a/integrations/editor_test.go b/integrations/editor_test.go index 48740414c..79b6bb790 100644 --- a/integrations/editor_test.go +++ b/integrations/editor_test.go @@ -19,8 +19,7 @@ func TestCreateFile(t *testing.T) { // Request editor page req := NewRequest(t, "GET", "/user2/repo1/_new/master/") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) doc := NewHTMLParser(t, resp.Body) lastCommit := doc.GetInputValueByName("last_commit") @@ -34,8 +33,7 @@ func TestCreateFile(t *testing.T) { "content": "Content", "commit_choice": "direct", }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusFound) } func TestCreateFileOnProtectedBranch(t *testing.T) { @@ -43,21 +41,14 @@ func TestCreateFileOnProtectedBranch(t *testing.T) { session := loginUser(t, "user2") - // Open repository branch settings - req := NewRequest(t, "GET", "/user2/repo1/settings/branches") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - - doc := NewHTMLParser(t, resp.Body) - + csrf := GetCSRF(t, session, "/user2/repo1/settings/branches") // Change master branch to protected - req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", map[string]string{ - "_csrf": doc.GetCSRF(), + req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", map[string]string{ + "_csrf": csrf, "branchName": "master", "canPush": "true", }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) // Check if master branch has been locked successfully flashCookie := session.GetCookie("macaron_flash") assert.NotNil(t, flashCookie) @@ -65,10 +56,9 @@ func TestCreateFileOnProtectedBranch(t *testing.T) { // Request editor page req = NewRequest(t, "GET", "/user2/repo1/_new/master/") - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusOK) - doc = NewHTMLParser(t, resp.Body) + doc := NewHTMLParser(t, resp.Body) lastCommit := doc.GetInputValueByName("last_commit") assert.NotEmpty(t, lastCommit) @@ -81,8 +71,7 @@ func TestCreateFileOnProtectedBranch(t *testing.T) { "commit_choice": "direct", }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusOK) // Check body for error message assert.Contains(t, string(resp.Body), "Can not commit to protected branch 'master'.") } @@ -93,8 +82,7 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa // Get to the 'edit this file' page req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath)) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) lastCommit := htmlDoc.GetInputValueByName("last_commit") @@ -110,13 +98,11 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa "commit_choice": "direct", }, ) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusFound) // Verify the change req = NewRequest(t, "GET", path.Join(user, repo, "raw", branch, filePath)) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusOK) assert.EqualValues(t, newContent, string(resp.Body)) return resp @@ -128,8 +114,7 @@ func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, bra // Get to the 'edit this file' page req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath)) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) lastCommit := htmlDoc.GetInputValueByName("last_commit") @@ -146,13 +131,11 @@ func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, bra "new_branch_name": targetBranch, }, ) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusFound) // Verify the change req = NewRequest(t, "GET", path.Join(user, repo, "raw", targetBranch, filePath)) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusOK) assert.EqualValues(t, newContent, string(resp.Body)) return resp diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 9332e1218..478eb976c 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -140,13 +140,13 @@ func (s *TestSession) GetCookie(name string) *http.Cookie { return nil } -func (s *TestSession) MakeRequest(t testing.TB, req *http.Request) *TestResponse { +func (s *TestSession) MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *TestResponse { baseURL, err := url.Parse(setting.AppURL) assert.NoError(t, err) for _, c := range s.jar.Cookies(baseURL) { req.AddCookie(c) } - resp := MakeRequest(req) + resp := MakeRequest(t, req, expectedStatus) ch := http.Header{} ch.Add("Cookie", strings.Join(resp.Headers["Set-Cookie"], ";")) @@ -164,8 +164,7 @@ func loginUser(t testing.TB, userName string) *TestSession { func loginUserWithPassword(t testing.TB, userName, password string) *TestSession { req := NewRequest(t, "GET", "/user/login") - resp := MakeRequest(req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := MakeRequest(t, req, http.StatusOK) doc := NewHTMLParser(t, resp.Body) req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{ @@ -173,8 +172,7 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession "user_name": userName, "password": password, }) - resp = MakeRequest(req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + resp = MakeRequest(t, req, http.StatusFound) ch := http.Header{} ch.Add("Cookie", strings.Join(resp.Headers["Set-Cookie"], ";")) @@ -246,13 +244,18 @@ func NewRequestWithBody(t testing.TB, method, urlStr string, body io.Reader) *ht return request } -func MakeRequest(req *http.Request) *TestResponse { +const NoExpectedStatus = -1 + +func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *TestResponse { buffer := bytes.NewBuffer(nil) respWriter := &TestResponseWriter{ Writer: buffer, Headers: make(map[string][]string), } mac.ServeHTTP(respWriter, req) + if expectedStatus != NoExpectedStatus { + assert.EqualValues(t, expectedStatus, respWriter.HeaderCode) + } return &TestResponse{ HeaderCode: respWriter.HeaderCode, Body: buffer.Bytes(), @@ -264,3 +267,16 @@ func DecodeJSON(t testing.TB, resp *TestResponse, v interface{}) { decoder := json.NewDecoder(bytes.NewBuffer(resp.Body)) assert.NoError(t, decoder.Decode(v)) } + +func GetCSRF(t testing.TB, session *TestSession, urlStr string) string { + req := NewRequest(t, "GET", urlStr) + resp := session.MakeRequest(t, req, http.StatusOK) + doc := NewHTMLParser(t, resp.Body) + return doc.GetCSRF() +} + +func RedirectURL(t testing.TB, resp *TestResponse) string { + urlSlice := resp.Headers["Location"] + assert.NotEmpty(t, urlSlice, "No redirect URL founds") + return urlSlice[0] +} diff --git a/integrations/internal_test.go b/integrations/internal_test.go index 3af079dc8..c22e951bc 100644 --- a/integrations/internal_test.go +++ b/integrations/internal_test.go @@ -23,9 +23,9 @@ func assertProtectedBranch(t *testing.T, repoID int64, branchName string, isErr, t.Log(reqURL) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken)) - resp := MakeRequest(req) + resp := MakeRequest(t, req, NoExpectedStatus) if isErr { - assert.EqualValues(t, 500, resp.HeaderCode) + assert.EqualValues(t, http.StatusInternalServerError, resp.HeaderCode) } else { assert.EqualValues(t, http.StatusOK, resp.HeaderCode) var branch models.ProtectedBranch diff --git a/integrations/issue_test.go b/integrations/issue_test.go index 320dad066..e728e4a15 100644 --- a/integrations/issue_test.go +++ b/integrations/issue_test.go @@ -35,8 +35,7 @@ func TestNoLoginViewIssues(t *testing.T) { prepareTestEnv(t) req := NewRequest(t, "GET", "/user2/repo1/issues") - resp := MakeRequest(req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + MakeRequest(t, req, http.StatusOK) } func TestNoLoginViewIssuesSortByType(t *testing.T) { @@ -48,8 +47,7 @@ func TestNoLoginViewIssuesSortByType(t *testing.T) { session := loginUser(t, user.Name) req := NewRequest(t, "GET", repo.RelLink()+"/issues?type=created_by") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) issuesSelection := getIssuesSelection(htmlDoc) @@ -73,15 +71,13 @@ func TestNoLoginViewIssue(t *testing.T) { prepareTestEnv(t) req := NewRequest(t, "GET", "/user2/repo1/issues/1") - resp := MakeRequest(req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + MakeRequest(t, req, http.StatusOK) } func testNewIssue(t *testing.T, session *TestSession, user, repo, title string) { req := NewRequest(t, "GET", path.Join(user, repo, "issues", "new")) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) link, exists := htmlDoc.doc.Find("form.ui.form").Attr("action") @@ -90,14 +86,10 @@ func testNewIssue(t *testing.T, session *TestSession, user, repo, title string) "_csrf": htmlDoc.GetCSRF(), "title": title, }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) - redirectedURL := resp.Headers["Location"] - assert.NotEmpty(t, redirectedURL, "Redirected URL is not found") - - req = NewRequest(t, "GET", redirectedURL[0]) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusFound) + + req = NewRequest(t, "GET", RedirectURL(t, resp)) + resp = session.MakeRequest(t, req, http.StatusOK) } func TestNewIssue(t *testing.T) { diff --git a/integrations/pull_compare_test.go b/integrations/pull_compare_test.go index 06826a08b..593b2f4d0 100644 --- a/integrations/pull_compare_test.go +++ b/integrations/pull_compare_test.go @@ -16,13 +16,12 @@ func TestPullCompare(t *testing.T) { session := loginUser(t, "user2") req := NewRequest(t, "GET", "/user2/repo1/pulls") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) link, exists := htmlDoc.doc.Find(".navbar").Find(".ui.green.button").Attr("href") assert.True(t, exists, "The template has changed") req = NewRequest(t, "GET", link) - resp = session.MakeRequest(t, req) + resp = session.MakeRequest(t, req, http.StatusOK) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) } diff --git a/integrations/pull_create_test.go b/integrations/pull_create_test.go index e03eae61d..ee7a92ce2 100644 --- a/integrations/pull_create_test.go +++ b/integrations/pull_create_test.go @@ -15,8 +15,7 @@ import ( func testPullCreate(t *testing.T, session *TestSession, user, repo, branch string) *TestResponse { req := NewRequest(t, "GET", path.Join(user, repo)) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) // Click the little green button to create a pull htmlDoc := NewHTMLParser(t, resp.Body) @@ -27,8 +26,7 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin } req = NewRequest(t, "GET", link) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusOK) // Submit the form for creating the pull htmlDoc = NewHTMLParser(t, resp.Body) @@ -38,8 +36,7 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin "_csrf": htmlDoc.GetCSRF(), "title": "This is a pull title", }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusFound) //TODO check the redirected URL diff --git a/integrations/pull_merge_test.go b/integrations/pull_merge_test.go index 9249f39bd..ddb18f802 100644 --- a/integrations/pull_merge_test.go +++ b/integrations/pull_merge_test.go @@ -15,36 +15,32 @@ import ( func testPullMerge(t *testing.T, session *TestSession, user, repo, pullnum string) *TestResponse { req := NewRequest(t, "GET", path.Join(user, repo, "pulls", pullnum)) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) - // Click the little green button to craete a pull + // Click the little green button to create a pull htmlDoc := NewHTMLParser(t, resp.Body) link, exists := htmlDoc.doc.Find("form.ui.form>button.ui.green.button").Parent().Attr("action") assert.True(t, exists, "The template has changed") req = NewRequestWithValues(t, "POST", link, map[string]string{ "_csrf": htmlDoc.GetCSRF(), }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusFound) return resp } func testPullCleanUp(t *testing.T, session *TestSession, user, repo, pullnum string) *TestResponse { req := NewRequest(t, "GET", path.Join(user, repo, "pulls", pullnum)) - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) - // Click the little green button to craete a pull + // Click the little green button to create a pull htmlDoc := NewHTMLParser(t, resp.Body) link, exists := htmlDoc.doc.Find(".comments .merge .delete-button").Attr("data-url") assert.True(t, exists, "The template has changed") req = NewRequestWithValues(t, "POST", link, map[string]string{ "_csrf": htmlDoc.GetCSRF(), }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusOK) return resp } @@ -56,10 +52,8 @@ func TestPullMerge(t *testing.T) { testEditFile(t, session, "user1", "repo1", "master", "README.md") resp := testPullCreate(t, session, "user1", "repo1", "master") - redirectedURL := resp.Headers["Location"] - assert.NotEmpty(t, redirectedURL, "Redirected URL is not found") - elem := strings.Split(redirectedURL[0], "/") + elem := strings.Split(RedirectURL(t, resp), "/") assert.EqualValues(t, "pulls", elem[3]) testPullMerge(t, session, elem[1], elem[2], elem[4]) } @@ -71,10 +65,8 @@ func TestPullCleanUpAfterMerge(t *testing.T) { testEditFileToNewBranch(t, session, "user1", "repo1", "master", "feature/test", "README.md") resp := testPullCreate(t, session, "user1", "repo1", "feature/test") - redirectedURL := resp.Headers["Location"] - assert.NotEmpty(t, redirectedURL, "Redirected URL is not found") - elem := strings.Split(redirectedURL[0], "/") + elem := strings.Split(RedirectURL(t, resp), "/") assert.EqualValues(t, "pulls", elem[3]) testPullMerge(t, session, elem[1], elem[2], elem[4]) @@ -92,8 +84,7 @@ func TestPullCleanUpAfterMerge(t *testing.T) { // Check branch deletion result req := NewRequest(t, "GET", respJSON.Redirect) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) resultMsg := htmlDoc.doc.Find(".ui.message>p").Text() diff --git a/integrations/release_test.go b/integrations/release_test.go index 43c283963..bce1c88fd 100644 --- a/integrations/release_test.go +++ b/integrations/release_test.go @@ -15,8 +15,7 @@ import ( func createNewRelease(t *testing.T, session *TestSession, repoURL, tag, title string, preRelease, draft bool) { req := NewRequest(t, "GET", repoURL+"/releases/new") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) link, exists := htmlDoc.doc.Find("form").Attr("action") @@ -37,17 +36,14 @@ func createNewRelease(t *testing.T, session *TestSession, repoURL, tag, title st } req = NewRequestWithValues(t, "POST", link, postData) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusFound) - redirectedURL := resp.Headers["Location"] - assert.NotEmpty(t, redirectedURL, "Redirected URL is not found") + RedirectURL(t, resp) // check that redirect URL exists } func checkLatestReleaseAndCount(t *testing.T, session *TestSession, repoURL, version, label string, count int) { req := NewRequest(t, "GET", repoURL+"/releases") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) labelText := htmlDoc.doc.Find("#release-list > li .meta .label").First().Text() @@ -64,16 +60,14 @@ func TestViewReleases(t *testing.T) { session := loginUser(t, "user2") req := NewRequest(t, "GET", "/user2/repo1/releases") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + session.MakeRequest(t, req, http.StatusOK) } func TestViewReleasesNoLogin(t *testing.T) { prepareTestEnv(t) req := NewRequest(t, "GET", "/user2/repo1/releases") - resp := MakeRequest(req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + MakeRequest(t, req, http.StatusOK) } func TestCreateRelease(t *testing.T) { diff --git a/integrations/repo_commits_test.go b/integrations/repo_commits_test.go index 69db24c45..bf3539824 100644 --- a/integrations/repo_commits_test.go +++ b/integrations/repo_commits_test.go @@ -21,8 +21,7 @@ func TestRepoCommits(t *testing.T) { // Request repository commits page req := NewRequest(t, "GET", "/user2/repo1/commits/master") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) doc := NewHTMLParser(t, resp.Body) commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href") @@ -37,8 +36,7 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) { // Request repository commits page req := NewRequest(t, "GET", "/user2/repo1/commits/master") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) doc := NewHTMLParser(t, resp.Body) // Get first commit URL @@ -56,12 +54,10 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) { }, ) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusCreated, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusCreated) req = NewRequest(t, "GET", "/user2/repo1/commits/master") - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusOK) doc = NewHTMLParser(t, resp.Body) // Check if commit status is displayed in message column diff --git a/integrations/repo_fork_test.go b/integrations/repo_fork_test.go index ccb7a4c19..bd6ec7915 100644 --- a/integrations/repo_fork_test.go +++ b/integrations/repo_fork_test.go @@ -14,21 +14,18 @@ import ( func testRepoFork(t *testing.T, session *TestSession) *TestResponse { // Step0: check the existence of the to-fork repo req := NewRequest(t, "GET", "/user1/repo1") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusNotFound) // Step1: go to the main page of repo req = NewRequest(t, "GET", "/user2/repo1") - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusOK) // Step2: click the fork button htmlDoc := NewHTMLParser(t, resp.Body) link, exists := htmlDoc.doc.Find("a.ui.button[href^=\"/repo/fork/\"]").Attr("href") assert.True(t, exists, "The template has changed") req = NewRequest(t, "GET", link) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusOK) // Step3: fill the form of the forking htmlDoc = NewHTMLParser(t, resp.Body) @@ -39,13 +36,11 @@ func testRepoFork(t *testing.T, session *TestSession) *TestResponse { "uid": "1", "repo_name": "repo1", }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusFound) // Step4: check the existence of the forked repo req = NewRequest(t, "GET", "/user1/repo1") - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusOK) return resp } diff --git a/integrations/repo_migrate_test.go b/integrations/repo_migrate_test.go index 85e4b218a..4307ddf90 100644 --- a/integrations/repo_migrate_test.go +++ b/integrations/repo_migrate_test.go @@ -13,8 +13,7 @@ import ( func testRepoMigrate(t testing.TB, session *TestSession, cloneAddr, repoName string) *TestResponse { req := NewRequest(t, "GET", "/repo/migrate") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) link, exists := htmlDoc.doc.Find("form.ui.form").Attr("action") @@ -30,8 +29,7 @@ func testRepoMigrate(t testing.TB, session *TestSession, cloneAddr, repoName str "repo_name": repoName, }, ) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + resp = session.MakeRequest(t, req, http.StatusFound) return resp } diff --git a/integrations/repo_test.go b/integrations/repo_test.go index 23df6d733..004156b44 100644 --- a/integrations/repo_test.go +++ b/integrations/repo_test.go @@ -7,24 +7,19 @@ package integrations import ( "net/http" "testing" - - "github.com/stretchr/testify/assert" ) func TestViewRepo(t *testing.T) { prepareTestEnv(t) req := NewRequest(t, "GET", "/user2/repo1") - resp := MakeRequest(req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + MakeRequest(t, req, http.StatusOK) req = NewRequest(t, "GET", "/user3/repo3") - resp = MakeRequest(req) - assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode) + MakeRequest(t, req, http.StatusNotFound) session := loginUser(t, "user1") - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode) + session.MakeRequest(t, req, http.StatusNotFound) } func TestViewRepo2(t *testing.T) { @@ -32,8 +27,7 @@ func TestViewRepo2(t *testing.T) { req := NewRequest(t, "GET", "/user3/repo3") session := loginUser(t, "user2") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + session.MakeRequest(t, req, http.StatusOK) } func TestViewRepo3(t *testing.T) { @@ -41,6 +35,5 @@ func TestViewRepo3(t *testing.T) { req := NewRequest(t, "GET", "/user3/repo3") session := loginUser(t, "user3") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + session.MakeRequest(t, req, http.StatusOK) } diff --git a/integrations/signup_test.go b/integrations/signup_test.go index bc32adb31..325c90632 100644 --- a/integrations/signup_test.go +++ b/integrations/signup_test.go @@ -9,8 +9,6 @@ import ( "testing" "code.gitea.io/gitea/modules/setting" - - "github.com/stretchr/testify/assert" ) func TestSignup(t *testing.T) { @@ -24,11 +22,9 @@ func TestSignup(t *testing.T) { "password": "examplePassword", "retype": "examplePassword", }) - resp := MakeRequest(req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + MakeRequest(t, req, http.StatusFound) // should be able to view new user's page req = NewRequest(t, "GET", "/exampleUser") - resp = MakeRequest(req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + MakeRequest(t, req, http.StatusOK) } diff --git a/integrations/user_test.go b/integrations/user_test.go index 4b7d81cfb..ddb46da31 100644 --- a/integrations/user_test.go +++ b/integrations/user_test.go @@ -18,27 +18,19 @@ func TestViewUser(t *testing.T) { prepareTestEnv(t) req := NewRequest(t, "GET", "/user2") - resp := MakeRequest(req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + MakeRequest(t, req, http.StatusOK) } func TestRenameUsername(t *testing.T) { prepareTestEnv(t) session := loginUser(t, "user2") - - req := NewRequest(t, "GET", "/user/settings") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - - htmlDoc := NewHTMLParser(t, resp.Body) - req = NewRequestWithValues(t, "POST", "/user/settings", map[string]string{ - "_csrf": htmlDoc.GetCSRF(), + req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{ + "_csrf": GetCSRF(t, session, "/user/settings"), "name": "newUsername", "email": "user2@example.com", }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + session.MakeRequest(t, req, http.StatusFound) models.AssertExistsAndLoadBean(t, &models.User{Name: "newUsername"}) models.AssertNotExistsBean(t, &models.User{Name: "user2"}) @@ -58,19 +50,14 @@ func TestRenameInvalidUsername(t *testing.T) { session := loginUser(t, "user2") for _, invalidUsername := range invalidUsernames { t.Logf("Testing username %s", invalidUsername) - req := NewRequest(t, "GET", "/user/settings") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - htmlDoc := NewHTMLParser(t, resp.Body) - req = NewRequestWithValues(t, "POST", "/user/settings", map[string]string{ - "_csrf": htmlDoc.GetCSRF(), + req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{ + "_csrf": GetCSRF(t, session, "/user/settings"), "name": invalidUsername, "email": "user2@example.com", }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - htmlDoc = NewHTMLParser(t, resp.Body) + resp := session.MakeRequest(t, req, http.StatusOK) + htmlDoc := NewHTMLParser(t, resp.Body) assert.Contains(t, htmlDoc.doc.Find(".ui.negative.message").Text(), i18n.Tr("en", "form.alpha_dash_dot_error"), @@ -92,23 +79,16 @@ func TestRenameReservedUsername(t *testing.T) { session := loginUser(t, "user2") for _, reservedUsername := range reservedUsernames { t.Logf("Testing username %s", reservedUsername) - req := NewRequest(t, "GET", "/user/settings") - resp := session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - - htmlDoc := NewHTMLParser(t, resp.Body) - req = NewRequestWithValues(t, "POST", "/user/settings", map[string]string{ - "_csrf": htmlDoc.GetCSRF(), + req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{ + "_csrf": GetCSRF(t, session, "/user/settings"), "name": reservedUsername, "email": "user2@example.com", }) - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + resp := session.MakeRequest(t, req, http.StatusFound) - req = NewRequest(t, "GET", "/user/settings") - resp = session.MakeRequest(t, req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - htmlDoc = NewHTMLParser(t, resp.Body) + req = NewRequest(t, "GET", RedirectURL(t, resp)) + resp = session.MakeRequest(t, req, http.StatusOK) + htmlDoc := NewHTMLParser(t, resp.Body) assert.Contains(t, htmlDoc.doc.Find(".ui.negative.message").Text(), i18n.Tr("en", "user.newName_reserved"), diff --git a/integrations/version_test.go b/integrations/version_test.go index 2234366b1..95b4dbc95 100644 --- a/integrations/version_test.go +++ b/integrations/version_test.go @@ -19,8 +19,7 @@ func TestVersion(t *testing.T) { setting.AppVer = "1.1.0+dev" req := NewRequest(t, "GET", "/api/v1/version") - resp := MakeRequest(req) - assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + resp := MakeRequest(t, req, http.StatusOK) var version gitea.ServerVersion DecodeJSON(t, resp, &version)