2017-10-15 21:59:24 +02:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package integrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-05-11 17:29:17 +02:00
|
|
|
"net/url"
|
2017-10-15 21:59:24 +02:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-07-08 15:57:24 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-12-15 22:11:02 +01:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
|
|
|
|
2017-10-15 21:59:24 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-08-23 18:40:30 +02:00
|
|
|
"github.com/unknwon/i18n"
|
2017-10-15 21:59:24 +02:00
|
|
|
)
|
|
|
|
|
2017-12-24 01:33:34 +01:00
|
|
|
func testCreateBranch(t testing.TB, session *TestSession, user, repo, oldRefSubURL, newBranchName string, expectedStatus int) string {
|
2017-10-15 21:59:24 +02:00
|
|
|
var csrf string
|
|
|
|
if expectedStatus == http.StatusNotFound {
|
2017-10-30 03:04:25 +01:00
|
|
|
csrf = GetCSRF(t, session, path.Join(user, repo, "src/branch/master"))
|
2017-10-15 21:59:24 +02:00
|
|
|
} else {
|
2017-10-30 03:04:25 +01:00
|
|
|
csrf = GetCSRF(t, session, path.Join(user, repo, "src", oldRefSubURL))
|
2017-10-15 21:59:24 +02:00
|
|
|
}
|
2017-10-30 03:04:25 +01:00
|
|
|
req := NewRequestWithValues(t, "POST", path.Join(user, repo, "branches/_new", oldRefSubURL), map[string]string{
|
2017-10-15 21:59:24 +02:00
|
|
|
"_csrf": csrf,
|
|
|
|
"new_branch_name": newBranchName,
|
|
|
|
})
|
|
|
|
resp := session.MakeRequest(t, req, expectedStatus)
|
|
|
|
if expectedStatus != http.StatusFound {
|
|
|
|
return ""
|
|
|
|
}
|
2017-12-15 22:11:02 +01:00
|
|
|
return test.RedirectURL(resp)
|
2017-10-15 21:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateBranch(t *testing.T) {
|
2019-05-11 17:29:17 +02:00
|
|
|
onGiteaRun(t, testCreateBranches)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCreateBranches(t *testing.T, giteaURL *url.URL) {
|
2017-10-15 21:59:24 +02:00
|
|
|
tests := []struct {
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL string
|
|
|
|
NewBranch string
|
|
|
|
CreateRelease string
|
|
|
|
FlashMessage string
|
|
|
|
ExpectedStatus int
|
2017-10-15 21:59:24 +02:00
|
|
|
}{
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "feature/test1",
|
|
|
|
ExpectedStatus: http.StatusFound,
|
|
|
|
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test1"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "",
|
|
|
|
ExpectedStatus: http.StatusFound,
|
|
|
|
FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.require_error"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "feature=test1",
|
|
|
|
ExpectedStatus: http.StatusFound,
|
2019-03-26 20:59:48 +01:00
|
|
|
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature=test1"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: strings.Repeat("b", 101),
|
|
|
|
ExpectedStatus: http.StatusFound,
|
|
|
|
FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.max_size_error", "100"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "master",
|
|
|
|
ExpectedStatus: http.StatusFound,
|
|
|
|
FlashMessage: i18n.Tr("en", "repo.branch.branch_already_exists", "master"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "master/test",
|
|
|
|
ExpectedStatus: http.StatusFound,
|
|
|
|
FlashMessage: i18n.Tr("en", "repo.branch.branch_name_conflict", "master/test", "master"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "commit/acd1d892867872cb47f3993468605b8aa59aa2e0",
|
|
|
|
NewBranch: "feature/test2",
|
|
|
|
ExpectedStatus: http.StatusNotFound,
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "commit/65f1bf27bc3bf70f64657658635e66094edbcb4d",
|
|
|
|
NewBranch: "feature/test3",
|
|
|
|
ExpectedStatus: http.StatusFound,
|
|
|
|
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test3"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "branch/master",
|
|
|
|
NewBranch: "v1.0.0",
|
|
|
|
CreateRelease: "v1.0.0",
|
|
|
|
ExpectedStatus: http.StatusFound,
|
|
|
|
FlashMessage: i18n.Tr("en", "repo.branch.tag_collision", "v1.0.0"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 03:04:25 +01:00
|
|
|
OldRefSubURL: "tag/v1.0.0",
|
|
|
|
NewBranch: "feature/test4",
|
2020-11-08 22:24:54 +01:00
|
|
|
CreateRelease: "v1.0.1",
|
2017-10-30 03:04:25 +01:00
|
|
|
ExpectedStatus: http.StatusFound,
|
|
|
|
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test4"),
|
2017-10-15 21:59:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
if test.CreateRelease != "" {
|
|
|
|
createNewRelease(t, session, "/user2/repo1", test.CreateRelease, test.CreateRelease, false, false)
|
|
|
|
}
|
2017-10-30 03:04:25 +01:00
|
|
|
redirectURL := testCreateBranch(t, session, "user2", "repo1", test.OldRefSubURL, test.NewBranch, test.ExpectedStatus)
|
2017-10-15 21:59:24 +02:00
|
|
|
if test.ExpectedStatus == http.StatusFound {
|
|
|
|
req := NewRequest(t, "GET", redirectURL)
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
assert.Equal(t,
|
|
|
|
test.FlashMessage,
|
|
|
|
strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateBranchInvalidCSRF(t *testing.T) {
|
2019-11-26 00:21:37 +01:00
|
|
|
defer prepareTestEnv(t)()
|
2017-10-15 21:59:24 +02:00
|
|
|
session := loginUser(t, "user2")
|
2017-10-30 03:04:25 +01:00
|
|
|
req := NewRequestWithValues(t, "POST", "user2/repo1/branches/_new/branch/master", map[string]string{
|
2017-10-15 21:59:24 +02:00
|
|
|
"_csrf": "fake_csrf",
|
|
|
|
"new_branch_name": "test",
|
|
|
|
})
|
2021-07-08 15:57:24 +02:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusFound)
|
|
|
|
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 14:28:27 +01:00
|
|
|
"Bad Request: invalid CSRF token",
|
2021-07-08 15:57:24 +02:00
|
|
|
strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
|
|
|
|
)
|
2017-10-15 21:59:24 +02:00
|
|
|
}
|