oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "html"
5 "net/http"
6 "net/http/httptest"
7 "net/url"
8 "regexp"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/home/connect"
13 "amadan.net/oficina/home/roster"
14 "amadan.net/oficina/home/teammode"
15 "amadan.net/oficina/home/teammode/teammodetest"
16)
17
18// Team mode's fixture: Home configured exactly as newHomeFixture does, and
19// home/teammode's test directory (teammodetest) answering the roster in
20// memory from a member list a test can change or take down, with a roster
21// clock a test can step past the lease.
22
23const (
24 teamTestDirectory = teammodetest.DirectoryOrigin
25 teamTestTeam = teammodetest.Team
26)
27
28// teamSubject is a well-formed person ref: "pr_" and 26 base32 letters.
29func teamSubject(c byte) string { return teammodetest.Subject(c) }
30
31func teamTestBindings() roster.Bindings {
32 return teammodetest.Bindings("sheets", "sheets-preview", testOrigin)
33}
34
35// teamConfigFor signs a provisioning record for bindings with a fresh
36// operator key, and returns the configuration that carries it plus the
37// directory that will answer for it.
38func teamConfigFor(t *testing.T, b roster.Bindings) (*teammode.Config, *teammodetest.Directory) {
39 t.Helper()
40 return teammodetest.Provision(t, b)
41}
42
43// teamSettingsFor verifies cfg for the test instance, as main does
44// before it migrates.
45func teamSettingsFor(t *testing.T, cfg *teammode.Config) *teammode.Settings {
46 t.Helper()
47 s, err := teammode.Verify(cfg, teammodetest.Home(teamTestBindings()), "sheets", testOrigin)
48 if err != nil {
49 t.Fatalf("team settings: %v", err)
50 }
51 return s
52}
53
54type teamFixture struct {
55 *homeFixture
56 dir *teammodetest.Directory
57 clock *teammodetest.Clock
58 cfg *teammode.Config
59}
60
61// newTeamFixture is a team-mode instance. owners is SHEETS_OWNERS, which
62// in team mode only admits local sign-in; pass none for Alta's shape.
63func newTeamFixture(t *testing.T, owners ...string) *teamFixture {
64 t.Helper()
65 return newTeamFixtureWith(t, "", owners...)
66}
67
68// newTeamFixtureWith sets HOME_DIRECTORY_ORIGIN too, which the dashboard
69// feed needs.
70func newTeamFixtureWith(t *testing.T, directory string, owners ...string) *teamFixture {
71 t.Helper()
72 f := newTeamApp(t, directory, owners...)
73 if err := f.app.ConfigureTeam(); err != nil {
74 t.Fatalf("ConfigureTeam: %v", err)
75 }
76 return f
77}
78
79// newTeamApp is newTeamFixtureWith before ConfigureTeam: the app built
80// as main builds a team-mode one (TeamBootSchema, verified settings in
81// Config.Team, Home configured with them), with the test directory and
82// clock in place of the real ones.
83func newTeamApp(t *testing.T, directory string, owners ...string) *teamFixture {
84 t.Helper()
85 cfg, dir := teamConfigFor(t, teamTestBindings())
86 app, _ := appWithSchema(t, testOrigin, TeamBootSchema, owners...)
87 clock := teammodetest.NewClock()
88 app.teamSettings, app.teamTransport, app.teamClock = teamSettingsFor(t, cfg), dir, clock
89 f := homeOn(t, app, directory)
90 return &teamFixture{homeFixture: f, dir: dir, clock: clock, cfg: cfg}
91}
92
93// signInHome runs a full Home exchange for subject from a clean jar, and
94// returns the finish response.
95//
96// A roster member with no member here yet is asked to choose (Home spec
97// 2026-11-14-oficina-id-native.md §8); signInHome answers "I'm new here",
98// as a new member would, and returns that answer. signInHomeChoosing
99// stops at the choice.
100func (f *teamFixture) signInHome(t *testing.T, subject string) *httptest.ResponseRecorder {
101 t.Helper()
102 fin := f.signInHomeChoosing(t, subject)
103 if fin.Code != http.StatusSeeOther || fin.Header().Get("Location") != connect.ChoosePath {
104 return fin
105 }
106 return f.choose(t, "new")
107}
108
109func (f *teamFixture) signInHomeChoosing(t *testing.T, subject string) *httptest.ResponseRecorder {
110 t.Helper()
111 f.jar = map[string]*http.Cookie{}
112 start := f.request(t, "GET", "/auth/start?return_to=%2Fsheets", nil, false)
113 return f.exchange(t, start, subject)
114}
115
116// choose reads the choice page's form token and posts choice with it.
117func (f *teamFixture) choose(t *testing.T, choice string) *httptest.ResponseRecorder {
118 t.Helper()
119 page := f.get(t, connect.ChoosePath)
120 if page.Code != http.StatusOK {
121 t.Fatalf("choose page = %d %s", page.Code, page.Body.String())
122 }
123 m := regexp.MustCompile(`name="token" value="([^"]+)"`).FindStringSubmatch(page.Body.String())
124 if m == nil {
125 t.Fatalf("choose page has no form token: %s", page.Body.String())
126 }
127 return f.request(t, "POST", connect.ChoosePath, url.Values{"choice": {choice}, "token": {html.UnescapeString(m[1])}}, false)
128}
129
130// refreshed steps the roster clock past the refresh point and waits for
131// the refresh, so the next request sees the directory's current list.
132func (f *teamFixture) refreshed(t *testing.T) {
133 t.Helper()
134 f.clock.Step(roster.RefreshAfter + time.Second)
135 if err := f.app.team.Refresh(t.Context()); err != nil {
136 t.Fatalf("roster refresh: %v", err)
137 }
138}
139
140// expired takes the directory down and steps past the lease: Unknown.
141func (f *teamFixture) expired(t *testing.T) {
142 t.Helper()
143 f.dir.SetDown(true)
144 f.clock.Step(roster.MaximumAge + time.Second)
145}
146
147func (f *teamFixture) get(t *testing.T, path string) *httptest.ResponseRecorder {
148 t.Helper()
149 return f.request(t, "GET", path, nil, false)
150}
151