2017-05-02 00:49:55 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-05-02 00:49:55 +00:00
|
|
|
|
2022-09-02 19:18:23 +00:00
|
|
|
package integration
|
2017-05-02 00:49:55 +00:00
|
|
|
|
|
|
|
import (
|
2024-02-18 18:41:54 +00:00
|
|
|
"bytes"
|
2023-11-16 23:17:40 +00:00
|
|
|
"fmt"
|
2024-02-18 18:41:54 +00:00
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
2017-05-02 00:49:55 +00:00
|
|
|
"net/http"
|
2017-12-03 22:46:01 +00:00
|
|
|
"net/http/httptest"
|
2019-05-11 15:29:17 +00:00
|
|
|
"net/url"
|
2017-06-08 11:15:41 +00:00
|
|
|
"path"
|
2017-05-02 00:49:55 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-11-16 23:17:40 +00:00
|
|
|
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"
|
2023-01-16 08:00:22 +00:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2023-11-16 23:17:40 +00:00
|
|
|
"code.gitea.io/gitea/modules/translation"
|
2024-02-27 07:12:22 +00:00
|
|
|
gitea_context "code.gitea.io/gitea/services/context"
|
2023-11-16 23:17:40 +00:00
|
|
|
"code.gitea.io/gitea/tests"
|
2023-01-16 08:00:22 +00:00
|
|
|
|
2017-05-02 00:49:55 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-02-18 18:41:54 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-05-02 00:49:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateFile(t *testing.T) {
|
2019-05-11 15:29:17 +00:00
|
|
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
|
|
|
session := loginUser(t, "user2")
|
2024-05-21 15:23:22 +00:00
|
|
|
testCreateFile(t, session, "user2", "repo1", "master", "test.txt", "Content")
|
|
|
|
})
|
|
|
|
}
|
2017-05-02 00:49:55 +00:00
|
|
|
|
2024-05-21 15:23:22 +00:00
|
|
|
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)
|
2017-05-02 00:49:55 +00:00
|
|
|
|
2024-05-21 15:23:22 +00:00
|
|
|
doc := NewHTMLParser(t, resp.Body)
|
|
|
|
lastCommit := doc.GetInputValueByName("last_commit")
|
|
|
|
assert.NotEmpty(t, lastCommit)
|
2017-05-02 00:49:55 +00:00
|
|
|
|
2024-05-21 15:23:22 +00:00
|
|
|
// 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",
|
|
|
|
"commit_mail_id": "3",
|
2017-06-17 04:49:45 +00:00
|
|
|
})
|
2024-05-21 15:23:22 +00:00
|
|
|
return session.MakeRequest(t, req, http.StatusSeeOther)
|
2017-05-02 00:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateFileOnProtectedBranch(t *testing.T) {
|
2019-05-11 15:29:17 +00:00
|
|
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
|
|
|
|
csrf := GetCSRF(t, session, "/user2/repo1/settings/branches")
|
|
|
|
// Change master branch to protected
|
2023-01-16 08:00:22 +00:00
|
|
|
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", map[string]string{
|
|
|
|
"_csrf": csrf,
|
|
|
|
"rule_name": "master",
|
|
|
|
"enable_push": "true",
|
2019-05-11 15:29:17 +00:00
|
|
|
})
|
2022-03-23 04:54:07 +00:00
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
2019-05-11 15:29:17 +00:00
|
|
|
// Check if master branch has been locked successfully
|
2023-04-13 19:45:33 +00:00
|
|
|
flashCookie := session.GetCookie(gitea_context.CookieNameFlash)
|
2019-05-11 15:29:17 +00:00
|
|
|
assert.NotNil(t, flashCookie)
|
2023-04-17 22:04:26 +00:00
|
|
|
assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Brule%2B%2522master%2522%2Bhas%2Bbeen%2Bupdated.", flashCookie.Value)
|
2019-05-11 15:29:17 +00:00
|
|
|
|
|
|
|
// Request editor page
|
|
|
|
req = NewRequest(t, "GET", "/user2/repo1/_new/master/")
|
2019-06-12 19:41:28 +00:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
2019-05-11 15:29:17 +00:00
|
|
|
|
|
|
|
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{
|
2023-11-16 23:17:40 +00:00
|
|
|
"_csrf": doc.GetCSRF(),
|
|
|
|
"last_commit": lastCommit,
|
|
|
|
"tree_path": "test.txt",
|
|
|
|
"content": "Content",
|
|
|
|
"commit_choice": "direct",
|
|
|
|
"commit_mail_id": "3",
|
2019-05-11 15:29:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
// Check body for error message
|
2023-04-17 22:04:26 +00:00
|
|
|
assert.Contains(t, resp.Body.String(), "Cannot commit to protected branch "master".")
|
2019-05-11 15:29:17 +00:00
|
|
|
|
|
|
|
// remove the protected branch
|
|
|
|
csrf = GetCSRF(t, session, "/user2/repo1/settings/branches")
|
2023-01-16 08:00:22 +00:00
|
|
|
|
2019-05-11 15:29:17 +00:00
|
|
|
// Change master branch to protected
|
2023-01-16 08:00:22 +00:00
|
|
|
req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/1/delete", map[string]string{
|
|
|
|
"_csrf": csrf,
|
2019-05-11 15:29:17 +00:00
|
|
|
})
|
2023-01-16 08:00:22 +00:00
|
|
|
|
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
res := make(map[string]string)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&res))
|
2023-01-16 08:00:22 +00:00
|
|
|
assert.EqualValues(t, "/user2/repo1/settings/branches", res["redirect"])
|
|
|
|
|
2019-05-11 15:29:17 +00:00
|
|
|
// Check if master branch has been locked successfully
|
2023-04-13 19:45:33 +00:00
|
|
|
flashCookie = session.GetCookie(gitea_context.CookieNameFlash)
|
2019-05-11 15:29:17 +00:00
|
|
|
assert.NotNil(t, flashCookie)
|
2023-04-17 22:04:26 +00:00
|
|
|
assert.EqualValues(t, "error%3DRemoving%2Bbranch%2Bprotection%2Brule%2B%25221%2522%2Bfailed.", flashCookie.Value)
|
2017-09-14 08:16:22 +00:00
|
|
|
})
|
2017-05-02 00:49:55 +00:00
|
|
|
}
|
2017-06-08 11:15:41 +00:00
|
|
|
|
2017-12-03 22:46:01 +00:00
|
|
|
func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath, newContent string) *httptest.ResponseRecorder {
|
2017-06-08 11:15:41 +00:00
|
|
|
// Get to the 'edit this file' page
|
2017-06-10 00:41:36 +00:00
|
|
|
req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
|
2017-07-07 19:36:47 +00:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
2017-06-08 11:15:41 +00:00
|
|
|
|
2017-06-17 16:29:59 +00:00
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
2017-06-08 11:15:41 +00:00
|
|
|
lastCommit := htmlDoc.GetInputValueByName("last_commit")
|
|
|
|
assert.NotEmpty(t, lastCommit)
|
|
|
|
|
|
|
|
// Submit the edits
|
2017-06-17 04:49:45 +00:00
|
|
|
req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath),
|
|
|
|
map[string]string{
|
2023-11-16 23:17:40 +00:00
|
|
|
"_csrf": htmlDoc.GetCSRF(),
|
|
|
|
"last_commit": lastCommit,
|
|
|
|
"tree_path": filePath,
|
|
|
|
"content": newContent,
|
|
|
|
"commit_choice": "direct",
|
|
|
|
"commit_mail_id": "-1",
|
2017-06-17 04:49:45 +00:00
|
|
|
},
|
2017-06-08 11:15:41 +00:00
|
|
|
)
|
2022-06-20 10:02:49 +00:00
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
2017-06-08 11:15:41 +00:00
|
|
|
|
|
|
|
// Verify the change
|
2017-10-30 02:04:25 +00:00
|
|
|
req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", branch, filePath))
|
2017-07-07 19:36:47 +00:00
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
2017-12-03 22:46:01 +00:00
|
|
|
assert.EqualValues(t, newContent, resp.Body.String())
|
2017-06-15 11:20:39 +00:00
|
|
|
|
|
|
|
return resp
|
2017-06-08 11:15:41 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 22:46:01 +00:00
|
|
|
func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, branch, targetBranch, filePath, newContent string) *httptest.ResponseRecorder {
|
2017-06-21 01:00:03 +00:00
|
|
|
// Get to the 'edit this file' page
|
|
|
|
req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
|
2017-07-07 19:36:47 +00:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
2017-06-21 01:00:03 +00:00
|
|
|
|
|
|
|
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,
|
2023-11-16 23:17:40 +00:00
|
|
|
"commit_mail_id": "-1",
|
2017-06-21 01:00:03 +00:00
|
|
|
},
|
|
|
|
)
|
2022-06-20 10:02:49 +00:00
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
2017-06-21 01:00:03 +00:00
|
|
|
|
|
|
|
// Verify the change
|
2017-10-30 02:04:25 +00:00
|
|
|
req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", targetBranch, filePath))
|
2017-07-07 19:36:47 +00:00
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
2017-12-03 22:46:01 +00:00
|
|
|
assert.EqualValues(t, newContent, resp.Body.String())
|
2017-06-21 01:00:03 +00:00
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2017-06-08 11:15:41 +00:00
|
|
|
func TestEditFile(t *testing.T) {
|
2019-05-11 15:29:17 +00:00
|
|
|
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")
|
|
|
|
})
|
2017-06-08 11:15:41 +00:00
|
|
|
}
|
2017-06-21 01:00:03 +00:00
|
|
|
|
|
|
|
func TestEditFileToNewBranch(t *testing.T) {
|
2019-05-11 15:29:17 +00:00
|
|
|
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")
|
|
|
|
})
|
2017-06-21 01:00:03 +00:00
|
|
|
}
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-04-16 10:37:32 +00:00
|
|
|
func TestEditorAddTranslation(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
req := NewRequest(t, "GET", "/user2/repo1/_new/master")
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
placeholder, ok := htmlDoc.Find("input[name='commit_summary']").Attr("placeholder")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.EqualValues(t, `Add "<filename>"`, placeholder)
|
|
|
|
}
|
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
func TestCommitMail(t *testing.T) {
|
2023-11-16 23:17:40 +00:00
|
|
|
onGiteaRun(t, func(t *testing.T, _ *url.URL) {
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2024-02-18 18:41:54 +00:00
|
|
|
// Require that the user has KeepEmailPrivate enabled, because it needs
|
|
|
|
// to be tested that even with this setting enabled, it will use the
|
|
|
|
// provided mail and not revert to the placeholder one.
|
|
|
|
assert.True(t, user.KeepEmailPrivate)
|
|
|
|
|
|
|
|
inactivatedMail := unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{ID: 35, UID: user.ID})
|
|
|
|
assert.False(t, inactivatedMail.IsActivated)
|
|
|
|
|
|
|
|
otherEmail := unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{ID: 1, IsActivated: true})
|
|
|
|
assert.NotEqualValues(t, otherEmail.UID, user.ID)
|
|
|
|
|
|
|
|
primaryEmail := unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{ID: 3, UID: user.ID, IsActivated: true})
|
|
|
|
|
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
gitRepo, _ := git.OpenRepository(git.DefaultContext, repo1.RepoPath())
|
|
|
|
defer gitRepo.Close()
|
2023-11-16 23:17:40 +00:00
|
|
|
|
|
|
|
session := loginUser(t, user.Name)
|
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
lastCommitAndCSRF := func(t *testing.T, link string, skipLastCommit bool) (string, string) {
|
|
|
|
t.Helper()
|
|
|
|
|
2023-11-16 23:17:40 +00:00
|
|
|
req := NewRequest(t, "GET", link)
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
lastCommit := htmlDoc.GetInputValueByName("last_commit")
|
2024-02-18 18:41:54 +00:00
|
|
|
if !skipLastCommit {
|
|
|
|
assert.NotEmpty(t, lastCommit)
|
|
|
|
}
|
2023-11-16 23:17:40 +00:00
|
|
|
|
|
|
|
return lastCommit, htmlDoc.GetCSRF()
|
|
|
|
}
|
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
type caseOpts struct {
|
|
|
|
link string
|
|
|
|
fileName string
|
|
|
|
base map[string]string
|
|
|
|
skipLastCommit bool
|
|
|
|
}
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
// Base2 should have different content, so we can test two 'correct' operations
|
|
|
|
// without the second becoming a noop because no content was changed. If needed,
|
|
|
|
// link2 can point to a new file that's used with base2.
|
|
|
|
assertCase := func(t *testing.T, case1, case2 caseOpts) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
t.Run("Not activated", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
lastCommit, csrf := lastCommitAndCSRF(t, case1.link, case1.skipLastCommit)
|
|
|
|
baseCopy := case1.base
|
|
|
|
baseCopy["_csrf"] = csrf
|
|
|
|
baseCopy["last_commit"] = lastCommit
|
|
|
|
baseCopy["commit_mail_id"] = fmt.Sprintf("%d", inactivatedMail.ID)
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", case1.link, baseCopy)
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
assert.Contains(t,
|
|
|
|
htmlDoc.doc.Find(".ui.negative.message").Text(),
|
|
|
|
translation.NewLocale("en-US").Tr("repo.editor.invalid_commit_mail"),
|
|
|
|
)
|
2023-11-16 23:17:40 +00:00
|
|
|
})
|
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
t.Run("Not belong to user", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
lastCommit, csrf := lastCommitAndCSRF(t, case1.link, case1.skipLastCommit)
|
|
|
|
baseCopy := case1.base
|
|
|
|
baseCopy["_csrf"] = csrf
|
|
|
|
baseCopy["last_commit"] = lastCommit
|
|
|
|
baseCopy["commit_mail_id"] = fmt.Sprintf("%d", otherEmail.ID)
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
req := NewRequestWithValues(t, "POST", case1.link, baseCopy)
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
assert.Contains(t,
|
|
|
|
htmlDoc.doc.Find(".ui.negative.message").Text(),
|
|
|
|
translation.NewLocale("en-US").Tr("repo.editor.invalid_commit_mail"),
|
|
|
|
)
|
2023-11-16 23:17:40 +00:00
|
|
|
})
|
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
t.Run("Placeholder mail", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
lastCommit, csrf := lastCommitAndCSRF(t, case1.link, case1.skipLastCommit)
|
|
|
|
baseCopy := case1.base
|
|
|
|
baseCopy["_csrf"] = csrf
|
|
|
|
baseCopy["last_commit"] = lastCommit
|
|
|
|
baseCopy["commit_mail_id"] = "-1"
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", case1.link, baseCopy)
|
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
if !case2.skipLastCommit {
|
|
|
|
newlastCommit, _ := lastCommitAndCSRF(t, case1.link, false)
|
|
|
|
assert.NotEqualValues(t, newlastCommit, lastCommit)
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := gitRepo.GetCommitByPath(case1.fileName)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2024-02-18 18:41:54 +00:00
|
|
|
|
|
|
|
assert.EqualValues(t, "user2", commit.Author.Name)
|
|
|
|
assert.EqualValues(t, "user2@noreply.example.org", commit.Author.Email)
|
|
|
|
assert.EqualValues(t, "user2", commit.Committer.Name)
|
|
|
|
assert.EqualValues(t, "user2@noreply.example.org", commit.Committer.Email)
|
|
|
|
})
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
t.Run("Normal", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
lastCommit, csrf := lastCommitAndCSRF(t, case2.link, case2.skipLastCommit)
|
|
|
|
baseCopy := case2.base
|
|
|
|
baseCopy["_csrf"] = csrf
|
|
|
|
baseCopy["last_commit"] = lastCommit
|
|
|
|
baseCopy["commit_mail_id"] = fmt.Sprintf("%d", primaryEmail.ID)
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", case2.link, baseCopy)
|
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
if !case2.skipLastCommit {
|
|
|
|
newlastCommit, _ := lastCommitAndCSRF(t, case2.link, false)
|
|
|
|
assert.NotEqualValues(t, newlastCommit, lastCommit)
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := gitRepo.GetCommitByPath(case2.fileName)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2024-02-18 18:41:54 +00:00
|
|
|
|
|
|
|
assert.EqualValues(t, "user2", commit.Author.Name)
|
|
|
|
assert.EqualValues(t, primaryEmail.Email, commit.Author.Email)
|
|
|
|
assert.EqualValues(t, "user2", commit.Committer.Name)
|
|
|
|
assert.EqualValues(t, primaryEmail.Email, commit.Committer.Email)
|
|
|
|
})
|
|
|
|
}
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
t.Run("New", func(t *testing.T) {
|
2023-11-16 23:17:40 +00:00
|
|
|
defer tests.PrintCurrentTest(t)()
|
2024-02-18 18:41:54 +00:00
|
|
|
assertCase(t, caseOpts{
|
|
|
|
fileName: "new_file",
|
|
|
|
link: "user2/repo1/_new/master",
|
|
|
|
base: map[string]string{
|
|
|
|
"tree_path": "new_file",
|
|
|
|
"content": "new_content",
|
|
|
|
"commit_choice": "direct",
|
|
|
|
},
|
|
|
|
}, caseOpts{
|
|
|
|
fileName: "new_file_2",
|
|
|
|
link: "user2/repo1/_new/master",
|
|
|
|
base: map[string]string{
|
|
|
|
"tree_path": "new_file_2",
|
|
|
|
"content": "new_content",
|
|
|
|
"commit_choice": "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
t.Run("Edit", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
assertCase(t, caseOpts{
|
|
|
|
fileName: "README.md",
|
|
|
|
link: "user2/repo1/_edit/master/README.md",
|
|
|
|
base: map[string]string{
|
|
|
|
"tree_path": "README.md",
|
|
|
|
"content": "Edit content",
|
|
|
|
"commit_choice": "direct",
|
|
|
|
},
|
|
|
|
}, caseOpts{
|
|
|
|
fileName: "README.md",
|
|
|
|
link: "user2/repo1/_edit/master/README.md",
|
|
|
|
base: map[string]string{
|
|
|
|
"tree_path": "README.md",
|
|
|
|
"content": "Other content",
|
|
|
|
"commit_choice": "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
t.Run("Delete", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
assertCase(t, caseOpts{
|
|
|
|
fileName: "new_file",
|
|
|
|
link: "user2/repo1/_delete/master/new_file",
|
|
|
|
base: map[string]string{
|
|
|
|
"commit_choice": "direct",
|
|
|
|
},
|
|
|
|
}, caseOpts{
|
|
|
|
fileName: "new_file_2",
|
|
|
|
link: "user2/repo1/_delete/master/new_file_2",
|
|
|
|
base: map[string]string{
|
|
|
|
"commit_choice": "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
t.Run("Upload", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-04-11 12:13:36 +00:00
|
|
|
// Upload two separate times, so we have two different 'uploads' that can
|
2024-05-09 13:49:37 +00:00
|
|
|
// be used independently of each other.
|
2024-02-18 18:41:54 +00:00
|
|
|
uploadFile := func(t *testing.T, name, content string) string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
body := &bytes.Buffer{}
|
|
|
|
mpForm := multipart.NewWriter(body)
|
|
|
|
err := mpForm.WriteField("_csrf", GetCSRF(t, session, "/user2/repo1/_upload/master"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
file, err := mpForm.CreateFormFile("file", name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
io.Copy(file, bytes.NewBufferString(content))
|
|
|
|
require.NoError(t, mpForm.Close())
|
|
|
|
|
|
|
|
req := NewRequestWithBody(t, "POST", "/user2/repo1/upload-file", body)
|
|
|
|
req.Header.Add("Content-Type", mpForm.FormDataContentType())
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
respMap := map[string]string{}
|
|
|
|
DecodeJSON(t, resp, &respMap)
|
|
|
|
return respMap["uuid"]
|
|
|
|
}
|
|
|
|
|
|
|
|
file1UUID := uploadFile(t, "upload_file_1", "Uploaded a file!")
|
|
|
|
file2UUID := uploadFile(t, "upload_file_2", "Uploaded another file!")
|
|
|
|
|
|
|
|
assertCase(t, caseOpts{
|
|
|
|
fileName: "upload_file_1",
|
|
|
|
link: "user2/repo1/_upload/master",
|
|
|
|
skipLastCommit: true,
|
|
|
|
base: map[string]string{
|
|
|
|
"commit_choice": "direct",
|
|
|
|
"files": file1UUID,
|
|
|
|
},
|
|
|
|
}, caseOpts{
|
|
|
|
fileName: "upload_file_2",
|
|
|
|
link: "user2/repo1/_upload/master",
|
|
|
|
skipLastCommit: true,
|
|
|
|
base: map[string]string{
|
|
|
|
"commit_choice": "direct",
|
|
|
|
"files": file2UUID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2023-11-16 23:17:40 +00:00
|
|
|
})
|
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
t.Run("Apply patch", func(t *testing.T) {
|
2023-11-16 23:17:40 +00:00
|
|
|
defer tests.PrintCurrentTest(t)()
|
2024-02-18 18:41:54 +00:00
|
|
|
assertCase(t, caseOpts{
|
|
|
|
fileName: "diff-file-1.txt",
|
|
|
|
link: "user2/repo1/_diffpatch/master",
|
|
|
|
base: map[string]string{
|
|
|
|
"tree_path": "patch",
|
|
|
|
"commit_choice": "direct",
|
|
|
|
"content": `diff --git a/diff-file-1.txt b/diff-file-1.txt
|
|
|
|
new file mode 100644
|
|
|
|
index 0000000000..50fcd26d6c
|
|
|
|
--- /dev/null
|
|
|
|
+++ b/diff-file-1.txt
|
|
|
|
@@ -0,0 +1 @@
|
|
|
|
+File 1
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}, caseOpts{
|
|
|
|
fileName: "diff-file-2.txt",
|
|
|
|
link: "user2/repo1/_diffpatch/master",
|
|
|
|
base: map[string]string{
|
|
|
|
"tree_path": "patch",
|
|
|
|
"commit_choice": "direct",
|
|
|
|
"content": `diff --git a/diff-file-2.txt b/diff-file-2.txt
|
|
|
|
new file mode 100644
|
|
|
|
index 0000000000..4475433e27
|
|
|
|
--- /dev/null
|
|
|
|
+++ b/diff-file-2.txt
|
|
|
|
@@ -0,0 +1 @@
|
|
|
|
+File 2
|
|
|
|
`,
|
|
|
|
},
|
2023-11-16 23:17:40 +00:00
|
|
|
})
|
2024-02-18 18:41:54 +00:00
|
|
|
})
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
t.Run("Cherry pick", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
commitID1, err := gitRepo.GetCommitByPath("diff-file-1.txt")
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2024-02-18 18:41:54 +00:00
|
|
|
commitID2, err := gitRepo.GetCommitByPath("diff-file-2.txt")
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2023-11-16 23:17:40 +00:00
|
|
|
|
2024-02-18 18:41:54 +00:00
|
|
|
assertCase(t, caseOpts{
|
|
|
|
fileName: "diff-file-1.txt",
|
|
|
|
link: "user2/repo1/_cherrypick/" + commitID1.ID.String() + "/master",
|
|
|
|
base: map[string]string{
|
|
|
|
"commit_choice": "direct",
|
|
|
|
"revert": "true",
|
|
|
|
},
|
|
|
|
}, caseOpts{
|
|
|
|
fileName: "diff-file-2.txt",
|
|
|
|
link: "user2/repo1/_cherrypick/" + commitID2.ID.String() + "/master",
|
|
|
|
base: map[string]string{
|
|
|
|
"commit_choice": "direct",
|
|
|
|
"revert": "true",
|
|
|
|
},
|
|
|
|
})
|
2023-11-16 23:17:40 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|