Add more unit tests for config package

This commit is contained in:
Frédéric Guillot 2018-09-23 15:23:46 -07:00
parent 8601a67a7d
commit 844680e573
3 changed files with 725 additions and 69 deletions

View file

@ -1,4 +1,4 @@
// Copyright 2017 Frédéric Guillot. All rights reserved. // Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0 // Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -14,20 +14,24 @@ import (
) )
const ( const (
defaultBaseURL = "http://localhost" defaultBaseURL = "http://localhost"
defaultDatabaseURL = "postgres://postgres:postgres@localhost/miniflux2?sslmode=disable" defaultDatabaseURL = "postgres://postgres:postgres@localhost/miniflux2?sslmode=disable"
defaultWorkerPoolSize = 5 defaultWorkerPoolSize = 5
defaultPollingFrequency = 60 defaultPollingFrequency = 60
defaultBatchSize = 10 defaultBatchSize = 10
defaultDatabaseMaxConns = 20 defaultDatabaseMaxConns = 20
defaultDatabaseMinConns = 1 defaultDatabaseMinConns = 1
defaultListenAddr = "127.0.0.1:8080" defaultListenAddr = "127.0.0.1:8080"
defaultCertFile = "" defaultCertFile = ""
defaultKeyFile = "" defaultKeyFile = ""
defaultCertDomain = "" defaultCertDomain = ""
defaultCertCache = "/tmp/cert_cache" defaultCertCache = "/tmp/cert_cache"
defaultCleanupFrequency = 24 defaultCleanupFrequency = 24
defaultProxyImages = "http-only" defaultProxyImages = "http-only"
defaultOAuth2ClientID = ""
defaultOAuth2ClientSecret = ""
defaultOAuth2RedirectURL = ""
defaultOAuth2Provider = ""
) )
// Config manages configuration parameters. // Config manages configuration parameters.
@ -38,25 +42,6 @@ type Config struct {
basePath string basePath string
} }
func (c *Config) get(key, fallback string) string {
value := os.Getenv(key)
if value == "" {
return fallback
}
return value
}
func (c *Config) getInt(key string, fallback int) int {
value := os.Getenv(key)
if value == "" {
return fallback
}
v, _ := strconv.Atoi(value)
return v
}
func (c *Config) parseBaseURL() { func (c *Config) parseBaseURL() {
baseURL := os.Getenv("BASE_URL") baseURL := os.Getenv("BASE_URL")
if baseURL == "" { if baseURL == "" {
@ -88,7 +73,7 @@ func (c *Config) parseBaseURL() {
// HasDebugMode returns true if debug mode is enabled. // HasDebugMode returns true if debug mode is enabled.
func (c *Config) HasDebugMode() bool { func (c *Config) HasDebugMode() bool {
return c.get("DEBUG", "") != "" return getBooleanValue("DEBUG")
} }
// BaseURL returns the application base URL with path. // BaseURL returns the application base URL with path.
@ -122,12 +107,12 @@ func (c *Config) DatabaseURL() string {
// DatabaseMaxConns returns the maximum number of database connections. // DatabaseMaxConns returns the maximum number of database connections.
func (c *Config) DatabaseMaxConns() int { func (c *Config) DatabaseMaxConns() int {
return c.getInt("DATABASE_MAX_CONNS", defaultDatabaseMaxConns) return getIntValue("DATABASE_MAX_CONNS", defaultDatabaseMaxConns)
} }
// DatabaseMinConns returns the minimum number of database connections. // DatabaseMinConns returns the minimum number of database connections.
func (c *Config) DatabaseMinConns() int { func (c *Config) DatabaseMinConns() int {
return c.getInt("DATABASE_MIN_CONNS", defaultDatabaseMinConns) return getIntValue("DATABASE_MIN_CONNS", defaultDatabaseMinConns)
} }
// ListenAddr returns the listen address for the HTTP server. // ListenAddr returns the listen address for the HTTP server.
@ -136,97 +121,97 @@ func (c *Config) ListenAddr() string {
return ":" + port return ":" + port
} }
return c.get("LISTEN_ADDR", defaultListenAddr) return getStringValue("LISTEN_ADDR", defaultListenAddr)
} }
// CertFile returns the SSL certificate filename if any. // CertFile returns the SSL certificate filename if any.
func (c *Config) CertFile() string { func (c *Config) CertFile() string {
return c.get("CERT_FILE", defaultCertFile) return getStringValue("CERT_FILE", defaultCertFile)
} }
// KeyFile returns the private key filename for custom SSL certificate. // KeyFile returns the private key filename for custom SSL certificate.
func (c *Config) KeyFile() string { func (c *Config) KeyFile() string {
return c.get("KEY_FILE", defaultKeyFile) return getStringValue("KEY_FILE", defaultKeyFile)
} }
// CertDomain returns the domain to use for Let's Encrypt certificate. // CertDomain returns the domain to use for Let's Encrypt certificate.
func (c *Config) CertDomain() string { func (c *Config) CertDomain() string {
return c.get("CERT_DOMAIN", defaultCertDomain) return getStringValue("CERT_DOMAIN", defaultCertDomain)
} }
// CertCache returns the directory to use for Let's Encrypt session cache. // CertCache returns the directory to use for Let's Encrypt session cache.
func (c *Config) CertCache() string { func (c *Config) CertCache() string {
return c.get("CERT_CACHE", defaultCertCache) return getStringValue("CERT_CACHE", defaultCertCache)
} }
// CleanupFrequency returns the interval for cleanup jobs. // CleanupFrequency returns the interval for cleanup jobs.
func (c *Config) CleanupFrequency() int { func (c *Config) CleanupFrequency() int {
return c.getInt("CLEANUP_FREQUENCY", defaultCleanupFrequency) return getIntValue("CLEANUP_FREQUENCY", defaultCleanupFrequency)
} }
// WorkerPoolSize returns the number of background worker. // WorkerPoolSize returns the number of background worker.
func (c *Config) WorkerPoolSize() int { func (c *Config) WorkerPoolSize() int {
return c.getInt("WORKER_POOL_SIZE", defaultWorkerPoolSize) return getIntValue("WORKER_POOL_SIZE", defaultWorkerPoolSize)
} }
// PollingFrequency returns the interval to refresh feeds in the background. // PollingFrequency returns the interval to refresh feeds in the background.
func (c *Config) PollingFrequency() int { func (c *Config) PollingFrequency() int {
return c.getInt("POLLING_FREQUENCY", defaultPollingFrequency) return getIntValue("POLLING_FREQUENCY", defaultPollingFrequency)
} }
// BatchSize returns the number of feeds to send for background processing. // BatchSize returns the number of feeds to send for background processing.
func (c *Config) BatchSize() int { func (c *Config) BatchSize() int {
return c.getInt("BATCH_SIZE", defaultBatchSize) return getIntValue("BATCH_SIZE", defaultBatchSize)
} }
// IsOAuth2UserCreationAllowed returns true if user creation is allowed for OAuth2 users. // IsOAuth2UserCreationAllowed returns true if user creation is allowed for OAuth2 users.
func (c *Config) IsOAuth2UserCreationAllowed() bool { func (c *Config) IsOAuth2UserCreationAllowed() bool {
return c.getInt("OAUTH2_USER_CREATION", 0) == 1 return getBooleanValue("OAUTH2_USER_CREATION")
} }
// OAuth2ClientID returns the OAuth2 Client ID. // OAuth2ClientID returns the OAuth2 Client ID.
func (c *Config) OAuth2ClientID() string { func (c *Config) OAuth2ClientID() string {
return c.get("OAUTH2_CLIENT_ID", "") return getStringValue("OAUTH2_CLIENT_ID", defaultOAuth2ClientID)
} }
// OAuth2ClientSecret returns the OAuth2 client secret. // OAuth2ClientSecret returns the OAuth2 client secret.
func (c *Config) OAuth2ClientSecret() string { func (c *Config) OAuth2ClientSecret() string {
return c.get("OAUTH2_CLIENT_SECRET", "") return getStringValue("OAUTH2_CLIENT_SECRET", defaultOAuth2ClientSecret)
} }
// OAuth2RedirectURL returns the OAuth2 redirect URL. // OAuth2RedirectURL returns the OAuth2 redirect URL.
func (c *Config) OAuth2RedirectURL() string { func (c *Config) OAuth2RedirectURL() string {
return c.get("OAUTH2_REDIRECT_URL", "") return getStringValue("OAUTH2_REDIRECT_URL", defaultOAuth2RedirectURL)
} }
// OAuth2Provider returns the name of the OAuth2 provider configured. // OAuth2Provider returns the name of the OAuth2 provider configured.
func (c *Config) OAuth2Provider() string { func (c *Config) OAuth2Provider() string {
return c.get("OAUTH2_PROVIDER", "") return getStringValue("OAUTH2_PROVIDER", defaultOAuth2Provider)
} }
// HasHSTS returns true if HTTP Strict Transport Security is enabled. // HasHSTS returns true if HTTP Strict Transport Security is enabled.
func (c *Config) HasHSTS() bool { func (c *Config) HasHSTS() bool {
return c.get("DISABLE_HSTS", "") == "" return !getBooleanValue("DISABLE_HSTS")
} }
// RunMigrations returns true if the environment variable RUN_MIGRATIONS is not empty. // RunMigrations returns true if the environment variable RUN_MIGRATIONS is not empty.
func (c *Config) RunMigrations() bool { func (c *Config) RunMigrations() bool {
return c.get("RUN_MIGRATIONS", "") != "" return getBooleanValue("RUN_MIGRATIONS")
} }
// CreateAdmin returns true if the environment variable CREATE_ADMIN is not empty. // CreateAdmin returns true if the environment variable CREATE_ADMIN is not empty.
func (c *Config) CreateAdmin() bool { func (c *Config) CreateAdmin() bool {
return c.get("CREATE_ADMIN", "") != "" return getBooleanValue("CREATE_ADMIN")
} }
// PocketConsumerKey returns the Pocket Consumer Key if defined as environment variable. // PocketConsumerKey returns the Pocket Consumer Key if defined as environment variable.
func (c *Config) PocketConsumerKey(defaultValue string) string { func (c *Config) PocketConsumerKey(defaultValue string) string {
return c.get("POCKET_CONSUMER_KEY", defaultValue) return getStringValue("POCKET_CONSUMER_KEY", defaultValue)
} }
// ProxyImages returns "none" to never proxy, "http-only" to proxy non-HTTPS, "all" to always proxy. // ProxyImages returns "none" to never proxy, "http-only" to proxy non-HTTPS, "all" to always proxy.
func (c *Config) ProxyImages() string { func (c *Config) ProxyImages() string {
return c.get("PROXY_IMAGES", defaultProxyImages) return getStringValue("PROXY_IMAGES", defaultProxyImages)
} }
// NewConfig returns a new Config. // NewConfig returns a new Config.
@ -234,9 +219,40 @@ func NewConfig() *Config {
cfg := &Config{ cfg := &Config{
baseURL: defaultBaseURL, baseURL: defaultBaseURL,
rootURL: defaultBaseURL, rootURL: defaultBaseURL,
IsHTTPS: os.Getenv("HTTPS") != "", IsHTTPS: getBooleanValue("HTTPS"),
} }
cfg.parseBaseURL() cfg.parseBaseURL()
return cfg return cfg
} }
func getBooleanValue(key string) bool {
value := strings.ToLower(os.Getenv(key))
if value == "1" || value == "yes" || value == "true" || value == "on" {
return true
}
return false
}
func getStringValue(key, fallback string) string {
value := os.Getenv(key)
if value == "" {
return fallback
}
return value
}
func getIntValue(key string, fallback int) int {
value := os.Getenv(key)
if value == "" {
return fallback
}
v, err := strconv.Atoi(value)
if err != nil {
return fallback
}
return v
}

View file

@ -1,4 +1,4 @@
// Copyright 2017 Frédéric Guillot. All rights reserved. // Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0 // Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -9,6 +9,75 @@ import (
"testing" "testing"
) )
func TestGetBooleanValueWithUnsetVariable(t *testing.T) {
os.Clearenv()
if getBooleanValue("MY_TEST_VARIABLE") {
t.Errorf(`Unset variables should returns false`)
}
}
func TestGetBooleanValue(t *testing.T) {
scenarios := map[string]bool{
"": false,
"1": true,
"Yes": true,
"yes": true,
"True": true,
"true": true,
"on": true,
"false": false,
"off": false,
"invalid": false,
}
for input, expected := range scenarios {
os.Clearenv()
os.Setenv("MY_TEST_VARIABLE", input)
result := getBooleanValue("MY_TEST_VARIABLE")
if result != expected {
t.Errorf(`Unexpected result for %q, got %v instead of %v`, input, result, expected)
}
}
}
func TestGetStringValueWithUnsetVariable(t *testing.T) {
os.Clearenv()
if getStringValue("MY_TEST_VARIABLE", "defaultValue") != "defaultValue" {
t.Errorf(`Unset variables should returns the default value`)
}
}
func TestGetStringValue(t *testing.T) {
os.Clearenv()
os.Setenv("MY_TEST_VARIABLE", "test")
if getStringValue("MY_TEST_VARIABLE", "defaultValue") != "test" {
t.Errorf(`Defined variables should returns the specified value`)
}
}
func TestGetIntValueWithUnsetVariable(t *testing.T) {
os.Clearenv()
if getIntValue("MY_TEST_VARIABLE", 42) != 42 {
t.Errorf(`Unset variables should returns the default value`)
}
}
func TestGetIntValueWithInvalidInput(t *testing.T) {
os.Clearenv()
os.Setenv("MY_TEST_VARIABLE", "invalid integer")
if getIntValue("MY_TEST_VARIABLE", 42) != 42 {
t.Errorf(`Invalid integer should returns the default value`)
}
}
func TestGetIntValue(t *testing.T) {
os.Clearenv()
os.Setenv("MY_TEST_VARIABLE", "2018")
if getIntValue("MY_TEST_VARIABLE", 42) != 2018 {
t.Errorf(`Defined variables should returns the specified value`)
}
}
func TestDebugModeOn(t *testing.T) { func TestDebugModeOn(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("DEBUG", "1") os.Setenv("DEBUG", "1")
@ -100,15 +169,16 @@ func TestBaseURLWithInvalidScheme(t *testing.T) {
} }
} }
func TestDefaultBaseURL(t *testing.T) { func TestInvalidBaseURL(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("BASE_URL", "http://example|org")
cfg := NewConfig() cfg := NewConfig()
if cfg.BaseURL() != "http://localhost" { if cfg.BaseURL() != defaultBaseURL {
t.Fatalf(`Unexpected base URL, got "%s"`, cfg.BaseURL()) t.Fatalf(`Unexpected base URL, got "%s"`, cfg.BaseURL())
} }
if cfg.RootURL() != "http://localhost" { if cfg.RootURL() != defaultBaseURL {
t.Fatalf(`Unexpected root URL, got "%s"`, cfg.RootURL()) t.Fatalf(`Unexpected root URL, got "%s"`, cfg.RootURL())
} }
@ -117,21 +187,591 @@ func TestDefaultBaseURL(t *testing.T) {
} }
} }
func TestHSTSOn(t *testing.T) { func TestDefaultBaseURL(t *testing.T) {
os.Clearenv() os.Clearenv()
cfg := NewConfig() cfg := NewConfig()
if !cfg.HasHSTS() { if cfg.BaseURL() != defaultBaseURL {
t.Fatalf(`Unexpected HSTS value, got "%v"`, cfg.HasHSTS()) t.Fatalf(`Unexpected base URL, got "%s"`, cfg.BaseURL())
}
if cfg.RootURL() != defaultBaseURL {
t.Fatalf(`Unexpected root URL, got "%s"`, cfg.RootURL())
}
if cfg.BasePath() != "" {
t.Fatalf(`Unexpected base path, got "%s"`, cfg.BasePath())
} }
} }
func TestHSTSOff(t *testing.T) { func TestDatabaseURL(t *testing.T) {
os.Clearenv()
os.Setenv("DATABASE_URL", "foobar")
cfg := NewConfig()
expected := "foobar"
result := cfg.DatabaseURL()
if result != expected {
t.Fatalf(`Unexpected DATABASE_URL value, got %q instead of %q`, result, expected)
}
}
func TestDefaultDatabaseURLValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
result := cfg.DatabaseURL()
expected := defaultDatabaseURL
if result != expected {
t.Fatalf(`Unexpected DATABASE_URL value, got %q instead of %q`, result, expected)
}
}
func TestDefaultDatabaseMaxConnsValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
expected := defaultDatabaseMaxConns
result := cfg.DatabaseMaxConns()
if result != expected {
t.Fatalf(`Unexpected DATABASE_MAX_CONNS value, got %v instead of %v`, result, expected)
}
}
func TestDeatabaseMaxConns(t *testing.T) {
os.Clearenv()
os.Setenv("DATABASE_MAX_CONNS", "42")
cfg := NewConfig()
expected := 42
result := cfg.DatabaseMaxConns()
if result != expected {
t.Fatalf(`Unexpected DATABASE_MAX_CONNS value, got %v instead of %v`, result, expected)
}
}
func TestDefaultDatabaseMinConnsValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
expected := defaultDatabaseMinConns
result := cfg.DatabaseMinConns()
if result != expected {
t.Fatalf(`Unexpected DATABASE_MIN_CONNS value, got %v instead of %v`, result, expected)
}
}
func TestDatabaseMinConns(t *testing.T) {
os.Clearenv()
os.Setenv("DATABASE_MIN_CONNS", "42")
cfg := NewConfig()
expected := 42
result := cfg.DatabaseMinConns()
if result != expected {
t.Fatalf(`Unexpected DATABASE_MIN_CONNS value, got %v instead of %v`, result, expected)
}
}
func TestListenAddr(t *testing.T) {
os.Clearenv()
os.Setenv("LISTEN_ADDR", "foobar")
cfg := NewConfig()
expected := "foobar"
result := cfg.ListenAddr()
if result != expected {
t.Fatalf(`Unexpected LISTEN_ADDR value, got %q instead of %q`, result, expected)
}
}
func TestListenAddrWithPortDefined(t *testing.T) {
os.Clearenv()
os.Setenv("PORT", "3000")
os.Setenv("LISTEN_ADDR", "foobar")
cfg := NewConfig()
expected := ":3000"
result := cfg.ListenAddr()
if result != expected {
t.Fatalf(`Unexpected LISTEN_ADDR value, got %q instead of %q`, result, expected)
}
}
func TestDefaultListenAddrValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
result := cfg.ListenAddr()
expected := defaultListenAddr
if result != expected {
t.Fatalf(`Unexpected LISTEN_ADDR value, got %q instead of %q`, result, expected)
}
}
func TestCertFile(t *testing.T) {
os.Clearenv()
os.Setenv("CERT_FILE", "foobar")
cfg := NewConfig()
expected := "foobar"
result := cfg.CertFile()
if result != expected {
t.Fatalf(`Unexpected CERT_FILE value, got %q instead of %q`, result, expected)
}
}
func TestDefaultCertFileValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
result := cfg.CertFile()
expected := defaultCertFile
if result != expected {
t.Fatalf(`Unexpected CERT_FILE value, got %q instead of %q`, result, expected)
}
}
func TestKeyFile(t *testing.T) {
os.Clearenv()
os.Setenv("KEY_FILE", "foobar")
cfg := NewConfig()
expected := "foobar"
result := cfg.KeyFile()
if result != expected {
t.Fatalf(`Unexpected KEY_FILE value, got %q instead of %q`, result, expected)
}
}
func TestDefaultKeyFileValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
result := cfg.KeyFile()
expected := defaultKeyFile
if result != expected {
t.Fatalf(`Unexpected KEY_FILE value, got %q instead of %q`, result, expected)
}
}
func TestCertDomain(t *testing.T) {
os.Clearenv()
os.Setenv("CERT_DOMAIN", "example.org")
cfg := NewConfig()
expected := "example.org"
result := cfg.CertDomain()
if result != expected {
t.Fatalf(`Unexpected CERT_DOMAIN value, got %q instead of %q`, result, expected)
}
}
func TestDefaultCertDomainValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
result := cfg.CertDomain()
expected := defaultCertDomain
if result != expected {
t.Fatalf(`Unexpected CERT_DOMAIN value, got %q instead of %q`, result, expected)
}
}
func TestCertCache(t *testing.T) {
os.Clearenv()
os.Setenv("CERT_CACHE", "foobar")
cfg := NewConfig()
expected := "foobar"
result := cfg.CertCache()
if result != expected {
t.Fatalf(`Unexpected CERT_CACHE value, got %q instead of %q`, result, expected)
}
}
func TestDefaultCertCacheValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
result := cfg.CertCache()
expected := defaultCertCache
if result != expected {
t.Fatalf(`Unexpected CERT_CACHE value, got %q instead of %q`, result, expected)
}
}
func TestDefaultCleanupFrequencyValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
expected := defaultCleanupFrequency
result := cfg.CleanupFrequency()
if result != expected {
t.Fatalf(`Unexpected CLEANUP_FREQUENCY value, got %v instead of %v`, result, expected)
}
}
func TestCleanupFrequency(t *testing.T) {
os.Clearenv()
os.Setenv("CLEANUP_FREQUENCY", "42")
cfg := NewConfig()
expected := 42
result := cfg.CleanupFrequency()
if result != expected {
t.Fatalf(`Unexpected CLEANUP_FREQUENCY value, got %v instead of %v`, result, expected)
}
}
func TestDefaultWorkerPoolSizeValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
expected := defaultWorkerPoolSize
result := cfg.WorkerPoolSize()
if result != expected {
t.Fatalf(`Unexpected WORKER_POOL_SIZE value, got %v instead of %v`, result, expected)
}
}
func TestWorkerPoolSize(t *testing.T) {
os.Clearenv()
os.Setenv("WORKER_POOL_SIZE", "42")
cfg := NewConfig()
expected := 42
result := cfg.WorkerPoolSize()
if result != expected {
t.Fatalf(`Unexpected WORKER_POOL_SIZE value, got %v instead of %v`, result, expected)
}
}
func TestDefautPollingFrequencyValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
expected := defaultPollingFrequency
result := cfg.PollingFrequency()
if result != expected {
t.Fatalf(`Unexpected POLLING_FREQUENCY value, got %v instead of %v`, result, expected)
}
}
func TestPollingFrequency(t *testing.T) {
os.Clearenv()
os.Setenv("POLLING_FREQUENCY", "42")
cfg := NewConfig()
expected := 42
result := cfg.PollingFrequency()
if result != expected {
t.Fatalf(`Unexpected POLLING_FREQUENCY value, got %v instead of %v`, result, expected)
}
}
func TestDefaultBatchSizeValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
expected := defaultBatchSize
result := cfg.BatchSize()
if result != expected {
t.Fatalf(`Unexpected BATCH_SIZE value, got %v instead of %v`, result, expected)
}
}
func TestBatchSize(t *testing.T) {
os.Clearenv()
os.Setenv("BATCH_SIZE", "42")
cfg := NewConfig()
expected := 42
result := cfg.BatchSize()
if result != expected {
t.Fatalf(`Unexpected BATCH_SIZE value, got %v instead of %v`, result, expected)
}
}
func TestOAuth2UserCreationWhenUnset(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
expected := false
result := cfg.IsOAuth2UserCreationAllowed()
if result != expected {
t.Fatalf(`Unexpected OAUTH2_USER_CREATION value, got %v instead of %v`, result, expected)
}
}
func TestOAuth2UserCreationAdmin(t *testing.T) {
os.Clearenv()
os.Setenv("OAUTH2_USER_CREATION", "1")
cfg := NewConfig()
expected := true
result := cfg.IsOAuth2UserCreationAllowed()
if result != expected {
t.Fatalf(`Unexpected OAUTH2_USER_CREATION value, got %v instead of %v`, result, expected)
}
}
func TestOAuth2ClientID(t *testing.T) {
os.Clearenv()
os.Setenv("OAUTH2_CLIENT_ID", "foobar")
cfg := NewConfig()
expected := "foobar"
result := cfg.OAuth2ClientID()
if result != expected {
t.Fatalf(`Unexpected OAUTH2_CLIENT_ID value, got %q instead of %q`, result, expected)
}
}
func TestDefaultOAuth2ClientIDValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
result := cfg.OAuth2ClientID()
expected := defaultOAuth2ClientID
if result != expected {
t.Fatalf(`Unexpected OAUTH2_CLIENT_ID value, got %q instead of %q`, result, expected)
}
}
func TestOAuth2ClientSecret(t *testing.T) {
os.Clearenv()
os.Setenv("OAUTH2_CLIENT_SECRET", "secret")
cfg := NewConfig()
expected := "secret"
result := cfg.OAuth2ClientSecret()
if result != expected {
t.Fatalf(`Unexpected OAUTH2_CLIENT_SECRET value, got %q instead of %q`, result, expected)
}
}
func TestDefaultOAuth2ClientSecretValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
result := cfg.OAuth2ClientSecret()
expected := defaultOAuth2ClientSecret
if result != expected {
t.Fatalf(`Unexpected OAUTH2_CLIENT_SECRET value, got %q instead of %q`, result, expected)
}
}
func TestOAuth2RedirectURL(t *testing.T) {
os.Clearenv()
os.Setenv("OAUTH2_REDIRECT_URL", "http://example.org")
cfg := NewConfig()
expected := "http://example.org"
result := cfg.OAuth2RedirectURL()
if result != expected {
t.Fatalf(`Unexpected OAUTH2_REDIRECT_URL value, got %q instead of %q`, result, expected)
}
}
func TestDefaultOAuth2RedirectURLValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
result := cfg.OAuth2RedirectURL()
expected := defaultOAuth2RedirectURL
if result != expected {
t.Fatalf(`Unexpected OAUTH2_REDIRECT_URL value, got %q instead of %q`, result, expected)
}
}
func TestOAuth2Provider(t *testing.T) {
os.Clearenv()
os.Setenv("OAUTH2_PROVIDER", "google")
cfg := NewConfig()
expected := "google"
result := cfg.OAuth2Provider()
if result != expected {
t.Fatalf(`Unexpected OAUTH2_PROVIDER value, got %q instead of %q`, result, expected)
}
}
func TestDefaultOAuth2ProviderValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
result := cfg.OAuth2Provider()
expected := defaultOAuth2Provider
if result != expected {
t.Fatalf(`Unexpected OAUTH2_PROVIDER value, got %q instead of %q`, result, expected)
}
}
func TestHSTSWhenUnset(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
expected := true
result := cfg.HasHSTS()
if result != expected {
t.Fatalf(`Unexpected DISABLE_HSTS value, got %v instead of %v`, result, expected)
}
}
func TestHSTS(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("DISABLE_HSTS", "1") os.Setenv("DISABLE_HSTS", "1")
cfg := NewConfig()
if cfg.HasHSTS() { cfg := NewConfig()
t.Fatalf(`Unexpected HSTS value, got "%v"`, cfg.HasHSTS()) expected := false
result := cfg.HasHSTS()
if result != expected {
t.Fatalf(`Unexpected DISABLE_HSTS value, got %v instead of %v`, result, expected)
}
}
func TestRunMigrationsWhenUnset(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
expected := false
result := cfg.RunMigrations()
if result != expected {
t.Fatalf(`Unexpected RUN_MIGRATIONS value, got %v instead of %v`, result, expected)
}
}
func TestRunMigrations(t *testing.T) {
os.Clearenv()
os.Setenv("RUN_MIGRATIONS", "yes")
cfg := NewConfig()
expected := true
result := cfg.RunMigrations()
if result != expected {
t.Fatalf(`Unexpected RUN_MIGRATIONS value, got %v instead of %v`, result, expected)
}
}
func TestCreateAdminWhenUnset(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
expected := false
result := cfg.CreateAdmin()
if result != expected {
t.Fatalf(`Unexpected CREATE_ADMIN value, got %v instead of %v`, result, expected)
}
}
func TestCreateAdmin(t *testing.T) {
os.Clearenv()
os.Setenv("CREATE_ADMIN", "true")
cfg := NewConfig()
expected := true
result := cfg.CreateAdmin()
if result != expected {
t.Fatalf(`Unexpected CREATE_ADMIN value, got %v instead of %v`, result, expected)
}
}
func TestPocketConsumerKeyFromEnvVariable(t *testing.T) {
os.Clearenv()
os.Setenv("POCKET_CONSUMER_KEY", "something")
cfg := NewConfig()
expected := "something"
result := cfg.PocketConsumerKey("default")
if result != expected {
t.Fatalf(`Unexpected POCKET_CONSUMER_KEY value, got %q instead of %q`, result, expected)
}
}
func TestPocketConsumerKeyFromUserPrefs(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
expected := "default"
result := cfg.PocketConsumerKey("default")
if result != expected {
t.Fatalf(`Unexpected POCKET_CONSUMER_KEY value, got %q instead of %q`, result, expected)
}
}
func TestProxyImages(t *testing.T) {
os.Clearenv()
os.Setenv("PROXY_IMAGES", "all")
cfg := NewConfig()
expected := "all"
result := cfg.ProxyImages()
if result != expected {
t.Fatalf(`Unexpected PROXY_IMAGES value, got %q instead of %q`, result, expected)
}
}
func TestDefaultProxyImagesValue(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
result := cfg.ProxyImages()
expected := defaultProxyImages
if result != expected {
t.Fatalf(`Unexpected PROXY_IMAGES value, got %q instead of %q`, result, expected)
}
}
func TestHTTPSOff(t *testing.T) {
os.Clearenv()
cfg := NewConfig()
if cfg.IsHTTPS {
t.Fatalf(`Unexpected HTTPS value, got "%v"`, cfg.IsHTTPS)
}
}
func TestHTTPSOn(t *testing.T) {
os.Clearenv()
os.Setenv("HTTPS", "on")
cfg := NewConfig()
if !cfg.IsHTTPS {
t.Fatalf(`Unexpected HTTPS value, got "%v"`, cfg.IsHTTPS)
} }
} }

View file

@ -4,7 +4,7 @@
/* /*
Package config handles configuration values for Miniflux application. Package config handles configuration values for the application.
*/ */
package config // import "miniflux.app/config" package config // import "miniflux.app/config"