1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-30 14:09:42 -05:00
forgejo/modules/git/pushoptions/pushoptions_test.go
Twenty Panda 5561e80b04 fix(hook): ignore unknown push options instead of failing
If a repository has

git config --add push.pushOption submit=".sourcehut/*.yml"

it failed when pushed because of the unknown submit push
option. It will be ignored instead.

Filtering out the push options is done in an earlier stage, when the
hook command runs, before it submits the options map to the private
endpoint.

* move all the push options logic to modules/git/pushoptions
* add 100% test coverage for modules/git/pushoptions

Test coverage for the code paths from which code was moved to the
modules/git/pushoptions package:

* cmd/hook.go:runHookPreReceive
* routers/private/hook_pre_receive.go:validatePushOptions
  tests/integration/git_push_test.go:TestOptionsGitPush runs through
  both. The test verifying the option is rejected was removed and, if
  added again, will fail because the option is now ignored instead of
  being rejected.

* cmd/hook.go:runHookProcReceive
* services/agit/agit.go:ProcReceive
  tests/integration/git_test.go: doCreateAgitFlowPull runs through
  both. It uses variations of AGit related push options.

* cmd/hook.go:runHookPostReceive
* routers/private/hook_post_receive.go:HookPostReceive
  tests/integration/git_test.go:doPushCreate called by TestGit/HTTP/sha1/PushCreate
  runs through both.
  Note that although it provides coverage for this code path it does not use push options.

Fixes: https://codeberg.org/forgejo/forgejo/issues/3651
2024-07-02 21:39:01 +02:00

125 lines
3.3 KiB
Go

// Copyright twenty-panda <twenty-panda@posteo.com>
// SPDX-License-Identifier: MIT
package pushoptions
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEmpty(t *testing.T) {
options := New()
assert.True(t, options.Empty())
options.Parse(fmt.Sprintf("%v", RepoPrivate))
assert.False(t, options.Empty())
}
func TestToAndFromMap(t *testing.T) {
options := New()
options.Parse(fmt.Sprintf("%v", RepoPrivate))
actual := options.Map()
expected := map[string]string{string(RepoPrivate): "true"}
assert.EqualValues(t, expected, actual)
assert.EqualValues(t, expected, NewFromMap(&actual).Map())
}
func TestChangeRepositorySettings(t *testing.T) {
options := New()
assert.False(t, options.ChangeRepoSettings())
assert.True(t, options.Parse(fmt.Sprintf("%v=description", AgitDescription)))
assert.False(t, options.ChangeRepoSettings())
options.Parse(fmt.Sprintf("%v", RepoPrivate))
assert.True(t, options.ChangeRepoSettings())
options = New()
options.Parse(fmt.Sprintf("%v", RepoTemplate))
assert.True(t, options.ChangeRepoSettings())
}
func TestParse(t *testing.T) {
t.Run("no key", func(t *testing.T) {
options := New()
val, ok := options.GetString(RepoPrivate)
assert.False(t, ok)
assert.Equal(t, "", val)
assert.True(t, options.GetBool(RepoPrivate, true))
assert.False(t, options.GetBool(RepoPrivate, false))
})
t.Run("key=value", func(t *testing.T) {
options := New()
topic := "TOPIC"
assert.True(t, options.Parse(fmt.Sprintf("%v=%s", AgitTopic, topic)))
val, ok := options.GetString(AgitTopic)
assert.True(t, ok)
assert.Equal(t, topic, val)
})
t.Run("key=true", func(t *testing.T) {
options := New()
assert.True(t, options.Parse(fmt.Sprintf("%v=true", RepoPrivate)))
assert.True(t, options.GetBool(RepoPrivate, false))
assert.True(t, options.Parse(fmt.Sprintf("%v=TRUE", RepoTemplate)))
assert.True(t, options.GetBool(RepoTemplate, false))
})
t.Run("key=false", func(t *testing.T) {
options := New()
assert.True(t, options.Parse(fmt.Sprintf("%v=false", RepoPrivate)))
assert.False(t, options.GetBool(RepoPrivate, true))
})
t.Run("key", func(t *testing.T) {
options := New()
assert.True(t, options.Parse(fmt.Sprintf("%v", RepoPrivate)))
assert.True(t, options.GetBool(RepoPrivate, false))
})
t.Run("unknown keys are ignored", func(t *testing.T) {
options := New()
assert.True(t, options.Empty())
assert.False(t, options.Parse("unknown=value"))
assert.True(t, options.Empty())
})
}
func TestReadEnv(t *testing.T) {
t.Setenv(envPrefix+"_0", fmt.Sprintf("%v=true", AgitForcePush))
t.Setenv(envPrefix+"_1", fmt.Sprintf("%v", RepoPrivate))
t.Setenv(envPrefix+"_2", fmt.Sprintf("%v=equal=in string", AgitTitle))
t.Setenv(envPrefix+"_3", "not=valid")
t.Setenv(envPrefix+"_4", fmt.Sprintf("%v=description", AgitDescription))
t.Setenv(EnvCount, "5")
options := New().ReadEnv()
assert.True(t, options.GetBool(AgitForcePush, false))
assert.True(t, options.GetBool(RepoPrivate, false))
assert.False(t, options.GetBool(RepoTemplate, false))
{
val, ok := options.GetString(AgitTitle)
assert.True(t, ok)
assert.Equal(t, "equal=in string", val)
}
{
val, ok := options.GetString(AgitDescription)
assert.True(t, ok)
assert.Equal(t, "description", val)
}
{
_, ok := options.GetString(AgitTopic)
assert.False(t, ok)
}
}