oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "context"
5 "errors"
6 "testing"
7 "time"
8
9 "amadan.net/oficina/home/connect"
10 connectlinks "amadan.net/oficina/home/connect/linkstore"
11 "amadan.net/oficina/home/roster"
12 "github.com/carlosframework/docs/internal/docs/principalstore"
13)
14
15// teamHooksFixture is connect's two team-mode hooks over a team runtime
16// whose roster lists one member, with principalstore's allocation gate
17// (the second gate, inside EnsureHome) counted and switchable.
18type teamHooksFixture struct {
19 app *App
20 rt *teamRuntime
21 clock *teamTestClock
22 member string
23 h *homeRoutes
24 calls int
25 refuse bool
26 persons func() (people, docs int64)
27}
28
29func newTeamHooksFixture(t *testing.T) *teamHooksFixture {
30 t.Helper()
31 app := newSampleDocumentsInternalTestApp(t, "0")
32 s := app.Store()
33 local, e := s.EnsurePerson(t.Context(), "local@example.com")
34 if e != nil {
35 t.Fatal(e)
36 }
37 if _, e = s.Create(t.Context(), local, "existing"); e != nil {
38 t.Fatal(e)
39 }
40 rt, clock, subject := testTeamRoster(t, s, app)
41 f := &teamHooksFixture{app: app, rt: rt, clock: clock, member: subject, h: &homeRoutes{team: rt}}
42 rt.principals, e = principalstore.New(app.db.Writer(), principalstore.Config{Issuer: rt.bindings.Issuer, Team: rt.bindings.Team, RequireMembership: func(ctx context.Context, q principalstore.MembershipRequest) error {
43 f.calls++
44 if f.refuse {
45 return principalstore.ErrRefused
46 }
47 d := rt.cache.Check(ctx, q.Subject)
48 if d.State == roster.Member && d.Lease.Valid() {
49 return nil
50 }
51 if d.State == roster.NotMember {
52 return principalstore.ErrRefused
53 }
54 return principalstore.ErrUnavailable
55 }})
56 if e != nil {
57 t.Fatal(e)
58 }
59 f.persons = func() (people, docs int64) {
60 s.db.Model(&Person{}).Count(&people)
61 s.db.Model(&Document{}).Count(&docs)
62 return
63 }
64 return f
65}
66
67func (f *teamHooksFixture) allocated(t *testing.T, subject string) bool {
68 t.Helper()
69 _, e := f.rt.principals.Resolve(t.Context(), f.rt.bindings.Issuer, subject)
70 if e != nil && !errors.Is(e, principalstore.ErrRefused) {
71 t.Fatal(e)
72 }
73 return e == nil
74}
75
76// Unlinked creates a Home-only principal for a roster Member, and for
77// nobody else: the roster's gate first, principalstore's again inside
78// EnsureHome. It never creates a local person or sample documents.
79func TestTeamUnlinkedAllocatesOnlyForAMemberThroughBothGates(t *testing.T) {
80 f := newTeamHooksFixture(t)
81 ctx, issuer := t.Context(), f.rt.bindings.Issuer
82 stranger := "pr_" + "bbbbbbbbbbbbbbbbbbbbbbbbbb"
83 if _, e := f.h.unlinked(ctx, issuer, stranger); !errors.Is(e, connect.ErrNoPerson) || f.calls != 0 || f.allocated(t, stranger) {
84 t.Fatal("a stranger reached allocation", e, f.calls)
85 }
86 if _, e := f.h.unlinked(ctx, "https://other.example", f.member); !errors.Is(e, connect.ErrNoPerson) || f.calls != 0 {
87 t.Fatal("another issuer reached allocation", e)
88 }
89 f.refuse = true
90 if _, e := f.h.unlinked(ctx, issuer, f.member); !errors.Is(e, connect.ErrNoPerson) || f.calls != 1 || f.allocated(t, f.member) {
91 t.Fatal("the second gate did not hold", e, f.calls)
92 }
93 f.refuse = false
94 p, e := f.h.unlinked(ctx, issuer, f.member)
95 if e != nil || p.ID != principalstore.HomeIDMin || p.Label != "" || f.calls != 2 {
96 t.Fatal("a member was not given a Home-only principal", p, e, f.calls)
97 }
98 if again, e := f.h.unlinked(ctx, issuer, f.member); e != nil || again.ID != p.ID {
99 t.Fatal("a second arrival allocated again", again, e)
100 }
101 if people, docs := f.persons(); people != 1 || docs != 1 {
102 t.Fatal("allocation fabricated a local person or samples", people, docs)
103 }
104 // The lease lapses and the directory is down: undecided, which connect
105 // answers 503, never "unknown", and nothing is allocated.
106 f.clock.step(61 * time.Second)
107 other := "pr_" + "cccccccccccccccccccccccccc"
108 if _, e := f.h.unlinked(ctx, issuer, other); e == nil || errors.Is(e, connect.ErrNoPerson) || f.allocated(t, other) {
109 t.Fatal("an undecided roster was not retryable", e)
110 }
111 if _, e := (&homeRoutes{}).unlinked(ctx, issuer, f.member); e == nil || errors.Is(e, connect.ErrNoPerson) {
112 t.Fatal("no team runtime was not unavailable", e)
113 }
114}
115
116// Admit is the roster decision connect takes first: a Member is nil, a
117// stranger or another issuer connect's ErrNoPerson, an undecidable roster
118// (or no runtime) unavailable, and it allocates nothing, ever.
119func TestTeamAdmitDecidesAndAllocatesNothing(t *testing.T) {
120 f := newTeamHooksFixture(t)
121 ctx, issuer := t.Context(), f.rt.bindings.Issuer
122 if e := f.h.admit(ctx, issuer, f.member); e != nil {
123 t.Fatal("a member was not admitted", e)
124 }
125 stranger := "pr_" + "bbbbbbbbbbbbbbbbbbbbbbbbbb"
126 for name, e := range map[string]error{
127 "stranger": f.h.admit(ctx, issuer, stranger),
128 "other issuer": f.h.admit(ctx, "https://other.example", f.member),
129 "not a ref": f.h.admit(ctx, issuer, "someone@example.com"),
130 } {
131 if !errors.Is(e, connect.ErrNoPerson) {
132 t.Errorf("%s: %v", name, e)
133 }
134 }
135 if f.calls != 0 || f.allocated(t, f.member) || f.allocated(t, stranger) {
136 t.Fatal("Admit allocated or asked the allocation gate", f.calls)
137 }
138 if people, docs := f.persons(); people != 1 || docs != 1 {
139 t.Fatal("Admit fabricated a local person or samples", people, docs)
140 }
141 f.clock.step(61 * time.Second)
142 if e := f.h.admit(ctx, issuer, "pr_"+"cccccccccccccccccccccccccc"); e == nil || errors.Is(e, connect.ErrNoPerson) {
143 t.Fatal("an undecided roster was not retryable", e)
144 }
145 if e := (&homeRoutes{}).admit(ctx, issuer, f.member); e == nil || errors.Is(e, connect.ErrNoPerson) {
146 t.Fatal("no team runtime was not unavailable", e)
147 }
148}
149
150// A subject linked to a local account in the meantime is that account,
151// never a second, Home-only one.
152func TestTeamUnlinkedRefusesALinkedSubject(t *testing.T) {
153 f := newTeamHooksFixture(t)
154 ctx := t.Context()
155 person, e := f.app.Store().EnsurePerson(ctx, "linked@example.com")
156 if e != nil {
157 t.Fatal(e)
158 }
159 if _, e = f.app.db.Writer().Exec("INSERT INTO home_identity_links(issuer,subject,person_id) VALUES(?,?,?)", f.rt.bindings.Issuer, f.member, person); e != nil {
160 t.Fatal(e)
161 }
162 if _, e = f.h.unlinked(ctx, f.rt.bindings.Issuer, f.member); !errors.Is(e, connect.ErrNoPerson) {
163 t.Fatal("a linked subject was given a Home-only principal", e)
164 }
165 var homeOnly int
166 if e = f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM docs_home_identities").Scan(&homeOnly); e != nil || homeOnly != 0 {
167 t.Fatal("allocated beside a link", homeOnly, e)
168 }
169}
170
171// HomeOnly reads the principal Unlinked made, and nothing else: not a
172// stranger, and not a local account reached through a link row. connect
173// asks it only after refusing that link (its binding no longer matches),
174// so answering with the local account would sign the subject in as it.
175func TestTeamHomeOnlyIsOnlyTheHomeOnlyPrincipal(t *testing.T) {
176 f := newTeamHooksFixture(t)
177 ctx, issuer := t.Context(), f.rt.bindings.Issuer
178 if _, e := f.h.homeOnly(ctx, issuer, f.member); !errors.Is(e, connect.ErrNoPerson) {
179 t.Fatal("a subject with no principal resolved", e)
180 }
181 made, e := f.h.unlinked(ctx, issuer, f.member)
182 if e != nil {
183 t.Fatal(e)
184 }
185 if p, e := f.h.homeOnly(ctx, issuer, f.member); e != nil || p.ID != made.ID || p.Label != "" {
186 t.Fatal("the Home-only principal did not resolve", p, e)
187 }
188 person, e := f.app.Store().EnsurePerson(ctx, "stale@example.com")
189 if e != nil {
190 t.Fatal(e)
191 }
192 stale := "pr_" + "dddddddddddddddddddddddddd"
193 if _, e = f.app.db.Writer().Exec("INSERT INTO home_identity_links(issuer,subject,person_id,person_binding) VALUES(?,?,?,'stale')", issuer, stale, person); e != nil {
194 t.Fatal(e)
195 }
196 if p, e := f.rt.principals.Resolve(ctx, issuer, stale); e != nil || p.Kind != principalstore.Local {
197 t.Fatal("fixture: principalstore does not resolve the link row", p, e)
198 }
199 if p, e := f.h.homeOnly(ctx, issuer, stale); !errors.Is(e, connect.ErrNoPerson) {
200 t.Fatal("a link connect refused resolved as a Home-only person", p, e)
201 }
202 if e := f.app.db.Writer().Close(); e != nil {
203 t.Fatal(e)
204 }
205 if _, e := f.h.homeOnly(ctx, issuer, f.member); e == nil || errors.Is(e, connect.ErrNoPerson) {
206 t.Fatal("a storage failure read as no person", e)
207 }
208}
209
210// homeOnlySubjects is connect's Resolve for a database with no links: the
211// HomeOnly hook, its answers mapped as connect maps them.
212func homeOnlySubjects(rt *teamRuntime) subjectsFunc {
213 h := &homeRoutes{team: rt}
214 return func(ctx context.Context, subject string) (connect.Person, error) {
215 p, err := h.homeOnly(ctx, rt.bindings.Issuer, subject)
216 switch {
217 case errors.Is(err, connect.ErrNoPerson):
218 return connect.Person{}, connectlinks.ErrRefused
219 case err != nil:
220 return connect.Person{}, connectlinks.ErrStorage
221 }
222 return p, nil
223 }
224}
225
226// The member choice speaks the spec's words (Home spec
227// 2026-11-14-oficina-id-native.md §8), typeset as Docs' copy is.
228func TestChooseCopyIsTheSpecs(t *testing.T) {
229 for key, want := range map[string]string{
230 "docs.home.choose_title": "Welcome to the team",
231 "docs.home.choose_body": "Do you already have an account here that signs in with email? Connect it, and your Oficina ID will open it from now on. If you’re new here, continue with Oficina ID.",
232 "docs.home.choose_connect_action": "Connect my existing account",
233 "docs.home.choose_new_action": "I’m new here — continue with Oficina ID",
234 } {
235 if Catalog[key] != want {
236 t.Errorf("%s = %q", key, Catalog[key])
237 }
238 }
239}
240