//go:build browser package calls // The smart door in a real browser: every state at 320px and 1280px, // forced to light (test/browser/door.mjs). Not part of `make ci`; `make // browser` runs it, after its npm install. // // go test -tags browser -run TestSmartDoorInABrowser ./internal/calls/ // // DOOR_SHOTS= keeps a screenshot of every state. import ( "bufio" "bytes" "context" "encoding/json" "net/http" "net/http/httptest" "os" "os/exec" "path/filepath" "strconv" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/lastsignin" ) // doorState is one state the browser opens: a path on the door, the // cookies the browser holds for it, and whether Continue with Oficina ID // is its one way in. type doorState struct { Name string `json:"name"` Path string `json:"path"` Glyph bool `json:"glyph"` jar []*http.Cookie want string } func TestSmartDoorInABrowser(t *testing.T) { f := newHomeRouterFixture(t) long := "a.very.long.remembered.name.with.dots@subdomain.example-organisation.test" jarFor := func(rec *lastsignin.Record) []*http.Cookie { br := f.fresh() if rec != nil { br.remember(t, *rec) } var out []*http.Cookie for _, c := range br.cookies { out = append(out, c) } return out } // The join cookie's door: an invitation to a meeting that needs an // emailed sign-in. joining := f.fresh() _, token := doorMeeting(t, joining, true) joining.get("/j/" + token) var joinJar []*http.Cookie for _, c := range joining.cookies { joinJar = append(joinJar, c) } // The confirm step an emailed link opens. f.mail.body = "" f.fresh().do(http.MethodPost, "/signin", "address=alice%40example.test") confirm := strings.TrimPrefix(signinLink.FindString(f.mailBody()), homeTestOrigin) oficina := &lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"} states := []doorState{ {Name: "first visit", Path: "/signin", Glyph: true, want: "Continue with Oficina ID"}, {Name: "Oficina ID remembered", Path: "/signin", Glyph: true, jar: jarFor(oficina), want: "Continue with Oficina ID"}, {Name: "a long remembered email", Path: "/signin", jar: jarFor(&lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: long}), want: "Continue as"}, {Name: "a long remembered Keymail", Path: "/signin", jar: jarFor(&lastsignin.Record{Method: lastsignin.MethodKeymail, Address: long}), want: "Continue as"}, {Name: "the chooser", Path: "/signin?ways=1", jar: jarFor(&lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: long}), want: "Other ways to sign in"}, {Name: "the email way", Path: "/signin?way=email", want: "Email"}, {Name: "email only", Path: "/signin/local", want: "Email"}, {Name: "a problem", Path: "/signin?err=expired", want: "expired"}, {Name: "a bad address", Path: "/signin?err=address", want: "look like an email address"}, {Name: "a step-up", Path: "/signin?reauth=1", want: "Sign in again to continue."}, {Name: "sent", Path: "/signin?sent=1", want: "Check your email"}, {Name: "the removal", Path: "/signin?err=removed", want: "Your access has been removed"}, {Name: "the refusal", Path: "/__refusal", want: "That email can’t sign in to Calls."}, {Name: "the join cookie", Path: "/signin", jar: joinJar, want: "Email"}, {Name: "the join cookie's other ways", Path: "/signin?ways=1&way=email", jar: joinJar, want: "Oficina ID"}, {Name: "confirm", Path: confirm, want: "Sign in"}, {Name: "signed out", Path: "/signed-out", want: "Forget me on this device"}, } byIndex := map[string]doorState{} for i := range states { k := strconv.Itoa(i) byIndex[k] = states[i] if states[i].Path != "/__refusal" { sep := "?" if strings.Contains(states[i].Path, "?") { sep = "&" } states[i].Path += sep + "__door=" + k } } // Each state's cookies are added on the server, named by its own query // parameter (taken off before the app sees it), so the browser needs // none of the door's __Host- cookies itself. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // The instance's own host: on a bare IP rastrillo reads the // request as the platform's direct host, where the door answers // 404 (the hub signs people in there). r.Host = strings.TrimPrefix(homeTestOrigin, "https://") if r.URL.Path == "/__refusal" { r.URL.Path = "/auth/callback" f.app.refused(w, r, rauthIdentity("someone@else.test")) return } if q := r.URL.Query(); q.Has("__door") { k := q.Get("__door") q.Del("__door") r.URL.RawQuery = q.Encode() r.RequestURI = r.URL.RequestURI() for _, c := range byIndex[k].jar { r.AddCookie(&http.Cookie{Name: c.Name, Value: c.Value}) } } f.handler.ServeHTTP(w, r) })) defer srv.Close() root, err := filepath.Abs(filepath.Join("..", "..")) if err != nil { t.Fatal(err) } cfg, _ := json.Marshal(map[string]any{"base": srv.URL, "states": states, "shots": os.Getenv("DOOR_SHOTS")}) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) defer cancel() cmd := exec.CommandContext(ctx, "node", "test/browser/door.mjs", string(cfg)) cmd.Dir = root var stderr bytes.Buffer cmd.Stderr = &stderr out, err := cmd.Output() if err != nil { t.Fatalf("node: %v\n%s", err, stderr.String()) } seen := 0 sc := bufio.NewScanner(bytes.NewReader(out)) sc.Buffer(make([]byte, 1<<20), 1<<20) for sc.Scan() { var got struct { Name string `json:"name"` Width int `json:"width"` Status int `json:"status"` Scroll int `json:"scroll"` Small []string `json:"small"` Oficina int `json:"oficina"` Glyph bool `json:"glyph"` Text string `json:"text"` } if err := json.Unmarshal(sc.Bytes(), &got); err != nil { t.Fatalf("node said %q", sc.Text()) } seen++ var state doorState for _, s := range states { if s.Name == got.Name { state = s } } where := got.Name + " at " + strconv.Itoa(got.Width) + "px" if got.Status != http.StatusOK && !(got.Name == "the refusal" && got.Status == http.StatusForbidden) { t.Errorf("%s: status %d", where, got.Status) } if got.Scroll > 0 { t.Errorf("%s scrolls sideways by %dpx", where, got.Scroll) } if len(got.Small) > 0 { t.Errorf("%s: targets under 24px: %v", where, got.Small) } if state.Glyph && !got.Glyph { t.Errorf("%s: Continue with Oficina ID draws no glyph", where) } if !strings.Contains(got.Text, state.want) { t.Errorf("%s: the card does not say %q:\n%s", where, state.want, got.Text) } } if seen != 2*len(states) { t.Fatalf("node checked %d states, want %d\n%s", seen, 2*len(states), stderr.String()) } }