Files
omnex/control-plane/api/internal/config/config_test.go
T
Xor290 76b1412968
ci-api / test (push) Successful in 24m6s
ci-web / test (push) Successful in 13m50s
chore: update
2026-09-20 11:50:02 +02:00

128 lines
3.4 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
}
}
}
}
func TestLoad_VitrineDefaults(t *testing.T) {
t.Setenv("OMNEX_JWT_SECRET", "0123456789abcdef0123456789abcdef")
cfg, err := Load()
if err != nil {
t.Fatal(err)
}
if cfg.VitrineDomain != "vitrine-omnex.club" || cfg.VitrineGraceDays != 7 || cfg.VitrineChartsDir != "/charts-vitrine" {
t.Errorf("défauts vitrine inattendus : %+v", cfg)
}
}
func TestLoad_VitrineGraceDaysInvalid(t *testing.T) {
t.Setenv("OMNEX_JWT_SECRET", "0123456789abcdef0123456789abcdef")
for _, v := range []string{"abc", "-1", "366"} {
t.Setenv("OMNEX_VITRINE_GRACE_DAYS", v)
if _, err := Load(); err == nil {
t.Errorf("OMNEX_VITRINE_GRACE_DAYS=%q accepté", v)
}
}
t.Setenv("OMNEX_VITRINE_GRACE_DAYS", "0")
if cfg, err := Load(); err != nil || cfg.VitrineGraceDays != 0 {
t.Errorf("grâce de 0 jour refusée ou mal lue : %v %d", err, cfg.VitrineGraceDays)
}
}