package calls // Every route and state of the smart door (Home spec 2026-11-19-smart-door.md, // "Routes, and every state the door must answer"), with Home and without. import ( "net/http" "net/url" "strings" "testing" "time" "amadan.net/oficina/home/connect" "amadan.net/rastrillo/rastrillo/lastsignin" ) func TestDoorStatesWithHome(t *testing.T) { f := newHomeRouterFixture(t) oficina := &lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"} email := &lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"} for _, c := range []struct { name, path string record *lastsignin.Record title string want, never []string }{ {"first visit", "/signin", nil, "Sign in to Calls", []string{oficinaButton, otherWays, `href="/signin/oficina"`}, []string{emailField}}, {"Oficina ID remembered", "/signin", oficina, "Sign in to Calls", []string{oficinaButton, otherWays}, []string{"Continue as", emailField}}, {"email remembered", "/signin", email, "Sign in to Calls", []string{"Continue as alice@example.test", otherWays}, []string{oficinaButton}}, {"the chooser", "/signin?ways=1", email, "Other ways to sign in to Calls", []string{"Oficina ID", "Back", "Use a different email"}, []string{emailField}}, {"the email way", "/signin?way=email", nil, "Sign in to Calls", []string{emailField, "Continue"}, []string{oficinaButton}}, {"the Oficina way", "/signin?way=oficina", email, "Sign in to Calls", []string{oficinaButton}, []string{"Continue as"}}, {"page carried", "/signin?return_to=%2Fsettings", nil, "Sign in to Calls", []string{`href="/signin/oficina?return_to=%2Fsettings"`}, nil}, {"email only", "/signin/local", oficina, "Sign in to Calls", []string{"Continue as alice@example.test"}, []string{oficinaButton}}, {"email only, nothing remembered", "/signin/local", nil, "Sign in to Calls", []string{emailField}, []string{oficinaButton, otherWays}}, {"a step-up", "/signin?reauth=1", oficina, "Sign in to Calls", []string{"Continue as alice@example.test", `name="reauth" value="1"`}, []string{oficinaButton}}, {"a problem", "/signin?err=expired", email, "Problem signing in to Calls", []string{"That link has expired or was already used.", emailField}, []string{oficinaButton}}, {"rate", "/signin?err=rate", nil, "Problem signing in to Calls", []string{"Too many tries."}, nil}, {"a bad address", "/signin?err=address", nil, "Problem signing in to Calls", []string{"That doesn't look like an email address."}, nil}, // rastrillo's own words since v0.45.0, which never name Keymail. {"Keymail failed", "/signin?force=1&err=keymail", nil, "Problem signing in to Calls", []string{`name="force" value="1"`, "We couldn't confirm it's you."}, []string{"Keymail"}}, {"sent", "/signin?sent=1", email, "Check your email to sign in to Calls", []string{"Check your email", "Use a different email"}, []string{oficinaButton, emailField}}, } { t.Run(c.name, func(t *testing.T) { br := f.fresh() if c.record != nil { br.remember(t, *c.record) } rec := br.get(c.path) if rec.Code != http.StatusOK || rec.Header().Get("Cache-Control") != "no-store" { t.Fatalf("GET %s: %d %q", c.path, rec.Code, rec.Header().Get("Cache-Control")) } if got := titleOf(t, rec.Body.String()); got != c.title { t.Errorf("title %q, want %q", got, c.title) } card := door(t, rec.Body.String()) for _, want := range c.want { if !strings.Contains(card, want) { t.Errorf("the door lacks %q:\n%s", want, card) } } for _, never := range c.never { if strings.Contains(card, never) { t.Errorf("the door has %q:\n%s", never, card) } } // The card is the door's alone: no Colophon, no account. if strings.Contains(rec.Body.String(), "/help/open-source") || strings.Contains(rec.Body.String(), "estilo-account") { t.Error("the door draws more than the door") } }) } // Continue with Oficina ID starts the exchange, with the page. br := f.fresh() redirectsTo(t, br.get("/signin/oficina"), "/auth/start?return_to=%2F") redirectsTo(t, br.get("/signin/oficina?return_to=%2Fsettings"), "/auth/start?return_to=%2Fsettings") // A step-up never reaches Home. redirectsTo(t, br.get("/signin/oficina?reauth=1&return_to=%2Faccount%2Fhome"), "/signin/local?reauth=1&return_to=%2Faccount%2Fhome") // The member choice is mounted (it answers 404 outside team mode, as // connect does without the team hooks, never 405). if rec := br.get(connect.ChoosePath); rec.Code != http.StatusNotFound && rec.Code != http.StatusForbidden && rec.Code != http.StatusBadRequest { t.Errorf("GET %s outside team mode = %d", connect.ChoosePath, rec.Code) } } func TestDoorStatesStandalone(t *testing.T) { f := newStandaloneDoor(t) email := lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"} for _, path := range []string{"/signin", "/signin?ways=1", "/signin?way=oficina", "/signin?err=expired", "/signin?reauth=1"} { br := f.fresh() br.remember(t, email) rec := br.get(path) if rec.Code != http.StatusOK { t.Fatalf("GET %s: %d", path, rec.Code) } if body := rec.Body.String(); strings.Contains(body, "Oficina ID") || strings.Contains(body, "/signin/oficina") { t.Errorf("standalone %s offers Oficina ID:\n%s", path, door(t, body)) } } br := f.fresh() if got := titleOf(t, br.get("/signin").Body.String()); got != "Sign in to Calls" { t.Errorf("title %q", got) } if card := door(t, br.get("/signin").Body.String()); !strings.Contains(card, emailField) { t.Errorf("standalone first visit is not the email field:\n%s", card) } // With one way in there is no chooser: "Use a different email" stands in // for Other ways. br.remember(t, email) if card := door(t, br.get("/signin").Body.String()); !strings.Contains(card, "Continue as alice@example.test") || !strings.Contains(card, "Use a different email") || strings.Contains(card, otherWays) { t.Errorf("standalone remembered door:\n%s", card) } for _, path := range []string{"/signin/local", "/signin/oficina", connect.ChoosePath} { if rec := f.fresh().get(path); rec.Code != http.StatusNotFound { t.Errorf("standalone GET %s = %d, want 404", path, rec.Code) } } } // Somebody signed in and admitted never sees the door unless the query // names one of its states, and goes to a safe return_to or "/". func TestASignedInMemberSkipsTheDoor(t *testing.T) { doorModes(t, func(t *testing.T, f *homeRouterFixture) { f.session(t, "alice@example.test", "magiclink", time.Now()) redirectsTo(t, f.get("/signin"), "/") redirectsTo(t, f.get("/signin?return_to=%2Fsettings"), "/settings") redirectsTo(t, f.get("/signin?return_to=%2F%2Fevil.example"), "/") for _, state := range []string{"/signin?sent=1", "/signin?err=expired", "/signin?reauth=1"} { if rec := f.get(state); rec.Code != http.StatusOK { t.Errorf("signed in, GET %s = %d, want the door's state", state, rec.Code) } } if f.sessionCookie() == nil { t.Fatal("the door ended an admitted session") } }) } // A whole email sign-in through the door, both modes, lands on the list. func TestAnEmailSigninThroughTheDoor(t *testing.T) { doorModes(t, func(t *testing.T, f *homeRouterFixture) { if got := f.emailSignin(t, url.Values{"address": {"alice@example.test"}}); got != "/" { t.Fatalf("landed on %q", got) } if rec := f.get("/"); rec.Code != http.StatusOK { t.Fatalf("the list = %d", rec.Code) } if rec, res := f.remembered(); res != lastsignin.Valid || rec.Method != lastsignin.MethodMagicLink || rec.Address != "alice@example.test" { t.Fatalf("remembered %+v (%v), want the emailed link for alice", rec, res) } }) }