package config import ( "os" "path/filepath" "testing" ) const testdataDir = "testdata" func testdataPath(name string) string { return filepath.Join(testdataDir, name) } // --- Config.Load tests --- func TestLoad_ValidConfig(t *testing.T) { cfg, err := Load(testdataPath("valid-config.yaml")) if err != nil { t.Fatalf("expected no error, got: %v", err) } if cfg.Server.Host != "0.0.0.0" { t.Errorf("expected Server.Host '0.0.0.0', got %q", cfg.Server.Host) } if cfg.Server.Port != 8080 { t.Errorf("expected Server.Port 8080, got %d", cfg.Server.Port) } if cfg.Admin.SecretToken != "test-admin-token-123" { t.Errorf("expected Admin.SecretToken 'test-admin-token-123', got %q", cfg.Admin.SecretToken) } if cfg.Database.Type != "sqlite" { t.Errorf("expected Database.Type 'sqlite', got %q", cfg.Database.Type) } if cfg.Database.Path != "/tmp/nextwks-test.db" { t.Errorf("expected Database.Path '/tmp/nextwks-test.db', got %q", cfg.Database.Path) } if cfg.Authelia.Host != "http://127.0.0.1:9091" { t.Errorf("expected Authelia.Host 'http://127.0.0.1:9091', got %q", cfg.Authelia.Host) } if cfg.Session.Secret != "test-session-secret" { t.Errorf("expected Session.Secret 'test-session-secret', got %q", cfg.Session.Secret) } if cfg.Session.ExpiryMinutes != 60 { t.Errorf("expected Session.ExpiryMinutes 60, got %d", cfg.Session.ExpiryMinutes) } if cfg.SMTP.Host != "mail.example.com" { t.Errorf("expected SMTP.Host 'mail.example.com', got %q", cfg.SMTP.Host) } if cfg.SMTP.Port != 587 { t.Errorf("expected SMTP.Port 587, got %d", cfg.SMTP.Port) } if cfg.SMTP.From != "noreply@example.com" { t.Errorf("expected SMTP.From 'noreply@example.com', got %q", cfg.SMTP.From) } } func TestLoad_MissingFile(t *testing.T) { _, err := Load(testdataPath("nonexistent-file.yaml")) if err == nil { t.Fatal("expected error for missing file, got nil") } } func TestLoad_InvalidYAML(t *testing.T) { tmpFile := filepath.Join(t.TempDir(), "invalid.yaml") if err := os.WriteFile(tmpFile, []byte("invalid: yaml: \n bad: ["), 0644); err != nil { t.Fatalf("failed to write temp file: %v", err) } _, err := Load(tmpFile) if err == nil { t.Fatal("expected error for invalid YAML, got nil") } } func TestLoad_EmptyFile(t *testing.T) { tmpFile := filepath.Join(t.TempDir(), "empty.yaml") if err := os.WriteFile(tmpFile, []byte(""), 0644); err != nil { t.Fatalf("failed to write temp file: %v", err) } cfg, err := Load(tmpFile) if err != nil { t.Fatalf("expected no error for empty file, got: %v", err) } // Empty file should yield zero-value config if cfg.Server.Port != 0 { t.Errorf("expected zero-value Port, got %d", cfg.Server.Port) } } // --- Config.AutheliaSessionSecret tests --- func TestAutheliaSessionSecret_Valid(t *testing.T) { secret, err := AutheliaSessionSecret(testdataPath("valid-authelia-config.yaml")) if err != nil { t.Fatalf("expected no error, got: %v", err) } if secret != "authelia-test-session-secret" { t.Errorf("expected secret 'authelia-test-session-secret', got %q", secret) } } func TestAutheliaSessionSecret_MissingFile(t *testing.T) { _, err := AutheliaSessionSecret(testdataPath("nonexistent-authelia-config.yaml")) if err == nil { t.Fatal("expected error for missing file, got nil") } } func TestAutheliaSessionSecret_NoSecretField(t *testing.T) { _, err := AutheliaSessionSecret(testdataPath("no-session-authelia-config.yaml")) if err == nil { t.Fatal("expected error when session.secret is missing, got nil") } } func TestAutheliaSessionSecret_EmptySecret(t *testing.T) { tmpFile := filepath.Join(t.TempDir(), "authelia-empty-secret.yaml") content := []byte("session:\n name: test\n secret: \"\"\n") if err := os.WriteFile(tmpFile, content, 0644); err != nil { t.Fatalf("failed to write temp file: %v", err) } _, err := AutheliaSessionSecret(tmpFile) if err == nil { t.Fatal("expected error for empty session.secret, got nil") } }