75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
|
|
package config
|
||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestLoad_Defaults(t *testing.T) {
|
||
|
|
// Clear env vars
|
||
|
|
os.Unsetenv("PORT")
|
||
|
|
os.Unsetenv("DB_URL")
|
||
|
|
os.Unsetenv("JWT_SECRET")
|
||
|
|
|
||
|
|
// JWT_SECRET is required, so this should panic
|
||
|
|
assert.Panics(t, func() {
|
||
|
|
Load()
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLoad_WithEnv(t *testing.T) {
|
||
|
|
os.Setenv("JWT_SECRET", "test-secret")
|
||
|
|
os.Setenv("PORT", "8080")
|
||
|
|
os.Setenv("DB_URL", "postgres://test")
|
||
|
|
defer func() {
|
||
|
|
os.Unsetenv("JWT_SECRET")
|
||
|
|
os.Unsetenv("PORT")
|
||
|
|
os.Unsetenv("DB_URL")
|
||
|
|
}()
|
||
|
|
|
||
|
|
cfg := Load()
|
||
|
|
assert.Equal(t, "8080", cfg.Port)
|
||
|
|
assert.Equal(t, "postgres://test", cfg.DBURL)
|
||
|
|
assert.Equal(t, "test-secret", cfg.JWTSecret)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLoad_CORSOrigins(t *testing.T) {
|
||
|
|
os.Setenv("JWT_SECRET", "test-secret")
|
||
|
|
os.Setenv("CORS_ORIGINS", "http://localhost:3000, http://localhost:3001")
|
||
|
|
defer func() {
|
||
|
|
os.Unsetenv("JWT_SECRET")
|
||
|
|
os.Unsetenv("CORS_ORIGINS")
|
||
|
|
}()
|
||
|
|
|
||
|
|
cfg := Load()
|
||
|
|
assert.Equal(t, []string{"http://localhost:3000", "http://localhost:3001"}, cfg.CORSOrigins)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLoad_KafkaBrokers(t *testing.T) {
|
||
|
|
os.Setenv("JWT_SECRET", "test-secret")
|
||
|
|
os.Setenv("KAFKA_BROKERS", "kafka1:9092,kafka2:9092")
|
||
|
|
defer func() {
|
||
|
|
os.Unsetenv("JWT_SECRET")
|
||
|
|
os.Unsetenv("KAFKA_BROKERS")
|
||
|
|
}()
|
||
|
|
|
||
|
|
cfg := Load()
|
||
|
|
assert.Equal(t, []string{"kafka1:9092", "kafka2:9092"}, cfg.KafkaBrokers)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestRequireEnv(t *testing.T) {
|
||
|
|
os.Setenv("TEST_VAR", "test-value")
|
||
|
|
defer os.Unsetenv("TEST_VAR")
|
||
|
|
|
||
|
|
assert.Equal(t, "test-value", requireEnv("TEST_VAR"))
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestRequireEnv_Missing(t *testing.T) {
|
||
|
|
os.Unsetenv("MISSING_VAR")
|
||
|
|
assert.Panics(t, func() {
|
||
|
|
requireEnv("MISSING_VAR")
|
||
|
|
})
|
||
|
|
}
|