| 1 | package altacompat |
| 2 | |
| 3 | // Existing-team adoption at Home's real operator code: the plan Alta signs |
| 4 | // for the six production apps, and the reserved label that keeps every |
| 5 | // other plan out of it (internal/operator existing.go, plan.go). |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "database/sql" |
| 10 | "errors" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/oficina/alta/homeop" |
| 16 | "amadan.net/oficina/alta/provision" |
| 17 | "amadan.net/oficina/home/internal/operator" |
| 18 | ) |
| 19 | |
| 20 | // adoptionPlan is the plan Alta signs for role: create-team under the |
| 21 | // reserved label, bound to the proposed team SQID. Home's credential role |
| 22 | // carries the owner; its directory role the label and name. |
| 23 | func (r *rig) adoptionPlan(t *testing.T, role, existing string) []byte { |
| 24 | t.Helper() |
| 25 | p, err := r.client.NewPlan(role, homeop.Token(), homeop.KindCreateTeam, adoptionApps, r.home.now) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if role == homeop.Directory { |
| 30 | p.Label, p.DisplayName = "oficina", "Oficina" |
| 31 | } else { |
| 32 | p.Owner = owner |
| 33 | } |
| 34 | p.ExistingTeam = existing |
| 35 | raw, err := homeop.SignPlan(r.client.Key, p) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | return raw |
| 40 | } |
| 41 | |
| 42 | func TestHomeAcceptsAltaSignedExistingTeamPlan(t *testing.T) { |
| 43 | r := newRig(t) |
| 44 | ctx := context.Background() |
| 45 | run, err := r.engine.StartInitialize(ctx, provision.InitializeAttestation) |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | r.home.confirm(t, "directory", "") |
| 50 | if got := r.drive(t, run); got.State != "done" { |
| 51 | t.Fatalf("initialize: %s %s", got.State, got.Code) |
| 52 | } |
| 53 | r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) |
| 54 | |
| 55 | sqid, err := operator.TeamSQID(1) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | |
| 60 | // An ordinary create-team under the reserved label is refused on the |
| 61 | // directory role: nothing but an adoption may name "oficina". |
| 62 | if o := r.client.SubmitPlan(ctx, homeop.Directory, r.adoptionPlan(t, homeop.Directory, "")); o != homeop.Refused { |
| 63 | t.Fatalf("ordinary create-team under oficina: %v", o) |
| 64 | } |
| 65 | if _, err := r.home.dirPlans.Pending(ctx, r.home.now); !errors.Is(err, operator.ErrRefused) { |
| 66 | t.Fatal("a refused plan is pending, or Home failed reading plans") |
| 67 | } |
| 68 | |
| 69 | // The adoption plan verifies on both roles. |
| 70 | for _, role := range []string{homeop.Directory, homeop.Credential} { |
| 71 | if o := r.client.SubmitPlan(ctx, role, r.adoptionPlan(t, role, sqid)); o != homeop.OK { |
| 72 | t.Fatalf("%s refused the adoption plan: %v", role, o) |
| 73 | } |
| 74 | } |
| 75 | // Home's own copy of each pending plan keeps existing_team. |
| 76 | views := map[string]operator.PlanView{} |
| 77 | for _, role := range []string{homeop.Directory, homeop.Credential} { |
| 78 | v := r.home.confirm(t, role, owner) |
| 79 | if v.ExistingTeam != sqid || v.Kind != homeop.KindCreateTeam { |
| 80 | t.Fatalf("%s plan view lost existing_team or kind", role) |
| 81 | } |
| 82 | views[role] = v |
| 83 | } |
| 84 | if v := views[homeop.Directory]; v.Label != "oficina" || v.DisplayName != "Oficina" || len(v.Apps) != len(adoptionApps) { |
| 85 | t.Fatal("directory plan view lost label, name or apps") |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Plan A interrupted after the seed, its windows then closing: Home's real |
| 90 | // operator admits the continuation Alta signs (add-apps, existing_team the |
| 91 | // seeded SQID, label oficina, no owner) and its provision and register |
| 92 | // for the apps still missing (internal/operator plan.go Plan.valid, |
| 93 | // windowAdmits; http_verbs.go provision under fence and routeScope, |
| 94 | // register verifying the env manifest), committing nothing twice. |
| 95 | func TestHomeAcceptsAdoptionContinuation(t *testing.T) { |
| 96 | ctx := context.Background() |
| 97 | env := map[string]string{} |
| 98 | for _, a := range adoptionApps { |
| 99 | env[a] = homeop.CanonicalOrigin(a) |
| 100 | } |
| 101 | crash, crashed := false, false |
| 102 | acts := 0 |
| 103 | r := newRigWith(t, env, func(key string) error { |
| 104 | // Dies after sheets' provision committed (owner-seed, seed-team, |
| 105 | // docs' provision and register, sheets' provision). |
| 106 | if crash && acts == 4 && !crashed { |
| 107 | crashed = true |
| 108 | return errors.New("kill -9") |
| 109 | } |
| 110 | if crash { |
| 111 | acts++ |
| 112 | } |
| 113 | return nil |
| 114 | }) |
| 115 | first, err := r.engine.StartInitialize(ctx, provision.InitializeAttestation) |
| 116 | if err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | r.home.confirm(t, "directory", "") |
| 120 | if got := r.drive(t, first); got.State != "done" { |
| 121 | t.Fatalf("initialize: %s %s", got.State, got.Code) |
| 122 | } |
| 123 | r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) |
| 124 | |
| 125 | run, err := r.engine.StartAdoption(ctx, "Oficina", owner) |
| 126 | if err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | r.home.confirm(t, "directory", "") |
| 130 | r.home.confirm(t, "credential", owner) |
| 131 | crash = true |
| 132 | for i := 0; i < 5 && !crashed; i++ { |
| 133 | _ = r.engine.Advance(ctx, run) |
| 134 | r.home.now = r.home.now.Add(3 * time.Second) |
| 135 | } |
| 136 | if !crashed { |
| 137 | t.Fatal("never reached sheets' provision") |
| 138 | } |
| 139 | r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) |
| 140 | if got := r.drive(t, run); got.State != "parked" || got.Code != "window-expired" { |
| 141 | t.Fatalf("stranded: %s %s", got.State, got.Code) |
| 142 | } |
| 143 | next, err := r.engine.ContinueAdoption(ctx) |
| 144 | if err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | team, err := r.store.Team(ctx, provision.AdoptionLabel) |
| 148 | if err != nil { |
| 149 | t.Fatal(err) |
| 150 | } |
| 151 | for _, role := range []string{"directory", "credential"} { |
| 152 | v := r.home.confirm(t, role, "") |
| 153 | // sheets' register and everything after it are missing (Home lists |
| 154 | // a plan's apps sorted). |
| 155 | if v.Kind != homeop.KindAddApps || v.ExistingTeam != team.TeamSQID || strings.Join(v.Apps, ",") != "base,calendar,calls,equipo,memoria,sheets,slides" || v.OwnerIndex != "" { |
| 156 | t.Fatalf("%s continuation view: kind %s apps %v", role, v.Kind, v.Apps) |
| 157 | } |
| 158 | } |
| 159 | if got := r.drive(t, next); got.State != "done" || got.Code != "awaiting-configuration" { |
| 160 | t.Fatalf("continuation: %s %s", got.State, got.Code) |
| 161 | } |
| 162 | count := func(db *sql.DB, q string) int { |
| 163 | t.Helper() |
| 164 | var n int |
| 165 | if err := db.QueryRow(q).Scan(&n); err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | return n |
| 169 | } |
| 170 | if n := count(r.home.dirDB, "SELECT count(*) FROM teams WHERE label='oficina'"); n != 1 { |
| 171 | t.Fatalf("teams %d", n) |
| 172 | } |
| 173 | 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) { |
| 174 | t.Fatalf("pending canonical rows %d", n) |
| 175 | } |
| 176 | if n := count(r.home.credDB, "SELECT count(*) FROM consumers"); n != 0 { |
| 177 | t.Fatalf("register inserted %d consumers", n) |
| 178 | } |
| 179 | } |
| 180 | |