| index 09dadf8e7..51248d8e0 100644 |
| --- a/internal/console/materialize_test.go |
| +++ b/internal/console/materialize_test.go |
| @@ -4,7 +4,9 @@ import ( |
| "bytes" |
| "context" |
| "encoding/json" |
| + "errors" |
| "log" |
| + "net/http" |
| "os" |
| "strings" |
| "testing" |
| @@ -728,3 +730,189 @@ func TestMaterializeWithNoStoreIsANoOp(t *testing.T) { |
| t.Errorf("MaterializeApps(nil store) = %v, want nil", err) |
| } |
| } |
| + |
| +// The platform-link tests' fixed sqids: bdf is the customer, bcd its |
| +// platform. linkMint is a mint id of the shape the console reserves. |
| +const ( |
| + linkCustomerID = int64(74) // Sqid(74) == "bdf" |
| + linkPlatform = "bcd" |
| + linkMint = "0123456789abcdef0123456789abcdef" |
| +) |
| + |
| +// putLinkManifest writes the customer manifest bdf, linked to platform with |
| +// ref and mint and no owner, over whatever is there. |
| +func putLinkManifest(t *testing.T, rel release.Store, platform, ref, mint string) { |
| + t.Helper() |
| + ctx := t.Context() |
| + sqid := domains.Sqid(linkCustomerID) |
| + if sqid != "bdf" { |
| + t.Fatalf("Sqid(%d) = %q, want bdf", linkCustomerID, sqid) |
| + } |
| + key := domains.AccountManifestKey(sqid) |
| + _, etag, err := rel.Get(ctx, key) |
| + if err != nil && !errors.Is(err, os.ErrNotExist) { |
| + t.Fatal(err) |
| + } |
| + body, err := json.Marshal(domains.AccountManifest{ID: linkCustomerID, Sqid: sqid, Name: "Shop", |
| + Platform: platform, PlatformRef: ref, PlatformMint: mint, CreatedAt: time.Unix(1700000000, 0).UTC()}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := rel.Put(ctx, key, body, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// accountPlatformOf is AccountPlatform, failing the test on error. |
| +func accountPlatformOf(t *testing.T, st *Store, id int64) (string, string) { |
| + t.Helper() |
| + p, ref, err := st.AccountPlatform(t.Context(), id) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return p, ref |
| +} |
| + |
| +// A customer manifest carries its link from the create-only mint, and the |
| +// console's row is born with it: that is how the link survives a console |
| +// rebuild (spec §5.2). A customer has no owner, and materialization does |
| +// not invent one. |
| +func TestMaterializeCarriesThePlatformLinkAtBirth(t *testing.T) { |
| + st := openTestStore(t) |
| + rel := release.NewDirStore(t.TempDir()) |
| + putLinkManifest(t, rel, linkPlatform, "4812", linkMint) |
| + |
| + if n, err := MaterializeAccounts(t.Context(), rel, st); err != nil || n != 1 { |
| + t.Fatalf("materialized %d (%v), want 1", n, err) |
| + } |
| + if p, ref := accountPlatformOf(t, st, linkCustomerID); p != linkPlatform || ref != "4812" { |
| + t.Errorf("AccountPlatform(bdf) = %q, %q; want bcd, 4812", p, ref) |
| + } |
| + if members, _ := st.Members(linkCustomerID); len(members) != 0 { |
| + t.Errorf("Members(bdf) = %+v, want none: a customer is born held by its platform", members) |
| + } |
| +} |
| + |
| +// The link is adopted at birth only, for the reason the owner is: the |
| +// manifest is not write-once, so adopting a link on a later pass would let |
| +// anyone with bucket write access hand a live account to a platform. |
| +func TestMaterializeNeverLinksAnAccountAfterBirth(t *testing.T) { |
| + st := openTestStore(t) |
| + rel := release.NewDirStore(t.TempDir()) |
| + putLinkManifest(t, rel, "", "", "") |
| + if n, err := MaterializeAccounts(t.Context(), rel, st); err != nil || n != 1 { |
| + t.Fatalf("materialized %d (%v), want 1", n, err) |
| + } |
| + |
| + putLinkManifest(t, rel, linkPlatform, "4812", linkMint) |
| + if n, err := MaterializeAccounts(t.Context(), rel, st); err != nil || n != 0 { |
| + t.Fatalf("second pass materialized %d (%v), want 0", n, err) |
| + } |
| + if p, ref := accountPlatformOf(t, st, linkCustomerID); p != "" || ref != "" { |
| + t.Errorf("AccountPlatform(bdf) = %q, %q after a rewritten manifest; want unlinked", p, ref) |
| + } |
| + if cs, err := st.LinkedCustomers(t.Context(), linkPlatform); err != nil || len(cs) != 0 { |
| + t.Errorf("LinkedCustomers(bcd) = %+v (%v), want none", cs, err) |
| + } |
| +} |
| + |
| +// A link MintLinkedAccount would refuse, or one naming the account itself, |
| +// is not adopted. The account is still born (one bad field must not hide |
| +// it), unlinked: with no owner that is unreachable, the safe side. |
| +func TestMaterializeRefusesASelfOrOpsLink(t *testing.T) { |
| + cases := []struct{ name, platform, mint string }{ |
| + {"itself", "bdf", linkMint}, |
| + {"the ops account", domains.Sqid(domains.ReservedAccountID), linkMint}, |
| + {"a name, not a sqid", "ops", linkMint}, |
| + {"not a sqid", "not a sqid!", linkMint}, |
| + {"no mint id", linkPlatform, ""}, |
| + {"an uppercase mint id", linkPlatform, strings.ToUpper(linkMint)}, |
| + } |
| + for _, c := range cases { |
| + t.Run(c.name, func(t *testing.T) { |
| + var logged bytes.Buffer |
| + log.SetOutput(&logged) |
| + t.Cleanup(func() { log.SetOutput(os.Stderr) }) |
| + st := openTestStore(t) |
| + rel := release.NewDirStore(t.TempDir()) |
| + putLinkManifest(t, rel, c.platform, "4812", c.mint) |
| + |
| + if n, err := MaterializeAccounts(t.Context(), rel, st); err != nil || n != 1 { |
| + t.Fatalf("materialized %d (%v), want the account itself", n, err) |
| + } |
| + if a, err := st.AccountBySqid("bdf"); err != nil || a == nil { |
| + t.Fatalf("AccountBySqid(bdf) = %v %v, want the row", a, err) |
| + } |
| + if p, ref := accountPlatformOf(t, st, linkCustomerID); p != "" || ref != "" { |
| + t.Errorf("AccountPlatform(bdf) = %q, %q; want unlinked", p, ref) |
| + } |
| + if !strings.Contains(logged.String(), domains.AccountManifestKey("bdf")) { |
| + t.Errorf("no log line names the manifest; logged:\n%s", logged.String()) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// A customer minted in the bucket whose console row was never written (a |
| +// create that failed after the mint, or a rebuilt console) is adopted, and |
| +// its row carries the manifest's mint id, so the platform's pending mint |
| +// and the account count once (pre-flight C15). |
| +func TestMaterializeAdoptsAStrandedLinkedMint(t *testing.T) { |
| + st := openTestStore(t) |
| + rel := release.NewDirStore(t.TempDir()) |
| + ctx := t.Context() |
| + newPlatform(t, st, linkPlatform) |
| + reserveMint(t, st, linkPlatform, linkMint, "4812") |
| + id, sqid, existing, err := domains.MintLinkedAccount(ctx, rel, "Shop", linkPlatform, "4812", linkMint, platformNow) |
| + if err != nil || existing { |
| + t.Fatalf("MintLinkedAccount = %d %q %v %v", id, sqid, existing, err) |
| + } |
| + |
| + if n, err := MaterializeAccounts(ctx, rel, st); err != nil || n != 1 { |
| + t.Fatalf("materialized %d (%v), want 1", n, err) |
| + } |
| + cs, err := st.LinkedCustomers(ctx, linkPlatform) |
| + if err != nil || len(cs) != 1 || cs[0].Sqid != sqid || cs[0].Ref != "4812" { |
| + t.Fatalf("LinkedCustomers(bcd) = %+v (%v), want %s with ref 4812", cs, err, sqid) |
| + } |
| + var mint string |
| + if err := st.db.QueryRowContext(ctx, `SELECT platform_mint FROM accounts WHERE id = ?`, id).Scan(&mint); err != nil { |
| + t.Fatal(err) |
| + } |
| + if mint != linkMint { |
| + t.Errorf("platform_mint = %q, want the manifest's %q", mint, linkMint) |
| + } |
| + if n := linkedCount(t, st, linkPlatform); n != 1 { |
| + t.Errorf("LinkedCustomerCount(bcd) = %d, want 1: the pending mint is this account", n) |
| + } |
| +} |
| + |
| +// Ownerless while linked is a supported state (spec §5.3), and membership is |
| +// authorization: nobody signed in reaches the customer through the person |
| +// doors. The platform's own doors are later tasks'. |
| +func TestLinkedOwnerlessAccountReachesNobody(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + putLinkManifest(t, fx.rel, linkPlatform, "4812", linkMint) |
| + if _, err := MaterializeAccounts(t.Context(), fx.rel, fx.st); err != nil { |
| + t.Fatal(err) |
| + } |
| + if p, _ := accountPlatformOf(t, fx.st, linkCustomerID); p != linkPlatform { |
| + t.Fatalf("AccountPlatform(bdf) = %q, want bcd", p) |
| + } |
| + browser := signInPaul(t, fx) |
| + for _, addr := range []string{"paul@keymail.dev", "owner-bcd@example.com", "someone@example.com"} { |
| + accts, err := fx.st.AccountsFor(addr) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, a := range accts { |
| + if a.ID == linkCustomerID { |
| + t.Errorf("AccountsFor(%s) includes the ownerless customer bdf", addr) |
| + } |
| + } |
| + } |
| + status, _, body := get(t, browser, fx.ts.URL+"/a/bdf/") |
| + if status != http.StatusNotFound || body != "404 page not found\n" { |
| + t.Errorf("GET /a/bdf/ signed in = %d %q, want the stdlib 404", status, body) |
| + } |
| +} |