2023-05-24 03:37:22 +00:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2023-07-10 21:17:39 +00:00
|
|
|
"regexp"
|
2023-05-24 03:37:22 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2023-07-10 21:17:39 +00:00
|
|
|
prefix := regexp.MustCompile(EnvConfigKeyPrefixGitea)
|
2023-05-24 03:37:22 +00:00
|
|
|
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)
|
|
|
|
|
2023-07-20 09:56:36 +00:00
|
|
|
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)
|
|
|
|
|
2023-05-24 03:37:22 +00:00
|
|
|
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)
|
|
|
|
|
2023-07-10 21:17:39 +00:00
|
|
|
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)
|
|
|
|
|
2023-05-24 03:37:22 +00:00
|
|
|
// 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) {
|
2023-06-02 09:27:30 +00:00
|
|
|
cfg, _ := NewConfigProviderFromData("")
|
2023-05-24 03:37:22 +00:00
|
|
|
|
2023-07-10 11:51:05 +00:00
|
|
|
changed := EnvironmentToConfig(cfg, nil)
|
2023-05-24 03:37:22 +00:00
|
|
|
assert.False(t, changed)
|
|
|
|
|
2023-06-02 09:27:30 +00:00
|
|
|
cfg, err := NewConfigProviderFromData(`
|
2023-05-24 03:37:22 +00:00
|
|
|
[sec]
|
|
|
|
key = old
|
2023-06-02 09:27:30 +00:00
|
|
|
`)
|
2023-05-24 03:37:22 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-07-10 11:51:05 +00:00
|
|
|
changed = EnvironmentToConfig(cfg, []string{"GITEA__sec__key=new"})
|
2023-05-24 03:37:22 +00:00
|
|
|
assert.True(t, changed)
|
|
|
|
assert.Equal(t, "new", cfg.Section("sec").Key("key").String())
|
|
|
|
|
2023-07-10 11:51:05 +00:00
|
|
|
changed = EnvironmentToConfig(cfg, []string{"GITEA__sec__key=new"})
|
2023-05-24 03:37:22 +00:00
|
|
|
assert.False(t, changed)
|
|
|
|
|
|
|
|
tmpFile := t.TempDir() + "/the-file"
|
|
|
|
_ = os.WriteFile(tmpFile, []byte("value-from-file"), 0o644)
|
2023-07-10 11:51:05 +00:00
|
|
|
changed = EnvironmentToConfig(cfg, []string{"GITEA__sec__key__FILE=" + tmpFile})
|
2023-05-24 03:37:22 +00:00
|
|
|
assert.True(t, changed)
|
|
|
|
assert.Equal(t, "value-from-file", cfg.Section("sec").Key("key").String())
|
|
|
|
}
|