Browse Source

Add integration test for pull-request merge (#1912)

Mura Li 2 years ago
parent
commit
01322af2e8

+ 3 - 1
integrations/editor_test.go

@@ -101,7 +101,7 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
101 101
 	assert.Contains(t, string(resp.Body), "Can not commit to protected branch 'master'.")
102 102
 }
103 103
 
104
-func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath string) {
104
+func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath string) *TestResponse {
105 105
 
106 106
 	newContent := "Hello, World (Edited)\n"
107 107
 
@@ -134,6 +134,8 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
134 134
 	resp = session.MakeRequest(t, req)
135 135
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
136 136
 	assert.EqualValues(t, newContent, string(resp.Body))
137
+
138
+	return resp
137 139
 }
138 140
 
139 141
 func TestEditFile(t *testing.T) {

+ 3 - 1
integrations/pull_create_test.go

@@ -14,7 +14,7 @@ import (
14 14
 	"github.com/stretchr/testify/assert"
15 15
 )
16 16
 
17
-func testPullCreate(t *testing.T, session *TestSession, user, repo, branch string) {
17
+func testPullCreate(t *testing.T, session *TestSession, user, repo, branch string) *TestResponse {
18 18
 	req := NewRequest(t, "GET", path.Join(user, repo))
19 19
 	resp := session.MakeRequest(t, req)
20 20
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
@@ -45,6 +45,8 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin
45 45
 	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
46 46
 
47 47
 	//TODO check the redirected URL
48
+
49
+	return resp
48 50
 }
49 51
 
50 52
 func TestPullCreate(t *testing.T) {

+ 53 - 0
integrations/pull_merge_test.go

@@ -0,0 +1,53 @@
1
+// Copyright 2017 The Gitea Authors. All rights reserved.
2
+// Use of this source code is governed by a MIT-style
3
+// license that can be found in the LICENSE file.
4
+
5
+package integrations
6
+
7
+import (
8
+	"bytes"
9
+	"net/http"
10
+	"net/url"
11
+	"path"
12
+	"strings"
13
+	"testing"
14
+
15
+	"github.com/stretchr/testify/assert"
16
+)
17
+
18
+func testPullMerge(t *testing.T, session *TestSession, user, repo, pullnum string) *TestResponse {
19
+	req := NewRequest(t, "GET", path.Join(user, repo, "pulls", pullnum))
20
+	resp := session.MakeRequest(t, req)
21
+	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
22
+
23
+	// Click the little green button to craete a pull
24
+	htmlDoc, err := NewHtmlParser(resp.Body)
25
+	assert.NoError(t, err)
26
+	link, exists := htmlDoc.doc.Find("form.ui.form>button.ui.green.button").Parent().Attr("action")
27
+	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
+	)
33
+	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
34
+	resp = session.MakeRequest(t, req)
35
+	assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
36
+
37
+	return resp
38
+}
39
+
40
+func TestPullMerge(t *testing.T) {
41
+	prepareTestEnv(t)
42
+	session := loginUser(t, "user1", "password")
43
+	testRepoFork(t, session)
44
+	testEditFile(t, session, "user1", "repo1", "master", "README.md")
45
+
46
+	resp := testPullCreate(t, session, "user1", "repo1", "master")
47
+	redirectedURL := resp.Headers["Location"]
48
+	assert.NotEmpty(t, redirectedURL, "Redirected URL is not found")
49
+
50
+	elem := strings.Split(redirectedURL[0], "/")
51
+	assert.EqualValues(t, "pulls", elem[3])
52
+	testPullMerge(t, session, elem[1], elem[2], elem[4])
53
+}

+ 3 - 1
integrations/repo_fork_test.go

@@ -13,7 +13,7 @@ import (
13 13
 	"github.com/stretchr/testify/assert"
14 14
 )
15 15
 
16
-func testRepoFork(t *testing.T, session *TestSession) {
16
+func testRepoFork(t *testing.T, session *TestSession) *TestResponse {
17 17
 	// Step0: check the existence of the to-fork repo
18 18
 	req := NewRequest(t, "GET", "/user1/repo1")
19 19
 	resp := session.MakeRequest(t, req)
@@ -53,6 +53,8 @@ func testRepoFork(t *testing.T, session *TestSession) {
53 53
 	req = NewRequest(t, "GET", "/user1/repo1")
54 54
 	resp = session.MakeRequest(t, req)
55 55
 	assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
56
+
57
+	return resp
56 58
 }
57 59
 
58 60
 func TestRepoFork(t *testing.T) {