Skip to content

Commit b02fffe

Browse files
committed
add TestPullCompare_EnableAllowEditsFromMaintainer
1 parent 1b96df2 commit b02fffe

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

tests/integration/pull_compare_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
repo_model "code.gitea.io/gitea/models/repo"
1515
"code.gitea.io/gitea/models/unittest"
1616
user_model "code.gitea.io/gitea/models/user"
17+
"code.gitea.io/gitea/modules/test"
1718
repo_service "code.gitea.io/gitea/services/repository"
1819
"code.gitea.io/gitea/tests"
1920

@@ -73,3 +74,80 @@ func TestPullCompare(t *testing.T) {
7374
assert.EqualValues(t, editButtonCount, 0, "Expected not to find a button to edit a file in the PR diff view because head repository has been deleted")
7475
})
7576
}
77+
78+
func TestPullCompare_EnableAllowEditsFromMaintainer(t *testing.T) {
79+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
80+
// repo3 is private
81+
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
82+
assert.True(t, repo3.IsPrivate)
83+
84+
// user4 forks repo3
85+
user4Session := loginUser(t, "user4")
86+
forkedRepoName := "user4-forked-repo3"
87+
testRepoFork(t, user4Session, repo3.OwnerName, repo3.Name, "user4", forkedRepoName, "")
88+
forkedRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user4", Name: forkedRepoName})
89+
assert.True(t, forkedRepo.IsPrivate)
90+
91+
// user4 creates a new branch and a PR
92+
testEditFileToNewBranch(t, user4Session, "user4", forkedRepoName, "master", "user4/update-readme", "README.md", "Hello, World\n(Edited by user4)\n")
93+
resp := testPullCreateDirectly(t, user4Session, repo3.OwnerName, repo3.Name, "master", "user4", forkedRepoName, "user4/update-readme", "PR for user4 forked repo3")
94+
prURL := test.RedirectURL(resp)
95+
96+
// user2 (admin of repo3) goes to the PR files page
97+
user2Session := loginUser(t, "user2")
98+
resp = user2Session.MakeRequest(t, NewRequest(t, "GET", fmt.Sprintf("%s/files", prURL)), http.StatusOK)
99+
htmlDoc := NewHTMLParser(t, resp.Body)
100+
nodes := htmlDoc.doc.Find(".diff-file-box[data-new-filename=\"README.md\"] .diff-file-header-actions .dropdown .menu a")
101+
if assert.Equal(t, 1, nodes.Length()) {
102+
// there is only "View File" button, no "Edit File" button
103+
assert.Equal(t, "View File", nodes.First().Text())
104+
viewFileLink, exists := nodes.First().Attr("href")
105+
if assert.True(t, exists) {
106+
user2Session.MakeRequest(t, NewRequest(t, "GET", viewFileLink), http.StatusOK)
107+
}
108+
}
109+
110+
// user4 goes to the PR page and enable "Allow maintainers to edit"
111+
resp = user4Session.MakeRequest(t, NewRequest(t, "GET", prURL), http.StatusOK)
112+
htmlDoc = NewHTMLParser(t, resp.Body)
113+
dataURL, exists := htmlDoc.doc.Find("#allow-edits-from-maintainers").Attr("data-url")
114+
assert.True(t, exists)
115+
req := NewRequestWithValues(t, "POST", fmt.Sprintf("%s/set_allow_maintainer_edit", dataURL), map[string]string{
116+
"_csrf": htmlDoc.GetCSRF(),
117+
"allow_maintainer_edit": "true",
118+
})
119+
user4Session.MakeRequest(t, req, http.StatusOK)
120+
121+
// user2 (admin of repo3) goes to the PR files page again
122+
resp = user2Session.MakeRequest(t, NewRequest(t, "GET", fmt.Sprintf("%s/files", prURL)), http.StatusOK)
123+
htmlDoc = NewHTMLParser(t, resp.Body)
124+
nodes = htmlDoc.doc.Find(".diff-file-box[data-new-filename=\"README.md\"] .diff-file-header-actions .dropdown .menu a")
125+
if assert.Equal(t, 2, nodes.Length()) {
126+
// there are "View File" button and "Edit File" button
127+
assert.Equal(t, "View File", nodes.First().Text())
128+
viewFileLink, exists := nodes.First().Attr("href")
129+
if assert.True(t, exists) {
130+
user2Session.MakeRequest(t, NewRequest(t, "GET", viewFileLink), http.StatusOK)
131+
}
132+
133+
assert.Equal(t, "Edit File", nodes.Last().Text())
134+
editFileLink, exists := nodes.Last().Attr("href")
135+
if assert.True(t, exists) {
136+
// edit the file
137+
resp := user2Session.MakeRequest(t, NewRequest(t, "GET", editFileLink), http.StatusOK)
138+
htmlDoc := NewHTMLParser(t, resp.Body)
139+
lastCommit := htmlDoc.GetInputValueByName("last_commit")
140+
assert.NotEmpty(t, lastCommit)
141+
req := NewRequestWithValues(t, "POST", editFileLink, map[string]string{
142+
"_csrf": htmlDoc.GetCSRF(),
143+
"last_commit": lastCommit,
144+
"tree_path": "README.md",
145+
"content": "File is edited by the maintainer user2",
146+
"commit_summary": "user2 updated the file",
147+
"commit_choice": "direct",
148+
})
149+
user2Session.MakeRequest(t, req, http.StatusSeeOther)
150+
}
151+
}
152+
})
153+
}

0 commit comments

Comments
 (0)