Skip to content

Commit a84e002

Browse files
committed
Consolidate boilerplate in integration tests
1 parent a3868ef commit a84e002

16 files changed

+149
-168
lines changed

integrations/api_team_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestAPITeam(t *testing.T) {
2323
team := models.AssertExistsAndLoadBean(t, &models.Team{ID: teamUser.TeamID}).(*models.Team)
2424
user := models.AssertExistsAndLoadBean(t, &models.User{ID: teamUser.UID}).(*models.User)
2525

26-
session := loginUser(t, user.Name, "password")
26+
session := loginUser(t, user.Name)
2727
url := fmt.Sprintf("/api/v1/teams/%d", teamUser.TeamID)
2828
req := NewRequest(t, "GET", url)
2929
resp := session.MakeRequest(t, req)

integrations/change_default_branch_test.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
package integrations
66

77
import (
8-
"bytes"
98
"fmt"
109
"net/http"
11-
"net/url"
1210
"testing"
1311

1412
"code.gitea.io/gitea/models"
@@ -21,37 +19,33 @@ func TestChangeDefaultBranch(t *testing.T) {
2119
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
2220
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
2321

24-
session := loginUser(t, owner.Name, "password")
22+
session := loginUser(t, owner.Name)
2523
branchesURL := fmt.Sprintf("/%s/%s/settings/branches", owner.Name, repo.Name)
2624

2725
req := NewRequest(t, "GET", branchesURL)
2826
resp := session.MakeRequest(t, req)
2927
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+
})
3935
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
4036
resp = session.MakeRequest(t, req)
4137
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
4238

4339
req = NewRequest(t, "GET", branchesURL)
4440
resp = session.MakeRequest(t, req)
4541
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+
})
5549
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
5650
resp = session.MakeRequest(t, req)
5751
assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode)

integrations/delete_user_test.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
package integrations
66

77
import (
8-
"bytes"
98
"net/http"
10-
"net/url"
119
"testing"
1210

1311
"code.gitea.io/gitea/models"
@@ -18,18 +16,16 @@ import (
1816
func TestDeleteUser(t *testing.T) {
1917
prepareTestEnv(t)
2018

21-
session := loginUser(t, "user1", "password")
19+
session := loginUser(t, "user1")
2220

2321
req := NewRequest(t, "GET", "/admin/users/8")
2422
resp := session.MakeRequest(t, req)
2523
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
2624

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+
})
3329
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
3430
resp = session.MakeRequest(t, req)
3531
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

integrations/editor_test.go

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
package integrations
66

77
import (
8-
"bytes"
98
"net/http"
10-
"net/url"
119
"path"
1210
"testing"
1311

@@ -17,28 +15,25 @@ import (
1715
func TestCreateFile(t *testing.T) {
1816
prepareTestEnv(t)
1917

20-
session := loginUser(t, "user2", "password")
18+
session := loginUser(t, "user2")
2119

2220
// Request editor page
2321
req := NewRequest(t, "GET", "/user2/repo1/_new/master/")
2422
resp := session.MakeRequest(t, req)
2523
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
2624

27-
doc, err := NewHtmlParser(resp.Body)
28-
assert.NoError(t, err)
25+
doc := NewHtmlParser(t, resp.Body)
2926
lastCommit := doc.GetInputValueByName("last_commit")
3027
assert.NotEmpty(t, lastCommit)
3128

3229
// 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+
})
4237
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
4338
resp = session.MakeRequest(t, req)
4439
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
@@ -47,25 +42,21 @@ func TestCreateFile(t *testing.T) {
4742
func TestCreateFileOnProtectedBranch(t *testing.T) {
4843
prepareTestEnv(t)
4944

50-
session := loginUser(t, "user2", "password")
45+
session := loginUser(t, "user2")
5146

5247
// Open repository branch settings
5348
req := NewRequest(t, "GET", "/user2/repo1/settings/branches")
5449
resp := session.MakeRequest(t, req)
5550
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
5651

57-
doc, err := NewHtmlParser(resp.Body)
58-
assert.NoError(t, err)
52+
doc := NewHtmlParser(t, resp.Body)
5953

6054
// 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+
})
6960
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
7061
resp = session.MakeRequest(t, req)
7162
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
@@ -79,21 +70,19 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
7970
resp = session.MakeRequest(t, req)
8071
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
8172

82-
doc, err = NewHtmlParser(resp.Body)
83-
assert.NoError(t, err)
73+
doc = NewHtmlParser(t, resp.Body)
8474
lastCommit := doc.GetInputValueByName("last_commit")
8575
assert.NotEmpty(t, lastCommit)
8676

8777
// 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+
9786
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
9887
resp = session.MakeRequest(t, req)
9988
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
@@ -110,20 +99,19 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
11099
resp := session.MakeRequest(t, req)
111100
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
112101

113-
htmlDoc, err := NewHtmlParser(resp.Body)
114-
assert.NoError(t, err)
102+
htmlDoc := NewHtmlParser(t, resp.Body)
115103
lastCommit := htmlDoc.GetInputValueByName("last_commit")
116104
assert.NotEmpty(t, lastCommit)
117105

118106
// 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+
},
127115
)
128116
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
129117
resp = session.MakeRequest(t, req)
@@ -140,6 +128,6 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
140128

141129
func TestEditFile(t *testing.T) {
142130
prepareTestEnv(t)
143-
session := loginUser(t, "user2", "password")
131+
session := loginUser(t, "user2")
144132
testEditFile(t, session, "user2", "repo1", "master", "README.md")
145133
}

integrations/html_helper.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@ package integrations
66

77
import (
88
"bytes"
9+
"testing"
910

1011
"github.com/PuerkitoBio/goquery"
12+
"github.com/stretchr/testify/assert"
1113
)
1214

1315
type HtmlDoc struct {
1416
doc *goquery.Document
1517
}
1618

17-
func NewHtmlParser(content []byte) (*HtmlDoc, error) {
19+
func NewHtmlParser(t *testing.T, content []byte) *HtmlDoc {
1820
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}
2423
}
2524

2625
func (doc *HtmlDoc) GetInputValueById(id string) string {
@@ -32,3 +31,7 @@ func (doc *HtmlDoc) GetInputValueByName(name string) string {
3231
text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value")
3332
return text
3433
}
34+
35+
func (doc *HtmlDoc) GetCSRF() string {
36+
return doc.GetInputValueByName("_csrf")
37+
}

integrations/integration_test.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package integrations
77
import (
88
"bytes"
99
"database/sql"
10+
"encoding/json"
1011
"fmt"
1112
"io"
1213
"log"
@@ -155,21 +156,23 @@ func (s *TestSession) MakeRequest(t *testing.T, req *http.Request) *TestResponse
155156
return resp
156157
}
157158

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 {
159166
req := NewRequest(t, "GET", "/user/login")
160167
resp := MakeRequest(req)
161168
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
162169

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+
})
173176
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
174177
resp = MakeRequest(req)
175178
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
@@ -211,14 +214,28 @@ type TestResponse struct {
211214
Headers http.Header
212215
}
213216

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))
216233
}
217234

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)
220237
assert.NoError(t, err)
221-
request.RequestURI = url
238+
request.RequestURI = urlStr
222239
return request
223240
}
224241

integrations/issue_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ func TestNoLoginViewIssuesSortByType(t *testing.T) {
4545
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
4646
repo.Owner = models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
4747

48-
session := loginUser(t, user.Name, "password")
48+
session := loginUser(t, user.Name)
4949
req := NewRequest(t, "GET", repo.RelLink()+"/issues?type=created_by")
5050
resp := session.MakeRequest(t, req)
5151
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
5252

53-
htmlDoc, err := NewHtmlParser(resp.Body)
54-
assert.NoError(t, err)
53+
htmlDoc := NewHtmlParser(t, resp.Body)
5554
issuesSelection := getIssuesSelection(htmlDoc)
5655
expectedNumIssues := models.GetCount(t,
5756
&models.Issue{RepoID: repo.ID, PosterID: user.ID},

integrations/pull_compare_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ import (
1414
func TestPullCompare(t *testing.T) {
1515
prepareTestEnv(t)
1616

17-
session := loginUser(t, "user2", "password")
17+
session := loginUser(t, "user2")
1818
req := NewRequest(t, "GET", "/user2/repo1/pulls")
1919
resp := session.MakeRequest(t, req)
2020
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
21-
htmlDoc, err := NewHtmlParser(resp.Body)
22-
assert.NoError(t, err)
21+
htmlDoc := NewHtmlParser(t, resp.Body)
2322
link, exists := htmlDoc.doc.Find(".navbar").Find(".ui.green.button").Attr("href")
2423
assert.True(t, exists, "The template has changed")
2524

0 commit comments

Comments
 (0)