116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// clearConfigEnv unsets all environment variables that Load() reads,
|
|
// so tests start from a clean slate.
|
|
func clearConfigEnv(t *testing.T) {
|
|
t.Helper()
|
|
envVars := []string{
|
|
"DATABASE_URL", "PUBLIC_ADDR", "BASE_URL",
|
|
"SESSION_SECRET", "FORGEJO_URL", "FORGEJO_API_TOKEN",
|
|
"POSTMARK_SERVER_TOKEN", "POSTMARK_FROM_EMAIL",
|
|
"GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_SECRET",
|
|
"MICROSOFT_CLIENT_ID", "MICROSOFT_CLIENT_SECRET", "MICROSOFT_TENANT_ID",
|
|
"APPLE_CLIENT_ID", "APPLE_TEAM_ID", "APPLE_KEY_ID", "APPLE_KEY_PATH",
|
|
}
|
|
for _, v := range envVars {
|
|
os.Unsetenv(v)
|
|
}
|
|
}
|
|
|
|
func TestLoad_MissingDatabaseURL(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
t.Setenv("SESSION_SECRET", "test-session-secret-that-is-32ch")
|
|
// DATABASE_URL is not set
|
|
|
|
_, err := Load()
|
|
if err == nil {
|
|
t.Fatal("expected error when DATABASE_URL is missing, got nil")
|
|
}
|
|
|
|
expected := "DATABASE_URL is required"
|
|
if err.Error() != expected {
|
|
t.Errorf("expected error %q, got %q", expected, err.Error())
|
|
}
|
|
}
|
|
|
|
func TestLoad_MissingSessionSecret(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
t.Setenv("DATABASE_URL", "postgres://localhost/test")
|
|
// SESSION_SECRET is not set
|
|
|
|
_, err := Load()
|
|
if err == nil {
|
|
t.Fatal("expected error when SESSION_SECRET is missing, got nil")
|
|
}
|
|
|
|
expected := "SESSION_SECRET must be at least 32 characters"
|
|
if err.Error() != expected {
|
|
t.Errorf("expected error %q, got %q", expected, err.Error())
|
|
}
|
|
}
|
|
|
|
func TestLoad_Success(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
t.Setenv("DATABASE_URL", "postgres://localhost/test")
|
|
t.Setenv("SESSION_SECRET", "test-session-secret-that-is-32ch")
|
|
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
|
|
if cfg.DatabaseURL != "postgres://localhost/test" {
|
|
t.Errorf("expected DatabaseURL %q, got %q", "postgres://localhost/test", cfg.DatabaseURL)
|
|
}
|
|
if cfg.SessionSecret != "test-session-secret-that-is-32ch" {
|
|
t.Errorf("expected SessionSecret %q, got %q", "test-session-secret-that-is-32ch", cfg.SessionSecret)
|
|
}
|
|
}
|
|
|
|
func TestLoad_DefaultValues(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
t.Setenv("DATABASE_URL", "postgres://localhost/test")
|
|
t.Setenv("SESSION_SECRET", "test-session-secret-that-is-32ch")
|
|
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
|
|
if cfg.PublicAddr != ":8080" {
|
|
t.Errorf("expected default PublicAddr %q, got %q", ":8080", cfg.PublicAddr)
|
|
}
|
|
if cfg.BaseURL != "http://localhost:8080" {
|
|
t.Errorf("expected default BaseURL %q, got %q", "http://localhost:8080", cfg.BaseURL)
|
|
}
|
|
if cfg.MicrosoftTenantID != "common" {
|
|
t.Errorf("expected default MicrosoftTenantID %q, got %q", "common", cfg.MicrosoftTenantID)
|
|
}
|
|
}
|
|
|
|
func TestLoad_OverrideDefaults(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
t.Setenv("DATABASE_URL", "postgres://localhost/test")
|
|
t.Setenv("SESSION_SECRET", "test-session-secret-that-is-32ch")
|
|
t.Setenv("PUBLIC_ADDR", ":9090")
|
|
t.Setenv("BASE_URL", "https://example.com")
|
|
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
|
|
if cfg.PublicAddr != ":9090" {
|
|
t.Errorf("expected PublicAddr %q, got %q", ":9090", cfg.PublicAddr)
|
|
}
|
|
if cfg.BaseURL != "https://example.com" {
|
|
t.Errorf("expected BaseURL %q, got %q", "https://example.com", cfg.BaseURL)
|
|
}
|
|
}
|
|
|