package webtest import ( "context" "encoding/json" "net/http" "strings" "testing" "amadan.net/oficina/base/internal/equipo/equipotest" "amadan.net/oficina/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) }