A restore is staged (checked, the current database backed up) and swapped in when the app next starts. Settings → Backups restores with a self-restart, cancels a waiting restore, and takes uploads; supermarket restore takes a backup name or a file; supermarket backup --with-secrets makes an archive with the keys, for moving machines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
118 lines
4.3 KiB
Go
118 lines
4.3 KiB
Go
package web
|
|
|
|
import (
|
|
"bytes"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"supermarket/internal/backup"
|
|
)
|
|
|
|
func TestBackupsPage(t *testing.T) {
|
|
s, h := newTestServer(t, nil)
|
|
if body := do(h, http.MethodGet, "/settings/backups", nil, nil).Body.String(); !strings.Contains(body, "No backups yet") {
|
|
t.Errorf("empty: %s", body)
|
|
}
|
|
body := postForm(h, "/settings/backups/now", url.Values{"note": {"Before the import"}}).Body.String()
|
|
if !strings.Contains(body, "Backed up to app-") || !strings.Contains(body, "-manual-before-the-import.db") ||
|
|
!strings.Contains(body, "By you") || !strings.Contains(body, "before-the-import") {
|
|
t.Errorf("after backing up: %s", body)
|
|
}
|
|
bs, _ := backup.List(s.backupDir())
|
|
if len(bs) != 1 {
|
|
t.Fatalf("backups: %v", bs)
|
|
}
|
|
rec := do(h, http.MethodGet, "/settings/backups/file/"+bs[0].Name, nil, nil)
|
|
if rec.Code != http.StatusOK || !strings.HasPrefix(rec.Body.String(), "SQLite format 3") ||
|
|
!strings.Contains(rec.Header().Get("Content-Disposition"), bs[0].Name) {
|
|
t.Errorf("download: %d %v", rec.Code, rec.Header())
|
|
}
|
|
for _, bad := range []string{"..%2Fapp.db", "secrets.json", "app.db"} {
|
|
if rec := do(h, http.MethodGet, "/settings/backups/file/"+bad, nil, nil); rec.Code != http.StatusNotFound {
|
|
t.Errorf("%s: %d", bad, rec.Code)
|
|
}
|
|
}
|
|
// The daily one, once a day.
|
|
s.dailyBackup(time.Now())
|
|
s.dailyBackup(time.Now())
|
|
if bs, _ := backup.List(s.backupDir()); len(bs) != 2 {
|
|
t.Errorf("after the daily: %d", len(bs))
|
|
}
|
|
body = postForm(h, "/settings/backups/delete", url.Values{"name": {bs[0].Name}}).Body.String()
|
|
if !strings.Contains(body, "Deleted "+bs[0].Name) {
|
|
t.Errorf("delete: %s", body)
|
|
}
|
|
if _, err := os.Stat(bs[0].Path); err == nil {
|
|
t.Error("still there")
|
|
}
|
|
if body := postForm(h, "/settings/backups/delete", url.Values{"name": {"../app.db"}}).Body.String(); !strings.Contains(body, "There's no backup called") {
|
|
t.Errorf("bad delete: %s", body)
|
|
}
|
|
}
|
|
|
|
func TestRestoreFromThePage(t *testing.T) {
|
|
s, h := newTestServer(t, nil)
|
|
s.DB.PutJSON("test:value", "before")
|
|
b, err := backup.Make(s.DB.DB, s.backupDir(), backup.Manual, "good", time.Now().Add(-time.Hour))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.DB.PutJSON("test:value", "after")
|
|
var restarts atomic.Int32
|
|
s.Restart = func() { restarts.Add(1) }
|
|
|
|
rec := postForm(h, "/settings/backups/restore", url.Values{"name": {b.Name}})
|
|
body := rec.Body.String()
|
|
if !strings.Contains(body, "Restoring "+b.Name) || !strings.Contains(body, "this page reloads by itself") ||
|
|
!strings.Contains(body, "A restore is waiting") || rec.Header().Get("Refresh") == "" {
|
|
t.Errorf("restore: %v %s", rec.Header(), body)
|
|
}
|
|
time.Sleep(700 * time.Millisecond)
|
|
if restarts.Load() != 1 || !backup.Pending(s.DataDir) {
|
|
t.Errorf("restarts %d, pending %v", restarts.Load(), backup.Pending(s.DataDir))
|
|
}
|
|
// Cancelled before the restart: nothing happens.
|
|
body = postForm(h, "/settings/backups/cancel", nil).Body.String()
|
|
if !strings.Contains(body, "The restore is cancelled") || backup.Pending(s.DataDir) {
|
|
t.Errorf("cancel: %s", body)
|
|
}
|
|
if body := postForm(h, "/settings/backups/restore", url.Values{"name": {"../app.db"}}).Body.String(); !strings.Contains(body, "There's no backup called") {
|
|
t.Errorf("bad name: %s", body)
|
|
}
|
|
}
|
|
|
|
func TestBackupUpload(t *testing.T) {
|
|
s, h := newTestServer(t, nil)
|
|
b, _ := backup.Make(s.DB.DB, t.TempDir(), backup.Manual, "elsewhere", time.Now())
|
|
raw, _ := os.ReadFile(b.Path)
|
|
upload := func(name string, content []byte) string {
|
|
var buf bytes.Buffer
|
|
mw := multipart.NewWriter(&buf)
|
|
fw, _ := mw.CreateFormFile("file", name)
|
|
fw.Write(content)
|
|
mw.Close()
|
|
return do(h, http.MethodPost, "/settings/backups/upload", &buf, map[string]string{"Content-Type": mw.FormDataContentType()}).Body.String()
|
|
}
|
|
body := upload("app-from-laptop.db", raw)
|
|
if !strings.Contains(body, "Uploaded as app-") || !strings.Contains(body, "-manual-uploaded-app-from-laptop.db") {
|
|
t.Errorf("upload: %s", body)
|
|
}
|
|
if body := upload("notes.txt", []byte("hello")); !strings.Contains(body, "isn't a backup Supermarket can use") {
|
|
t.Errorf("bad upload: %s", body)
|
|
}
|
|
if bs, _ := backup.List(s.backupDir()); len(bs) != 1 {
|
|
t.Errorf("backups: %v", bs)
|
|
}
|
|
left, _ := filepath.Glob(filepath.Join(s.backupDir(), ".upload-*"))
|
|
if len(left) != 0 {
|
|
t.Errorf("temporary files left: %v", left)
|
|
}
|
|
}
|