Merge branch 'ask-disconnect-sticks' into main
Merge branch 'ask-disconnect-sticks' into main Ask's Disconnect sticks for the member on every device: a stored members.ask_disconnected flag set by Disconnect and cleared only when a connection the member makes is stored; automatic native set-ups send "auto": true and get 409 disconnected while it is set; /ask/state carries disconnected. Reviewed by Claude Fable 5.2 (approved at 5ef917e after one round). make ci green.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
11 files changed,
+588
−60
docs/api/base-native-v1.md+29 −5go.mod+1 −1internal/store/disconnected_test.go+64 −0internal/store/equipo.go+79 −23internal/store/members.go+7 −2internal/store/migrations/0012_ask_disconnected.sql+4 −0internal/web/ask.go+4 −1internal/web/ask_connect.go+6 −3internal/web/ask_oficina_native.go+73 −23internal/webtest/ask_disconnect_test.go+304 −0internal/webtest/ask_native_contract_test.go+17 −2
diff --git a/docs/api/base-native-v1.md b/docs/api/base-native-v1.md| index 9bc4f88..620fa6d 100644 |
| --- a/docs/api/base-native-v1.md |
| +++ b/docs/api/base-native-v1.md |
| @@ -110,7 +110,8 @@ for a form). Every answer is `no-store`. |
| ```json |
| {"state": "ready", "messages": [Message…], "jobs": [Job…], "cards": [Card…], "turn": Turn|null, |
| - "status": "Thinking…", "cursor": 42, "renew_after": "2026-11-28T10:00:00Z", "equipo_origin": "https://equipo.acme.example"} |
| + "status": "Thinking…", "cursor": 42, "renew_after": "2026-11-28T10:00:00Z", "equipo_origin": "https://equipo.acme.example", |
| + "disconnected": false} |
| ``` |
| - `messages`, `jobs` and `cards` are never `null`. Outside `ready`, |
| @@ -125,6 +126,11 @@ for a form). Every answer is `no-store`. |
| (below), silently. `""` for a key from the web's code flow, or none. |
| - `equipo_origin` is the Equipo instance Ask relays to: the audience of the |
| assertion for Ask set-up. |
| +- `disconnected` is `true` once the member has disconnected Ask, on any |
| + device or the web, until a connection they make themselves is stored. |
| + While it is, no client sets Ask up by itself; the app offers Connect |
| + instead. `state` is then `connect`, as after any Disconnect, or `pending` |
| + while a code from the web's Connect waits for approval (still `true`). |
| **Message** `{"id", "role": "person"|"assistant", "text", "turn_id", "created_at"}`. |
| An assistant's `text` may carry the little Markdown `ask.js` draws (lists, |
| @@ -209,14 +215,31 @@ Form: `choice` (an option's `value`), `revision` (the card's, as seen) and |
| ### `POST /ask/disconnect` |
| Ends the person's Ask connection, revoking the key in Equipo, whichever |
| -way it was made. It answers `303 /settings`. |
| +way it was made, and marks Ask disconnected by the member (`disconnected` |
| +in `/ask/state`), for every device and the web. It answers `303 /settings`. |
| +A Disconnect that fails changes nothing, the mark included. |
| + |
| +Only a connection the member makes clears the mark, in the same |
| +transaction that stores it: |
| +- the web's code flow once the code is approved and the key stored (pressing |
| + Connect or Reconnect alone leaves it set; a code that expires, is refused |
| + or is abandoned leaves it set); |
| +- `POST /api/v1/ask/oficina` without `"auto": true`, once its key is stored. |
| + |
| +Any set-up that fails leaves it set. An automatic set-up never clears it, |
| +and the save itself refuses while it is set. |
| ## Ask set-up with Oficina ID |
| ### `POST /api/v1/ask/oficina` |
| -JSON `{"assertion", "verifier"}`: an assertion the kit fetched from Oficina |
| -ID addressed to `equipo_origin`, and its verifier. Only a native session may |
| +JSON `{"assertion", "verifier", "auto"?}`: an assertion the kit fetched |
| +from Oficina ID addressed to `equipo_origin`, and its verifier. `auto` is |
| +`true` for a set-up the app starts by itself (after sign-in, or to renew |
| +after `renew_after`); leave it out, or send `false`, when the person asked |
| +to connect. `auto` must be exactly `true` or `false`: `null`, any other |
| +type, a repeated key or another spelling (`"AUTO"`) is `400 invalid`, as is |
| +any unknown or repeated name. Only a native session may |
| call it (with `Origin`, as every POST). Base completes Equipo's own native |
| sign-in for the person (Equipo's `POST /native/oficina-id`, then |
| `POST /api/v1/auth/devices/native`), keeps the key it gets sealed where the |
| @@ -227,10 +250,11 @@ anything else that changes the person's connection meanwhile. |
| | status | body | meaning | |
| |---|---|---| |
| | `200` | `{"state":"ready","renew_after":"<RFC 3339>"}` | Ask is ready. The key ends with Equipo's session, seven days after it was made; `renew_after` is five days after. After it, post a fresh assertion: that replaces the key and revokes the old one in Equipo. | |
| +| `409` | `{"state":"disconnected"}` | `auto` was `true`, and the member has disconnected Ask (also while a web code waits for approval). Nothing is stored; if the member disconnected while this set-up was at Equipo, the device Equipo made was revoked. Show Connect; post again without `auto` only when the person asks. | |
| | `409` | `{"state":"not_linked"}` | Equipo has no member linked to this Oficina ID. Show Ask's "not set up for you yet" state, with the web's code flow; don't retry in a loop. | |
| | `409` | `{"state":"superseded"}` | Ask's connection changed while Equipo answered (Disconnect, or a connection from the web): nothing was saved, and the device Equipo made was revoked. Reload `/ask/state`; don't retry at once. | |
| | `422` | `{"state":"failed"}` | Equipo didn't accept the assertion (expired, wrong audience or verifier, already used). Get a fresh one and try once more. | |
| -| `400` | `{"state":"invalid"}` | Not JSON, an unknown or missing field, or Equipo couldn't read it. | |
| +| `400` | `{"state":"invalid"}` | Not JSON, an unknown, repeated or missing name, `auto` not exactly `true` or `false`, or Equipo couldn't read it. | |
| | `403` | `{"state":"forbidden"}` | Not a native session: the web keeps its code flow. | |
| | `502` | `{"state":"unavailable"}` | Equipo didn't answer, or refused the device. Try again later. | |
| | `503` | `{"state":"unavailable"}` | Base couldn't read or store the connection. | |
diff --git a/go.mod b/go.mod| index d416e8a..72610f4 100644 |
| --- a/go.mod |
| +++ b/go.mod |
| @@ -9,6 +9,7 @@ require ( |
| github.com/chromedp/cdproto v0.158.6 |
| github.com/chromedp/chromedp v0.21.0 |
| github.com/go-chi/chi/v5 v5.3.2 |
| + github.com/go-json-experiment/json v0.0.0-20260820222146-c27c302e5fc3 |
| ) |
| require ( |
| @@ -17,7 +18,6 @@ require ( |
| amadan.net/rastrillo/rastrillo/money v0.1.0 // indirect |
| github.com/BurntSushi/toml v1.6.0 // indirect |
| github.com/dustin/go-humanize v1.0.1 // indirect |
| - github.com/go-json-experiment/json v0.0.0-20260820222146-c27c302e5fc3 // indirect |
| github.com/google/uuid v1.6.0 // indirect |
| github.com/jinzhu/inflection v1.0.0 // indirect |
| github.com/jinzhu/now v1.1.5 // indirect |
diff --git a/internal/store/disconnected_test.go b/internal/store/disconnected_test.go| new file mode 100644 |
| index 0000000..7d5b6f7 |
| --- /dev/null |
| +++ b/internal/store/disconnected_test.go |
| @@ -0,0 +1,64 @@ |
| +package store |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// AskDisconnected is set by Disconnect and cleared only where a connection |
| +// is made: an explicit connected save, or an approval stored. A pending |
| +// code leaves it, and an automatic save refuses while it is set. |
| +func TestAskDisconnectedClearsOnlyOnAConnection(t *testing.T) { |
| + ctx, db := context.Background(), open(t) |
| + ana, _, err := Invite(ctx, db, "ana@example.com") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + flag := func() bool { |
| + t.Helper() |
| + v, err := AskDisconnected(ctx, db, ana.ID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return v |
| + } |
| + link := func(dev, state string) EquipoLink { |
| + return EquipoLink{MemberID: ana.ID, Origin: "https://equipo.test", State: state, DeviceID: dev, |
| + Sealed: []byte("s"), ExpiresAt: time.Now().Add(time.Hour)} |
| + } |
| + if err := DisconnectEquipo(ctx, db, ana.ID); err != nil || !flag() { |
| + t.Fatalf("disconnect: %v, flag %v", err, flag()) |
| + } |
| + // A pending code is not a connection. |
| + if err := SaveEquipoLink(ctx, db, link("CODE", EquipoPending)); err != nil || !flag() { |
| + t.Fatalf("pending code: %v, flag %v", err, flag()) |
| + } |
| + // An automatic save refuses, writing nothing. |
| + if err := SaveConnectedEquipoLink(ctx, db, link("AUTO", EquipoConnected), false); !errors.Is(err, ErrAskDisconnected) { |
| + t.Fatalf("automatic save while disconnected: %v", err) |
| + } |
| + if l, _, _ := EquipoLinkFor(ctx, db, ana.ID); l.DeviceID != "CODE" || !flag() { |
| + t.Fatalf("the refused automatic save wrote: %+v, flag %v", l, flag()) |
| + } |
| + // The approval of the code is the connection. |
| + if ok, err := ConnectEquipoLink(ctx, db, ana.ID, "CODE", []byte("b"), time.Now().Add(time.Hour), 3); !ok || err != nil || flag() { |
| + t.Fatalf("approval: %v %v, flag %v", ok, err, flag()) |
| + } |
| + // An approval that stores nothing clears nothing. |
| + if err := DisconnectEquipo(ctx, db, ana.ID); err != nil { |
| + t.Fatal(err) |
| + } |
| + if ok, _ := ConnectEquipoLink(ctx, db, ana.ID, "CODE", []byte("b"), time.Now().Add(time.Hour), 3); ok || !flag() { |
| + t.Fatalf("a late approval: stored %v, flag %v", ok, flag()) |
| + } |
| + // An explicit connected save clears it. |
| + if err := SaveConnectedEquipoLink(ctx, db, link("NATIVE", EquipoConnected), true); err != nil || flag() { |
| + t.Fatalf("explicit save: %v, flag %v", err, flag()) |
| + } |
| + // And an automatic one, not disconnected, saves. |
| + if err := SaveConnectedEquipoLink(ctx, db, link("RENEW", EquipoConnected), false); err != nil || flag() { |
| + t.Fatalf("automatic renewal: %v, flag %v", err, flag()) |
| + } |
| +} |
diff --git a/internal/store/equipo.go b/internal/store/equipo.go| index b00579b..04b11b2 100644 |
| --- a/internal/store/equipo.go |
| +++ b/internal/store/equipo.go |
| @@ -53,30 +53,62 @@ func EquipoLinkFor(ctx context.Context, db *sql.DB, memberID string) (l EquipoLi |
| return l, true, nil |
| } |
| +// ErrAskDisconnected: the member has disconnected Ask, so an automatic |
| +// save stores nothing (SaveConnectedEquipoLink). |
| +var ErrAskDisconnected = errors.New("store: Ask disconnected by the member") |
| + |
| // SaveEquipoLink stores l as the member's only link, replacing any other, |
| // and forgets the member's host token in the same transaction: a new |
| // connection may be another Equipo person, and gets its own once ready. |
| +// It leaves AskDisconnected as it is: a pending code is not a connection. |
| func SaveEquipoLink(ctx context.Context, db *sql.DB, l EquipoLink) error { |
| + return inTx(ctx, db, func(tx *sql.Tx) error { return saveEquipoLink(ctx, tx, l) }) |
| +} |
| + |
| +// SaveConnectedEquipoLink is SaveEquipoLink for a connected link, made in |
| +// one go (Ask set up with Oficina ID). An explicit one, which the member |
| +// asked for, clears AskDisconnected in the same transaction. An automatic |
| +// one leaves it, and while it is set stores nothing and answers |
| +// ErrAskDisconnected: the flag is checked where the link is written, not |
| +// only by the caller. |
| +func SaveConnectedEquipoLink(ctx context.Context, db *sql.DB, l EquipoLink, explicit bool) error { |
| + return inTx(ctx, db, func(tx *sql.Tx) error { |
| + if explicit { |
| + if _, err := tx.ExecContext(ctx, `UPDATE members SET ask_disconnected = 0 WHERE id = ?`, l.MemberID); err != nil { |
| + return err |
| + } |
| + } else { |
| + var disconnected bool |
| + if err := tx.QueryRowContext(ctx, `SELECT ask_disconnected FROM members WHERE id = ?`, l.MemberID).Scan(&disconnected); err != nil { |
| + return err |
| + } |
| + if disconnected { |
| + return ErrAskDisconnected |
| + } |
| + } |
| + return saveEquipoLink(ctx, tx, l) |
| + }) |
| +} |
| + |
| +func saveEquipoLink(ctx context.Context, tx *sql.Tx, l EquipoLink) error { |
| var polled any |
| if l.PolledAt != nil { |
| polled = l.PolledAt.UTC() |
| } |
| - return inTx(ctx, db, func(tx *sql.Tx) error { |
| - if _, err := tx.ExecContext(ctx, `DELETE FROM host_tokens WHERE member_id = ?`, l.MemberID); err != nil { |
| - return err |
| - } |
| - _, err := tx.ExecContext(ctx, `INSERT INTO equipo_links (`+equipoCols+`, updated_at) |
| - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| - ON CONFLICT (member_id) DO UPDATE SET origin = excluded.origin, state = excluded.state, |
| - device_id = excluded.device_id, user_code = excluded.user_code, |
| - verification_uri = excluded.verification_uri, sealed = excluded.sealed, |
| - expires_at = excluded.expires_at, revision = excluded.revision, |
| - polled_at = excluded.polled_at, person = excluded.person, |
| - exchanging_until = NULL, updated_at = excluded.updated_at`, |
| - l.MemberID, l.Origin, l.State, l.DeviceID, l.UserCode, l.VerificationURI, |
| - l.Sealed, l.ExpiresAt.UTC(), l.Revision, polled, l.Person, time.Now().UTC()) |
| + if _, err := tx.ExecContext(ctx, `DELETE FROM host_tokens WHERE member_id = ?`, l.MemberID); err != nil { |
| return err |
| - }) |
| + } |
| + _, err := tx.ExecContext(ctx, `INSERT INTO equipo_links (`+equipoCols+`, updated_at) |
| + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| + ON CONFLICT (member_id) DO UPDATE SET origin = excluded.origin, state = excluded.state, |
| + device_id = excluded.device_id, user_code = excluded.user_code, |
| + verification_uri = excluded.verification_uri, sealed = excluded.sealed, |
| + expires_at = excluded.expires_at, revision = excluded.revision, |
| + polled_at = excluded.polled_at, person = excluded.person, |
| + exchanging_until = NULL, updated_at = excluded.updated_at`, |
| + l.MemberID, l.Origin, l.State, l.DeviceID, l.UserCode, l.VerificationURI, |
| + l.Sealed, l.ExpiresAt.UTC(), l.Revision, polled, l.Person, time.Now().UTC()) |
| + return err |
| } |
| func inTx(ctx context.Context, db *sql.DB, fn func(*sql.Tx) error) error { |
| @@ -116,12 +148,24 @@ func ReleaseEquipoPoll(ctx context.Context, db *sql.DB, memberID, dev string) er |
| // ConnectEquipoLink stores the bearer an exchange brought back, sealed, but |
| // only while the member's link is still that pending device. false means |
| // it is not (disconnected, or replaced by a newer Connect): the caller must |
| -// revoke the bearer instead. |
| -func ConnectEquipoLink(ctx context.Context, db *sql.DB, memberID, dev string, sealed []byte, expires time.Time, revision int64) (bool, error) { |
| - res, err := db.ExecContext(ctx, `UPDATE equipo_links SET state = ?, sealed = ?, expires_at = ?, revision = ?, |
| - user_code = '', exchanging_until = NULL, updated_at = ? WHERE member_id = ? AND device_id = ? AND state = ?`, |
| - EquipoConnected, sealed, expires.UTC(), revision, time.Now().UTC(), memberID, dev, EquipoPending) |
| - return affected(res, err) |
| +// revoke the bearer instead. Stored, it is the member's connection, made |
| +// with their approval in Equipo, so it clears AskDisconnected in the same |
| +// transaction. |
| +func ConnectEquipoLink(ctx context.Context, db *sql.DB, memberID, dev string, sealed []byte, expires time.Time, revision int64) (stored bool, err error) { |
| + err = inTx(ctx, db, func(tx *sql.Tx) error { |
| + res, err := tx.ExecContext(ctx, `UPDATE equipo_links SET state = ?, sealed = ?, expires_at = ?, revision = ?, |
| + user_code = '', exchanging_until = NULL, updated_at = ? WHERE member_id = ? AND device_id = ? AND state = ?`, |
| + EquipoConnected, sealed, expires.UTC(), revision, time.Now().UTC(), memberID, dev, EquipoPending) |
| + if stored, err = affected(res, err); err != nil || !stored { |
| + return err |
| + } |
| + _, err = tx.ExecContext(ctx, `UPDATE members SET ask_disconnected = 0 WHERE id = ?`, memberID) |
| + return err |
| + }) |
| + if err != nil { |
| + stored = false |
| + } |
| + return stored, err |
| } |
| func affected(res sql.Result, err error) (bool, error) { |
| @@ -156,17 +200,29 @@ func LoseEquipoLink(ctx context.Context, db *sql.DB, memberID, dev string) error |
| } |
| // DisconnectEquipo forgets the member's link, whichever device it is now, |
| -// and their host token, in one transaction: both go or neither does. |
| +// and their host token, and marks Ask disconnected by the member |
| +// (AskDisconnected), in one transaction: all three or none. |
| func DisconnectEquipo(ctx context.Context, db *sql.DB, memberID string) error { |
| return inTx(ctx, db, func(tx *sql.Tx) error { |
| if _, err := tx.ExecContext(ctx, `DELETE FROM equipo_links WHERE member_id = ?`, memberID); err != nil { |
| return err |
| } |
| - _, err := tx.ExecContext(ctx, `DELETE FROM host_tokens WHERE member_id = ?`, memberID) |
| + if _, err := tx.ExecContext(ctx, `DELETE FROM host_tokens WHERE member_id = ?`, memberID); err != nil { |
| + return err |
| + } |
| + _, err := tx.ExecContext(ctx, `UPDATE members SET ask_disconnected = 1 WHERE id = ?`, memberID) |
| return err |
| }) |
| } |
| +// AskDisconnected reports whether the member has disconnected Ask and not |
| +// connected it since. |
| +func AskDisconnected(ctx context.Context, q QueryRower, memberID string) (bool, error) { |
| + var v bool |
| + err := q.QueryRowContext(ctx, `SELECT ask_disconnected FROM members WHERE id = ?`, memberID).Scan(&v) |
| + return v, err |
| +} |
| + |
| // SetEquipoPerson records the Equipo address the member's device dev |
| // speaks for, if the link is still that device. |
| func SetEquipoPerson(ctx context.Context, db *sql.DB, memberID, dev, person string) error { |
diff --git a/internal/store/members.go b/internal/store/members.go| index 3249d4e..04fdd58 100644 |
| --- a/internal/store/members.go |
| +++ b/internal/store/members.go |
| @@ -24,9 +24,14 @@ type Member struct { |
| // AskClosed is true once the member has closed Ask Base, docked open |
| // beside Home on a wide screen, until they open it again. |
| AskClosed bool |
| + // AskDisconnected is true once the member has disconnected Ask Base, |
| + // until a connection they make themselves is stored (DisconnectEquipo, |
| + // ConnectEquipoLink, SaveConnectedEquipoLink): |
| + // no app sets it up again by itself meanwhile. |
| + AskDisconnected bool |
| } |
| -const memberCols = `id, email, name, role, invited_at, joined_at, invite_sent_at, theme, ask_closed` |
| +const memberCols = `id, email, name, role, invited_at, joined_at, invite_sent_at, theme, ask_closed, ask_disconnected` |
| // memberColsOf is memberCols qualified by a table alias, for joins. |
| func memberColsOf(alias string) string { |
| @@ -42,7 +47,7 @@ type scanner interface{ Scan(...any) error } |
| func scanMember(s scanner) (Member, error) { |
| var m Member |
| var joined, sent sql.NullTime |
| - if err := s.Scan(&m.ID, &m.Email, &m.Name, &m.Role, &m.InvitedAt, &joined, &sent, &m.Theme, &m.AskClosed); err != nil { |
| + if err := s.Scan(&m.ID, &m.Email, &m.Name, &m.Role, &m.InvitedAt, &joined, &sent, &m.Theme, &m.AskClosed, &m.AskDisconnected); err != nil { |
| return Member{}, err |
| } |
| if joined.Valid { |
diff --git a/internal/store/migrations/0012_ask_disconnected.sql b/internal/store/migrations/0012_ask_disconnected.sql| new file mode 100644 |
| index 0000000..a309682 |
| --- /dev/null |
| +++ b/internal/store/migrations/0012_ask_disconnected.sql |
| @@ -0,0 +1,4 @@ |
| +-- ask_disconnected: the member disconnected Ask Base, and no app connects |
| +-- it again by itself until they connect it themselves. Theirs, on every |
| +-- device. |
| +ALTER TABLE members ADD COLUMN ask_disconnected integer NOT NULL DEFAULT 0; |
diff --git a/internal/web/ask.go b/internal/web/ask.go| index 7b06cf4..ea02167 100644 |
| --- a/internal/web/ask.go |
| +++ b/internal/web/ask.go |
| @@ -570,8 +570,11 @@ func (a *app) askStateJSON(w http.ResponseWriter, r *http.Request) { |
| Cursor int64 `json:"cursor"` |
| RenewAfter string `json:"renew_after"` |
| EquipoOrigin string `json:"equipo_origin"` |
| + // Disconnected: the member disconnected Ask, so no app sets it up |
| + // again by itself. |
| + Disconnected bool `json:"disconnected"` |
| }{v.State, append([]equipo.Message{}, v.Messages...), append([]jobView{}, v.Jobs...), append([]equipo.Card{}, v.Cards...), v.Turn, v.Status, cursor, |
| - renew, a.ask.client.Origin()} |
| + renew, a.ask.client.Origin(), me.AskDisconnected} |
| writeJSON(w, http.StatusOK, out) |
| } |
diff --git a/internal/web/ask_connect.go b/internal/web/ask_connect.go| index 5cded9b..3dd4963 100644 |
| --- a/internal/web/ask_connect.go |
| +++ b/internal/web/ask_connect.go |
| @@ -383,12 +383,15 @@ func (a *app) changeLink(id string, change func() error) error { |
| return change() |
| } |
| -// linkGen is the member's connection generation now. |
| -func (a *app) linkGen(id string) uint64 { |
| +// linkGenDisconnected is the member's connection generation now, and |
| +// whether they have disconnected Ask, read together under their lock: |
| +// Disconnect sets the one and moves the other in one locked step. |
| +func (a *app) linkGenDisconnected(ctx context.Context, id string) (uint64, bool, error) { |
| c := a.memberConn(id) |
| c.mu.Lock() |
| defer c.mu.Unlock() |
| - return c.gen |
| + disconnected, err := store.AskDisconnected(ctx, a.db, id) |
| + return c.gen, disconnected, err |
| } |
| func (a *app) renderConnect(w http.ResponseWriter, r *http.Request, status int, v connectView) { |
diff --git a/internal/web/ask_oficina_native.go b/internal/web/ask_oficina_native.go| index f489495..a65b868 100644 |
| --- a/internal/web/ask_oficina_native.go |
| +++ b/internal/web/ask_oficina_native.go |
| @@ -4,13 +4,15 @@ import ( |
| "context" |
| "crypto/ed25519" |
| "crypto/rand" |
| - "encoding/json" |
| "errors" |
| "io" |
| "net/http" |
| "sync" |
| "time" |
| + jsonv2 "github.com/go-json-experiment/json" |
| + "github.com/go-json-experiment/json/jsontext" |
| + |
| "amadan.net/oficina/home/connect" |
| "amadan.net/rastrillo/rastrillo/sessions" |
| @@ -51,6 +53,10 @@ const askNotLinked = "not_linked" |
| // saved nothing, and revoked the device it made. |
| const askSuperseded = "superseded" |
| +// askDisconnected is the answer to an automatic set-up after the member |
| +// disconnected Ask (store.Member.AskDisconnected): nothing was done. |
| +const askDisconnected = "disconnected" |
| + |
| // renewAfter is when the app should send a fresh assertion for link l, |
| // zero for a key from the code flow. |
| func renewAfter(l store.EquipoLink, sec deviceSecrets) time.Time { |
| @@ -78,12 +84,8 @@ func (a *app) askOficina(w http.ResponseWriter, r *http.Request) { |
| writeJSON(w, http.StatusForbidden, map[string]string{"state": "forbidden"}) |
| return |
| } |
| - var in struct { |
| - Assertion string `json:"assertion"` |
| - Verifier string `json:"verifier"` |
| - } |
| - if !isJSONBody(r) || decodeStrict(http.MaxBytesReader(w, r.Body, 16<<10), &in) != nil || |
| - in.Assertion == "" || len(in.Assertion) > 8192 || in.Verifier == "" || len(in.Verifier) > 256 { |
| + in, ok := readAskOficina(w, r) |
| + if !ok { |
| writeJSON(w, http.StatusBadRequest, map[string]string{"state": "invalid"}) |
| return |
| } |
| @@ -91,10 +93,20 @@ func (a *app) askOficina(w http.ResponseWriter, r *http.Request) { |
| ctx, cancel := context.WithTimeout(context.WithoutCancel(r.Context()), askOficinaTimeout) |
| defer cancel() |
| - // The connection as the set-up begins. Anything that changes it while |
| + // The connection as the set-up begins, and whether the member has |
| + // disconnected Ask, read together. Anything that changes it while |
| // Equipo answers (Disconnect, the web's Connect or its approval) wins: |
| // the set-up then saves nothing. |
| - gen := a.linkGen(me.ID) |
| + gen, disconnected, err := a.linkGenDisconnected(ctx, me.ID) |
| + if err != nil { |
| + a.logger.Error("ask oficina: disconnected", "err", err) |
| + writeJSON(w, http.StatusServiceUnavailable, map[string]string{"state": askUnavailable}) |
| + return |
| + } |
| + if in.Auto && disconnected { |
| + writeJSON(w, http.StatusConflict, map[string]string{"state": askDisconnected}) |
| + return |
| + } |
| sess, err := a.ask.client.NativeExchange(ctx, in.Assertion, in.Verifier) |
| if err != nil { |
| a.askOficinaRefused(w, "exchange", err) |
| @@ -122,14 +134,18 @@ func (a *app) askOficina(w http.ResponseWriter, r *http.Request) { |
| var old store.EquipoLink |
| var oldSec deviceSecrets |
| if err == nil { |
| - superseded, old, oldSec, err = a.saveOficinaLink(ctx, me, gen, l) |
| + superseded, old, oldSec, err = a.saveOficinaLink(ctx, me, gen, l, !in.Auto) |
| } |
| if err != nil || superseded { |
| // Nothing was saved: the device made for it goes. |
| a.revoke(ctx, l, deviceSecrets{Token: cred.Token}) |
| - if superseded { |
| + switch { |
| + case superseded: |
| writeJSON(w, http.StatusConflict, map[string]string{"state": askSuperseded}) |
| return |
| + case errors.Is(err, store.ErrAskDisconnected): |
| + writeJSON(w, http.StatusConflict, map[string]string{"state": askDisconnected}) |
| + return |
| } |
| a.logger.Error("ask oficina: store", "device", cred.ID, "err", err) |
| writeJSON(w, http.StatusServiceUnavailable, map[string]string{"state": askUnavailable}) |
| @@ -145,8 +161,10 @@ func (a *app) askOficina(w http.ResponseWriter, r *http.Request) { |
| // saveOficinaLink stores l as the member's link, unless their connection |
| // has changed since generation gen (superseded, and nothing is stored). It |
| // answers the link it replaced, read under the member's lock as it saves. |
| -// Saving forgets the old host token with it. |
| -func (a *app) saveOficinaLink(ctx context.Context, me store.Member, gen uint64, l store.EquipoLink) (superseded bool, old store.EquipoLink, oldSec deviceSecrets, err error) { |
| +// Saving forgets the old host token with it. An explicit save clears the |
| +// member's Disconnect; an automatic one is refused while it stands |
| +// (store.ErrAskDisconnected), checked again in the save's own transaction. |
| +func (a *app) saveOficinaLink(ctx context.Context, me store.Member, gen uint64, l store.EquipoLink, explicit bool) (superseded bool, old store.EquipoLink, oldSec deviceSecrets, err error) { |
| c := a.memberConn(me.ID) |
| c.mu.Lock() |
| defer c.mu.Unlock() |
| @@ -156,8 +174,11 @@ func (a *app) saveOficinaLink(ctx context.Context, me store.Member, gen uint64, |
| if old, oldSec, _, err = a.askLink(ctx, me); err != nil { |
| return false, store.EquipoLink{}, deviceSecrets{}, err |
| } |
| + if err = store.SaveConnectedEquipoLink(ctx, a.db, l, explicit); err != nil { |
| + return false, store.EquipoLink{}, deviceSecrets{}, err |
| + } |
| c.gen++ |
| - return false, old, oldSec, store.SaveEquipoLink(ctx, a.db, l) |
| + return false, old, oldSec, nil |
| } |
| // askOficinaRefused answers for a step of Equipo's sign-in that failed, |
| @@ -182,15 +203,44 @@ func (a *app) askOficinaRefused(w http.ResponseWriter, step string, err error) { |
| } |
| } |
| -// decodeStrict decodes exactly one JSON object with no unknown fields. |
| -func decodeStrict(r io.Reader, v any) error { |
| - d := json.NewDecoder(r) |
| - d.DisallowUnknownFields() |
| - if err := d.Decode(v); err != nil { |
| - return err |
| +// askOficinaIn is POST /api/v1/ask/oficina's body. |
| +type askOficinaIn struct { |
| + Assertion, Verifier string |
| + // Auto is a set-up the app starts by itself (after sign-in, or to |
| + // renew), which yields to the member's Disconnect; without it the |
| + // person asked, and it connects Ask again. |
| + Auto bool |
| +} |
| + |
| +// readAskOficina reads the body strictly: one JSON object with exactly the |
| +// names assertion, verifier and, optionally, auto, each once and spelt so; |
| +// auto only true or false (never null, which a client might send for "not |
| +// set", and which must not read as the person asking). |
| +func readAskOficina(w http.ResponseWriter, r *http.Request) (askOficinaIn, bool) { |
| + if !isJSONBody(r) { |
| + return askOficinaIn{}, false |
| + } |
| + raw, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 16<<10)) |
| + if err != nil { |
| + return askOficinaIn{}, false |
| } |
| - if d.Decode(new(json.RawMessage)) != io.EOF { |
| - return errors.New("trailing JSON") |
| + var in struct { |
| + Assertion string `json:"assertion"` |
| + Verifier string `json:"verifier"` |
| + Auto jsontext.Value `json:"auto"` |
| + } |
| + if jsonv2.Unmarshal(raw, &in, jsonv2.RejectUnknownMembers(true)) != nil || |
| + in.Assertion == "" || len(in.Assertion) > 8192 || in.Verifier == "" || len(in.Verifier) > 256 { |
| + return askOficinaIn{}, false |
| + } |
| + out := askOficinaIn{Assertion: in.Assertion, Verifier: in.Verifier} |
| + switch string(in.Auto) { |
| + case "": |
| + case "true": |
| + out.Auto = true |
| + case "false": |
| + default: |
| + return askOficinaIn{}, false |
| } |
| - return nil |
| + return out, true |
| } |
diff --git a/internal/webtest/ask_disconnect_test.go b/internal/webtest/ask_disconnect_test.go| new file mode 100644 |
| index 0000000..0f26f1f |
| --- /dev/null |
| +++ b/internal/webtest/ask_disconnect_test.go |
| @@ -0,0 +1,304 @@ |
| +package webtest |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "net/http" |
| + "strings" |
| + "testing" |
| + |
| + "amadan.net/carlos/base/internal/equipo/equipotest" |
| + "amadan.net/carlos/base/internal/store" |
| +) |
| + |
| +// Disconnect is the member's, not a device's: once they disconnect Ask |
| +// anywhere, no app connects it again by itself. Only an explicit connect |
| +// (the web's Connect, or the app's set-up when the person asks) does. |
| + |
| +// connectAuto posts what the app posts when it sets Ask up by itself. |
| +func (a *testApp) connectAuto(f *equipotest.Fake, subject string) Resp { |
| + a.t.Helper() |
| + assertion, verifier := f.OficinaAssertion(subject) |
| + body, _ := json.Marshal(map[string]any{"assertion": assertion, "verifier": verifier, "auto": true}) |
| + return a.nativePost("/api/v1/ask/oficina", string(body)) |
| +} |
| + |
| +// askDisconnected is /ask/state's "disconnected". |
| +func (a *testApp) askDisconnected() bool { |
| + a.t.Helper() |
| + _, r := a.askState() |
| + var s struct { |
| + Disconnected *bool `json:"disconnected"` |
| + } |
| + if err := json.Unmarshal([]byte(r.Body), &s); err != nil || s.Disconnected == nil { |
| + a.t.Fatalf("no disconnected in /ask/state: %s", r.Body) |
| + } |
| + return *s.Disconnected |
| +} |
| + |
| +// storedDisconnected is the flag as stored, read afresh: what a restarted |
| +// Base reads. |
| +func (a *testApp) storedDisconnected(email string) bool { |
| + a.t.Helper() |
| + m, ok, err := store.MemberByEmail(context.Background(), a.DB, email) |
| + if err != nil || !ok { |
| + a.t.Fatalf("member %s: %v", email, err) |
| + } |
| + return m.AskDisconnected |
| +} |
| + |
| +func TestDisconnectOnTheWebStopsAutomaticSetUp(t *testing.T) { |
| + web, app, f := newNativeAskApp(t) |
| + f.LinkOficinaID("subject-owner", ownerAddress) |
| + if r := app.connectAuto(f, "subject-owner"); r.Code != 200 { |
| + t.Fatalf("automatic set-up: %d %s", r.Code, r.Body) |
| + } |
| + if app.askDisconnected() { |
| + t.Fatal("disconnected before any Disconnect") |
| + } |
| + if r := web.post("/ask/disconnect", nil); r.Code != 303 { |
| + t.Fatalf("disconnect: %d", r.Code) |
| + } |
| + if !app.askDisconnected() || !app.storedDisconnected(ownerAddress) { |
| + t.Fatal("Disconnect did not stick") |
| + } |
| + exchanges := len(f.Calls("/native/oficina-id")) |
| + wantBody(t, "automatic set-up after Disconnect", app.connectAuto(f, "subject-owner"), 409, `{"state":"disconnected"}`) |
| + if len(f.Calls("/native/oficina-id")) != exchanges || app.equipoLinks() != 0 { |
| + t.Fatalf("an automatic set-up after Disconnect reached Equipo or stored a link") |
| + } |
| + if s, _ := app.askState(); s.State != "connect" { |
| + t.Errorf("state: %s", s.State) |
| + } |
| + // The person asks for it: the explicit set-up connects and clears it. |
| + if r := app.connectWithOficina(f, "subject-owner"); r.Code != 200 { |
| + t.Fatalf("explicit set-up: %d %s", r.Code, r.Body) |
| + } |
| + if app.askDisconnected() || app.storedDisconnected(ownerAddress) { |
| + t.Fatal("an explicit connect left the flag set") |
| + } |
| + if r := app.connectAuto(f, "subject-owner"); r.Code != 200 { |
| + t.Fatalf("automatic renewal after reconnecting: %d %s", r.Code, r.Body) |
| + } |
| +} |
| + |
| +// "auto": false is explicit. |
| +func TestExplicitSetUpSaysSo(t *testing.T) { |
| + _, app, f := newNativeAskApp(t) |
| + f.LinkOficinaID("subject-owner", ownerAddress) |
| + app.connectWithOficina(f, "subject-owner") |
| + app.post("/ask/disconnect", nil) |
| + assertion, verifier := f.OficinaAssertion("subject-owner") |
| + body, _ := json.Marshal(map[string]any{"assertion": assertion, "verifier": verifier, "auto": false}) |
| + if r := app.nativePost("/api/v1/ask/oficina", string(body)); r.Code != 200 { |
| + t.Fatalf("auto false: %d %s", r.Code, r.Body) |
| + } |
| + // Only absent, true or false: anything else is refused, whatever it |
| + // might have meant, so no client undoes a Disconnect by accident. |
| + app.post("/ask/disconnect", nil) |
| + for _, auto := range []string{`"auto":null`, `"auto":"true"`, `"auto":1`, `"auto":{}`, `"auto":true,"auto":false`, `"auto":false,"auto":true`, `"AUTO":true`, `"Auto":false`} { |
| + a, v := f.OficinaAssertion("subject-owner") |
| + body := `{"assertion":"` + a + `","verifier":"` + v + `",` + auto + `}` |
| + wantBody(t, auto, app.nativePost("/api/v1/ask/oficina", body), 400, `{"state":"invalid"}`) |
| + } |
| + if !app.storedDisconnected(ownerAddress) || app.equipoLinks() != 0 { |
| + t.Fatal("a refused body connected Ask") |
| + } |
| +} |
| + |
| +// The web's Connect clears the flag once the connection is made (its code |
| +// approved), not when the code is shown; an explicit set-up that fails |
| +// leaves the flag as it was. |
| +func TestWebConnectClearsDisconnected(t *testing.T) { |
| + web, app, f := newNativeAskApp(t) |
| + web.approveEquipo(f) |
| + web.post("/ask/disconnect", nil) |
| + f.FailNextNativeExchange(503) |
| + if r := app.connectWithOficina(f, "subject-owner"); r.Code != 502 { |
| + t.Fatalf("explicit set-up with Equipo down: %d %s", r.Code, r.Body) |
| + } |
| + if !app.askDisconnected() { |
| + t.Fatal("a failed explicit set-up cleared the flag") |
| + } |
| + code := web.startWebConnect() |
| + if !app.askDisconnected() || !app.storedDisconnected(ownerAddress) { |
| + t.Fatal("pressing Connect cleared the flag before anything was connected") |
| + } |
| + if r := web.approveAndCheck(f, code); r.Code != 303 || r.Location() != "/settings" { |
| + t.Fatalf("approval: %d %q", r.Code, r.Location()) |
| + } |
| + if app.askDisconnected() || app.storedDisconnected(ownerAddress) { |
| + t.Fatal("the web's approved connection left the flag set") |
| + } |
| +} |
| + |
| +// Fable's P4: a code that is never approved (it expires, or Equipo refuses |
| +// it) leaves the member disconnected, and the app does not connect them. |
| +func TestUnapprovedWebCodeLeavesDisconnected(t *testing.T) { |
| + for _, end := range []string{"expired", "refused"} { |
| + t.Run(end, func(t *testing.T) { |
| + web, app, f := newNativeAskApp(t) |
| + f.LinkOficinaID("subject-owner", ownerAddress) |
| + web.approveEquipo(f) |
| + web.post("/ask/disconnect", nil) |
| + web.startWebConnect() |
| + if end == "expired" { |
| + f.Expire() |
| + } else { |
| + f.RevokeAll() |
| + } |
| + web.pollable() |
| + web.check("settings") |
| + if s, _ := app.askState(); s.State != "connect" || !app.askDisconnected() { |
| + t.Fatalf("after the code %s: state %s, disconnected %v", end, s.State, app.askDisconnected()) |
| + } |
| + wantBody(t, "automatic set-up", app.connectAuto(f, "subject-owner"), 409, `{"state":"disconnected"}`) |
| + wantLive(t, f) |
| + }) |
| + } |
| +} |
| + |
| +// Fable's P4b: while the code waits, the app's automatic set-up does not |
| +// overtake it. |
| +func TestAutomaticSetUpLeavesAWaitingCode(t *testing.T) { |
| + web, app, f := newNativeAskApp(t) |
| + f.LinkOficinaID("subject-owner", ownerAddress) |
| + web.approveEquipo(f) |
| + web.post("/ask/disconnect", nil) |
| + code := web.startWebConnect() |
| + wantBody(t, "automatic set-up while a code waits", app.connectAuto(f, "subject-owner"), 409, `{"state":"disconnected"}`) |
| + if s, _ := app.askState(); s.State != "pending" { |
| + t.Fatalf("the waiting code went: %s", s.State) |
| + } |
| + if r := web.approveAndCheck(f, code); r.Code != 303 || r.Location() != "/settings" { |
| + t.Fatalf("approval: %d %q", r.Code, r.Location()) |
| + } |
| + if app.askDisconnected() || len(f.LiveCredentials()) != 1 { |
| + t.Fatalf("after approval: disconnected %v, live %v", app.askDisconnected(), f.LiveCredentials()) |
| + } |
| +} |
| + |
| +// An automatic set-up waiting at Equipo when Disconnect lands: overtaken, |
| +// nothing live, the flag stored, and the next automatic set-up refused. |
| +func TestAutomaticSetUpOvertakenByDisconnect(t *testing.T) { |
| + _, app, f := newNativeAskApp(t) |
| + f.LinkOficinaID("subject-owner", ownerAddress) |
| + if r := app.connectAuto(f, "subject-owner"); r.Code != 200 { |
| + t.Fatalf("connect: %d %s", r.Code, r.Body) |
| + } |
| + entered, release := f.HoldNextNativeExchange() |
| + done := background(func() Resp { return app.connectAuto(f, "subject-owner") }) |
| + arrived(t, entered, "the automatic set-up") |
| + if r := app.post("/ask/disconnect", nil); r.Code != 303 { |
| + t.Fatalf("disconnect: %d", r.Code) |
| + } |
| + release() |
| + wantBody(t, "overtaken automatic set-up", <-done, 409, `{"state":"superseded"}`) |
| + if n := app.equipoLinks(); n != 0 || !app.storedDisconnected(ownerAddress) { |
| + t.Fatalf("links %d, flag %v", n, app.storedDisconnected(ownerAddress)) |
| + } |
| + wantLive(t, f) |
| + wantBody(t, "next automatic set-up", app.connectAuto(f, "subject-owner"), 409, `{"state":"disconnected"}`) |
| +} |
| + |
| +// The web's approval landing after Disconnect stores nothing and leaves |
| +// the flag set. |
| +func TestLateWebApprovalAfterDisconnect(t *testing.T) { |
| + web, app, f := newNativeAskApp(t) |
| + f.LinkOficinaID("subject-owner", ownerAddress) |
| + code := web.startWebConnect() |
| + if err := f.Approve(code); err != nil { |
| + t.Fatal(err) |
| + } |
| + web.pollable() |
| + entered, release := f.HoldNextExchange() |
| + checked := background(func() Resp { return web.check("settings") }) |
| + arrived(t, entered, "the web's exchange") |
| + if r := app.post("/ask/disconnect", nil); r.Code != 303 { |
| + t.Fatalf("disconnect: %d", r.Code) |
| + } |
| + release() |
| + <-checked |
| + if n := app.equipoLinks(); n != 0 || !app.storedDisconnected(ownerAddress) { |
| + t.Fatalf("links %d, flag %v", n, app.storedDisconnected(ownerAddress)) |
| + } |
| + wantLive(t, f) |
| +} |
| + |
| +func TestDisconnectedIsPerMember(t *testing.T) { |
| + web, app, f := newNativeAskApp(t) |
| + if _, _, err := store.Invite(context.Background(), web.DB, "ana@acme.test"); err != nil { |
| + t.Fatal(err) |
| + } |
| + web.newBrowser().link("ana@acme.test", "subject-ana") |
| + ana := web.nativeSignIn("subject-ana") |
| + f.LinkOficinaID("subject-owner", ownerAddress) |
| + f.LinkOficinaID("subject-ana", "ana@acme.test") |
| + app.connectWithOficina(f, "subject-owner") |
| + if r := app.post("/ask/disconnect", nil); r.Code != 303 { |
| + t.Fatalf("disconnect from the app: %d", r.Code) |
| + } |
| + if ana.askDisconnected() || web.storedDisconnected("ana@acme.test") { |
| + t.Fatal("the owner's Disconnect reached Ana") |
| + } |
| + if r := ana.connectAuto(f, "subject-ana"); r.Code != 200 { |
| + t.Fatalf("Ana's automatic set-up: %d %s", r.Code, r.Body) |
| + } |
| + wantBody(t, "the owner's", app.connectAuto(f, "subject-owner"), 409, `{"state":"disconnected"}`) |
| +} |
| + |
| +// A Disconnect that fails as it sets the flag, after the link and host |
| +// token were deleted, undoes all three: one transaction. |
| +func TestFailedDisconnectLeavesNoFlag(t *testing.T) { |
| + web, app, f := newNativeAskApp(t) |
| + web.approveEquipo(f) |
| + if web.hostRows() != 1 { |
| + t.Fatalf("host tokens: %d", web.hostRows()) |
| + } |
| + if _, err := web.DB.Exec(`CREATE TRIGGER keep_flag BEFORE UPDATE OF ask_disconnected ON members BEGIN SELECT RAISE(ABORT, 'injected'); END`); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r := web.post("/ask/disconnect", nil); r.Code == 303 { |
| + t.Fatal("Disconnect reported success") |
| + } |
| + if app.askDisconnected() || web.storedDisconnected(ownerAddress) { |
| + t.Fatal("a failed Disconnect set the flag") |
| + } |
| + if web.equipoLinks() != 1 || web.hostRows() != 1 { |
| + t.Fatalf("a failed Disconnect removed the link (%d) or host token (%d)", web.equipoLinks(), web.hostRows()) |
| + } |
| +} |
| + |
| +// The web shows what it always showed after Disconnect: the connect state. |
| +func TestWebAfterDisconnectShowsConnect(t *testing.T) { |
| + web, _, f := newNativeAskApp(t) |
| + web.approveEquipo(f) |
| + web.post("/ask/disconnect", nil) |
| + page := web.get("/ask") |
| + if page.Code != 200 || !strings.Contains(page.Body, `action="/ask/connect"`) { |
| + t.Fatalf("/ask after Disconnect: %d\n%s", page.Code, page.Body) |
| + } |
| + if r := web.get("/settings"); r.Code != http.StatusOK || !strings.Contains(r.Body, `action="/ask/connect"`) { |
| + t.Fatalf("Settings after Disconnect: %d", r.Code) |
| + } |
| +} |
| + |
| +// The save's own transaction checks the flag, not only this process's |
| +// generation: a Disconnect another Base process stored while an automatic |
| +// set-up waited at Equipo (the generation here unmoved) still wins. |
| +func TestAutomaticSaveChecksTheStoredFlag(t *testing.T) { |
| + _, app, f := newNativeAskApp(t) |
| + f.LinkOficinaID("subject-owner", ownerAddress) |
| + entered, release := f.HoldNextNativeExchange() |
| + done := background(func() Resp { return app.connectAuto(f, "subject-owner") }) |
| + arrived(t, entered, "the automatic set-up") |
| + if _, err := app.DB.Exec(`UPDATE members SET ask_disconnected = 1 WHERE email = ?`, ownerAddress); err != nil { |
| + t.Fatal(err) |
| + } |
| + release() |
| + wantBody(t, "automatic set-up", <-done, 409, `{"state":"disconnected"}`) |
| + if n := app.equipoLinks(); n != 0 || !app.storedDisconnected(ownerAddress) { |
| + t.Fatalf("links %d, flag %v", n, app.storedDisconnected(ownerAddress)) |
| + } |
| + wantLive(t, f) |
| +} |
diff --git a/internal/webtest/ask_native_contract_test.go b/internal/webtest/ask_native_contract_test.go| index be42266..f7f92f0 100644 |
| --- a/internal/webtest/ask_native_contract_test.go |
| +++ b/internal/webtest/ask_native_contract_test.go |
| @@ -100,13 +100,13 @@ func (a *testApp) askState() (askState, Resp) { |
| return s, r |
| } |
| -var stateKeys = []string{"state", "messages", "jobs", "cards", "turn", "status", "cursor", "renew_after", "equipo_origin"} |
| +var stateKeys = []string{"state", "messages", "jobs", "cards", "turn", "status", "cursor", "renew_after", "equipo_origin", "disconnected"} |
| func TestAskContractBeforeConnecting(t *testing.T) { |
| _, app, f := newNativeAskApp(t) |
| s, r := app.askState() |
| wantKeys(t, "state", r.Body, stateKeys...) |
| - if strings.TrimSpace(r.Body) != `{"state":"connect","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":0,"renew_after":"","equipo_origin":"`+f.URL+`"}` || s.State != "connect" { |
| + if strings.TrimSpace(r.Body) != `{"state":"connect","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":0,"renew_after":"","equipo_origin":"`+f.URL+`","disconnected":false}` || s.State != "connect" { |
| t.Errorf("not connected: %s", r.Body) |
| } |
| wantBody(t, "send", app.nativeForm("/ask/messages", url.Values{"text": {"Hello"}, "key": {"k1"}}), 409, `{"state":"connect"}`) |
| @@ -379,6 +379,21 @@ var keySeq int |
| func equipoKey() string { keySeq++; return "never-401-" + strconv.Itoa(keySeq) } |
| +// After the member disconnects Ask, /ask/state says so and an automatic |
| +// set-up is refused with 409 disconnected. |
| +func TestAskContractDisconnected(t *testing.T) { |
| + web, app, f := newNativeAskApp(t) |
| + f.LinkOficinaID("subject-owner", ownerAddress) |
| + web.approveEquipo(f) |
| + web.post("/ask/disconnect", nil) |
| + _, r := app.askState() |
| + wantKeys(t, "state", r.Body, stateKeys...) |
| + if !strings.Contains(r.Body, `"state":"connect"`) || !strings.Contains(r.Body, `"disconnected":true`) { |
| + t.Fatalf("state after Disconnect: %s", r.Body) |
| + } |
| + wantBody(t, "automatic set-up", app.connectAuto(f, "subject-owner"), 409, `{"state":"disconnected"}`) |
| +} |
| + |
| // sseFrame is one frame of an event stream, as the native client parses it. |
| type sseFrame struct { |
| ID, Event, Data, Retry string |