package thintest import ( "amadan.net/oficina/equipo/internal/wire" "io" "net/http" "net/url" "strings" "testing" ) func TestRosterStopFinishedRedirects(t *testing.T) { r := newToolRig(t) if err := r.runner.Send(r.ctx, "finish-before-stop", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Finished."}); err != nil { t.Fatal(err) } r.wait("finished", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnReplied }) status, location := r.post(r.inst.Cookie(), "/conversation/turns/"+r.turn+"/stop", url.Values{}) if status != http.StatusSeeOther || location != "/conversation" { t.Fatalf("finished Stop: %d %s", status, location) } } func TestRosterActionCSRF(t *testing.T) { r := newToolRig(t) if res := r.call("csrf-question", wire.ToolAskPerson, map[string]any{"question": "Which page?", "options": []string{}}); res.Code != "asked" { t.Fatal(res) } snap, err := r.client.Snapshot(r.ctx) if err != nil { t.Fatal(err) } var card wire.Card for _, c := range snap.Cards { if c.Kind == wire.CardQuestion { card = c } } if _, err := r.client.Answer(r.ctx, card.ID, "This one", card.Revision); err != nil { t.Fatal(err) } paths := []string{"/conversation/cards/" + card.ID + "/visibility", "/conversation/cards/" + card.ID + "/reply", "/conversation/turns/" + r.turn + "/stop"} for _, path := range paths { req, _ := http.NewRequest("POST", r.inst.URL+path, strings.NewReader(url.Values{"aside": {"true"}, "revision": {"2"}, "request_id": {"csrf-reply"}, "body": {"Unsafe reply"}}.Encode())) req.AddCookie(r.inst.Cookie()) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Origin", "https://foreign.example") req.Header.Set("Sec-Fetch-Site", "cross-site") res, err := http.DefaultClient.Do(req) if err != nil { t.Fatal(err) } io.Copy(io.Discard, res.Body) res.Body.Close() if res.StatusCode != http.StatusForbidden { t.Fatalf("cross-site %s: %d", path, res.StatusCode) } } var n int for _, q := range []string{"SELECT count(*) FROM chat_card_visibility", "SELECT count(*) FROM chat_card_threads", "SELECT count(*) FROM chat_messages WHERE text='Unsafe reply'", "SELECT count(*) FROM runner_commands WHERE kind='cancel'"} { if err := r.inst.DB.Writer().QueryRow(q).Scan(&n); err != nil { t.Fatal(err) } if n != 0 { t.Fatalf("CSRF changed state: %s", q) } } var state string if err := r.inst.DB.Writer().QueryRow("SELECT state FROM chat_turns WHERE id=?", r.turn).Scan(&state); err != nil { t.Fatal(err) } if state != wire.TurnRunning { t.Fatalf("cross-site Stop changed turn: %s", state) } }