80 lines
1.7 KiB
Go
80 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoad_Defaults(t *testing.T) {
|
|
// Clear env to test defaults
|
|
envKeys := []string{"DATABASE_URL", "JWT_SECRET", "PORT", "RESEND_API_KEY", "FROM_EMAIL", "FROM_NAME", "APP_URL", "TELEGRAM_BOT_TOKEN"}
|
|
originals := make(map[string]string)
|
|
for _, k := range envKeys {
|
|
originals[k] = os.Getenv(k)
|
|
os.Unsetenv(k)
|
|
}
|
|
defer func() {
|
|
for k, v := range originals {
|
|
if v != "" {
|
|
os.Setenv(k, v)
|
|
}
|
|
}
|
|
}()
|
|
|
|
cfg := Load()
|
|
|
|
if cfg.Port != "8080" {
|
|
t.Errorf("expected default port 8080, got %s", cfg.Port)
|
|
}
|
|
if cfg.JWTSecret != "change-me-in-production" {
|
|
t.Errorf("expected default JWT secret, got %s", cfg.JWTSecret)
|
|
}
|
|
if cfg.FromEmail != "noreply@digital-home.site" {
|
|
t.Errorf("expected default from email, got %s", cfg.FromEmail)
|
|
}
|
|
}
|
|
|
|
func TestLoad_FromEnv(t *testing.T) {
|
|
os.Setenv("PORT", "9090")
|
|
os.Setenv("JWT_SECRET", "my-secret")
|
|
defer func() {
|
|
os.Unsetenv("PORT")
|
|
os.Unsetenv("JWT_SECRET")
|
|
}()
|
|
|
|
cfg := Load()
|
|
|
|
if cfg.Port != "9090" {
|
|
t.Errorf("expected port 9090, got %s", cfg.Port)
|
|
}
|
|
if cfg.JWTSecret != "my-secret" {
|
|
t.Errorf("expected JWT secret my-secret, got %s", cfg.JWTSecret)
|
|
}
|
|
}
|
|
|
|
func TestGetEnv(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
envValue string
|
|
def string
|
|
want string
|
|
}{
|
|
{"returns env value", "TEST_KEY_1", "env_val", "default", "env_val"},
|
|
{"returns default when empty", "TEST_KEY_2", "", "default", "default"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if tt.envValue != "" {
|
|
os.Setenv(tt.key, tt.envValue)
|
|
defer os.Unsetenv(tt.key)
|
|
}
|
|
got := getEnv(tt.key, tt.def)
|
|
if got != tt.want {
|
|
t.Errorf("getEnv(%s) = %s, want %s", tt.key, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|