Skip to content

Commit b0456cf

Browse files
committed
Add test
1 parent 9ce1cae commit b0456cf

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

tests/integration/pull_merge_test.go

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
repo_service "code.gitea.io/gitea/services/repository"
4141
commitstatus_service "code.gitea.io/gitea/services/repository/commitstatus"
4242
files_service "code.gitea.io/gitea/services/repository/files"
43+
"github.com/stretchr/testify/require"
4344

4445
"github.com/stretchr/testify/assert"
4546
)
@@ -168,10 +169,104 @@ func TestPullSquash(t *testing.T) {
168169

169170
resp := testPullCreate(t, session, "user1", "repo1", false, "master", "master", "This is a pull title")
170171

171-
elem := strings.Split(test.RedirectURL(resp), "/")
172+
prURL := test.RedirectURL(resp)
173+
elem := strings.Split(prURL, "/")
172174
assert.Equal(t, "pulls", elem[3])
175+
176+
resp = session.MakeRequest(t, NewRequest(t, "GET", prURL), http.StatusOK)
177+
htmlDoc := NewHTMLParser(t, resp.Body)
178+
dataURL, exists := htmlDoc.doc.Find("#allow-edits-from-maintainers").Attr("data-url")
179+
assert.True(t, exists)
180+
req := NewRequestWithValues(t, "POST", dataURL+"/set_allow_maintainer_edit", map[string]string{
181+
"_csrf": htmlDoc.GetCSRF(),
182+
"allow_maintainer_edit": "true",
183+
})
184+
session.MakeRequest(t, req, http.StatusOK)
185+
186+
user2Session := loginUser(t, "user2")
187+
resp = user2Session.MakeRequest(t, NewRequest(t, "GET", prURL+"/files"), http.StatusOK)
188+
htmlDoc = NewHTMLParser(t, resp.Body)
189+
nodes := htmlDoc.doc.Find(".diff-file-box[data-new-filename=\"README.md\"] .diff-file-header-actions .tippy-target a")
190+
if assert.Equal(t, 2, nodes.Length()) {
191+
assert.Equal(t, "Edit File", nodes.Last().Text())
192+
editFileLink, exists := nodes.Last().Attr("href")
193+
if assert.True(t, exists) {
194+
// edit the file
195+
resp := user2Session.MakeRequest(t, NewRequest(t, "GET", editFileLink), http.StatusOK)
196+
htmlDoc := NewHTMLParser(t, resp.Body)
197+
lastCommit := htmlDoc.GetInputValueByName("last_commit")
198+
assert.NotEmpty(t, lastCommit)
199+
req := NewRequestWithValues(t, "POST", editFileLink, map[string]string{
200+
"_csrf": htmlDoc.GetCSRF(),
201+
"last_commit": lastCommit,
202+
"tree_path": "README.md",
203+
"content": "Hello, World (Edite!!)",
204+
"commit_summary": "user2 updated the file",
205+
"commit_choice": "direct",
206+
})
207+
resp = user2Session.MakeRequest(t, req, http.StatusOK)
208+
assert.NotEmpty(t, test.RedirectURL(resp))
209+
}
210+
}
211+
resp = user2Session.MakeRequest(t, NewRequest(t, "GET", prURL+"/files"), http.StatusOK)
212+
htmlDoc = NewHTMLParser(t, resp.Body)
213+
nodes = htmlDoc.doc.Find(".diff-file-box[data-new-filename=\"README.md\"] .diff-file-header-actions .tippy-target a")
214+
if assert.Equal(t, 2, nodes.Length()) {
215+
assert.Equal(t, "Edit File", nodes.Last().Text())
216+
editFileLink, exists := nodes.Last().Attr("href")
217+
if assert.True(t, exists) {
218+
// edit the file
219+
resp := user2Session.MakeRequest(t, NewRequest(t, "GET", editFileLink), http.StatusOK)
220+
htmlDoc := NewHTMLParser(t, resp.Body)
221+
lastCommit := htmlDoc.GetInputValueByName("last_commit")
222+
assert.NotEmpty(t, lastCommit)
223+
req := NewRequestWithValues(t, "POST", editFileLink, map[string]string{
224+
"_csrf": htmlDoc.GetCSRF(),
225+
"last_commit": lastCommit,
226+
"tree_path": "README.md",
227+
"content": "Hello, World (Edite!!!)",
228+
"commit_summary": "user2 updated the file!",
229+
"commit_choice": "direct",
230+
})
231+
resp = user2Session.MakeRequest(t, req, http.StatusOK)
232+
assert.NotEmpty(t, test.RedirectURL(resp))
233+
}
234+
}
235+
173236
testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleSquash, false)
174237

238+
req = NewRequest(t, "GET", "/user2/repo1/src/branch/master/")
239+
resp = user2Session.MakeRequest(t, req, http.StatusOK)
240+
htmlDoc = NewHTMLParser(t, resp.Body)
241+
commitHref := htmlDoc.doc.Find(".latest-commit .commit-id-short").AttrOr("href", "")
242+
assert.NotEmpty(t, commitHref)
243+
244+
req = NewRequest(t, "GET", commitHref)
245+
resp = user2Session.MakeRequest(t, req, http.StatusOK)
246+
htmlDoc = NewHTMLParser(t, resp.Body)
247+
prTitle, exists := htmlDoc.doc.Find(".commit-summary").Attr("title")
248+
assert.True(t, exists)
249+
assert.Contains(t, prTitle, "This is a pull title")
250+
251+
commitBody := htmlDoc.doc.Find(".commit-body").Text()
252+
assert.NotEmpty(t, commitBody)
253+
assert.Contains(t, commitBody, "--------------------")
254+
255+
req = NewRequest(t, "GET", fmt.Sprintf("/user2/repo1/pulls/%s/commits/list", elem[4]))
256+
resp = user2Session.MakeRequest(t, req, http.StatusOK)
257+
258+
var pullCommitList struct {
259+
Commits []pull_service.CommitInfo `json:"commits"`
260+
LastReviewCommitSha string `json:"last_review_commit_sha"`
261+
}
262+
DecodeJSON(t, resp, &pullCommitList)
263+
264+
require.Len(t, pullCommitList.Commits, 4)
265+
266+
for _, commit := range pullCommitList.Commits {
267+
assert.Contains(t, commitBody, fmt.Sprintf("* %s", commit.Summary))
268+
}
269+
175270
hookTasks, err = webhook.HookTasks(db.DefaultContext, 1, 1)
176271
assert.NoError(t, err)
177272
assert.Len(t, hookTasks, hookTasksLenBefore+1)

0 commit comments

Comments
 (0)