2018-05-15 11:54:36 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-07-15 14:10:26 +00:00
|
|
|
"io/ioutil"
|
2018-05-15 11:54:36 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-07-15 14:10:26 +00:00
|
|
|
func TestLoadEnv(t *testing.T) {
|
|
|
|
before := len(os.Environ())
|
|
|
|
LoadEnv("")
|
|
|
|
LoadEnv("1230")
|
|
|
|
after := len(os.Environ())
|
|
|
|
|
|
|
|
if before != after {
|
|
|
|
t.Errorf("Expected the same number of env values")
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []byte("FATHOM_DATABASE_DRIVER=\"sqlite3\"")
|
|
|
|
ioutil.WriteFile("env_values", data, 0644)
|
|
|
|
defer os.Remove("env_values")
|
|
|
|
|
|
|
|
LoadEnv("env_values")
|
|
|
|
|
|
|
|
got := os.Getenv("FATHOM_DATABASE_DRIVER")
|
|
|
|
if got != "sqlite3" {
|
|
|
|
t.Errorf("Expected %v, got %v", "sqlite3", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 11:54:36 +00:00
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
// empty config, should not fatal
|
2018-07-15 14:10:26 +00:00
|
|
|
cfg := Parse()
|
2018-05-15 11:54:36 +00:00
|
|
|
if cfg.Secret == "" {
|
|
|
|
t.Errorf("expected secret, got empty string")
|
|
|
|
}
|
|
|
|
|
2018-05-15 12:15:47 +00:00
|
|
|
secret := "my-super-secret-string"
|
|
|
|
os.Setenv("FATHOM_SECRET", secret)
|
2018-07-15 14:10:26 +00:00
|
|
|
cfg = Parse()
|
2018-05-15 12:15:47 +00:00
|
|
|
if cfg.Secret != secret {
|
|
|
|
t.Errorf("Expected %#v, got %#v", secret, cfg.Secret)
|
|
|
|
}
|
|
|
|
|
2018-05-15 11:54:36 +00:00
|
|
|
os.Setenv("FATHOM_DATABASE_DRIVER", "sqlite")
|
2018-07-15 14:10:26 +00:00
|
|
|
cfg = Parse()
|
2018-05-15 11:54:36 +00:00
|
|
|
if cfg.Database.Driver != "sqlite3" {
|
|
|
|
t.Errorf("expected %#v, got %#v", "sqlite3", cfg.Database.Driver)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 16:32:04 +00:00
|
|
|
func TestDatabaseURL(t *testing.T) {
|
|
|
|
data := []byte("FATHOM_DATABASE_URL=\"postgres://dbuser:dbsecret@dbhost:1234/dbname\"")
|
|
|
|
ioutil.WriteFile("env_values", data, 0644)
|
|
|
|
defer os.Remove("env_values")
|
|
|
|
|
|
|
|
LoadEnv("env_values")
|
|
|
|
cfg := Parse()
|
|
|
|
driver := "postgres"
|
|
|
|
url := "postgres://dbuser:dbsecret@dbhost:1234/dbname"
|
|
|
|
if cfg.Database.Driver != driver {
|
|
|
|
t.Errorf("Expected %#v, got %#v", driver, cfg.Database.Driver)
|
|
|
|
}
|
|
|
|
if cfg.Database.URL != url {
|
|
|
|
t.Errorf("Expected %#v, got %#v", url, cfg.Database.URL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 11:54:36 +00:00
|
|
|
func TestRandomString(t *testing.T) {
|
|
|
|
r1 := randomString(10)
|
|
|
|
r2 := randomString(10)
|
|
|
|
|
|
|
|
if r1 == r2 {
|
|
|
|
t.Errorf("expected two different strings, got %#v", r1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if l := len(r1); l != 10 {
|
|
|
|
t.Errorf("expected string of length %d, got string of length %d", 10, l)
|
|
|
|
}
|
|
|
|
}
|