Files
GL/infrastructure/config/config_test.go
T

110 lines
3.2 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestLoadUsesDefaultsAndOverrides(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "gl.toml")
contents := []byte("[grpc]\nport = 0\ntimeout = \"3s\"\n")
if err := os.WriteFile(path, contents, 0o600); err != nil {
t.Fatal(err)
}
cfg, err := Load(path)
if err != nil {
t.Fatal(err)
}
if cfg.GRPC.Port != 0 || cfg.GRPC.ShutdownTimeout != 3*time.Second {
t.Fatalf("unexpected grpc config: %+v", cfg.GRPC)
}
if cfg.Database.Name != "gl_db" || cfg.Database.Port != 5432 {
t.Fatalf("unexpected database defaults: %+v", cfg.Database)
}
}
func TestLoadDashboardUsesHTTPDefaultsAndOverrides(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "dashboard.toml")
contents := []byte("[http]\nport = 0\nshutdown-timeout = \"3s\"\n[db]\nname = \"dashboard_db\"\n")
if err := os.WriteFile(path, contents, 0o600); err != nil {
t.Fatal(err)
}
cfg, err := LoadDashboard(path)
if err != nil {
t.Fatal(err)
}
if cfg.HTTP.Port != 0 || cfg.HTTP.ShutdownTimeout != 3*time.Second || cfg.HTTP.ReadHeaderTimeout != 5*time.Second {
t.Fatalf("unexpected http config: %+v", cfg.HTTP)
}
if cfg.Database.Name != "dashboard_db" || cfg.Database.Port != 5432 {
t.Fatalf("unexpected database defaults: %+v", cfg.Database)
}
}
func TestLoadRejectsInvalidConfiguration(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "gl.toml")
if err := os.WriteFile(path, []byte("[grpc]\ntimeout = \"0s\"\n"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := Load(path); err == nil {
t.Fatal("expected validation error")
}
}
func TestLoadReturnsMissingFileError(t *testing.T) {
if _, err := Load(filepath.Join(t.TempDir(), "missing.toml")); err == nil {
t.Fatal("expected missing file error")
}
}
func TestLoadAppliesSecretEnvironmentOverrides(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "gl.toml")
contents := []byte("environment = \"production\"\n[grpc]\ntimeout = \"3s\"\n[settlement]\nenabled = true\nendpoint = \"https://invalid.example\"\n")
if err := os.WriteFile(path, contents, 0o600); err != nil {
t.Fatal(err)
}
t.Setenv("DARANO_GL_DB_PASSWORD", "runtime-db-secret")
t.Setenv("DARANO_GL_ADMIN_TOKEN", "runtime-admin-secret")
t.Setenv("DARANO_GL_SETTLEMENT_ENDPOINT", "https://horizon.example/")
cfg, err := Load(path)
if err != nil {
t.Fatal(err)
}
if cfg.Database.Password != "runtime-db-secret" {
t.Fatal("database password was not loaded from the environment")
}
if cfg.Settlement.AdminToken != "runtime-admin-secret" {
t.Fatal("admin token was not loaded from the environment")
}
if cfg.Settlement.Endpoint != "https://horizon.example" {
t.Fatalf("unexpected settlement endpoint: %q", cfg.Settlement.Endpoint)
}
}
func TestLoadDashboardAppliesDatabasePasswordEnvironmentOverride(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "dashboard.toml")
if err := os.WriteFile(path, []byte("[http]\nport = 0\n"), 0o600); err != nil {
t.Fatal(err)
}
t.Setenv("DARANO_GL_DB_PASSWORD", "runtime-db-secret")
cfg, err := LoadDashboard(path)
if err != nil {
t.Fatal(err)
}
if cfg.Database.Password != "runtime-db-secret" {
t.Fatal("database password was not loaded from the environment")
}
}