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{ "