fix: user:backfill-handles opens its own database

- without a published *gorm.DB the command opens one with lagoon.OpenFromApp
  for its run and closes it, as the plugin's other commands do (withDB)
- TestBackfillHandlesOpensFromConfigWhenUnpublished; README CLI note
This commit is contained in:
Jakub Zych
2026-10-20 23:07:14 +02:00
parent 174161895e
commit c43ba87371
3 changed files with 47 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package user
import (
"bytes"
"context"
"encoding/base64"
"strings"
"testing"
@@ -77,6 +78,40 @@ func TestConsoleWithDBOpensFromConfigWhenUnpublished(t *testing.T) {
}
}
// TestBackfillHandlesOpensFromConfigWhenUnpublished runs user:backfill-handles
// as the console does: no published database, only database.dsn in the config.
func TestBackfillHandlesOpensFromConfigWhenUnpublished(t *testing.T) {
_, gdb := sessionApp(t, "0s")
user := insertUser(t, gdb, "backfill-unpublished@example.test", "correct-horse")
if err := gdb.Model(user).Update("handle", nil).Error; err != nil {
t.Fatal(err)
}
app := backpack.New(sessionConfig(t))
if err := app.Config.Set("database.dsn", sessionDSN); err != nil {
t.Fatal(err)
}
// Opening a database also loads the encryption keys from app.key.
if err := app.Config.Set("app.key", base64.StdEncoding.EncodeToString([]byte("0123456789abcdef0123456789abcdef"))); err != nil {
t.Fatal(err)
}
if _, ok := app.Lookup[*gorm.DB](); ok {
t.Fatal("setup: a database is published")
}
cmd := commandNamed(t, (&Plugin{app: app}).Commands(), "user:backfill-handles")
var buf bytes.Buffer
out := bonfire.NewOutput(nil, &buf, &buf)
if err := cmd.Run(context.Background(), emailArg{}, out); err != nil {
t.Fatalf("run: %v (output %q)", err, buf.String())
}
var fresh models.User
if err := gdb.First(&fresh, user.ID).Error; err != nil {
t.Fatal(err)
}
if fresh.Handle == nil || *fresh.Handle == "" {
t.Fatal("the user still has no handle")
}
}
func commandNamed(t *testing.T, cmds []bonfire.Command, name string) bonfire.Command {
t.Helper()
for _, candidate := range cmds {