Перенести пользователей в PostgreSQL и добавить пароли
This commit is contained in:
@@ -2,6 +2,9 @@ package config_test
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -15,8 +18,29 @@ func env(values map[string]string) func(string) (string, bool) {
|
||||
}
|
||||
}
|
||||
|
||||
func secretFile(t *testing.T, content string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "secret")
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
const databaseURL = "postgres://app:s3cret@db:5432/messenger"
|
||||
|
||||
// required holds the variables without defaults, merged with extra.
|
||||
func required(t *testing.T, extra map[string]string) map[string]string {
|
||||
t.Helper()
|
||||
values := map[string]string{"DATABASE_URL_FILE": secretFile(t, databaseURL+"\n")}
|
||||
for k, v := range extra {
|
||||
values[k] = v
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func TestDefaults(t *testing.T) {
|
||||
cfg, err := config.Load(env(nil))
|
||||
cfg, err := config.Load(env(required(t, nil)))
|
||||
if err != nil {
|
||||
t.Fatalf("Load error: %v", err)
|
||||
}
|
||||
@@ -29,17 +53,39 @@ func TestDefaults(t *testing.T) {
|
||||
if cfg.ShutdownTimeout != 8*time.Second || cfg.MaxBodyBytes != 65536 {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if cfg.DatabaseURL != databaseURL {
|
||||
t.Fatalf("DatabaseURL = %q, want the file content without the newline", cfg.DatabaseURL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecretFilesAreRequiredAndNeverEchoed(t *testing.T) {
|
||||
cases := map[string]map[string]string{
|
||||
"unset": nil,
|
||||
"empty path": {"DATABASE_URL_FILE": ""},
|
||||
"no file": {"DATABASE_URL_FILE": filepath.Join(t.TempDir(), "missing")},
|
||||
"blank file": {"DATABASE_URL_FILE": secretFile(t, " \r\n")},
|
||||
}
|
||||
for name, values := range cases {
|
||||
_, err := config.Load(env(values))
|
||||
if err == nil {
|
||||
t.Errorf("%s: want error", name)
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(err.Error(), "DATABASE_URL_FILE") || strings.Contains(err.Error(), "s3cret") {
|
||||
t.Errorf("%s: error must name the variable and hide the secret: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOverrides(t *testing.T) {
|
||||
cfg, err := config.Load(env(map[string]string{
|
||||
cfg, err := config.Load(env(required(t, map[string]string{
|
||||
"APP_HOST": "127.0.0.1",
|
||||
"APP_PORT": "9090",
|
||||
"APP_ENV": "dev",
|
||||
"APP_LOG_LEVEL": "debug",
|
||||
"APP_SHUTDOWN_TIMEOUT": "3s",
|
||||
"APP_MAX_BODY_BYTES": "1024",
|
||||
}))
|
||||
})))
|
||||
if err != nil {
|
||||
t.Fatalf("Load error: %v", err)
|
||||
}
|
||||
@@ -64,7 +110,7 @@ func TestInvalidValuesAreReported(t *testing.T) {
|
||||
"empty host": {"APP_HOST": ""},
|
||||
}
|
||||
for name, values := range cases {
|
||||
if _, err := config.Load(env(values)); err == nil {
|
||||
if _, err := config.Load(env(required(t, values))); err == nil {
|
||||
t.Errorf("%s: want error", name)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user