37 lines
1.4 KiB
Go
37 lines
1.4 KiB
Go
package api
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateConfigValues(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
mode string
|
|
secret string
|
|
driver string
|
|
source string
|
|
wantErr bool
|
|
forbidden string
|
|
}{
|
|
{name: "production configuration", mode: "prod", secret: strings.Repeat("s", 32), driver: "postgres", source: "host=database dbname=sense", wantErr: false},
|
|
{name: "missing database source", mode: "prod", secret: strings.Repeat("s", 32), driver: "postgres", source: "", wantErr: true},
|
|
{name: "rejects non PostgreSQL", mode: "prod", secret: strings.Repeat("s", 32), driver: "mysql", source: "sensitive-source", wantErr: true, forbidden: "sensitive-source"},
|
|
{name: "short production secret", mode: "prod", secret: "short", driver: "postgres", source: "sensitive-source", wantErr: true, forbidden: "sensitive-source"},
|
|
{name: "development permits short secret", mode: "dev", secret: "short", driver: "postgres", source: "host=database dbname=sense", wantErr: false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := validateConfigValues(tt.mode, tt.secret, tt.driver, tt.source)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Fatalf("validateConfigValues() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
if err != nil && tt.forbidden != "" && strings.Contains(err.Error(), tt.forbidden) {
|
|
t.Fatalf("error leaked database source: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|