oficina / calendar Public

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

Plain git — no account needed to clone.

Download

Download this file

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