| 1 | package calendar |
| 2 | |
| 3 | // The smart door's routes and every state it answers. |
| 4 | |
| 5 | import ( |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/estilo.git/chrome" |
| 12 | "amadan.net/oficina/home/connect" |
| 13 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 14 | ) |
| 15 | |
| 16 | func TestSmartDoorStatesWithHome(t *testing.T) { |
| 17 | f := newHomeRouterFixture(t) |
| 18 | glyph := string(chrome.OficinaGlyph) |
| 19 | oficina := &lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"} |
| 20 | cases := []struct { |
| 21 | name, path string |
| 22 | record *lastsignin.Record |
| 23 | want, not []string |
| 24 | title string |
| 25 | }{ |
| 26 | {name: "first visit", path: "/signin", |
| 27 | want: []string{oficinaButton, `href="/signin/oficina"`, glyph, otherWays, `href="/signin?way=oficina&ways=1"`}, |
| 28 | not: []string{emailField, "Continue as", "Use a different email"}, |
| 29 | title: "Sign in to Calendar"}, |
| 30 | {name: "first visit from a page", path: "/signin?return_to=%2Fday%2F2026-09-10", |
| 31 | want: []string{`href="/signin/oficina?return_to=%2Fday%2F2026-09-10"`, `href="/signin?return_to=%2Fday%2F2026-09-10&way=oficina&ways=1"`}}, |
| 32 | {name: "local: email is the one way", path: "/signin/local", |
| 33 | want: []string{emailField, ">Continue</button>", `action="/signin"`}, |
| 34 | not: []string{"Oficina ID", "/signin/oficina", otherWays}, |
| 35 | title: "Sign in to Calendar"}, |
| 36 | {name: "step-up: local ways only", path: "/signin?reauth=1", |
| 37 | want: []string{emailField, "Sign in again to continue."}, |
| 38 | not: []string{"Oficina ID", "/signin/oficina"}}, |
| 39 | {name: "step-up on /signin/local", path: "/signin/local?reauth=1", |
| 40 | want: []string{emailField, "Sign in again to continue."}, |
| 41 | not: []string{"Oficina ID", "/signin/oficina"}}, |
| 42 | {name: "step-up remembered Oficina ID", path: "/signin?reauth=1", record: oficina, |
| 43 | want: []string{"Continue as <bdi>alice@example.test</bdi>", "Sign in again to continue."}, |
| 44 | not: []string{"Oficina ID", "/signin/oficina"}}, |
| 45 | {name: "chooser", path: "/signin?ways=1", |
| 46 | want: []string{`<h1 id="rst-signin-heading">Other ways to sign in</h1>`, `href="/signin?way=email"`, ">Email</span>", ">Back</span>"}, |
| 47 | not: []string{oficinaButton, emailField}, |
| 48 | title: "Other ways to sign in to Calendar"}, |
| 49 | {name: "email chosen", path: "/signin?way=email", |
| 50 | want: []string{emailField, otherWays}, |
| 51 | not: []string{oficinaButton}}, |
| 52 | {name: "Oficina ID remembered", path: "/signin", record: oficina, |
| 53 | want: []string{oficinaButton, otherWays}, |
| 54 | // Never captioned with an address. |
| 55 | not: []string{"alice@example.test", emailField}}, |
| 56 | {name: "Oficina ID remembered: the chooser", path: "/signin?ways=1&way=oficina", record: oficina, |
| 57 | want: []string{">Email <bdi>alice@example.test</bdi></span>", "Use a different email", ">Back</span>"}, |
| 58 | not: []string{oficinaButton}}, |
| 59 | {name: "Oficina ID remembered for a Home-only member", path: "/signin?ways=1&way=oficina", |
| 60 | record: &lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina}, |
| 61 | want: []string{">Email</span>"}, |
| 62 | not: []string{"<bdi>"}}, |
| 63 | {name: "email link remembered", path: "/signin", |
| 64 | record: &lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "bob@example.test"}, |
| 65 | want: []string{"Continue as <bdi>bob@example.test</bdi>", `name="address" value="bob@example.test"`, otherWays}, |
| 66 | not: []string{oficinaButton}}, |
| 67 | {name: "email link remembered: the chooser", path: "/signin?ways=1&way=email", |
| 68 | record: &lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "bob@example.test"}, |
| 69 | want: []string{glyph + "<span>Oficina ID</span>", `href="/signin?way=oficina"`, "Use a different email", `action="/signin/forget"`}, |
| 70 | not: []string{">Email</span>"}}, |
| 71 | {name: "Keymail remembered", path: "/signin", |
| 72 | record: &lastsignin.Record{Method: lastsignin.MethodKeymail, Address: "bob@example.test"}, |
| 73 | // rastrillo v0.45.0: the door never names Keymail. |
| 74 | want: []string{"Continue as <bdi>bob@example.test</bdi>", `name="expect" value="keymail"`, otherWays}}, |
| 75 | {name: "problem", path: "/signin?err=expired", record: oficina, |
| 76 | want: []string{"That link has expired or was already used.", emailField}, |
| 77 | not: []string{oficinaButton}, |
| 78 | title: "Problem signing in to Calendar"}, |
| 79 | {name: "rate", path: "/signin?err=rate", |
| 80 | want: []string{"Too many tries. Try again in a few minutes.", emailField}}, |
| 81 | {name: "a failed Keymail keeps force", path: "/signin?err=keymail&force=1", |
| 82 | want: []string{"We couldn't confirm it's you.", `name="force" value="1"`, "Send me a link instead"}}, |
| 83 | {name: "removed is team mode's alone", path: "/signin?err=removed", |
| 84 | want: []string{"Something went wrong. Try again."}, |
| 85 | not: []string{appProblem, signinRemoved}}, |
| 86 | {name: "sent", path: "/signin?sent=1", |
| 87 | want: []string{"Check your email", "We sent a link to your inbox.", "Use a different email"}, |
| 88 | not: []string{oficinaButton, emailField}, |
| 89 | title: "Check your email to sign in to Calendar"}, |
| 90 | {name: "an unknown way is the remembered one", path: "/signin?way=nonsense", |
| 91 | want: []string{oficinaButton}}, |
| 92 | {name: "a typed refusal is no refusal", path: "/signin?err=refused", |
| 93 | not: []string{appProblem, signinRefusal}}, |
| 94 | } |
| 95 | for _, c := range cases { |
| 96 | t.Run(c.name, func(t *testing.T) { |
| 97 | br := f.fresh() |
| 98 | if c.record != nil { |
| 99 | br.remember(t, *c.record) |
| 100 | } |
| 101 | rec := br.get(c.path) |
| 102 | if rec.Code != http.StatusOK { |
| 103 | t.Fatalf("GET %s: %d %q", c.path, rec.Code, rec.Header().Get("Location")) |
| 104 | } |
| 105 | if rec.Header().Get("Cache-Control") != "no-store" { |
| 106 | t.Error("the door is cacheable") |
| 107 | } |
| 108 | card := door(t, rec.Body.String()) |
| 109 | for _, want := range c.want { |
| 110 | if !strings.Contains(card, want) { |
| 111 | t.Errorf("lacks %q:\n%s", want, card) |
| 112 | } |
| 113 | } |
| 114 | for _, not := range c.not { |
| 115 | if strings.Contains(card, not) { |
| 116 | t.Errorf("has %q:\n%s", not, card) |
| 117 | } |
| 118 | } |
| 119 | if c.title != "" { |
| 120 | if got := titleOf(t, rec.Body.String()); got != c.title { |
| 121 | t.Errorf("<title> = %q, want %q", got, c.title) |
| 122 | } |
| 123 | } |
| 124 | body := rec.Body.String() |
| 125 | if strings.Contains(body, "rastrillo.ui.") { |
| 126 | t.Error("a catalog key leaked onto the door") |
| 127 | } |
| 128 | // The bar shows the brand alone on every door, and the door |
| 129 | // carries no Colophon link (Paul, 2026-11-19). |
| 130 | if strings.Contains(body, "estilo-account") || strings.Contains(body, `href="/help/open-source"`) { |
| 131 | t.Error("the door draws an account menu or a Colophon link") |
| 132 | } |
| 133 | }) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Standalone Calendar has only email: no Oficina ID anywhere, no chooser |
| 138 | // link with nothing on it, and no /signin/oficina, /signin/local or choose |
| 139 | // page. |
| 140 | func TestSmartDoorStandaloneHasOnlyEmail(t *testing.T) { |
| 141 | f := newStandaloneDoor(t) |
| 142 | for _, path := range []string{"/signin", "/signin?way=oficina"} { |
| 143 | rec := f.get(path) |
| 144 | card := door(t, rec.Body.String()) |
| 145 | if rec.Code != http.StatusOK || !strings.Contains(card, emailField) || strings.Contains(card, "Oficina") || strings.Contains(card, otherWays) { |
| 146 | t.Errorf("GET %s: %d\n%s", path, rec.Code, card) |
| 147 | } |
| 148 | } |
| 149 | if got := titleOf(t, f.get("/signin").Body.String()); got != "Sign in to Calendar" { |
| 150 | t.Errorf("<title> = %q", got) |
| 151 | } |
| 152 | // Nothing links the chooser here; typed in, it has no Oficina ID row. |
| 153 | if card := door(t, f.get("/signin?ways=1").Body.String()); strings.Contains(card, "Oficina") || !strings.Contains(card, ">Back</span>") { |
| 154 | t.Errorf("standalone chooser:\n%s", card) |
| 155 | } |
| 156 | for _, path := range []string{connect.SigninOficinaPath, connect.SigninLocalPath, connect.ChoosePath} { |
| 157 | if rec := f.get(path); rec.Code != http.StatusNotFound { |
| 158 | t.Errorf("standalone %s: %d, want 404", path, rec.Code) |
| 159 | } |
| 160 | } |
| 161 | // A remembered email way is offered back as a one-tap, with Use a |
| 162 | // different email in place of the Other ways link. |
| 163 | r := f.fresh() |
| 164 | r.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"}) |
| 165 | card := door(t, r.get("/signin").Body.String()) |
| 166 | if !strings.Contains(card, "Continue as <bdi>alice@example.test</bdi>") || !strings.Contains(card, "Use a different email") || strings.Contains(card, otherWays) { |
| 167 | t.Errorf("standalone remembered door:\n%s", card) |
| 168 | } |
| 169 | // Sent, standalone, is the door's own Check your email. |
| 170 | if got := titleOf(t, f.get("/signin?sent=1").Body.String()); got != "Check your email to sign in to Calendar" { |
| 171 | t.Errorf("standalone Sent <title> = %q", got) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Continue with Oficina ID starts the Home exchange, keeping the page; a |
| 176 | // step-up never reaches Home; a browser already signed in and admitted |
| 177 | // skips both doors, unless the query names a state of the door, and |
| 178 | // /signin/local has no shortcut at all. |
| 179 | func TestSmartDoorRoutesWithHome(t *testing.T) { |
| 180 | f := newHomeRouterFixture(t) |
| 181 | redirectsTo(t, f.get("/signin/oficina?return_to=%2Fday%2F2026-09-10"), "/auth/start?return_to=%2Fday%2F2026-09-10") |
| 182 | redirectsTo(t, f.get("/signin/oficina?reauth=1&return_to=%2Fday%2F2026-09-10"), "/signin/local?reauth=1&return_to=%2Fday%2F2026-09-10") |
| 183 | |
| 184 | alice := f.fresh() |
| 185 | alice.session(t, "alice@example.test", "magiclink", time.Now()) |
| 186 | in := alice.get("/signin?return_to=%2Fday%2F2026-09-10") |
| 187 | redirectsTo(t, in, "/day/2026-09-10") |
| 188 | if len(in.Result().Cookies()) != 0 { |
| 189 | t.Errorf("an admitted /signin set cookies: %v", in.Result().Cookies()) |
| 190 | } |
| 191 | redirectsTo(t, alice.get("/signin/oficina"), "/") |
| 192 | for _, path := range []string{"/signin?reauth=1", "/signin?sent=1", "/signin?err=expired", "/signin/local"} { |
| 193 | if rec := alice.get(path); rec.Code != http.StatusOK { |
| 194 | t.Errorf("signed in, GET %s: %d %q, want the door", path, rec.Code, rec.Header().Get("Location")) |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // Standalone, an admitted session skips the door to its page; the door's |
| 200 | // own states still draw; a page that would loop is never a destination. |
| 201 | func TestSmartDoorRoutesStandalone(t *testing.T) { |
| 202 | f := newStandaloneDoor(t) |
| 203 | f.session(t, "alice@example.test", "magiclink", time.Now()) |
| 204 | redirectsTo(t, f.get("/signin?return_to=%2Fday%2F2026-09-10"), "/day/2026-09-10") |
| 205 | redirectsTo(t, f.get("/signin"), "/") |
| 206 | for _, loop := range []string{"/signed-out", "/signin", "/auth/verify", "//evil.example/x"} { |
| 207 | redirectsTo(t, f.get("/signin?return_to="+strings.ReplaceAll(loop, "/", "%2F")), "/") |
| 208 | } |
| 209 | for _, path := range []string{"/signin?sent=1", "/signin?err=expired"} { |
| 210 | if rec := f.get(path); rec.Code != http.StatusOK { |
| 211 | t.Errorf("signed in, GET %s: %d, want the door", path, rec.Code) |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // The confirm step an emailed link opens is the door too, in both modes: |
| 217 | // one Sign in button posting the token back, and the right title. |
| 218 | func TestSmartDoorConfirmStep(t *testing.T) { |
| 219 | for name, f := range map[string]*homeRouterFixture{"home": newHomeRouterFixture(t), "standalone": newStandaloneDoor(t)} { |
| 220 | t.Run(name, func(t *testing.T) { |
| 221 | if w := f.do(http.MethodPost, "/signin", "address=alice%40example.test"); w.Code != http.StatusSeeOther { |
| 222 | t.Fatalf("POST /signin: %d", w.Code) |
| 223 | } |
| 224 | f.app.signinMail.wait() |
| 225 | link := magicLinkPattern.FindString(f.app.mailRecorder.last()) |
| 226 | target, err := linkPath(link) |
| 227 | if err != nil || link == "" { |
| 228 | t.Fatal("no link mailed") |
| 229 | } |
| 230 | open := f.get(target) |
| 231 | if _, _, ok := ConfirmFormForTest(open.Body.String()); open.Code != http.StatusOK || !ok { |
| 232 | t.Fatalf("confirm: %d\n%s", open.Code, open.Body.String()) |
| 233 | } |
| 234 | card := door(t, open.Body.String()) |
| 235 | if !strings.Contains(card, ">Sign in</button>") || strings.Contains(card, emailField) || strings.Contains(card, "Oficina ID") { |
| 236 | t.Errorf("the confirm door:\n%s", card) |
| 237 | } |
| 238 | if got := titleOf(t, open.Body.String()); got != "Sign in to Calendar" { |
| 239 | t.Errorf("confirm <title> = %q", got) |
| 240 | } |
| 241 | }) |
| 242 | } |
| 243 | } |
| 244 | |