[FEAT] Trim spaces from repository name

- This uses the `TrimSpace` preprocessing of the binding library to
remove any accidental spaces from the input.
- Integration test added.
- Resolves #4309
This commit is contained in:
Gusted 2024-11-05 22:05:35 +01:00
parent 310376525b
commit 4952747699
No known key found for this signature in database
GPG key ID: FD821B732837125F
2 changed files with 19 additions and 1 deletions

View file

@ -27,7 +27,7 @@ import (
// CreateRepoForm form for creating repository // CreateRepoForm form for creating repository
type CreateRepoForm struct { type CreateRepoForm struct {
UID int64 `binding:"Required"` UID int64 `binding:"Required"`
RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"` RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)" preprocess:"TrimSpace"`
Private bool Private bool
Description string `binding:"MaxSize(2048)"` Description string `binding:"MaxSize(2048)"`
DefaultBranch string `binding:"GitRefName;MaxSize(100)"` DefaultBranch string `binding:"GitRefName;MaxSize(100)"`

View file

@ -11,9 +11,11 @@ import (
"strings" "strings"
"testing" "testing"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/translation" "code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/tests" "code.gitea.io/gitea/tests"
@ -135,3 +137,19 @@ func TestRepoGenerateToOrg(t *testing.T) {
testRepoGenerate(t, session, "44", "user27", "template1", user, org, "generated2") testRepoGenerate(t, session, "44", "user27", "template1", user, org, "generated2")
} }
func TestRepoCreateFormTrimSpace(t *testing.T) {
defer tests.PrepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
session := loginUser(t, user.Name)
req := NewRequestWithValues(t, "POST", "/repo/create", map[string]string{
"_csrf": GetCSRF(t, session, "/repo/create"),
"uid": "2",
"repo_name": " spaced-name ",
})
resp := session.MakeRequest(t, req, http.StatusSeeOther)
assert.EqualValues(t, "/user2/spaced-name", test.RedirectURL(resp))
unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 2, Name: "spaced-name"})
}