Router tests for the member choice: connect, new over a session, cross-origin
Human
Prompt
Fable review: add router-level tests for the connect choice, new refused with an app session, and a cross-origin choose POST through the real csrf.Protect.
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
Through Docs' real router: 'Connect my existing account' spends the proof, resumes the local link and the member's Oficina ID then opens Alice's account; 'I'm new here' is refused while the browser holds a session and the proof survives; a cross-origin choose POST is stopped by csrf.Protect before connect and allocates nothing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+105
−5
internal/docs/home_choose_test.go+89 −0internal/docs/team_router_test.go+16 −5
CI passed — run details
diff --git a/internal/docs/home_choose_test.go b/internal/docs/home_choose_test.go| new file mode 100644 |
| index 0000000..2b83dc6 |
| --- /dev/null |
| +++ b/internal/docs/home_choose_test.go |
| @@ -0,0 +1,89 @@ |
| +package docs |
| + |
| +import ( |
| + "errors" |
| + "net/url" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/home/connect" |
| + "github.com/carlosframework/docs/internal/docs/principalstore" |
| +) |
| + |
| +// The member choice through Docs' real router (Home spec |
| +// 2026-11-14-oficina-id-native.md §8). |
| + |
| +// "Connect my existing account": the proof is spent, local sign-in resumes |
| +// the link at once, and the member's Oficina ID then opens that account. |
| +func TestTeamChooseConnectLinksTheExistingAccount(t *testing.T) { |
| + f := newTeamRouterFixture(t) |
| + b := f.browser() |
| + nonce := b.start(t, "/auth/start?return_to=%2F", "") |
| + token := b.choosePage(t, b.exchange(t, nonce, teamSubject('a'), time.Now().Add(-time.Minute))) |
| + routerRedirect(t, b.choose("connect", token), "/signin/local") |
| + if _, e := f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('a')); !errors.Is(e, principalstore.ErrRefused) { |
| + t.Fatal("connect allocated a principal", e) |
| + } |
| + // The spent proof chooses nothing more. |
| + if r := b.choose("new", token); r.Code == 303 { |
| + t.Fatal("a spent proof chose again") |
| + } |
| + // Local sign-in lands on the link's fresh Home check, then the confirm. |
| + b.localSignin(t, "alice@example.com", "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm") |
| + nonce = b.start(t, "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm", "300") |
| + routerRedirect(t, b.exchange(t, nonce, teamSubject('a'), time.Now().Add(-time.Minute)), "/account/home/confirm") |
| + routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| + p, e := f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('a')) |
| + if e != nil || p.ID != f.alice || p.Kind != principalstore.Local { |
| + t.Fatal("the member's Oficina ID does not open the existing account", p, e) |
| + } |
| + // A fresh browser now signs straight in as Alice, with no choice. |
| + other := f.browser() |
| + nonce = other.start(t, "/auth/start?return_to=%2F", "") |
| + routerRedirect(t, other.exchange(t, nonce, teamSubject('a'), time.Now()), "/") |
| + if r := other.do("GET", "/d/"+f.own.Sqid, ""); r.Code != 200 { |
| + t.Fatal("linked member cannot open their own document", r.Code) |
| + } |
| +} |
| + |
| +// "I'm new here" is refused while the browser holds any app session, and |
| +// the proof survives for a choice made without one. |
| +func TestTeamChooseNewRefusedWithAnAppSession(t *testing.T) { |
| + f := newTeamRouterFixture(t) |
| + b := f.browser() |
| + nonce := b.start(t, "/auth/start?return_to=%2F", "") |
| + token := b.choosePage(t, b.exchange(t, nonce, teamSubject('b'), time.Now())) |
| + proof := b.cookies["__Host-oficina-home-choice"] |
| + b.localSignin(t, "bob@example.com") |
| + b.cookies[proof.Name] = proof |
| + if r := b.choose("new", token); r.Code != 403 { |
| + t.Fatalf("new over a session=%d", r.Code) |
| + } |
| + if _, e := f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('b')); !errors.Is(e, principalstore.ErrRefused) { |
| + t.Fatal("allocated over a live session", e) |
| + } |
| + fresh := f.browser() |
| + fresh.cookies[proof.Name] = proof |
| + routerRedirect(t, fresh.choose("new", token), "/") |
| +} |
| + |
| +// A cross-origin choose POST is stopped by Docs' own CSRF middleware |
| +// (csrf.Protect, under connect's Middleware) before connect sees it. |
| +func TestTeamChooseRefusesACrossOriginPost(t *testing.T) { |
| + f := newTeamRouterFixture(t) |
| + b := f.browser() |
| + nonce := b.start(t, "/auth/start?return_to=%2F", "") |
| + token := b.choosePage(t, b.exchange(t, nonce, teamSubject('b'), time.Now())) |
| + r := b.request("POST", connect.ChoosePath, url.Values{"choice": {"new"}, "token": {token}}.Encode()) |
| + r.Header.Set("Origin", "https://evil.example") |
| + r.Header.Set("Sec-Fetch-Site", "cross-site") |
| + rec := b.serve(r) |
| + if rec.Code != 403 || !strings.Contains(rec.Body.String(), "cross-origin") { |
| + t.Fatalf("cross-origin choose=%d %q", rec.Code, rec.Body.String()) |
| + } |
| + if _, e := f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('b')); !errors.Is(e, principalstore.ErrRefused) { |
| + t.Fatal("a cross-origin POST allocated", e) |
| + } |
| + routerRedirect(t, b.choose("new", token), "/") |
| +} |
diff --git a/internal/docs/team_router_test.go b/internal/docs/team_router_test.go| index e27e904..7a47f60 100644 |
| --- a/internal/docs/team_router_test.go |
| +++ b/internal/docs/team_router_test.go |
| @@ -173,10 +173,10 @@ func teamSubject(c byte) string { return "pr_" + strings.Repeat(string(c), 26) } |
| var chooseToken = regexp.MustCompile(`name="token" value="([A-Za-z0-9_-]{43})"`) |
| -// chooseNew follows a roster member's first finish (Home spec |
| -// 2026-11-14-oficina-id-native.md §8): the choose page, then "I'm new here". |
| -// Nothing is allocated before the POST. |
| -func (b *routerBrowser) chooseNew(t *testing.T, finish *httptest.ResponseRecorder) { |
| +// choosePage follows a roster member's first finish (Home spec |
| +// 2026-11-14-oficina-id-native.md §8) to the choose page, and returns its |
| +// form token. Nothing is allocated before a POST. |
| +func (b *routerBrowser) choosePage(t *testing.T, finish *httptest.ResponseRecorder) string { |
| t.Helper() |
| routerRedirect(t, finish, connect.ChoosePath) |
| page := b.do("GET", connect.ChoosePath, "") |
| @@ -192,8 +192,19 @@ func (b *routerBrowser) chooseNew(t *testing.T, finish *httptest.ResponseRecorde |
| if m == nil { |
| t.Fatal("choose page carries no form token") |
| } |
| - routerRedirect(t, b.do("POST", connect.ChoosePath, url.Values{"choice": {"new"}, "token": {m[1]}}.Encode()), "/") |
| + return m[1] |
| +} |
| + |
| +func (b *routerBrowser) choose(choice, token string) *httptest.ResponseRecorder { |
| + return b.do("POST", connect.ChoosePath, url.Values{"choice": {choice}, "token": {token}}.Encode()) |
| +} |
| + |
| +// chooseNew is a member's first finish, then "I'm new here". |
| +func (b *routerBrowser) chooseNew(t *testing.T, finish *httptest.ResponseRecorder) { |
| + t.Helper() |
| + routerRedirect(t, b.choose("new", b.choosePage(t, finish)), "/") |
| } |
| + |
| func (b *routerBrowser) update(doc *Document, body []byte) int { |
| r := b.request("POST", "/d/"+doc.Sqid+"/updates", string(body)) |
| r.Header.Set("Content-Type", "application/octet-stream") |