| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/home/connect" |
| 12 | ) |
| 13 | |
| 14 | // connectItem is the account menu's "Connect your Oficina account" row, as |
| 15 | // estilo's menu draws it. |
| 16 | const connectItem = `<a estilo-menu-item href="/account/home"><span estilo-menu-label>Connect your Oficina account</span></a>` |
| 17 | |
| 18 | const freshHomeStart = "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" |
| 19 | |
| 20 | func (f *homeRouterFixture) offersConnect(t *testing.T) bool { |
| 21 | t.Helper() |
| 22 | w := f.do("GET", "/day/2026-09-10", "") |
| 23 | if w.Code != 200 { |
| 24 | t.Fatalf("GET /day: %d", w.Code) |
| 25 | } |
| 26 | if !strings.Contains(w.Body.String(), "estilo-account") { |
| 27 | t.Fatal("GET /day drew no account menu") |
| 28 | } |
| 29 | return strings.Contains(w.Body.String(), connectItem) |
| 30 | } |
| 31 | |
| 32 | // linkCount is how many stored links the person has: the table itself, |
| 33 | // not the bridge's answer about it. |
| 34 | func (f *homeRouterFixture) linkCount(t *testing.T, address string) int64 { |
| 35 | t.Helper() |
| 36 | var n int64 |
| 37 | if err := f.app.db.Writer().QueryRowContext(t.Context(), `SELECT COUNT(*) FROM home_identity_links l JOIN people p ON p.id = l.person_id WHERE p.address = ?`, address).Scan(&n); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | return n |
| 41 | } |
| 42 | |
| 43 | // connectFromMenu runs the menu's link flow for a locally signed-in alice, |
| 44 | // through the confirm POST, and returns where the POST sent her. |
| 45 | func (f *homeRouterFixture) connectFromMenu(t *testing.T, subject string) string { |
| 46 | t.Helper() |
| 47 | // A signed-in page first: requirePerson creates the person the |
| 48 | // connection pages look up (TestHomeConnectStartPageNeverNamesHome). |
| 49 | if w := f.do("GET", "/day/2026-09-10", ""); w.Code != 200 { |
| 50 | t.Fatal("local calendar", w.Code) |
| 51 | } |
| 52 | if w := f.do("GET", "/account/home", ""); w.Code != 200 { |
| 53 | t.Fatal("start page", w.Code, w.Body.String()) |
| 54 | } |
| 55 | start := homeRedirect(t, f.do("POST", "/account/home", "")) |
| 56 | if got := homeRedirect(t, f.exchange(t, start, subject)); got != "/account/home/confirm" { |
| 57 | t.Fatal("fresh assertion landed on", got) |
| 58 | } |
| 59 | if w := f.do("GET", "/account/home/confirm", ""); w.Code != 200 || !strings.Contains(w.Body.String(), "Connect these two accounts?") { |
| 60 | t.Fatalf("confirm page: %d\n%s", w.Code, w.Body.String()) |
| 61 | } |
| 62 | return homeRedirect(t, f.do("POST", "/account/home/confirm", "")) |
| 63 | } |
| 64 | |
| 65 | // A person signed in with an emailed link, with no stored link, is offered |
| 66 | // the connection in the account menu. |
| 67 | func TestConnectItemOfferedToALocalUnlinkedPerson(t *testing.T) { |
| 68 | f := newHomeRouterFixture(t) |
| 69 | f.local(t) |
| 70 | if !f.offersConnect(t) { |
| 71 | t.Fatal("a locally signed-in, unlinked person is not offered Connect your Oficina account") |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Once linked, the item is gone however the session signed in: here a |
| 76 | // fresh emailed-link sign-in, in a new browser, after the link was stored |
| 77 | // (Paul's Docs report, 2026-10-24: the item was offered by session method). |
| 78 | func TestConnectItemNotOfferedToALinkedPersonSignedInLocally(t *testing.T) { |
| 79 | f := newHomeRouterFixture(t) |
| 80 | f.local(t) |
| 81 | f.connectFromMenu(t, "opaque-home-alice") |
| 82 | if f.linkCount(t, "alice@example.test") != 1 { |
| 83 | t.Fatal("the link flow stored no link") |
| 84 | } |
| 85 | f.cookies = map[string]*http.Cookie{} |
| 86 | f.local(t) |
| 87 | if f.offersConnect(t) { |
| 88 | t.Fatal("a linked person signed in with an emailed link is still offered Connect your Oficina account") |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestConnectItemNotOfferedToAHomeSession(t *testing.T) { |
| 93 | f := newHomeRouterFixture(t) |
| 94 | f.local(t) |
| 95 | f.connectFromMenu(t, "opaque-home-alice") |
| 96 | f.cookies = map[string]*http.Cookie{} |
| 97 | start := homeRedirect(t, f.do("GET", connect.SigninOficinaPath, "")) |
| 98 | if got := homeRedirect(t, f.exchange(t, start, "opaque-home-alice")); got != "/" { |
| 99 | t.Fatal("Home sign-in landed on", got) |
| 100 | } |
| 101 | if f.offersConnect(t) { |
| 102 | t.Fatal("a Home session is offered Connect your Oficina account") |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Right after the confirm POST, in the same (local) session: "Accounts |
| 107 | // connected", a stored row, and no item on the next page. |
| 108 | func TestConfirmLeadsToAccountsConnectedAndRetiresTheItem(t *testing.T) { |
| 109 | f := newHomeRouterFixture(t) |
| 110 | f.local(t) |
| 111 | if !f.offersConnect(t) { |
| 112 | t.Fatal("not offered before linking") |
| 113 | } |
| 114 | next := f.connectFromMenu(t, "opaque-home-alice") |
| 115 | u, err := url.Parse(next) |
| 116 | if err != nil || u.Path != "/account/home/confirm" || u.Query().Get("connected") != "1" { |
| 117 | t.Fatal("confirm POST sent the browser to", next) |
| 118 | } |
| 119 | w := f.do("GET", next, "") |
| 120 | if w.Code != 200 || !strings.Contains(w.Body.String(), "Accounts connected") { |
| 121 | t.Fatalf("after confirm: %d, want Accounts connected\n%s", w.Code, w.Body.String()) |
| 122 | } |
| 123 | if n := f.linkCount(t, "alice@example.test"); n != 1 { |
| 124 | t.Fatalf("home_identity_links holds %d rows for alice, want 1", n) |
| 125 | } |
| 126 | if f.offersConnect(t) { |
| 127 | t.Fatal("the next page still offers Connect your Oficina account to a now-linked person") |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // The success page's way back is the page the link began from |
| 132 | // (connect.PageFor), not "/". |
| 133 | func TestAccountsConnectedReturnsToWhereTheLinkBegan(t *testing.T) { |
| 134 | f := newHomeRouterFixture(t) |
| 135 | f.local(t) |
| 136 | f.connectFromMenu(t, "opaque-home-alice") |
| 137 | w := f.do("GET", "/account/home/confirm?connected=1&return_to=%2Fday%2F2026-09-10", "") |
| 138 | if w.Code != 200 || !strings.Contains(w.Body.String(), "Accounts connected") { |
| 139 | t.Fatalf("success page: %d\n%s", w.Code, w.Body.String()) |
| 140 | } |
| 141 | if !strings.Contains(w.Body.String(), `<a rst-btn="primary" href="/day/2026-09-10">`) { |
| 142 | t.Fatalf("Accounts connected does not lead back to /day/2026-09-10:\n%s", w.Body.String()) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // An Oficina sign-in with an unlinked subject shows the unknown page; a |
| 147 | // local sign-in in the same browser then begins the link itself, through |
| 148 | // the fresh Home assertion, to the confirm page, and the confirm stores it. |
| 149 | // "first sign-in" starts with no people row at all (Astra's review of #31): |
| 150 | // the local sign-in must create the person before connect resumes, or the |
| 151 | // resume falls to /account/home and that page answers 403. |
| 152 | func TestUnknownOficinaSigninResumesAfterLocalSignin(t *testing.T) { |
| 153 | for _, seeded := range []bool{true, false} { |
| 154 | name := "existing person" |
| 155 | if !seeded { |
| 156 | name = "first sign-in" |
| 157 | } |
| 158 | t.Run(name, func(t *testing.T) { |
| 159 | f := newHomeRouterFixture(t) |
| 160 | if seeded { |
| 161 | if _, err := f.app.store.EnsurePersonSetup(t.Context(), "alice@example.test"); err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | } |
| 165 | unknownThenResume(t, f) |
| 166 | }) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | func unknownThenResume(t *testing.T, f *homeRouterFixture) { |
| 171 | t.Helper() |
| 172 | start := homeRedirect(t, f.do("GET", connect.SigninOficinaPath, "")) |
| 173 | unknown := f.exchange(t, start, "opaque-home-alice") |
| 174 | if unknown.Code != 403 || !strings.Contains(unknown.Body.String(), "Oficina account not connected") || !strings.Contains(unknown.Body.String(), `<a rst-btn="primary" href="/signin/local">`) { |
| 175 | t.Fatalf("unknown assertion: %d, want the unknown page leading to /signin/local\n%s", unknown.Code, unknown.Body.String()) |
| 176 | } |
| 177 | if got := f.local(t); got != freshHomeStart { |
| 178 | t.Fatalf("local sign-in after an unknown assertion landed on %q, want %q", got, freshHomeStart) |
| 179 | } |
| 180 | if f.cookies["__Host-oficina-home-init"] == nil { |
| 181 | t.Fatal("the resumed link set no __Host-oficina-home-init") |
| 182 | } |
| 183 | if got := homeRedirect(t, f.exchange(t, freshHomeStart, "opaque-home-alice")); got != "/account/home/confirm" { |
| 184 | t.Fatal("fresh assertion landed on", got) |
| 185 | } |
| 186 | if w := f.do("GET", "/account/home/confirm", ""); w.Code != 200 || !strings.Contains(w.Body.String(), "Connect these two accounts?") { |
| 187 | t.Fatalf("confirm page: %d\n%s", w.Code, w.Body.String()) |
| 188 | } |
| 189 | if f.linkCount(t, "alice@example.test") != 0 { |
| 190 | t.Fatal("linked before the confirm click") |
| 191 | } |
| 192 | homeRedirect(t, f.do("POST", "/account/home/confirm", "")) |
| 193 | if n := f.linkCount(t, "alice@example.test"); n != 1 { |
| 194 | t.Fatalf("home_identity_links holds %d rows for alice after confirm, want 1", n) |
| 195 | } |
| 196 | resolved, err := f.app.home.bridge.Resolve(t.Context(), "opaque-home-alice") |
| 197 | if err != nil || resolved.Label != "alice@example.test" { |
| 198 | t.Fatal("the stored link does not resolve to alice", resolved, err) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // The remembered unknown assertion lasts ten minutes. Older, and the local |
| 203 | // sign-in lands as any other does. |
| 204 | func TestUnknownOficinaSigninExpiresAfterTenMinutes(t *testing.T) { |
| 205 | f := newHomeRouterFixture(t, 11*time.Minute) |
| 206 | if _, err := f.app.store.EnsurePersonSetup(t.Context(), "alice@example.test"); err != nil { |
| 207 | t.Fatal(err) |
| 208 | } |
| 209 | start := homeRedirect(t, f.do("GET", connect.SigninOficinaPath, "")) |
| 210 | if w := f.exchange(t, start, "opaque-home-alice"); w.Code != 403 { |
| 211 | t.Fatal("unknown assertion", w.Code) |
| 212 | } |
| 213 | if f.cookies["__Host-oficina-local-link-navigation"] == nil { |
| 214 | t.Fatal("the unknown assertion left no marker, so this test would prove nothing") |
| 215 | } |
| 216 | f.behind = 0 |
| 217 | if got := f.local(t); got != "/" { |
| 218 | t.Fatalf("local sign-in after an expired marker landed on %q, want /", got) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // The connection pages put their buttons in one rst-form-foot row, primary |
| 223 | // first (estilo's homeconnect). |
| 224 | func TestConnectPageButtonsShareOneFormFootRow(t *testing.T) { |
| 225 | f := newHomeRouterFixture(t) |
| 226 | f.local(t) |
| 227 | f.do("GET", "/day/2026-09-10", "") |
| 228 | w := f.do("GET", "/account/home", "") |
| 229 | if w.Code != 200 { |
| 230 | t.Fatal(w.Code) |
| 231 | } |
| 232 | row := regexp.MustCompile(`<div rst-form-foot>\s*<form method="post" action="/account/home"><button rst-btn="primary" type="submit">Connect</button></form>\s*<a rst-btn="ghost" href="/">Back</a>\s*</div>`) |
| 233 | if !row.MatchString(w.Body.String()) { |
| 234 | t.Fatalf("the start page's buttons are not one rst-form-foot row, primary first:\n%s", w.Body.String()) |
| 235 | } |
| 236 | } |
| 237 | |