oficina / memoria Public

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

Plain git — no account needed to clone.

Download

Download this file

1package memoria
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 "time"
13
14 "amadan.net/oficina/home/connect"
15 "amadan.net/rastrillo/rastrillo/lastsignin"
16)
17
18// The contract estilo's review asked for: estilo-signin-screen over
19// rastrillo's real sign-in states. HideBrand drops the partial's brand
20// column (estilo's door draws Memoria's mark and name around the card),
21// so [estilo-signin-screen] > section has the door as its only element
22// child, in every state, both modes, the refusal, the removal and the
23// confirm step included.
24func TestSmartDoorScreenHoldsOnlyTheDoor(t *testing.T) {
25 f := newHomeRouterFixture(t)
26 oficina := &lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"}
27 email := &lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "bob@example.test"}
28 for _, c := range []struct {
29 name, path string
30 record *lastsignin.Record
31 }{
32 {"first visit", "/signin", nil},
33 {"Oficina ID remembered", "/signin", oficina},
34 {"email remembered", "/signin", email},
35 {"keymail remembered", "/signin", &lastsignin.Record{Method: lastsignin.MethodKeymail, Address: "bob@example.test"}},
36 {"the chooser", "/signin?ways=1", email},
37 {"problem", "/signin?err=expired", email},
38 {"the removal", "/signin?err=removed", nil},
39 {"sent", "/signin?sent=1", email},
40 {"step-up", "/signin?reauth=1", oficina},
41 {"email only", "/signin/local", nil},
42 } {
43 t.Run(c.name, func(t *testing.T) {
44 br := f.fresh()
45 if c.record != nil {
46 br.remember(t, *c.record)
47 }
48 rec := br.get(c.path)
49 if rec.Code != http.StatusOK {
50 t.Fatalf("GET %s: %d", c.path, rec.Code)
51 }
52 screenOnlyHoldsTheDoor(t, rec.Body.String())
53 })
54 }
55 // The refusal (a session the signed-in routes refuse), standalone.
56 refused := newStandaloneDoor(t)
57 refused.session(t, "opaque-home-person", connect.HomeMethod, time.Now())
58 body := refused.get("/signin").Body.String()
59 if !strings.Contains(body, signinRefusal) {
60 t.Fatal("the refusal case drew no refusal")
61 }
62 screenOnlyHoldsTheDoor(t, body)
63 // The confirm step an emailed link opens (auth's RenderConfirm).
64 br := f.fresh()
65 f.mail.body = ""
66 br.do(http.MethodPost, "/auth/begin", url.Values{"address": {"alice@example.test"}, "force": {"1"}}.Encode())
67 link := signinLink.FindString(f.mail.body)
68 confirm := br.get(strings.TrimPrefix(link, homeTestOrigin))
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>Memoria</p></div>
143<div rst-signin-door="one-way"><h1>Sign in to Memoria</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