package dobotaiops import ( "errors" "strings" "testing" "time" ) func TestLoadSettingsConsumesIndependentConnectionLimits(t *testing.T) { env := map[string]string{ "DOBOTAI_OPS_READ_ENABLED": "true", "DOBOTAI_OPS_READ_DSN_FILE": "/run/secrets/source", "DOBOTAI_OPS_TENANT_KEY": "approved-tenant", "DOBOTAI_OPS_DIAL_MODE": "ssh", "DOBOTAI_OPS_SSH_TARGET": "approved-hop", "DOBOTAI_OPS_CONNECT_TIMEOUT": "7s", "DOBOTAI_OPS_QUERY_TIMEOUT": "3s", "DOBOTAI_OPS_MAX_CONNS": "3", "DOBOTAI_OPS_MIN_CONNS": "1", } s := LoadSettings(func(k string) string { return env[k] }, "postgres://governance@elsewhere/governance") if !s.Enabled || s.Problem != "" || s.TenantKey != "approved-tenant" { t.Fatalf("settings were not loaded: %+v", s) } if s.Connection.ConnectTimeout != 7*time.Second || s.Connection.QueryTimeout != 3*time.Second || s.Connection.MaxConns != 3 || s.Connection.MinConns != 1 { t.Fatalf("environment overrides ignored: %+v", s.Connection) } } func TestLoadSettingsIsDisabledByDefaultAndContainsInvalidSourceConfiguration(t *testing.T) { if s := LoadSettings(func(string) string { return "" }, ""); s.Enabled || s.Problem != "" { t.Fatalf("not disabled by default: %+v", s) } for _, key := range []string{"DOBOTAI_OPS_READ_ENABLED", "DOBOTAI_OPS_CONNECT_TIMEOUT", "DOBOTAI_OPS_QUERY_TIMEOUT", "DOBOTAI_OPS_MAX_CONNS", "DOBOTAI_OPS_MIN_CONNS"} { env := map[string]string{"DOBOTAI_OPS_READ_ENABLED": "true", "DOBOTAI_OPS_READ_DSN_FILE": "/run/secrets/source", "DOBOTAI_OPS_TENANT_KEY": "approved-tenant"} env[key] = "invalid-sensitive-value" s := LoadSettings(func(k string) string { return env[k] }, "postgres://governance@elsewhere/governance") if s.Problem == "" || strings.Contains(s.Problem, "invalid-sensitive-value") { t.Fatalf("%s must fail closed without echoing its value: %+v", key, s) } } s := LoadSettings(func(k string) string { if k == "DOBOTAI_OPS_READ_ENABLED" { return "true" } return "" }, "") if s.Problem == "" { t.Fatal("missing secret and tenant must block source access") } } func TestSecretReaderDoesNotSelectAnUnrelatedDatabaseURL(t *testing.T) { unrelated := testDSN("writer", "not"+"a-secret", "other", "other_db") wanted := testDSN("reader", "fixture"+"only", "source", "source_db") got, ok := parseDSNLine("DATABASE_URL=" + unrelated + "\nDOBOTAI_OPS_READ_DSN=" + wanted) if !ok || got != wanted { t.Fatal("must select only the dedicated read DSN key") } if _, ok := parseDSNLine("DATABASE_URL=" + unrelated); ok { t.Fatal("must not borrow another application's credential") } } func TestMalformedGovernanceDSNDoesNotDisableIdentityGuard(t *testing.T) { err := GuardGovernanceDatabase("postgres://reader@source/dobotai_prod", "invalid-main-dsn") if !errors.Is(err, ErrUnavailable) { t.Fatalf("malformed main identity must fail closed: %v", err) } }