129 lines
4.2 KiB
Go
129 lines
4.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestParseConfigDefaultsToLocalOutsideProduction(t *testing.T) {
|
|
cfg, err := ParseConfig(env(map[string]string{"NODE_ENV": "development"}), os.ReadFile)
|
|
if err != nil {
|
|
t.Fatalf("ParseConfig() error = %v", err)
|
|
}
|
|
if cfg.Backend != BackendLocal {
|
|
t.Fatalf("Backend = %q, want %q", cfg.Backend, BackendLocal)
|
|
}
|
|
}
|
|
|
|
func TestParseConfigRequiresExplicitBackendInProduction(t *testing.T) {
|
|
_, err := ParseConfig(env(map[string]string{"NODE_ENV": "production"}), os.ReadFile)
|
|
if err == nil || !strings.Contains(err.Error(), "ZHINIAN_DATA_BACKEND") {
|
|
t.Fatalf("ParseConfig() error = %v, want backend validation error", err)
|
|
}
|
|
}
|
|
|
|
func TestParseConfigRequiresPostgresURL(t *testing.T) {
|
|
_, err := ParseConfig(env(map[string]string{"ZHINIAN_DATA_BACKEND": "postgres"}), os.ReadFile)
|
|
if err == nil || !strings.Contains(err.Error(), "DATABASE_URL") {
|
|
t.Fatalf("ParseConfig() error = %v, want DATABASE_URL error", err)
|
|
}
|
|
}
|
|
|
|
func TestParseConfigAcceptsOnlyPostgresSchemesAndRejectsUnsupportedSSLQueryParameters(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
url string
|
|
}{
|
|
{name: "wrong scheme", url: "https://db.example/app"},
|
|
{name: "mixed case unsupported ssl parameter", url: "postgresql://db.example/app?SSLcert=x"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := ParseConfig(env(map[string]string{
|
|
"ZHINIAN_DATA_BACKEND": "postgres",
|
|
"DATABASE_URL": tt.url,
|
|
}), os.ReadFile)
|
|
if err == nil {
|
|
t.Fatal("ParseConfig() error = nil, want validation error")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseConfigForcesPlaintextAndDoesNotReadURLCA(t *testing.T) {
|
|
readCalled := false
|
|
cfg, err := ParseConfig(env(map[string]string{
|
|
"ZHINIAN_DATA_BACKEND": "postgres",
|
|
"DATABASE_URL": "postgresql://db.example/app?connect_timeout=5&sslmode=verify-full&sslrootcert=%2Fetc%2Fzhinian%2Frds%2Fca.pem",
|
|
}), func(string) ([]byte, error) {
|
|
readCalled = true
|
|
return nil, os.ErrNotExist
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ParseConfig() error = %v", err)
|
|
}
|
|
if readCalled {
|
|
t.Fatal("ParseConfig() read sslrootcert, want plaintext configuration without CA access")
|
|
}
|
|
if cfg.SSLMode != SSLDisable || cfg.TLSConfig != nil {
|
|
t.Fatalf("TLS configuration = mode %q config %v, want disabled and nil", cfg.SSLMode, cfg.TLSConfig)
|
|
}
|
|
parsed, err := url.Parse(cfg.DatabaseURL)
|
|
if err != nil {
|
|
t.Fatalf("parse normalized DATABASE_URL: %v", err)
|
|
}
|
|
if got := parsed.Query().Get("sslmode"); got != "disable" {
|
|
t.Fatalf("normalized sslmode = %q, want disable", got)
|
|
}
|
|
if parsed.Query().Has("sslrootcert") {
|
|
t.Fatal("normalized DATABASE_URL retains sslrootcert")
|
|
}
|
|
if got := parsed.Query().Get("connect_timeout"); got != "5" {
|
|
t.Fatalf("normalized connect_timeout = %q, want 5", got)
|
|
}
|
|
}
|
|
|
|
func TestParseConfigDefaultsAndNumericValidation(t *testing.T) {
|
|
cfg, err := ParseConfig(env(map[string]string{
|
|
"ZHINIAN_DATA_BACKEND": "postgres",
|
|
"DATABASE_URL": "postgres://db.example/app",
|
|
}), os.ReadFile)
|
|
if err != nil {
|
|
t.Fatalf("ParseConfig() error = %v", err)
|
|
}
|
|
if cfg.PoolMax != 10 || cfg.IdleTimeout != 30*time.Second || cfg.ConnectionTimeout != 10*time.Second || cfg.StatementTimeout != 30*time.Second {
|
|
t.Fatalf("unexpected defaults: %+v", cfg)
|
|
}
|
|
if cfg.ApplicationName != "zhinian-go" || cfg.SSLMode != SSLDisable {
|
|
t.Fatalf("unexpected identity/TLS defaults: %+v", cfg)
|
|
}
|
|
if cfg.TLSConfig != nil {
|
|
t.Fatal("default PostgreSQL configuration must not negotiate TLS")
|
|
}
|
|
|
|
for name, value := range map[string]string{
|
|
"DATABASE_POOL_MAX": "0",
|
|
"DATABASE_IDLE_TIMEOUT_MS": "-1",
|
|
"DATABASE_CONNECTION_TIMEOUT_MS": "0",
|
|
"DATABASE_STATEMENT_TIMEOUT_MS": "nope",
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
_, err := ParseConfig(env(map[string]string{
|
|
"ZHINIAN_DATA_BACKEND": "postgres",
|
|
"DATABASE_URL": "postgres://db.example/app",
|
|
name: value,
|
|
}), os.ReadFile)
|
|
if err == nil || !strings.Contains(err.Error(), name) {
|
|
t.Fatalf("ParseConfig() error = %v, want %s validation error", err, name)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func env(values map[string]string) Getenv {
|
|
return func(name string) string { return values[name] }
|
|
}
|