Test overriding values with environment variables

This commit is contained in:
Joel Speed 2019-06-08 23:12:56 +02:00
parent 6f9db420d5
commit 8e92e3dc3d
No known key found for this signature in database
GPG Key ID: 6E80578D6751DEFB
2 changed files with 39 additions and 3 deletions

View File

@ -46,9 +46,9 @@ func Load(config io.Reader, configType string, args []string) (*Options, error)
// Configure loading of environment variables // Configure loading of environment variables
// All flag options are prefixed by the EnvPrefix // All flag options are prefixed by the EnvPrefix
v.SetEnvPrefix("OAUTH2_PROXY_") v.SetEnvPrefix("OAUTH2_PROXY")
// Substitute "-" for "_" so `FOO_BAR` matches the flag `foo-bar` // Substitute "-" for "_" so `FOO_BAR` matches the config `foo.bar`
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv() v.AutomaticEnv()
// Read the configuration file // Read the configuration file

View File

@ -3,6 +3,7 @@ package options
import ( import (
"bytes" "bytes"
"io" "io"
"os"
"testing" "testing"
"time" "time"
@ -62,6 +63,10 @@ var _ = Describe("Load", func() {
`)) `))
}) })
It("returns no error", func() {
Expect(err).NotTo(HaveOccurred())
})
It("sets the correct configuration", func() { It("sets the correct configuration", func() {
expected := &CookieOptions{ expected := &CookieOptions{
Name: "cookie_name", Name: "cookie_name",
@ -76,5 +81,36 @@ var _ = Describe("Load", func() {
Expect(opts.Cookie).To(Equal(expected)) Expect(opts.Cookie).To(Equal(expected))
}) })
Context("with environment configuration", func() {
BeforeEach(func() {
os.Setenv("OAUTH2_PROXY_COOKIE_NAME", "env_cookie_name")
os.Setenv("OAUTH2_PROXY_COOKIE_SECRET", "env_secret_12345")
os.Setenv("OAUTH2_PROXY_COOKIE_DOMAIN", "env.example.com")
os.Setenv("OAUTH2_PROXY_COOKIE_PATH", "/env")
os.Setenv("OAUTH2_PROXY_COOKIE_EXPIRE", "24h")
os.Setenv("OAUTH2_PROXY_COOKIE_REFRESH", "2h")
os.Setenv("OAUTH2_PROXY_COOKIE_SECURE", "true")
os.Setenv("OAUTH2_PROXY_COOKIE_HTTPONLY", "true")
})
It("returns no error", func() {
Expect(err).NotTo(HaveOccurred())
})
It("the environment overrides the config file", func() {
expected := &CookieOptions{
Name: "env_cookie_name",
Secret: "env_secret_12345",
Domain: "env.example.com",
Path: "/env",
Expire: time.Duration(24) * time.Hour,
Refresh: time.Duration(2) * time.Hour,
Secure: true,
HTTPOnly: true,
}
Expect(opts.Cookie).To(Equal(expected))
})
})
}) })
}) })