oficina / meet Public

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

Plain git — no account needed to clone.

Download

Download this file

1package calls
2
3// The contract between estilo's door and rastrillo's screen.
4
5import (
6 "fmt"
7 "net/http"
8 "regexp"
9 "strings"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/home/connect"
14 "amadan.net/rastrillo/rastrillo/lastsignin"
15)
16
17// The contract estilo's review asked for: estilo-signin-screen over
18// rastrillo's real sign-in states. HideBrand drops the partial's brand
19// column (estilo's door draws Calls' mark and name around the card), so
20// [estilo-signin-screen] > section has the door as its only element child,
21// in every state, both modes, the refusal, the removal, the join cookie's
22// door and the confirm step included.
23func TestSmartDoorScreenHoldsOnlyTheDoor(t *testing.T) {
24 f := newHomeRouterFixture(t)
25 oficina := &lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"}
26 email := &lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "bob@example.test"}
27 for _, c := range []struct {
28 name, path string
29 record *lastsignin.Record
30 }{
31 {"first visit", "/signin", nil},
32 {"Oficina ID remembered", "/signin", oficina},
33 {"email remembered", "/signin", email},
34 {"keymail remembered", "/signin", &lastsignin.Record{Method: lastsignin.MethodKeymail, Address: "bob@example.test"}},
35 {"the chooser", "/signin?ways=1", email},
36 {"problem", "/signin?err=expired", email},
37 {"the removal", "/signin?err=removed", nil},
38 {"sent", "/signin?sent=1", email},
39 {"step-up", "/signin?reauth=1", oficina},
40 {"email only", "/signin/local", nil},
41 } {
42 t.Run(c.name, func(t *testing.T) {
43 br := f.fresh()
44 if c.record != nil {
45 br.remember(t, *c.record)
46 }
47 rec := br.get(c.path)
48 if rec.Code != http.StatusOK {
49 t.Fatalf("GET %s: %d", c.path, rec.Code)
50 }
51 screenOnlyHoldsTheDoor(t, rec.Body.String())
52 })
53 }
54 // The refusal (a session the signed-in routes refuse), standalone.
55 refused := newStandaloneDoor(t)
56 refused.session(t, "opaque-home-person", connect.HomeMethod, time.Now())
57 body := refused.get("/signin").Body.String()
58 if !strings.Contains(body, signinRefusal) {
59 t.Fatal("the refusal case drew no refusal")
60 }
61 screenOnlyHoldsTheDoor(t, body)
62 // The join cookie's email-only door.
63 joining := f.fresh()
64 _, token := doorMeeting(t, joining, true)
65 joining.get("/j/" + token)
66 screenOnlyHoldsTheDoor(t, joining.get("/signin").Body.String())
67 // The confirm step an emailed link opens (auth's RenderConfirm).
68 br := f.fresh()
69 f.mail.body = ""
70 br.do(http.MethodPost, "/signin", "address=alice%40example.test")
71 link := signinLink.FindString(f.mailBody())
72 confirm := br.get(strings.TrimPrefix(link, homeTestOrigin))
73 if confirm.Code != http.StatusOK {
74 t.Fatalf("confirm: %d", confirm.Code)
75 }
76 screenOnlyHoldsTheDoor(t, confirm.Body.String())
77 // Standalone, too.
78 screenOnlyHoldsTheDoor(t, newStandaloneDoor(t).get("/signin").Body.String())
79}
80
81// startTag is one start or end tag.
82var startTag = regexp.MustCompile(`<(/?)([a-zA-Z][a-zA-Z0-9-]*)([^>]*)>`)
83
84// voidElements have no end tag.
85var voidElements = map[string]bool{"area": true, "base": true, "br": true, "col": true, "embed": true, "hr": true, "img": true, "input": true, "link": true, "meta": true, "source": true, "track": true, "wbr": true}
86
87// screenOnlyHoldsTheDoor checks that the first section inside
88// [estilo-signin-screen] is rastrillo's [rst-signin] and that its only
89// element child is the door, [rst-signin-door].
90func screenOnlyHoldsTheDoor(t *testing.T, body string) {
91 t.Helper()
92 children, err := screenChildren(body)
93 if err != nil {
94 t.Fatal(err)
95 }
96 if len(children) != 1 || !strings.HasPrefix(children[0], "<div rst-signin-door") {
97 t.Fatalf("[estilo-signin-screen] > section has %d element children %q, want only the door", len(children), children)
98 }
99}
100
101// screenChildren is the start tags of the element children of
102// [estilo-signin-screen] > section, which must be rastrillo's.
103func screenChildren(body string) ([]string, error) {
104 at := strings.Index(body, "<div estilo-signin-screen>")
105 if at < 0 {
106 return nil, fmt.Errorf("no [estilo-signin-screen]")
107 }
108 rest := body[at+len("<div estilo-signin-screen>"):]
109 tags := startTag.FindAllStringSubmatchIndex(rest, -1)
110 if len(tags) == 0 {
111 return nil, fmt.Errorf("[estilo-signin-screen] is empty")
112 }
113 first := rest[tags[0][0]:tags[0][1]]
114 if strings.TrimSpace(rest[:tags[0][0]]) != "" || !strings.HasPrefix(first, "<section rst-signin") {
115 return nil, fmt.Errorf("[estilo-signin-screen]'s first child is not rastrillo's section: %q", first)
116 }
117 depth := 0
118 var children []string
119 for _, m := range tags[1:] {
120 closing, name, tag := rest[m[2]:m[3]] == "/", strings.ToLower(rest[m[4]:m[5]]), rest[m[0]:m[1]]
121 if closing {
122 if depth == 0 {
123 if name != "section" {
124 return nil, fmt.Errorf("unbalanced markup: %q closes the section", tag)
125 }
126 return children, nil
127 }
128 depth--
129 continue
130 }
131 if depth == 0 {
132 children = append(children, tag)
133 }
134 if !voidElements[name] && !strings.HasSuffix(tag, "/>") {
135 depth++
136 }
137 }
138 return nil, fmt.Errorf("the section never closes")
139}
140
141// The check sees what HideBrand removes: a brand column beside the door,
142// as the partial draws without it, is a second child.
143func TestScreenChildrenSeesABrandColumn(t *testing.T) {
144 branded := `<div estilo-signin-screen>
145<section rst-signin aria-labelledby="rst-signin-heading">
146<div rst-signin-brand><div rst-signin-mark><img src="x" alt=""></div><p rst-signin-name>Calls</p></div>
147<div rst-signin-door="one-way"><h1>Sign in to Calls</h1><input name="address"><br></div>
148</section>
149</div>`
150 children, err := screenChildren(branded)
151 if err != nil || len(children) != 2 || !strings.HasPrefix(children[0], "<div rst-signin-brand") {
152 t.Fatalf("children %q, %v: want the brand column and the door", children, err)
153 }
154}
155