aboutsummaryrefslogtreecommitdiffstats
path: root/tests/integration/editor_test.go
blob: a5936d86de225bc70e195508870ec264a67079d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"bytes"
	"fmt"
	"io"
	"mime/multipart"
	"net/http"
	"net/http/httptest"
	"net/url"
	"path"
	"strings"
	"testing"

	repo_model "code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/git"
	"code.gitea.io/gitea/modules/json"
	"code.gitea.io/gitea/modules/translation"
	"code.gitea.io/gitea/tests"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestCreateFile(t *testing.T) {
	onGiteaRun(t, func(t *testing.T, u *url.URL) {
		session := loginUser(t, "user2")
		testCreateFile(t, session, "user2", "repo1", "master", "test.txt", "Content")
	})
}

func testCreateFile(t *testing.T, session *TestSession, user, repo, branch, filePath, content string) *httptest.ResponseRecorder {
	// Request editor page
	newURL := fmt.Sprintf("/%s/%s/_new/%s/", user, repo, branch)
	req := NewRequest(t, "GET", newURL)
	resp := session.MakeRequest(t, req, http.StatusOK)

	doc := NewHTMLParser(t, resp.Body)
	lastCommit := doc.GetInputValueByName("last_commit")
	assert.NotEmpty(t, lastCommit)

	// Save new file to master branch
	req = NewRequestWithValues(t, "POST", newURL, map[string]string{
		"_csrf":         doc.GetCSRF(),
		"last_commit":   lastCommit,
		"tree_path":     filePath,
		"content":       content,
		"commit_choice": "direct",
	})
	return session.MakeRequest(t, req, http.StatusSeeOther)
}

func TestCreateFileOnProtectedBranch(t *testing.T) {
	onGiteaRun(t, func(t *testing.T, u *url.URL) {
		session := loginUser(t, "user2")

		csrf := GetUserCSRFToken(t, session)
		// Change master branch to protected
		req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", map[string]string{
			"_csrf":       csrf,
			"rule_name":   "master",
			"enable_push": "true",
		})
		session.MakeRequest(t, req, http.StatusSeeOther)
		// Check if master branch has been locked successfully
		flashMsg := session.GetCookieFlashMessage()
		assert.Equal(t, `Branch protection for rule "master" has been updated.`, flashMsg.SuccessMsg)

		// Request editor page
		req = NewRequest(t, "GET", "/user2/repo1/_new/master/")
		resp := session.MakeRequest(t, req, http.StatusOK)

		doc := NewHTMLParser(t, resp.Body)
		lastCommit := doc.GetInputValueByName("last_commit")
		assert.NotEmpty(t, lastCommit)

		// Save new file to master branch
		req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
			"_csrf":         doc.GetCSRF(),
			"last_commit":   lastCommit,
			"tree_path":     "test.txt",
			"content":       "Content",
			"commit_choice": "direct",
		})

		resp = session.MakeRequest(t, req, http.StatusOK)
		// Check body for error message
		assert.Contains(t, resp.Body.String(), "Cannot commit to protected branch "master".")

		// remove the protected branch
		csrf = GetUserCSRFToken(t, session)

		// Change master branch to protected
		req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/1/delete", map[string]string{
			"_csrf": csrf,
		})

		resp = session.MakeRequest(t, req, http.StatusOK)

		res := make(map[string]string)
		assert.NoError(t, json.NewDecoder(resp.Body).Decode(&res))
		assert.Equal(t, "/user2/repo1/settings/branches", res["redirect"])

		// Check if master branch has been locked successfully
		flashMsg = session.GetCookieFlashMessage()
		assert.Equal(t, `Removing branch protection rule "1" failed.`, flashMsg.ErrorMsg)
	})
}

func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath, newContent string) *httptest.ResponseRecorder {
	// Get to the 'edit this file' page
	req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
	resp := session.MakeRequest(t, req, http.StatusOK)

	htmlDoc := NewHTMLParser(t, resp.Body)
	lastCommit := htmlDoc.GetInputValueByName("last_commit")
	assert.NotEmpty(t, lastCommit)

	// Submit the edits
	req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
		map[string]string{
			"_csrf":         htmlDoc.GetCSRF(),
			"last_commit":   lastCommit,
			"tree_path":     filePath,
			"content":       newContent,
			"commit_choice": "direct",
		},
	)
	session.MakeRequest(t, req, http.StatusSeeOther)

	// Verify the change
	req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", branch, filePath))
	resp = session.MakeRequest(t, req, http.StatusOK)
	assert.Equal(t, newContent, resp.Body.String())

	return resp
}

func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, branch, targetBranch, filePath, newContent string) *httptest.ResponseRecorder {
	// Get to the 'edit this file' page
	req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
	resp := session.MakeRequest(t, req, http.StatusOK)

	htmlDoc := NewHTMLParser(t, resp.Body)
	lastCommit := htmlDoc.GetInputValueByName("last_commit")
	assert.NotEmpty(t, lastCommit)

	// Submit the edits
	req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
		map[string]string{
			"_csrf":           htmlDoc.GetCSRF(),
			"last_commit":     lastCommit,
			"tree_path":       filePath,
			"content":         newContent,
			"commit_choice":   "commit-to-new-branch",
			"new_branch_name": targetBranch,
		},
	)
	session.MakeRequest(t, req, http.StatusSeeOther)

	// Verify the change
	req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", targetBranch, filePath))
	resp = session.MakeRequest(t, req, http.StatusOK)
	assert.Equal(t, newContent, resp.Body.String())

	return resp
}

func TestEditFile(t *testing.T) {
	onGiteaRun(t, func(t *testing.T, u *url.URL) {
		session := loginUser(t, "user2")
		testEditFile(t, session, "user2", "repo1", "master", "README.md", "Hello, World (Edited)\n")
	})
}

func TestEditFileToNewBranch(t *testing.T) {
	onGiteaRun(t, func(t *testing.T, u *url.URL) {
		session := loginUser(t, "user2")
		testEditFileToNewBranch(t, session, "user2", "repo1", "master", "feature/test", "README.md", "Hello, World (Edited)\n")
	})
}

func TestWebGitCommitEmail(t *testing.T) {
	onGiteaRun(t, func(t *testing.T, _ *url.URL) {
		user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
		require.True(t, user.KeepEmailPrivate)

		repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
		gitRepo, _ := git.OpenRepository(git.DefaultContext, repo1.RepoPath())
		defer gitRepo.Close()
		getLastCommit := func(t *testing.T) *git.Commit {
			c, err := gitRepo.GetBranchCommit("master")
			require.NoError(t, err)
			return c
		}

		session := loginUser(t, user.Name)

		makeReq := func(t *testing.T, link string, params map[string]string, expectedUserName, expectedEmail string) *httptest.ResponseRecorder {
			lastCommit := getLastCommit(t)
			params["_csrf"] = GetUserCSRFToken(t, session)
			params["last_commit"] = lastCommit.ID.String()
			params["commit_choice"] = "direct"
			req := NewRequestWithValues(t, "POST", link, params)
			resp := session.MakeRequest(t, req, NoExpectedStatus)
			newCommit := getLastCommit(t)
			if expectedUserName == "" {
				require.Equal(t, lastCommit.ID.String(), newCommit.ID.String())
				htmlDoc := NewHTMLParser(t, resp.Body)
				errMsg := htmlDoc.doc.Find(".ui.negative.message").Text()
				assert.Contains(t, errMsg, translation.NewLocale("en-US").Tr("repo.editor.invalid_commit_email"))
			} else {
				require.NotEqual(t, lastCommit.ID.String(), newCommit.ID.String())
				assert.Equal(t, expectedUserName, newCommit.Author.Name)
				assert.Equal(t, expectedEmail, newCommit.Author.Email)
				assert.Equal(t, expectedUserName, newCommit.Committer.Name)
				assert.Equal(t, expectedEmail, newCommit.Committer.Email)
			}
			return resp
		}

		uploadFile := func(t *testing.T, name, content string) string {
			body := &bytes.Buffer{}
			uploadForm := multipart.NewWriter(body)
			file, _ := uploadForm.CreateFormFile("file", name)
			_, _ = io.Copy(file, strings.NewReader(content))
			_ = uploadForm.WriteField("_csrf", GetUserCSRFToken(t, session))
			_ = uploadForm.Close()

			req := NewRequestWithBody(t, "POST", "/user2/repo1/upload-file", body)
			req.Header.Add("Content-Type", uploadForm.FormDataContentType())
			resp := session.MakeRequest(t, req, http.StatusOK)

			respMap := map[string]string{}
			DecodeJSON(t, resp, &respMap)
			return respMap["uuid"]
		}

		t.Run("EmailInactive", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()
			email := unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{ID: 35, UID: user.ID})
			require.False(t, email.IsActivated)
			makeReq(t, "/user2/repo1/_edit/master/README.md", map[string]string{
				"tree_path":    "README.md",
				"content":      "test content",
				"commit_email": email.Email,
			}, "", "")
		})

		t.Run("EmailInvalid", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()
			email := unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{ID: 1, IsActivated: true})
			require.NotEqual(t, email.UID, user.ID)
			makeReq(t, "/user2/repo1/_edit/master/README.md", map[string]string{
				"tree_path":    "README.md",
				"content":      "test content",
				"commit_email": email.Email,
			}, "", "")
		})

		testWebGit := func(t *testing.T, linkForKeepPrivate string, paramsForKeepPrivate map[string]string, linkForChosenEmail string, paramsForChosenEmail map[string]string) (resp1, resp2 *httptest.ResponseRecorder) {
			t.Run("DefaultEmailKeepPrivate", func(t *testing.T) {
				defer tests.PrintCurrentTest(t)()
				paramsForKeepPrivate["commit_email"] = ""
				resp1 = makeReq(t, linkForKeepPrivate, paramsForKeepPrivate, "User Two", "user2@noreply.example.org")
			})
			t.Run("ChooseEmail", func(t *testing.T) {
				defer tests.PrintCurrentTest(t)()
				paramsForChosenEmail["commit_email"] = "user2@example.com"
				resp2 = makeReq(t, linkForChosenEmail, paramsForChosenEmail, "User Two", "user2@example.com")
			})
			return resp1, resp2
		}

		t.Run("Edit", func(t *testing.T) {
			testWebGit(t,
				"/user2/repo1/_edit/master/README.md", map[string]string{"tree_path": "README.md", "content": "for keep private"},
				"/user2/repo1/_edit/master/README.md", map[string]string{"tree_path": "README.md", "content": "for chosen email"},
			)
		})

		t.Run("UploadDelete", func(t *testing.T) {
			file1UUID := uploadFile(t, "file1", "File 1")
			file2UUID := uploadFile(t, "file2", "File 2")
			testWebGit(t,
				"/user2/repo1/_upload/master", map[string]string{"files": file1UUID},
				"/user2/repo1/_upload/master", map[string]string{"files": file2UUID},
			)
			testWebGit(t,
				"/user2/repo1/_delete/master/file1", map[string]string{},
				"/user2/repo1/_delete/master/file2", map[string]string{},
			)
		})

		t.Run("ApplyPatchCherryPick", func(t *testing.T) {
			testWebGit(t,
				"/user2/repo1/_diffpatch/master", map[string]string{
					"tree_path": "__dummy__",
					"content": `diff --git a/patch-file-1.txt b/patch-file-1.txt
new file mode 100644
index 0000000000..aaaaaaaaaa
--- /dev/null
+++ b/patch-file-1.txt
@@ -0,0 +1 @@
+File 1
`,
				},
				"/user2/repo1/_diffpatch/master", map[string]string{
					"tree_path": "__dummy__",
					"content": `diff --git a/patch-file-2.txt b/patch-file-2.txt
new file mode 100644
index 0000000000..bbbbbbbbbb
--- /dev/null
+++ b/patch-file-2.txt
@@ -0,0 +1 @@
+File 2
`,
				},
			)

			commit1, err := gitRepo.GetCommitByPath("patch-file-1.txt")
			require.NoError(t, err)
			commit2, err := gitRepo.GetCommitByPath("patch-file-2.txt")
			require.NoError(t, err)
			resp1, _ := testWebGit(t,
				"/user2/repo1/_cherrypick/"+commit1.ID.String()+"/master", map[string]string{"revert": "true"},
				"/user2/repo1/_cherrypick/"+commit2.ID.String()+"/master", map[string]string{"revert": "true"},
			)

			// By the way, test the "cherrypick" page: a successful revert redirects to the main branch
			assert.Equal(t, "/user2/repo1/src/branch/master", resp1.Header().Get("Location"))
		})
	})
}