oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package teammode_test
2
3import (
4 "bytes"
5 "context"
6 "io"
7 "log/slog"
8 "path/filepath"
9 "testing"
10 "testing/fstest"
11 "time"
12
13 "amadan.net/oficina/home/handoff/seal"
14 "amadan.net/oficina/home/roster"
15 "amadan.net/oficina/home/teammode"
16 "amadan.net/oficina/home/teammode/teammodetest"
17 "amadan.net/rastrillo/rastrillo/db"
18 "amadan.net/rastrillo/rastrillo/migrate"
19)
20
21const (
22 testApp = "sheets"
23 testOrigin = "https://sheets.example.com"
24 testConsumer = "sheets-preview"
25)
26
27var testState = teammode.MustState("sheets")
28
29// baseSchema stands in for an app's own schema: its people, connect's links
30// and a Home-only identity table, the two tables a census reads.
31var baseSchema = migrate.MustFromFS(fstest.MapFS{"migrations/0001_app.sql": {Data: []byte(`
32CREATE TABLE people (id INTEGER PRIMARY KEY AUTOINCREMENT, address TEXT UNIQUE);
33CREATE TABLE home_identity_links (issuer TEXT NOT NULL, subject TEXT NOT NULL, person_id INTEGER NOT NULL, PRIMARY KEY(issuer,subject));
34CREATE TABLE app_home_only (issuer TEXT NOT NULL, subject TEXT NOT NULL, person_id INTEGER NOT NULL UNIQUE, PRIMARY KEY(issuer,subject));
35CREATE TABLE items (id INTEGER PRIMARY KEY AUTOINCREMENT, owner INTEGER NOT NULL, title TEXT NOT NULL);
36CREATE TABLE shares (item INTEGER NOT NULL, person INTEGER NOT NULL);
37`)}}, "app")
38
39var teamSchema = migrate.Merge(baseSchema, testState.Schema())
40
41var issuerTables = []string{"home_identity_links", "app_home_only"}
42
43type fixture struct {
44 t *testing.T
45 db *db.DB
46 cfg *teammode.Config
47 bindings roster.Bindings
48 dir *teammodetest.Directory
49 clock *teammodetest.Clock
50 settings *teammode.Settings
51 rt *teammode.Runtime
52}
53
54func openDB(t *testing.T, path string) *db.DB {
55 t.Helper()
56 d, err := db.Open(path, slog.New(slog.NewTextHandler(io.Discard, nil)))
57 if err != nil {
58 t.Fatal(err)
59 }
60 t.Cleanup(func() { d.Close() })
61 return d
62}
63
64// newFixture is a team-mode instance at boot: schema chosen by BootSchema,
65// applied, and the runtime opened, with an empty roster.
66func newFixture(t *testing.T) *fixture {
67 t.Helper()
68 f := provisioned(t)
69 f.boot()
70 return f
71}
72
73// provisioned is the configuration and a fresh database, nothing applied.
74func provisioned(t *testing.T) *fixture {
75 t.Helper()
76 b := teammodetest.Bindings(testApp, testConsumer, testOrigin)
77 cfg, dir := teammodetest.Provision(t, b)
78 settings, err := teammode.Verify(cfg, teammodetest.Home(b), testApp, testOrigin)
79 if err != nil {
80 t.Fatal(err)
81 }
82 return &fixture{t: t, db: openDB(t, filepath.Join(t.TempDir(), "app.db")), cfg: cfg, bindings: b, dir: dir, clock: teammodetest.NewClock(), settings: settings}
83}
84
85func (f *fixture) boot() {
86 f.t.Helper()
87 ctx := context.Background()
88 schema, err := testState.BootSchema(ctx, f.db.Writer(), f.settings, baseSchema, teamSchema)
89 if err != nil {
90 f.t.Fatalf("BootSchema: %v", err)
91 }
92 if _, err := migrate.Apply(ctx, f.db, schema); err != nil {
93 f.t.Fatal(err)
94 }
95 f.rt, err = f.open()
96 if err != nil {
97 f.t.Fatalf("Open: %v", err)
98 }
99}
100
101func testSealer(t testing.TB) *seal.Sealer {
102 t.Helper()
103 s, err := seal.New(bytes.Repeat([]byte{9}, 32), "oficina/sheets")
104 if err != nil {
105 t.Fatal(err)
106 }
107 return s
108}
109
110func (f *fixture) runtimeConfig() teammode.RuntimeConfig {
111 return teammode.RuntimeConfig{Settings: f.settings, State: testState, DB: f.db.Writer(), Sealer: testSealer(f.t), IssuerTables: issuerTables, Transport: f.dir, Clock: f.clock}
112}
113
114func (f *fixture) open() (*teammode.Runtime, error) {
115 return teammode.Open(context.Background(), f.runtimeConfig())
116}
117
118// refreshed steps past the refresh point and waits for the refresh, so the
119// next decision sees the directory's current list.
120func (f *fixture) refreshed() {
121 f.t.Helper()
122 f.clock.Step(roster.RefreshAfter + time.Second)
123 if err := f.rt.Refresh(context.Background()); err != nil {
124 f.t.Fatalf("refresh: %v", err)
125 }
126}
127
128// expired takes the directory down and steps past the lease: Unknown.
129func (f *fixture) expired() {
130 f.dir.SetDown(true)
131 f.clock.Step(roster.MaximumAge + time.Second)
132}
133
134var (
135 alice = teammodetest.Subject('a')
136 bob = teammodetest.Subject('b')
137 carol = teammodetest.Subject('c')
138)
139
140func wantState(t *testing.T, a *teammode.Access, want roster.State) {
141 t.Helper()
142 if got := a.State(); got != want {
143 t.Fatalf("state %d, want %d", got, want)
144 }
145}
146
147func migrateApply(f *fixture, schema *migrate.Set) (migrate.Result, error) {
148 return migrate.Apply(context.Background(), f.db, schema)
149}
150