Browse Source

Consolidate boilerplate in integration tests (#1979)

Ethan Koenig 2 years ago
parent
commit
ce9b86082c

+ 1 - 1
integrations/api_team_test.go

@@ -23,7 +23,7 @@ func TestAPITeam(t *testing.T) {
23 23
 	team := models.AssertExistsAndLoadBean(t, &models.Team{ID: teamUser.TeamID}).(*models.Team)
24 24
 	user := models.AssertExistsAndLoadBean(t, &models.User{ID: teamUser.UID}).(*models.User)
25 25
 
26
-	session := loginUser(t, user.Name, "password")
26
+	session := loginUser(t, user.Name)
27 27
 	url := fmt.Sprintf("/api/v1/teams/%d", teamUser.TeamID)
28 28
 	req := NewRequest(t, "GET", url)
29 29
 	resp := session.MakeRequest(t, req)

+ 15 - 21
integrations/change_default_branch_test.go

@@ -5,10 +5,8 @@
5 5
 package integrations
6 6
 
7 7
 import (
8
-	"bytes"
9 8
 	"fmt"
10 9
 	"net/http"
11
-	"net/url"
12 10
 	"testing"
13 11
 
14 12
 	"code.gitea.io/gitea/models"
@@ -21,21 +19,19 @@ func TestChangeDefaultBranch(t *testing.T) {
21 19
 	repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
22 20
 	owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
23 21
 
24
-	session := loginUser(t, owner.Name, "password")
22
+	session := loginUser(t, owner.Name)
25 23
 	branchesURL := fmt.Sprintf("/%s/%s/settings/branches", owner.Name, repo.Name)
26 24
 
27 25
 	req := NewRequest(t, "GET", branchesURL)
28 26
 	resp := session.MakeRequest(t, req)
29 27
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
30
-	doc, err := NewHtmlParser(resp.Body)
31
-	assert.NoError(t, err)
32
-
33
-	req = NewRequestBody(t, "POST", branchesURL,
34
-		bytes.NewBufferString(url.Values{
35
-			"_csrf":  []string{doc.GetInputValueByName("_csrf")},
36
-			"action": []string{"default_branch"},
37
-			"branch": []string{"DefaultBranch"},
38
-		}.Encode()))
28
+	doc := NewHtmlParser(t, resp.Body)
29
+
30
+	req = NewRequestWithValues(t, "POST", branchesURL, map[string]string{
31
+		"_csrf":  doc.GetCSRF(),
32
+		"action": "default_branch",
33
+		"branch": "DefaultBranch",
34
+	})
39 35
 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
40 36
 	resp = session.MakeRequest(t, req)
41 37
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
@@ -43,15 +39,13 @@ func TestChangeDefaultBranch(t *testing.T) {
43 39
 	req = NewRequest(t, "GET", branchesURL)
44 40
 	resp = session.MakeRequest(t, req)
45 41
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
46
-	doc, err = NewHtmlParser(resp.Body)
47
-	assert.NoError(t, err)
48
-
49
-	req = NewRequestBody(t, "POST", branchesURL,
50
-		bytes.NewBufferString(url.Values{
51
-			"_csrf":  []string{doc.GetInputValueByName("_csrf")},
52
-			"action": []string{"default_branch"},
53
-			"branch": []string{"does_not_exist"},
54
-		}.Encode()))
42
+	doc = NewHtmlParser(t, resp.Body)
43
+
44
+	req = NewRequestWithValues(t, "POST", branchesURL, map[string]string{
45
+		"_csrf":  doc.GetInputValueByName("_csrf"),
46
+		"action": "default_branch",
47
+		"branch": "does_not_exist",
48
+	})
55 49
 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
56 50
 	resp = session.MakeRequest(t, req)
57 51
 	assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode)

+ 5 - 9
integrations/delete_user_test.go

@@ -5,9 +5,7 @@
5 5
 package integrations
6 6
 
7 7
 import (
8
-	"bytes"
9 8
 	"net/http"
10
-	"net/url"
11 9
 	"testing"
12 10
 
13 11
 	"code.gitea.io/gitea/models"
@@ -18,18 +16,16 @@ import (
18 16
 func TestDeleteUser(t *testing.T) {
19 17
 	prepareTestEnv(t)
20 18
 
21
-	session := loginUser(t, "user1", "password")
19
+	session := loginUser(t, "user1")
22 20
 
23 21
 	req := NewRequest(t, "GET", "/admin/users/8")
24 22
 	resp := session.MakeRequest(t, req)
25 23
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
26 24
 
27
-	doc, err := NewHtmlParser(resp.Body)
28
-	assert.NoError(t, err)
29
-	req = NewRequestBody(t, "POST", "/admin/users/8/delete",
30
-		bytes.NewBufferString(url.Values{
31
-			"_csrf": []string{doc.GetInputValueByName("_csrf")},
32
-		}.Encode()))
25
+	doc := NewHtmlParser(t, resp.Body)
26
+	req = NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{
27
+		"_csrf": doc.GetCSRF(),
28
+	})
33 29
 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
34 30
 	resp = session.MakeRequest(t, req)
35 31
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

+ 35 - 47
integrations/editor_test.go

@@ -5,9 +5,7 @@
5 5
 package integrations
6 6
 
7 7
 import (
8
-	"bytes"
9 8
 	"net/http"
10
-	"net/url"
11 9
 	"path"
12 10
 	"testing"
13 11
 
@@ -17,28 +15,25 @@ import (
17 15
 func TestCreateFile(t *testing.T) {
18 16
 	prepareTestEnv(t)
19 17
 
20
-	session := loginUser(t, "user2", "password")
18
+	session := loginUser(t, "user2")
21 19
 
22 20
 	// Request editor page
23 21
 	req := NewRequest(t, "GET", "/user2/repo1/_new/master/")
24 22
 	resp := session.MakeRequest(t, req)
25 23
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
26 24
 
27
-	doc, err := NewHtmlParser(resp.Body)
28
-	assert.NoError(t, err)
25
+	doc := NewHtmlParser(t, resp.Body)
29 26
 	lastCommit := doc.GetInputValueByName("last_commit")
30 27
 	assert.NotEmpty(t, lastCommit)
31 28
 
32 29
 	// Save new file to master branch
33
-	req = NewRequestBody(t, "POST", "/user2/repo1/_new/master/",
34
-		bytes.NewBufferString(url.Values{
35
-			"_csrf":         []string{doc.GetInputValueByName("_csrf")},
36
-			"last_commit":   []string{lastCommit},
37
-			"tree_path":     []string{"test.txt"},
38
-			"content":       []string{"Content"},
39
-			"commit_choice": []string{"direct"},
40
-		}.Encode()),
41
-	)
30
+	req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
31
+		"_csrf":         doc.GetCSRF(),
32
+		"last_commit":   lastCommit,
33
+		"tree_path":     "test.txt",
34
+		"content":       "Content",
35
+		"commit_choice": "direct",
36
+	})
42 37
 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
43 38
 	resp = session.MakeRequest(t, req)
44 39
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
@@ -47,25 +42,21 @@ func TestCreateFile(t *testing.T) {
47 42
 func TestCreateFileOnProtectedBranch(t *testing.T) {
48 43
 	prepareTestEnv(t)
49 44
 
50
-	session := loginUser(t, "user2", "password")
45
+	session := loginUser(t, "user2")
51 46
 
52 47
 	// Open repository branch settings
53 48
 	req := NewRequest(t, "GET", "/user2/repo1/settings/branches")
54 49
 	resp := session.MakeRequest(t, req)
55 50
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
56 51
 
57
-	doc, err := NewHtmlParser(resp.Body)
58
-	assert.NoError(t, err)
52
+	doc := NewHtmlParser(t, resp.Body)
59 53
 
60 54
 	// Change master branch to protected
61
-	req = NewRequestBody(t, "POST", "/user2/repo1/settings/branches?action=protected_branch",
62
-		bytes.NewBufferString(url.Values{
63
-			"_csrf":      []string{doc.GetInputValueByName("_csrf")},
64
-			"branchName": []string{"master"},
65
-			"canPush":    []string{"true"},
66
-		}.Encode()),
67
-	)
68
-	assert.NoError(t, err)
55
+	req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches?action=protected_branch", map[string]string{
56
+		"_csrf":      doc.GetCSRF(),
57
+		"branchName": "master",
58
+		"canPush":    "true",
59
+	})
69 60
 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
70 61
 	resp = session.MakeRequest(t, req)
71 62
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
@@ -79,21 +70,19 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
79 70
 	resp = session.MakeRequest(t, req)
80 71
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
81 72
 
82
-	doc, err = NewHtmlParser(resp.Body)
83
-	assert.NoError(t, err)
73
+	doc = NewHtmlParser(t, resp.Body)
84 74
 	lastCommit := doc.GetInputValueByName("last_commit")
85 75
 	assert.NotEmpty(t, lastCommit)
86 76
 
87 77
 	// Save new file to master branch
88
-	req = NewRequestBody(t, "POST", "/user2/repo1/_new/master/",
89
-		bytes.NewBufferString(url.Values{
90
-			"_csrf":         []string{doc.GetInputValueByName("_csrf")},
91
-			"last_commit":   []string{lastCommit},
92
-			"tree_path":     []string{"test.txt"},
93
-			"content":       []string{"Content"},
94
-			"commit_choice": []string{"direct"},
95
-		}.Encode()),
96
-	)
78
+	req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
79
+		"_csrf":         doc.GetCSRF(),
80
+		"last_commit":   lastCommit,
81
+		"tree_path":     "test.txt",
82
+		"content":       "Content",
83
+		"commit_choice": "direct",
84
+	})
85
+
97 86
 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
98 87
 	resp = session.MakeRequest(t, req)
99 88
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
@@ -110,20 +99,19 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
110 99
 	resp := session.MakeRequest(t, req)
111 100
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
112 101
 
113
-	htmlDoc, err := NewHtmlParser(resp.Body)
114
-	assert.NoError(t, err)
102
+	htmlDoc := NewHtmlParser(t, resp.Body)
115 103
 	lastCommit := htmlDoc.GetInputValueByName("last_commit")
116 104
 	assert.NotEmpty(t, lastCommit)
117 105
 
118 106
 	// Submit the edits
119
-	req = NewRequestBody(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
120
-		bytes.NewBufferString(url.Values{
121
-			"_csrf":         []string{htmlDoc.GetInputValueByName("_csrf")},
122
-			"last_commit":   []string{lastCommit},
123
-			"tree_path":     []string{filePath},
124
-			"content":       []string{newContent},
125
-			"commit_choice": []string{"direct"},
126
-		}.Encode()),
107
+	req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
108
+		map[string]string{
109
+			"_csrf":         htmlDoc.GetCSRF(),
110
+			"last_commit":   lastCommit,
111
+			"tree_path":     filePath,
112
+			"content":       newContent,
113
+			"commit_choice": "direct",
114
+		},
127 115
 	)
128 116
 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
129 117
 	resp = session.MakeRequest(t, req)
@@ -140,6 +128,6 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
140 128
 
141 129
 func TestEditFile(t *testing.T) {
142 130
 	prepareTestEnv(t)
143
-	session := loginUser(t, "user2", "password")
131
+	session := loginUser(t, "user2")
144 132
 	testEditFile(t, session, "user2", "repo1", "master", "README.md")
145 133
 }

+ 9 - 6
integrations/html_helper.go

@@ -6,21 +6,20 @@ package integrations
6 6
 
7 7
 import (
8 8
 	"bytes"
9
+	"testing"
9 10
 
10 11
 	"github.com/PuerkitoBio/goquery"
12
+	"github.com/stretchr/testify/assert"
11 13
 )
12 14
 
13 15
 type HtmlDoc struct {
14 16
 	doc *goquery.Document
15 17
 }
16 18
 
17
-func NewHtmlParser(content []byte) (*HtmlDoc, error) {
19
+func NewHtmlParser(t *testing.T, content []byte) *HtmlDoc {
18 20
 	doc, err := goquery.NewDocumentFromReader(bytes.NewReader(content))
19
-	if err != nil {
20
-		return nil, err
21
-	}
22
-
23
-	return &HtmlDoc{doc: doc}, nil
21
+	assert.NoError(t, err)
22
+	return &HtmlDoc{doc: doc}
24 23
 }
25 24
 
26 25
 func (doc *HtmlDoc) GetInputValueById(id string) string {
@@ -32,3 +31,7 @@ func (doc *HtmlDoc) GetInputValueByName(name string) string {
32 31
 	text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value")
33 32
 	return text
34 33
 }
34
+
35
+func (doc *HtmlDoc) GetCSRF() string {
36
+	return doc.GetInputValueByName("_csrf")
37
+}

+ 33 - 16
integrations/integration_test.go

@@ -7,6 +7,7 @@ package integrations
7 7
 import (
8 8
 	"bytes"
9 9
 	"database/sql"
10
+	"encoding/json"
10 11
 	"fmt"
11 12
 	"io"
12 13
 	"log"
@@ -155,21 +156,23 @@ func (s *TestSession) MakeRequest(t *testing.T, req *http.Request) *TestResponse
155 156
 	return resp
156 157
 }
157 158
 
158
-func loginUser(t *testing.T, userName, password string) *TestSession {
159
+const userPassword = "password"
160
+
161
+func loginUser(t *testing.T, userName string) *TestSession {
162
+	return loginUserWithPassword(t, userName, userPassword)
163
+}
164
+
165
+func loginUserWithPassword(t *testing.T, userName, password string) *TestSession {
159 166
 	req := NewRequest(t, "GET", "/user/login")
160 167
 	resp := MakeRequest(req)
161 168
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
162 169
 
163
-	doc, err := NewHtmlParser(resp.Body)
164
-	assert.NoError(t, err)
165
-
166
-	req = NewRequestBody(t, "POST", "/user/login",
167
-		bytes.NewBufferString(url.Values{
168
-			"_csrf":     []string{doc.GetInputValueByName("_csrf")},
169
-			"user_name": []string{userName},
170
-			"password":  []string{password},
171
-		}.Encode()),
172
-	)
170
+	doc := NewHtmlParser(t, resp.Body)
171
+	req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{
172
+		"_csrf":     doc.GetCSRF(),
173
+		"user_name": userName,
174
+		"password":  password,
175
+	})
173 176
 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
174 177
 	resp = MakeRequest(req)
175 178
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
@@ -211,14 +214,28 @@ type TestResponse struct {
211 214
 	Headers    http.Header
212 215
 }
213 216
 
214
-func NewRequest(t *testing.T, method, url string) *http.Request {
215
-	return NewRequestBody(t, method, url, nil)
217
+func NewRequest(t *testing.T, method, urlStr string) *http.Request {
218
+	return NewRequestWithBody(t, method, urlStr, nil)
219
+}
220
+
221
+func NewRequestWithValues(t *testing.T, method, urlStr string, values map[string]string) *http.Request {
222
+	urlValues := url.Values{}
223
+	for key, value := range values {
224
+		urlValues[key] = []string{value}
225
+	}
226
+	return NewRequestWithBody(t, method, urlStr, bytes.NewBufferString(urlValues.Encode()))
227
+}
228
+
229
+func NewRequestWithJSON(t *testing.T, method, urlStr string, v interface{}) *http.Request {
230
+	jsonBytes, err := json.Marshal(v)
231
+	assert.NoError(t, err)
232
+	return NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes))
216 233
 }
217 234
 
218
-func NewRequestBody(t *testing.T, method, url string, body io.Reader) *http.Request {
219
-	request, err := http.NewRequest(method, url, body)
235
+func NewRequestWithBody(t *testing.T, method, urlStr string, body io.Reader) *http.Request {
236
+	request, err := http.NewRequest(method, urlStr, body)
220 237
 	assert.NoError(t, err)
221
-	request.RequestURI = url
238
+	request.RequestURI = urlStr
222 239
 	return request
223 240
 }
224 241
 

+ 2 - 3
integrations/issue_test.go

@@ -45,13 +45,12 @@ func TestNoLoginViewIssuesSortByType(t *testing.T) {
45 45
 	repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
46 46
 	repo.Owner = models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
47 47
 
48
-	session := loginUser(t, user.Name, "password")
48
+	session := loginUser(t, user.Name)
49 49
 	req := NewRequest(t, "GET", repo.RelLink()+"/issues?type=created_by")
50 50
 	resp := session.MakeRequest(t, req)
51 51
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
52 52
 
53
-	htmlDoc, err := NewHtmlParser(resp.Body)
54
-	assert.NoError(t, err)
53
+	htmlDoc := NewHtmlParser(t, resp.Body)
55 54
 	issuesSelection := getIssuesSelection(htmlDoc)
56 55
 	expectedNumIssues := models.GetCount(t,
57 56
 		&models.Issue{RepoID: repo.ID, PosterID: user.ID},

+ 2 - 3
integrations/pull_compare_test.go

@@ -14,12 +14,11 @@ import (
14 14
 func TestPullCompare(t *testing.T) {
15 15
 	prepareTestEnv(t)
16 16
 
17
-	session := loginUser(t, "user2", "password")
17
+	session := loginUser(t, "user2")
18 18
 	req := NewRequest(t, "GET", "/user2/repo1/pulls")
19 19
 	resp := session.MakeRequest(t, req)
20 20
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
21
-	htmlDoc, err := NewHtmlParser(resp.Body)
22
-	assert.NoError(t, err)
21
+	htmlDoc := NewHtmlParser(t, resp.Body)
23 22
 	link, exists := htmlDoc.doc.Find(".navbar").Find(".ui.green.button").Attr("href")
24 23
 	assert.True(t, exists, "The template has changed")
25 24
 

+ 7 - 13
integrations/pull_create_test.go

@@ -5,9 +5,7 @@
5 5
 package integrations
6 6
 
7 7
 import (
8
-	"bytes"
9 8
 	"net/http"
10
-	"net/url"
11 9
 	"path"
12 10
 	"testing"
13 11
 
@@ -20,8 +18,7 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin
20 18
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
21 19
 
22 20
 	// Click the little green button to create a pull
23
-	htmlDoc, err := NewHtmlParser(resp.Body)
24
-	assert.NoError(t, err)
21
+	htmlDoc := NewHtmlParser(t, resp.Body)
25 22
 	link, exists := htmlDoc.doc.Find("button.ui.green.small.button").Parent().Attr("href")
26 23
 	assert.True(t, exists, "The template has changed")
27 24
 
@@ -30,16 +27,13 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin
30 27
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
31 28
 
32 29
 	// Submit the form for creating the pull
33
-	htmlDoc, err = NewHtmlParser(resp.Body)
34
-	assert.NoError(t, err)
30
+	htmlDoc = NewHtmlParser(t, resp.Body)
35 31
 	link, exists = htmlDoc.doc.Find("form.ui.form").Attr("action")
36 32
 	assert.True(t, exists, "The template has changed")
37
-	req = NewRequestBody(t, "POST", link,
38
-		bytes.NewBufferString(url.Values{
39
-			"_csrf": []string{htmlDoc.GetInputValueByName("_csrf")},
40
-			"title": []string{"This is a pull title"},
41
-		}.Encode()),
42
-	)
33
+	req = NewRequestWithValues(t, "POST", link, map[string]string{
34
+		"_csrf": htmlDoc.GetCSRF(),
35
+		"title": "This is a pull title",
36
+	})
43 37
 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
44 38
 	resp = session.MakeRequest(t, req)
45 39
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
@@ -51,7 +45,7 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin
51 45
 
52 46
 func TestPullCreate(t *testing.T) {
53 47
 	prepareTestEnv(t)
54
-	session := loginUser(t, "user1", "password")
48
+	session := loginUser(t, "user1")
55 49
 	testRepoFork(t, session)
56 50
 	testEditFile(t, session, "user1", "repo1", "master", "README.md")
57 51
 	testPullCreate(t, session, "user1", "repo1", "master")

+ 5 - 10
integrations/pull_merge_test.go

@@ -5,9 +5,7 @@
5 5
 package integrations
6 6
 
7 7
 import (
8
-	"bytes"
9 8
 	"net/http"
10
-	"net/url"
11 9
 	"path"
12 10
 	"strings"
13 11
 	"testing"
@@ -21,15 +19,12 @@ func testPullMerge(t *testing.T, session *TestSession, user, repo, pullnum strin
21 19
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
22 20
 
23 21
 	// Click the little green button to craete a pull
24
-	htmlDoc, err := NewHtmlParser(resp.Body)
25
-	assert.NoError(t, err)
22
+	htmlDoc := NewHtmlParser(t, resp.Body)
26 23
 	link, exists := htmlDoc.doc.Find("form.ui.form>button.ui.green.button").Parent().Attr("action")
27 24
 	assert.True(t, exists, "The template has changed")
28
-	req = NewRequestBody(t, "POST", link,
29
-		bytes.NewBufferString(url.Values{
30
-			"_csrf": []string{htmlDoc.GetInputValueByName("_csrf")},
31
-		}.Encode()),
32
-	)
25
+	req = NewRequestWithValues(t, "POST", link, map[string]string{
26
+		"_csrf": htmlDoc.GetCSRF(),
27
+	})
33 28
 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
34 29
 	resp = session.MakeRequest(t, req)
35 30
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
@@ -39,7 +34,7 @@ func testPullMerge(t *testing.T, session *TestSession, user, repo, pullnum strin
39 34
 
40 35
 func TestPullMerge(t *testing.T) {
41 36
 	prepareTestEnv(t)
42
-	session := loginUser(t, "user1", "password")
37
+	session := loginUser(t, "user1")
43 38
 	testRepoFork(t, session)
44 39
 	testEditFile(t, session, "user1", "repo1", "master", "README.md")
45 40
 

+ 1 - 1
integrations/release_test.go

@@ -14,7 +14,7 @@ import (
14 14
 func TestViewReleases(t *testing.T) {
15 15
 	prepareTestEnv(t)
16 16
 
17
-	session := loginUser(t, "user2", "password")
17
+	session := loginUser(t, "user2")
18 18
 	req := NewRequest(t, "GET", "/user2/repo1/releases")
19 19
 	resp := session.MakeRequest(t, req)
20 20
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

+ 15 - 11
integrations/repo_commits_test.go

@@ -5,26 +5,26 @@
5 5
 package integrations
6 6
 
7 7
 import (
8
-	"bytes"
9 8
 	"net/http"
10 9
 	"path"
11 10
 	"testing"
12 11
 
12
+	api "code.gitea.io/sdk/gitea"
13
+
13 14
 	"github.com/stretchr/testify/assert"
14 15
 )
15 16
 
16 17
 func TestRepoCommits(t *testing.T) {
17 18
 	prepareTestEnv(t)
18 19
 
19
-	session := loginUser(t, "user2", "password")
20
+	session := loginUser(t, "user2")
20 21
 
21 22
 	// Request repository commits page
22 23
 	req := NewRequest(t, "GET", "/user2/repo1/commits/master")
23 24
 	resp := session.MakeRequest(t, req)
24 25
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
25 26
 
26
-	doc, err := NewHtmlParser(resp.Body)
27
-	assert.NoError(t, err)
27
+	doc := NewHtmlParser(t, resp.Body)
28 28
 	commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href")
29 29
 	assert.True(t, exists)
30 30
 	assert.NotEmpty(t, commitURL)
@@ -33,23 +33,28 @@ func TestRepoCommits(t *testing.T) {
33 33
 func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
34 34
 	prepareTestEnv(t)
35 35
 
36
-	session := loginUser(t, "user2", "password")
36
+	session := loginUser(t, "user2")
37 37
 
38 38
 	// Request repository commits page
39 39
 	req := NewRequest(t, "GET", "/user2/repo1/commits/master")
40 40
 	resp := session.MakeRequest(t, req)
41 41
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
42 42
 
43
-	doc, err := NewHtmlParser(resp.Body)
44
-	assert.NoError(t, err)
43
+	doc := NewHtmlParser(t, resp.Body)
45 44
 	// Get first commit URL
46 45
 	commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href")
47 46
 	assert.True(t, exists)
48 47
 	assert.NotEmpty(t, commitURL)
49 48
 
50 49
 	// Call API to add status for commit
51
-	req = NewRequestBody(t, "POST", "/api/v1/repos/user2/repo1/statuses/"+path.Base(commitURL),
52
-		bytes.NewBufferString("{\"state\":\""+state+"\", \"target_url\": \"http://test.ci/\", \"description\": \"\", \"context\": \"testci\"}"))
50
+	req = NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/statuses/"+path.Base(commitURL),
51
+		api.CreateStatusOption{
52
+			State:       api.StatusState(state),
53
+			TargetURL:   "http://test.ci/",
54
+			Description: "",
55
+			Context:     "testci",
56
+		},
57
+	)
53 58
 
54 59
 	req.Header.Add("Content-Type", "application/json")
55 60
 	resp = session.MakeRequest(t, req)
@@ -59,8 +64,7 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
59 64
 	resp = session.MakeRequest(t, req)
60 65
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
61 66
 
62
-	doc, err = NewHtmlParser(resp.Body)
63
-	assert.NoError(t, err)
67
+	doc = NewHtmlParser(t, resp.Body)
64 68
 	// Check if commit status is displayed in message column
65 69
 	sel := doc.doc.Find("#commits-table tbody tr td.message i.commit-status")
66 70
 	assert.Equal(t, sel.Length(), 1)

+ 8 - 14
integrations/repo_fork_test.go

@@ -5,9 +5,7 @@
5 5
 package integrations
6 6
 
7 7
 import (
8
-	"bytes"
9 8
 	"net/http"
10
-	"net/url"
11 9
 	"testing"
12 10
 
13 11
 	"github.com/stretchr/testify/assert"
@@ -25,8 +23,7 @@ func testRepoFork(t *testing.T, session *TestSession) *TestResponse {
25 23
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
26 24
 
27 25
 	// Step2: click the fork button
28
-	htmlDoc, err := NewHtmlParser(resp.Body)
29
-	assert.NoError(t, err)
26
+	htmlDoc := NewHtmlParser(t, resp.Body)
30 27
 	link, exists := htmlDoc.doc.Find("a.ui.button[href^=\"/repo/fork/\"]").Attr("href")
31 28
 	assert.True(t, exists, "The template has changed")
32 29
 	req = NewRequest(t, "GET", link)
@@ -34,17 +31,14 @@ func testRepoFork(t *testing.T, session *TestSession) *TestResponse {
34 31
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
35 32
 
36 33
 	// Step3: fill the form of the forking
37
-	htmlDoc, err = NewHtmlParser(resp.Body)
38
-	assert.NoError(t, err)
34
+	htmlDoc = NewHtmlParser(t, resp.Body)
39 35
 	link, exists = htmlDoc.doc.Find("form.ui.form[action^=\"/repo/fork/\"]").Attr("action")
40 36
 	assert.True(t, exists, "The template has changed")
41
-	req = NewRequestBody(t, "POST", link,
42
-		bytes.NewBufferString(url.Values{
43
-			"_csrf":     []string{htmlDoc.GetInputValueByName("_csrf")},
44
-			"uid":       []string{"1"},
45
-			"repo_name": []string{"repo1"},
46
-		}.Encode()),
47
-	)
37
+	req = NewRequestWithValues(t, "POST", link, map[string]string{
38
+		"_csrf":     htmlDoc.GetCSRF(),
39
+		"uid":       "1",
40
+		"repo_name": "repo1",
41
+	})
48 42
 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
49 43
 	resp = session.MakeRequest(t, req)
50 44
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
@@ -59,6 +53,6 @@ func testRepoFork(t *testing.T, session *TestSession) *TestResponse {
59 53
 
60 54
 func TestRepoFork(t *testing.T) {
61 55
 	prepareTestEnv(t)
62
-	session := loginUser(t, "user1", "password")
56
+	session := loginUser(t, "user1")
63 57
 	testRepoFork(t, session)
64 58
 }

+ 3 - 3
integrations/repo_test.go

@@ -22,7 +22,7 @@ func TestViewRepo(t *testing.T) {
22 22
 	resp = MakeRequest(req)
23 23
 	assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode)
24 24
 
25
-	session := loginUser(t, "user1", "password")
25
+	session := loginUser(t, "user1")
26 26
 	resp = session.MakeRequest(t, req)
27 27
 	assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode)
28 28
 }
@@ -31,7 +31,7 @@ func TestViewRepo2(t *testing.T) {
31 31
 	prepareTestEnv(t)
32 32
 
33 33
 	req := NewRequest(t, "GET", "/user3/repo3")
34
-	session := loginUser(t, "user2", "password")
34
+	session := loginUser(t, "user2")
35 35
 	resp := session.MakeRequest(t, req)
36 36
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
37 37
 }
@@ -40,7 +40,7 @@ func TestViewRepo3(t *testing.T) {
40 40
 	prepareTestEnv(t)
41 41
 
42 42
 	req := NewRequest(t, "GET", "/user3/repo3")
43
-	session := loginUser(t, "user3", "password")
43
+	session := loginUser(t, "user3")
44 44
 	resp := session.MakeRequest(t, req)
45 45
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
46 46
 }

+ 6 - 10
integrations/signup_test.go

@@ -5,9 +5,7 @@
5 5
 package integrations
6 6
 
7 7
 import (
8
-	"bytes"
9 8
 	"net/http"
10
-	"net/url"
11 9
 	"testing"
12 10
 
13 11
 	"code.gitea.io/gitea/modules/setting"
@@ -20,14 +18,12 @@ func TestSignup(t *testing.T) {
20 18
 
21 19
 	setting.Service.EnableCaptcha = false
22 20
 
23
-	req := NewRequestBody(t, "POST", "/user/sign_up",
24
-		bytes.NewBufferString(url.Values{
25
-			"user_name": []string{"exampleUser"},
26
-			"email":     []string{"exampleUser@example.com"},
27
-			"password":  []string{"examplePassword"},
28
-			"retype":    []string{"examplePassword"},
29
-		}.Encode()),
30
-	)
21
+	req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
22
+		"user_name": "exampleUser",
23
+		"email":     "exampleUser@example.com",
24
+		"password":  "examplePassword",
25
+		"retype":    "examplePassword",
26
+	})
31 27
 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
32 28
 	resp := MakeRequest(req)
33 29
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

+ 2 - 0
models/fixtures/user.yml

@@ -1,3 +1,5 @@
1
+# NOTE: all users should have a password of "password"
2
+
1 3
 - # NOTE: this user (id=1) is the admin
2 4
   id: 1
3 5
   lower_name: user1