Skip to content

Commit eead5c0

Browse files
committed
pick some tests
1 parent c7395dc commit eead5c0

File tree

1 file changed

+91
-9
lines changed

1 file changed

+91
-9
lines changed

tests/integration/editor_test.go

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,43 @@ import (
3232
func TestEditor(t *testing.T) {
3333
onGiteaRun(t, func(t *testing.T, u *url.URL) {
3434
sessionUser2 := loginUser(t, "user2")
35-
t.Run("CreateFile", func(t *testing.T) {
36-
testCreateFile(t, sessionUser2, "user2", "repo1", "master", "test.txt", "Content")
37-
})
35+
t.Run("EditFileNotAllowed", testEditFileNotAllowed)
36+
t.Run("DiffPreview", testEditorDiffPreview)
37+
t.Run("CreateFile", testEditorCreateFile)
3838
t.Run("EditFile", func(t *testing.T) {
3939
testEditFile(t, sessionUser2, "user2", "repo1", "master", "README.md", "Hello, World (direct)\n")
40-
})
41-
t.Run("EditFileToNewBranch", func(t *testing.T) {
4240
testEditFileToNewBranch(t, sessionUser2, "user2", "repo1", "master", "feature/test", "README.md", "Hello, World (commit-to-new-branch)\n")
4341
})
42+
t.Run("PatchFile", testEditorPatchFile)
43+
t.Run("DeleteFile", func(t *testing.T) {
44+
viewLink := "/user2/repo1/src/branch/branch2/README.md"
45+
sessionUser2.MakeRequest(t, NewRequest(t, "GET", viewLink), http.StatusOK)
46+
testEditorActionPostRequest(t, sessionUser2, "/user2/repo1/_delete/branch2/README.md", map[string]string{"commit_choice": "direct"})
47+
sessionUser2.MakeRequest(t, NewRequest(t, "GET", viewLink), http.StatusNotFound)
48+
})
4449
t.Run("ForkToEditFile", func(t *testing.T) {
4550
testForkToEditFile(t, loginUser(t, "user4"), "user4", "user2", "repo1", "master", "README.md")
4651
})
47-
t.Run("WebGitCommitEmail", testWebGitCommitEmail)
48-
t.Run("CreateFileOnProtectedBranch", testCreateFileOnProtectedBranch)
52+
t.Run("WebGitCommitEmail", testEditorWebGitCommitEmail)
53+
t.Run("ProtectedBranch", testEditorProtectedBranch)
4954
})
5055
}
5156

57+
func testEditorCreateFile(t *testing.T) {
58+
session := loginUser(t, "user2")
59+
testCreateFile(t, session, "user2", "repo1", "master", "test.txt", "Content")
60+
testEditorActionPostRequestError(t, session, "/user2/repo1/_new/master/", map[string]string{
61+
"tree_path": "test.txt",
62+
"commit_choice": "direct",
63+
"new_branch_name": "master",
64+
}, `A file named "test.txt" already exists in this repository.`)
65+
testEditorActionPostRequestError(t, session, "/user2/repo1/_new/master/", map[string]string{
66+
"tree_path": "test.txt",
67+
"commit_choice": "commit-to-new-branch",
68+
"new_branch_name": "branch2",
69+
}, `Branch "branch2" already exists in this repository.`)
70+
}
71+
5272
func testCreateFile(t *testing.T, session *TestSession, user, repo, branch, filePath, content string) {
5373
testEditorActionEdit(t, session, user, repo, "_new", branch, "", map[string]string{
5474
"tree_path": filePath,
@@ -57,7 +77,7 @@ func testCreateFile(t *testing.T, session *TestSession, user, repo, branch, file
5777
})
5878
}
5979

60-
func testCreateFileOnProtectedBranch(t *testing.T) {
80+
func testEditorProtectedBranch(t *testing.T) {
6181
session := loginUser(t, "user2")
6282
// Change the "master" branch to "protected"
6383
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", map[string]string{
@@ -88,6 +108,12 @@ func testEditorActionPostRequest(t *testing.T, session *TestSession, requestPath
88108
return session.MakeRequest(t, req, NoExpectedStatus)
89109
}
90110

111+
func testEditorActionPostRequestError(t *testing.T, session *TestSession, requestPath string, params map[string]string, errorMessage string) {
112+
resp := testEditorActionPostRequest(t, session, requestPath, params)
113+
assert.Equal(t, http.StatusBadRequest, resp.Code)
114+
assert.Equal(t, errorMessage, test.ParseJSONError(resp.Body.Bytes()).ErrorMessage)
115+
}
116+
91117
func testEditorActionEdit(t *testing.T, session *TestSession, user, repo, editorAction, branch, filePath string, params map[string]string) *httptest.ResponseRecorder {
92118
params["tree_path"] = util.IfZero(params["tree_path"], filePath)
93119
newBranchName := util.Iif(params["commit_choice"] == "direct", branch, params["new_branch_name"])
@@ -115,7 +141,40 @@ func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, bra
115141
})
116142
}
117143

118-
func testWebGitCommitEmail(t *testing.T) {
144+
func testEditorDiffPreview(t *testing.T) {
145+
session := loginUser(t, "user2")
146+
req := NewRequestWithValues(t, "POST", "/user2/repo1/_preview/master/README.md", map[string]string{
147+
"_csrf": GetUserCSRFToken(t, session),
148+
"content": "Hello, World (Edited)\n",
149+
})
150+
resp := session.MakeRequest(t, req, http.StatusOK)
151+
assert.Contains(t, resp.Body.String(), `<span class="added-code">Hello, World (Edited)</span>`)
152+
}
153+
154+
func testEditorPatchFile(t *testing.T) {
155+
session := loginUser(t, "user2")
156+
pathContent := `diff --git a/patch-file-1.txt b/patch-file-1.txt
157+
new file mode 100644
158+
index 0000000000..aaaaaaaaaa
159+
--- /dev/null
160+
+++ b/patch-file-1.txt
161+
@@ -0,0 +1 @@
162+
+patched content
163+
`
164+
patchForm := map[string]string{
165+
"content": pathContent,
166+
"commit_choice": "commit-to-new-branch",
167+
"new_branch_name": "patched-branch",
168+
}
169+
testEditorActionPostRequest(t, session, "/user2/repo1/_diffpatch/master/", patchForm)
170+
resp := MakeRequest(t, NewRequest(t, "GET", "/user2/repo1/raw/branch/patched-branch/patch-file-1.txt"), http.StatusOK)
171+
assert.Equal(t, "patched content\n", resp.Body.String())
172+
173+
resp = testEditorActionPostRequest(t, session, "/user2/repo1/_diffpatch/master/", patchForm)
174+
assert.Equal(t, "Unable to apply patch", test.ParseJSONError(resp.Body.Bytes()).ErrorMessage)
175+
}
176+
177+
func testEditorWebGitCommitEmail(t *testing.T) {
119178
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
120179
require.True(t, user.KeepEmailPrivate)
121180

@@ -368,3 +427,26 @@ func testForkToEditFile(t *testing.T, session *TestSession, user, owner, repo, b
368427
assert.Contains(t, resp.Body.String(), "new content in fork")
369428
})
370429
}
430+
431+
func testEditFileNotAllowed(t *testing.T) {
432+
sessionUser1 := loginUser(t, "user1") // admin, all access
433+
sessionUser4 := loginUser(t, "user4")
434+
// "_cherrypick" has a different route pattern, so skip its test
435+
operations := []string{"_new", "_edit", "_delete", "_upload", "_diffpatch"}
436+
for _, operation := range operations {
437+
t.Run(operation, func(t *testing.T) {
438+
// Branch does not exist
439+
targetLink := path.Join("user2", "repo1", operation, "missing", "README.md")
440+
sessionUser1.MakeRequest(t, NewRequest(t, "GET", targetLink), http.StatusNotFound)
441+
442+
// Private repository
443+
targetLink = path.Join("user2", "repo2", operation, "master", "Home.md")
444+
sessionUser1.MakeRequest(t, NewRequest(t, "GET", targetLink), http.StatusOK)
445+
sessionUser4.MakeRequest(t, NewRequest(t, "GET", targetLink), http.StatusNotFound)
446+
447+
// Empty repository
448+
targetLink = path.Join("org41", "repo61", operation, "master", "README.md")
449+
sessionUser1.MakeRequest(t, NewRequest(t, "GET", targetLink), http.StatusNotFound)
450+
})
451+
}
452+
}

0 commit comments

Comments
 (0)