Files
minstrel/internal/api/auth_restore_test.go
T
bvandeusenandClaude Opus 5.5 68e1e47534
release / govulncheck (push) Successful in 16s
release / go (push) Failing after 49s
release / web (push) Successful in 1m9s
release / integration (push) Failing after 4m14s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / android (push) Canceled after 4m38s
release / Build signed APK (push) Canceled after 4m38s
fix: a new server takes a backup after its first scan, and restarts to load the restored settings (#6786)
A one-off end-to-end run of the :dev image (scratch Postgres, generated
FLAC files, no outbound network) found that the web restore, done the way
docs/hosting.md describes, was refused with not_empty: a new server's
startup scan fills the library before anyone signs up, and Restore counted
tracks as occupied. The command-line restore passed the same run.

- Restore now asks only that the instance has no accounts. The library the
  first scan found is replaced by the backup's (every foreign key into
  artists/albums/tracks cascades or sets null), and the restore locks the
  library tables so a running scan waits rather than adding to them.
- Settings are read once at boot by about ten services, the library folders
  among them, so after a web restore the server ran on its defaults until
  restarted. The restore endpoint now answers, then asks the process to
  restart in place (syscall.Exec of the same binary, arguments and
  environment), which needs no container restart policy. The startup scan
  of the restarted server replaces the scan the endpoint used to start.
- The restore form says Minstrel is restarting; the docs say a scanned new
  server still takes a backup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 20:21:00 -05:00

99 lines
3.2 KiB
Go

package api
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/backup"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
)
// A new server takes a backup only with the setup token from its log, only
// while it has no accounts, and only a file that is a backup; then the
// backup's accounts sign in again.
func TestRestore_ANewServerTakesABackupWithTheSetupToken(t *testing.T) {
h, pool := testHandlers(t)
ctx := context.Background()
truncateLibrary(t, pool)
resetUsers(t, h)
seedUser(t, pool, "restored", "abcd1234", true)
c, err := backup.Snapshot(ctx, pool)
if err != nil {
t.Fatal(err)
}
var file bytes.Buffer
if _, err := backup.Write(&file, c, time.Now(), "test"); err != nil {
t.Fatal(err)
}
resetUsers(t, h)
token, err := auth.NewSetupToken()
if err != nil {
t.Fatal(err)
}
h.setupToken = token
h.requireSetupToken = true
restarts := 0
h.restart = func() { restarts++ }
restore := func(setupToken string, body []byte) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodPost, "/api/auth/restore", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/x-tar")
if setupToken != "" {
req.Header.Set(SetupTokenHeader, setupToken)
}
rec := httptest.NewRecorder()
h.handleRestore(rec, req)
return rec
}
if w := restore("", file.Bytes()); w.Code != http.StatusForbidden {
t.Errorf("no setup token: status = %d, want 403", w.Code)
}
if w := restore("wrong", file.Bytes()); w.Code != http.StatusForbidden {
t.Errorf("a wrong setup token: status = %d, want 403", w.Code)
}
if w := restore(token.Value(), []byte("not a tar file")); w.Code != http.StatusBadRequest ||
!strings.Contains(w.Body.String(), "not_a_backup") {
t.Errorf("not a backup: status = %d body = %s, want 400 not_a_backup", w.Code, w.Body.String())
}
var n int
if err := pool.QueryRow(ctx, "SELECT count(*) FROM users").Scan(&n); err != nil || n != 0 {
t.Fatalf("a refused restore left %d accounts (%v)", n, err)
}
if restarts != 0 {
t.Errorf("a refused restore restarted the server %d times", restarts)
}
w := restore(token.Value(), file.Bytes())
if w.Code != http.StatusOK {
t.Fatalf("restore: status = %d body = %s", w.Code, w.Body.String())
}
var out restoreResp
if err := json.NewDecoder(w.Body).Decode(&out); err != nil || out.Accounts != 1 || !out.Restarting {
t.Errorf("restore answered %+v (%v), want one account and a restart", out, err)
}
if restarts != 1 {
t.Errorf("the restore restarted the server %d times, want once so the restored settings load", restarts)
}
var admin bool
if err := pool.QueryRow(ctx, "SELECT is_admin FROM users WHERE username = $1",
dbtest.TestUserPrefix+"restored").Scan(&admin); err != nil || !admin {
t.Errorf("the restored admin: is_admin = %v, %v", admin, err)
}
if w := restore(token.Value(), file.Bytes()); w.Code != http.StatusConflict ||
!strings.Contains(w.Body.String(), "not_empty") {
t.Errorf("a second restore: status = %d body = %s, want 409 not_empty", w.Code, w.Body.String())
}
if restarts != 1 {
t.Errorf("a refused second restore restarted the server (%d restarts)", restarts)
}
}