| 1 | package sheets |
| 2 | |
| 3 | // The refusal: a verified address Admits refuses, and a session the |
| 4 | // signed-in routes no longer admit. |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "regexp" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "amadan.net/rastrillo/rastrillo/auth" |
| 15 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 16 | ) |
| 17 | |
| 18 | // The refusal (auth's Refused) is drawn in the screen's app-problem slot, |
| 19 | // with the email field, never the remembered one-tap that would only offer |
| 20 | // the refused address again, in a 403, with Paul's approved words. |
| 21 | func TestSmartDoorRefusalIsTheAppProblem(t *testing.T) { |
| 22 | for _, home := range []bool{true, false} { |
| 23 | t.Run(fmt.Sprintf("home=%v", home), func(t *testing.T) { |
| 24 | var app *App |
| 25 | if home { |
| 26 | app = newHomeFixture(t).app |
| 27 | } else { |
| 28 | app, _ = appWithOrigin(t, testOrigin) |
| 29 | } |
| 30 | w := httptest.NewRecorder() |
| 31 | app.auth.RememberJar().Remember(w, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "carol@example.com"}) |
| 32 | req := httptest.NewRequest(http.MethodPost, testOrigin+"/auth/verify", nil) |
| 33 | req.AddCookie(w.Result().Cookies()[0]) |
| 34 | rec := httptest.NewRecorder() |
| 35 | app.refused(rec, req, auth.Identity{}) |
| 36 | card := door(t, rec.Body.String()) |
| 37 | if rec.Code != http.StatusForbidden || !strings.Contains(card, appProblem) || !strings.Contains(card, signinRefusal) || |
| 38 | !strings.Contains(card, `id="rst-signin-email" name="address"`) || strings.Contains(card, "Continue as") || strings.Contains(card, oficinaButton) { |
| 39 | t.Fatalf("refusal: %d\n%s", rec.Code, card) |
| 40 | } |
| 41 | }) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Paul approved the refusal's wording on 2026-11-21. |
| 46 | func TestTheRefusalIsPaulsApprovedWording(t *testing.T) { |
| 47 | if want := "That email can’t sign in to Sheets. Use a different email, or ask whoever runs this team to add it."; signinRefusal != want { |
| 48 | t.Fatalf("refusal %q, want %q", signinRefusal, want) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // A session the signed-in routes refuse gets the door, not a bounce: the |
| 53 | // refusal says why, with the email field, a remembered Oficina ID is |
| 54 | // forgotten so it is not offered round the same loop again, and a |
| 55 | // remembered email way is kept. Continue with Oficina ID starts afresh. |
| 56 | func TestARefusedSessionGetsTheDoorWithTheRefusal(t *testing.T) { |
| 57 | f := newHomeFixture(t) |
| 58 | b := f.browser() |
| 59 | b.local(t, "carol@example.com") // signed in, never admitted |
| 60 | if list := b.request(t, "GET", "/sheets", nil, false); list.Code != http.StatusSeeOther || !strings.HasPrefix(list.Header().Get("Location"), "/signin") { |
| 61 | t.Fatalf("the refused session reached the list: %d %q", list.Code, list.Header().Get("Location")) |
| 62 | } |
| 63 | jar := f.app.auth.RememberJar().CookieName() |
| 64 | for _, path := range []string{"/signin", "/signin/local"} { |
| 65 | b.local(t, "carol@example.com") |
| 66 | b.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "carol@example.com"}) |
| 67 | rec := b.request(t, "GET", path, nil, false) |
| 68 | card := door(t, rec.Body.String()) |
| 69 | if rec.Code != http.StatusOK || !strings.Contains(card, appProblem) || !strings.Contains(card, signinRefusal) || !strings.Contains(card, `id="rst-signin-email" name="address"`) || strings.Contains(card, oficinaButton) { |
| 70 | t.Fatalf("GET %s for a refused session: %d\n%s", path, rec.Code, card) |
| 71 | } |
| 72 | if _, kept := b.jar[jar]; kept { |
| 73 | t.Fatalf("GET %s kept the remembered Oficina ID for a refused session", path) |
| 74 | } |
| 75 | } |
| 76 | b.local(t, "carol@example.com") |
| 77 | b.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "carol@example.com"}) |
| 78 | b.request(t, "GET", "/signin", nil, false) |
| 79 | if _, kept := b.jar[jar]; !kept { |
| 80 | t.Fatal("the door forgot a remembered email way for a refused session") |
| 81 | } |
| 82 | b.local(t, "carol@example.com") |
| 83 | if rec := b.request(t, "GET", "/signin/oficina", nil, false); rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), "/auth/start?") { |
| 84 | t.Fatalf("Continue with Oficina ID for a refused session: %d %q, want the exchange", rec.Code, rec.Header().Get("Location")) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Fable on Docs #139, N1: for a refused session, "Other ways to sign in" |
| 89 | // opens the chooser, and each way its own page, with no refusal over them, |
| 90 | // so a refused person can reach another way in (another Oficina ID |
| 91 | // account). |
| 92 | func TestARefusedSessionCanReachOtherWays(t *testing.T) { |
| 93 | f := newHomeFixture(t) |
| 94 | b := f.browser() |
| 95 | get := func(path string) string { |
| 96 | b.local(t, "carol@example.com") |
| 97 | return door(t, b.request(t, "GET", path, nil, false).Body.String()) |
| 98 | } |
| 99 | plain := get("/signin") |
| 100 | if !strings.Contains(plain, appProblem) { |
| 101 | t.Fatalf("the plain door does not say why:\n%s", plain) |
| 102 | } |
| 103 | if way := get("/signin?way=email"); strings.Contains(way, appProblem) { |
| 104 | t.Fatalf("a way's own page redraws the refusal:\n%s", way) |
| 105 | } |
| 106 | m := regexp.MustCompile(`href="(/signin\?[^"]*ways=1[^"]*)"`).FindStringSubmatch(plain) |
| 107 | if m == nil { |
| 108 | t.Fatalf("no Other ways to sign in on the refusal:\n%s", plain) |
| 109 | } |
| 110 | chooser := get(m[1]) |
| 111 | if strings.Contains(chooser, appProblem) || !strings.Contains(chooser, `<h1 id="rst-signin-heading">Other ways to sign in</h1>`) { |
| 112 | t.Fatalf("GET %s for a refused session is not the chooser:\n%s", m[1], chooser) |
| 113 | } |
| 114 | if !strings.Contains(chooser, `href="/signin?way=oficina"`) { |
| 115 | t.Fatalf("the chooser does not offer Oficina ID:\n%s", chooser) |
| 116 | } |
| 117 | if way := get("/signin?way=oficina"); strings.Contains(way, appProblem) || !strings.Contains(way, oficinaButton) { |
| 118 | t.Fatalf("Oficina ID's own page for a refused session:\n%s", way) |
| 119 | } |
| 120 | } |
| 121 | |