| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/equipo" |
| 15 | "amadan.net/oficina/equipo/internal/equipo/oficina/oficinatest" |
| 16 | "amadan.net/oficina/equipo/internal/wire" |
| 17 | ) |
| 18 | |
| 19 | // oficinaRig is an Assistant rig whose instance has oficina-connect |
| 20 | // configured: a fake connect that approves every consent itself. |
| 21 | type oficinaRig struct { |
| 22 | *opsRig |
| 23 | fake *oficinatest.Connect |
| 24 | } |
| 25 | |
| 26 | func newOficinaRig(t *testing.T) *oficinaRig { |
| 27 | fake := oficinatest.Start(t) |
| 28 | base := StartFakeBase(t) |
| 29 | r := newRigWith(t, func(i *Instance) { |
| 30 | i.AssistantHosts = []string{base.Origin} |
| 31 | i.OficinaConnect, i.OficinaClientID = fake.URL, fake.ClientID |
| 32 | fake.Redirect = i.URL + "/oficina/connect/callback" |
| 33 | }) |
| 34 | runner := r.dialAt(wire.ProtocolAssistant) |
| 35 | o := &oficinaRig{opsRig: &opsRig{toolRig: &toolRig{rig: r, runner: runner}, base: base, a: r.client.Assistant(), protocol: wire.ProtocolAssistant}, fake: fake} |
| 36 | o.connect(base.Token) |
| 37 | return o |
| 38 | } |
| 39 | |
| 40 | // viaBase connects as Base does it: the card's link is Base's route, |
| 41 | // which asks the person API for connect's address with the person's |
| 42 | // bearer and sends the browser there; connect's consent comes back to |
| 43 | // the instance's callback, with no Equipo session, which hands the state |
| 44 | // and code on to Base's return address, saving nothing; Base finishes |
| 45 | // with the person's bearer. It returns the finish's status. |
| 46 | func (o *oficinaRig) viaBase() string { |
| 47 | o.t.Helper() |
| 48 | state, code := o.handBack(o.base.Origin + "/ask?open=1") |
| 49 | if n := o.rows("SELECT count(*) FROM oficina_connections"); n != 0 { |
| 50 | o.t.Fatalf("the callback saved %d connections", n) |
| 51 | } |
| 52 | status, err := o.a.OficinaFinish(o.ctx, state, code) |
| 53 | if err != nil { |
| 54 | o.t.Fatalf("finish: %v", err) |
| 55 | } |
| 56 | return status |
| 57 | } |
| 58 | |
| 59 | // handBack starts a consent for returnTo and follows it through |
| 60 | // connect and the callback: the state and code the callback hands back. |
| 61 | func (o *oficinaRig) handBack(returnTo string) (state, code string) { |
| 62 | o.t.Helper() |
| 63 | back := o.callback(returnTo) |
| 64 | u, err := url.Parse(back) |
| 65 | if err != nil || !strings.HasPrefix(back, strings.SplitN(returnTo, "?", 2)[0]+"?") || u.Query().Get("oficina_state") == "" || u.Query().Get("oficina_code") == "" { |
| 66 | o.t.Fatalf("handed back: %q", back) |
| 67 | } |
| 68 | if ex, _ := o.fake.Counts(); ex != 0 { |
| 69 | o.t.Fatalf("the callback exchanged the code") |
| 70 | } |
| 71 | return u.Query().Get("oficina_state"), u.Query().Get("oficina_code") |
| 72 | } |
| 73 | |
| 74 | // callback starts a consent for returnTo and follows it to where the |
| 75 | // callback sends the browser, with no session. |
| 76 | func (o *oficinaRig) callback(returnTo string) string { |
| 77 | o.t.Helper() |
| 78 | authorize, err := o.a.OficinaConnect(o.ctx, returnTo) |
| 79 | if err != nil || !strings.HasPrefix(authorize, o.fake.URL+"/oauth/authorize?") { |
| 80 | o.t.Fatalf("start: %q %v", authorize, err) |
| 81 | } |
| 82 | status, callback, _ := o.browse(http.MethodGet, authorize) |
| 83 | if status != http.StatusSeeOther || !strings.HasPrefix(callback, o.inst.URL+"/oficina/connect/callback?") { |
| 84 | o.t.Fatalf("consent: %d %q", status, callback) |
| 85 | } |
| 86 | status, back, _ := o.anonymous(callback) |
| 87 | if status != http.StatusSeeOther { |
| 88 | o.t.Fatalf("callback: %d %q", status, back) |
| 89 | } |
| 90 | return back |
| 91 | } |
| 92 | |
| 93 | // anonymous is a GET with no session at all. |
| 94 | func (o *oficinaRig) anonymous(raw string) (int, string, string) { |
| 95 | o.t.Helper() |
| 96 | client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} |
| 97 | res, err := client.Get(raw) |
| 98 | if err != nil { |
| 99 | o.t.Fatal(err) |
| 100 | } |
| 101 | defer res.Body.Close() |
| 102 | body, _ := io.ReadAll(res.Body) |
| 103 | return res.StatusCode, res.Header.Get("Location"), string(body) |
| 104 | } |
| 105 | |
| 106 | // browse is one browser request with the owner's session, redirects not |
| 107 | // followed: the status, Location and body. |
| 108 | func (o *oficinaRig) browse(method, raw string) (int, string, string) { |
| 109 | o.t.Helper() |
| 110 | req, _ := http.NewRequest(method, raw, nil) |
| 111 | if strings.HasPrefix(raw, o.inst.URL) { |
| 112 | req.AddCookie(o.inst.Cookie()) |
| 113 | if method == http.MethodPost { |
| 114 | req.Header.Set("Origin", o.inst.URL) |
| 115 | } |
| 116 | } |
| 117 | client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} |
| 118 | res, err := client.Do(req) |
| 119 | if err != nil { |
| 120 | o.t.Fatal(err) |
| 121 | } |
| 122 | defer res.Body.Close() |
| 123 | body, _ := io.ReadAll(res.Body) |
| 124 | return res.StatusCode, res.Header.Get("Location"), string(body) |
| 125 | } |
| 126 | |
| 127 | // consent follows the connect card's link through connect's consent |
| 128 | // and back, as the person's browser does; it returns the last page. |
| 129 | func (o *oficinaRig) consent(start string) string { |
| 130 | o.t.Helper() |
| 131 | status, authorize, _ := o.browse(http.MethodGet, start) |
| 132 | if status != http.StatusSeeOther || !strings.HasPrefix(authorize, o.fake.URL+"/oauth/authorize?") { |
| 133 | o.t.Fatalf("start: %d %q", status, authorize) |
| 134 | } |
| 135 | status, callback, _ := o.browse(http.MethodGet, authorize) |
| 136 | if status != http.StatusSeeOther || !strings.HasPrefix(callback, o.inst.URL+"/oficina/connect/callback?") { |
| 137 | o.t.Fatalf("consent: %d %q", status, callback) |
| 138 | } |
| 139 | status, done, _ := o.anonymous(callback) |
| 140 | if status != http.StatusSeeOther || !strings.HasPrefix(done, "/oficina/connect/done?") || !strings.Contains(done, "oficina_state=") { |
| 141 | o.t.Fatalf("callback: %d %q", status, done) |
| 142 | } |
| 143 | // Without the session that started it, nothing is finished. |
| 144 | if _, to, _ := o.anonymous(o.inst.URL + done); strings.HasPrefix(to, "/oficina") { |
| 145 | o.t.Fatalf("done without a session: %q", to) |
| 146 | } |
| 147 | if ex, _ := o.fake.Counts(); ex != 0 { |
| 148 | o.t.Fatal("finished without the session") |
| 149 | } |
| 150 | status, _, page := o.browse(http.MethodGet, o.inst.URL+done) |
| 151 | if status != http.StatusOK { |
| 152 | o.t.Fatalf("done: %d %s", status, page) |
| 153 | } |
| 154 | return page |
| 155 | } |
| 156 | |
| 157 | // connectCards are the person's connect cards in the Assistant lane. |
| 158 | func (o *oficinaRig) connectCards() []wire.AssistantCard { |
| 159 | o.t.Helper() |
| 160 | snap, err := o.a.Snapshot(o.ctx) |
| 161 | if err != nil { |
| 162 | o.t.Fatal(err) |
| 163 | } |
| 164 | return slices.DeleteFunc(snap.Cards, func(c wire.AssistantCard) bool { return c.Kind != wire.CardConnect }) |
| 165 | } |
| 166 | |
| 167 | // TestContractAssistantOficinaConnectCard: a look for something the |
| 168 | // Oficina apps do, by a person who has not connected them, tells the |
| 169 | // model so and raises one connect card, whose link starts connecting; a |
| 170 | // look for anything else does neither. Connecting (PKCE, checked by the |
| 171 | // fake) settles the card and the apps' operations are listed. |
| 172 | func TestContractAssistantOficinaConnectCard(t *testing.T) { |
| 173 | o := newOficinaRig(t) |
| 174 | o.begin("Put lunch with Ana in my calendar on Friday.") |
| 175 | for _, id := range []string{"l1", "l2"} { |
| 176 | res := o.tool(id, wire.ToolLook, map[string]any{"query": "calendar"}) |
| 177 | if res.IsError || !strings.Contains(res.Text, "not connected") || !strings.Contains(res.Text, "(oficina)") { |
| 178 | t.Fatalf("look %s: %+v", id, res) |
| 179 | } |
| 180 | } |
| 181 | if res := o.tool("l3", wire.ToolLook, map[string]any{"query": "weather"}); strings.Contains(res.Text, "oficina") { |
| 182 | t.Errorf("weather: %+v", res) |
| 183 | } |
| 184 | cards := o.connectCards() |
| 185 | if len(cards) != 1 { |
| 186 | t.Fatalf("connect cards: %+v", cards) |
| 187 | } |
| 188 | c := cards[0] |
| 189 | if c.State != wire.CardOpen || c.Question != "Connect your Oficina apps" || len(c.Links) != 1 || c.Links[0].URL != o.base.Origin+"/ask/oficina/connect" || c.Links[0].Rel != "oficina_connect" || c.Links[0].Label != "Connect" || c.Options == nil { |
| 190 | t.Fatalf("card: %+v", c) |
| 191 | } |
| 192 | raw, _ := json.Marshal(c) |
| 193 | var decoded any |
| 194 | _ = json.Unmarshal(raw, &decoded) |
| 195 | noMachinery(t, "k_none", []string{"/tasks/", "Equipo", "equipo"})("the connect card", decoded, false) |
| 196 | |
| 197 | if status := o.viaBase(); status != "connected" { |
| 198 | t.Errorf("finish: %q", status) |
| 199 | } |
| 200 | if ex, _ := o.fake.Counts(); ex != 1 { |
| 201 | t.Errorf("exchanges %d", ex) |
| 202 | } |
| 203 | cards = o.connectCards() |
| 204 | if len(cards) != 1 || cards[0].State != wire.CardResolved || cards[0].Revision != c.Revision+1 { |
| 205 | t.Errorf("after connecting: %+v", cards) |
| 206 | } |
| 207 | res := o.tool("l4", wire.ToolLook, map[string]any{"query": "calendar"}) |
| 208 | if strings.Contains(res.Text, "not connected") || !strings.Contains(res.Text, "Calendar (oficina)") || !strings.Contains(res.Text, "calendar_create_event") { |
| 209 | t.Errorf("connected look: %+v", res) |
| 210 | } |
| 211 | if n := len(o.connectCards()); n != 1 { |
| 212 | t.Errorf("%d connect cards after connecting", n) |
| 213 | } |
| 214 | |
| 215 | if ex, _ := o.fake.Counts(); ex != 1 { |
| 216 | t.Errorf("exchanges %d", ex) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // TestContractAssistantOficinaCallbackState (consent fixation): the |
| 221 | // callback needs no session and saves nothing; it hands the state and |
| 222 | // code back to Base. Only the person who started the consent finishes |
| 223 | // it, once, with their bearer: another person's bearer is refused (403) |
| 224 | // and uses the state up. A forged or finished state goes nowhere; |
| 225 | // connect's consent refused, or from another issuer, goes back to Base |
| 226 | // saying so. The return address must be on an allowed Base origin. |
| 227 | func TestContractAssistantOficinaCallbackState(t *testing.T) { |
| 228 | o := newOficinaRig(t) |
| 229 | ana := o.other("ana@example.com") |
| 230 | ana.connect(o.base.Mint()) |
| 231 | state, code := o.handBack(o.base.Origin + "/ask") |
| 232 | if n := o.rows("SELECT count(*) FROM oficina_connections"); n != 0 { |
| 233 | t.Fatalf("the callback saved %d connections", n) |
| 234 | } |
| 235 | var s *ErrStatus |
| 236 | if _, err := ana.a.OficinaFinish(o.ctx, state, code); !errors.As(err, &s) || s.Status != http.StatusForbidden || strings.Contains(string(s.Body), Owner) { |
| 237 | t.Fatalf("another person's finish: %v", err) |
| 238 | } |
| 239 | if status, err := o.a.OficinaFinish(o.ctx, state, code); err != nil || status != "expired" { |
| 240 | t.Errorf("after another person tried: %q %v", status, err) |
| 241 | } |
| 242 | if ex, _ := o.fake.Counts(); ex != 0 || o.rows("SELECT count(*) FROM oficina_connections") != 0 { |
| 243 | t.Fatalf("exchanged %d", ex) |
| 244 | } |
| 245 | |
| 246 | state, code = o.handBack(o.base.Origin + "/ask") |
| 247 | if status, err := o.a.OficinaFinish(o.ctx, state, code); err != nil || status != "connected" { |
| 248 | t.Fatalf("finish: %q %v", status, err) |
| 249 | } |
| 250 | if status, err := o.a.OficinaFinish(o.ctx, state, code); err != nil || status != "expired" { |
| 251 | t.Errorf("replayed finish: %q %v", status, err) |
| 252 | } |
| 253 | if status, _, _ := o.anonymous(o.inst.URL + "/oficina/connect/callback?state=" + url.QueryEscape(state) + "&code=" + url.QueryEscape(code) + "&iss=" + url.QueryEscape(o.fake.URL)); status != http.StatusBadRequest { |
| 254 | t.Errorf("a finished state's callback: %d", status) |
| 255 | } |
| 256 | if status, back, page := o.anonymous(o.inst.URL + "/oficina/connect/callback?state=" + strings.Repeat("A", 43) + "&code=x&iss=" + url.QueryEscape(o.fake.URL)); status != http.StatusBadRequest || back != "" || strings.Contains(page, "rst-shell") { |
| 257 | t.Errorf("forged: %d %q", status, back) |
| 258 | } |
| 259 | if ex, _ := o.fake.Counts(); ex != 1 { |
| 260 | t.Errorf("exchanges %d", ex) |
| 261 | } |
| 262 | |
| 263 | // Another issuer: not handed on, and Base hears it expired. |
| 264 | authorize, _ := o.a.OficinaConnect(o.ctx, o.base.Origin+"/ask") |
| 265 | _, callback, _ := o.browse(http.MethodGet, authorize) |
| 266 | u, _ := url.Parse(callback) |
| 267 | q := u.Query() |
| 268 | q.Set("iss", "https://elsewhere.example.com") |
| 269 | u.RawQuery = q.Encode() |
| 270 | if _, back, _ := o.anonymous(u.String()); back != o.base.Origin+"/ask?oficina=expired" { |
| 271 | t.Errorf("another issuer: %q", back) |
| 272 | } |
| 273 | o.fake.Deny = true |
| 274 | if back := o.callback(o.base.Origin + "/ask"); back != o.base.Origin+"/ask?oficina=denied" { |
| 275 | t.Errorf("denied: %q", back) |
| 276 | } |
| 277 | |
| 278 | for _, bad := range []string{"https://evil.example.com/ask", strings.Replace(o.base.Origin, "127.0.0.1", "localhost", 1) + "/ask", "/ask", o.base.Origin + "/ask#x", "", "javascript:alert(1)"} { |
| 279 | if _, err := o.a.OficinaConnect(o.ctx, bad); !errors.As(err, &s) || s.Status != 422 { |
| 280 | t.Errorf("return to %q: %v", bad, err) |
| 281 | } |
| 282 | } |
| 283 | // A cookie is not the bearer these routes take. |
| 284 | for _, path := range []string{"/api/v1/assistant/oficina/connect", "/api/v1/assistant/oficina/connect/finish"} { |
| 285 | req, _ := http.NewRequest(http.MethodPost, o.inst.URL+path, strings.NewReader(`{"return_to":"`+o.base.Origin+`/ask"}`)) |
| 286 | req.Header.Set("Content-Type", "application/json") |
| 287 | req.Header.Set("Origin", o.inst.URL) |
| 288 | req.AddCookie(o.inst.Cookie()) |
| 289 | if res, err := http.DefaultClient.Do(req); err != nil || res.StatusCode/100 == 2 { |
| 290 | t.Errorf("%s: cookie accepted: %v", path, err) |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // TestContractAssistantOficinaFromEquipo: a person who uses Equipo |
| 296 | // directly connects through its own start, behind their session, and |
| 297 | // lands on Equipo's page; with no Base host the card links there. |
| 298 | func TestContractAssistantOficinaFromEquipo(t *testing.T) { |
| 299 | fake := oficinatest.Start(t) |
| 300 | r := newRigWith(t, func(i *Instance) { |
| 301 | i.OficinaConnect, i.OficinaClientID = fake.URL, fake.ClientID |
| 302 | fake.Redirect = i.URL + "/oficina/connect/callback" |
| 303 | }) |
| 304 | o := &oficinaRig{opsRig: &opsRig{toolRig: &toolRig{rig: r, runner: r.dialAt(wire.ProtocolAssistant)}, a: r.client.Assistant(), protocol: wire.ProtocolAssistant}, fake: fake} |
| 305 | o.begin("Put lunch in my calendar.") |
| 306 | o.tool("l1", wire.ToolLook, map[string]any{"query": "calendar"}) |
| 307 | link := o.connectCards()[0].Links[0] |
| 308 | if link.URL != o.inst.URL+"/oficina/connect/start" || link.Rel != "oficina_connect" { |
| 309 | t.Fatalf("link: %+v", link) |
| 310 | } |
| 311 | if page := o.consent(link.URL); !strings.Contains(page, "Your Oficina apps are connected.") { |
| 312 | t.Errorf("page: %s", page) |
| 313 | } |
| 314 | if _, to, _ := o.anonymous(o.inst.URL + "/oficina/connect/start"); strings.HasPrefix(to, fake.URL) { |
| 315 | t.Error("start without a session") |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // TestContractAssistantOficinaUse: through use, a read and a write |
| 320 | // reach connect with a current bearer (refreshed, rotated, as each |
| 321 | // expires) and the person's one team; the write's client_id is a UUIDv5 |
| 322 | // of its ledger key, the same when a failed call is made again, and a |
| 323 | // repeat is replayed. Destructive, external and unkeyed writes never |
| 324 | // reach connect. Nothing stored or audited carries a token. |
| 325 | func TestContractAssistantOficinaUse(t *testing.T) { |
| 326 | o := newOficinaRig(t) |
| 327 | o.fake.AccessTTL = time.Second |
| 328 | o.begin("Book lunch.") |
| 329 | o.tool("l1", wire.ToolLook, map[string]any{"query": "calendar"}) |
| 330 | o.viaBase() |
| 331 | |
| 332 | d := o.tool("d1", wire.ToolDescribe, map[string]any{"operation": "calendar_create_event"}) |
| 333 | if d.IsError || strings.Contains(d.Text, "client_id") || strings.Contains(d.Text, `"teams"`) { |
| 334 | t.Fatalf("describe: %+v", d) |
| 335 | } |
| 336 | if res := o.use("r1", "calendar_free_busy", map[string]any{"start": "2026-11-20T00:00:00Z", "end": "2026-11-21T00:00:00Z"}); res.IsError || res.Code != "used" { |
| 337 | t.Fatalf("read: %+v", res) |
| 338 | } |
| 339 | o.fake.FailNext = true |
| 340 | lunch := map[string]any{"title": "Lunch with Ana", "start_local": "2026-11-20T13:00"} |
| 341 | if res := o.use("w1", "calendar_create_event", lunch); !res.IsError || res.Code != "unavailable" { |
| 342 | t.Fatalf("failed write: %+v", res) |
| 343 | } |
| 344 | if res := o.use("w2", "calendar_create_event", lunch); res.IsError || res.Code != "used" || res.Replayed { |
| 345 | t.Fatalf("write: %+v", res) |
| 346 | } |
| 347 | if res := o.use("w3", "calendar_create_event", lunch); res.IsError || !res.Replayed { |
| 348 | t.Fatalf("repeat: %+v", res) |
| 349 | } |
| 350 | for op, code := range map[string]string{"docs_delete": "not_yet", "calls_invite": "not_yet", "docs_append": "write_not_supported"} { |
| 351 | if res := o.use("x-"+op, op, map[string]any{"id": "d1", "email": "guest@example.com", "text": "hi"}); !res.IsError || res.Code != code { |
| 352 | t.Errorf("%s: %+v", op, res) |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | calls := o.fake.Calls() |
| 357 | if len(calls) != 3 || calls[0].Name != "calendar_free_busy" || calls[1].Name != "calendar_create_event" || calls[2].Name != "calendar_create_event" { |
| 358 | t.Fatalf("calls: %+v", calls) |
| 359 | } |
| 360 | key, _ := calls[1].Input["client_id"].(string) |
| 361 | if !uuidV5.MatchString(key) || calls[2].Input["client_id"] != key { |
| 362 | t.Errorf("client_id %q then %v", key, calls[2].Input["client_id"]) |
| 363 | } |
| 364 | for _, c := range calls { |
| 365 | if c.Input["team"] != "acme" || c.Bearer == "" { |
| 366 | t.Errorf("call %s: %+v", c.Name, c) |
| 367 | } |
| 368 | } |
| 369 | if _, refreshes := o.fake.Counts(); refreshes == 0 { |
| 370 | t.Error("expired access tokens were not refreshed") |
| 371 | } |
| 372 | |
| 373 | // The refresh token is stored sealed; neither it nor any access |
| 374 | // token is in the audit or the logs. |
| 375 | var sealed string |
| 376 | if err := o.inst.DB.Writer().QueryRow("SELECT refresh FROM oficina_connections WHERE person = ?", Owner).Scan(&sealed); err != nil { |
| 377 | t.Fatal(err) |
| 378 | } |
| 379 | if o.fake.Live(sealed) { |
| 380 | t.Error("the refresh token is stored as it is") |
| 381 | } |
| 382 | logs := o.inst.Logs() |
| 383 | for _, c := range calls { |
| 384 | if n := o.rows("SELECT count(*) FROM audit_events WHERE record_id LIKE ?", "%"+c.Bearer+"%"); n != 0 || strings.Contains(logs, c.Bearer) { |
| 385 | t.Error("an access token in the audit or the logs") |
| 386 | } |
| 387 | } |
| 388 | if n := o.rows("SELECT count(*) FROM audit_events WHERE kind = 'oficina.connect' AND actor = ? AND record_id = ''", Owner); n != 1 { |
| 389 | t.Errorf("connect audits %d", n) |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | // TestContractAssistantOficinaDisconnect: the person's disconnect |
| 394 | // revokes the connection at connect and forgets it; the next look for |
| 395 | // the apps raises a connect card again. |
| 396 | func TestContractAssistantOficinaDisconnect(t *testing.T) { |
| 397 | o := newOficinaRig(t) |
| 398 | o.begin("What is on Friday?") |
| 399 | o.tool("l1", wire.ToolLook, map[string]any{"query": "calendar"}) |
| 400 | o.viaBase() |
| 401 | if res := o.use("r1", "calendar_free_busy", map[string]any{"start": "a", "end": "b"}); res.IsError { |
| 402 | t.Fatalf("read: %+v", res) |
| 403 | } |
| 404 | var sealed string |
| 405 | if err := o.inst.DB.Writer().QueryRow("SELECT refresh FROM oficina_connections WHERE person = ?", Owner).Scan(&sealed); err != nil { |
| 406 | t.Fatal(err) |
| 407 | } |
| 408 | |
| 409 | status, _, page := o.browse(http.MethodPost, o.inst.URL+"/oficina/connect/disconnect") |
| 410 | if status != http.StatusOK || !strings.Contains(page, "Your Oficina apps are disconnected.") { |
| 411 | t.Fatalf("disconnect: %d %s", status, page) |
| 412 | } |
| 413 | if revoked := o.fake.Revoked(); len(revoked) != 1 || o.fake.Live(revoked[0]) { |
| 414 | t.Errorf("revoked %v", revoked) |
| 415 | } |
| 416 | if n := o.rows("SELECT count(*) FROM oficina_connections"); n != 0 { |
| 417 | t.Errorf("%d connections kept", n) |
| 418 | } |
| 419 | if n := o.rows("SELECT count(*) FROM audit_events WHERE kind = 'oficina.disconnect' AND actor = ?", Owner); n != 1 { |
| 420 | t.Errorf("disconnect audits %d", n) |
| 421 | } |
| 422 | if res := o.tool("l2", wire.ToolLook, map[string]any{"query": "calendar"}); !strings.Contains(res.Text, "not connected") { |
| 423 | t.Errorf("look after disconnecting: %+v", res) |
| 424 | } |
| 425 | if res := o.use("r2", "calendar_free_busy", map[string]any{"start": "a", "end": "b"}); !res.IsError { |
| 426 | t.Errorf("use after disconnecting: %+v", res) |
| 427 | } |
| 428 | cards := o.connectCards() |
| 429 | if len(cards) != 2 || cards[0].State == cards[1].State { |
| 430 | t.Errorf("cards after disconnecting: %+v", cards) |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | // TestContractAssistantOficinaOffByDefault: without |
| 435 | // EQUIPO_OFICINA_CONNECT_ORIGIN nothing changes: no hint, no card, no |
| 436 | // routes. |
| 437 | func TestContractAssistantOficinaOffByDefault(t *testing.T) { |
| 438 | o := newOpsRig(t) |
| 439 | o.begin("Put lunch in my calendar.") |
| 440 | if res := o.tool("l1", wire.ToolLook, map[string]any{"query": "calendar"}); strings.Contains(res.Text, "oficina") { |
| 441 | t.Errorf("look: %+v", res) |
| 442 | } |
| 443 | snap, err := o.a.Snapshot(o.ctx) |
| 444 | if err != nil { |
| 445 | t.Fatal(err) |
| 446 | } |
| 447 | var s *ErrStatus |
| 448 | if _, err := o.a.OficinaConnect(o.ctx, o.base.Origin+"/ask"); !errors.As(err, &s) || s.Status/100 == 2 { |
| 449 | t.Errorf("connect API: %v", err) |
| 450 | } |
| 451 | if slices.ContainsFunc(snap.Cards, func(c wire.AssistantCard) bool { return c.Kind == wire.CardConnect }) { |
| 452 | t.Errorf("a connect card: %+v", snap.Cards) |
| 453 | } |
| 454 | for _, path := range []string{"/oficina/connect/start", "/oficina/connect/callback"} { |
| 455 | req, _ := http.NewRequest(http.MethodGet, o.inst.URL+path, nil) |
| 456 | req.AddCookie(o.inst.Cookie()) |
| 457 | res, err := (&http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}).Do(req) |
| 458 | if err != nil { |
| 459 | t.Fatal(err) |
| 460 | } |
| 461 | res.Body.Close() |
| 462 | if res.StatusCode != http.StatusNotFound { |
| 463 | t.Errorf("%s: %d", path, res.StatusCode) |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | // TestContractAssistantOficinaSettleChecksRoster: a person removed from |
| 469 | // the roster has their connect card settled by nothing, in the same |
| 470 | // transaction that would settle it. |
| 471 | func TestContractAssistantOficinaSettleChecksRoster(t *testing.T) { |
| 472 | o := newOficinaRig(t) |
| 473 | ana := o.other("ana@example.com") |
| 474 | ana.connect(o.base.Mint()) |
| 475 | ana.begin("Put lunch in my calendar.") |
| 476 | ana.tool("l1", wire.ToolLook, map[string]any{"query": "calendar"}) |
| 477 | state := func() string { |
| 478 | var s string |
| 479 | if err := o.inst.DB.Writer().QueryRow("SELECT state FROM chat_cards WHERE person = ? AND kind = ?", "ana@example.com", wire.CardConnect).Scan(&s); err != nil { |
| 480 | t.Fatal(err) |
| 481 | } |
| 482 | return s |
| 483 | } |
| 484 | if state() != wire.CardOpen { |
| 485 | t.Fatal("no open card") |
| 486 | } |
| 487 | m, err := o.inst.Store.Member(o.ctx, "ana@example.com") |
| 488 | if err != nil { |
| 489 | t.Fatal(err) |
| 490 | } |
| 491 | if err := o.inst.Store.SetMember(o.ctx, equipo.Actor{Address: Owner}, "ana@example.com", equipo.RoleOwner, false, m.Revision); err != nil { |
| 492 | t.Fatal(err) |
| 493 | } |
| 494 | if err := o.inst.Store.Chat().SettleConnect(o.ctx, "ana@example.com", "oficina"); err == nil { |
| 495 | t.Error("settled for a removed member") |
| 496 | } |
| 497 | if s := state(); s != wire.CardOpen { |
| 498 | t.Errorf("card %s", s) |
| 499 | } |
| 500 | if n := o.rows("SELECT count(*) FROM chat_turns WHERE person = ? AND kind = ?", "ana@example.com", wire.TurnTeamUpdate); n != 0 { |
| 501 | t.Errorf("%d turns queued for a removed member", n) |
| 502 | } |
| 503 | } |
| 504 | |