138 lines
4.9 KiB
Go
138 lines
4.9 KiB
Go
package releaseupdate
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
)
|
|
|
|
func TestResolveDatabasePassword(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
env map[string]string
|
|
envFile map[string]string
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{name: "environment wins over the env file", env: map[string]string{"PGPASSWORD": "from-env"}, envFile: map[string]string{"PGPASSWORD": "from-file"}, want: "from-env"},
|
|
{name: "env file supplies it when the environment does not", envFile: map[string]string{"PGPASSWORD": "from-file"}, want: "from-file"},
|
|
{name: "neither holds one", wantErr: true},
|
|
{name: "empty entries do not count", env: map[string]string{"PGPASSWORD": ""}, envFile: map[string]string{"PGPASSWORD": ""}, wantErr: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
getenv := func(key string) string { return tt.env[key] }
|
|
got, err := resolveDatabasePassword(getenv, tt.envFile)
|
|
if tt.wantErr {
|
|
if err == nil || !strings.Contains(err.Error(), DatabasePasswordEnv) {
|
|
t.Fatalf("resolveDatabasePassword error = %v, want one naming %s", err, DatabasePasswordEnv)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("resolveDatabasePassword: %v", err)
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("resolveDatabasePassword = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestURLCarriesPassword(t *testing.T) {
|
|
tests := []struct {
|
|
url string
|
|
want bool
|
|
}{
|
|
{"postgres://archie:secret@localhost/archie", true},
|
|
{"postgres://archie@/archie?host=/run/archie/postgres", false},
|
|
{"postgres://archie@localhost/archie", false},
|
|
{"host=localhost user=archie password=secret", true},
|
|
{"host=localhost user=archie", false},
|
|
{"", false},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := urlCarriesPassword(tt.url); got != tt.want {
|
|
t.Errorf("urlCarriesPassword(%q) = %v, want %v", tt.url, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoadEnvCredentials(t *testing.T) {
|
|
writeConfig := func(t *testing.T, databaseURL string) (configPath string) {
|
|
t.Helper()
|
|
configPath = filepath.Join(t.TempDir(), "config.toml")
|
|
if err := os.WriteFile(configPath, []byte("database_url = \""+databaseURL+"\"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return configPath
|
|
}
|
|
|
|
t.Run("env file beside the config supplies the password", func(t *testing.T) {
|
|
t.Setenv(DatabasePasswordEnv, "")
|
|
configPath := writeConfig(t, "postgres://archie@/archie?host=/run/archie/postgres")
|
|
envPath := config.EnvFilePath(configPath)
|
|
body := "# secrets\nPGPASSWORD='from-file'\nARCHIE_TEST_TOKEN='file-token'\n"
|
|
if err := os.WriteFile(envPath, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tx := &Transaction{ConfigPath: configPath}
|
|
if err := tx.loadEnvCredentials(); err != nil {
|
|
t.Fatalf("loadEnvCredentials: %v", err)
|
|
}
|
|
if got := os.Getenv(DatabasePasswordEnv); got != "from-file" {
|
|
t.Errorf("%s = %q, want %q", DatabasePasswordEnv, got, "from-file")
|
|
}
|
|
if got := os.Getenv("ARCHIE_TEST_TOKEN"); got != "file-token" {
|
|
t.Errorf("ARCHIE_TEST_TOKEN = %q, want %q", got, "file-token")
|
|
}
|
|
})
|
|
|
|
t.Run("the environment wins over the env file", func(t *testing.T) {
|
|
t.Setenv(DatabasePasswordEnv, "from-env")
|
|
t.Setenv("ARCHIE_TEST_TOKEN", "env-token")
|
|
configPath := writeConfig(t, "postgres://archie@/archie?host=/run/archie/postgres")
|
|
if err := os.WriteFile(config.EnvFilePath(configPath), []byte("PGPASSWORD='from-file'\nARCHIE_TEST_TOKEN='file-token'\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tx := &Transaction{ConfigPath: configPath}
|
|
if err := tx.loadEnvCredentials(); err != nil {
|
|
t.Fatalf("loadEnvCredentials: %v", err)
|
|
}
|
|
if got := os.Getenv(DatabasePasswordEnv); got != "from-env" {
|
|
t.Errorf("%s = %q, want %q", DatabasePasswordEnv, got, "from-env")
|
|
}
|
|
if got := os.Getenv("ARCHIE_TEST_TOKEN"); got != "env-token" {
|
|
t.Errorf("ARCHIE_TEST_TOKEN = %q, want %q", got, "env-token")
|
|
}
|
|
})
|
|
|
|
t.Run("a database_url with its own password needs no env entry", func(t *testing.T) {
|
|
t.Setenv(DatabasePasswordEnv, "")
|
|
configPath := writeConfig(t, "postgres://archie:secret@localhost/archie")
|
|
tx := &Transaction{ConfigPath: configPath}
|
|
if err := tx.loadEnvCredentials(); err != nil {
|
|
t.Fatalf("loadEnvCredentials: %v", err)
|
|
}
|
|
if got := os.Getenv(DatabasePasswordEnv); got != "" {
|
|
t.Errorf("%s = %q, want unset", DatabasePasswordEnv, got)
|
|
}
|
|
})
|
|
|
|
t.Run("no password anywhere is a clear error", func(t *testing.T) {
|
|
t.Setenv(DatabasePasswordEnv, "")
|
|
configPath := writeConfig(t, "postgres://archie@/archie?host=/run/archie/postgres")
|
|
tx := &Transaction{ConfigPath: configPath}
|
|
err := tx.loadEnvCredentials()
|
|
if err == nil {
|
|
t.Fatal("loadEnvCredentials succeeded without any password")
|
|
}
|
|
if !strings.Contains(err.Error(), DatabasePasswordEnv) || !strings.Contains(err.Error(), config.EnvFilePath(configPath)) {
|
|
t.Errorf("error = %v, want it to name %s and the env file path", err, DatabasePasswordEnv)
|
|
}
|
|
})
|
|
}
|