| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // signInCardRig starts a Claude sign-in by a link for the owner and has |
| 13 | // the runner put its link on the card. It returns the attempt's id. |
| 14 | func signInCardRig(t *testing.T, r *toolRig, runner *FakeRunner, frame string) string { |
| 15 | t.Helper() |
| 16 | attempt, err := r.client.Connect(r.ctx, wire.Connect{Provider: "claude"}) |
| 17 | if err != nil || attempt.Prompt == nil || attempt.Prompt.Kind != wire.PromptAuthorize { |
| 18 | t.Fatalf("sign-in attempt %+v %v", attempt, err) |
| 19 | } |
| 20 | f, _, err := runner.Expect(r.ctx, wire.FrameConnect) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | _ = runner.Send(r.ctx, "a-"+frame, wire.FrameAck, wire.Ack{ID: f.ID}) |
| 25 | if err := runner.Send(r.ctx, "r-"+frame, wire.FrameConnection, wire.ConnectionReport{Connection: attempt.ID, Person: Owner, Provider: "claude", State: wire.ConnConnecting, Prompt: &wire.ConnectPrompt{Kind: wire.PromptAuthorize, URL: signInLink, Attempt: 1}}); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | r.wait("the link on the card", func(c *Conversation) bool { |
| 29 | p := c.Connections[attempt.ID].Prompt |
| 30 | return p != nil && p.URL == signInLink |
| 31 | }) |
| 32 | return attempt.ID |
| 33 | } |
| 34 | |
| 35 | // codeForm is the sign-in card's code form on the Add page. |
| 36 | func codeForm(t *testing.T, page string) string { |
| 37 | t.Helper() |
| 38 | i := strings.Index(page, "data-code-form") |
| 39 | if i < 0 { |
| 40 | t.Fatalf("no code form: %.400s", page) |
| 41 | } |
| 42 | start := strings.LastIndex(page[:i], "<form") |
| 43 | form, _, _ := strings.Cut(page[start:], "</form>") |
| 44 | return form |
| 45 | } |
| 46 | |
| 47 | // connectedAs has the runner finish the attempt as a token connection |
| 48 | // the provider named no account for. |
| 49 | func connectedAs(t *testing.T, r *toolRig, runner *FakeRunner, id, local, frame string) { |
| 50 | t.Helper() |
| 51 | if err := runner.Send(r.ctx, "r-"+frame, wire.FrameConnection, wire.ConnectionReport{Connection: id, Person: Owner, LocalID: local, Provider: "claude", State: wire.ConnHealthy}); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | r.wait("healthy", func(c *Conversation) bool { return c.Connections[id].State == wire.ConnHealthy }) |
| 55 | } |
| 56 | |
| 57 | // TestPagesSignInEmail (Paul, 2026-11-23): a Claude sign-in by a link |
| 58 | // makes a token connection whose account the provider will not name, so |
| 59 | // the sign-in step itself asks for the account email, once and |
| 60 | // optionally, as part of the code's form: no value prefilled, the code |
| 61 | // field still unnamed (never posted). An email typed there is the |
| 62 | // stated email once the connection is made, shown on the row as the |
| 63 | // person's own with the field closed; left empty, the row asks as |
| 64 | // before. A bad shape, a teammate and a cross-site post are refused as |
| 65 | // on the row, and a token paste still connecting takes none. The email |
| 66 | // goes nowhere else. |
| 67 | func TestPagesSignInEmail(t *testing.T) { |
| 68 | r := &toolRig{rig: newRig(t)} |
| 69 | keys := sealKeys(t) |
| 70 | runner := r.dial(0) |
| 71 | hello := held() |
| 72 | hello.Generation, hello.SealKey = runner.Welcome.Generation, keys.PublicString() |
| 73 | if err := runner.Send(r.ctx, "h1", wire.FrameHello, hello); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | r.wait("runner online", func(c *Conversation) bool { return c.Runner.Online }) |
| 77 | owner := r.inst.Cookie() |
| 78 | const add = "/conversation/settings/connections/add" |
| 79 | const page = "/conversation/settings/connections" |
| 80 | |
| 81 | id := signInCardRig(t, r, runner, "one") |
| 82 | form := codeForm(t, r.get(owner, add, 200)) |
| 83 | for _, want := range []string{ |
| 84 | `method="post" action="/conversation/connections/` + id + `/email"`, |
| 85 | `<input type="hidden" name="from" value="add">`, |
| 86 | `<label for="signin-email-` + id + `">Claude account email</label>`, |
| 87 | `<input rst-input id="signin-email-` + id + `" type="email" name="email" maxlength="254" autocapitalize="off" spellcheck="false" data-email-field>`, |
| 88 | "Optional. Shown wherever Equipo names this connection. Equipo does not verify it with Claude.", |
| 89 | `data-email-note role="status" hidden data-bad="That does not look like an email address."`, |
| 90 | "Paste the code Anthropic shows you", |
| 91 | } { |
| 92 | if !strings.Contains(form, want) { |
| 93 | t.Fatalf("the sign-in step lacks %q: %s", want, form) |
| 94 | } |
| 95 | } |
| 96 | if _, code, _ := strings.Cut(form, `id="code-`+id+`"`); strings.Contains(strings.SplitN(code, ">", 2)[0], "name=") { |
| 97 | t.Fatal("the code field has a name: a scriptless post would send it") |
| 98 | } |
| 99 | if strings.Count(form, "data-email-field") != 1 || strings.Contains(form, "value=\"\" data-email-field") || strings.Contains(form, `data-email-field required`) { |
| 100 | t.Fatalf("the email field is prefilled, required or doubled: %s", form) |
| 101 | } |
| 102 | |
| 103 | email := func(c *http.Cookie, id, v string) (int, string) { |
| 104 | return r.post(c, "/conversation/connections/"+id+"/email", url.Values{"email": {v}, "from": {"add"}}) |
| 105 | } |
| 106 | // Refused as on the row: a teammate, a cross-site post, a bad shape. |
| 107 | if status, _ := email(r.memberCookie(), id, "mallory@example.com"); status != http.StatusNotFound { |
| 108 | t.Fatalf("a teammate set the email: %d", status) |
| 109 | } |
| 110 | cross, _ := http.NewRequest("POST", r.inst.URL+"/conversation/connections/"+id+"/email", strings.NewReader(url.Values{"email": {"cross@example.com"}}.Encode())) |
| 111 | cross.AddCookie(owner) |
| 112 | cross.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 113 | cross.Header.Set("Origin", "https://elsewhere.example") |
| 114 | cross.Header.Set("Sec-Fetch-Site", "cross-site") |
| 115 | if res, err := http.DefaultClient.Do(cross); err != nil || res.StatusCode != http.StatusForbidden { |
| 116 | t.Fatalf("a cross-site post: %v %v", res.StatusCode, err) |
| 117 | } else { |
| 118 | res.Body.Close() |
| 119 | } |
| 120 | for _, bad := range []string{"not an email", "me@example", "me@@example.com", "@example.com", "me@example.com\u202e", "me\x07@example.com", strings.Repeat("a", 243) + "@example.com"} { |
| 121 | if status, _ := email(owner, id, bad); status != http.StatusUnprocessableEntity { |
| 122 | t.Fatalf("%q: %d", bad, status) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // The owner's, typed with the code: kept for the attempt, the page |
| 127 | // back on the Add page, the card's field still empty. |
| 128 | if status, loc := email(owner, id, " me@example.com "); status != http.StatusSeeOther || loc != add { |
| 129 | t.Fatalf("the owner's email: %d to %q", status, loc) |
| 130 | } |
| 131 | if form = codeForm(t, r.get(owner, add, 200)); strings.Contains(form, "me@example.com") { |
| 132 | t.Fatal("the sign-in step prefills the email") |
| 133 | } |
| 134 | connectedAs(t, r, runner, id, "claude-1", "one-ok") |
| 135 | row := rowHTML(t, r.get(owner, page+"?added="+id, 200), id) |
| 136 | for _, want := range []string{"<span data-stated-email>me@example.com</span> <span equipo-conn-said>(added by you)</span>", "Change the account email", `value="me@example.com"`} { |
| 137 | if !strings.Contains(row, want) { |
| 138 | t.Fatalf("the signed-in row lacks %q: %s", want, row) |
| 139 | } |
| 140 | } |
| 141 | if strings.Contains(row, `data-account-email="`+id+`" open`) { |
| 142 | t.Fatal("the row asks again for the email given at sign-in") |
| 143 | } |
| 144 | list, err := r.client.Connections(r.ctx) |
| 145 | if err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | for _, c := range list.Items { |
| 149 | if c.ID == id && (c.Label != "" || c.Kind != wire.KindToken) { |
| 150 | t.Fatalf("the stated email became the provider's label, or not a token: %+v", c) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Left empty: the row asks after the sign-in, as before. |
| 155 | empty := signInCardRig(t, r, runner, "two") |
| 156 | connectedAs(t, r, runner, empty, "claude-2", "two-ok") |
| 157 | row = rowHTML(t, r.get(owner, page+"?added="+empty, 200), empty) |
| 158 | if !strings.Contains(row, `data-account-email="`+empty+`" open`) || !strings.Contains(row, "Add the account email") || strings.Contains(row, "(added by you)") { |
| 159 | t.Fatalf("an empty sign-in email changed the row: %s", row) |
| 160 | } |
| 161 | |
| 162 | // A token paste still connecting takes none; nor does an ended one. |
| 163 | paste, err := r.client.Connect(r.ctx, wire.Connect{Provider: "claude", Kind: wire.KindToken}) |
| 164 | if err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | if status, _ := email(owner, paste.ID, "paste@example.com"); status != http.StatusConflict { |
| 168 | t.Fatalf("an email on a token paste still connecting: %d", status) |
| 169 | } |
| 170 | if _, err := r.client.CancelConnection(r.ctx, paste.ID); err != nil { |
| 171 | t.Fatal(err) |
| 172 | } |
| 173 | if status, _ := email(owner, paste.ID, "paste@example.com"); status != http.StatusConflict { |
| 174 | t.Fatalf("an email on an ended attempt: %d", status) |
| 175 | } |
| 176 | |
| 177 | // Nowhere else: not to the runner, not in the event stream. |
| 178 | var sent int |
| 179 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE body LIKE '%me@example.com%' OR body LIKE '%paste@example.com%'").Scan(&sent); err != nil || sent != 0 { |
| 180 | t.Fatalf("the stated email went to the runner: %d %v", sent, err) |
| 181 | } |
| 182 | if raw := rawEvents(t, r.rig); strings.Contains(raw, "me@example.com") { |
| 183 | t.Fatal("the stated email is in the event stream") |
| 184 | } |
| 185 | } |
| 186 | |