| 1 | package calls |
| 2 | |
| 3 | // Every route and state of the smart door (Home spec 2026-11-19-smart-door.md, |
| 4 | // "Routes, and every state the door must answer"), with Home and without. |
| 5 | |
| 6 | import ( |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/home/connect" |
| 14 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 15 | ) |
| 16 | |
| 17 | func TestDoorStatesWithHome(t *testing.T) { |
| 18 | f := newHomeRouterFixture(t) |
| 19 | oficina := &lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"} |
| 20 | email := &lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"} |
| 21 | for _, c := range []struct { |
| 22 | name, path string |
| 23 | record *lastsignin.Record |
| 24 | title string |
| 25 | want, never []string |
| 26 | }{ |
| 27 | {"first visit", "/signin", nil, "Sign in to Calls", []string{oficinaButton, otherWays, `href="/signin/oficina"`}, []string{emailField}}, |
| 28 | {"Oficina ID remembered", "/signin", oficina, "Sign in to Calls", []string{oficinaButton, otherWays}, []string{"Continue as", emailField}}, |
| 29 | {"email remembered", "/signin", email, "Sign in to Calls", []string{"Continue as alice@example.test", otherWays}, []string{oficinaButton}}, |
| 30 | {"the chooser", "/signin?ways=1", email, "Other ways to sign in to Calls", []string{"Oficina ID", "Back", "Use a different email"}, []string{emailField}}, |
| 31 | {"the email way", "/signin?way=email", nil, "Sign in to Calls", []string{emailField, "Continue"}, []string{oficinaButton}}, |
| 32 | {"the Oficina way", "/signin?way=oficina", email, "Sign in to Calls", []string{oficinaButton}, []string{"Continue as"}}, |
| 33 | {"page carried", "/signin?return_to=%2Fsettings", nil, "Sign in to Calls", []string{`href="/signin/oficina?return_to=%2Fsettings"`}, nil}, |
| 34 | {"email only", "/signin/local", oficina, "Sign in to Calls", []string{"Continue as alice@example.test"}, []string{oficinaButton}}, |
| 35 | {"email only, nothing remembered", "/signin/local", nil, "Sign in to Calls", []string{emailField}, []string{oficinaButton, otherWays}}, |
| 36 | {"a step-up", "/signin?reauth=1", oficina, "Sign in to Calls", []string{"Continue as alice@example.test", `name="reauth" value="1"`}, []string{oficinaButton}}, |
| 37 | {"a problem", "/signin?err=expired", email, "Problem signing in to Calls", []string{"That link has expired or was already used.", emailField}, []string{oficinaButton}}, |
| 38 | {"rate", "/signin?err=rate", nil, "Problem signing in to Calls", []string{"Too many tries."}, nil}, |
| 39 | {"a bad address", "/signin?err=address", nil, "Problem signing in to Calls", []string{"That doesn't look like an email address."}, nil}, |
| 40 | // rastrillo's own words since v0.45.0, which never name Keymail. |
| 41 | {"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"}}, |
| 42 | {"sent", "/signin?sent=1", email, "Check your email to sign in to Calls", []string{"Check your email", "Use a different email"}, []string{oficinaButton, emailField}}, |
| 43 | } { |
| 44 | t.Run(c.name, func(t *testing.T) { |
| 45 | br := f.fresh() |
| 46 | if c.record != nil { |
| 47 | br.remember(t, *c.record) |
| 48 | } |
| 49 | rec := br.get(c.path) |
| 50 | if rec.Code != http.StatusOK || rec.Header().Get("Cache-Control") != "no-store" { |
| 51 | t.Fatalf("GET %s: %d %q", c.path, rec.Code, rec.Header().Get("Cache-Control")) |
| 52 | } |
| 53 | if got := titleOf(t, rec.Body.String()); got != c.title { |
| 54 | t.Errorf("title %q, want %q", got, c.title) |
| 55 | } |
| 56 | card := door(t, rec.Body.String()) |
| 57 | for _, want := range c.want { |
| 58 | if !strings.Contains(card, want) { |
| 59 | t.Errorf("the door lacks %q:\n%s", want, card) |
| 60 | } |
| 61 | } |
| 62 | for _, never := range c.never { |
| 63 | if strings.Contains(card, never) { |
| 64 | t.Errorf("the door has %q:\n%s", never, card) |
| 65 | } |
| 66 | } |
| 67 | // The card is the door's alone: no Colophon, no account. |
| 68 | if strings.Contains(rec.Body.String(), "/help/open-source") || strings.Contains(rec.Body.String(), "estilo-account") { |
| 69 | t.Error("the door draws more than the door") |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | // Continue with Oficina ID starts the exchange, with the page. |
| 74 | br := f.fresh() |
| 75 | redirectsTo(t, br.get("/signin/oficina"), "/auth/start?return_to=%2F") |
| 76 | redirectsTo(t, br.get("/signin/oficina?return_to=%2Fsettings"), "/auth/start?return_to=%2Fsettings") |
| 77 | // A step-up never reaches Home. |
| 78 | redirectsTo(t, br.get("/signin/oficina?reauth=1&return_to=%2Faccount%2Fhome"), "/signin/local?reauth=1&return_to=%2Faccount%2Fhome") |
| 79 | // The member choice is mounted (it answers 404 outside team mode, as |
| 80 | // connect does without the team hooks, never 405). |
| 81 | if rec := br.get(connect.ChoosePath); rec.Code != http.StatusNotFound && rec.Code != http.StatusForbidden && rec.Code != http.StatusBadRequest { |
| 82 | t.Errorf("GET %s outside team mode = %d", connect.ChoosePath, rec.Code) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func TestDoorStatesStandalone(t *testing.T) { |
| 87 | f := newStandaloneDoor(t) |
| 88 | email := lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"} |
| 89 | for _, path := range []string{"/signin", "/signin?ways=1", "/signin?way=oficina", "/signin?err=expired", "/signin?reauth=1"} { |
| 90 | br := f.fresh() |
| 91 | br.remember(t, email) |
| 92 | rec := br.get(path) |
| 93 | if rec.Code != http.StatusOK { |
| 94 | t.Fatalf("GET %s: %d", path, rec.Code) |
| 95 | } |
| 96 | if body := rec.Body.String(); strings.Contains(body, "Oficina ID") || strings.Contains(body, "/signin/oficina") { |
| 97 | t.Errorf("standalone %s offers Oficina ID:\n%s", path, door(t, body)) |
| 98 | } |
| 99 | } |
| 100 | br := f.fresh() |
| 101 | if got := titleOf(t, br.get("/signin").Body.String()); got != "Sign in to Calls" { |
| 102 | t.Errorf("title %q", got) |
| 103 | } |
| 104 | if card := door(t, br.get("/signin").Body.String()); !strings.Contains(card, emailField) { |
| 105 | t.Errorf("standalone first visit is not the email field:\n%s", card) |
| 106 | } |
| 107 | // With one way in there is no chooser: "Use a different email" stands in |
| 108 | // for Other ways. |
| 109 | br.remember(t, email) |
| 110 | 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) { |
| 111 | t.Errorf("standalone remembered door:\n%s", card) |
| 112 | } |
| 113 | for _, path := range []string{"/signin/local", "/signin/oficina", connect.ChoosePath} { |
| 114 | if rec := f.fresh().get(path); rec.Code != http.StatusNotFound { |
| 115 | t.Errorf("standalone GET %s = %d, want 404", path, rec.Code) |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Somebody signed in and admitted never sees the door unless the query |
| 121 | // names one of its states, and goes to a safe return_to or "/". |
| 122 | func TestASignedInMemberSkipsTheDoor(t *testing.T) { |
| 123 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 124 | f.session(t, "alice@example.test", "magiclink", time.Now()) |
| 125 | redirectsTo(t, f.get("/signin"), "/") |
| 126 | redirectsTo(t, f.get("/signin?return_to=%2Fsettings"), "/settings") |
| 127 | redirectsTo(t, f.get("/signin?return_to=%2F%2Fevil.example"), "/") |
| 128 | for _, state := range []string{"/signin?sent=1", "/signin?err=expired", "/signin?reauth=1"} { |
| 129 | if rec := f.get(state); rec.Code != http.StatusOK { |
| 130 | t.Errorf("signed in, GET %s = %d, want the door's state", state, rec.Code) |
| 131 | } |
| 132 | } |
| 133 | if f.sessionCookie() == nil { |
| 134 | t.Fatal("the door ended an admitted session") |
| 135 | } |
| 136 | }) |
| 137 | } |
| 138 | |
| 139 | // A whole email sign-in through the door, both modes, lands on the list. |
| 140 | func TestAnEmailSigninThroughTheDoor(t *testing.T) { |
| 141 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 142 | if got := f.emailSignin(t, url.Values{"address": {"alice@example.test"}}); got != "/" { |
| 143 | t.Fatalf("landed on %q", got) |
| 144 | } |
| 145 | if rec := f.get("/"); rec.Code != http.StatusOK { |
| 146 | t.Fatalf("the list = %d", rec.Code) |
| 147 | } |
| 148 | if rec, res := f.remembered(); res != lastsignin.Valid || rec.Method != lastsignin.MethodMagicLink || rec.Address != "alice@example.test" { |
| 149 | t.Fatalf("remembered %+v (%v), want the emailed link for alice", rec, res) |
| 150 | } |
| 151 | }) |
| 152 | } |
| 153 | |