2017-10-15 15:59:24 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2024-01-13 17:07:12 -05:00
|
|
|
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-10-15 15:59:24 -04:00
|
|
|
|
2022-09-02 15:18:23 -04:00
|
|
|
package integration
|
2017-10-15 15:59:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-05-11 11:29:17 -04:00
|
|
|
"net/url"
|
2017-10-15 15:59:24 -04:00
|
|
|
"path"
|
2024-01-14 15:44:26 -05:00
|
|
|
"strconv"
|
2017-10-15 15:59:24 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2024-01-13 17:07:12 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2023-12-17 16:49:58 -05:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2024-01-13 17:07:12 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2023-12-17 16:49:58 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2024-01-14 15:44:26 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-01-13 17:07:12 -05:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2024-01-14 15:44:26 -05:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2021-07-08 09:57:24 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-12-15 16:11:02 -05:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2022-06-26 10:19:22 -04:00
|
|
|
"code.gitea.io/gitea/modules/translation"
|
2024-01-14 15:44:26 -05:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2022-09-02 15:18:23 -04:00
|
|
|
"code.gitea.io/gitea/tests"
|
2017-12-15 16:11:02 -05:00
|
|
|
|
2017-10-15 15:59:24 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-12-23 19:33:34 -05:00
|
|
|
func testCreateBranch(t testing.TB, session *TestSession, user, repo, oldRefSubURL, newBranchName string, expectedStatus int) string {
|
2017-10-15 15:59:24 -04:00
|
|
|
var csrf string
|
|
|
|
if expectedStatus == http.StatusNotFound {
|
2017-10-29 22:04:25 -04:00
|
|
|
csrf = GetCSRF(t, session, path.Join(user, repo, "src/branch/master"))
|
2017-10-15 15:59:24 -04:00
|
|
|
} else {
|
2017-10-29 22:04:25 -04:00
|
|
|
csrf = GetCSRF(t, session, path.Join(user, repo, "src", oldRefSubURL))
|
2017-10-15 15:59:24 -04:00
|
|
|
}
|
2017-10-29 22:04:25 -04:00
|
|
|
req := NewRequestWithValues(t, "POST", path.Join(user, repo, "branches/_new", oldRefSubURL), map[string]string{
|
2017-10-15 15:59:24 -04:00
|
|
|
"_csrf": csrf,
|
|
|
|
"new_branch_name": newBranchName,
|
|
|
|
})
|
|
|
|
resp := session.MakeRequest(t, req, expectedStatus)
|
2022-03-23 00:54:07 -04:00
|
|
|
if expectedStatus != http.StatusSeeOther {
|
2017-10-15 15:59:24 -04:00
|
|
|
return ""
|
|
|
|
}
|
2017-12-15 16:11:02 -05:00
|
|
|
return test.RedirectURL(resp)
|
2017-10-15 15:59:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateBranch(t *testing.T) {
|
2019-05-11 11:29:17 -04:00
|
|
|
onGiteaRun(t, testCreateBranches)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCreateBranches(t *testing.T, giteaURL *url.URL) {
|
2017-10-15 15:59:24 -04:00
|
|
|
tests := []struct {
|
2017-10-29 22:04:25 -04:00
|
|
|
OldRefSubURL string
|
|
|
|
NewBranch string
|
|
|
|
CreateRelease string
|
|
|
|
FlashMessage string
|
|
|
|
ExpectedStatus int
|
2023-12-17 16:49:58 -05:00
|
|
|
CheckBranch bool
|
2017-10-15 15:59:24 -04:00
|
|
|
}{
|
|
|
|
{
|
2017-10-29 22:04:25 -04:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "feature/test1",
|
2022-03-23 00:54:07 -04:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2022-06-26 10:19:22 -04:00
|
|
|
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.create_success", "feature/test1"),
|
2023-12-17 16:49:58 -05:00
|
|
|
CheckBranch: true,
|
2017-10-15 15:59:24 -04:00
|
|
|
},
|
|
|
|
{
|
2017-10-29 22:04:25 -04:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "",
|
2022-03-23 00:54:07 -04:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2022-06-26 10:19:22 -04:00
|
|
|
FlashMessage: translation.NewLocale("en-US").Tr("form.NewBranchName") + translation.NewLocale("en-US").Tr("form.require_error"),
|
2017-10-15 15:59:24 -04:00
|
|
|
},
|
|
|
|
{
|
2017-10-29 22:04:25 -04:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "feature=test1",
|
2022-03-23 00:54:07 -04:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2022-06-26 10:19:22 -04:00
|
|
|
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.create_success", "feature=test1"),
|
2023-12-17 16:49:58 -05:00
|
|
|
CheckBranch: true,
|
2017-10-15 15:59:24 -04:00
|
|
|
},
|
|
|
|
{
|
2017-10-29 22:04:25 -04:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: strings.Repeat("b", 101),
|
2022-03-23 00:54:07 -04:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2022-06-26 10:19:22 -04:00
|
|
|
FlashMessage: translation.NewLocale("en-US").Tr("form.NewBranchName") + translation.NewLocale("en-US").Tr("form.max_size_error", "100"),
|
2017-10-15 15:59:24 -04:00
|
|
|
},
|
|
|
|
{
|
2017-10-29 22:04:25 -04:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "master",
|
2022-03-23 00:54:07 -04:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2022-06-26 10:19:22 -04:00
|
|
|
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.branch_already_exists", "master"),
|
2017-10-15 15:59:24 -04:00
|
|
|
},
|
|
|
|
{
|
2017-10-29 22:04:25 -04:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "master/test",
|
2022-03-23 00:54:07 -04:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2022-06-26 10:19:22 -04:00
|
|
|
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.branch_name_conflict", "master/test", "master"),
|
2017-10-15 15:59:24 -04:00
|
|
|
},
|
|
|
|
{
|
2017-10-29 22:04:25 -04:00
|
|
|
OldRefSubURL: "commit/acd1d892867872cb47f3993468605b8aa59aa2e0",
|
|
|
|
NewBranch: "feature/test2",
|
|
|
|
ExpectedStatus: http.StatusNotFound,
|
2017-10-15 15:59:24 -04:00
|
|
|
},
|
|
|
|
{
|
2017-10-29 22:04:25 -04:00
|
|
|
OldRefSubURL: "commit/65f1bf27bc3bf70f64657658635e66094edbcb4d",
|
|
|
|
NewBranch: "feature/test3",
|
2022-03-23 00:54:07 -04:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2022-06-26 10:19:22 -04:00
|
|
|
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.create_success", "feature/test3"),
|
2023-12-17 16:49:58 -05:00
|
|
|
CheckBranch: true,
|
2017-10-15 15:59:24 -04:00
|
|
|
},
|
|
|
|
{
|
2017-10-29 22:04:25 -04:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "v1.0.0",
|
|
|
|
CreateRelease: "v1.0.0",
|
2022-03-23 00:54:07 -04:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2022-06-26 10:19:22 -04:00
|
|
|
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.tag_collision", "v1.0.0"),
|
2017-10-15 15:59:24 -04:00
|
|
|
},
|
|
|
|
{
|
2017-10-29 22:04:25 -04:00
|
|
|
OldRefSubURL: "tag/v1.0.0",
|
|
|
|
NewBranch: "feature/test4",
|
2020-11-08 16:24:54 -05:00
|
|
|
CreateRelease: "v1.0.1",
|
2022-03-23 00:54:07 -04:00
|
|
|
ExpectedStatus: http.StatusSeeOther,
|
2022-06-26 10:19:22 -04:00
|
|
|
FlashMessage: translation.NewLocale("en-US").Tr("repo.branch.create_success", "feature/test4"),
|
2023-12-17 16:49:58 -05:00
|
|
|
CheckBranch: true,
|
2017-10-15 15:59:24 -04:00
|
|
|
},
|
|
|
|
}
|
2023-12-17 16:49:58 -05:00
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
2017-10-15 15:59:24 -04:00
|
|
|
for _, test := range tests {
|
2023-12-17 16:49:58 -05:00
|
|
|
if test.CheckBranch {
|
|
|
|
unittest.AssertNotExistsBean(t, &git_model.Branch{RepoID: 1, Name: test.NewBranch})
|
|
|
|
}
|
2017-10-15 15:59:24 -04:00
|
|
|
if test.CreateRelease != "" {
|
|
|
|
createNewRelease(t, session, "/user2/repo1", test.CreateRelease, test.CreateRelease, false, false)
|
|
|
|
}
|
2017-10-29 22:04:25 -04:00
|
|
|
redirectURL := testCreateBranch(t, session, "user2", "repo1", test.OldRefSubURL, test.NewBranch, test.ExpectedStatus)
|
2022-03-23 00:54:07 -04:00
|
|
|
if test.ExpectedStatus == http.StatusSeeOther {
|
2017-10-15 15:59:24 -04:00
|
|
|
req := NewRequest(t, "GET", redirectURL)
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
2023-07-07 23:19:00 -04:00
|
|
|
assert.Contains(t,
|
2017-10-15 15:59:24 -04:00
|
|
|
strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
|
2023-07-07 23:19:00 -04:00
|
|
|
test.FlashMessage,
|
2017-10-15 15:59:24 -04:00
|
|
|
)
|
|
|
|
}
|
2023-12-17 16:49:58 -05:00
|
|
|
if test.CheckBranch {
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: 1, Name: test.NewBranch})
|
|
|
|
}
|
2017-10-15 15:59:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateBranchInvalidCSRF(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2017-10-15 15:59:24 -04:00
|
|
|
session := loginUser(t, "user2")
|
2017-10-29 22:04:25 -04:00
|
|
|
req := NewRequestWithValues(t, "POST", "user2/repo1/branches/_new/branch/master", map[string]string{
|
2017-10-15 15:59:24 -04:00
|
|
|
"_csrf": "fake_csrf",
|
|
|
|
"new_branch_name": "test",
|
|
|
|
})
|
2022-03-23 00:54:07 -04:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusSeeOther)
|
2021-07-08 09:57:24 -04:00
|
|
|
loc := resp.Header().Get("Location")
|
|
|
|
assert.Equal(t, setting.AppSubURL+"/", loc)
|
|
|
|
resp = session.MakeRequest(t, NewRequest(t, "GET", loc), http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
assert.Equal(t,
|
2021-12-28 08:28:27 -05:00
|
|
|
"Bad Request: invalid CSRF token",
|
2021-07-08 09:57:24 -04:00
|
|
|
strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
|
|
|
|
)
|
2017-10-15 15:59:24 -04:00
|
|
|
}
|
2024-01-13 17:07:12 -05:00
|
|
|
|
|
|
|
func TestDatabaseMissingABranch(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(t *testing.T, URL *url.URL) {
|
2024-01-14 15:44:26 -05:00
|
|
|
adminUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{IsAdmin: true})
|
2024-01-13 17:07:12 -05:00
|
|
|
session := loginUser(t, "user2")
|
|
|
|
|
|
|
|
// Create two branches
|
|
|
|
testCreateBranch(t, session, "user2", "repo1", "branch/master", "will-be-present", http.StatusSeeOther)
|
|
|
|
testCreateBranch(t, session, "user2", "repo1", "branch/master", "will-be-missing", http.StatusSeeOther)
|
|
|
|
|
2024-01-14 15:44:26 -05:00
|
|
|
// Run the repo branch sync, to ensure the db and git agree.
|
|
|
|
err2 := repo_service.AddAllRepoBranchesToSyncQueue(graceful.GetManager().ShutdownContext(), adminUser.ID)
|
|
|
|
assert.NoError(t, err2)
|
|
|
|
|
2024-01-13 17:07:12 -05:00
|
|
|
// Delete one branch from git only, leaving it in the database
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
cmd := git.NewCommand(db.DefaultContext, "branch", "-D").AddDynamicArguments("will-be-missing")
|
|
|
|
_, _, err := cmd.RunStdString(&git.RunOpts{Dir: repo.RepoPath()})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-01-14 15:44:26 -05:00
|
|
|
// Verify that loading the repo's branches page works still, and that it
|
|
|
|
// reports at least three branches (master, will-be-present, and
|
|
|
|
// will-be-missing).
|
2024-01-13 17:07:12 -05:00
|
|
|
req := NewRequest(t, "GET", "/user2/repo1/branches")
|
2024-01-14 15:44:26 -05:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
doc := NewHTMLParser(t, resp.Body)
|
|
|
|
firstBranchCount, _ := strconv.Atoi(doc.Find(".repository-menu a[href*='/branches'] b").Text())
|
|
|
|
assert.GreaterOrEqual(t, firstBranchCount, 3)
|
|
|
|
|
|
|
|
// Run the repo branch sync again
|
|
|
|
err2 = repo_service.AddAllRepoBranchesToSyncQueue(graceful.GetManager().ShutdownContext(), adminUser.ID)
|
|
|
|
assert.NoError(t, err2)
|
|
|
|
|
|
|
|
// Verify that loading the repo's branches page works still, and that it
|
|
|
|
// reports one branch less than the first time.
|
|
|
|
//
|
|
|
|
// NOTE: This assumes that the branch counter on the web UI is out of
|
|
|
|
// date before the sync. If that problem gets resolved, we'll have to
|
|
|
|
// find another way to test that the syncing works.
|
|
|
|
req = NewRequest(t, "GET", "/user2/repo1/branches")
|
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
doc = NewHTMLParser(t, resp.Body)
|
|
|
|
secondBranchCount, _ := strconv.Atoi(doc.Find(".repository-menu a[href*='/branches'] b").Text())
|
|
|
|
assert.Equal(t, firstBranchCount-1, secondBranchCount)
|
2024-01-13 17:07:12 -05:00
|
|
|
})
|
|
|
|
}
|