51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package config
|
|
|
|
import (
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
func TestDatabaseURLPreservesCredentials(t *testing.T) {
|
|
c := Config{PGHost: "192.168.100.127", PGPort: "5432", PGUser: "user@name", PGPassword: "p@ss:/?#&", PGSSLMode: "require"}
|
|
u, err := url.Parse(c.DatabaseURL("veyra"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
password, _ := u.User.Password()
|
|
if password != c.PGPassword || u.User.Username() != c.PGUser || u.Path != "/veyra" || u.Query().Get("sslmode") != "require" {
|
|
t.Fatal("connection URL changed configuration values")
|
|
}
|
|
}
|
|
|
|
func TestRejectInvalidRedisDB(t *testing.T) {
|
|
t.Setenv("REDIS_DB", "-1")
|
|
if _, err := Load(); err == nil {
|
|
t.Fatal("negative database must be rejected")
|
|
}
|
|
}
|
|
|
|
func TestPublicOriginConfiguration(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
origin string
|
|
valid bool
|
|
}{
|
|
{"", true},
|
|
{"https://veyra.sundynix.cn", true},
|
|
{"http://localhost:8080", true},
|
|
{"https://veyra.sundynix.cn/", false},
|
|
{"https://veyra.sundynix.cn?", false},
|
|
{"https://veyra.sundynix.cn#", false},
|
|
{"https://user:password@veyra.sundynix.cn", false},
|
|
{"ftp://veyra.sundynix.cn", false},
|
|
{"https:///missing-host", false},
|
|
} {
|
|
t.Run(tc.origin, func(t *testing.T) {
|
|
t.Setenv("PUBLIC_ORIGIN", tc.origin)
|
|
_, err := Load()
|
|
if (err == nil) != tc.valid {
|
|
t.Fatalf("valid=%v error=%v", tc.valid, err)
|
|
}
|
|
})
|
|
}
|
|
}
|