Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c492e5969e |
No files matched your search
+5
-1
@@ -14,6 +14,10 @@ var errNoPassword = errors.New(
|
||||
"account has no password set",
|
||||
)
|
||||
|
||||
// PasswordCost is the bcrypt cost at which SetPassword hashes a
|
||||
// password. Tests lower it to bcrypt.MinCost before they start.
|
||||
var PasswordCost = bcrypt.DefaultCost //nolint:gochecknoglobals // tests lower it
|
||||
|
||||
// SetPassword sets a bcrypt-hashed password on a session,
|
||||
// enabling multi-client login via POST /api/v1/login.
|
||||
func (database *Database) SetPassword(
|
||||
@@ -22,7 +26,7 @@ func (database *Database) SetPassword(
|
||||
password string,
|
||||
) error {
|
||||
hash, err := bcrypt.GenerateFromPassword(
|
||||
[]byte(password), database.passwordCost,
|
||||
[]byte(password), PasswordCost,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("hash password: %w", err)
|
||||
|
||||
+5
-18
@@ -15,7 +15,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"go.uber.org/fx"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/logger"
|
||||
|
||||
@@ -40,19 +39,13 @@ type Params struct {
|
||||
|
||||
Logger *logger.Logger
|
||||
Config *config.Config
|
||||
|
||||
// PasswordCost is the bcrypt cost at which SetPassword hashes
|
||||
// a password. The server leaves it unset, which means
|
||||
// bcrypt.DefaultCost. Tests set a lower cost to save time.
|
||||
PasswordCost int `optional:"true"`
|
||||
}
|
||||
|
||||
// Database manages the SQLite connection and migrations.
|
||||
type Database struct {
|
||||
conn *sql.DB
|
||||
log *slog.Logger
|
||||
params *Params
|
||||
passwordCost int
|
||||
conn *sql.DB
|
||||
log *slog.Logger
|
||||
params *Params
|
||||
}
|
||||
|
||||
// New creates a new Database and registers lifecycle hooks.
|
||||
@@ -60,15 +53,9 @@ func New(
|
||||
lifecycle fx.Lifecycle,
|
||||
params Params,
|
||||
) (*Database, error) {
|
||||
passwordCost := params.PasswordCost
|
||||
if passwordCost == 0 {
|
||||
passwordCost = bcrypt.DefaultCost
|
||||
}
|
||||
|
||||
database := &Database{ //nolint:exhaustruct // conn set in OnStart
|
||||
params: ¶ms,
|
||||
log: params.Logger.Get(),
|
||||
passwordCost: passwordCost,
|
||||
params: ¶ms,
|
||||
log: params.Logger.Get(),
|
||||
}
|
||||
|
||||
database.log.Info("Database instantiated")
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"go.uber.org/fx/fxtest"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/db"
|
||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
||||
@@ -29,6 +30,13 @@ const (
|
||||
endlessQueryTimeout = 100 * time.Millisecond
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// A password hashed at full cost takes seconds under -race.
|
||||
db.PasswordCost = bcrypt.MinCost
|
||||
|
||||
m.Run()
|
||||
}
|
||||
|
||||
// TestDefaultDBURLSetsWALAndBusyTimeout opens a file database
|
||||
// with the default URL's parameters the way the server does,
|
||||
// and checks the journal mode and busy timeout it ends up with.
|
||||
|
||||
@@ -8,12 +8,10 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
||||
)
|
||||
|
||||
// NewTestDatabase creates an in-memory database for testing. It
|
||||
// hashes passwords at bcrypt.MinCost.
|
||||
// NewTestDatabase creates an in-memory database for testing.
|
||||
func NewTestDatabase(tb testing.TB) (*Database, error) {
|
||||
tb.Helper()
|
||||
|
||||
@@ -25,9 +23,8 @@ func NewTestDatabase(tb testing.TB) (*Database, error) {
|
||||
}
|
||||
|
||||
database := &Database{ //nolint:exhaustruct // test helper, params not needed
|
||||
conn: conn,
|
||||
log: slog.Default(),
|
||||
passwordCost: bcrypt.MinCost,
|
||||
conn: conn,
|
||||
log: slog.Default(),
|
||||
}
|
||||
|
||||
err = database.runMigrations(context.Background())
|
||||
|
||||
@@ -47,6 +47,9 @@ import (
|
||||
const templateDBURL = "file:template?mode=memory&cache=shared"
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// A password hashed at full cost takes seconds under -race.
|
||||
db.PasswordCost = bcrypt.MinCost
|
||||
|
||||
template := fx.New(
|
||||
fx.NopLogger,
|
||||
fx.Supply(
|
||||
@@ -234,7 +237,7 @@ func newTestDB(
|
||||
cfg *config.Config,
|
||||
) (*db.Database, error) {
|
||||
database, err := db.New(lifecycle, db.Params{ //nolint:exhaustruct
|
||||
Logger: log, Config: cfg, PasswordCost: bcrypt.MinCost,
|
||||
Logger: log, Config: cfg,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("test db: %w", err)
|
||||
|
||||
@@ -39,6 +39,13 @@ const testTimeout = 5 * time.Second
|
||||
// notification, and a test fails when one is missing.
|
||||
const testPollInterval = time.Hour
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// A password hashed at full cost takes seconds under -race.
|
||||
db.PasswordCost = bcrypt.MinCost
|
||||
|
||||
m.Run()
|
||||
}
|
||||
|
||||
// testEnv holds the shared test infrastructure.
|
||||
type testEnv struct {
|
||||
dbURL string
|
||||
@@ -126,9 +133,8 @@ func newTestEnvWithResolver(
|
||||
}
|
||||
|
||||
database, err := db.New(lifecycle, db.Params{
|
||||
Logger: dbLog,
|
||||
Config: &config.Config{DBURL: dbURL},
|
||||
PasswordCost: bcrypt.MinCost,
|
||||
Logger: dbLog,
|
||||
Config: &config.Config{DBURL: dbURL},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("new database: %v", err)
|
||||
|
||||
Reference in new issue
Block a user