| 1 | package webtest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/base/internal/equipo/equipotest" |
| 11 | "amadan.net/oficina/base/internal/store" |
| 12 | ) |
| 13 | |
| 14 | // Disconnect is the member's, not a device's: once they disconnect Ask |
| 15 | // anywhere, no app connects it again by itself. Only an explicit connect |
| 16 | // (the web's Connect, or the app's set-up when the person asks) does. |
| 17 | |
| 18 | // connectAuto posts what the app posts when it sets Ask up by itself. |
| 19 | func (a *testApp) connectAuto(f *equipotest.Fake, subject string) Resp { |
| 20 | a.t.Helper() |
| 21 | assertion, verifier := f.OficinaAssertion(subject) |
| 22 | body, _ := json.Marshal(map[string]any{"assertion": assertion, "verifier": verifier, "auto": true}) |
| 23 | return a.nativePost("/api/v1/ask/oficina", string(body)) |
| 24 | } |
| 25 | |
| 26 | // askDisconnected is /ask/state's "disconnected". |
| 27 | func (a *testApp) askDisconnected() bool { |
| 28 | a.t.Helper() |
| 29 | _, r := a.askState() |
| 30 | var s struct { |
| 31 | Disconnected *bool `json:"disconnected"` |
| 32 | } |
| 33 | if err := json.Unmarshal([]byte(r.Body), &s); err != nil || s.Disconnected == nil { |
| 34 | a.t.Fatalf("no disconnected in /ask/state: %s", r.Body) |
| 35 | } |
| 36 | return *s.Disconnected |
| 37 | } |
| 38 | |
| 39 | // storedDisconnected is the flag as stored, read afresh: what a restarted |
| 40 | // Base reads. |
| 41 | func (a *testApp) storedDisconnected(email string) bool { |
| 42 | a.t.Helper() |
| 43 | m, ok, err := store.MemberByEmail(context.Background(), a.DB, email) |
| 44 | if err != nil || !ok { |
| 45 | a.t.Fatalf("member %s: %v", email, err) |
| 46 | } |
| 47 | return m.AskDisconnected |
| 48 | } |
| 49 | |
| 50 | func TestDisconnectOnTheWebStopsAutomaticSetUp(t *testing.T) { |
| 51 | web, app, f := newNativeAskApp(t) |
| 52 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 53 | if r := app.connectAuto(f, "subject-owner"); r.Code != 200 { |
| 54 | t.Fatalf("automatic set-up: %d %s", r.Code, r.Body) |
| 55 | } |
| 56 | if app.askDisconnected() { |
| 57 | t.Fatal("disconnected before any Disconnect") |
| 58 | } |
| 59 | if r := web.post("/ask/disconnect", nil); r.Code != 303 { |
| 60 | t.Fatalf("disconnect: %d", r.Code) |
| 61 | } |
| 62 | if !app.askDisconnected() || !app.storedDisconnected(ownerAddress) { |
| 63 | t.Fatal("Disconnect did not stick") |
| 64 | } |
| 65 | exchanges := len(f.Calls("/native/oficina-id")) |
| 66 | wantBody(t, "automatic set-up after Disconnect", app.connectAuto(f, "subject-owner"), 409, `{"state":"disconnected"}`) |
| 67 | if len(f.Calls("/native/oficina-id")) != exchanges || app.equipoLinks() != 0 { |
| 68 | t.Fatalf("an automatic set-up after Disconnect reached Equipo or stored a link") |
| 69 | } |
| 70 | if s, _ := app.askState(); s.State != "connect" { |
| 71 | t.Errorf("state: %s", s.State) |
| 72 | } |
| 73 | // The person asks for it: the explicit set-up connects and clears it. |
| 74 | if r := app.connectWithOficina(f, "subject-owner"); r.Code != 200 { |
| 75 | t.Fatalf("explicit set-up: %d %s", r.Code, r.Body) |
| 76 | } |
| 77 | if app.askDisconnected() || app.storedDisconnected(ownerAddress) { |
| 78 | t.Fatal("an explicit connect left the flag set") |
| 79 | } |
| 80 | if r := app.connectAuto(f, "subject-owner"); r.Code != 200 { |
| 81 | t.Fatalf("automatic renewal after reconnecting: %d %s", r.Code, r.Body) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // "auto": false is explicit. |
| 86 | func TestExplicitSetUpSaysSo(t *testing.T) { |
| 87 | _, app, f := newNativeAskApp(t) |
| 88 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 89 | app.connectWithOficina(f, "subject-owner") |
| 90 | app.post("/ask/disconnect", nil) |
| 91 | assertion, verifier := f.OficinaAssertion("subject-owner") |
| 92 | body, _ := json.Marshal(map[string]any{"assertion": assertion, "verifier": verifier, "auto": false}) |
| 93 | if r := app.nativePost("/api/v1/ask/oficina", string(body)); r.Code != 200 { |
| 94 | t.Fatalf("auto false: %d %s", r.Code, r.Body) |
| 95 | } |
| 96 | // Only absent, true or false: anything else is refused, whatever it |
| 97 | // might have meant, so no client undoes a Disconnect by accident. |
| 98 | app.post("/ask/disconnect", nil) |
| 99 | for _, auto := range []string{`"auto":null`, `"auto":"true"`, `"auto":1`, `"auto":{}`, `"auto":true,"auto":false`, `"auto":false,"auto":true`, `"AUTO":true`, `"Auto":false`} { |
| 100 | a, v := f.OficinaAssertion("subject-owner") |
| 101 | body := `{"assertion":"` + a + `","verifier":"` + v + `",` + auto + `}` |
| 102 | wantBody(t, auto, app.nativePost("/api/v1/ask/oficina", body), 400, `{"state":"invalid"}`) |
| 103 | } |
| 104 | if !app.storedDisconnected(ownerAddress) || app.equipoLinks() != 0 { |
| 105 | t.Fatal("a refused body connected Ask") |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // The web's Connect clears the flag once the connection is made (its code |
| 110 | // approved), not when the code is shown; an explicit set-up that fails |
| 111 | // leaves the flag as it was. |
| 112 | func TestWebConnectClearsDisconnected(t *testing.T) { |
| 113 | web, app, f := newNativeAskApp(t) |
| 114 | web.approveEquipo(f) |
| 115 | web.post("/ask/disconnect", nil) |
| 116 | f.FailNextNativeExchange(503) |
| 117 | if r := app.connectWithOficina(f, "subject-owner"); r.Code != 502 { |
| 118 | t.Fatalf("explicit set-up with Equipo down: %d %s", r.Code, r.Body) |
| 119 | } |
| 120 | if !app.askDisconnected() { |
| 121 | t.Fatal("a failed explicit set-up cleared the flag") |
| 122 | } |
| 123 | code := web.startWebConnect() |
| 124 | if !app.askDisconnected() || !app.storedDisconnected(ownerAddress) { |
| 125 | t.Fatal("pressing Connect cleared the flag before anything was connected") |
| 126 | } |
| 127 | if r := web.approveAndCheck(f, code); r.Code != 303 || r.Location() != "/settings" { |
| 128 | t.Fatalf("approval: %d %q", r.Code, r.Location()) |
| 129 | } |
| 130 | if app.askDisconnected() || app.storedDisconnected(ownerAddress) { |
| 131 | t.Fatal("the web's approved connection left the flag set") |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Fable's P4: a code that is never approved (it expires, or Equipo refuses |
| 136 | // it) leaves the member disconnected, and the app does not connect them. |
| 137 | func TestUnapprovedWebCodeLeavesDisconnected(t *testing.T) { |
| 138 | for _, end := range []string{"expired", "refused"} { |
| 139 | t.Run(end, func(t *testing.T) { |
| 140 | web, app, f := newNativeAskApp(t) |
| 141 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 142 | web.approveEquipo(f) |
| 143 | web.post("/ask/disconnect", nil) |
| 144 | web.startWebConnect() |
| 145 | if end == "expired" { |
| 146 | f.Expire() |
| 147 | } else { |
| 148 | f.RevokeAll() |
| 149 | } |
| 150 | web.pollable() |
| 151 | web.check("settings") |
| 152 | if s, _ := app.askState(); s.State != "connect" || !app.askDisconnected() { |
| 153 | t.Fatalf("after the code %s: state %s, disconnected %v", end, s.State, app.askDisconnected()) |
| 154 | } |
| 155 | wantBody(t, "automatic set-up", app.connectAuto(f, "subject-owner"), 409, `{"state":"disconnected"}`) |
| 156 | wantLive(t, f) |
| 157 | }) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Fable's P4b: while the code waits, the app's automatic set-up does not |
| 162 | // overtake it. |
| 163 | func TestAutomaticSetUpLeavesAWaitingCode(t *testing.T) { |
| 164 | web, app, f := newNativeAskApp(t) |
| 165 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 166 | web.approveEquipo(f) |
| 167 | web.post("/ask/disconnect", nil) |
| 168 | code := web.startWebConnect() |
| 169 | wantBody(t, "automatic set-up while a code waits", app.connectAuto(f, "subject-owner"), 409, `{"state":"disconnected"}`) |
| 170 | if s, _ := app.askState(); s.State != "pending" { |
| 171 | t.Fatalf("the waiting code went: %s", s.State) |
| 172 | } |
| 173 | if r := web.approveAndCheck(f, code); r.Code != 303 || r.Location() != "/settings" { |
| 174 | t.Fatalf("approval: %d %q", r.Code, r.Location()) |
| 175 | } |
| 176 | if app.askDisconnected() || len(f.LiveCredentials()) != 1 { |
| 177 | t.Fatalf("after approval: disconnected %v, live %v", app.askDisconnected(), f.LiveCredentials()) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // An automatic set-up waiting at Equipo when Disconnect lands: overtaken, |
| 182 | // nothing live, the flag stored, and the next automatic set-up refused. |
| 183 | func TestAutomaticSetUpOvertakenByDisconnect(t *testing.T) { |
| 184 | _, app, f := newNativeAskApp(t) |
| 185 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 186 | if r := app.connectAuto(f, "subject-owner"); r.Code != 200 { |
| 187 | t.Fatalf("connect: %d %s", r.Code, r.Body) |
| 188 | } |
| 189 | entered, release := f.HoldNextNativeExchange() |
| 190 | done := background(func() Resp { return app.connectAuto(f, "subject-owner") }) |
| 191 | arrived(t, entered, "the automatic set-up") |
| 192 | if r := app.post("/ask/disconnect", nil); r.Code != 303 { |
| 193 | t.Fatalf("disconnect: %d", r.Code) |
| 194 | } |
| 195 | release() |
| 196 | wantBody(t, "overtaken automatic set-up", <-done, 409, `{"state":"superseded"}`) |
| 197 | if n := app.equipoLinks(); n != 0 || !app.storedDisconnected(ownerAddress) { |
| 198 | t.Fatalf("links %d, flag %v", n, app.storedDisconnected(ownerAddress)) |
| 199 | } |
| 200 | wantLive(t, f) |
| 201 | wantBody(t, "next automatic set-up", app.connectAuto(f, "subject-owner"), 409, `{"state":"disconnected"}`) |
| 202 | } |
| 203 | |
| 204 | // The web's approval landing after Disconnect stores nothing and leaves |
| 205 | // the flag set. |
| 206 | func TestLateWebApprovalAfterDisconnect(t *testing.T) { |
| 207 | web, app, f := newNativeAskApp(t) |
| 208 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 209 | code := web.startWebConnect() |
| 210 | if err := f.Approve(code); err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | web.pollable() |
| 214 | entered, release := f.HoldNextExchange() |
| 215 | checked := background(func() Resp { return web.check("settings") }) |
| 216 | arrived(t, entered, "the web's exchange") |
| 217 | if r := app.post("/ask/disconnect", nil); r.Code != 303 { |
| 218 | t.Fatalf("disconnect: %d", r.Code) |
| 219 | } |
| 220 | release() |
| 221 | <-checked |
| 222 | if n := app.equipoLinks(); n != 0 || !app.storedDisconnected(ownerAddress) { |
| 223 | t.Fatalf("links %d, flag %v", n, app.storedDisconnected(ownerAddress)) |
| 224 | } |
| 225 | wantLive(t, f) |
| 226 | } |
| 227 | |
| 228 | func TestDisconnectedIsPerMember(t *testing.T) { |
| 229 | web, app, f := newNativeAskApp(t) |
| 230 | if _, _, err := store.Invite(context.Background(), web.DB, "ana@acme.test"); err != nil { |
| 231 | t.Fatal(err) |
| 232 | } |
| 233 | web.newBrowser().link("ana@acme.test", "subject-ana") |
| 234 | ana := web.nativeSignIn("subject-ana") |
| 235 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 236 | f.LinkOficinaID("subject-ana", "ana@acme.test") |
| 237 | app.connectWithOficina(f, "subject-owner") |
| 238 | if r := app.post("/ask/disconnect", nil); r.Code != 303 { |
| 239 | t.Fatalf("disconnect from the app: %d", r.Code) |
| 240 | } |
| 241 | if ana.askDisconnected() || web.storedDisconnected("ana@acme.test") { |
| 242 | t.Fatal("the owner's Disconnect reached Ana") |
| 243 | } |
| 244 | if r := ana.connectAuto(f, "subject-ana"); r.Code != 200 { |
| 245 | t.Fatalf("Ana's automatic set-up: %d %s", r.Code, r.Body) |
| 246 | } |
| 247 | wantBody(t, "the owner's", app.connectAuto(f, "subject-owner"), 409, `{"state":"disconnected"}`) |
| 248 | } |
| 249 | |
| 250 | // A Disconnect that fails as it sets the flag, after the link and host |
| 251 | // token were deleted, undoes all three: one transaction. |
| 252 | func TestFailedDisconnectLeavesNoFlag(t *testing.T) { |
| 253 | web, app, f := newNativeAskApp(t) |
| 254 | web.approveEquipo(f) |
| 255 | if web.hostRows() != 1 { |
| 256 | t.Fatalf("host tokens: %d", web.hostRows()) |
| 257 | } |
| 258 | if _, err := web.DB.Exec(`CREATE TRIGGER keep_flag BEFORE UPDATE OF ask_disconnected ON members BEGIN SELECT RAISE(ABORT, 'injected'); END`); err != nil { |
| 259 | t.Fatal(err) |
| 260 | } |
| 261 | if r := web.post("/ask/disconnect", nil); r.Code == 303 { |
| 262 | t.Fatal("Disconnect reported success") |
| 263 | } |
| 264 | if app.askDisconnected() || web.storedDisconnected(ownerAddress) { |
| 265 | t.Fatal("a failed Disconnect set the flag") |
| 266 | } |
| 267 | if web.equipoLinks() != 1 || web.hostRows() != 1 { |
| 268 | t.Fatalf("a failed Disconnect removed the link (%d) or host token (%d)", web.equipoLinks(), web.hostRows()) |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // The web shows what it always showed after Disconnect: the connect state. |
| 273 | func TestWebAfterDisconnectShowsConnect(t *testing.T) { |
| 274 | web, _, f := newNativeAskApp(t) |
| 275 | web.approveEquipo(f) |
| 276 | web.post("/ask/disconnect", nil) |
| 277 | page := web.get("/ask") |
| 278 | if page.Code != 200 || !strings.Contains(page.Body, `action="/ask/connect"`) { |
| 279 | t.Fatalf("/ask after Disconnect: %d\n%s", page.Code, page.Body) |
| 280 | } |
| 281 | if r := web.get("/settings"); r.Code != http.StatusOK || !strings.Contains(r.Body, `action="/ask/connect"`) { |
| 282 | t.Fatalf("Settings after Disconnect: %d", r.Code) |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // The save's own transaction checks the flag, not only this process's |
| 287 | // generation: a Disconnect another Base process stored while an automatic |
| 288 | // set-up waited at Equipo (the generation here unmoved) still wins. |
| 289 | func TestAutomaticSaveChecksTheStoredFlag(t *testing.T) { |
| 290 | _, app, f := newNativeAskApp(t) |
| 291 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 292 | entered, release := f.HoldNextNativeExchange() |
| 293 | done := background(func() Resp { return app.connectAuto(f, "subject-owner") }) |
| 294 | arrived(t, entered, "the automatic set-up") |
| 295 | if _, err := app.DB.Exec(`UPDATE members SET ask_disconnected = 1 WHERE email = ?`, ownerAddress); err != nil { |
| 296 | t.Fatal(err) |
| 297 | } |
| 298 | release() |
| 299 | wantBody(t, "automatic set-up", <-done, 409, `{"state":"disconnected"}`) |
| 300 | if n := app.equipoLinks(); n != 0 || !app.storedDisconnected(ownerAddress) { |
| 301 | t.Fatalf("links %d, flag %v", n, app.storedDisconnected(ownerAddress)) |
| 302 | } |
| 303 | wantLive(t, f) |
| 304 | } |
| 305 | |