43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"lincodex/backend/config"
|
|
)
|
|
|
|
func TestSeedDemoPolicySkipsWithoutStartupMigrations(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
settings config.Config
|
|
}{
|
|
// An unreachable address proves that no connection is attempted.
|
|
{name: "startup migrations disabled", settings: config.Config{DatabaseMigrateOnStart: false, DatabaseURL: "postgres://app@db:not-a-port/app"}},
|
|
{name: "database not configured", settings: config.Config{DatabaseMigrateOnStart: true}},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if err := seedDemoPolicy(t.Context(), test.settings, slog.New(slog.DiscardHandler)); err != nil {
|
|
t.Fatalf("seedDemoPolicy() error = %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSeedDemoPolicyHidesConnectionStringOnFailure(t *testing.T) {
|
|
const password = "seed-secret-password"
|
|
settings := config.Config{
|
|
DatabaseMigrateOnStart: true,
|
|
DatabaseURL: "postgres://app:" + password + "@db:not-a-port/app",
|
|
}
|
|
err := seedDemoPolicy(t.Context(), settings, slog.New(slog.DiscardHandler))
|
|
if err == nil {
|
|
t.Fatal("seedDemoPolicy() accepted an invalid DATABASE_URL")
|
|
}
|
|
if strings.Contains(err.Error(), password) || strings.Contains(err.Error(), "db:not-a-port") {
|
|
t.Fatalf("seedDemoPolicy() error exposes the connection string: %v", err)
|
|
}
|
|
}
|