Files
omnex/control-plane/api/internal/config/config_test.go
T
Xor290 2f6e5a953d
ci-api / test (push) Successful in 28m2s
chore: update
2026-08-08 18:27:52 +02:00

103 lines
2.6 KiB
Go

package config
import "testing"
func TestLoad_MissingJWTSecret(t *testing.T) {
t.Setenv("OMNEX_JWT_SECRET", "")
if _, err := Load(); err == nil {
t.Error("Load() error = nil, want error for missing OMNEX_JWT_SECRET")
}
}
func TestLoad_JWTSecretTooShort(t *testing.T) {
t.Setenv("OMNEX_JWT_SECRET", "short")
if _, err := Load(); err == nil {
t.Error("Load() error = nil, want error for JWT secret < 32 bytes")
}
}
func TestLoad_DevDefaults(t *testing.T) {
t.Setenv("OMNEX_JWT_SECRET", "12345678901234567890123456789012")
t.Setenv("OMNEX_ENV", "")
t.Setenv("OMNEX_DATABASE_URL", "")
t.Setenv("OMNEX_REDIS_URL", "")
cfg, err := Load()
if err != nil {
t.Fatalf("Load() error = %v, want nil", err)
}
if cfg.Env != "dev" {
t.Errorf("Env = %q, want %q", cfg.Env, "dev")
}
if cfg.Addr != ":8080" {
t.Errorf("Addr = %q, want %q", cfg.Addr, ":8080")
}
if cfg.Secure() {
t.Error("Secure() = true in dev, want false")
}
}
func TestLoad_ProdRequiresDatabaseAndRedis(t *testing.T) {
t.Setenv("OMNEX_JWT_SECRET", "12345678901234567890123456789012")
t.Setenv("OMNEX_ENV", "prod")
t.Setenv("OMNEX_DATABASE_URL", "")
t.Setenv("OMNEX_REDIS_URL", "")
if _, err := Load(); err == nil {
t.Error("Load() error = nil, want error for prod without DATABASE_URL/REDIS_URL")
}
}
func TestLoad_ProdRequiresRedisEvenWithDatabase(t *testing.T) {
t.Setenv("OMNEX_JWT_SECRET", "12345678901234567890123456789012")
t.Setenv("OMNEX_ENV", "prod")
t.Setenv("OMNEX_DATABASE_URL", "postgres://localhost/omnex")
t.Setenv("OMNEX_REDIS_URL", "")
if _, err := Load(); err == nil {
t.Error("Load() error = nil, want error for prod without REDIS_URL")
}
}
func TestLoad_ProdSucceedsWithAllRequired(t *testing.T) {
t.Setenv("OMNEX_JWT_SECRET", "12345678901234567890123456789012")
t.Setenv("OMNEX_ENV", "prod")
t.Setenv("OMNEX_DATABASE_URL", "postgres://localhost/omnex")
t.Setenv("OMNEX_REDIS_URL", "redis://localhost:6379/0")
cfg, err := Load()
if err != nil {
t.Fatalf("Load() error = %v, want nil", err)
}
if !cfg.Secure() {
t.Error("Secure() = false in prod, want true")
}
}
func TestSplitCSV(t *testing.T) {
cases := []struct {
in string
want []string
}{
{"", nil},
{"a", []string{"a"}},
{"a,b,c", []string{"a", "b", "c"}},
{"a,,c", []string{"a", "c"}},
{"a,b,", []string{"a", "b"}},
{",a,b", []string{"a", "b"}},
}
for _, tc := range cases {
got := splitCSV(tc.in)
if len(got) != len(tc.want) {
t.Errorf("splitCSV(%q) = %v, want %v", tc.in, got, tc.want)
continue
}
for i := range got {
if got[i] != tc.want[i] {
t.Errorf("splitCSV(%q) = %v, want %v", tc.in, got, tc.want)
break
}
}
}
}