test: hook post-receive for sha256 repos

failing push-to-create for sha256 will be fixed in a followup PR
This commit is contained in:
oliverpool 2024-05-15 15:35:19 +02:00
parent fe3473fc8b
commit 2ac3dcbd43
4 changed files with 152 additions and 136 deletions

View file

@ -117,10 +117,10 @@ func doGitCloneFail(u *url.URL) func(*testing.T) {
} }
} }
func doGitInitTestRepository(dstPath string) func(*testing.T) { func doGitInitTestRepository(dstPath string, objectFormat git.ObjectFormat) func(*testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
// Init repository in dstPath // Init repository in dstPath
assert.NoError(t, git.InitRepository(git.DefaultContext, dstPath, false, git.Sha1ObjectFormat.Name())) assert.NoError(t, git.InitRepository(git.DefaultContext, dstPath, false, objectFormat.Name()))
// forcibly set default branch to master // forcibly set default branch to master
_, _, err := git.NewCommand(git.DefaultContext, "symbolic-ref", "HEAD", git.BranchPrefix+"master").RunStdString(&git.RunOpts{Dir: dstPath}) _, _, err := git.NewCommand(git.DefaultContext, "symbolic-ref", "HEAD", git.BranchPrefix+"master").RunStdString(&git.RunOpts{Dir: dstPath})
assert.NoError(t, err) assert.NoError(t, err)
@ -148,6 +148,7 @@ func doGitAddRemote(dstPath, remoteName string, u *url.URL) func(*testing.T) {
func doGitPushTestRepository(dstPath string, args ...string) func(*testing.T) { func doGitPushTestRepository(dstPath string, args ...string) func(*testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
t.Helper()
_, _, err := git.NewCommand(git.DefaultContext, "push", "-u").AddArguments(git.ToTrustedCmdArgs(args)...).RunStdString(&git.RunOpts{Dir: dstPath}) _, _, err := git.NewCommand(git.DefaultContext, "push", "-u").AddArguments(git.ToTrustedCmdArgs(args)...).RunStdString(&git.RunOpts{Dir: dstPath})
assert.NoError(t, err) assert.NoError(t, err)
} }

View file

@ -24,90 +24,101 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func forEachObjectFormat(t *testing.T, f func(t *testing.T, objectFormat git.ObjectFormat)) {
for _, objectFormat := range []git.ObjectFormat{git.Sha256ObjectFormat, git.Sha1ObjectFormat} {
t.Run(objectFormat.Name(), func(t *testing.T) {
f(t, objectFormat)
})
}
}
func TestGitPush(t *testing.T) { func TestGitPush(t *testing.T) {
onGiteaRun(t, testGitPush) onGiteaRun(t, testGitPush)
} }
func testGitPush(t *testing.T, u *url.URL) { func testGitPush(t *testing.T, u *url.URL) {
t.Run("Push branches at once", func(t *testing.T) { forEachObjectFormat(t, func(t *testing.T, objectFormat git.ObjectFormat) {
runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) { t.Run("Push branches at once", func(t *testing.T) {
for i := 0; i < 100; i++ { runTestGitPush(t, u, objectFormat, func(t *testing.T, gitPath string) (pushed, deleted []string) {
branchName := fmt.Sprintf("branch-%d", i) for i := 0; i < 100; i++ {
pushed = append(pushed, branchName) branchName := fmt.Sprintf("branch-%d", i)
doGitCreateBranch(gitPath, branchName)(t) pushed = append(pushed, branchName)
} doGitCreateBranch(gitPath, branchName)(t)
pushed = append(pushed, "master") }
doGitPushTestRepository(gitPath, "origin", "--all")(t) pushed = append(pushed, "master")
return pushed, deleted doGitPushTestRepository(gitPath, "origin", "--all")(t)
return pushed, deleted
})
}) })
})
t.Run("Push branches one by one", func(t *testing.T) { t.Run("Push branches one by one", func(t *testing.T) {
runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) { runTestGitPush(t, u, objectFormat, func(t *testing.T, gitPath string) (pushed, deleted []string) {
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
branchName := fmt.Sprintf("branch-%d", i) branchName := fmt.Sprintf("branch-%d", i)
doGitCreateBranch(gitPath, branchName)(t) doGitCreateBranch(gitPath, branchName)(t)
doGitPushTestRepository(gitPath, "origin", branchName)(t) doGitPushTestRepository(gitPath, "origin", branchName)(t)
pushed = append(pushed, branchName) pushed = append(pushed, branchName)
} }
return pushed, deleted return pushed, deleted
})
}) })
})
t.Run("Delete branches", func(t *testing.T) { t.Run("Delete branches", func(t *testing.T) {
runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) { runTestGitPush(t, u, objectFormat, func(t *testing.T, gitPath string) (pushed, deleted []string) {
doGitPushTestRepository(gitPath, "origin", "master")(t) // make sure master is the default branch instead of a branch we are going to delete doGitPushTestRepository(gitPath, "origin", "master")(t) // make sure master is the default branch instead of a branch we are going to delete
pushed = append(pushed, "master") pushed = append(pushed, "master")
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
branchName := fmt.Sprintf("branch-%d", i) branchName := fmt.Sprintf("branch-%d", i)
pushed = append(pushed, branchName) pushed = append(pushed, branchName)
doGitCreateBranch(gitPath, branchName)(t) doGitCreateBranch(gitPath, branchName)(t)
} }
doGitPushTestRepository(gitPath, "origin", "--all")(t) doGitPushTestRepository(gitPath, "origin", "--all")(t)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
branchName := fmt.Sprintf("branch-%d", i) branchName := fmt.Sprintf("branch-%d", i)
doGitPushTestRepository(gitPath, "origin", "--delete", branchName)(t) doGitPushTestRepository(gitPath, "origin", "--delete", branchName)(t)
deleted = append(deleted, branchName) deleted = append(deleted, branchName)
} }
return pushed, deleted return pushed, deleted
})
}) })
})
t.Run("Push to deleted branch", func(t *testing.T) { t.Run("Push to deleted branch", func(t *testing.T) {
runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) { runTestGitPush(t, u, objectFormat, func(t *testing.T, gitPath string) (pushed, deleted []string) {
doGitPushTestRepository(gitPath, "origin", "master")(t) // make sure master is the default branch instead of a branch we are going to delete doGitPushTestRepository(gitPath, "origin", "master")(t) // make sure master is the default branch instead of a branch we are going to delete
pushed = append(pushed, "master") pushed = append(pushed, "master")
doGitCreateBranch(gitPath, "branch-1")(t) doGitCreateBranch(gitPath, "branch-1")(t)
doGitPushTestRepository(gitPath, "origin", "branch-1")(t) doGitPushTestRepository(gitPath, "origin", "branch-1")(t)
pushed = append(pushed, "branch-1") pushed = append(pushed, "branch-1")
// delete and restore // delete and restore
doGitPushTestRepository(gitPath, "origin", "--delete", "branch-1")(t) doGitPushTestRepository(gitPath, "origin", "--delete", "branch-1")(t)
doGitPushTestRepository(gitPath, "origin", "branch-1")(t) doGitPushTestRepository(gitPath, "origin", "branch-1")(t)
return pushed, deleted return pushed, deleted
})
}) })
}) })
} }
func runTestGitPush(t *testing.T, u *url.URL, gitOperation func(t *testing.T, gitPath string) (pushed, deleted []string)) { func runTestGitPush(t *testing.T, u *url.URL, objectFormat git.ObjectFormat, gitOperation func(t *testing.T, gitPath string) (pushed, deleted []string)) {
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{ repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
Name: "repo-to-push", Name: "repo-to-push",
Description: "test git push", Description: "test git push",
AutoInit: false, AutoInit: false,
DefaultBranch: "main", DefaultBranch: "main",
IsPrivate: false, IsPrivate: false,
ObjectFormatName: objectFormat.Name(),
}) })
require.NoError(t, err) require.NoError(t, err)
require.NotEmpty(t, repo) require.NotEmpty(t, repo)
gitPath := t.TempDir() gitPath := t.TempDir()
doGitInitTestRepository(gitPath)(t) doGitInitTestRepository(gitPath, objectFormat)(t)
oldPath := u.Path oldPath := u.Path
oldUser := u.User oldUser := u.User
@ -158,83 +169,87 @@ func TestOptionsGitPush(t *testing.T) {
func testOptionsGitPush(t *testing.T, u *url.URL) { func testOptionsGitPush(t *testing.T, u *url.URL) {
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
Name: "repo-to-push",
Description: "test git push",
AutoInit: false,
DefaultBranch: "main",
IsPrivate: false,
})
require.NoError(t, err)
require.NotEmpty(t, repo)
gitPath := t.TempDir() forEachObjectFormat(t, func(t *testing.T, objectFormat git.ObjectFormat) {
repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
doGitInitTestRepository(gitPath)(t) Name: "repo-to-push",
Description: "test git push",
u.Path = repo.FullName() + ".git" AutoInit: false,
u.User = url.UserPassword(user.LowerName, userPassword) DefaultBranch: "main",
doGitAddRemote(gitPath, "origin", u)(t) IsPrivate: false,
ObjectFormatName: objectFormat.Name(),
t.Run("Unknown push options are rejected", func(t *testing.T) { })
logChecker, cleanup := test.NewLogChecker(log.DEFAULT, log.TRACE)
logChecker.Filter("unknown option").StopMark("Git push options validation")
defer cleanup()
branchName := "branch0"
doGitCreateBranch(gitPath, branchName)(t)
doGitPushTestRepositoryFail(gitPath, "origin", branchName, "-o", "repo.template=false", "-o", "uknownoption=randomvalue")(t)
logFiltered, logStopped := logChecker.Check(5 * time.Second)
assert.True(t, logStopped)
assert.True(t, logFiltered[0])
})
t.Run("Owner sets private & template to true via push options", func(t *testing.T) {
branchName := "branch1"
doGitCreateBranch(gitPath, branchName)(t)
doGitPushTestRepository(gitPath, "origin", branchName, "-o", "repo.private=true", "-o", "repo.template=true")(t)
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, user.Name, "repo-to-push")
require.NoError(t, err) require.NoError(t, err)
require.True(t, repo.IsPrivate) require.NotEmpty(t, repo)
require.True(t, repo.IsTemplate)
gitPath := t.TempDir()
doGitInitTestRepository(gitPath, objectFormat)(t)
u.Path = repo.FullName() + ".git"
u.User = url.UserPassword(user.LowerName, userPassword)
doGitAddRemote(gitPath, "origin", u)(t)
t.Run("Unknown push options are rejected", func(t *testing.T) {
logChecker, cleanup := test.NewLogChecker(log.DEFAULT, log.TRACE)
logChecker.Filter("unknown option").StopMark("Git push options validation")
defer cleanup()
branchName := "branch0"
doGitCreateBranch(gitPath, branchName)(t)
doGitPushTestRepositoryFail(gitPath, "origin", branchName, "-o", "repo.template=false", "-o", "uknownoption=randomvalue")(t)
logFiltered, logStopped := logChecker.Check(5 * time.Second)
assert.True(t, logStopped)
assert.True(t, logFiltered[0])
})
t.Run("Owner sets private & template to true via push options", func(t *testing.T) {
branchName := "branch1"
doGitCreateBranch(gitPath, branchName)(t)
doGitPushTestRepository(gitPath, "origin", branchName, "-o", "repo.private=true", "-o", "repo.template=true")(t)
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, user.Name, "repo-to-push")
require.NoError(t, err)
require.True(t, repo.IsPrivate)
require.True(t, repo.IsTemplate)
})
t.Run("Owner sets private & template to false via push options", func(t *testing.T) {
branchName := "branch2"
doGitCreateBranch(gitPath, branchName)(t)
doGitPushTestRepository(gitPath, "origin", branchName, "-o", "repo.private=false", "-o", "repo.template=false")(t)
repo, err = repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, user.Name, "repo-to-push")
require.NoError(t, err)
require.False(t, repo.IsPrivate)
require.False(t, repo.IsTemplate)
})
// create a collaborator with write access
collaborator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
u.User = url.UserPassword(collaborator.LowerName, userPassword)
doGitAddRemote(gitPath, "collaborator", u)(t)
repo_module.AddCollaborator(db.DefaultContext, repo, collaborator)
t.Run("Collaborator with write access is allowed to push", func(t *testing.T) {
branchName := "branch3"
doGitCreateBranch(gitPath, branchName)(t)
doGitPushTestRepository(gitPath, "collaborator", branchName)(t)
})
t.Run("Collaborator with write access fails to change private & template via push options", func(t *testing.T) {
logChecker, cleanup := test.NewLogChecker(log.DEFAULT, log.TRACE)
logChecker.Filter("permission denied for changing repo settings").StopMark("Git push options validation")
defer cleanup()
branchName := "branch4"
doGitCreateBranch(gitPath, branchName)(t)
doGitPushTestRepositoryFail(gitPath, "collaborator", branchName, "-o", "repo.private=true", "-o", "repo.template=true")(t)
repo, err = repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, user.Name, "repo-to-push")
require.NoError(t, err)
require.False(t, repo.IsPrivate)
require.False(t, repo.IsTemplate)
logFiltered, logStopped := logChecker.Check(5 * time.Second)
assert.True(t, logStopped)
assert.True(t, logFiltered[0])
})
require.NoError(t, repo_service.DeleteRepositoryDirectly(db.DefaultContext, user, repo.ID))
}) })
t.Run("Owner sets private & template to false via push options", func(t *testing.T) {
branchName := "branch2"
doGitCreateBranch(gitPath, branchName)(t)
doGitPushTestRepository(gitPath, "origin", branchName, "-o", "repo.private=false", "-o", "repo.template=false")(t)
repo, err = repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, user.Name, "repo-to-push")
require.NoError(t, err)
require.False(t, repo.IsPrivate)
require.False(t, repo.IsTemplate)
})
// create a collaborator with write access
collaborator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
u.User = url.UserPassword(collaborator.LowerName, userPassword)
doGitAddRemote(gitPath, "collaborator", u)(t)
repo_module.AddCollaborator(db.DefaultContext, repo, collaborator)
t.Run("Collaborator with write access is allowed to push", func(t *testing.T) {
branchName := "branch3"
doGitCreateBranch(gitPath, branchName)(t)
doGitPushTestRepository(gitPath, "collaborator", branchName)(t)
})
t.Run("Collaborator with write access fails to change private & template via push options", func(t *testing.T) {
logChecker, cleanup := test.NewLogChecker(log.DEFAULT, log.TRACE)
logChecker.Filter("permission denied for changing repo settings").StopMark("Git push options validation")
defer cleanup()
branchName := "branch4"
doGitCreateBranch(gitPath, branchName)(t)
doGitPushTestRepositoryFail(gitPath, "collaborator", branchName, "-o", "repo.private=true", "-o", "repo.template=true")(t)
repo, err = repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, user.Name, "repo-to-push")
require.NoError(t, err)
require.False(t, repo.IsPrivate)
require.False(t, repo.IsTemplate)
logFiltered, logStopped := logChecker.Check(5 * time.Second)
assert.True(t, logStopped)
assert.True(t, logFiltered[0])
})
require.NoError(t, repo_service.DeleteRepositoryDirectly(db.DefaultContext, user, repo.ID))
} }

View file

@ -598,7 +598,7 @@ func doPushCreate(ctx APITestContext, u *url.URL) func(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
// Now create local repository to push as our test and set its origin // Now create local repository to push as our test and set its origin
t.Run("InitTestRepository", doGitInitTestRepository(tmpDir)) t.Run("InitTestRepository", doGitInitTestRepository(tmpDir, git.Sha1ObjectFormat)) // FIXME: use forEachObjectFormat
t.Run("AddRemote", doGitAddRemote(tmpDir, "origin", u)) t.Run("AddRemote", doGitAddRemote(tmpDir, "origin", u))
// Disable "Push To Create" and attempt to push // Disable "Push To Create" and attempt to push

View file

@ -64,7 +64,7 @@ func testPushDeployKeyOnEmptyRepo(t *testing.T, u *url.URL) {
// Setup the testing repository // Setup the testing repository
dstPath := t.TempDir() dstPath := t.TempDir()
t.Run("InitTestRepository", doGitInitTestRepository(dstPath)) t.Run("InitTestRepository", doGitInitTestRepository(dstPath, git.Sha1ObjectFormat)) // FIXME: use forEachObjectFormat
// Setup remote link // Setup remote link
sshURL := createSSHUrl(ctx.GitPath(), u) sshURL := createSSHUrl(ctx.GitPath(), u)