+ {{template "repo/header" .}} @@ -1315,7 +1348,6 @@ index 0000000000..f89bf2f3f2 + {{template "base/alert" .}} +
+ {{.CsrfTokenHtml}} -+ +
+ +
+
@@ -1382,7 +1414,7 @@ index 5725020406..2b3737eeac 100644 + diff --git a/tests/integration/editor_test.go b/tests/integration/editor_test.go -index f0f71b80d1..b9ed72c196 100644 +index a5936d86de..3c54228a13 100644 --- a/tests/integration/editor_test.go +++ b/tests/integration/editor_test.go -@@ -9,9 +9,11 @@ import ( - "net/http/httptest" - "net/url" - "path" -+ "strings" - "testing" - +@@ -20,6 +20,7 @@ import ( + 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/test" - gitea_context "code.gitea.io/gitea/services/context" + "code.gitea.io/gitea/modules/translation" + "code.gitea.io/gitea/tests" - "github.com/stretchr/testify/assert" -@@ -20,13 +22,26 @@ import ( +@@ -30,13 +31,26 @@ import ( func TestCreateFile(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { session := loginUser(t, "user2") @@ -1557,7 +1511,7 @@ index f0f71b80d1..b9ed72c196 100644 req := NewRequest(t, "GET", newURL) resp := session.MakeRequest(t, req, http.StatusOK) -@@ -36,72 +51,99 @@ func testCreateFile(t *testing.T, session *TestSession, user, repo, branch, file +@@ -46,72 +60,99 @@ func testCreateFile(t *testing.T, session *TestSession, user, repo, branch, file // Save new file to master branch req = NewRequestWithValues(t, "POST", newURL, map[string]string{ @@ -1594,10 +1548,7 @@ index f0f71b80d1..b9ed72c196 100644 func TestCreateFileOnProtectedBranch(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { session := loginUser(t, "user2") -+ testCreateFileOnProtectedBranch(t, session, "user2", "user2", "repo1", "master", "master", "direct") -+ }) -+} - +- - csrf := GetUserCSRFToken(t, session) - // Change master branch to protected - req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", map[string]string{ @@ -1607,9 +1558,8 @@ index f0f71b80d1..b9ed72c196 100644 - }) - session.MakeRequest(t, req, http.StatusSeeOther) - // Check if master branch has been locked successfully -- flashCookie := session.GetCookie(gitea_context.CookieNameFlash) -- assert.NotNil(t, flashCookie) -- assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Brule%2B%2522master%2522%2Bhas%2Bbeen%2Bupdated.", flashCookie.Value) +- 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/") @@ -1618,14 +1568,7 @@ index f0f71b80d1..b9ed72c196 100644 - doc := NewHTMLParser(t, resp.Body) - lastCommit := doc.GetInputValueByName("last_commit") - assert.NotEmpty(t, lastCommit) -+func TestCreateFileOnProtectedBranchFork(t *testing.T) { -+ onGiteaRun(t, func(t *testing.T, u *url.URL) { -+ session := loginUser(t, "user4") -+ forkToEdit(t, session, "user2", "repo1", "_new", "master", "test.txt") -+ testCreateFileOnProtectedBranch(t, session, "user4", "user2", "repo1", "master", "feature/test", "commit-to-new-branch") -+ }) -+} - +- - // Save new file to master branch - req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{ - "_csrf": doc.GetCSRF(), @@ -1634,6 +1577,40 @@ index f0f71b80d1..b9ed72c196 100644 - "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) ++ testCreateFileOnProtectedBranch(t, session, "user2", "user2", "repo1", "master", "master", "direct") + }) + } + ++func TestCreateFileOnProtectedBranchFork(t *testing.T) { ++ onGiteaRun(t, func(t *testing.T, u *url.URL) { ++ session := loginUser(t, "user4") ++ forkToEdit(t, session, "user2", "repo1", "_new", "master", "test.txt") ++ testCreateFileOnProtectedBranch(t, session, "user4", "user2", "repo1", "master", "feature/test", "commit-to-new-branch") ++ }) ++} ++ +func testCreateFileOnProtectedBranch(t *testing.T, session *TestSession, user, owner, repo, branch, targetBranch, commitChoice string) { + csrf := GetUserCSRFToken(t, session) + // Change target branch to protected @@ -1644,27 +1621,17 @@ index f0f71b80d1..b9ed72c196 100644 + }) + session.MakeRequest(t, req, http.StatusSeeOther) + // Check if target branch has been locked successfully -+ flashCookie := session.GetCookie(gitea_context.CookieNameFlash) -+ assert.NotNil(t, flashCookie) -+ assert.Equal(t, "success%3DBranch%2Bprotection%2Bfor%2Brule%2B%2522"+strings.ReplaceAll(targetBranch, "/", "%252F")+"%2522%2Bhas%2Bbeen%2Bupdated.", flashCookie.Value) - -- resp = session.MakeRequest(t, req, http.StatusOK) -- // Check body for error message -- assert.Contains(t, resp.Body.String(), "Cannot commit to protected branch "master".") ++ flashMsg := session.GetCookieFlashMessage() ++ assert.Equal(t, fmt.Sprintf(`Branch protection for rule "%s" has been updated.`, targetBranch), flashMsg.SuccessMsg) ++ + // Request editor page + req = NewRequest(t, "GET", path.Join(owner, repo, "_new", branch)) + resp := session.MakeRequest(t, req, http.StatusOK) - -- // remove the protected branch -- csrf = GetUserCSRFToken(t, session) ++ + doc := NewHTMLParser(t, resp.Body) + lastCommit := doc.GetInputValueByName("last_commit") + assert.NotEmpty(t, lastCommit) - -- // Change master branch to protected -- req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/1/delete", map[string]string{ -- "_csrf": csrf, -- }) ++ + // Save new file to target branch + req = NewRequestWithValues(t, "POST", path.Join(owner, repo, "_new", branch), map[string]string{ + "_csrf": doc.GetCSRF(), @@ -1674,26 +1641,18 @@ index f0f71b80d1..b9ed72c196 100644 + "commit_choice": commitChoice, + "new_branch_name": targetBranch, + }) - -- resp = session.MakeRequest(t, req, http.StatusOK) ++ + resp = session.MakeRequest(t, req, http.StatusOK) + // Check body for error message + assert.Contains(t, resp.Body.String(), fmt.Sprintf("Cannot commit to protected branch "%s".", targetBranch)) - -- res := make(map[string]string) -- assert.NoError(t, json.NewDecoder(resp.Body).Decode(&res)) -- assert.EqualValues(t, "/user2/repo1/settings/branches", res["redirect"]) ++ + // remove the protected branch + csrf = GetUserCSRFToken(t, session) - -- // Check if master branch has been locked successfully -- flashCookie = session.GetCookie(gitea_context.CookieNameFlash) -- assert.NotNil(t, flashCookie) -- assert.EqualValues(t, "error%3DRemoving%2Bbranch%2Bprotection%2Brule%2B%25221%2522%2Bfailed.", flashCookie.Value) ++ + // Change target branch to protected + req = NewRequestWithValues(t, "POST", path.Join(user, repo, "settings", "branches", "1", "delete"), map[string]string{ + "_csrf": csrf, - }) ++ }) + + resp = session.MakeRequest(t, req, http.StatusOK) + @@ -1702,13 +1661,14 @@ index f0f71b80d1..b9ed72c196 100644 + assert.Equal(t, "/"+path.Join(user, repo, "settings", "branches"), res["redirect"]) + + // Check if target branch has been locked successfully -+ flashCookie = session.GetCookie(gitea_context.CookieNameFlash) -+ assert.NotNil(t, flashCookie) -+ assert.Equal(t, "error%3DRemoving%2Bbranch%2Bprotection%2Brule%2B%25221%2522%2Bfailed.", flashCookie.Value) - } - ++ 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 { -@@ -133,9 +175,9 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa + // Get to the 'edit this file' page + req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath)) +@@ -141,9 +182,9 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa return resp } @@ -1720,7 +1680,7 @@ index f0f71b80d1..b9ed72c196 100644 resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) -@@ -143,7 +185,7 @@ func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, bra +@@ -151,7 +192,7 @@ func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, bra assert.NotEmpty(t, lastCommit) // Submit the edits @@ -1729,15 +1689,15 @@ index f0f71b80d1..b9ed72c196 100644 map[string]string{ "_csrf": htmlDoc.GetCSRF(), "last_commit": lastCommit, -@@ -173,6 +215,256 @@ func TestEditFile(t *testing.T) { +@@ -181,10 +222,168 @@ func TestEditFile(t *testing.T) { 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") + testEditFileToNewBranch(t, session, "user2", "user2", "repo1", "master", "feature/test", "README.md", "Hello, World (Edited)\n") -+ }) -+} -+ + }) + } + +func TestEditFileToNewBranchFork(t *testing.T) { + onGiteaRun(t, func(t *testing.T, u *url.URL) { + session := loginUser(t, "user4") @@ -1764,7 +1724,7 @@ index f0f71b80d1..b9ed72c196 100644 + ) + resp = session.MakeRequest(t, req, http.StatusOK) + -+ assert.Contains(t, resp.Body.String(), `Hello, World (Edited)`) ++ assert.Contains(t, resp.Body.String(), `Hello, World (Edited)`) +} + +func TestEditFileDiffPreview(t *testing.T) { @@ -1895,6 +1855,14 @@ index f0f71b80d1..b9ed72c196 100644 + req = NewRequestf(t, "GET", "/%s/%s/src/branch/%s/%s", user, repo, targetBranch, "patch-file-1.txt") + session.MakeRequest(t, req, http.StatusOK) +} ++ + func TestWebGitCommitEmail(t *testing.T) { + onGiteaRun(t, func(t *testing.T, _ *url.URL) { + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) +@@ -337,3 +536,95 @@ index 0000000000..bbbbbbbbbb + }) + }) + } + +func forkToEdit(t *testing.T, session *TestSession, owner, repo, operation, branch, filePath string) { + // Attempt to edit file @@ -1985,13 +1953,13 @@ index f0f71b80d1..b9ed72c196 100644 + req = NewRequest(t, "GET", url) + session.MakeRequest(t, req, http.StatusNotFound) + } - }) - } ++ }) ++} diff --git a/tests/integration/pull_compare_test.go b/tests/integration/pull_compare_test.go -index 106774aa54..f5fd26ae95 100644 +index 86bdd1b9e3..c29d2001ad 100644 --- a/tests/integration/pull_compare_test.go +++ b/tests/integration/pull_compare_test.go -@@ -89,7 +89,7 @@ func TestPullCompare_EnableAllowEditsFromMaintainer(t *testing.T) { +@@ -104,7 +104,7 @@ func TestPullCompare_EnableAllowEditsFromMaintainer(t *testing.T) { assert.True(t, forkedRepo.IsPrivate) // user4 creates a new branch and a PR @@ -2001,10 +1969,10 @@ index 106774aa54..f5fd26ae95 100644 prURL := test.RedirectURL(resp) diff --git a/tests/integration/pull_merge_test.go b/tests/integration/pull_merge_test.go -index 1521fcfe8a..9e0e89fe9a 100644 +index cf50d5e639..3080f484ce 100644 --- a/tests/integration/pull_merge_test.go +++ b/tests/integration/pull_merge_test.go -@@ -182,7 +182,7 @@ func TestPullCleanUpAfterMerge(t *testing.T) { +@@ -180,7 +180,7 @@ func TestPullCleanUpAfterMerge(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { session := loginUser(t, "user1") testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "") @@ -2013,7 +1981,7 @@ index 1521fcfe8a..9e0e89fe9a 100644 resp := testPullCreate(t, session, "user1", "repo1", false, "master", "feature/test", "This is a pull title") -@@ -236,8 +236,8 @@ func TestCantMergeConflict(t *testing.T) { +@@ -234,8 +234,8 @@ func TestCantMergeConflict(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { session := loginUser(t, "user1") testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "") @@ -2024,7 +1992,7 @@ index 1521fcfe8a..9e0e89fe9a 100644 // Use API to create a conflicting pr token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) -@@ -282,7 +282,7 @@ func TestCantMergeUnrelated(t *testing.T) { +@@ -280,7 +280,7 @@ func TestCantMergeUnrelated(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { session := loginUser(t, "user1") testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "") @@ -2033,8 +2001,8 @@ index 1521fcfe8a..9e0e89fe9a 100644 // Now we want to create a commit on a branch that is totally unrelated to our current head // Drop down to pure code at this point -@@ -345,7 +345,7 @@ func TestCantMergeUnrelated(t *testing.T) { - _, _, err = git.NewCommand(git.DefaultContext, "branch", "unrelated").AddDynamicArguments(commitSha).RunStdString(&git.RunOpts{Dir: path}) +@@ -343,7 +343,7 @@ func TestCantMergeUnrelated(t *testing.T) { + _, _, err = git.NewCommand("branch", "unrelated").AddDynamicArguments(commitSha).RunStdString(git.DefaultContext, &git.RunOpts{Dir: path}) assert.NoError(t, err) - testEditFileToNewBranch(t, session, "user1", "repo1", "master", "conflict", "README.md", "Hello, World (Edited Once)\n") @@ -2042,7 +2010,7 @@ index 1521fcfe8a..9e0e89fe9a 100644 // Use API to create a conflicting pr token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) -@@ -377,7 +377,7 @@ func TestFastForwardOnlyMerge(t *testing.T) { +@@ -375,7 +375,7 @@ func TestFastForwardOnlyMerge(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { session := loginUser(t, "user1") testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "") @@ -2051,7 +2019,7 @@ index 1521fcfe8a..9e0e89fe9a 100644 // Use API to create a pr from update to master token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) -@@ -418,7 +418,7 @@ func TestCantFastForwardOnlyMergeDiverging(t *testing.T) { +@@ -416,7 +416,7 @@ func TestCantFastForwardOnlyMergeDiverging(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { session := loginUser(t, "user1") testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "") @@ -2060,7 +2028,7 @@ index 1521fcfe8a..9e0e89fe9a 100644 testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World 2\n") // Use API to create a pr from diverging to update -@@ -540,9 +540,9 @@ func TestConflictChecking(t *testing.T) { +@@ -538,9 +538,9 @@ func TestConflictChecking(t *testing.T) { func TestPullRetargetChildOnBranchDelete(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { session := loginUser(t, "user1") @@ -2072,7 +2040,7 @@ index 1521fcfe8a..9e0e89fe9a 100644 respBasePR := testPullCreate(t, session, "user2", "repo1", true, "master", "base-pr", "Base Pull Request") elemBasePR := strings.Split(test.RedirectURL(respBasePR), "/") -@@ -575,8 +575,8 @@ func TestPullDontRetargetChildOnWrongRepo(t *testing.T) { +@@ -573,8 +573,8 @@ func TestPullDontRetargetChildOnWrongRepo(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { session := loginUser(t, "user1") testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "") @@ -2093,10 +2061,10 @@ index 1521fcfe8a..9e0e89fe9a 100644 respBasePR := testPullCreate(t, session, "user4", "repo1", false, "master", "base-pr", "Base Pull Request") elemBasePR := strings.Split(test.RedirectURL(respBasePR), "/") diff --git a/tests/integration/pull_review_test.go b/tests/integration/pull_review_test.go -index 878020625b..f648b272da 100644 +index 1121751c88..7ae0c65576 100644 --- a/tests/integration/pull_review_test.go +++ b/tests/integration/pull_review_test.go -@@ -246,7 +246,7 @@ func TestPullView_GivenApproveOrRejectReviewOnClosedPR(t *testing.T) { +@@ -228,7 +228,7 @@ func TestPullView_GivenApproveOrRejectReviewOnClosedPR(t *testing.T) { t.Run("Submit approve/reject review on closed PR", func(t *testing.T) { // Created a closed PR (made by user1) in the upstream repo1. @@ -2104,12 +2072,12 @@ index 878020625b..f648b272da 100644 + testEditFileToNewBranch(t, user1Session, "user1", "user1", "repo1", "master", "a-test-branch", "README.md", "Hello, World (Editied...again)\n") resp := testPullCreate(t, user1Session, "user1", "repo1", false, "master", "a-test-branch", "This is a pull title") elem := strings.Split(test.RedirectURL(resp), "/") - assert.EqualValues(t, "pulls", elem[3]) + assert.Equal(t, "pulls", elem[3]) diff --git a/tests/integration/pull_status_test.go b/tests/integration/pull_status_test.go -index ac9036ca96..c0e7532cfd 100644 +index 4d43847f1b..a659c4e502 100644 --- a/tests/integration/pull_status_test.go +++ b/tests/integration/pull_status_test.go -@@ -24,7 +24,7 @@ func TestPullCreate_CommitStatus(t *testing.T) { +@@ -28,7 +28,7 @@ func TestPullCreate_CommitStatus(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { session := loginUser(t, "user1") testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "") @@ -2118,7 +2086,7 @@ index ac9036ca96..c0e7532cfd 100644 url := path.Join("user1", "repo1", "compare", "master...status1") req := NewRequestWithValues(t, "POST", url, -@@ -123,8 +123,8 @@ func TestPullCreate_EmptyChangesWithDifferentCommits(t *testing.T) { +@@ -127,8 +127,8 @@ func TestPullCreate_EmptyChangesWithDifferentCommits(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { session := loginUser(t, "user1") testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "") @@ -2130,11 +2098,11 @@ index ac9036ca96..c0e7532cfd 100644 url := path.Join("user1", "repo1", "compare", "master...status1") req := NewRequestWithValues(t, "POST", url, diff --git a/tests/integration/repo_activity_test.go b/tests/integration/repo_activity_test.go -index e520a0ed56..8a7cf04ce7 100644 +index d5025decba..85110e064c 100644 --- a/tests/integration/repo_activity_test.go +++ b/tests/integration/repo_activity_test.go -@@ -30,10 +30,10 @@ func TestRepoActivity(t *testing.T) { - assert.EqualValues(t, "pulls", elem[3]) +@@ -29,10 +29,10 @@ func TestRepoActivity(t *testing.T) { + assert.Equal(t, "pulls", elem[3]) testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleMerge, false) - testEditFileToNewBranch(t, session, "user1", "repo1", "master", "feat/better_readme", "README.md", "Hello, World (Edited Again)\n") @@ -2147,10 +2115,10 @@ index e520a0ed56..8a7cf04ce7 100644 // Create issues (3 new issues) diff --git a/tests/integration/repo_webhook_test.go b/tests/integration/repo_webhook_test.go -index 0952263968..a31c5f67f7 100644 +index 3ccc34f20f..ac742ab172 100644 --- a/tests/integration/repo_webhook_test.go +++ b/tests/integration/repo_webhook_test.go -@@ -308,7 +308,7 @@ func Test_WebhookPush(t *testing.T) { +@@ -359,7 +359,7 @@ func Test_WebhookPush(t *testing.T) { testAPICreateWebhookForRepo(t, session, "user2", "repo1", provider.URL(), "push") // 2. trigger the webhook @@ -2158,8 +2126,8 @@ index 0952263968..a31c5f67f7 100644 + testCreateFile(t, session, "user2", "user2", "repo1", "master", "master", "direct", "test_webhook_push.md", "# a test file for webhook push", "") // 3. validate the webhook is triggered - assert.EqualValues(t, "push", triggeredEvent) -@@ -601,7 +601,7 @@ func Test_WebhookStatus_NoWrongTrigger(t *testing.T) { + assert.Equal(t, "push", triggeredEvent) +@@ -762,7 +762,7 @@ func Test_WebhookStatus_NoWrongTrigger(t *testing.T) { testCreateWebhookForRepo(t, session, "gitea", "user2", "repo1", provider.URL(), "push_only") // 2. trigger the webhook with a push action @@ -2167,7 +2135,4 @@ index 0952263968..a31c5f67f7 100644 + testCreateFile(t, session, "user2", "user2", "repo1", "master", "master", "direct", "test_webhook_push.md", "# a test file for webhook push", "") // 3. validate the webhook is triggered with right event - assert.EqualValues(t, "push", trigger) --- -2.49.0 - + assert.Equal(t, "push", trigger)