package fichastest // The smart door's routes and states (Home spec 2026-11-19-smart-door.md, // step 6). Fichas has no Home: email is the one way in. import ( "net/http" "net/url" "strings" "testing" ) // Every route the door answers, and the two a Home-derived app mounts // that Fichas does not. func TestTheDoorsRoutes(t *testing.T) { h, _, _ := wiredApp(t) for path, want := range map[string]int{ "/signin": http.StatusOK, "/signed-out": http.StatusOK, "/signin/local": http.StatusNotFound, "/signin/oficina": http.StatusNotFound, } { rec := get(t, h, path) if rec.Code != want { t.Errorf("GET %s: %d, want %d", path, rec.Code, want) } if want == http.StatusOK && rec.Header().Get("Cache-Control") != "no-store" { t.Errorf("GET %s: Cache-Control %q, want no-store", path, rec.Header().Get("Cache-Control")) } } } // Each state of the door, signed out, with its tab title, which is what // its heading says. func TestEveryDoorStateHasItsTitle(t *testing.T) { h, _, _ := wiredApp(t) for path, title := range map[string]string{ "/signin": "Sign in to Fichas", "/signin?err=rate": "Problem signing in to Fichas", "/signin?err=address": "Problem signing in to Fichas", "/signin?err=expired": "Problem signing in to Fichas", "/signin?err=keymail&force=1": "Problem signing in to Fichas", "/signin?err=1": "Problem signing in to Fichas", "/signin?continue=nothing": "Problem signing in to Fichas", "/signin?reauth=1": "Sign in to Fichas", "/signin?force=1": "Sign in to Fichas", "/signed-out": "Signed out · Fichas", } { rec := get(t, h, path) if rec.Code != http.StatusOK { t.Errorf("GET %s: %d", path, rec.Code) continue } body := rec.Body.String() if got := titleOf(body); got != title { t.Errorf("GET %s: title %q, want %q", path, got, title) } if !strings.Contains(body, "estilo-door") || !strings.Contains(body, "Fichas") { t.Errorf("GET %s is not the door", path) } if strings.Contains(body, "Oficina ID") { t.Errorf("GET %s offers Oficina ID", path) } } // A failed Keymail round trip keeps the plain-email choice it offered. if body := get(t, h, "/signin?err=keymail&force=1").Body.String(); !strings.Contains(body, ``) { t.Error("after a Keymail failure the door does not keep force=1") } // A step-up keeps reauth on the post. if body := get(t, h, "/signin?reauth=1").Body.String(); !strings.Contains(body, ``) { t.Error("a step-up's door does not post reauth=1") } } // Signed in and admitted, a bare /signin is no door: it goes back to the // page asked for, or the list, and never to a page that is no page. func TestASignedInOwnerIsSentOn(t *testing.T) { h, m, _ := wiredApp(t) b := newBrowser() signIn(t, h, b, m, TestOwner, nil) for target, want := range map[string]string{ "/signin": "/", "/signin?return_to=%2Fcards%2Fnew": "/cards/new", "/signin?return_to=%2Fsigned-out": "/", "/signin?return_to=%2F%2Fevil.example%2F": "/", "/signin?return_to=https%3A%2F%2Fevil.example%2F": "/", "/signin?return_to=%2Fa&return_to=%2Fb": "/", } { rec := b.get(t, h, target) if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != want { t.Errorf("GET %s signed in: %d to %q, want 303 to %q", target, rec.Code, rec.Header().Get("Location"), want) } } // A state of the door is drawn for a signed-in browser too, and every // door is drawn signed out: no account menu, no address. for _, target := range []string{"/signin?sent=1", "/signin?err=expired", "/signin?reauth=1", "/signed-out"} { rec := b.get(t, h, target) body := rec.Body.String() if rec.Code != http.StatusOK { t.Errorf("GET %s signed in: %d", target, rec.Code) } for _, account := range []string{"estilo-account", "Signed in as", "Sign out"} { if strings.Contains(body, account) { t.Errorf("GET %s draws the account (%q): every door is drawn signed out", target, account) } } } if !b.has(sessionCookie) { t.Error("an admitted session was ended by the door") } } // The email way is remembered: back on the door after signing out, the // one way in is "Continue as {address}", with "Use a different email" // under it; that forgets it, and the field comes back. func TestTheEmailWayIsRemembered(t *testing.T) { h, m, _ := wiredApp(t) b := newBrowser() signIn(t, h, b, m, TestOwner, nil) if !b.has(rememberName) { t.Fatal("signing in remembered nothing") } out := b.post(t, h, "/signout", url.Values{}) if out.Code != http.StatusSeeOther || out.Header().Get("Location") != "/signed-out" { t.Fatalf("Sign out: %d to %q, want 303 to /signed-out", out.Code, out.Header().Get("Location")) } if b.has(sessionCookie) { t.Fatal("Sign out kept the session cookie") } door := b.get(t, h, "/signin").Body.String() for _, want := range []string{TestOwner, "Use a different email", `action="/signin/forget"`} { if !strings.Contains(door, want) { t.Errorf("the returning door lacks %q:\n%s", want, door) } } if strings.Contains(door, "Other ways to sign in") { t.Error("an email-only door offers Other ways") } forget := b.post(t, h, "/signin/forget", url.Values{"way": {"email"}}) if forget.Code != http.StatusSeeOther { t.Fatalf("Forget: %d", forget.Code) } if b.has(rememberName) { t.Error("Forget kept the remembered way") } if door := b.get(t, h, "/signin").Body.String(); strings.Contains(door, TestOwner) || !strings.Contains(door, `name="address" required`) { t.Errorf("after Forget the door is not the empty field:\n%s", door) } } // Where Sign out lands: "You’re signed out", Sign in, and the quiet // "Forget me on this device", a POST to auth's Forget. Fichas has no Home, // so nothing chains on to Oficina ID. func TestSignedOutOffersForget(t *testing.T) { h, m, _ := wiredApp(t) b := newBrowser() signIn(t, h, b, m, TestOwner, nil) b.post(t, h, "/signout", url.Values{}) page := b.get(t, h, "/signed-out").Body.String() for _, want := range []string{ "

You’re signed out

", "You’ve signed out of Fichas on this device.", `Sign in`, `
`, ">Forget me on this device", } { if !strings.Contains(page, want) { t.Errorf("the signed-out page lacks %q:\n%s", want, page) } } for _, gone := range []string{"chain", "oficina", "id.oficina.works"} { if strings.Contains(page, gone) { t.Errorf("the signed-out page chains on (%q), with no Home to chain to", gone) } } rec := b.post(t, h, "/signin/forget", url.Values{}) if rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), "/signin") { t.Errorf("Forget: %d to %q, want the door", rec.Code, rec.Header().Get("Location")) } if b.has(rememberName) { t.Error("Forget me on this device kept the remembered way") } // Same-origin only, as Begin is: a cross-site form forgets nothing. cross := newBrowser() signIn(t, h, cross, m, TestOwner, nil) if r := crossSitePost(t, h, cross, "/signin/forget"); r.Code != http.StatusForbidden { t.Errorf("a cross-site Forget answered %d, want 403", r.Code) } if !cross.has(rememberName) { t.Error("a cross-site Forget forgot") } } // The confirm step an emailed link opens is the door, with one Sign in // button that POSTs the token back; its GET spends nothing and sets // nothing, and it is kept out of caches and referrers. func TestTheConfirmStepIsTheDoor(t *testing.T) { h, m, _ := wiredApp(t) b := newBrowser() tok := linkFor(t, h, b, m, TestOwner, nil) rec := get(t, h, "/auth/verify?token="+url.QueryEscape(tok)) if rec.Code != http.StatusOK { t.Fatalf("GET /auth/verify: %d", rec.Code) } if len(rec.Result().Cookies()) != 0 { t.Error("the confirm step's GET set a cookie") } if rec.Header().Get("Cache-Control") != "no-store" || rec.Header().Get("Referrer-Policy") == "" { t.Errorf("confirm headers: Cache-Control %q, Referrer-Policy %q", rec.Header().Get("Cache-Control"), rec.Header().Get("Referrer-Policy")) } body := rec.Body.String() if got := titleOf(body); got != "Sign in to Fichas" { t.Errorf("confirm title %q", got) } // On the one-way door, so its Sign in is the door's pill like every // other state's main action. for _, want := range []string{"
", `
`, ``, `name="token"`, ">Sign in"} { if !strings.Contains(body, want) { t.Errorf("the confirm step lacks %q:\n%s", want, body) } } if strings.Contains(body, TestOwner) { t.Error("the confirm step names the address: the link may be open in another browser") } } // Begin's own problems reach the door as they are, for any address: a // mistyped address is "That doesn't look like an email address", never a // cheerful Sent (no sameAnswer), with nothing mailed and no dial, and it // keeps force and reauth. func TestAMistypedAddressIsSaidSo(t *testing.T) { h, m, _ := wiredApp(t) for _, extra := range []url.Values{{}, {"force": {"1"}}, {"reauth": {"1"}}} { form := url.Values{"address": {"not an address"}} for k, v := range extra { form[k] = v } rec := post(t, h, "/auth/begin", form) loc := rec.Header().Get("Location") if rec.Code != http.StatusSeeOther || !strings.HasPrefix(loc, "/signin?err=address") { t.Errorf("%v: %d to %q, want the door with err=address", extra, rec.Code, loc) } for k := range extra { if !strings.Contains(loc, k+"=1") { t.Errorf("%v: %q dropped %s", extra, loc, k) } } if body := get(t, h, loc).Body.String(); !strings.Contains(body, "That doesn't look like an email address.") { t.Errorf("%q does not say so", loc) } } if len(m.sent) != 0 { t.Errorf("%d mails sent for mistyped addresses", len(m.sent)) } }