feat: a new Minstrel restores a backup, from the web app or the command line, then rescans (#6775)
release / govulncheck (push) Successful in 29s
release / web (push) Failing after 1m21s
release / go (push) Successful in 1m39s
release / integration (push) Successful in 5m26s
release / android (push) Successful in 5m27s
release / Build signed APK (push) Successful in 5m44s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
release / govulncheck (push) Successful in 29s
release / web (push) Failing after 1m21s
release / go (push) Successful in 1m39s
release / integration (push) Successful in 5m26s
release / android (push) Successful in 5m27s
release / Build signed APK (push) Successful in 5m44s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
A new server (no accounts, no library) offers "Restore a backup" beside "Create account". The file goes up as the request body to POST /api/auth/restore with the setup token from the log in X-Setup-Token, since the backup's admins become this server's. It loads in one transaction, then a library scan starts and finds each file at its path. The same load runs offline as `minstrel restore FILE`, which migrates the schema first. That path alone gets a 1 GiB, 15-minute body allowance instead of 4 MiB and 30s, and skips the JSON-only check for cookie writes: a stale cookie from the old server would otherwise refuse the upload, and the custom header already rules out a cross-site form. The library sync answers 410 cursor_ahead to a cursor past the last one the server has handed out, read from the change sequence, so an app synced with the old server after the backup was made starts over instead of skipping the ids the restored server reuses. Restore now leaves each sequence as having issued the restored maximum, so that reading holds; a compacted log still counts its handed-out cursors. backup.Read keeps the underlying error in its chain, so an oversized upload answers 413 rather than "not a backup". The hosting docs say how to put the music back first and what is entered again afterwards. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
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
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
t.Errorf("restore answered %+v (%v), want one account", out, err)
|
||||
}
|
||||
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())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user