| 1 | package webtest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "io" |
| 7 | "log/slog" |
| 8 | "strings" |
| 9 | "sync" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/rastrillo/rastrillo/crypto" |
| 14 | |
| 15 | "amadan.net/oficina/base/internal/config" |
| 16 | "amadan.net/oficina/base/internal/install" |
| 17 | "amadan.net/oficina/base/internal/platform" |
| 18 | "amadan.net/oficina/base/internal/store" |
| 19 | "amadan.net/oficina/base/internal/web" |
| 20 | ) |
| 21 | |
| 22 | // recorder is a fake Installer that records its calls. |
| 23 | type recorder struct { |
| 24 | mu sync.Mutex |
| 25 | installs []string |
| 26 | uninstalls []string |
| 27 | uninstallErr error |
| 28 | } |
| 29 | |
| 30 | func newRecorder() *recorder { return &recorder{} } |
| 31 | |
| 32 | func (r *recorder) Start(_ context.Context, app string) error { |
| 33 | r.mu.Lock() |
| 34 | r.installs = append(r.installs, app) |
| 35 | r.mu.Unlock() |
| 36 | return nil |
| 37 | } |
| 38 | |
| 39 | func (r *recorder) Uninstall(_ context.Context, app string) error { |
| 40 | r.mu.Lock() |
| 41 | r.uninstalls = append(r.uninstalls, app) |
| 42 | r.mu.Unlock() |
| 43 | return r.uninstallErr |
| 44 | } |
| 45 | |
| 46 | func (r *recorder) counts() (int, int) { |
| 47 | r.mu.Lock() |
| 48 | defer r.mu.Unlock() |
| 49 | return len(r.installs), len(r.uninstalls) |
| 50 | } |
| 51 | |
| 52 | const confirmCopy = "Uninstall Scratchpad? It's removed from Home for everyone at Acme. Its data is kept, and comes back if you install it again." |
| 53 | |
| 54 | func contains(body, want string) bool { |
| 55 | return strings.Contains(body, want) || strings.Contains(body, strings.ReplaceAll(want, "'", "'")) |
| 56 | } |
| 57 | |
| 58 | // Start is synchronous by contract (it only claims), so the handler calls |
| 59 | // it before redirecting. That the install then outlives the request is |
| 60 | // install.TestStartClaimsThenProvisionsInBackground's to prove. |
| 61 | func TestStoreInstallButtonPostsAndRedirects(t *testing.T) { |
| 62 | rec := newRecorder() |
| 63 | a := newAppWith(t, rec, time.UTC) |
| 64 | a.signedInOwner() |
| 65 | page := a.get("/store") |
| 66 | if page.Code != 200 || !strings.Contains(page.Body, `action="/store/scratchpad/install"`) { |
| 67 | t.Fatalf("store page lacks an install form: %d\n%s", page.Code, page.Body) |
| 68 | } |
| 69 | if r := a.post("/store/scratchpad/install", nil); r.Code != 303 || r.Location() != "/store" { |
| 70 | t.Fatalf("install: %d %q", r.Code, r.Location()) |
| 71 | } |
| 72 | rec.mu.Lock() |
| 73 | got := append([]string(nil), rec.installs...) |
| 74 | rec.mu.Unlock() |
| 75 | if len(got) != 1 || got[0] != "scratchpad" { |
| 76 | t.Fatalf("Start calls = %q, want one for scratchpad", got) |
| 77 | } |
| 78 | if r := a.post("/store/nope/install", nil); r.Code != 404 { |
| 79 | t.Fatalf("unknown app: %d", r.Code) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestStoreShowsInstallingWithRefresh(t *testing.T) { |
| 84 | a := newApp(t) |
| 85 | a.signedInOwner() |
| 86 | if strings.Contains(a.get("/store").Body, `http-equiv="refresh"`) { |
| 87 | t.Fatal("refresh with nothing installing") |
| 88 | } |
| 89 | a.setInstall("scratchpad", "installing", "") |
| 90 | body := a.get("/store").Body |
| 91 | if !strings.Contains(body, `<meta http-equiv="refresh" content="2">`) || strings.Contains(body, "<noscript>") { |
| 92 | t.Fatalf("want a plain meta refresh, not wrapped in noscript:\n%s", body) |
| 93 | } |
| 94 | if !strings.Contains(body, `disabled`) || strings.Contains(body, `action="/store/scratchpad/install"`) { |
| 95 | t.Fatalf("installing app should show a disabled button, not an install form:\n%s", body) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestStoreShowsFailedWithRetry(t *testing.T) { |
| 100 | a := newApp(t) |
| 101 | a.signedInOwner() |
| 102 | a.setInstall("scratchpad", "failed", "The app did not start answering") |
| 103 | body := a.get("/store").Body |
| 104 | if !strings.Contains(body, "The app did not start answering") { |
| 105 | t.Errorf("no failure reason:\n%s", body) |
| 106 | } |
| 107 | if !strings.Contains(body, `action="/store/scratchpad/install"`) || !strings.Contains(body, "Try again") { |
| 108 | t.Errorf("no retry:\n%s", body) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestUninstallNeedsConfirm(t *testing.T) { |
| 113 | rec := newRecorder() |
| 114 | a := newAppWith(t, rec, time.UTC) |
| 115 | a.signedInOwner() |
| 116 | a.setInstall("scratchpad", "installed", "") |
| 117 | if !strings.Contains(a.get("/store").Body, `href="/store/scratchpad/uninstall"`) { |
| 118 | t.Fatal("no uninstall link on store") |
| 119 | } |
| 120 | page := a.get("/store/scratchpad/uninstall") |
| 121 | if page.Code != 200 || !contains(page.Body, confirmCopy) { |
| 122 | t.Fatalf("confirmation: %d\n%s", page.Code, page.Body) |
| 123 | } |
| 124 | if _, u := rec.counts(); u != 0 { |
| 125 | t.Fatal("GET uninstalled") |
| 126 | } |
| 127 | if r := a.post("/store/scratchpad/uninstall", nil); r.Code != 303 || r.Location() != "/store" { |
| 128 | t.Fatalf("uninstall: %d %q", r.Code, r.Location()) |
| 129 | } |
| 130 | if _, u := rec.counts(); u != 1 { |
| 131 | t.Fatal("POST did not uninstall") |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestUninstallBusyAndGone(t *testing.T) { |
| 136 | rec := newRecorder() |
| 137 | a := newAppWith(t, rec, time.UTC) |
| 138 | a.signedInOwner() |
| 139 | rec.uninstallErr = install.ErrBusy |
| 140 | r := a.post("/store/scratchpad/uninstall", nil) |
| 141 | if r.Code != 303 || r.Location() != "/store?problem=busy" { |
| 142 | t.Fatalf("busy: %d %q", r.Code, r.Location()) |
| 143 | } |
| 144 | if !strings.Contains(a.get("/store?problem=busy").Body, install.ErrBusy.Error()) { |
| 145 | t.Error("busy message not shown") |
| 146 | } |
| 147 | rec.uninstallErr = store.ErrNoInstall |
| 148 | if r := a.post("/store/scratchpad/uninstall", nil); r.Code != 303 || r.Location() != "/store" { |
| 149 | t.Fatalf("gone: %d %q", r.Code, r.Location()) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestMemberSeesStoreButCannotInstall(t *testing.T) { |
| 154 | rec := newRecorder() |
| 155 | a := newAppWith(t, rec, time.UTC) |
| 156 | if _, _, err := store.Invite(context.Background(), a.DB, "maria@acme.co"); err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | a.setInstall("scratchpad", "installed", "") |
| 160 | if r := a.signIn("maria@acme.co"); r.Code != 303 { |
| 161 | t.Fatalf("member sign-in: %d", r.Code) |
| 162 | } |
| 163 | page := a.get("/store") |
| 164 | if page.Code != 200 || strings.Contains(page.Body, "/install") || strings.Contains(page.Body, "/uninstall") { |
| 165 | t.Fatalf("member store page: %d\n%s", page.Code, page.Body) |
| 166 | } |
| 167 | for _, p := range []string{"/store/scratchpad/install", "/store/scratchpad/uninstall"} { |
| 168 | if r := a.post(p, nil); r.Code != 403 { |
| 169 | t.Errorf("POST %s: %d", p, r.Code) |
| 170 | } |
| 171 | } |
| 172 | if r := a.get("/store/scratchpad/uninstall"); r.Code != 403 { |
| 173 | t.Errorf("GET uninstall: %d", r.Code) |
| 174 | } |
| 175 | if i, u := rec.counts(); i != 0 || u != 0 { |
| 176 | t.Fatal("member reached the installer") |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // slowWorker is the real Installer, with any blocking Install held back |
| 181 | // until release closes, as a goroutine slow to get going would be. The |
| 182 | // handler must not depend on such a goroutine for what the next page shows: |
| 183 | // Start claims before it returns. |
| 184 | type slowWorker struct { |
| 185 | *install.Installer |
| 186 | release chan struct{} |
| 187 | } |
| 188 | |
| 189 | func (s slowWorker) Install(ctx context.Context, app string) error { |
| 190 | <-s.release |
| 191 | return s.Installer.Install(ctx, app) |
| 192 | } |
| 193 | |
| 194 | // holdSetEnv holds Platform's first write until release closes. |
| 195 | type holdSetEnv struct { |
| 196 | platform.Client |
| 197 | release chan struct{} |
| 198 | } |
| 199 | |
| 200 | func (h holdSetEnv) SetEnv(ctx context.Context, app, env string, vars map[string]string) error { |
| 201 | <-h.release |
| 202 | return h.Client.SetEnv(ctx, app, env, vars) |
| 203 | } |
| 204 | |
| 205 | // Finding 5 (R33): the page the install POST redirects to already shows the |
| 206 | // app installing, with the refresh that brings the result. |
| 207 | func TestStoreRedirectShowsInstalling(t *testing.T) { |
| 208 | release := make(chan struct{}) |
| 209 | defer close(release) |
| 210 | a := newAppBuilt(t, func(db *sql.DB, cfg config.Config) web.Installer { |
| 211 | kp, err := crypto.Generate() |
| 212 | if err != nil { |
| 213 | t.Fatal(err) |
| 214 | } |
| 215 | cfg.AssertionKey = kp |
| 216 | pc := holdSetEnv{Client: platform.NewFake(), release: release} |
| 217 | in := install.New(db, cfg, pc, func(context.Context, string) error { return nil }, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 218 | return slowWorker{Installer: in, release: release} |
| 219 | }, time.UTC) |
| 220 | a.signedInOwner() |
| 221 | r := a.post("/store/scratchpad/install", nil) |
| 222 | if r.Code != 303 || r.Location() != "/store" { |
| 223 | t.Fatalf("install: %d %q", r.Code, r.Location()) |
| 224 | } |
| 225 | body := a.get(r.Location()).Body |
| 226 | if !strings.Contains(body, `<meta http-equiv="refresh" content="2">`) || !strings.Contains(body, ">Installing<") { |
| 227 | t.Fatalf("the page after Install does not show it installing:\n%s", body) |
| 228 | } |
| 229 | } |
| 230 | |