From ce9b86082c4824917023b1bb480648d0cc56dd04 Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Sat, 17 Jun 2017 00:49:45 -0400 Subject: [PATCH] Consolidate boilerplate in integration tests (#1979) --- integrations/api_team_test.go | 2 +- integrations/change_default_branch_test.go | 32 ++++----- integrations/delete_user_test.go | 14 ++-- integrations/editor_test.go | 82 +++++++++------------- integrations/html_helper.go | 15 ++-- integrations/integration_test.go | 49 ++++++++----- integrations/issue_test.go | 5 +- integrations/pull_compare_test.go | 5 +- integrations/pull_create_test.go | 20 ++---- integrations/pull_merge_test.go | 15 ++-- integrations/release_test.go | 2 +- integrations/repo_commits_test.go | 26 ++++--- integrations/repo_fork_test.go | 22 +++--- integrations/repo_test.go | 6 +- integrations/signup_test.go | 16 ++--- models/fixtures/user.yml | 2 + 16 files changed, 147 insertions(+), 166 deletions(-) diff --git a/integrations/api_team_test.go b/integrations/api_team_test.go index 69198cd7e..910304204 100644 --- a/integrations/api_team_test.go +++ b/integrations/api_team_test.go @@ -23,7 +23,7 @@ func TestAPITeam(t *testing.T) { team := models.AssertExistsAndLoadBean(t, &models.Team{ID: teamUser.TeamID}).(*models.Team) user := models.AssertExistsAndLoadBean(t, &models.User{ID: teamUser.UID}).(*models.User) - session := loginUser(t, user.Name, "password") + session := loginUser(t, user.Name) url := fmt.Sprintf("/api/v1/teams/%d", teamUser.TeamID) req := NewRequest(t, "GET", url) resp := session.MakeRequest(t, req) diff --git a/integrations/change_default_branch_test.go b/integrations/change_default_branch_test.go index 03ece46a5..d34cd176c 100644 --- a/integrations/change_default_branch_test.go +++ b/integrations/change_default_branch_test.go @@ -5,10 +5,8 @@ package integrations import ( - "bytes" "fmt" "net/http" - "net/url" "testing" "code.gitea.io/gitea/models" @@ -21,21 +19,19 @@ func TestChangeDefaultBranch(t *testing.T) { repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) - session := loginUser(t, owner.Name, "password") + 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, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + doc := NewHtmlParser(t, resp.Body) - req = NewRequestBody(t, "POST", branchesURL, - bytes.NewBufferString(url.Values{ - "_csrf": []string{doc.GetInputValueByName("_csrf")}, - "action": []string{"default_branch"}, - "branch": []string{"DefaultBranch"}, - }.Encode())) + req = NewRequestWithValues(t, "POST", branchesURL, map[string]string{ + "_csrf": doc.GetCSRF(), + "action": "default_branch", + "branch": "DefaultBranch", + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusFound, resp.HeaderCode) @@ -43,15 +39,13 @@ func TestChangeDefaultBranch(t *testing.T) { req = NewRequest(t, "GET", branchesURL) resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc, err = NewHtmlParser(resp.Body) - assert.NoError(t, err) + doc = NewHtmlParser(t, resp.Body) - req = NewRequestBody(t, "POST", branchesURL, - bytes.NewBufferString(url.Values{ - "_csrf": []string{doc.GetInputValueByName("_csrf")}, - "action": []string{"default_branch"}, - "branch": []string{"does_not_exist"}, - }.Encode())) + req = NewRequestWithValues(t, "POST", branchesURL, map[string]string{ + "_csrf": doc.GetInputValueByName("_csrf"), + "action": "default_branch", + "branch": "does_not_exist", + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode) diff --git a/integrations/delete_user_test.go b/integrations/delete_user_test.go index 2040830f2..c5230d022 100644 --- a/integrations/delete_user_test.go +++ b/integrations/delete_user_test.go @@ -5,9 +5,7 @@ package integrations import ( - "bytes" "net/http" - "net/url" "testing" "code.gitea.io/gitea/models" @@ -18,18 +16,16 @@ import ( func TestDeleteUser(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user1", "password") + session := loginUser(t, "user1") req := NewRequest(t, "GET", "/admin/users/8") resp := session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) - req = NewRequestBody(t, "POST", "/admin/users/8/delete", - bytes.NewBufferString(url.Values{ - "_csrf": []string{doc.GetInputValueByName("_csrf")}, - }.Encode())) + doc := NewHtmlParser(t, resp.Body) + req = NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{ + "_csrf": doc.GetCSRF(), + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) diff --git a/integrations/editor_test.go b/integrations/editor_test.go index 540caca26..10d9dc531 100644 --- a/integrations/editor_test.go +++ b/integrations/editor_test.go @@ -5,9 +5,7 @@ package integrations import ( - "bytes" "net/http" - "net/url" "path" "testing" @@ -17,28 +15,25 @@ import ( func TestCreateFile(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user2", "password") + session := loginUser(t, "user2") // Request editor page req := NewRequest(t, "GET", "/user2/repo1/_new/master/") resp := session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + doc := NewHtmlParser(t, resp.Body) lastCommit := doc.GetInputValueByName("last_commit") assert.NotEmpty(t, lastCommit) // Save new file to master branch - req = NewRequestBody(t, "POST", "/user2/repo1/_new/master/", - bytes.NewBufferString(url.Values{ - "_csrf": []string{doc.GetInputValueByName("_csrf")}, - "last_commit": []string{lastCommit}, - "tree_path": []string{"test.txt"}, - "content": []string{"Content"}, - "commit_choice": []string{"direct"}, - }.Encode()), - ) + req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{ + "_csrf": doc.GetCSRF(), + "last_commit": lastCommit, + "tree_path": "test.txt", + "content": "Content", + "commit_choice": "direct", + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusFound, resp.HeaderCode) @@ -47,25 +42,21 @@ func TestCreateFile(t *testing.T) { func TestCreateFileOnProtectedBranch(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user2", "password") + 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, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + doc := NewHtmlParser(t, resp.Body) // Change master branch to protected - req = NewRequestBody(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", - bytes.NewBufferString(url.Values{ - "_csrf": []string{doc.GetInputValueByName("_csrf")}, - "branchName": []string{"master"}, - "canPush": []string{"true"}, - }.Encode()), - ) - assert.NoError(t, err) + req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", map[string]string{ + "_csrf": doc.GetCSRF(), + "branchName": "master", + "canPush": "true", + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) @@ -79,21 +70,19 @@ func TestCreateFileOnProtectedBranch(t *testing.T) { resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc, err = NewHtmlParser(resp.Body) - assert.NoError(t, err) + doc = NewHtmlParser(t, resp.Body) lastCommit := doc.GetInputValueByName("last_commit") assert.NotEmpty(t, lastCommit) // Save new file to master branch - req = NewRequestBody(t, "POST", "/user2/repo1/_new/master/", - bytes.NewBufferString(url.Values{ - "_csrf": []string{doc.GetInputValueByName("_csrf")}, - "last_commit": []string{lastCommit}, - "tree_path": []string{"test.txt"}, - "content": []string{"Content"}, - "commit_choice": []string{"direct"}, - }.Encode()), - ) + req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{ + "_csrf": doc.GetCSRF(), + "last_commit": lastCommit, + "tree_path": "test.txt", + "content": "Content", + "commit_choice": "direct", + }) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) @@ -110,20 +99,19 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa resp := session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - htmlDoc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + htmlDoc := NewHtmlParser(t, resp.Body) lastCommit := htmlDoc.GetInputValueByName("last_commit") assert.NotEmpty(t, lastCommit) // Submit the edits - req = NewRequestBody(t, "POST", path.Join(user, repo, "_edit", branch, filePath), - bytes.NewBufferString(url.Values{ - "_csrf": []string{htmlDoc.GetInputValueByName("_csrf")}, - "last_commit": []string{lastCommit}, - "tree_path": []string{filePath}, - "content": []string{newContent}, - "commit_choice": []string{"direct"}, - }.Encode()), + req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath), + map[string]string{ + "_csrf": htmlDoc.GetCSRF(), + "last_commit": lastCommit, + "tree_path": filePath, + "content": newContent, + "commit_choice": "direct", + }, ) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) @@ -140,6 +128,6 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa func TestEditFile(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user2", "password") + session := loginUser(t, "user2") testEditFile(t, session, "user2", "repo1", "master", "README.md") } diff --git a/integrations/html_helper.go b/integrations/html_helper.go index 63f7340c5..3bddd2930 100644 --- a/integrations/html_helper.go +++ b/integrations/html_helper.go @@ -6,21 +6,20 @@ package integrations import ( "bytes" + "testing" "github.com/PuerkitoBio/goquery" + "github.com/stretchr/testify/assert" ) type HtmlDoc struct { doc *goquery.Document } -func NewHtmlParser(content []byte) (*HtmlDoc, error) { +func NewHtmlParser(t *testing.T, content []byte) *HtmlDoc { doc, err := goquery.NewDocumentFromReader(bytes.NewReader(content)) - if err != nil { - return nil, err - } - - return &HtmlDoc{doc: doc}, nil + assert.NoError(t, err) + return &HtmlDoc{doc: doc} } func (doc *HtmlDoc) GetInputValueById(id string) string { @@ -32,3 +31,7 @@ func (doc *HtmlDoc) GetInputValueByName(name string) string { text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value") return text } + +func (doc *HtmlDoc) GetCSRF() string { + return doc.GetInputValueByName("_csrf") +} diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 5c0da501d..cc5a57f2d 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -7,6 +7,7 @@ package integrations import ( "bytes" "database/sql" + "encoding/json" "fmt" "io" "log" @@ -155,21 +156,23 @@ func (s *TestSession) MakeRequest(t *testing.T, req *http.Request) *TestResponse return resp } -func loginUser(t *testing.T, userName, password string) *TestSession { +const userPassword = "password" + +func loginUser(t *testing.T, userName string) *TestSession { + return loginUserWithPassword(t, userName, userPassword) +} + +func loginUserWithPassword(t *testing.T, userName, password string) *TestSession { req := NewRequest(t, "GET", "/user/login") resp := MakeRequest(req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) - - req = NewRequestBody(t, "POST", "/user/login", - bytes.NewBufferString(url.Values{ - "_csrf": []string{doc.GetInputValueByName("_csrf")}, - "user_name": []string{userName}, - "password": []string{password}, - }.Encode()), - ) + doc := NewHtmlParser(t, resp.Body) + req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{ + "_csrf": doc.GetCSRF(), + "user_name": userName, + "password": password, + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = MakeRequest(req) assert.EqualValues(t, http.StatusFound, resp.HeaderCode) @@ -211,14 +214,28 @@ type TestResponse struct { Headers http.Header } -func NewRequest(t *testing.T, method, url string) *http.Request { - return NewRequestBody(t, method, url, nil) +func NewRequest(t *testing.T, method, urlStr string) *http.Request { + return NewRequestWithBody(t, method, urlStr, nil) } -func NewRequestBody(t *testing.T, method, url string, body io.Reader) *http.Request { - request, err := http.NewRequest(method, url, body) +func NewRequestWithValues(t *testing.T, method, urlStr string, values map[string]string) *http.Request { + urlValues := url.Values{} + for key, value := range values { + urlValues[key] = []string{value} + } + return NewRequestWithBody(t, method, urlStr, bytes.NewBufferString(urlValues.Encode())) +} + +func NewRequestWithJSON(t *testing.T, method, urlStr string, v interface{}) *http.Request { + jsonBytes, err := json.Marshal(v) assert.NoError(t, err) - request.RequestURI = url + return NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes)) +} + +func NewRequestWithBody(t *testing.T, method, urlStr string, body io.Reader) *http.Request { + request, err := http.NewRequest(method, urlStr, body) + assert.NoError(t, err) + request.RequestURI = urlStr return request } diff --git a/integrations/issue_test.go b/integrations/issue_test.go index f94f11a86..4d78ab4b9 100644 --- a/integrations/issue_test.go +++ b/integrations/issue_test.go @@ -45,13 +45,12 @@ func TestNoLoginViewIssuesSortByType(t *testing.T) { repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repo.Owner = models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) - session := loginUser(t, user.Name, "password") + 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) - htmlDoc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + htmlDoc := NewHtmlParser(t, resp.Body) issuesSelection := getIssuesSelection(htmlDoc) expectedNumIssues := models.GetCount(t, &models.Issue{RepoID: repo.ID, PosterID: user.ID}, diff --git a/integrations/pull_compare_test.go b/integrations/pull_compare_test.go index 737d5ab82..5ff7b4b6c 100644 --- a/integrations/pull_compare_test.go +++ b/integrations/pull_compare_test.go @@ -14,12 +14,11 @@ import ( func TestPullCompare(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user2", "password") + session := loginUser(t, "user2") req := NewRequest(t, "GET", "/user2/repo1/pulls") resp := session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - htmlDoc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + 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") diff --git a/integrations/pull_create_test.go b/integrations/pull_create_test.go index ac8df33c6..f518c1bb5 100644 --- a/integrations/pull_create_test.go +++ b/integrations/pull_create_test.go @@ -5,9 +5,7 @@ package integrations import ( - "bytes" "net/http" - "net/url" "path" "testing" @@ -20,8 +18,7 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin assert.EqualValues(t, http.StatusOK, resp.HeaderCode) // Click the little green button to create a pull - htmlDoc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + htmlDoc := NewHtmlParser(t, resp.Body) link, exists := htmlDoc.doc.Find("button.ui.green.small.button").Parent().Attr("href") assert.True(t, exists, "The template has changed") @@ -30,16 +27,13 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin assert.EqualValues(t, http.StatusOK, resp.HeaderCode) // Submit the form for creating the pull - htmlDoc, err = NewHtmlParser(resp.Body) - assert.NoError(t, err) + htmlDoc = NewHtmlParser(t, resp.Body) link, exists = htmlDoc.doc.Find("form.ui.form").Attr("action") assert.True(t, exists, "The template has changed") - req = NewRequestBody(t, "POST", link, - bytes.NewBufferString(url.Values{ - "_csrf": []string{htmlDoc.GetInputValueByName("_csrf")}, - "title": []string{"This is a pull title"}, - }.Encode()), - ) + req = NewRequestWithValues(t, "POST", link, map[string]string{ + "_csrf": htmlDoc.GetCSRF(), + "title": "This is a pull title", + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusFound, resp.HeaderCode) @@ -51,7 +45,7 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin func TestPullCreate(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user1", "password") + session := loginUser(t, "user1") testRepoFork(t, session) testEditFile(t, session, "user1", "repo1", "master", "README.md") testPullCreate(t, session, "user1", "repo1", "master") diff --git a/integrations/pull_merge_test.go b/integrations/pull_merge_test.go index 1b9337a67..33d996011 100644 --- a/integrations/pull_merge_test.go +++ b/integrations/pull_merge_test.go @@ -5,9 +5,7 @@ package integrations import ( - "bytes" "net/http" - "net/url" "path" "strings" "testing" @@ -21,15 +19,12 @@ func testPullMerge(t *testing.T, session *TestSession, user, repo, pullnum strin assert.EqualValues(t, http.StatusOK, resp.HeaderCode) // Click the little green button to craete a pull - htmlDoc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + 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 = NewRequestBody(t, "POST", link, - bytes.NewBufferString(url.Values{ - "_csrf": []string{htmlDoc.GetInputValueByName("_csrf")}, - }.Encode()), - ) + req = NewRequestWithValues(t, "POST", link, map[string]string{ + "_csrf": htmlDoc.GetCSRF(), + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusFound, resp.HeaderCode) @@ -39,7 +34,7 @@ func testPullMerge(t *testing.T, session *TestSession, user, repo, pullnum strin func TestPullMerge(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user1", "password") + session := loginUser(t, "user1") testRepoFork(t, session) testEditFile(t, session, "user1", "repo1", "master", "README.md") diff --git a/integrations/release_test.go b/integrations/release_test.go index 8f29ddc63..ce33ad61d 100644 --- a/integrations/release_test.go +++ b/integrations/release_test.go @@ -14,7 +14,7 @@ import ( func TestViewReleases(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user2", "password") + session := loginUser(t, "user2") req := NewRequest(t, "GET", "/user2/repo1/releases") resp := session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) diff --git a/integrations/repo_commits_test.go b/integrations/repo_commits_test.go index 476458e63..8343cf22a 100644 --- a/integrations/repo_commits_test.go +++ b/integrations/repo_commits_test.go @@ -5,26 +5,26 @@ package integrations import ( - "bytes" "net/http" "path" "testing" + api "code.gitea.io/sdk/gitea" + "github.com/stretchr/testify/assert" ) func TestRepoCommits(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user2", "password") + session := loginUser(t, "user2") // Request repository commits page req := NewRequest(t, "GET", "/user2/repo1/commits/master") resp := session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + doc := NewHtmlParser(t, resp.Body) commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href") assert.True(t, exists) assert.NotEmpty(t, commitURL) @@ -33,23 +33,28 @@ func TestRepoCommits(t *testing.T) { func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) { prepareTestEnv(t) - session := loginUser(t, "user2", "password") + session := loginUser(t, "user2") // Request repository commits page req := NewRequest(t, "GET", "/user2/repo1/commits/master") resp := session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + doc := NewHtmlParser(t, resp.Body) // Get first commit URL commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href") assert.True(t, exists) assert.NotEmpty(t, commitURL) // Call API to add status for commit - req = NewRequestBody(t, "POST", "/api/v1/repos/user2/repo1/statuses/"+path.Base(commitURL), - bytes.NewBufferString("{\"state\":\""+state+"\", \"target_url\": \"http://test.ci/\", \"description\": \"\", \"context\": \"testci\"}")) + req = NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/statuses/"+path.Base(commitURL), + api.CreateStatusOption{ + State: api.StatusState(state), + TargetURL: "http://test.ci/", + Description: "", + Context: "testci", + }, + ) req.Header.Add("Content-Type", "application/json") resp = session.MakeRequest(t, req) @@ -59,8 +64,7 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) { resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) - doc, err = NewHtmlParser(resp.Body) - assert.NoError(t, err) + doc = NewHtmlParser(t, resp.Body) // Check if commit status is displayed in message column sel := doc.doc.Find("#commits-table tbody tr td.message i.commit-status") assert.Equal(t, sel.Length(), 1) diff --git a/integrations/repo_fork_test.go b/integrations/repo_fork_test.go index a0cf85a01..7aebd5dbd 100644 --- a/integrations/repo_fork_test.go +++ b/integrations/repo_fork_test.go @@ -5,9 +5,7 @@ package integrations import ( - "bytes" "net/http" - "net/url" "testing" "github.com/stretchr/testify/assert" @@ -25,8 +23,7 @@ func testRepoFork(t *testing.T, session *TestSession) *TestResponse { assert.EqualValues(t, http.StatusOK, resp.HeaderCode) // Step2: click the fork button - htmlDoc, err := NewHtmlParser(resp.Body) - assert.NoError(t, err) + 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) @@ -34,17 +31,14 @@ func testRepoFork(t *testing.T, session *TestSession) *TestResponse { assert.EqualValues(t, http.StatusOK, resp.HeaderCode) // Step3: fill the form of the forking - htmlDoc, err = NewHtmlParser(resp.Body) - assert.NoError(t, err) + htmlDoc = NewHtmlParser(t, resp.Body) link, exists = htmlDoc.doc.Find("form.ui.form[action^=\"/repo/fork/\"]").Attr("action") assert.True(t, exists, "The template has changed") - req = NewRequestBody(t, "POST", link, - bytes.NewBufferString(url.Values{ - "_csrf": []string{htmlDoc.GetInputValueByName("_csrf")}, - "uid": []string{"1"}, - "repo_name": []string{"repo1"}, - }.Encode()), - ) + req = NewRequestWithValues(t, "POST", link, map[string]string{ + "_csrf": htmlDoc.GetCSRF(), + "uid": "1", + "repo_name": "repo1", + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusFound, resp.HeaderCode) @@ -59,6 +53,6 @@ func testRepoFork(t *testing.T, session *TestSession) *TestResponse { func TestRepoFork(t *testing.T) { prepareTestEnv(t) - session := loginUser(t, "user1", "password") + session := loginUser(t, "user1") testRepoFork(t, session) } diff --git a/integrations/repo_test.go b/integrations/repo_test.go index a602d96f7..23df6d733 100644 --- a/integrations/repo_test.go +++ b/integrations/repo_test.go @@ -22,7 +22,7 @@ func TestViewRepo(t *testing.T) { resp = MakeRequest(req) assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode) - session := loginUser(t, "user1", "password") + session := loginUser(t, "user1") resp = session.MakeRequest(t, req) assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode) } @@ -31,7 +31,7 @@ func TestViewRepo2(t *testing.T) { prepareTestEnv(t) req := NewRequest(t, "GET", "/user3/repo3") - session := loginUser(t, "user2", "password") + session := loginUser(t, "user2") resp := session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) } @@ -40,7 +40,7 @@ func TestViewRepo3(t *testing.T) { prepareTestEnv(t) req := NewRequest(t, "GET", "/user3/repo3") - session := loginUser(t, "user3", "password") + session := loginUser(t, "user3") resp := session.MakeRequest(t, req) assert.EqualValues(t, http.StatusOK, resp.HeaderCode) } diff --git a/integrations/signup_test.go b/integrations/signup_test.go index 4bb84c1a2..6d4768ec1 100644 --- a/integrations/signup_test.go +++ b/integrations/signup_test.go @@ -5,9 +5,7 @@ package integrations import ( - "bytes" "net/http" - "net/url" "testing" "code.gitea.io/gitea/modules/setting" @@ -20,14 +18,12 @@ func TestSignup(t *testing.T) { setting.Service.EnableCaptcha = false - req := NewRequestBody(t, "POST", "/user/sign_up", - bytes.NewBufferString(url.Values{ - "user_name": []string{"exampleUser"}, - "email": []string{"exampleUser@example.com"}, - "password": []string{"examplePassword"}, - "retype": []string{"examplePassword"}, - }.Encode()), - ) + req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{ + "user_name": "exampleUser", + "email": "exampleUser@example.com", + "password": "examplePassword", + "retype": "examplePassword", + }) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp := MakeRequest(req) assert.EqualValues(t, http.StatusFound, resp.HeaderCode) diff --git a/models/fixtures/user.yml b/models/fixtures/user.yml index dbf991bb8..89e8785a7 100644 --- a/models/fixtures/user.yml +++ b/models/fixtures/user.yml @@ -1,3 +1,5 @@ +# NOTE: all users should have a password of "password" + - # NOTE: this user (id=1) is the admin id: 1 lower_name: user1