| 1 | package memoriatest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "net/url" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // B5 (polish audit, 2026-09-23): the sign-in page said nothing after any |
| 13 | // sign-in attempt. It switched on ?outcome=, a word nothing writes. Since |
| 14 | // the smart door (2026-11-22) the words are rastrillo's own, read by its |
| 15 | // screen from the same query rastrillo writes, so the switch that drifted |
| 16 | // is gone; what is left to pin is that every outcome lands on a door that |
| 17 | // says it, in rastrillo's approved copy. |
| 18 | // |
| 19 | // Every case here drives the REAL flow and follows the Location it was |
| 20 | // given, with the browser's cookies: never a hand-built query. A |
| 21 | // hand-built query is written from the same model as the page that reads |
| 22 | // it. Each case also asserts the Location's problem, so if rastrillo |
| 23 | // renames a code the failure names the new spelling rather than a missing |
| 24 | // sentence. |
| 25 | |
| 26 | // followToSignin performs the redirect a browser would, with c's cookies, |
| 27 | // and returns the page body. It refuses anything that is not a redirect to |
| 28 | // the door with the problem wantErr ("" for none), so a changed status |
| 29 | // cannot pass as "the page rendered without the problem". |
| 30 | func followToSignin(t *testing.T, c *client, rr *httptest.ResponseRecorder, wantErr string) string { |
| 31 | t.Helper() |
| 32 | if rr.Code != http.StatusSeeOther && rr.Code != http.StatusFound { |
| 33 | t.Fatalf("status = %d, want a redirect to the sign-in page; body %q", rr.Code, rr.Body.String()) |
| 34 | } |
| 35 | loc := rr.Header().Get("Location") |
| 36 | u, err := url.Parse(loc) |
| 37 | if err != nil || u.Path != "/signin" { |
| 38 | t.Fatalf("Location = %q, want /signin?…", loc) |
| 39 | } |
| 40 | if got := u.Query().Get("err"); got != wantErr { |
| 41 | t.Fatalf("Location %q has err=%q, want %q: rastrillo's vocabulary moved", loc, got, wantErr) |
| 42 | } |
| 43 | page := c.get(testOrigin + loc) |
| 44 | if page.Code != http.StatusOK { |
| 45 | t.Fatalf("GET %s = %d", loc, page.Code) |
| 46 | } |
| 47 | return page.Body.String() |
| 48 | } |
| 49 | |
| 50 | // assertProblem fails unless body is the door with a negative callout |
| 51 | // saying sentence. |
| 52 | func assertProblem(t *testing.T, body, sentence string) { |
| 53 | t.Helper() |
| 54 | if !strings.Contains(body, `rst-tone="negative"`) { |
| 55 | t.Errorf("no negative-toned callout on the page the redirect landed on") |
| 56 | } |
| 57 | if !strings.Contains(body, sentence) { |
| 58 | t.Errorf("page does not say %q", sentence) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // attemptID matches the attempt id wherever a Sent page carries one. |
| 63 | var attemptID = regexp.MustCompile(`attempt=[A-Za-z0-9_-]+`) |
| 64 | |
| 65 | func TestSigninPageSaysWhatHappened(t *testing.T) { |
| 66 | t.Run("a sent link says check your email", func(t *testing.T) { |
| 67 | // Both writers of Sent: rastrillo's Begin after mailing an owner's |
| 68 | // link, and beginGuard answering a stranger with rastrillo's |
| 69 | // AnswerAsSent, the same answer without the mail, by design. |
| 70 | var bodies []string |
| 71 | for _, address := range []string{dialTestOwner, "stranger@attacker.example"} { |
| 72 | h, _ := signinApp(t) |
| 73 | c := newClient(t, h) |
| 74 | rr := c.do(http.MethodPost, testOrigin+"/auth/begin", url.Values{"address": {address}}) |
| 75 | if !sentLocation(rr.Header().Get("Location")) { |
| 76 | t.Fatalf("%s: Location %q, want Sent", address, rr.Header().Get("Location")) |
| 77 | } |
| 78 | body := followToSignin(t, c, rr, "") |
| 79 | for _, want := range []string{ |
| 80 | `<h1 id="rst-signin-heading">Check your email</h1>`, |
| 81 | "We sent a link to <bdi>" + address + "</bdi>.", |
| 82 | "The link works once and expires soon.", |
| 83 | "<title>Check your email to sign in to Memoria</title>", |
| 84 | "Use a different email", |
| 85 | } { |
| 86 | if !strings.Contains(body, want) { |
| 87 | t.Errorf("%s: the sent page lacks %q:\n%s", address, want, body) |
| 88 | } |
| 89 | } |
| 90 | if strings.Contains(body, `action="/auth/begin"`) { |
| 91 | t.Error("the sign-in form is still on the page after a link was sent") |
| 92 | } |
| 93 | bodies = append(bodies, attemptID.ReplaceAllString(strings.ReplaceAll(body, address, "ADDRESS"), "attempt=…")) |
| 94 | } |
| 95 | // Anti-oracle: an owner and a stranger land on the same page, byte |
| 96 | // for byte but the address each typed and the attempt's own id. |
| 97 | if bodies[0] != bodies[1] { |
| 98 | t.Error("the sent page differs between an owner and a stranger; the page is an oracle for who the owners are") |
| 99 | } |
| 100 | }) |
| 101 | |
| 102 | t.Run("an address rastrillo cannot parse says so", func(t *testing.T) { |
| 103 | // Admitted by the owners list, refused by signin's parser: a |
| 104 | // dotless domain. Only an owner reaches rastrillo at all. |
| 105 | const owner = "paul@localhost" |
| 106 | h, _ := signinAppWithOwners(t, []string{owner}) |
| 107 | c := newClient(t, h) |
| 108 | body := followToSignin(t, c, c.do(http.MethodPost, testOrigin+"/auth/begin", url.Values{"address": {owner}}), "address") |
| 109 | // In the field's own error slot, not a banner (spec §9.2). |
| 110 | if !strings.Contains(body, "That doesn't look like an email address.") || strings.Contains(body, `rst-tone="negative"`) { |
| 111 | t.Errorf("the address error is not in the field's slot:\n%s", body) |
| 112 | } |
| 113 | }) |
| 114 | |
| 115 | t.Run("a rate-limited address says wait", func(t *testing.T) { |
| 116 | // signin's per-address budget, spent on an owner whose links |
| 117 | // are being sent (the stub mailer takes them). |
| 118 | h, _ := signinApp(t) |
| 119 | c := newClient(t, h) |
| 120 | var rr *httptest.ResponseRecorder |
| 121 | for i := 0; i < 20; i++ { |
| 122 | rr = c.do(http.MethodPost, testOrigin+"/auth/begin", url.Values{"address": {dialTestOwner}}) |
| 123 | if strings.Contains(rr.Header().Get("Location"), "err=rate") { |
| 124 | break |
| 125 | } |
| 126 | } |
| 127 | assertProblem(t, followToSignin(t, c, rr, "rate"), "Too many tries. Try again in a few minutes.") |
| 128 | }) |
| 129 | |
| 130 | t.Run("a dead magic link says it expired", func(t *testing.T) { |
| 131 | // The GET is the confirm door and spends nothing, so a dead |
| 132 | // token is only found dead at the POST its button makes. |
| 133 | h, _ := signinApp(t) |
| 134 | c := newClient(t, h) |
| 135 | assertProblem(t, followToSignin(t, c, c.openLink("/auth/verify?token=not-a-real-token"), "expired"), |
| 136 | "That link has expired or was already used. Send a new one.") |
| 137 | }) |
| 138 | |
| 139 | t.Run("a keymail return with no pending cookie says it expired", func(t *testing.T) { |
| 140 | h, _ := signinApp(t) |
| 141 | c := newClient(t, h) |
| 142 | assertProblem(t, followToSignin(t, c, c.get(testOrigin+"/auth/callback?code=x&state=y"), "expired"), |
| 143 | "That link has expired or was already used. Send a new one.") |
| 144 | }) |
| 145 | |
| 146 | t.Run("a plain visit says nothing", func(t *testing.T) { |
| 147 | h, _ := signinApp(t) |
| 148 | rr := httptest.NewRecorder() |
| 149 | h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/signin", nil)) |
| 150 | if strings.Contains(rr.Body.String(), "rst-callout") { |
| 151 | t.Error("a sign-in page nobody submitted carries a callout") |
| 152 | } |
| 153 | }) |
| 154 | } |
| 155 | |