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 "html"
5 "net/http/httptest"
6 "net/url"
7 "strings"
8 "testing"
9 "time"
10
11 "amadan.net/rastrillo/rastrillo"
12)
13
14const connectItem = `href="/account/home"`
15
16func (b *routerBrowser) menuOffersConnect(t *testing.T) bool {
17 t.Helper()
18 rec := b.do("GET", "/", "")
19 if rec.Code != 200 {
20 t.Fatalf("list=%d", rec.Code)
21 }
22 return strings.Contains(rec.Body.String(), connectItem)
23}
24
25// confirmLink posts "Connect accounts" and follows it to the success page.
26func (b *routerBrowser) confirmLink(t *testing.T, back string) {
27 t.Helper()
28 routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL(back))
29 done := b.do("GET", connectedURL(back), "")
30 body := done.Body.String()
31 if done.Code != 200 || !strings.Contains(body, rastrillo.T(httptest.NewRequest("GET", "/", nil), "docs.home.success_title")) || !strings.Contains(body, `href="`+back+`"`) {
32 t.Fatalf("no Accounts connected page leading back to %q: %d\n%s", back, done.Code, body)
33 }
34}
35
36// TestHomeMenuOffersConnectOnlyWithoutAStoredLink is the bug Paul hit as
37// paul@tito.io: signed in with the Docs email link to an account already
38// linked to an Oficina identity, the menu still offered to connect it.
39func TestHomeMenuOffersConnectOnlyWithoutAStoredLink(t *testing.T) {
40 f := newRouterHomeFixture(t)
41 b := f.browser()
42 b.localSignin(t, "alice@example.com")
43 if !b.menuOffersConnect(t) {
44 t.Fatal("signed in locally and not linked: no Connect item")
45 }
46 b.linkProof(t, "opaque-alice", "alice@example.com")
47 b.confirmLink(t, "/")
48 if routerRows(t, f, "home_identity_links") != 1 {
49 t.Fatal("Connect accounts did not store the link")
50 }
51 if b.menuOffersConnect(t) {
52 t.Fatal("same session, just linked: Connect item still offered")
53 }
54 again := f.browser()
55 again.localSignin(t, "alice@example.com")
56 if again.menuOffersConnect(t) {
57 t.Fatal("signed in locally and linked: Connect item offered")
58 }
59 home := f.browser()
60 nonce := home.start(t, "/auth/start?return_to=%2F", "")
61 routerRedirect(t, home.exchange(t, nonce, "opaque-alice", time.Now().Add(-time.Hour)), "/")
62 if home.menuOffersConnect(t) {
63 t.Fatal("signed in through Home: Connect item offered")
64 }
65 other := f.browser()
66 other.localSignin(t, "bob@example.com")
67 if !other.menuOffersConnect(t) {
68 t.Fatal("another unlinked person lost the Connect item")
69 }
70}
71
72// TestHomeUnknownArrivalResumesLinkAfterLocalSignIn: an Oficina assertion
73// with no link, then signing in to Docs, lands on the confirm step for a
74// fresh Oficina assertion. Nothing to find in the menu, and every rule of
75// the menu path still holds.
76func TestHomeUnknownArrivalResumesLinkAfterLocalSignIn(t *testing.T) {
77 f := newRouterHomeFixture(t)
78 // A brand-new address has no Docs account to connect yet: it lands on
79 // the Connect page, which creates nothing until the person goes on.
80 fresh := f.browser()
81 if rec := fresh.exchange(t, fresh.start(t, "/auth/start?return_to=%2F", ""), "opaque-bob", time.Now().Add(-time.Hour)); rec.Code != 403 {
82 t.Fatal("unknown Bob", rec.Code)
83 }
84 bobPath, bobForm := confirmForm(fresh.localLink(t, "bob@example.com"))
85 routerRedirect(t, fresh.do("POST", bobPath, bobForm), localLinkCompletePath)
86 routerRedirect(t, fresh.do("GET", localLinkCompletePath, ""), "/account/home")
87 if _, err := f.app.store.EnsurePerson(t.Context(), "alice@example.com"); err != nil {
88 t.Fatal(err)
89 }
90 b := f.browser()
91 nonce := b.start(t, "/auth/start?return_to=%2F", "")
92 unknown := b.exchange(t, nonce, "opaque-alice", time.Now().Add(-time.Hour))
93 body := unknown.Body.String()
94 if unknown.Code != 403 || !strings.Contains(body, `href="/signin/local"`) || !strings.Contains(body, "docs.home.unknown_body") || Catalog["docs.home.unknown_body"] != "Sign in to your Docs account once to connect it to your Oficina account." {
95 t.Fatalf("unknown page does not say what signing in will do: %d\n%s", unknown.Code, body)
96 }
97 if b.cookies[localLinkNavigationCookie] == nil || b.cookies[f.sessions.CookieName()] != nil {
98 t.Fatal("unknown arrival not remembered, or it minted a session")
99 }
100 verifyPath, verifyForm := confirmForm(b.localLink(t, "alice@example.com"))
101 routerRedirect(t, b.do("POST", verifyPath, verifyForm), localLinkCompletePath)
102 next := routerRedirect(t, b.do("GET", localLinkCompletePath, ""), "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm")
103 if b.cookies[homeInitCookie] == nil {
104 t.Fatal("resume did not begin the link")
105 }
106 nonce = b.start(t, next, "300")
107 routerRedirect(t, b.exchange(t, nonce, "opaque-alice", time.Now().Add(-time.Minute)), "/account/home/confirm")
108 if routerRows(t, f, "home_identity_links") != 0 {
109 t.Fatal("linked before the explicit confirm")
110 }
111 confirm := b.do("GET", "/account/home/confirm", "")
112 if confirm.Code != 200 || !strings.Contains(confirm.Body.String(), "alice@example.com") || !strings.Contains(confirm.Body.String(), "docs.home.confirm_title") || Catalog["docs.home.confirm_title"] == Catalog["docs.home.start_title"] {
113 t.Fatalf("confirm step not shown: %d", confirm.Code)
114 }
115 b.confirmLink(t, "/")
116 if routerRows(t, f, "home_identity_links") != 1 || b.menuOffersConnect(t) {
117 t.Fatal("resumed link not stored, or the menu still offers it")
118 }
119}
120
121// TestHomeLinkActionsShareOneRow: Connect and Back to Docs sat stacked
122// flush; they share the door's form-foot row, primary first.
123func TestHomeLinkActionsShareOneRow(t *testing.T) {
124 f := newRouterHomeFixture(t)
125 b := f.browser()
126 b.localSignin(t, "alice@example.com")
127 html := b.do("GET", "/account/home", "").Body.String()
128 row := strings.Index(html, "<div rst-form-foot>")
129 primary, ghost := strings.Index(html, `rst-btn="primary"`), strings.LastIndex(html, `rst-btn="ghost"`)
130 if row < 0 || primary < row || ghost < primary || !strings.Contains(html[row:], "</div>") || row+strings.Index(html[row:], "</div>") < ghost {
131 t.Fatalf("actions are not one row with primary first:\n%s", html)
132 }
133}
134
135// With Home, a deep link opened signed out and finished by a local
136// sign-in still lands on the deep link (spec §9.4). connect lands a local
137// sign-in on "/"; Docs puts its own remembered page there.
138func TestHomeLocalSignInReturnsToTheDeepLink(t *testing.T) {
139 f := newRouterHomeFixture(t)
140 owner := f.browser()
141 owner.localSignin(t, "alice@example.com")
142 deep := routerRedirect(t, owner.do("POST", "/documents", "title=Minutes"), "") + "?view=1"
143 b := f.browser()
144 signin := routerRedirect(t, b.do("GET", deep, ""), "/signin?return_to="+url.QueryEscape(deep))
145 // /signin is the door now (Home spec 2026-11-19-smart-door.md), and
146 // Continue with Oficina ID carries the deep link to /signin/oficina.
147 door := b.do("GET", signin, "")
148 oficina := "/signin/oficina?return_to=" + url.QueryEscape(deep)
149 if door.Code != 200 || !strings.Contains(door.Body.String(), `href="`+html.EscapeString(oficina)+`"`) {
150 t.Fatalf("the door does not offer Oficina ID with the deep link (%d)", door.Code)
151 }
152 if b.cookies[homeReturnCookie] == nil {
153 t.Fatal("the door did not remember the deep link for the local landing")
154 }
155 routerRedirect(t, b.do("GET", oficina, ""), "/auth/start?return_to="+url.QueryEscape(deep))
156 if b.cookies[homeReturnCookie] == nil || b.cookies[connectReturnCookie] == nil {
157 t.Fatal("the deep link was not remembered for both landings")
158 }
159 b.returnTo = deep // the door's form posts the page it was opened for
160 b.localSignin(t, "alice@example.com", deep)
161}
162