2022-07-26 09:33:35 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigEnv(t *testing.T) {
|
|
|
|
os.Setenv("AUTHENTIK_SECRET_KEY", "bar")
|
|
|
|
cfg = nil
|
2022-07-26 09:48:57 +00:00
|
|
|
if err := Get().fromEnv(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-26 09:33:35 +00:00
|
|
|
assert.Equal(t, "bar", Get().SecretKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigEnv_Scheme(t *testing.T) {
|
|
|
|
os.Setenv("foo", "bar")
|
|
|
|
os.Setenv("AUTHENTIK_SECRET_KEY", "env://foo")
|
|
|
|
cfg = nil
|
2022-07-26 09:48:57 +00:00
|
|
|
if err := Get().fromEnv(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-26 09:33:35 +00:00
|
|
|
assert.Equal(t, "bar", Get().SecretKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigEnv_File(t *testing.T) {
|
|
|
|
file, err := os.CreateTemp("", "")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(file.Name())
|
2022-07-26 09:48:57 +00:00
|
|
|
_, err = file.Write([]byte("bar"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-26 09:33:35 +00:00
|
|
|
|
|
|
|
os.Setenv("AUTHENTIK_SECRET_KEY", fmt.Sprintf("file://%s", file.Name()))
|
|
|
|
cfg = nil
|
2022-07-26 09:48:57 +00:00
|
|
|
if err := Get().fromEnv(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-26 09:33:35 +00:00
|
|
|
assert.Equal(t, "bar", Get().SecretKey)
|
|
|
|
}
|