Files
iam/internal/oidc/provision_test.go
zeekay e34828c260 oidc: founding an org at onboarding never moves the founder
POST /v1/iam/onboard and POST /v1/iam/admin/provision moved the caller into
the org they founded, re-keying them to <org>/<name>. Sign-in resolves an
address in the application's org, so the founder was then found by nobody: the
sign-in screen said the address had no account and offered a second one, and
hanzo.id answered "the username or password is incorrect". Founding the same
name at cloud's door and then here also moved the person and left their first
spelling on the roster as a second owner, holding the org's key.

Both endpoints now found an org beside the account: the org row with its
Founder stamp, the caller's owner membership, and <org>/default held by the
caller. The account, how it signs in and its sessions are unchanged, and the
org rides the next token's orgs claim. A replay converges on the same org and
key. A founder resumes an org only while nobody else owns it, so one who
handed it over and was removed does not take it back, and an account founds
at most store.MaxFoundedOrgs, as the membership surface holds it. An account
of the reserved admin org founds nothing. A signup at a founding application
still moves the account it just made into its personal org (Charter), the one
converge that sets claim.move.
2026-10-16 13:50:55 -07:00

707 lines
28 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package oidc
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"sync/atomic"
"testing"
"github.com/hanzoai/orm"
"github.com/hanzoai/iam/pkg/schema"
"github.com/hanzoai/iam/pkg/store"
)
// seedUser inserts a user (owner, name) so provision has a caller to move. Mirrors
// the (owner/name) storage-id convention the bootstrap upsert uses.
func seedUserIn(t *testing.T, db orm.DB, owner, name, email string) {
t.Helper()
u := orm.New[schema.User](db)
u.Owner, u.Name = owner, name
u.Email = email
u.EmailVerified = true
u.Type = "normal-user"
u.SetId(owner + "/" + name)
if err := u.CreateCtx(context.Background()); err != nil {
t.Fatalf("seed user %s/%s: %v", owner, name, err)
}
}
// countKeys / countOrgs report how many rows an owner/name owns — used to prove a
// retry converges to exactly ONE, never a duplicate.
func countKeys(t *testing.T, db orm.DB, owner string) int {
t.Helper()
ks, err := orm.TypedQuery[schema.Key](db).Filter("Owner=", owner).GetAll(context.Background())
if err != nil && !errors.Is(err, orm.ErrNotFound) {
t.Fatalf("count keys: %v", err)
}
return len(ks)
}
// founderKey asserts that org holds no account standing between its founder and
// their key — no "<org>-default" user and no service account at all — and exactly
// ONE API key: the founder's default, the row <org>/default labelled "Default",
// held by founder ("<org>/<name>"), with a pk- publishable half and the secret
// kept only as a digest. It returns that key.
func founderKey(t *testing.T, db orm.DB, org, founder string) *schema.Key {
t.Helper()
ctx := context.Background()
if u, _ := store.GetUserByName(ctx, db, org, org+"-default"); u != nil {
t.Fatalf("an account %s/%s stands between the founder and their key", u.Owner, u.Name)
}
machines, err := orm.TypedQuery[schema.User](db).Filter("Owner=", org).Filter("Type=", schema.ServiceAccount).GetAll(ctx)
if err != nil && !errors.Is(err, orm.ErrNotFound) {
t.Fatalf("read service accounts: %v", err)
}
if len(machines) != 0 {
t.Fatalf("org %s holds %d service account(s), want none", org, len(machines))
}
ks, err := orm.TypedQuery[schema.Key](db).Filter("Owner=", org).GetAll(ctx)
if err != nil && !errors.Is(err, orm.ErrNotFound) {
t.Fatalf("read keys: %v", err)
}
if len(ks) != 1 {
t.Fatalf("org %s holds %d key(s), want exactly the founder's default", org, len(ks))
}
k := ks[0]
if k.Name != "default" || k.DisplayName != "Default" || k.User != founder {
t.Fatalf("key %s/%s %q held by %q, want %s/default \"Default\" held by %s", k.Owner, k.Name, k.DisplayName, k.User, org, founder)
}
if schema.ClassOf(k.Scope) == schema.KeyScopePublish {
t.Fatalf("the default key is publish-scoped (%q), want a secret key", k.Scope)
}
if !strings.HasPrefix(k.AccessKey, "pk-") {
t.Fatalf("publishable half = %q, want a pk-", k.AccessKey)
}
if k.AccessSecret != "" || k.AccessSecretDigest == "" {
t.Fatalf("the key must keep a secret digest and no plaintext: secret=%q digest=%q", k.AccessSecret, k.AccessSecretDigest)
}
return k
}
func countOrgs(t *testing.T, db orm.DB, name string) int {
t.Helper()
os, err := orm.TypedQuery[schema.Organization](db).Filter("Name=", name).GetAll(context.Background())
if err != nil {
t.Fatalf("count orgs: %v", err)
}
return len(os)
}
// ── fault injection ──────────────────────────────────────────────────────────
// faultyDB wraps an orm.DB and fails the Nth Put inside the NEXT transaction, so a
// test can prove provision() is ATOMIC: a fault at any step (org create, user move,
// key mint) must roll the WHOLE converge back — no org, no moved user, no key left
// behind — so a retry re-drives cleanly to ONE correct end state.
type faultyDB struct {
orm.DB
failOnPut int // 1-based Put ordinal to fail; 0 = never
puts int // Puts seen so far this run
}
// trip advances the Put counter and returns the injected fault when this is the
// configured ordinal. Shared so a fault fires whether provision writes through a
// real transaction (faultyTx, the SQLite path) or directly against the db (the
// autocommit path, where RunInTransaction does not wrap the writes).
func (f *faultyDB) trip(key orm.Key) error {
f.puts++
if f.failOnPut != 0 && f.puts == f.failOnPut {
return fmt.Errorf("injected fault at put #%d (kind=%s)", f.puts, key.Kind())
}
return nil
}
func (f *faultyDB) Put(ctx context.Context, key orm.Key, src interface{}) (orm.Key, error) {
if err := f.trip(key); err != nil {
return nil, err
}
return f.DB.Put(ctx, key, src)
}
// CreateIfAbsent is a write like Put and counts toward the same ordinal: the org
// row is inserted create-only.
func (f *faultyDB) CreateIfAbsent(ctx context.Context, key orm.Key, src interface{}) (bool, error) {
if err := f.trip(key); err != nil {
return false, err
}
return f.DB.CreateIfAbsent(ctx, key, src)
}
func (f *faultyDB) RunInTransactionWith(ctx context.Context, opts *orm.TxOptions, fn func(tx orm.DB) error) error {
return f.DB.RunInTransactionWith(ctx, opts, func(tx orm.DB) error {
return fn(&faultyTx{DB: tx, f: f})
})
}
type faultyTx struct {
orm.DB
f *faultyDB
}
func (t *faultyTx) Put(ctx context.Context, key orm.Key, src interface{}) (orm.Key, error) {
if err := t.f.trip(key); err != nil {
return nil, err
}
return t.DB.Put(ctx, key, src)
}
func (t *faultyTx) CreateIfAbsent(ctx context.Context, key orm.Key, src interface{}) (bool, error) {
if err := t.f.trip(key); err != nil {
return false, err
}
return t.DB.CreateIfAbsent(ctx, key, src)
}
// ── tests ────────────────────────────────────────────────────────────────────
// TestProvision_PersonalHappyPath: one signup converges to a personal org, the user
// as its admin, and a metered org-scoped key with a one-time secret.
func TestProvision_PersonalHappyPath(t *testing.T) {
db := openTestDB(t)
ctx := context.Background()
seedUserIn(t, db, "landing", "dave", "[email protected]")
out, err := provision(ctx, db, claim{move: true, owner: "landing", name: "dave", slug: "dave", display: "Dave", personal: true})
if err != nil {
t.Fatalf("provision: %v", err)
}
if out.org != "dave" || !out.orgCreated || !out.keyCreated {
t.Fatalf("unexpected result: %+v", out)
}
if out.accessKey == "" || out.accessSecret == "" {
t.Fatalf("first mint must reveal pk+sk, got %+v", out)
}
org, err := store.GetOrganizationByName(ctx, db, "dave")
if err != nil || org == nil || !org.IsPersonal {
t.Fatalf("org row: %+v err=%v", org, err)
}
// User is now admin of its OWN org, resolvable under the new natural key.
moved, err := store.GetUserByName(ctx, db, "dave", "dave")
if err != nil || moved == nil {
t.Fatalf("user not resolvable at dave/dave: %v", err)
}
if moved.Owner != "dave" || !moved.IsAdmin {
t.Fatalf("user is not admin of its own org: %+v", moved)
}
// Stale landing identity is gone.
if stale, _ := store.GetUserByName(ctx, db, "landing", "dave"); stale != nil {
t.Fatalf("stale identity landing/dave still present")
}
// The org's one key is the founder's.
founderKey(t, db, "dave", "dave/dave")
}
// The org a signup founds carries ONE API key and the person who founded it holds
// it. The secret it reveals once authenticates as the founder, in the org, and a
// replay names the same key without revealing the secret again or minting another.
func TestProvision_FounderHoldsTheDefaultKey(t *testing.T) {
db := openTestDB(t)
ctx := context.Background()
seedUserIn(t, db, "landing", "dave", "[email protected]")
out, err := provision(ctx, db, claim{move: true, owner: "landing", name: "dave", slug: "dave", display: "Dave", personal: true})
if err != nil {
t.Fatalf("provision: %v", err)
}
if !out.keyCreated || !strings.HasPrefix(out.accessSecret, "sk-") {
t.Fatalf("the first provision must mint the key and reveal its sk- once: %+v", out)
}
k := founderKey(t, db, "dave", "dave/dave")
if out.accessKey != k.AccessKey {
t.Fatalf("returned accessKey %q, the key holds %q", out.accessKey, k.AccessKey)
}
if k.AccessSecretDigest != schema.DigestSecret(out.accessSecret) {
t.Fatal("the key does not hold the digest of the secret it revealed")
}
h, err := store.HolderByAccessKey(ctx, db, out.accessSecret)
if err != nil {
t.Fatalf("the secret does not authenticate: %s", store.Reason(err))
}
if h.User.Owner != "dave" || h.User.Name != "dave" || h.Org != "dave" {
t.Fatalf("the secret speaks for %s/%s in %s, want dave/dave in dave", h.User.Owner, h.User.Name, h.Org)
}
again, err := provision(ctx, db, claim{move: true, owner: "dave", name: "dave", slug: "dave", display: "Dave", personal: true})
if err != nil {
t.Fatalf("replay: %v", err)
}
if again.keyCreated || again.accessSecret != "" || again.accessKey != out.accessKey {
t.Fatalf("replay = %+v, want the same accessKey %q, no secret and no new key", again, out.accessKey)
}
founderKey(t, db, "dave", "dave/dave")
}
// TestProvision_Idempotent: re-driving the SAME signup converges to ONE org + ONE
// user + ONE key, and the secret is revealed only on the first mint.
func TestProvision_Idempotent(t *testing.T) {
db := openTestDB(t)
ctx := context.Background()
seedUserIn(t, db, "landing", "dave", "[email protected]")
first, err := provision(ctx, db, claim{move: true, owner: "landing", name: "dave", slug: "dave", display: "Dave", personal: true})
if err != nil {
t.Fatalf("first provision: %v", err)
}
// Second call resolves the caller under its NEW identity (owner==name==dave),
// exactly as callerOf would after the re-key.
second, err := provision(ctx, db, claim{move: true, owner: "dave", name: "dave", slug: "dave", display: "Dave", personal: true})
if err != nil {
t.Fatalf("second provision (replay): %v", err)
}
if second.orgCreated || second.keyCreated {
t.Fatalf("replay must create nothing, got %+v", second)
}
if second.accessKey != first.accessKey {
t.Fatalf("replay returned a different key: %q != %q", second.accessKey, first.accessKey)
}
if second.accessSecret != "" {
t.Fatalf("replay must NOT re-reveal the secret, got %q", second.accessSecret)
}
if n := countOrgs(t, db, "dave"); n != 1 {
t.Fatalf("want exactly 1 org, got %d", n)
}
if n := countKeys(t, db, "dave"); n != 1 {
t.Fatalf("want exactly 1 key, got %d", n)
}
}
// TestProvision_AtomicRollbackAndRedrive: inject a failure at EACH step; every one
// must leave the store untouched (no org, no moved user, no key), and a clean
// re-drive must then converge to exactly ONE correct tenant.
func TestProvision_AtomicRollbackAndRedrive(t *testing.T) {
for _, tc := range []struct {
name string
step int // 1=org create, 2=user move, 3=key mint
}{
{"fail at org create", 1},
{"fail at user move", 2},
{"fail at key mint", 3},
} {
t.Run(tc.name, func(t *testing.T) {
real := openTestDB(t)
ctx := context.Background()
seedUserIn(t, real, "landing", "dave", "[email protected]")
// Inject the fault.
fdb := &faultyDB{DB: real, failOnPut: tc.step}
cl := claim{move: true, owner: "landing", name: "dave", slug: "dave", display: "Dave", personal: true}
if _, err := provision(ctx, fdb, cl); err == nil {
t.Fatalf("step %d: expected provision to fail", tc.step)
}
// Nothing persisted — full rollback.
if org, _ := store.GetOrganizationByName(ctx, real, "dave"); org != nil {
t.Fatalf("step %d: org leaked after rollback", tc.step)
}
if moved, _ := store.GetUserByName(ctx, real, "dave", "dave"); moved != nil {
t.Fatalf("step %d: user move leaked after rollback", tc.step)
}
if landing, _ := store.GetUserByName(ctx, real, "landing", "dave"); landing == nil || landing.Owner != "landing" {
t.Fatalf("step %d: caller not left intact in landing org", tc.step)
}
if n := countKeys(t, real, "dave"); n != 0 {
t.Fatalf("step %d: %d key(s) leaked after rollback", tc.step, n)
}
// Re-drive on the clean db → exactly one correct tenant.
out, err := provision(ctx, real, cl)
if err != nil {
t.Fatalf("step %d: re-drive failed: %v", tc.step, err)
}
if !out.orgCreated || !out.keyCreated || out.accessSecret == "" {
t.Fatalf("step %d: re-drive did not freshly provision: %+v", tc.step, out)
}
if n := countOrgs(t, real, "dave"); n != 1 {
t.Fatalf("step %d: want 1 org after re-drive, got %d", tc.step, n)
}
if n := countKeys(t, real, "dave"); n != 1 {
t.Fatalf("step %d: want 1 key after re-drive, got %d", tc.step, n)
}
if moved, _ := store.GetUserByName(ctx, real, "dave", "dave"); moved == nil || !moved.IsAdmin {
t.Fatalf("step %d: user not admin of own org after re-drive", tc.step)
}
})
}
}
// TestProvision_TenantIsolation: a second identity can NOT claim, join, or hijack an
// org another identity already founded — it is refused, and the first tenant's key
// stays sole.
func TestProvision_TenantIsolation(t *testing.T) {
db := openTestDB(t)
ctx := context.Background()
seedUserIn(t, db, "landing", "alice", "[email protected]")
seedUserIn(t, db, "landing", "mallory", "[email protected]")
if _, err := provision(ctx, db, claim{move: true, owner: "landing", name: "alice", slug: "acme", display: "Acme", personal: false}); err != nil {
t.Fatalf("alice provision: %v", err)
}
// Mallory tries to claim acme.
_, err := provision(ctx, db, claim{move: true, owner: "landing", name: "mallory", slug: "acme", display: "Acme", personal: false})
if err == nil {
t.Fatalf("mallory must not claim alice's org")
}
ft, ok := err.(*fault)
if !ok || ft.status != 409 {
t.Fatalf("want 409 conflict, got %v", err)
}
// Mallory was NOT moved into acme; acme still has exactly Alice's key.
if m, _ := store.GetUserByName(ctx, db, "acme", "mallory"); m != nil {
t.Fatalf("mallory leaked into acme")
}
if still, _ := store.GetUserByName(ctx, db, "landing", "mallory"); still == nil {
t.Fatalf("mallory should remain in landing org")
}
if n := countKeys(t, db, "acme"); n != 1 {
t.Fatalf("acme must have exactly 1 key, got %d", n)
}
}
// TestProvision_ReservedRejected: a self-service tenant can never be provisioned
// into a reserved system org — above all the SuperAdmin `admin` org (provision, do
// not promote). Enforced in the primitive itself, before any write.
func TestProvision_ReservedRejected(t *testing.T) {
db := openTestDB(t)
ctx := context.Background()
seedUserIn(t, db, "landing", "eve", "[email protected]")
for _, slug := range []string{"admin", "built-in", "app"} {
_, err := provision(ctx, db, claim{move: true, owner: "landing", name: "eve", slug: slug, display: slug, personal: false})
ft, ok := err.(*fault)
if !ok || ft.status != 400 {
t.Fatalf("slug %q: want 400 reserved, got %v", slug, err)
}
// Eve was not promoted into the reserved org.
if e, _ := store.GetUserByName(ctx, db, slug, "eve"); e != nil {
t.Fatalf("eve promoted into reserved org %q", slug)
}
}
// Crucially, the real admin org membership is untouched.
if e, _ := store.GetUserByName(ctx, db, "admin", "eve"); e != nil {
t.Fatalf("eve leaked into the SuperAdmin admin org")
}
}
// ── autocommit (ZAP-contract) emulation ──────────────────────────────────────
// The production `sql`/`datastore` backend autocommits every write independently —
// its RunInTransaction opens NO transaction, so a fault mid-provision does NOT roll
// back. autocommitDB emulates that contract over the SQLite store: RunInTransaction*
// runs fn directly (each Put/Get autocommits, no rollback), so a test can prove
// provision converges on the backend production actually runs, not just on SQLite's
// serialized transaction. Composed with faultyDB it proves the Founder RESUME path:
// a fault after the org write leaves the org committed, and a retry completes it.
type autocommitDB struct{ orm.DB }
func (a *autocommitDB) RunInTransactionWith(ctx context.Context, _ *orm.TxOptions, fn func(tx orm.DB) error) error {
return fn(a.DB)
}
func (a *autocommitDB) RunInTransaction(ctx context.Context, fn func(tx orm.DB) error) error {
return fn(a.DB)
}
// TestProvision_ResumeUnderAutocommit: with NO transaction rollback (production
// contract), a fault after the org is written leaves it committed and the caller not
// yet moved. A retry must RESUME via the Founder stamp — complete the move — instead
// of the permanent "already exists" 409 that stranded the original bug. This is the
// backend production runs, proven without a live ZAP server.
func TestProvision_ResumeUnderAutocommit(t *testing.T) {
real := openTestDB(t)
ctx := context.Background()
seedUserIn(t, real, "landing", "dave", "[email protected]")
cl := claim{move: true, owner: "landing", name: "dave", slug: "dave", display: "Dave", personal: true}
// Autocommit (no rollback) + fault at the user-move write (Put #2).
faulty := &autocommitDB{DB: &faultyDB{DB: real, failOnPut: 2}}
if _, err := provision(ctx, faulty, cl); err == nil {
t.Fatalf("expected the injected fault to fail provision")
}
// Under autocommit the org is NOT rolled back — it persists, founder-stamped,
// with the caller NOT yet moved (the exact orphan state that stranded on 409).
org, _ := store.GetOrganizationByName(ctx, real, "dave")
if org == nil {
t.Fatalf("autocommit should have left the org committed")
}
if moved, _ := store.GetUserByName(ctx, real, "dave", "dave"); moved != nil {
t.Fatalf("user should NOT have been moved before the fault")
}
// Retry on the same autocommit backend → RESUME (not 409), converging to one
// org + admin + key.
out, err := provision(ctx, &autocommitDB{DB: real}, cl)
if err != nil {
t.Fatalf("retry must RESUME the founder's own org, got: %v", err)
}
if out.orgCreated {
t.Fatalf("retry should have found the existing org, not recreated it")
}
if moved, _ := store.GetUserByName(ctx, real, "dave", "dave"); moved == nil || !moved.IsAdmin {
t.Fatalf("retry did not move the caller in as admin")
}
if n := countOrgs(t, real, "dave"); n != 1 {
t.Fatalf("want exactly 1 org after resume, got %d", n)
}
if n := countKeys(t, real, "dave"); n != 1 {
t.Fatalf("want exactly 1 key after resume, got %d", n)
}
}
// TestProvision_FounderFencesResume: a DIFFERENT identity can never complete an org
// another identity founded — even under autocommit, where the org may sit
// half-provisioned. The Founder stamp fences it to one tenant.
func TestProvision_FounderFencesResume(t *testing.T) {
real := openTestDB(t)
ctx := context.Background()
seedUserIn(t, real, "landing", "alice", "[email protected]")
seedUserIn(t, real, "landing", "mallory", "[email protected]")
// Alice's onboard faults after the org write (autocommit → org persists, founder=alice).
faulty := &autocommitDB{DB: &faultyDB{DB: real, failOnPut: 2}}
if _, err := provision(ctx, faulty, claim{move: true, owner: "landing", name: "alice", slug: "acme", display: "Acme"}); err == nil {
t.Fatalf("expected Alice's provision to fault")
}
if org, _ := store.GetOrganizationByName(ctx, real, "acme"); org == nil {
t.Fatalf("acme should be half-provisioned (autocommit)")
}
// Mallory tries to complete/claim Alice's half-provisioned org → refused.
_, err := provision(ctx, &autocommitDB{DB: real}, claim{move: true, owner: "landing", name: "mallory", slug: "acme", display: "Acme"})
if ft, ok := err.(*fault); !ok || ft.status != 409 {
t.Fatalf("mallory must be refused (409) on Alice's founded org, got %v", err)
}
if m, _ := store.GetUserByName(ctx, real, "acme", "mallory"); m != nil {
t.Fatalf("mallory leaked into Alice's org")
}
// Alice retries → resumes her own org.
if _, err := provision(ctx, &autocommitDB{DB: real}, claim{move: true, owner: "landing", name: "alice", slug: "acme", display: "Acme"}); err != nil {
t.Fatalf("alice must resume her own org, got %v", err)
}
if a, _ := store.GetUserByName(ctx, real, "acme", "alice"); a == nil || !a.IsAdmin {
t.Fatalf("alice did not become admin of her resumed org")
}
}
// TestProvision_SecondOrgRefused: once an identity admins its OWN org, founding a
// SECOND via onboarding is refused — a move would orphan the first (its billing
// account + metered key left ownerless).
func TestProvision_SecondOrgRefused(t *testing.T) {
db := openTestDB(t)
ctx := context.Background()
seedUserIn(t, db, "landing", "dave", "[email protected]")
if _, err := provision(ctx, db, claim{move: true, owner: "landing", name: "dave", slug: "dave", display: "Dave", personal: true}); err != nil {
t.Fatalf("first onboard: %v", err)
}
// Now admin of "dave"; founding "daveteam" must be refused.
_, err := provision(ctx, db, claim{move: true, owner: "dave", name: "dave", slug: "daveteam", display: "Dave Team"})
if ft, ok := err.(*fault); !ok || ft.status != 409 {
t.Fatalf("founding a second org must be refused 409, got %v", err)
}
if n := countOrgs(t, db, "daveteam"); n != 0 {
t.Fatalf("the refused second org must NOT be created, got %d", n)
}
// The first org is intact and still admined.
if a, _ := store.GetUserByName(ctx, db, "dave", "dave"); a == nil || !a.IsAdmin {
t.Fatalf("first org's admin was disturbed")
}
}
// TestProvision_StaleCredentialReplay: a retry carrying the PRE-move credential
// (owner still the landing org, because the client has not re-authenticated yet)
// still converges — the caller is re-resolved under the target slug.
func TestProvision_StaleCredentialReplay(t *testing.T) {
db := openTestDB(t)
ctx := context.Background()
seedUserIn(t, db, "landing", "dave", "[email protected]")
if _, err := provision(ctx, db, claim{move: true, owner: "landing", name: "dave", slug: "dave", display: "Dave", personal: true}); err != nil {
t.Fatalf("first onboard: %v", err)
}
// Replay with the STALE pre-move owner ("landing") — dave now lives in "dave".
out, err := provision(ctx, db, claim{move: true, owner: "landing", name: "dave", slug: "dave", display: "Dave", personal: true})
if err != nil {
t.Fatalf("stale-credential replay must converge, got %v", err)
}
if out.orgCreated {
t.Fatalf("replay must not recreate the org")
}
if n := countOrgs(t, db, "dave"); n != 1 {
t.Fatalf("want exactly 1 org, got %d", n)
}
}
// TestProvision_ConcurrentSameSlugOneWinner: N distinct callers race for the SAME
// org name. Exactly ONE wins the tenant; the rest are refused. The store converges
// to ONE org + ONE key — no duplicate billing account, no orphan — under real
// contention (run with -race).
func TestProvision_ConcurrentSameSlugOneWinner(t *testing.T) {
db := openTestDB(t)
ctx := context.Background()
const n = 6
for i := 0; i < n; i++ {
seedUserIn(t, db, "landing", fmt.Sprintf("u%d", i), fmt.Sprintf("u%[email protected]", i))
}
var wg sync.WaitGroup
var success int64
for i := 0; i < n; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if _, err := provision(ctx, db, claim{move: true, owner: "landing", name: fmt.Sprintf("u%d", i), slug: "team", display: "Team", personal: false}); err == nil {
atomic.AddInt64(&success, 1)
}
}(i)
}
wg.Wait()
if success != 1 {
t.Fatalf("want exactly 1 winner, got %d", success)
}
if c := countOrgs(t, db, "team"); c != 1 {
t.Fatalf("want exactly 1 org, got %d", c)
}
if c := countKeys(t, db, "team"); c != 1 {
t.Fatalf("want exactly 1 key, got %d", c)
}
}
// TestProvision_ConcurrentSameCallerConverges: the SAME caller double-submits N
// times at once (a console retry storm). The store converges to exactly ONE org,
// ONE key, and ONE admin — never a duplicate or an orphan.
func TestProvision_ConcurrentSameCallerConverges(t *testing.T) {
db := openTestDB(t)
ctx := context.Background()
seedUserIn(t, db, "landing", "dave", "[email protected]")
var wg sync.WaitGroup
const n = 6
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, _ = provision(ctx, db, claim{move: true, owner: "landing", name: "dave", slug: "dave", display: "Dave", personal: true})
}()
}
wg.Wait()
if c := countOrgs(t, db, "dave"); c != 1 {
t.Fatalf("want exactly 1 org, got %d", c)
}
if c := countKeys(t, db, "dave"); c != 1 {
t.Fatalf("want exactly 1 key, got %d", c)
}
admins := 0
us, _ := orm.TypedQuery[schema.User](db).Filter("Owner=", "dave").GetAll(ctx)
for _, u := range us {
if u.IsAdmin {
admins++
}
}
if admins != 1 {
t.Fatalf("want exactly 1 admin of dave, got %d", admins)
}
}
// A founder whose default key is gone — revoked, or a write that never landed —
// gets it back on the next converge, and the key issued then authenticates as
// them. A further replay is a replay: the same key, and no second reveal.
func TestProvision_ConvergesAFounderWhoHoldsNoKey(t *testing.T) {
db := openTestDB(t)
ctx := context.Background()
seedUserIn(t, db, "landing", "erin", "[email protected]")
if _, err := provision(ctx, db, claim{move: true, owner: "landing", name: "erin", slug: "erin", display: "Erin"}); err != nil {
t.Fatalf("provision: %v", err)
}
k, err := orm.Get[schema.Key](db, "erin/default")
if err != nil {
t.Fatalf("read the default key: %v", err)
}
if err := k.DeleteCtx(ctx); err != nil {
t.Fatalf("drop the default key: %v", err)
}
out, err := provision(ctx, db, claim{move: true, owner: "erin", name: "erin", slug: "erin", display: "Erin"})
if err != nil {
t.Fatalf("replay: %v", err)
}
if !out.keyCreated || out.accessSecret == "" {
t.Fatalf("the replay did not issue the missing key: %+v", out)
}
founderKey(t, db, "erin", "erin/erin")
h, err := store.HolderByAccessKey(ctx, db, out.accessSecret)
if err != nil {
t.Fatalf("the converged key does not authenticate: %s", store.Reason(err))
}
if h.User.Owner != "erin" || h.User.Name != "erin" || h.Org != "erin" {
t.Fatalf("it speaks for %s/%s in %s, want erin/erin in erin", h.User.Owner, h.User.Name, h.Org)
}
again, err := provision(ctx, db, claim{move: true, owner: "erin", name: "erin", slug: "erin", display: "Erin"})
if err != nil {
t.Fatalf("second replay: %v", err)
}
if again.keyCreated || again.accessSecret != "" {
t.Fatalf("a replay re-issued the key: %+v", again)
}
if again.accessKey != out.accessKey {
t.Fatalf("replay named %q, want the key already held %q", again.accessKey, out.accessKey)
}
}
// TestProvision_HeldNameRefused: a first run never founds an org whose name is
// still held by a removed org's leftovers — an account filed under it would be
// inside the new org, and a key would authenticate into it.
func TestProvision_HeldNameRefused(t *testing.T) {
db := openTestDB(t)
ctx := context.Background()
seedUserIn(t, db, "landing", "eve", "[email protected]")
seedUserIn(t, db, "widgets", "mallory", "[email protected]") // left behind by a removed "widgets"
_, err := provision(ctx, db, claim{move: true, owner: "landing", name: "eve", slug: "widgets", display: "Widgets"})
if ft, ok := err.(*fault); !ok || ft.status != 409 {
t.Fatalf("want 409 for a held name, got %v", err)
}
if org, _ := store.GetOrganizationByName(ctx, db, "widgets"); org != nil {
t.Fatalf("org founded over another org's leftovers")
}
}
// TestProvision_HeldNamesAndHiddenDisplayRefused: a first run never founds a held
// name (a brand, an org funded by name, the estate's own namespaces), and never
// stores a display name that draws nothing or reorders the text around it.
func TestProvision_HeldNamesAndHiddenDisplayRefused(t *testing.T) {
db := openTestDB(t)
ctx := context.Background()
seedUserIn(t, db, "landing", "eve", "[email protected]")
for _, slug := range []string{"osage", "luxfi", "hanzo-inc", "hanzo-community", "zooai"} {
_, err := provision(ctx, db, claim{move: true, owner: "landing", name: "eve", slug: slug, display: slug})
if ft, ok := err.(*fault); !ok || ft.status != 400 {
t.Fatalf("slug %q: want 400 held, got %v", slug, err)
}
}
for _, display := range []string{"Acme\u202eeVIL", "\u3164", "\u2800", "Ac\u2028me"} {
_, err := provision(ctx, db, claim{move: true, owner: "landing", name: "eve", slug: "acme", display: display})
if ft, ok := err.(*fault); !ok || ft.status != 400 {
t.Fatalf("display %q: want 400, got %v", display, err)
}
}
if org, _ := store.GetOrganizationByName(ctx, db, "acme"); org != nil {
t.Fatal("an org was founded with a hidden display name")
}
}