mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-11 23:32:09 +01:00
4de909747b
Some checks are pending
/ release (push) Waiting to run
testing / test-remote-cacher (map[image:redis:7.2 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:registry.redict.io/redict:7.3.0-scratch port:6379]) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-remote-cacher (map[image:docker.io/valkey/valkey:7.2.5-alpine3.19 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:ghcr.io/microsoft/garnet-alpine:1.0.14 port:6379]) (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
go-require lint is ignored for now Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4535 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: TheFox0x7 <thefox0x7@gmail.com> Co-committed-by: TheFox0x7 <thefox0x7@gmail.com>
151 lines
4.6 KiB
Go
151 lines
4.6 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package setting
|
|
|
|
import (
|
|
"os"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestDecodeEnvSectionKey(t *testing.T) {
|
|
ok, section, key := decodeEnvSectionKey("SEC__KEY")
|
|
assert.True(t, ok)
|
|
assert.Equal(t, "sec", section)
|
|
assert.Equal(t, "KEY", key)
|
|
|
|
ok, section, key = decodeEnvSectionKey("sec__key")
|
|
assert.True(t, ok)
|
|
assert.Equal(t, "sec", section)
|
|
assert.Equal(t, "key", key)
|
|
|
|
ok, section, key = decodeEnvSectionKey("LOG_0x2E_CONSOLE__STDERR")
|
|
assert.True(t, ok)
|
|
assert.Equal(t, "log.console", section)
|
|
assert.Equal(t, "STDERR", key)
|
|
|
|
ok, section, key = decodeEnvSectionKey("SEC")
|
|
assert.False(t, ok)
|
|
assert.Equal(t, "", section)
|
|
assert.Equal(t, "", key)
|
|
}
|
|
|
|
func TestDecodeEnvironmentKey(t *testing.T) {
|
|
prefix := regexp.MustCompile(EnvConfigKeyPrefixGitea)
|
|
suffix := "__FILE"
|
|
|
|
ok, section, key, file := decodeEnvironmentKey(prefix, suffix, "SEC__KEY")
|
|
assert.False(t, ok)
|
|
assert.Equal(t, "", section)
|
|
assert.Equal(t, "", key)
|
|
assert.False(t, file)
|
|
|
|
ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "GITEA__SEC")
|
|
assert.False(t, ok)
|
|
assert.Equal(t, "", section)
|
|
assert.Equal(t, "", key)
|
|
assert.False(t, file)
|
|
|
|
ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "GITEA____KEY")
|
|
assert.True(t, ok)
|
|
assert.Equal(t, "", section)
|
|
assert.Equal(t, "KEY", key)
|
|
assert.False(t, file)
|
|
|
|
ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "GITEA__SEC__KEY")
|
|
assert.True(t, ok)
|
|
assert.Equal(t, "sec", section)
|
|
assert.Equal(t, "KEY", key)
|
|
assert.False(t, file)
|
|
|
|
ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "FORGEJO__SEC__KEY")
|
|
assert.True(t, ok)
|
|
assert.Equal(t, "sec", section)
|
|
assert.Equal(t, "KEY", key)
|
|
assert.False(t, file)
|
|
|
|
// with "__FILE" suffix, it doesn't support to write "[sec].FILE" to config (no such key FILE is used in Gitea)
|
|
// but it could be fixed in the future by adding a new suffix like "__VALUE" (no such key VALUE is used in Gitea either)
|
|
ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "GITEA__SEC__FILE")
|
|
assert.False(t, ok)
|
|
assert.Equal(t, "", section)
|
|
assert.Equal(t, "", key)
|
|
assert.True(t, file)
|
|
|
|
ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "GITEA__SEC__KEY__FILE")
|
|
assert.True(t, ok)
|
|
assert.Equal(t, "sec", section)
|
|
assert.Equal(t, "KEY", key)
|
|
assert.True(t, file)
|
|
}
|
|
|
|
func TestEnvironmentToConfig(t *testing.T) {
|
|
cfg, _ := NewConfigProviderFromData("")
|
|
|
|
changed := EnvironmentToConfig(cfg, nil)
|
|
assert.False(t, changed)
|
|
|
|
cfg, err := NewConfigProviderFromData(`
|
|
[sec]
|
|
key = old
|
|
`)
|
|
require.NoError(t, err)
|
|
|
|
changed = EnvironmentToConfig(cfg, []string{"GITEA__sec__key=new"})
|
|
assert.True(t, changed)
|
|
assert.Equal(t, "new", cfg.Section("sec").Key("key").String())
|
|
|
|
changed = EnvironmentToConfig(cfg, []string{"GITEA__sec__key=new"})
|
|
assert.False(t, changed)
|
|
|
|
tmpFile := t.TempDir() + "/the-file"
|
|
_ = os.WriteFile(tmpFile, []byte("value-from-file"), 0o644)
|
|
changed = EnvironmentToConfig(cfg, []string{"GITEA__sec__key__FILE=" + tmpFile})
|
|
assert.True(t, changed)
|
|
assert.Equal(t, "value-from-file", cfg.Section("sec").Key("key").String())
|
|
|
|
cfg, _ = NewConfigProviderFromData("")
|
|
_ = os.WriteFile(tmpFile, []byte("value-from-file\n"), 0o644)
|
|
EnvironmentToConfig(cfg, []string{"GITEA__sec__key__FILE=" + tmpFile})
|
|
assert.Equal(t, "value-from-file", cfg.Section("sec").Key("key").String())
|
|
|
|
cfg, _ = NewConfigProviderFromData("")
|
|
_ = os.WriteFile(tmpFile, []byte("value-from-file\r\n"), 0o644)
|
|
EnvironmentToConfig(cfg, []string{"GITEA__sec__key__FILE=" + tmpFile})
|
|
assert.Equal(t, "value-from-file", cfg.Section("sec").Key("key").String())
|
|
|
|
cfg, _ = NewConfigProviderFromData("")
|
|
_ = os.WriteFile(tmpFile, []byte("value-from-file\n\n"), 0o644)
|
|
EnvironmentToConfig(cfg, []string{"GITEA__sec__key__FILE=" + tmpFile})
|
|
assert.Equal(t, "value-from-file\n", cfg.Section("sec").Key("key").String())
|
|
}
|
|
|
|
func TestEnvironmentToConfigSubSecKey(t *testing.T) {
|
|
// the INI package has a quirk: by default, the keys are inherited.
|
|
// when maintaining the keys, the newly added sub key should not be affected by the parent key.
|
|
cfg, err := NewConfigProviderFromData(`
|
|
[sec]
|
|
key = some
|
|
`)
|
|
require.NoError(t, err)
|
|
|
|
changed := EnvironmentToConfig(cfg, []string{"GITEA__sec_0X2E_sub__key=some"})
|
|
assert.True(t, changed)
|
|
|
|
tmpFile := t.TempDir() + "/test-sub-sec-key.ini"
|
|
defer os.Remove(tmpFile)
|
|
err = cfg.SaveTo(tmpFile)
|
|
require.NoError(t, err)
|
|
bs, err := os.ReadFile(tmpFile)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, `[sec]
|
|
key = some
|
|
|
|
[sec.sub]
|
|
key = some
|
|
`, string(bs))
|
|
}
|