| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "database/sql" |
| 6 | "embed" |
| 7 | "encoding/base64" |
| 8 | "io/fs" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/home/dashboard" |
| 14 | "amadan.net/oficina/home/handoff" |
| 15 | "amadan.net/oficina/home/roster" |
| 16 | "amadan.net/oficina/home/teammode" |
| 17 | "amadan.net/rastrillo/rastrillo/db" |
| 18 | "amadan.net/rastrillo/rastrillo/migrate" |
| 19 | "amadan.net/rastrillo/rastrillo/sessions" |
| 20 | "github.com/carlosframework/docs/internal/docs/linkstore" |
| 21 | "github.com/carlosframework/docs/internal/docs/principalstore" |
| 22 | "github.com/go-json-experiment/json" |
| 23 | ) |
| 24 | |
| 25 | // The team migration exactly as Docs applied it before teammode |
| 26 | // (internal/docs/teamstate/migrations/0001_team.sql at e58a22f). |
| 27 | // |
| 28 | //go:embed testdata/teamstate-e58a22f/migrations/0001_team.sql |
| 29 | var teamstateE58a22f embed.FS |
| 30 | |
| 31 | // legacyTeamDatabase is a database as the old code left it: the old |
| 32 | // docs_team set applied beside the app's schema, and the marker the old |
| 33 | // teamstate.Activate wrote, with the old fingerprint (base64url SHA-256 of |
| 34 | // the bindings' JSON). |
| 35 | func legacyTeamDatabase(t *testing.T, path, migration string, b roster.Bindings) []ledgerRow { |
| 36 | t.Helper() |
| 37 | sub, err := fs.Sub(teamstateE58a22f, "testdata/teamstate-e58a22f") |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | old := migrate.MustFromFS(sub, "docs_team") |
| 42 | if migration != "" { |
| 43 | old = migrate.MustFromFS(embed.FS{}, "docs_team").Add(migrate.Migration{ID: "0001_team", SQL: migration}) |
| 44 | } |
| 45 | d, err := db.Open(path, nil) |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | defer d.Close() |
| 50 | if _, err = migrate.Apply(t.Context(), d, migrate.Merge(mainBootSchema, principalstore.Schema, old)); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | raw, err := json.Marshal(b) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | digest := sha256.Sum256(raw) |
| 58 | if _, err = d.Writer().Exec("INSERT INTO docs_team_required(id,bindings) VALUES(1,?)", base64.RawURLEncoding.EncodeToString(digest[:])); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | return readLedger(t, d.Writer()) |
| 62 | } |
| 63 | |
| 64 | type ledgerRow struct{ ID, AppliedAt, Checksum string } |
| 65 | |
| 66 | // mainBootSchema is BootSchema as main applied it at 7c9122f, the last |
| 67 | // commit before Docs moved onto home/connect: no connect link schema. |
| 68 | var mainBootSchema = migrate.Merge(migrate.MustFromFS(dashboard.Migrations, "oficina_dashboard"), sessions.Schema, authSchema, Schema, linkstore.Schema, migrate.MustFromFS(handoff.Migrations, "oficina_handoff")) |
| 69 | |
| 70 | // requireLedgerKept: every row the old code wrote is still there, unchanged |
| 71 | // (id, applied_at, checksum), and the only new rows are the migrations |
| 72 | // that are new in schema since main. |
| 73 | func requireLedgerKept(t *testing.T, before, after []ledgerRow, schema, mainSchema *migrate.Set) { |
| 74 | t.Helper() |
| 75 | old := map[string]bool{} |
| 76 | for _, m := range mainSchema.All() { |
| 77 | old[m.ID] = true |
| 78 | } |
| 79 | added := map[string]bool{} |
| 80 | for _, m := range schema.All() { |
| 81 | if !old[m.ID] { |
| 82 | added[m.ID] = true |
| 83 | } |
| 84 | } |
| 85 | kept := map[string]ledgerRow{} |
| 86 | for _, r := range after { |
| 87 | kept[r.ID] = r |
| 88 | } |
| 89 | for _, r := range before { |
| 90 | if kept[r.ID] != r { |
| 91 | t.Fatalf("ledger row moved: before %v, after %v", r, kept[r.ID]) |
| 92 | } |
| 93 | delete(kept, r.ID) |
| 94 | } |
| 95 | for id := range kept { |
| 96 | if !added[id] { |
| 97 | t.Fatalf("ledger gained %s, which is not new since main", id) |
| 98 | } |
| 99 | delete(added, id) |
| 100 | } |
| 101 | if len(added) != 0 { |
| 102 | t.Fatalf("new migrations not applied: %v", added) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func readLedger(t *testing.T, q *sql.DB) []ledgerRow { |
| 107 | t.Helper() |
| 108 | rows, err := q.QueryContext(t.Context(), "SELECT id, applied_at, checksum FROM rastrillo_migrations ORDER BY id") |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | defer rows.Close() |
| 113 | var out []ledgerRow |
| 114 | for rows.Next() { |
| 115 | var r ledgerRow |
| 116 | if err := rows.Scan(&r.ID, &r.AppliedAt, &r.Checksum); err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | out = append(out, r) |
| 120 | } |
| 121 | return out |
| 122 | } |
| 123 | |
| 124 | // A database activated by the old code boots under teammode with its |
| 125 | // migration ledger untouched: State("docs").Schema() is the applied |
| 126 | // docs_team/0001_team byte for byte, so nothing re-runs and no checksum |
| 127 | // moves, and the old marker is the fingerprint teammode computes. The only |
| 128 | // rows added are the migrations new since main (connect's link schema and |
| 129 | // linkstore.ForPrincipals). |
| 130 | func TestTeamStateKeepsTheAppliedLedger(t *testing.T) { |
| 131 | cfg := newTeamRouterFixture(t).cfg |
| 132 | b := roster.Bindings{Issuer: bridgeIssuer, Directory: "https://home.example", Consumer: "router-docs", App: "docs", Team: "TeamA", Audience: bridgeOrigin} |
| 133 | |
| 134 | cfg.DBPath = filepath.Join(t.TempDir(), "legacy.db") |
| 135 | before := legacyTeamDatabase(t, cfg.DBPath, "", b) |
| 136 | app, err := New(cfg) |
| 137 | if err != nil { |
| 138 | t.Fatal("a database activated by the old code did not boot:", err) |
| 139 | } |
| 140 | after := readLedger(t, app.db.Writer()) |
| 141 | marker, activated, err := teamState.Required(t.Context(), app.db.Writer()) |
| 142 | if closeErr := app.Close(); closeErr != nil { |
| 143 | t.Fatal(closeErr) |
| 144 | } |
| 145 | requireLedgerKept(t, before, after, TeamBootSchema, migrate.Merge(mainBootSchema, principalstore.Schema, teamState.Schema())) |
| 146 | if err != nil || !activated || marker != teammode.Fingerprint(b) { |
| 147 | t.Fatal("the old marker is not teammode's fingerprint", err) |
| 148 | } |
| 149 | |
| 150 | // Byte for byte, not only up to the ledger's whitespace-blind checksum. |
| 151 | applied, err := teamstateE58a22f.ReadFile("testdata/teamstate-e58a22f/migrations/0001_team.sql") |
| 152 | if err != nil || string(applied) != teamState.SQL() { |
| 153 | t.Fatal("State(\"docs\").SQL() is not the migration Docs applied", err) |
| 154 | } |
| 155 | |
| 156 | // The ledger check is live: had the applied SQL differed (here, in a |
| 157 | // trigger's message), the same boot would have refused. |
| 158 | cfg.DBPath = filepath.Join(t.TempDir(), "moved.db") |
| 159 | legacyTeamDatabase(t, cfg.DBPath, strings.Replace(teamState.SQL(), "docs team: refused", "docs team: refusal", 1), b) |
| 160 | if app, err := New(cfg); err == nil { |
| 161 | app.Close() |
| 162 | t.Fatal("a moved team migration booted") |
| 163 | } |
| 164 | } |
| 165 | |