package thintest import ( "net/http" "net/url" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // signInCardRig starts a Claude sign-in by a link for the owner and has // the runner put its link on the card. It returns the attempt's id. func signInCardRig(t *testing.T, r *toolRig, runner *FakeRunner, frame string) string { t.Helper() attempt, err := r.client.Connect(r.ctx, wire.Connect{Provider: "claude"}) if err != nil || attempt.Prompt == nil || attempt.Prompt.Kind != wire.PromptAuthorize { t.Fatalf("sign-in attempt %+v %v", attempt, err) } f, _, err := runner.Expect(r.ctx, wire.FrameConnect) if err != nil { t.Fatal(err) } _ = runner.Send(r.ctx, "a-"+frame, wire.FrameAck, wire.Ack{ID: f.ID}) 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 { t.Fatal(err) } r.wait("the link on the card", func(c *Conversation) bool { p := c.Connections[attempt.ID].Prompt return p != nil && p.URL == signInLink }) return attempt.ID } // codeForm is the sign-in card's code form on the Add page. func codeForm(t *testing.T, page string) string { t.Helper() i := strings.Index(page, "data-code-form") if i < 0 { t.Fatalf("no code form: %.400s", page) } start := strings.LastIndex(page[:i], "") return form } // connectedAs has the runner finish the attempt as a token connection // the provider named no account for. func connectedAs(t *testing.T, r *toolRig, runner *FakeRunner, id, local, frame string) { t.Helper() 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 { t.Fatal(err) } r.wait("healthy", func(c *Conversation) bool { return c.Connections[id].State == wire.ConnHealthy }) } // TestPagesSignInEmail (Paul, 2026-11-23): a Claude sign-in by a link // makes a token connection whose account the provider will not name, so // the sign-in step itself asks for the account email, once and // optionally, as part of the code's form: no value prefilled, the code // field still unnamed (never posted). An email typed there is the // stated email once the connection is made, shown on the row as the // person's own with the field closed; left empty, the row asks as // before. A bad shape, a teammate and a cross-site post are refused as // on the row, and a token paste still connecting takes none. The email // goes nowhere else. func TestPagesSignInEmail(t *testing.T) { r := &toolRig{rig: newRig(t)} keys := sealKeys(t) runner := r.dial(0) hello := held() hello.Generation, hello.SealKey = runner.Welcome.Generation, keys.PublicString() if err := runner.Send(r.ctx, "h1", wire.FrameHello, hello); err != nil { t.Fatal(err) } r.wait("runner online", func(c *Conversation) bool { return c.Runner.Online }) owner := r.inst.Cookie() const add = "/conversation/settings/connections/add" const page = "/conversation/settings/connections" id := signInCardRig(t, r, runner, "one") form := codeForm(t, r.get(owner, add, 200)) for _, want := range []string{ `method="post" action="/conversation/connections/` + id + `/email"`, ``, ``, ``, "Optional. Shown wherever Equipo names this connection. Equipo does not verify it with Claude.", `data-email-note role="status" hidden data-bad="That does not look like an email address."`, "Paste the code Anthropic shows you", } { if !strings.Contains(form, want) { t.Fatalf("the sign-in step lacks %q: %s", want, form) } } if _, code, _ := strings.Cut(form, `id="code-`+id+`"`); strings.Contains(strings.SplitN(code, ">", 2)[0], "name=") { t.Fatal("the code field has a name: a scriptless post would send it") } if strings.Count(form, "data-email-field") != 1 || strings.Contains(form, "value=\"\" data-email-field") || strings.Contains(form, `data-email-field required`) { t.Fatalf("the email field is prefilled, required or doubled: %s", form) } email := func(c *http.Cookie, id, v string) (int, string) { return r.post(c, "/conversation/connections/"+id+"/email", url.Values{"email": {v}, "from": {"add"}}) } // Refused as on the row: a teammate, a cross-site post, a bad shape. if status, _ := email(r.memberCookie(), id, "mallory@example.com"); status != http.StatusNotFound { t.Fatalf("a teammate set the email: %d", status) } cross, _ := http.NewRequest("POST", r.inst.URL+"/conversation/connections/"+id+"/email", strings.NewReader(url.Values{"email": {"cross@example.com"}}.Encode())) cross.AddCookie(owner) cross.Header.Set("Content-Type", "application/x-www-form-urlencoded") cross.Header.Set("Origin", "https://elsewhere.example") cross.Header.Set("Sec-Fetch-Site", "cross-site") if res, err := http.DefaultClient.Do(cross); err != nil || res.StatusCode != http.StatusForbidden { t.Fatalf("a cross-site post: %v %v", res.StatusCode, err) } else { res.Body.Close() } 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"} { if status, _ := email(owner, id, bad); status != http.StatusUnprocessableEntity { t.Fatalf("%q: %d", bad, status) } } // The owner's, typed with the code: kept for the attempt, the page // back on the Add page, the card's field still empty. if status, loc := email(owner, id, " me@example.com "); status != http.StatusSeeOther || loc != add { t.Fatalf("the owner's email: %d to %q", status, loc) } if form = codeForm(t, r.get(owner, add, 200)); strings.Contains(form, "me@example.com") { t.Fatal("the sign-in step prefills the email") } connectedAs(t, r, runner, id, "claude-1", "one-ok") row := rowHTML(t, r.get(owner, page+"?added="+id, 200), id) for _, want := range []string{"me@example.com (added by you)", "Change the account email", `value="me@example.com"`} { if !strings.Contains(row, want) { t.Fatalf("the signed-in row lacks %q: %s", want, row) } } if strings.Contains(row, `data-account-email="`+id+`" open`) { t.Fatal("the row asks again for the email given at sign-in") } list, err := r.client.Connections(r.ctx) if err != nil { t.Fatal(err) } for _, c := range list.Items { if c.ID == id && (c.Label != "" || c.Kind != wire.KindToken) { t.Fatalf("the stated email became the provider's label, or not a token: %+v", c) } } // Left empty: the row asks after the sign-in, as before. empty := signInCardRig(t, r, runner, "two") connectedAs(t, r, runner, empty, "claude-2", "two-ok") row = rowHTML(t, r.get(owner, page+"?added="+empty, 200), empty) if !strings.Contains(row, `data-account-email="`+empty+`" open`) || !strings.Contains(row, "Add the account email") || strings.Contains(row, "(added by you)") { t.Fatalf("an empty sign-in email changed the row: %s", row) } // A token paste still connecting takes none; nor does an ended one. paste, err := r.client.Connect(r.ctx, wire.Connect{Provider: "claude", Kind: wire.KindToken}) if err != nil { t.Fatal(err) } if status, _ := email(owner, paste.ID, "paste@example.com"); status != http.StatusConflict { t.Fatalf("an email on a token paste still connecting: %d", status) } if _, err := r.client.CancelConnection(r.ctx, paste.ID); err != nil { t.Fatal(err) } if status, _ := email(owner, paste.ID, "paste@example.com"); status != http.StatusConflict { t.Fatalf("an email on an ended attempt: %d", status) } // Nowhere else: not to the runner, not in the event stream. var sent int 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 { t.Fatalf("the stated email went to the runner: %d %v", sent, err) } if raw := rawEvents(t, r.rig); strings.Contains(raw, "me@example.com") { t.Fatal("the stated email is in the event stream") } }