oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package slides
2
3import (
4 "context"
5 "database/sql"
6 "net/http"
7 "net/http/httptest"
8 "net/url"
9 "strings"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/home/connect"
14 "amadan.net/oficina/home/roster"
15 "amadan.net/oficina/home/teammode/teammodetest"
16 "amadan.net/rastrillo/rastrillo"
17)
18
19func deckPath(d Deck) string { return "/d/" + d.Sqid }
20
21// viewerPath is the deck's viewer: its bare address opens the editor
22// (viewer.go's showDeck).
23func viewerPath(d Deck) string { return viewerStart(d.Sqid) }
24
25func countRows(t *testing.T, f *teamFixture, model any) int64 {
26 t.Helper()
27 var n int64
28 if err := f.d.G.Model(model).Count(&n).Error; err != nil {
29 t.Fatal(err)
30 }
31 return n
32}
33
34// A roster member with no account here signs in with Home alone: an
35// addressless user, the sample deck, the team rung, and the chrome of a
36// signed-in person named by their person ref.
37func TestTeamHomeOnlyMemberSignsInAndReachesTheTeamRung(t *testing.T) {
38 f := newTeamFixture(t)
39 colleague := f.member(teamSubject('b'), "member")
40 plan := f.deck(colleague, "Team plan", rungTeam)
41 private := f.deck(colleague, "Colleague's own", rungPrivate)
42
43 u := f.member(teamSubject('a'), "member")
44 var email sql.NullString
45 if err := f.d.Writer().QueryRow("SELECT email FROM users WHERE id=?", u.ID).Scan(&email); err != nil || email.Valid {
46 t.Fatalf("Home-only user email = %v, %v; want NULL", email, err)
47 }
48 list := f.get("/")
49 body := list.Body.String()
50 if list.Code != 200 || !strings.Contains(body, "Team plan") || !strings.Contains(body, sampleDeckTitle) || strings.Contains(body, "Colleague's own") {
51 t.Fatalf("member list = %d, want the team deck and their sample deck only:\n%s", list.Code, body)
52 }
53 if strings.Contains(body, "data-partial") {
54 t.Fatal("a member's list was marked partial")
55 }
56 // Signed-in chrome, named by the ref: a sign-out form, no Sign in.
57 if !strings.Contains(body, `action="/signout"`) || !strings.Contains(body, homeOnlyLabel(teamSubject('a'))) {
58 t.Fatalf("Home-only member's chrome is not signed in as %q", homeOnlyLabel(teamSubject('a')))
59 }
60 if got := f.get(viewerPath(plan)); got.Code != 200 {
61 t.Fatalf("team deck = %d", got.Code)
62 }
63 if got := f.get(viewerPath(private)); got.Code != 404 {
64 t.Fatalf("another member's private deck = %d, want 404", got.Code)
65 }
66}
67
68// Allocation only for a current Member: a stranger is connect's
69// "unknown", an undecidable roster is a 503, and a live session for
70// somebody else is refused before anything is allocated.
71func TestTeamSignInAllocatesOnlyForACurrentMember(t *testing.T) {
72 f := newTeamFixture(t, "x@example.com")
73 users := countRows(t, f, &User{})
74
75 if got := f.freshHomeSignIn(teamSubject('s')); got.Code != http.StatusForbidden {
76 t.Fatalf("stranger = %d, want connect's 403", got.Code)
77 }
78 if countRows(t, f, &User{}) != users {
79 t.Fatal("a stranger was allocated a user")
80 }
81
82 f.dir.Set(teamSubject('u'), "member")
83 f.expired()
84 if got := f.freshHomeSignIn(teamSubject('u')); got.Code != http.StatusServiceUnavailable {
85 t.Fatalf("Unknown roster = %d, want 503", got.Code)
86 }
87 if countRows(t, f, &User{}) != users {
88 t.Fatal("an undecidable roster allocated a user")
89 }
90 f.dir.SetDown(false)
91 f.refreshed()
92
93 f.jar = map[string]*http.Cookie{}
94 f.localUser("x@example.com")
95 users = countRows(t, f, &User{})
96 f.localSession("x@example.com")
97 if got := f.signInHome(teamSubject('u')); got.Code != http.StatusForbidden {
98 t.Fatalf("member arriving over another session = %d, want 403", got.Code)
99 }
100 if countRows(t, f, &User{}) != users {
101 t.Fatal("an arrival over a live session allocated a user")
102 }
103}
104
105// A known Home-only member arriving over somebody else's live session is
106// connect's same-person refusal, and that session survives.
107func TestTeamKnownArrivalOverAnotherSessionIsRefused(t *testing.T) {
108 f := newTeamFixture(t, "x@example.com")
109 f.member(teamSubject('a'), "member")
110 f.jar = map[string]*http.Cookie{}
111 f.localUser("x@example.com")
112 f.localSession("x@example.com")
113 before := map[string]string{}
114 for n, c := range f.jar {
115 before[n] = c.Value
116 }
117 if got := f.signInHome(teamSubject('a')); got.Code != http.StatusForbidden {
118 t.Fatalf("known arrival over another session = %d, want 403", got.Code)
119 }
120 name := f.a.homeSessions.CookieName()
121 if f.jar[name] == nil || f.jar[name].Value != before[name] {
122 t.Fatal("the local session was replaced")
123 }
124 s, ok := f.sessionNow()
125 if !ok || s.Subject != "x@example.com" {
126 t.Fatalf("session now = %+v, want the local one", s)
127 }
128}
129
130// Removal reaches an existing session within the lease: the team deck
131// and their own deck are gone, the list is complete (not partial) and
132// empty, create is 403, and they are not signed out.
133func TestTeamRemovalRevokesAHomeOnlyMembersExistingSession(t *testing.T) {
134 f := newTeamFixture(t)
135 colleague := f.member(teamSubject('b'), "member")
136 plan := f.deck(colleague, "Team plan", rungTeam)
137 u := f.member(teamSubject('a'), "member")
138 own := f.deck(u, "My own", rungPrivate)
139 for _, d := range []Deck{plan, own} {
140 if got := f.get(viewerPath(d)); got.Code != 200 {
141 t.Fatalf("before removal %s = %d", d.Title, got.Code)
142 }
143 }
144 f.dir.Set(teamSubject('a'), "")
145 f.refreshed()
146 for _, d := range []Deck{plan, own} {
147 if got := f.get(viewerPath(d)); got.Code != 404 {
148 t.Fatalf("after removal %s = %d, want 404", d.Title, got.Code)
149 }
150 }
151 list := f.get("/")
152 if list.Code != 200 || strings.Contains(list.Body.String(), "data-partial") || strings.Contains(list.Body.String(), "Team plan") {
153 t.Fatalf("removed member's list = %d (partial or team deck shown)", list.Code)
154 }
155 // Nor is a button offered that would only answer 403.
156 if strings.Contains(list.Body.String(), "slides-create") {
157 t.Fatal("removed member's list offers New deck")
158 }
159 if got := f.get("/decks/new"); got.Code != http.StatusForbidden {
160 t.Fatalf("removed member's new deck page = %d, want 403", got.Code)
161 }
162 if got := f.post("/decks", url.Values{"title": {"x"}, "theme": {defaultTheme}}); got.Code != http.StatusForbidden {
163 t.Fatalf("removed member's create = %d, want 403", got.Code)
164 }
165 if _, ok := f.sessionNow(); !ok {
166 t.Fatal("a removed member was signed out")
167 }
168 // And a grant somebody else gave still reaches.
169 f.grant(plan, u, colleague)
170 if got := f.get(viewerPath(plan)); got.Code != 200 {
171 t.Fatalf("granted deck after removal = %d, want 200", got.Code)
172 }
173}
174
175// The role is the roster's: a member is no owner of a colleague's deck,
176// and a local account with no Home identity is a guest however the
177// allowlist admitted it.
178func TestTeamStandingIsTheRostersNeverTheAllowlists(t *testing.T) {
179 f := newTeamFixture(t, "local@example.com")
180 owner := f.member(teamSubject('o'), "owner")
181 plan := f.deck(owner, "Team plan", rungTeam)
182
183 f.jar = map[string]*http.Cookie{}
184 f.localSession("local@example.com")
185 if got := f.get(viewerPath(plan)); got.Code != 404 {
186 t.Fatalf("unlinked local account on a team deck = %d, want 404", got.Code)
187 }
188 if got := f.get("/decks/new"); got.Code != http.StatusForbidden {
189 t.Fatalf("unlinked local account's create = %d, want 403", got.Code)
190 }
191
192 f.member(teamSubject('m'), "member")
193 if got := f.get(deckPath(plan) + "/settings"); got.Code != 404 {
194 t.Fatalf("a member's settings on a colleague's deck = %d, want 404", got.Code)
195 }
196 if got := f.get(viewerPath(plan)); got.Code != 200 {
197 t.Fatalf("a member's team deck = %d", got.Code)
198 }
199}
200
201// A held decision ends at the lease under a failing refresh: 59 s still
202// a member, 61 s a retryable 503 rather than a 404.
203func TestTeamStandingEndsAtTheLeaseUnderAFailingRefresh(t *testing.T) {
204 f := newTeamFixture(t)
205 colleague := f.member(teamSubject('b'), "member")
206 plan := f.deck(colleague, "Team plan", rungTeam)
207 f.member(teamSubject('a'), "member")
208 f.dir.SetDown(true)
209 f.clock.Step(59 * time.Second)
210 if got := f.get(viewerPath(plan)); got.Code != 200 {
211 t.Fatalf("at 59 s = %d, want 200", got.Code)
212 }
213 f.clock.Step(2 * time.Second)
214 got := f.get(viewerPath(plan))
215 if got.Code != http.StatusServiceUnavailable || got.Header().Get("Retry-After") == "" || got.Header().Get("Cache-Control") != "no-store" {
216 t.Fatalf("at 61 s = %d %v, want a retryable no-store 503", got.Code, got.Header())
217 }
218 // The same answer for a deck that does not exist: no oracle.
219 if got := f.get("/d/2222222222"); got.Code != http.StatusServiceUnavailable {
220 t.Fatalf("missing deck under Unknown = %d, want 503", got.Code)
221 }
222}
223
224// Unknown: the list is the grants with a partial notice, the team rung
225// withheld; a granted deck still opens and a team deck is a retryable
226// 503; create is a 503 too.
227func TestTeamUnknownListIsPartialAndItemsAreRetryable(t *testing.T) {
228 f := newTeamFixture(t)
229 colleague := f.member(teamSubject('b'), "member")
230 plan := f.deck(colleague, "Team plan", rungTeam)
231 shared := f.deck(colleague, "Shared with you", rungPrivate)
232 u := f.member(teamSubject('a'), "member")
233 f.grant(shared, u, colleague)
234 f.expired()
235
236 list := f.get("/")
237 body := list.Body.String()
238 if list.Code != 200 || !strings.Contains(body, "data-partial") || !strings.Contains(body, "Shared with you") || strings.Contains(body, "Team plan") || strings.Contains(body, sampleDeckTitle) {
239 t.Fatalf("Unknown list = %d, want the grant alone, marked partial:\n%s", list.Code, body)
240 }
241 if got := f.get(viewerPath(shared)); got.Code != 200 {
242 t.Fatalf("granted deck under Unknown = %d", got.Code)
243 }
244 if got := f.get(viewerPath(plan)); got.Code != http.StatusServiceUnavailable {
245 t.Fatalf("team deck under Unknown = %d, want 503", got.Code)
246 }
247 if got := f.get("/decks/new"); got.Code != http.StatusServiceUnavailable {
248 t.Fatalf("create under Unknown = %d, want 503", got.Code)
249 }
250}
251
252// Fable's #53 required test 3, for Slides: a Home-only member who holds
253// no grants, under Unknown, gets 200, the notice, and not the "No decks
254// yet" empty state.
255func TestTeamEmptyPartialListSaysPartialNotEmpty(t *testing.T) {
256 f := newTeamFixture(t)
257 f.member(teamSubject('a'), "member")
258 f.expired()
259 list := f.get("/")
260 body := list.Body.String()
261 if list.Code != 200 || !strings.Contains(body, "data-partial") || strings.Contains(body, "rst-empty") || strings.Contains(body, "slides.decks.empty.title") || strings.Contains(body, sampleDeckTitle) {
262 t.Fatalf("empty Unknown list = %d, want the partial notice and no empty state:\n%s", list.Code, body)
263 }
264}
265
266// A storage failure resolving the session's identity is a retryable 503,
267// never a sign-out.
268func TestTeamIdentityStorageFailureIsUnavailableNotSignOut(t *testing.T) {
269 f := newTeamFixture(t, "x@example.com")
270 f.localUser("x@example.com")
271 f.localSession("x@example.com")
272 broken, err := sql.Open("sqlite", "file:closed?mode=memory")
273 if err == nil {
274 broken.Close()
275 }
276 real := f.a.team.db
277 f.a.team.db = broken
278 got := f.get("/")
279 f.a.team.db = real
280 if got.Code != http.StatusServiceUnavailable {
281 t.Fatalf("identity storage failure = %d, want 503", got.Code)
282 }
283 if _, ok := f.sessionNow(); !ok {
284 t.Fatal("a storage failure signed the person out")
285 }
286}
287
288// connect's HomeOnly hook: a storage failure is not "nobody".
289func TestTeamHomeOnlyStorageFailureIsNotNobody(t *testing.T) {
290 f := newTeamFixture(t)
291 broken, err := sql.Open("sqlite", "file:closed2?mode=memory")
292 if err == nil {
293 broken.Close()
294 }
295 _, err = f.a.teamHomeOnly(broken)(context.Background(), teamTestIssuer, teamSubject('a'))
296 if err == nil || err == connect.ErrNoPerson {
297 t.Fatalf("HomeOnly over broken storage = %v, want an error that is not ErrNoPerson", err)
298 }
299}
300
301// sessionNow is the session the jar's cookie currently resolves to.
302func (f *teamFixture) sessionNow() (struct{ Subject, Method string }, bool) {
303 r, _ := http.NewRequest("GET", homeTestOrigin+"/", nil)
304 for _, c := range f.jar {
305 r.AddCookie(c)
306 }
307 s, ok := f.a.homeSessions.From(r)
308 return struct{ Subject, Method string }{s.Subject, s.Method}, ok
309}
310
311var _ = roster.Unknown
312var _ = teammodetest.Team
313
314// A roster Member with no account here is asked to choose (Home spec
315// 2026-11-14-oficina-id-native.md §8, connect's Admit): "Welcome to the
316// team", with Connect my existing account and I'm new here. "I'm new
317// here" makes the Home-only user and signs them in; "connect" goes on to
318// the email-only door to sign in to the existing account; a stranger is
319// connect's unknown, and no choice is offered.
320func TestTeamAMemberWithNoAccountChooses(t *testing.T) {
321 f := newTeamFixture(t)
322 f.dir.Set(teamSubject('a'), "member")
323 f.dir.Set(teamSubject('b'), "member")
324 f.refreshed()
325
326 fin := f.signInHomeChoosing(teamSubject('a'))
327 if fin.Code != http.StatusSeeOther || fin.Header().Get("Location") != connect.ChoosePath {
328 t.Fatalf("finish = %d %q, want the choice", fin.Code, fin.Header().Get("Location"))
329 }
330 page := f.get(connect.ChoosePath)
331 body := page.Body.String()
332 // This fixture has no locale middleware, so the catalog keys are what
333 // prove which sentences were chosen (en.toml holds connect's English).
334 for _, want := range []string{"<h1>slides.home.choose_title</h1>", "slides.home.choose_body", `value="connect">slides.home.choose_connect_action</button>`, `value="new">slides.home.choose_new_action</button>`, `action="/account/home/choose"`} {
335 if page.Code != http.StatusOK || !strings.Contains(body, want) {
336 t.Errorf("choose page lacks %q: %d\n%s", want, page.Code, body)
337 }
338 }
339 if got := f.choose("new"); got.Code != http.StatusSeeOther {
340 t.Fatalf("I'm new here = %d %s", got.Code, got.Body.String())
341 }
342 if _, ok := f.sessionNow(); !ok {
343 t.Fatal("I'm new here signed nobody in")
344 }
345
346 f.jar = map[string]*http.Cookie{}
347 if fin := f.signInHomeChoosing(teamSubject('b')); fin.Header().Get("Location") != connect.ChoosePath {
348 t.Fatalf("finish = %d %q", fin.Code, fin.Header().Get("Location"))
349 }
350 got := f.choose("connect")
351 if got.Code != http.StatusSeeOther || !strings.HasPrefix(got.Header().Get("Location"), connect.SigninLocalPath) {
352 t.Fatalf("connect = %d %q, want the email-only door", got.Code, got.Header().Get("Location"))
353 }
354
355 // With no live choice, the page is "failed" with a way on.
356 f.jar = map[string]*http.Cookie{}
357 if none := f.get(connect.ChoosePath); none.Code != http.StatusForbidden {
358 t.Fatalf("choose with no proof = %d", none.Code)
359 }
360
361 // A stranger is unknown, offered no choice.
362 f.jar = map[string]*http.Cookie{}
363 if fin := f.signInHomeChoosing(teamSubject('z')); fin.Code == http.StatusSeeOther && fin.Header().Get("Location") == connect.ChoosePath {
364 t.Fatal("a stranger was offered the choice")
365 }
366}
367
368// The choice page's words are connect's approved English, word for word.
369func TestTeamChooseCopyIsConnects(t *testing.T) {
370 loc, err := rastrillo.NewLocales(Locales, DefaultLocale, rastrillo.BaseCatalog(), LocaleFS)
371 if err != nil {
372 t.Fatal(err)
373 }
374 loc.Middleware(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
375 for _, key := range []string{"choose_title", "choose_body", "choose_connect_action", "choose_new_action"} {
376 if got, want := rastrillo.T(r, "slides.home."+key), connect.English[key]; got != want {
377 t.Errorf("slides.home.%s = %q, want connect's %q", key, got, want)
378 }
379 }
380 })).ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
381}
382
383// The door in team mode, for a member the roster has removed who still
384// holds a session: removed people keep their session and reach only
385// grants (as in Docs and Sheets), so the gate admits them and /signin
386// sends them on to the list, which answers 200 with what was shared;
387// anything a member does is refused; and nothing loops.
388func TestTeamARemovedMemberAtTheDoorDoesNotLoop(t *testing.T) {
389 f := newTeamFixture(t)
390 f.member(teamSubject('a'), "member")
391 f.dir.Set(teamSubject('a'), "")
392 f.refreshed()
393 for i := 0; i < 2; i++ {
394 if rec := f.get("/signin"); rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/" {
395 t.Fatalf("GET /signin as a removed member: %d %q, want 303 /", rec.Code, rec.Header().Get("Location"))
396 }
397 if rec := f.get("/"); rec.Code != http.StatusOK {
398 t.Fatalf("GET / as a removed member: %d %q, want 200 (grants only)", rec.Code, rec.Header().Get("Location"))
399 }
400 }
401 if rec := f.get("/decks/new"); rec.Code != http.StatusForbidden {
402 t.Fatalf("GET /decks/new as a removed member: %d, want 403", rec.Code)
403 }
404 if _, ok := f.sessionNow(); !ok {
405 t.Fatal("the door signed a removed member out")
406 }
407}
408
409// A member the roster removes between the proof and the choice: "I'm new
410// here" is refused and signs nobody in, since connect asks Admit again.
411func TestTeamAMemberRemovedBeforeChoosingIsRefused(t *testing.T) {
412 f := newTeamFixture(t)
413 f.dir.Set(teamSubject('a'), "member")
414 f.refreshed()
415 if fin := f.signInHomeChoosing(teamSubject('a')); fin.Header().Get("Location") != connect.ChoosePath {
416 t.Fatalf("finish = %d %q, want the choice", fin.Code, fin.Header().Get("Location"))
417 }
418 f.dir.Set(teamSubject('a'), "")
419 f.refreshed()
420 if got := f.choose("new"); got.Code != http.StatusForbidden {
421 t.Fatalf("I'm new here after removal = %d %q, want 403", got.Code, got.Header().Get("Location"))
422 }
423 if _, ok := f.sessionNow(); ok {
424 t.Fatal("a removed member was signed in")
425 }
426}
427
428// "I'm new here" with any app session present is refused: allocation is
429// reachable only with no session at all.
430func TestTeamNewWithASessionIsRefused(t *testing.T) {
431 f := newTeamFixture(t, "x@example.com")
432 f.localUser("x@example.com")
433 f.dir.Set(teamSubject('a'), "member")
434 f.refreshed()
435 if fin := f.signInHomeChoosing(teamSubject('a')); fin.Header().Get("Location") != connect.ChoosePath {
436 t.Fatalf("finish = %d %q, want the choice", fin.Code, fin.Header().Get("Location"))
437 }
438 f.localSession("x@example.com")
439 if got := f.choose("new"); got.Code != http.StatusForbidden {
440 t.Fatalf("I'm new here over a session = %d %q, want 403", got.Code, got.Header().Get("Location"))
441 }
442 if s, ok := f.sessionNow(); !ok || s.Subject != "x@example.com" {
443 t.Fatalf("the session is now %+v (%v), want the local one untouched", s, ok)
444 }
445}
446
447// A stranger to the roster is refused at the finish (connect's unknown),
448// signed in nowhere, and the door still draws for them.
449func TestTeamAStrangerIsRefusedAndTheDoorStillDraws(t *testing.T) {
450 f := newTeamFixture(t)
451 fin := f.signInHomeChoosing(teamSubject('z'))
452 if fin.Code != http.StatusForbidden {
453 t.Fatalf("a stranger's finish = %d %q, want 403", fin.Code, fin.Header().Get("Location"))
454 }
455 if _, ok := f.sessionNow(); ok {
456 t.Fatal("a stranger was signed in")
457 }
458 if rec := f.get("/signin"); rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "estilo-signin-screen") {
459 t.Fatalf("GET /signin for a stranger: %d", rec.Code)
460 }
461}
462