package altacompat // Existing-team adoption at Home's real operator code: the plan Alta signs // for the six production apps, and the reserved label that keeps every // other plan out of it (internal/operator existing.go, plan.go). import ( "context" "database/sql" "errors" "strings" "testing" "time" "amadan.net/oficina/alta/homeop" "amadan.net/oficina/alta/provision" "amadan.net/oficina/home/internal/operator" ) // adoptionPlan is the plan Alta signs for role: create-team under the // reserved label, bound to the proposed team SQID. Home's credential role // carries the owner; its directory role the label and name. func (r *rig) adoptionPlan(t *testing.T, role, existing string) []byte { t.Helper() p, err := r.client.NewPlan(role, homeop.Token(), homeop.KindCreateTeam, adoptionApps, r.home.now) if err != nil { t.Fatal(err) } if role == homeop.Directory { p.Label, p.DisplayName = "oficina", "Oficina" } else { p.Owner = owner } p.ExistingTeam = existing raw, err := homeop.SignPlan(r.client.Key, p) if err != nil { t.Fatal(err) } return raw } func TestHomeAcceptsAltaSignedExistingTeamPlan(t *testing.T) { r := newRig(t) ctx := context.Background() run, err := r.engine.StartInitialize(ctx, provision.InitializeAttestation) if err != nil { t.Fatal(err) } r.home.confirm(t, "directory", "") if got := r.drive(t, run); got.State != "done" { t.Fatalf("initialize: %s %s", got.State, got.Code) } r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) sqid, err := operator.TeamSQID(1) if err != nil { t.Fatal(err) } // An ordinary create-team under the reserved label is refused on the // directory role: nothing but an adoption may name "oficina". if o := r.client.SubmitPlan(ctx, homeop.Directory, r.adoptionPlan(t, homeop.Directory, "")); o != homeop.Refused { t.Fatalf("ordinary create-team under oficina: %v", o) } if _, err := r.home.dirPlans.Pending(ctx, r.home.now); !errors.Is(err, operator.ErrRefused) { t.Fatal("a refused plan is pending, or Home failed reading plans") } // The adoption plan verifies on both roles. for _, role := range []string{homeop.Directory, homeop.Credential} { if o := r.client.SubmitPlan(ctx, role, r.adoptionPlan(t, role, sqid)); o != homeop.OK { t.Fatalf("%s refused the adoption plan: %v", role, o) } } // Home's own copy of each pending plan keeps existing_team. views := map[string]operator.PlanView{} for _, role := range []string{homeop.Directory, homeop.Credential} { v := r.home.confirm(t, role, owner) if v.ExistingTeam != sqid || v.Kind != homeop.KindCreateTeam { t.Fatalf("%s plan view lost existing_team or kind", role) } views[role] = v } if v := views[homeop.Directory]; v.Label != "oficina" || v.DisplayName != "Oficina" || len(v.Apps) != len(adoptionApps) { t.Fatal("directory plan view lost label, name or apps") } } // Plan A interrupted after the seed, its windows then closing: Home's real // operator admits the continuation Alta signs (add-apps, existing_team the // seeded SQID, label oficina, no owner) and its provision and register // for the apps still missing (internal/operator plan.go Plan.valid, // windowAdmits; http_verbs.go provision under fence and routeScope, // register verifying the env manifest), committing nothing twice. func TestHomeAcceptsAdoptionContinuation(t *testing.T) { ctx := context.Background() env := map[string]string{} for _, a := range adoptionApps { env[a] = homeop.CanonicalOrigin(a) } crash, crashed := false, false acts := 0 r := newRigWith(t, env, func(key string) error { // Dies after sheets' provision committed (owner-seed, seed-team, // docs' provision and register, sheets' provision). if crash && acts == 4 && !crashed { crashed = true return errors.New("kill -9") } if crash { acts++ } return nil }) first, err := r.engine.StartInitialize(ctx, provision.InitializeAttestation) if err != nil { t.Fatal(err) } r.home.confirm(t, "directory", "") if got := r.drive(t, first); got.State != "done" { t.Fatalf("initialize: %s %s", got.State, got.Code) } r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) run, err := r.engine.StartAdoption(ctx, "Oficina", owner) if err != nil { t.Fatal(err) } r.home.confirm(t, "directory", "") r.home.confirm(t, "credential", owner) crash = true for i := 0; i < 5 && !crashed; i++ { _ = r.engine.Advance(ctx, run) r.home.now = r.home.now.Add(3 * time.Second) } if !crashed { t.Fatal("never reached sheets' provision") } r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) if got := r.drive(t, run); got.State != "parked" || got.Code != "window-expired" { t.Fatalf("stranded: %s %s", got.State, got.Code) } next, err := r.engine.ContinueAdoption(ctx) if err != nil { t.Fatal(err) } team, err := r.store.Team(ctx, provision.AdoptionLabel) if err != nil { t.Fatal(err) } for _, role := range []string{"directory", "credential"} { v := r.home.confirm(t, role, "") // sheets' register and everything after it are missing (Home lists // a plan's apps sorted). if v.Kind != homeop.KindAddApps || v.ExistingTeam != team.TeamSQID || strings.Join(v.Apps, ",") != "base,calendar,calls,equipo,memoria,sheets,slides" || v.OwnerIndex != "" { t.Fatalf("%s continuation view: kind %s apps %v", role, v.Kind, v.Apps) } } if got := r.drive(t, next); got.State != "done" || got.Code != "awaiting-configuration" { t.Fatalf("continuation: %s %s", got.State, got.Code) } count := func(db *sql.DB, q string) int { t.Helper() var n int if err := db.QueryRow(q).Scan(&n); err != nil { t.Fatal(err) } return n } if n := count(r.home.dirDB, "SELECT count(*) FROM teams WHERE label='oficina'"); n != 1 { t.Fatalf("teams %d", n) } if n := count(r.home.dirDB, "SELECT count(*) FROM team_instances WHERE state='pending' AND consumer_id=app AND origin='https://'||app||'.oficina.works'"); n != len(adoptionApps) { t.Fatalf("pending canonical rows %d", n) } if n := count(r.home.credDB, "SELECT count(*) FROM consumers"); n != 0 { t.Fatalf("register inserted %d consumers", n) } }