Browse Source

Improve integration test helper functions (#2049)

Set request headers in helper functions, and new helper for requests with string-formatted URLs
Ethan Koenig 2 years ago
parent
commit
f64c232953

+ 1 - 3
integrations/api_comment_test.go

@@ -5,7 +5,6 @@
5 5
 package integrations
6 6
 
7 7
 import (
8
-	"fmt"
9 8
 	"net/http"
10 9
 	"testing"
11 10
 
@@ -25,9 +24,8 @@ func TestAPIListComments(t *testing.T) {
25 24
 	repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
26 25
 
27 26
 	session := loginUser(t, repoOwner.Name)
28
-	requestUrl := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments",
27
+	req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/comments",
29 28
 		repoOwner.Name, repo.Name, issue.Index)
30
-	req := NewRequest(t, "GET", requestUrl)
31 29
 	resp := session.MakeRequest(t, req)
32 30
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
33 31
 

+ 1 - 3
integrations/api_team_test.go

@@ -5,7 +5,6 @@
5 5
 package integrations
6 6
 
7 7
 import (
8
-	"fmt"
9 8
 	"net/http"
10 9
 	"testing"
11 10
 
@@ -22,8 +21,7 @@ func TestAPITeam(t *testing.T) {
22 21
 	user := models.AssertExistsAndLoadBean(t, &models.User{ID: teamUser.UID}).(*models.User)
23 22
 
24 23
 	session := loginUser(t, user.Name)
25
-	url := fmt.Sprintf("/api/v1/teams/%d", teamUser.TeamID)
26
-	req := NewRequest(t, "GET", url)
24
+	req := NewRequestf(t, "GET", "/api/v1/teams/%d", teamUser.TeamID)
27 25
 	resp := session.MakeRequest(t, req)
28 26
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
29 27
 

+ 0 - 2
integrations/change_default_branch_test.go

@@ -32,7 +32,6 @@ func TestChangeDefaultBranch(t *testing.T) {
32 32
 		"action": "default_branch",
33 33
 		"branch": "DefaultBranch",
34 34
 	})
35
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
36 35
 	resp = session.MakeRequest(t, req)
37 36
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
38 37
 
@@ -46,7 +45,6 @@ func TestChangeDefaultBranch(t *testing.T) {
46 45
 		"action": "default_branch",
47 46
 		"branch": "does_not_exist",
48 47
 	})
49
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
50 48
 	resp = session.MakeRequest(t, req)
51 49
 	assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode)
52 50
 }

+ 0 - 1
integrations/delete_user_test.go

@@ -26,7 +26,6 @@ func TestDeleteUser(t *testing.T) {
26 26
 	req = NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{
27 27
 		"_csrf": doc.GetCSRF(),
28 28
 	})
29
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
30 29
 	resp = session.MakeRequest(t, req)
31 30
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
32 31
 

+ 0 - 5
integrations/editor_test.go

@@ -34,7 +34,6 @@ func TestCreateFile(t *testing.T) {
34 34
 		"content":       "Content",
35 35
 		"commit_choice": "direct",
36 36
 	})
37
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
38 37
 	resp = session.MakeRequest(t, req)
39 38
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
40 39
 }
@@ -57,7 +56,6 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
57 56
 		"branchName": "master",
58 57
 		"canPush":    "true",
59 58
 	})
60
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
61 59
 	resp = session.MakeRequest(t, req)
62 60
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
63 61
 	// Check if master branch has been locked successfully
@@ -83,7 +81,6 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
83 81
 		"commit_choice": "direct",
84 82
 	})
85 83
 
86
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
87 84
 	resp = session.MakeRequest(t, req)
88 85
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
89 86
 	// Check body for error message
@@ -113,7 +110,6 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
113 110
 			"commit_choice": "direct",
114 111
 		},
115 112
 	)
116
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
117 113
 	resp = session.MakeRequest(t, req)
118 114
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
119 115
 
@@ -150,7 +146,6 @@ func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, bra
150 146
 			"new_branch_name": targetBranch,
151 147
 		},
152 148
 	)
153
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
154 149
 	resp = session.MakeRequest(t, req)
155 150
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
156 151
 

+ 7 - 2
integrations/integration_test.go

@@ -173,7 +173,6 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
173 173
 		"user_name": userName,
174 174
 		"password":  password,
175 175
 	})
176
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
177 176
 	resp = MakeRequest(req)
178 177
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
179 178
 
@@ -218,12 +217,18 @@ func NewRequest(t testing.TB, method, urlStr string) *http.Request {
218 217
 	return NewRequestWithBody(t, method, urlStr, nil)
219 218
 }
220 219
 
220
+func NewRequestf(t testing.TB, method, urlFormat string, args ...interface{}) *http.Request {
221
+	return NewRequest(t, method, fmt.Sprintf(urlFormat, args...))
222
+}
223
+
221 224
 func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string]string) *http.Request {
222 225
 	urlValues := url.Values{}
223 226
 	for key, value := range values {
224 227
 		urlValues[key] = []string{value}
225 228
 	}
226
-	return NewRequestWithBody(t, method, urlStr, bytes.NewBufferString(urlValues.Encode()))
229
+	req := NewRequestWithBody(t, method, urlStr, bytes.NewBufferString(urlValues.Encode()))
230
+	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
231
+	return req
227 232
 }
228 233
 
229 234
 func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *http.Request {

+ 0 - 1
integrations/issue_test.go

@@ -90,7 +90,6 @@ func testNewIssue(t *testing.T, session *TestSession, user, repo, title string)
90 90
 		"_csrf": htmlDoc.GetCSRF(),
91 91
 		"title": title,
92 92
 	})
93
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
94 93
 	resp = session.MakeRequest(t, req)
95 94
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
96 95
 	redirectedURL := resp.Headers["Location"]

+ 0 - 1
integrations/pull_create_test.go

@@ -38,7 +38,6 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin
38 38
 		"_csrf": htmlDoc.GetCSRF(),
39 39
 		"title": "This is a pull title",
40 40
 	})
41
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
42 41
 	resp = session.MakeRequest(t, req)
43 42
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
44 43
 

+ 0 - 2
integrations/pull_merge_test.go

@@ -25,7 +25,6 @@ func testPullMerge(t *testing.T, session *TestSession, user, repo, pullnum strin
25 25
 	req = NewRequestWithValues(t, "POST", link, map[string]string{
26 26
 		"_csrf": htmlDoc.GetCSRF(),
27 27
 	})
28
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
29 28
 	resp = session.MakeRequest(t, req)
30 29
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
31 30
 
@@ -44,7 +43,6 @@ func testPullCleanUp(t *testing.T, session *TestSession, user, repo, pullnum str
44 43
 	req = NewRequestWithValues(t, "POST", link, map[string]string{
45 44
 		"_csrf": htmlDoc.GetCSRF(),
46 45
 	})
47
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
48 46
 	resp = session.MakeRequest(t, req)
49 47
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
50 48
 

+ 0 - 1
integrations/repo_commits_test.go

@@ -56,7 +56,6 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
56 56
 		},
57 57
 	)
58 58
 
59
-	req.Header.Add("Content-Type", "application/json")
60 59
 	resp = session.MakeRequest(t, req)
61 60
 	assert.EqualValues(t, http.StatusCreated, resp.HeaderCode)
62 61
 

+ 0 - 1
integrations/repo_fork_test.go

@@ -39,7 +39,6 @@ func testRepoFork(t *testing.T, session *TestSession) *TestResponse {
39 39
 		"uid":       "1",
40 40
 		"repo_name": "repo1",
41 41
 	})
42
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
43 42
 	resp = session.MakeRequest(t, req)
44 43
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
45 44
 

+ 0 - 1
integrations/repo_migrate_test.go

@@ -30,7 +30,6 @@ func testRepoMigrate(t testing.TB, session *TestSession, cloneAddr, repoName str
30 30
 		"repo_name":  repoName,
31 31
 	},
32 32
 	)
33
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
34 33
 	resp = session.MakeRequest(t, req)
35 34
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
36 35
 

+ 0 - 1
integrations/signup_test.go

@@ -24,7 +24,6 @@ func TestSignup(t *testing.T) {
24 24
 		"password":  "examplePassword",
25 25
 		"retype":    "examplePassword",
26 26
 	})
27
-	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
28 27
 	resp := MakeRequest(req)
29 28
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
30 29