package console import ( "bytes" "context" "errors" "log" "net/http" "net/url" "os" "strings" "testing" "time" ) func TestPermanentClaimsRefuseTheTmpNamespace(t *testing.T) { fx := newConsoleFixture(t) acct := opsAccount(t, fx) if got, _, _ := fx.srv.claimApp(context.Background(), acct, "tmp-docs", "paul@keymail.dev", "", true, regionReq{}); got != claimTemporaryReserved { t.Fatalf("claimApp(tmp-docs) = %v, want claimTemporaryReserved", got) } // And a temporary claim refuses anything outside it. if got, _, _ := fx.srv.claimAppWith(context.Background(), acct, "docs", "paul@keymail.dev", "", false, regionReq{}, time.Now().Add(time.Hour)); got != claimInvalid { t.Fatalf("temporary claimAppWith(docs) = %v, want claimInvalid", got) } // The CLI door. token := cliToken(t, fx, "paul@keymail.dev", "code-tmp-new") status, out := postJSON(t, fx.ts.URL+"/api/cli/apps/bab/new", map[string]string{"name": "tmp-docs"}, token) if status != http.StatusConflict || !strings.Contains(str(t, out, "error"), "tmp-") { t.Fatalf("/new tmp-docs = %d %v, want 409 naming the tmp- reservation", status, out) } // The browser door. c, csrf := signInAs(t, fx, "paul@keymail.dev", "code-tmp-form") status, loc, _ := postForm(t, c, fx.u("/apps/new"), url.Values{"csrf": {csrf}, "name": {"tmp-docs"}}) if status != http.StatusSeeOther || loc != fx.path("/apps/new") { t.Fatalf("browser create tmp-docs = %d -> %q, want 303 back to the form", status, loc) } if _, _, body := get(t, c, fx.u("/apps/new")); !strings.Contains(body, "tmp-") { t.Fatal("browser create tmp-docs: the flash does not name the tmp- reservation") } if accts, _ := fx.st.AppAccounts("tmp-docs"); len(accts) != 0 { t.Fatalf("a permanent door created tmp-docs in %v", accts) } } func TestRenameRefusesATmpDestination(t *testing.T) { fx := newConsoleFixture(t) if _, err := fx.st.CreateAppIn(OpsAccountID, "draft", "paul@keymail.dev", time.Now()); err != nil { t.Fatal(err) } c, csrf := signInAs(t, fx, "paul@keymail.dev", "code-tmp-rename") status, _, body := postForm(t, c, fx.u("/apps/draft/rename"), url.Values{"csrf": {csrf}, "to": {"tmp-draft"}}) if status != http.StatusUnprocessableEntity || !strings.Contains(body, "tmp-") { t.Fatalf("rename to tmp-draft = %d, want 422 naming the reservation", status) } if err := fx.st.RenameAppIn(OpsAccountID, "draft", "tmp-draft"); !errors.Is(err, ErrTemporaryNameReserved) { t.Fatalf("RenameAppIn onto tmp- = %v, want ErrTemporaryNameReserved", err) } // The undo of a kept tmp- app's rename is the one way back into the // namespace, and only onto the name it came from. if _, err := fx.st.CreateAppIn(OpsAccountID, "tmp-kept", "paul@keymail.dev", time.Now()); err != nil { t.Fatal(err) } if err := fx.st.RenameAppIn(OpsAccountID, "tmp-kept", "kept"); err != nil { t.Fatal(err) } if err := fx.st.UndoRenameAppIn(OpsAccountID, "kept", "tmp-kept"); err != nil { t.Fatalf("UndoRenameAppIn = %v, want the undo to land", err) } } func TestGrantAndBackfillRespectTheTmpNamespace(t *testing.T) { st := openTestStore(t) if err := st.Grant("paul@keymail.dev", "tmp-x"); !errors.Is(err, ErrTemporaryNameReserved) { t.Fatalf("Grant(tmp-x) = %v, want ErrTemporaryNameReserved", err) } // A grant written before the reservation existed. if _, err := st.db.Exec(`INSERT INTO owners (address, app, created_at) VALUES ('paul@keymail.dev', 'tmp-old', 1)`); err != nil { t.Fatal(err) } var logged bytes.Buffer log.SetOutput(&logged) t.Cleanup(func() { log.SetOutput(os.Stderr) }) if _, err := st.BackfillOpsMemberships(time.Unix(1700000000, 0)); err != nil { t.Fatal(err) } if has, _ := st.HasApp("tmp-old"); has { t.Fatal("backfill turned a tmp- grant into a permanent row") } if !strings.Contains(logged.String(), "tmp-old") { t.Fatalf("backfill skipped tmp-old silently; log:\n%s", logged.String()) } }