oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

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