carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package console
2
3import (
4 "bytes"
5 "context"
6 "errors"
7 "log"
8 "net/http"
9 "net/url"
10 "os"
11 "strings"
12 "testing"
13 "time"
14)
15
16func TestPermanentClaimsRefuseTheTmpNamespace(t *testing.T) {
17 fx := newConsoleFixture(t)
18 acct := opsAccount(t, fx)
19 if got, _, _ := fx.srv.claimApp(context.Background(), acct, "tmp-docs", "paul@keymail.dev", "", true, regionReq{}); got != claimTemporaryReserved {
20 t.Fatalf("claimApp(tmp-docs) = %v, want claimTemporaryReserved", got)
21 }
22 // And a temporary claim refuses anything outside it.
23 if got, _, _ := fx.srv.claimAppWith(context.Background(), acct, "docs", "paul@keymail.dev", "", false, regionReq{}, time.Now().Add(time.Hour)); got != claimInvalid {
24 t.Fatalf("temporary claimAppWith(docs) = %v, want claimInvalid", got)
25 }
26
27 // The CLI door.
28 token := cliToken(t, fx, "paul@keymail.dev", "code-tmp-new")
29 status, out := postJSON(t, fx.ts.URL+"/api/cli/apps/bab/new", map[string]string{"name": "tmp-docs"}, token)
30 if status != http.StatusConflict || !strings.Contains(str(t, out, "error"), "tmp-") {
31 t.Fatalf("/new tmp-docs = %d %v, want 409 naming the tmp- reservation", status, out)
32 }
33
34 // The browser door.
35 c, csrf := signInAs(t, fx, "paul@keymail.dev", "code-tmp-form")
36 status, loc, _ := postForm(t, c, fx.u("/apps/new"), url.Values{"csrf": {csrf}, "name": {"tmp-docs"}})
37 if status != http.StatusSeeOther || loc != fx.path("/apps/new") {
38 t.Fatalf("browser create tmp-docs = %d -> %q, want 303 back to the form", status, loc)
39 }
40 if _, _, body := get(t, c, fx.u("/apps/new")); !strings.Contains(body, "tmp-") {
41 t.Fatal("browser create tmp-docs: the flash does not name the tmp- reservation")
42 }
43 if accts, _ := fx.st.AppAccounts("tmp-docs"); len(accts) != 0 {
44 t.Fatalf("a permanent door created tmp-docs in %v", accts)
45 }
46}
47
48func TestRenameRefusesATmpDestination(t *testing.T) {
49 fx := newConsoleFixture(t)
50 if _, err := fx.st.CreateAppIn(OpsAccountID, "draft", "paul@keymail.dev", time.Now()); err != nil {
51 t.Fatal(err)
52 }
53 c, csrf := signInAs(t, fx, "paul@keymail.dev", "code-tmp-rename")
54 status, _, body := postForm(t, c, fx.u("/apps/draft/rename"), url.Values{"csrf": {csrf}, "to": {"tmp-draft"}})
55 if status != http.StatusUnprocessableEntity || !strings.Contains(body, "tmp-") {
56 t.Fatalf("rename to tmp-draft = %d, want 422 naming the reservation", status)
57 }
58 if err := fx.st.RenameAppIn(OpsAccountID, "draft", "tmp-draft"); !errors.Is(err, ErrTemporaryNameReserved) {
59 t.Fatalf("RenameAppIn onto tmp- = %v, want ErrTemporaryNameReserved", err)
60 }
61 // The undo of a kept tmp- app's rename is the one way back into the
62 // namespace, and only onto the name it came from.
63 if _, err := fx.st.CreateAppIn(OpsAccountID, "tmp-kept", "paul@keymail.dev", time.Now()); err != nil {
64 t.Fatal(err)
65 }
66 if err := fx.st.RenameAppIn(OpsAccountID, "tmp-kept", "kept"); err != nil {
67 t.Fatal(err)
68 }
69 if err := fx.st.UndoRenameAppIn(OpsAccountID, "kept", "tmp-kept"); err != nil {
70 t.Fatalf("UndoRenameAppIn = %v, want the undo to land", err)
71 }
72}
73
74func TestGrantAndBackfillRespectTheTmpNamespace(t *testing.T) {
75 st := openTestStore(t)
76 if err := st.Grant("paul@keymail.dev", "tmp-x"); !errors.Is(err, ErrTemporaryNameReserved) {
77 t.Fatalf("Grant(tmp-x) = %v, want ErrTemporaryNameReserved", err)
78 }
79 // A grant written before the reservation existed.
80 if _, err := st.db.Exec(`INSERT INTO owners (address, app, created_at) VALUES ('paul@keymail.dev', 'tmp-old', 1)`); err != nil {
81 t.Fatal(err)
82 }
83 var logged bytes.Buffer
84 log.SetOutput(&logged)
85 t.Cleanup(func() { log.SetOutput(os.Stderr) })
86 if _, err := st.BackfillOpsMemberships(time.Unix(1700000000, 0)); err != nil {
87 t.Fatal(err)
88 }
89 if has, _ := st.HasApp("tmp-old"); has {
90 t.Fatal("backfill turned a tmp- grant into a permanent row")
91 }
92 if !strings.Contains(logged.String(), "tmp-old") {
93 t.Fatalf("backfill skipped tmp-old silently; log:\n%s", logged.String())
94 }
95}
96