97 lines
2.4 KiB
Go
97 lines
2.4 KiB
Go
package config
|
|
|
|
import "testing"
|
|
|
|
func clearEnv(t *testing.T) {
|
|
t.Helper()
|
|
keys := []string{
|
|
"DATABASE_URL", "HTTP_PORT", "BASE_URL", "SESSION_SECRET", "UPLOAD_DIR",
|
|
"MAX_UPLOAD_MB", "MEILI_URL", "MEILI_KEY", "SMTP_HOST", "SMTP_PORT",
|
|
"SMTP_USER", "SMTP_PASS", "SMTP_FROM", "GITHUB_CLIENT_ID", "GITHUB_CLIENT_SECRET",
|
|
"GITEA_URL", "GITEA_CLIENT_ID", "GITEA_CLIENT_SECRET", "CULL_AFTER_DAYS", "LOG_LEVEL",
|
|
}
|
|
for _, k := range keys {
|
|
t.Setenv(k, "")
|
|
}
|
|
}
|
|
|
|
func validRequired(t *testing.T) {
|
|
t.Helper()
|
|
t.Setenv("DATABASE_URL", "postgres://localhost/solopm")
|
|
t.Setenv("BASE_URL", "http://localhost:8080")
|
|
t.Setenv("SESSION_SECRET", "01234567890123456789012345678901")
|
|
}
|
|
|
|
func TestLoad_MissingRequired(t *testing.T) {
|
|
clearEnv(t)
|
|
_, err := Load()
|
|
if err == nil {
|
|
t.Fatal("expected error for missing required vars, got nil")
|
|
}
|
|
}
|
|
|
|
func TestLoad_ShortSessionSecret(t *testing.T) {
|
|
clearEnv(t)
|
|
t.Setenv("DATABASE_URL", "postgres://localhost/solopm")
|
|
t.Setenv("BASE_URL", "http://localhost:8080")
|
|
t.Setenv("SESSION_SECRET", "tooshort")
|
|
_, err := Load()
|
|
if err == nil {
|
|
t.Fatal("expected error for short SESSION_SECRET, got nil")
|
|
}
|
|
}
|
|
|
|
func TestLoad_Defaults(t *testing.T) {
|
|
clearEnv(t)
|
|
validRequired(t)
|
|
c, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if c.HTTPPort != "8080" {
|
|
t.Errorf("HTTPPort = %q, want 8080", c.HTTPPort)
|
|
}
|
|
if c.UploadDir != "./uploads" {
|
|
t.Errorf("UploadDir = %q, want ./uploads", c.UploadDir)
|
|
}
|
|
if c.MaxUploadMB != 25 {
|
|
t.Errorf("MaxUploadMB = %d, want 25", c.MaxUploadMB)
|
|
}
|
|
if c.CullAfterDays != 30 {
|
|
t.Errorf("CullAfterDays = %d, want 30", c.CullAfterDays)
|
|
}
|
|
if c.LogLevel != "info" {
|
|
t.Errorf("LogLevel = %q, want info", c.LogLevel)
|
|
}
|
|
if c.SearchEnabled() {
|
|
t.Error("SearchEnabled() = true, want false when MEILI_URL/KEY unset")
|
|
}
|
|
if c.EmailEnabled() {
|
|
t.Error("EmailEnabled() = true, want false when SMTP_HOST unset")
|
|
}
|
|
}
|
|
|
|
func TestLoad_SearchEnabled(t *testing.T) {
|
|
clearEnv(t)
|
|
validRequired(t)
|
|
t.Setenv("MEILI_URL", "http://meilisearch:7700")
|
|
t.Setenv("MEILI_KEY", "key")
|
|
c, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !c.SearchEnabled() {
|
|
t.Error("SearchEnabled() = false, want true when MEILI_URL/KEY set")
|
|
}
|
|
}
|
|
|
|
func TestLoad_InvalidInt(t *testing.T) {
|
|
clearEnv(t)
|
|
validRequired(t)
|
|
t.Setenv("MAX_UPLOAD_MB", "not-a-number")
|
|
_, err := Load()
|
|
if err == nil {
|
|
t.Fatal("expected error for non-integer MAX_UPLOAD_MB, got nil")
|
|
}
|
|
}
|