oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package calls
4
5// The smart door in a real browser: every state at 320px and 1280px,
6// forced to light (test/browser/door.mjs). Not part of `make ci`; `make
7// browser` runs it, after its npm install.
8//
9// go test -tags browser -run TestSmartDoorInABrowser ./internal/calls/
10//
11// DOOR_SHOTS=<dir> keeps a screenshot of every state.
12
13import (
14 "bufio"
15 "bytes"
16 "context"
17 "encoding/json"
18 "net/http"
19 "net/http/httptest"
20 "os"
21 "os/exec"
22 "path/filepath"
23 "strconv"
24 "strings"
25 "testing"
26 "time"
27
28 "amadan.net/rastrillo/rastrillo/lastsignin"
29)
30
31// doorState is one state the browser opens: a path on the door, the
32// cookies the browser holds for it, and whether Continue with Oficina ID
33// is its one way in.
34type doorState struct {
35 Name string `json:"name"`
36 Path string `json:"path"`
37 Glyph bool `json:"glyph"`
38 jar []*http.Cookie
39 want string
40}
41
42func TestSmartDoorInABrowser(t *testing.T) {
43 f := newHomeRouterFixture(t)
44 long := "a.very.long.remembered.name.with.dots@subdomain.example-organisation.test"
45 jarFor := func(rec *lastsignin.Record) []*http.Cookie {
46 br := f.fresh()
47 if rec != nil {
48 br.remember(t, *rec)
49 }
50 var out []*http.Cookie
51 for _, c := range br.cookies {
52 out = append(out, c)
53 }
54 return out
55 }
56 // The join cookie's door: an invitation to a meeting that needs an
57 // emailed sign-in.
58 joining := f.fresh()
59 _, token := doorMeeting(t, joining, true)
60 joining.get("/j/" + token)
61 var joinJar []*http.Cookie
62 for _, c := range joining.cookies {
63 joinJar = append(joinJar, c)
64 }
65 // The confirm step an emailed link opens.
66 f.mail.body = ""
67 f.fresh().do(http.MethodPost, "/signin", "address=alice%40example.test")
68 confirm := strings.TrimPrefix(signinLink.FindString(f.mailBody()), homeTestOrigin)
69 oficina := &lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"}
70 states := []doorState{
71 {Name: "first visit", Path: "/signin", Glyph: true, want: "Continue with Oficina ID"},
72 {Name: "Oficina ID remembered", Path: "/signin", Glyph: true, jar: jarFor(oficina), want: "Continue with Oficina ID"},
73 {Name: "a long remembered email", Path: "/signin", jar: jarFor(&lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: long}), want: "Continue as"},
74 {Name: "a long remembered Keymail", Path: "/signin", jar: jarFor(&lastsignin.Record{Method: lastsignin.MethodKeymail, Address: long}), want: "Continue as"},
75 {Name: "the chooser", Path: "/signin?ways=1", jar: jarFor(&lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: long}), want: "Other ways to sign in"},
76 {Name: "the email way", Path: "/signin?way=email", want: "Email"},
77 {Name: "email only", Path: "/signin/local", want: "Email"},
78 {Name: "a problem", Path: "/signin?err=expired", want: "expired"},
79 {Name: "a bad address", Path: "/signin?err=address", want: "look like an email address"},
80 {Name: "a step-up", Path: "/signin?reauth=1", want: "Sign in again to continue."},
81 {Name: "sent", Path: "/signin?sent=1", want: "Check your email"},
82 {Name: "the removal", Path: "/signin?err=removed", want: "Your access has been removed"},
83 {Name: "the refusal", Path: "/__refusal", want: "That email can’t sign in to Calls."},
84 {Name: "the join cookie", Path: "/signin", jar: joinJar, want: "Email"},
85 {Name: "the join cookie's other ways", Path: "/signin?ways=1&way=email", jar: joinJar, want: "Oficina ID"},
86 {Name: "confirm", Path: confirm, want: "Sign in"},
87 {Name: "signed out", Path: "/signed-out", want: "Forget me on this device"},
88 }
89 byIndex := map[string]doorState{}
90 for i := range states {
91 k := strconv.Itoa(i)
92 byIndex[k] = states[i]
93 if states[i].Path != "/__refusal" {
94 sep := "?"
95 if strings.Contains(states[i].Path, "?") {
96 sep = "&"
97 }
98 states[i].Path += sep + "__door=" + k
99 }
100 }
101 // Each state's cookies are added on the server, named by its own query
102 // parameter (taken off before the app sees it), so the browser needs
103 // none of the door's __Host- cookies itself.
104 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
105 // The instance's own host: on a bare IP rastrillo reads the
106 // request as the platform's direct host, where the door answers
107 // 404 (the hub signs people in there).
108 r.Host = strings.TrimPrefix(homeTestOrigin, "https://")
109 if r.URL.Path == "/__refusal" {
110 r.URL.Path = "/auth/callback"
111 f.app.refused(w, r, rauthIdentity("someone@else.test"))
112 return
113 }
114 if q := r.URL.Query(); q.Has("__door") {
115 k := q.Get("__door")
116 q.Del("__door")
117 r.URL.RawQuery = q.Encode()
118 r.RequestURI = r.URL.RequestURI()
119 for _, c := range byIndex[k].jar {
120 r.AddCookie(&http.Cookie{Name: c.Name, Value: c.Value})
121 }
122 }
123 f.handler.ServeHTTP(w, r)
124 }))
125 defer srv.Close()
126 root, err := filepath.Abs(filepath.Join("..", ".."))
127 if err != nil {
128 t.Fatal(err)
129 }
130 cfg, _ := json.Marshal(map[string]any{"base": srv.URL, "states": states, "shots": os.Getenv("DOOR_SHOTS")})
131 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
132 defer cancel()
133 cmd := exec.CommandContext(ctx, "node", "test/browser/door.mjs", string(cfg))
134 cmd.Dir = root
135 var stderr bytes.Buffer
136 cmd.Stderr = &stderr
137 out, err := cmd.Output()
138 if err != nil {
139 t.Fatalf("node: %v\n%s", err, stderr.String())
140 }
141 seen := 0
142 sc := bufio.NewScanner(bytes.NewReader(out))
143 sc.Buffer(make([]byte, 1<<20), 1<<20)
144 for sc.Scan() {
145 var got struct {
146 Name string `json:"name"`
147 Width int `json:"width"`
148 Status int `json:"status"`
149 Scroll int `json:"scroll"`
150 Small []string `json:"small"`
151 Oficina int `json:"oficina"`
152 Glyph bool `json:"glyph"`
153 Text string `json:"text"`
154 }
155 if err := json.Unmarshal(sc.Bytes(), &got); err != nil {
156 t.Fatalf("node said %q", sc.Text())
157 }
158 seen++
159 var state doorState
160 for _, s := range states {
161 if s.Name == got.Name {
162 state = s
163 }
164 }
165 where := got.Name + " at " + strconv.Itoa(got.Width) + "px"
166 if got.Status != http.StatusOK && !(got.Name == "the refusal" && got.Status == http.StatusForbidden) {
167 t.Errorf("%s: status %d", where, got.Status)
168 }
169 if got.Scroll > 0 {
170 t.Errorf("%s scrolls sideways by %dpx", where, got.Scroll)
171 }
172 if len(got.Small) > 0 {
173 t.Errorf("%s: targets under 24px: %v", where, got.Small)
174 }
175 if state.Glyph && !got.Glyph {
176 t.Errorf("%s: Continue with Oficina ID draws no glyph", where)
177 }
178 if !strings.Contains(got.Text, state.want) {
179 t.Errorf("%s: the card does not say %q:\n%s", where, state.want, got.Text)
180 }
181 }
182 if seen != 2*len(states) {
183 t.Fatalf("node checked %d states, want %d\n%s", seen, 2*len(states), stderr.String())
184 }
185}
186