54 lines
1.7 KiB
Go
54 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSaveAndLoadInstalledConfiguration(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "state", "config.json")
|
|
t.Setenv("SEA_CONFIG_FILE", path)
|
|
t.Setenv("SEA_SESSION_SECRET", "")
|
|
t.Setenv("SEA_INTERNAL_AUTH_TOKEN", strings.Repeat("i", 48))
|
|
cfg := NewInstalled(
|
|
"sea_vhost:secret@tcp(127.0.0.1:3306)/sea_vhost?parseTime=true",
|
|
"012345678901234567890123456789012345678901234567",
|
|
"hosts.example.com", "console.example.com", "ftp.example.com", []string{"panel.example.com"}, false,
|
|
)
|
|
if err := Save(cfg); err != nil {
|
|
t.Fatalf("Save() error = %v", err)
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(data), "panel_api_key") {
|
|
t.Fatal("Web API configuration contains a panel API key")
|
|
}
|
|
loaded, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
if loaded.DatabaseDSN != cfg.DatabaseDSN || loaded.PanelBrokerSocket != cfg.PanelBrokerSocket || loaded.RootDomain != cfg.RootDomain || loaded.ManagementDomain != cfg.ManagementDomain || loaded.FTPDomain != cfg.FTPDomain {
|
|
t.Fatalf("Load() did not preserve installed configuration")
|
|
}
|
|
if loaded.PanelMock || loaded.AgentMock || loaded.DomainApproval {
|
|
t.Fatalf("production flags were not preserved")
|
|
}
|
|
if err := Save(cfg); err == nil {
|
|
t.Fatal("Save() unexpectedly overwrote an installed configuration")
|
|
}
|
|
}
|
|
|
|
func TestLoadRequiresSetupWithoutConfigOrLegacySecret(t *testing.T) {
|
|
t.Setenv("SEA_CONFIG_FILE", filepath.Join(t.TempDir(), "missing.json"))
|
|
t.Setenv("SEA_SESSION_SECRET", "")
|
|
_, err := Load()
|
|
if !errors.Is(err, ErrSetupRequired) {
|
|
t.Fatalf("Load() error = %v, want ErrSetupRequired", err)
|
|
}
|
|
}
|