lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"mtusi-messenger/internal/config"
|
||||
)
|
||||
|
||||
func TestNormalizeKeepsProvidedValues(t *testing.T) {
|
||||
cfg, err := normalize(config.Postgres{
|
||||
Host: "db",
|
||||
Port: 6543,
|
||||
Database: "app",
|
||||
User: "app",
|
||||
Password: "database-secret",
|
||||
SSLMode: "verify-full",
|
||||
MaxConns: 7,
|
||||
MinConns: 2,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "db", cfg.Host)
|
||||
require.Equal(t, 6543, cfg.Port)
|
||||
require.Equal(t, "app", cfg.Database)
|
||||
require.Equal(t, "verify-full", cfg.SSLMode)
|
||||
require.Equal(t, int32(7), cfg.MaxConns)
|
||||
}
|
||||
|
||||
func TestNormalizeRejectsMinGreaterThanMax(t *testing.T) {
|
||||
_, err := normalize(config.Postgres{User: "app", Password: "secret", MinConns: 5, MaxConns: 2})
|
||||
require.ErrorContains(t, err, "min_conns must not exceed max_conns")
|
||||
}
|
||||
|
||||
func TestNormalizeRequiresCredentials(t *testing.T) {
|
||||
for _, cfg := range []config.Postgres{{User: "app"}, {Password: "secret"}} {
|
||||
_, err := normalize(cfg)
|
||||
require.ErrorContains(t, err, "user and password are required")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeRejectsUnverifiedTLS(t *testing.T) {
|
||||
for _, mode := range []string{"disable", "allow", "prefer", "require", "verify-ca"} {
|
||||
_, err := normalize(config.Postgres{User: "app", Password: "secret", SSLMode: mode})
|
||||
require.ErrorContains(t, err, "ssl_mode must be verify-full")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeRejectsNonTCPHost(t *testing.T) {
|
||||
for _, host := range []string{"/var/run/postgresql", "db,other", "db\n"} {
|
||||
_, err := normalize(config.Postgres{Host: host, User: "app", Password: "secret"})
|
||||
require.ErrorContains(t, err, "single tcp hostname")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user