package web import ( "net/http" "net/url" "os" "strings" "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) } }