console: owners choose which running things stay (plans 4d)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9 files changed,
+498
−0
internal/console/claim_invariant_fixtures_test.go+23 −0internal/console/claim_invariant_probes_read_test.go+13 −0internal/console/plancaps_copy.go+17 −0internal/console/planlapse_keep.go+255 −0internal/console/planlapse_keep_test.go+149 −0internal/console/server.go+12 −0internal/console/templates/layout.html+1 −0internal/console/templates/plan_keep.html+26 −0internal/console/testdata/claim-routes.txt+2 −0
diff --git a/internal/console/claim_invariant_fixtures_test.go b/internal/console/claim_invariant_fixtures_test.go| index ef1c6e510..4abb83010 100644 |
| --- a/internal/console/claim_invariant_fixtures_test.go |
| +++ b/internal/console/claim_invariant_fixtures_test.go |
| @@ -22,6 +22,7 @@ import ( |
| "context" |
| "encoding/json" |
| "errors" |
| + "fmt" |
| "net/http" |
| "net/url" |
| "strconv" |
| @@ -803,6 +804,7 @@ var claimFixtures = map[string]claimFixture{ |
| "route GET /a/{sqid}/fleet/byo": claimRead("renders bring-your-own-box instructions"), |
| "route GET /a/{sqid}/fleets/{fleet}": claimRead("renders one fleet's boxes and placements"), |
| "route GET /a/{sqid}/settings": claimRead("renders account settings"), |
| + "route GET /a/{sqid}/plan/keep": claimRead("renders the keep page (plans 4d) from the plan row, lapse items and an inventory read; writes nothing"), |
| "route GET /a/{sqid}/team": claimRead("renders members and invites"), |
| "route GET /a/{sqid}/trash": claimRead("lists trashed apps; purge is the trash/delete door"), |
| "route GET /a/{sqid}/{$}": claimRead("renders the account home"), |
| @@ -1007,6 +1009,27 @@ var claimFixtures = map[string]claimFixture{ |
| do: claimForm("/a/bab/trash/restore", "app", "titogo"), |
| }}, |
| + // Plans 4d: an owner's keep choice is a lapse item on each chosen app. |
| + "route POST /a/{sqid}/plan/keep": {door: &claimDoor{ |
| + apps: claimRefs("bab/kass"), |
| + setup: claimWorldLapse, |
| + do: claimForm("/a/bab/plan/keep", "keep", "app:kass"), |
| + check: func(t *testing.T, cx *claimCase, open bool) string { |
| + items, err := cx.st.LapseItems(OpsAccountID) |
| + if err != nil { |
| + return err.Error() |
| + } |
| + kept := false |
| + for _, it := range items { |
| + kept = kept || (it.App == "kass" && it.Kind == lapseApp && it.Keep) |
| + } |
| + if kept != open { |
| + return fmt.Sprintf("kass kept = %v, want %v", kept, open) |
| + } |
| + return "" |
| + }, |
| + }}, |
| + |
| // ---- POST: browser, app level ---- |
| "route POST /a/{sqid}/apps/{app}/analytics/setting": {door: &claimDoor{ |
| apps: claimRefs("bab/kass"), |
diff --git a/internal/console/claim_invariant_probes_read_test.go b/internal/console/claim_invariant_probes_read_test.go| index e4c317053..5fe4d042e 100644 |
| --- a/internal/console/claim_invariant_probes_read_test.go |
| +++ b/internal/console/claim_invariant_probes_read_test.go |
| @@ -45,6 +45,18 @@ func claimWorldInstance(t *testing.T, cx *claimCase) { |
| func claimWorldFleet(t *testing.T, cx *claimCase) { t.Helper(); cx.fleet(t) } |
| +// claimWorldLapse turns plans on and puts account bab on a lapse (plans 4d). |
| +func claimWorldLapse(t *testing.T, cx *claimCase) { |
| + t.Helper() |
| + cx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + if _, err := cx.st.SetAccountPlan(OpsAccountID, "free", nil, PlanSourceOperator, nil, "r", "ops@example.com", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := cx.st.MarkLapse(OpsAccountID, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| func claimWorldMove(t *testing.T, cx *claimCase) { t.Helper(); cx.movableInstance(t) } |
| func claimWorldAlias(t *testing.T, cx *claimCase) { |
| @@ -136,6 +148,7 @@ var claimReadProbes = map[string]claimProbe{ |
| "route GET /a/{sqid}/fleet/byo": claimPageRead(nil, "/a/bab/fleet/byo", "<h1>Bring your own fleet</h1>"), |
| "route GET /a/{sqid}/fleets/{fleet}": claimPageRead(claimWorldFleet, "/a/bab/fleets/pi", "<h1>pi</h1>"), |
| "route GET /a/{sqid}/settings": claimPageRead(nil, "/a/bab/settings", "<h1>Account settings</h1>"), |
| + "route GET /a/{sqid}/plan/keep": claimPageRead(claimWorldLapse, "/a/bab/plan/keep", "<h1>Choose what stays</h1>"), |
| "route GET /a/{sqid}/team": claimPageRead(nil, "/a/bab/team", "<h1>Team</h1>", claimFxOwner), |
| "route GET /a/{sqid}/trash": claimPageRead(func(t *testing.T, cx *claimCase) { cx.trash(t, "titogo") }, |
| "/a/bab/trash", "<h1>Trash</h1>", "titogo"), |
diff --git a/internal/console/plancaps_copy.go b/internal/console/plancaps_copy.go| index 2d1cc2289..407c601d6 100644 |
| --- a/internal/console/plancaps_copy.go |
| +++ b/internal/console/plancaps_copy.go |
| @@ -72,3 +72,20 @@ var planNoticeCopy = map[string]string{ |
| "email.lapse_off": "- Turned off: %s.", |
| "email.lapse_off_list": "Turned off: %s.\n\nChoose which stay:\n%s", |
| } |
| + |
| +// planKeepCopy holds the keep page's words (plans 4d): where an owner of a |
| +// lapsed account chooses which running things stay. |
| +var planKeepCopy = map[string]string{ |
| + "keep.title": "Choose what stays", |
| + "keep.intro": "This account's plan covers %d apps and instances and %d workers per app. Tick the ones to keep. The rest are turned off, newest first. Settings and data are kept either way.", |
| + "keep.save": "Save", |
| + "keep.too_many": "You ticked %d, and the plan covers %d. Untick some and save again.", |
| + "keep.stale": "Something you ticked is no longer there. Look at the list again and save.", |
| + "keep.saved": "Saved. The change reaches your apps within a few minutes.", |
| + "keep.nothing": "Nothing to choose: this account's plan covers everything it runs.", |
| + "keep.link": "Choose what stays", |
| + "keep.failed": "Could not read what this account runs. Try again in a moment.", |
| + "keep.on": "On", |
| + "keep.off": "Turned off", |
| + "keep.workers": "Workers", |
| +} |
diff --git a/internal/console/planlapse_keep.go b/internal/console/planlapse_keep.go| new file mode 100644 |
| index 000000000..feb11d9cc |
| --- /dev/null |
| +++ b/internal/console/planlapse_keep.go |
| @@ -0,0 +1,255 @@ |
| +package console |
| + |
| +import ( |
| + "crypto/subtle" |
| + "fmt" |
| + "log" |
| + "net/http" |
| + "net/url" |
| + "strings" |
| + "time" |
| +) |
| + |
| +// The keep page (plans 4d, Call 2): an owner of a lapsed account chooses |
| +// which apps, instances and workers stay; the rest are turned off, newest |
| +// first. A save takes effect at once: the handler re-converges and |
| +// publishes in the same request. |
| + |
| +// planKeepView is the keep page's body. |
| +type planKeepView struct { |
| + Title, Intro, Save, Nothing string |
| + Rows []planKeepRow |
| +} |
| + |
| +// planKeepRow is one thing on the keep page. |
| +type planKeepRow struct { |
| + Value, Name, Created, State string |
| + Keep, Indent bool |
| +} |
| + |
| +// planKeepOffered is whether an account's owners get the keep page link: |
| +// plans on, the account on the lapse path, and its running things or |
| +// workers over. |
| +func (s *Server) planKeepOffered(acct *Account) bool { |
| + if s.cfg.PlanCatalog == nil { |
| + return false |
| + } |
| + row, err := s.store.AccountPlan(acct.ID) |
| + if err != nil || !lapseEligible(row) { |
| + return false |
| + } |
| + states, err := s.store.CapStates(acct.ID) |
| + if err != nil { |
| + return false |
| + } |
| + for _, st := range states { |
| + if (st.Cap == capRunningThings || st.Cap == capSidecars) && st.Level.over() { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +// planKeepGate is the keep page's door: plans on (404 otherwise, as if the |
| +// page did not exist) and an owner (the owner gate's not-found otherwise). |
| +func (s *Server) planKeepGate(w http.ResponseWriter, r *http.Request) (addr, raw string, acct *Account, ok bool) { |
| + if s.cfg.PlanCatalog == nil { |
| + http.NotFound(w, r) |
| + return "", "", nil, false |
| + } |
| + return s.owner(w, r) |
| +} |
| + |
| +func (s *Server) handlePlanKeep(w http.ResponseWriter, r *http.Request) { |
| + addr, raw, acct, ok := s.planKeepGate(w, r) |
| + if !ok { |
| + return |
| + } |
| + p := s.accountPage(w, r, addr, raw, acct, RoleOwner) |
| + p.Title = planKeepCopy["keep.title"] |
| + p.ActiveNav = "settings" |
| + view := &planKeepView{Title: planKeepCopy["keep.title"], Save: planKeepCopy["keep.save"]} |
| + p.PlanKeep = view |
| + row, err := s.store.AccountPlan(acct.ID) |
| + if err != nil || !lapseEligible(row) { |
| + if err != nil { |
| + log.Printf("console: plan keep %s: %v", acct.Sqid, err) |
| + } |
| + view.Nothing = planKeepCopy["keep.nothing"] |
| + s.render(w, http.StatusOK, "plan_keep", p) |
| + return |
| + } |
| + l, err := s.EffectiveLimits(acct) |
| + if err != nil || l == nil { |
| + view.Nothing = planKeepCopy["keep.failed"] |
| + s.render(w, http.StatusOK, "plan_keep", p) |
| + return |
| + } |
| + inv, err := s.lapseInventory(r.Context(), acct) |
| + if err != nil { |
| + log.Printf("console: plan keep %s: inventory: %v", acct.Sqid, err) |
| + view.Nothing = planKeepCopy["keep.failed"] |
| + s.render(w, http.StatusOK, "plan_keep", p) |
| + return |
| + } |
| + items, err := s.store.LapseItems(acct.ID) |
| + if err != nil { |
| + log.Printf("console: plan keep %s: items: %v", acct.Sqid, err) |
| + } |
| + off := map[string]bool{} |
| + for _, it := range items { |
| + if !it.DisabledAt.IsZero() { |
| + off[lapseKey(it.Kind, it.Ref)] = true |
| + } |
| + } |
| + view.Intro = fmt.Sprintf(planKeepCopy["keep.intro"], l.RunningThings, l.SidecarsPerApp) |
| + add := func(x lapseThing, indent bool) { |
| + state := planKeepCopy["keep.on"] |
| + if off[lapseKey(x.Kind, x.Ref)] { |
| + state = planKeepCopy["keep.off"] |
| + } |
| + created := "" |
| + if !x.CreatedAt.IsZero() { |
| + created = x.CreatedAt.UTC().Format("2 January 2006") |
| + } |
| + view.Rows = append(view.Rows, planKeepRow{Value: x.Kind + ":" + x.Ref, Name: x.Ref, Created: created, |
| + State: state, Keep: x.Keep, Indent: indent}) |
| + } |
| + for _, x := range inv.Things { |
| + if x.Kind != lapseApp { |
| + continue |
| + } |
| + add(x, false) |
| + for _, y := range inv.Things { |
| + if y.Kind == lapseInstance && y.App == x.App { |
| + add(y, true) |
| + } |
| + } |
| + for _, y := range inv.Sidecars { |
| + if y.App == x.App { |
| + add(y, true) |
| + } |
| + } |
| + } |
| + s.render(w, http.StatusOK, "plan_keep", p) |
| +} |
| + |
| +func (s *Server) handlePlanKeepSave(w http.ResponseWriter, r *http.Request) { |
| + addr, raw, acct, ok := s.planKeepGate(w, r) |
| + if !ok { |
| + return |
| + } |
| + if subtle.ConstantTimeCompare([]byte(r.FormValue("csrf")), []byte(raw)) != 1 { |
| + s.renderMessage(w, r, http.StatusForbidden, "Refused", "Cross-site request blocked.") |
| + return |
| + } |
| + back := "/a/" + url.PathEscape(acct.Sqid) + "/plan/keep" |
| + fail := func(msg string) { |
| + s.setFlash(w, "err", msg) |
| + http.Redirect(w, r, back, http.StatusSeeOther) |
| + } |
| + row, err := s.store.AccountPlan(acct.ID) |
| + if err != nil || !lapseEligible(row) { |
| + fail(planKeepCopy["keep.nothing"]) |
| + return |
| + } |
| + l, err := s.EffectiveLimits(acct) |
| + if err != nil || l == nil { |
| + fail(planKeepCopy["keep.failed"]) |
| + return |
| + } |
| + inv, err := s.lapseInventory(r.Context(), acct) |
| + if err != nil { |
| + log.Printf("console: plan keep %s: inventory: %v", acct.Sqid, err) |
| + fail(planKeepCopy["keep.failed"]) |
| + return |
| + } |
| + byKey := map[string]lapseThing{} |
| + for _, list := range [][]lapseThing{inv.Things, inv.Sidecars} { |
| + for _, x := range list { |
| + byKey[lapseKey(x.Kind, x.Ref)] = x |
| + } |
| + } |
| + keep := map[string]bool{} |
| + for _, v := range r.Form["keep"] { |
| + kind, ref, _ := strings.Cut(v, ":") |
| + key := lapseKey(kind, ref) |
| + if _, ok := byKey[key]; !ok || (kind != lapseApp && kind != lapseInstance && kind != lapseSidecar) { |
| + fail(planKeepCopy["keep.stale"]) |
| + return |
| + } |
| + keep[key] = true |
| + } |
| + // Count what stays: a kept instance keeps its app. |
| + apps, things, perApp := map[string]bool{}, 0, map[string]int{} |
| + for key := range keep { |
| + x := byKey[key] |
| + switch x.Kind { |
| + case lapseApp, lapseInstance: |
| + if x.Kind == lapseInstance { |
| + things++ |
| + } |
| + apps[x.App] = true |
| + case lapseSidecar: |
| + perApp[x.App]++ |
| + } |
| + } |
| + things += len(apps) |
| + if l.RunningThings > 0 && things > l.RunningThings { |
| + fail(fmt.Sprintf(planKeepCopy["keep.too_many"], things, l.RunningThings)) |
| + return |
| + } |
| + for _, n := range perApp { |
| + if l.SidecarsPerApp > 0 && n > l.SidecarsPerApp { |
| + fail(fmt.Sprintf(planKeepCopy["keep.too_many"], n, l.SidecarsPerApp)) |
| + return |
| + } |
| + } |
| + items, err := s.store.LapseItems(acct.ID) |
| + if err != nil { |
| + fail(planKeepCopy["keep.failed"]) |
| + return |
| + } |
| + itemOf := map[string]lapseItem{} |
| + for _, it := range items { |
| + itemOf[lapseKey(it.Kind, it.Ref)] = it |
| + } |
| + now := time.Now() |
| + for key, x := range byKey { |
| + it, has := itemOf[key] |
| + current := has && unixOrZero(it.ThingCreated) == unixOrZero(x.CreatedAt) |
| + var err error |
| + switch { |
| + case keep[key]: |
| + put := lapseItem{AccountID: acct.ID, App: x.App, Kind: x.Kind, Ref: x.Ref, ThingCreated: x.CreatedAt, Keep: true, UpdatedAt: now} |
| + if current { |
| + put.DisabledAt = it.DisabledAt |
| + } |
| + if !current || !it.Keep { |
| + err = s.store.PutLapseItem(r.Context(), put) |
| + } |
| + case current && it.Keep && !it.DisabledAt.IsZero(): |
| + it.Keep, it.UpdatedAt = false, now |
| + err = s.store.PutLapseItem(r.Context(), it) |
| + case current && it.Keep: |
| + err = s.store.DeleteLapseItem(r.Context(), acct.ID, it.App, it.Kind, it.Ref) |
| + } |
| + if err != nil { |
| + log.Printf("console: plan keep %s: %v", acct.Sqid, err) |
| + fail(planKeepCopy["keep.failed"]) |
| + return |
| + } |
| + } |
| + s.audit(acct, accountAuditSubject, addr, "plan.keep", fmt.Sprintf("chose %d things to keep", len(keep))) |
| + // Take effect at once: re-converge, then publish. |
| + if err := s.planGraceAccount(r.Context(), acct, now); err != nil { |
| + log.Printf("console: plan keep %s: grace: %v", acct.Sqid, err) |
| + } |
| + if row, err := s.store.AccountPlan(acct.ID); err == nil && row != nil && s.cfg.Releases != nil { |
| + if err := s.publishAccountLimits(r.Context(), *row, now); err != nil { |
| + log.Printf("console: plan keep %s: publish: %v", acct.Sqid, err) |
| + } |
| + } |
| + s.setFlash(w, "ok", planKeepCopy["keep.saved"]) |
| + http.Redirect(w, r, back, http.StatusSeeOther) |
| +} |
diff --git a/internal/console/planlapse_keep_test.go b/internal/console/planlapse_keep_test.go| new file mode 100644 |
| index 000000000..209119218 |
| --- /dev/null |
| +++ b/internal/console/planlapse_keep_test.go |
| @@ -0,0 +1,149 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "net/http" |
| + "net/url" |
| + "slices" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// keepFixture is lapseFixture with paul as an owner of the lapsed account, |
| +// on the real clock (the page's handlers use it): free since eight days |
| +// ago, so after the pass now the two newest are off. |
| +func keepFixture(t *testing.T, turnOff bool) (*consoleFixture, *Account, *http.Client, string) { |
| + t.Helper() |
| + fx, acct, _ := lapseFixture(t) |
| + if err := fx.st.AddMember(acct.ID, "paul@keymail.dev", RoleOwner, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + base := time.Now().Add(-8 * 24 * time.Hour) |
| + applyPlan(t, fx, acct, "personal", nil, nil, base.Add(-time.Hour)) |
| + applyPlan(t, fx, acct, "free", nil, nil, base) |
| + if turnOff { |
| + fx.srv.planGracePass(context.Background(), time.Now()) |
| + if got, want := disabledOf(t, fx, acct), []string{"blog-1.example.com", "shop-2.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("fixture: disabled = %v, want %v", got, want) |
| + } |
| + } |
| + c := signInPaul(t, fx) |
| + return fx, acct, c, sessionToken(t, fx, c) |
| +} |
| + |
| +func keepURL(fx *consoleFixture, acct *Account) string { |
| + return fx.ts.URL + "/a/" + acct.Sqid + "/plan/keep" |
| +} |
| + |
| +func TestKeepPageListsRunningThings(t *testing.T) { |
| + fx, acct, c, _ := keepFixture(t, true) |
| + status, _, body := get(t, c, keepURL(fx, acct)) |
| + if status != http.StatusOK { |
| + t.Fatalf("GET = %d", status) |
| + } |
| + for _, want := range []string{planKeepCopy["keep.title"], "shop", "blog", "shop-1.example.com", "blog-1.example.com", |
| + "shop-2.example.com", `value="instance:blog-1.example.com"`, `value="app:shop"`, "3 apps and instances"} { |
| + if !strings.Contains(body, want) { |
| + t.Fatalf("keep page lacks %q:\n%s", want, body) |
| + } |
| + } |
| +} |
| + |
| +func TestKeepPageOwnersOnly(t *testing.T) { |
| + fx, acct, _, _ := keepFixture(t, false) |
| + if err := fx.st.AddMember(acct.ID, "kim@keymail.dev", RoleMember, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + kim := newBrowser(t) |
| + fx.setUser("kim@keymail.dev") |
| + signUp(t, fx, kim, "code-kim-keep") |
| + if status, _, _ := get(t, kim, keepURL(fx, acct)); status != http.StatusNotFound { |
| + t.Fatalf("a member's GET = %d, want 404", status) |
| + } |
| + outsider := newBrowser(t) |
| + fx.setUser("eve@keymail.dev") |
| + signUp(t, fx, outsider, "code-eve-keep") |
| + if status, _, _ := get(t, outsider, keepURL(fx, acct)); status != http.StatusNotFound { |
| + t.Fatalf("an outsider's GET = %d, want 404", status) |
| + } |
| +} |
| + |
| +func keptRefs(t *testing.T, fx *consoleFixture, acct *Account) []string { |
| + t.Helper() |
| + items, err := fx.st.LapseItems(acct.ID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var out []string |
| + for _, it := range items { |
| + if it.Keep { |
| + out = append(out, it.Ref) |
| + } |
| + } |
| + slices.Sort(out) |
| + return out |
| +} |
| + |
| +func TestKeepSaveRefusesMoreThanTheCap(t *testing.T) { |
| + fx, acct, c, csrf := keepFixture(t, false) |
| + form := url.Values{"csrf": {csrf}, "keep": {"app:shop", "app:blog", "instance:shop-1.example.com", "instance:blog-1.example.com"}} |
| + if status, _, _ := postForm(t, c, keepURL(fx, acct), form); status != http.StatusSeeOther { |
| + t.Fatalf("POST = %d, want a redirect", status) |
| + } |
| + if got := keptRefs(t, fx, acct); got != nil { |
| + t.Fatalf("a refused save kept %v", got) |
| + } |
| + if _, _, body := get(t, c, keepURL(fx, acct)); !strings.Contains(body, "You ticked 4, and the plan covers 3") { |
| + t.Fatalf("no too-many message:\n%s", body) |
| + } |
| +} |
| + |
| +func TestKeepSaveImpliesTheApp(t *testing.T) { |
| + fx, acct, c, csrf := keepFixture(t, false) |
| + // Two instances of two unticked apps are four running things. |
| + form := url.Values{"csrf": {csrf}, "keep": {"instance:shop-1.example.com", "instance:blog-1.example.com"}} |
| + postForm(t, c, keepURL(fx, acct), form) |
| + if got := keptRefs(t, fx, acct); got != nil { |
| + t.Fatalf("over the cap through implied apps, kept %v", got) |
| + } |
| + form = url.Values{"csrf": {csrf}, "keep": {"instance:blog-1.example.com"}} |
| + postForm(t, c, keepURL(fx, acct), form) |
| + if got, want := keptRefs(t, fx, acct), []string{"blog-1.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("kept %v, want %v", got, want) |
| + } |
| +} |
| + |
| +func TestKeepSaveSwapsWhatIsOffAtOnce(t *testing.T) { |
| + fx, acct, c, csrf := keepFixture(t, true) |
| + form := url.Values{"csrf": {csrf}, "keep": {"instance:blog-1.example.com"}} |
| + status, _, _ := postForm(t, c, keepURL(fx, acct), form) |
| + if status != http.StatusSeeOther { |
| + t.Fatalf("POST = %d", status) |
| + } |
| + if got, want := disabledOf(t, fx, acct), []string{"shop-1.example.com", "shop-2.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("after keeping blog-1: disabled = %v, want %v", got, want) |
| + } |
| + if got, want := publishedDisabled(t, fx, acct), []string{"shop-1.example.com", "shop-2.example.com"}; !slices.Equal(got, want) { |
| + t.Fatalf("record not republished in the same request: %v", got) |
| + } |
| +} |
| + |
| +func TestKeepPageWithoutACatalog(t *testing.T) { |
| + fx, acct, c, csrf := keepFixture(t, false) |
| + fx.srv.cfg.PlanCatalog = nil |
| + if status, _, _ := get(t, c, keepURL(fx, acct)); status != http.StatusNotFound { |
| + t.Fatalf("GET without a catalog = %d, want 404", status) |
| + } |
| + if status, _, _ := postForm(t, c, keepURL(fx, acct), url.Values{"csrf": {csrf}}); status != http.StatusNotFound { |
| + t.Fatalf("POST without a catalog = %d, want 404", status) |
| + } |
| +} |
| + |
| +func TestBannerLinksToKeepPage(t *testing.T) { |
| + fx, acct, c, _ := keepFixture(t, false) |
| + _, _, body := get(t, c, fx.ts.URL+"/a/"+acct.Sqid+"/") |
| + if !strings.Contains(body, `href="/a/`+acct.Sqid+`/plan/keep"`) { |
| + t.Fatalf("no link to the keep page:\n%s", body) |
| + } |
| +} |
diff --git a/internal/console/server.go b/internal/console/server.go| index c351563ad..353fb6f5c 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -1516,6 +1516,9 @@ func NewServer(cfg Config, st *Store) (*Server, error) { |
| s.registerRoute("GET /a/{sqid}/settings", s.handleAccountSettings) |
| s.registerRoute("POST /a/{sqid}/errors/level", s.handleAccountErrorLevel) |
| s.registerRoute("POST /a/{sqid}/analytics/setting", s.handleAccountAnalyticsSetting) |
| + // Plans 4d: owners choose what stays after a lapse (planlapse_keep.go). |
| + s.registerRoute("GET /a/{sqid}/plan/keep", s.handlePlanKeep) |
| + s.registerRoute("POST /a/{sqid}/plan/keep", s.handlePlanKeepSave) |
| // Go 1.22's mux prefers this literal over apps/{app} below — exactly why |
| // "new" must be a reserved app name (claimApp): an app so named could |
| // never be viewed. |
| @@ -1882,6 +1885,11 @@ type page struct { |
| // PlanNotices are the account's plan warnings (plans 4a), one banner |
| // line each; nil when plans are off or nothing is near a limit. |
| PlanNotices []string |
| + // PlanKeepURL links an owner of a lapsed account to the keep page |
| + // (plans 4d), with PlanKeepLink as its words; "" for none. |
| + PlanKeepURL, PlanKeepLink string |
| + // PlanKeep is the keep page's body (plans 4d). |
| + PlanKeep *planKeepView |
| // planSeen are the over rows behind PlanNotices when the viewer owns |
| // the account; render records them as notice once the page is written. |
| planSeen []capState |
| @@ -3368,6 +3376,10 @@ func (s *Server) accountPage(w http.ResponseWriter, r *http.Request, addr, raw s |
| p.Nav = "/a/" + url.PathEscape(preferredSeg(p.Accounts, acct)) |
| p.PlanNotices, p.planSeen = s.planBanner(acct, role, time.Now()) |
| p.planAcctID = acct.ID |
| + if role == RoleOwner && s.planKeepOffered(acct) { |
| + p.PlanKeepURL = "/a/" + url.PathEscape(acct.Sqid) + "/plan/keep" |
| + p.PlanKeepLink = planKeepCopy["keep.link"] |
| + } |
| return p |
| } |
diff --git a/internal/console/templates/layout.html b/internal/console/templates/layout.html| index 8d3884f33..b9fecac85 100644 |
| --- a/internal/console/templates/layout.html |
| +++ b/internal/console/templates/layout.html |
| @@ -592,6 +592,7 @@ |
| </header> |
| <main> |
| {{range .PlanNotices}}<p class="banner">{{.}}</p> |
| +{{end}}{{if .PlanKeepURL}}<p class="banner"><a href="{{.PlanKeepURL}}">{{.PlanKeepLink}}</a></p> |
| {{end}}{{end}} |
| {{define "bottom"}} |
diff --git a/internal/console/templates/plan_keep.html b/internal/console/templates/plan_keep.html| new file mode 100644 |
| index 000000000..809f6cf3c |
| --- /dev/null |
| +++ b/internal/console/templates/plan_keep.html |
| @@ -0,0 +1,26 @@ |
| +{{define "plan_keep"}}{{template "top" .}} |
| +{{if .Flash}}<p class="banner">{{.Flash}}</p>{{end}} |
| +{{if .FlashErr}}<p class="banner error">{{.FlashErr}}</p>{{end}} |
| +<h1>{{.PlanKeep.Title}}</h1> |
| +{{with .PlanKeep}} |
| +{{if .Nothing}}<p>{{.Nothing}}</p>{{else}} |
| +<p>{{.Intro}}</p> |
| +<form method="post" action="{{$.Base}}/plan/keep"> |
| + <input type="hidden" name="csrf" value="{{$.CSRF}}"> |
| + <div class="tablewrap"> |
| + <table> |
| + {{range .Rows}} |
| + <tr> |
| + <td><input type="checkbox" id="keep-{{.Value}}" name="keep" value="{{.Value}}"{{if .Keep}} checked{{end}}></td> |
| + <td><label for="keep-{{.Value}}">{{if .Indent}} {{end}}{{.Name}}</label></td> |
| + <td>{{.Created}}</td> |
| + <td>{{.State}}</td> |
| + </tr> |
| + {{end}} |
| + </table> |
| + </div> |
| + <button type="submit">{{.Save}}</button> |
| +</form> |
| +{{end}} |
| +{{end}} |
| +{{template "bottom" .}}{{end}} |
diff --git a/internal/console/testdata/claim-routes.txt b/internal/console/testdata/claim-routes.txt| index a526dc00c..f44fb6390 100644 |
| --- a/internal/console/testdata/claim-routes.txt |
| +++ b/internal/console/testdata/claim-routes.txt |
| @@ -32,6 +32,7 @@ route GET /a/{sqid}/apps/{app}/settings/{section} |
| route GET /a/{sqid}/fleet |
| route GET /a/{sqid}/fleet/byo |
| route GET /a/{sqid}/fleets/{fleet} |
| +route GET /a/{sqid}/plan/keep |
| route GET /a/{sqid}/settings |
| route GET /a/{sqid}/team |
| route GET /a/{sqid}/trash |
| @@ -157,6 +158,7 @@ route POST /a/{sqid}/invite |
| route POST /a/{sqid}/invite/revoke |
| route POST /a/{sqid}/members/remove |
| route POST /a/{sqid}/members/transfer |
| +route POST /a/{sqid}/plan/keep |
| route POST /a/{sqid}/services/new |
| route POST /a/{sqid}/services/revoke |
| route POST /a/{sqid}/services/rotate |