oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package idtest
2
3// The contract between estilo's door and rastrillo's screen, as Equipo
4// draws it.
5
6import (
7 "fmt"
8 "html/template"
9 "net/http"
10 "regexp"
11 "strings"
12 "testing"
13
14 "amadan.net/oficina/equipo/internal/equipo/door"
15 "amadan.net/oficina/estilo.git/chrome"
16 "amadan.net/oficina/estilo.git/sky"
17 "amadan.net/rastrillo/rastrillo/ui"
18)
19
20// estilo-signin-screen over rastrillo's real states: HideBrand drops the
21// partial's brand column (estilo's door draws Equipo's mark and name
22// around the card), so [estilo-signin-screen] > section has the door as
23// its only element child in every state, both modes, the refusal and the
24// confirm step included.
25func TestDoorScreenHoldsOnlyTheDoor(t *testing.T) {
26 t.Parallel()
27 modes(t, func(t *testing.T, f *fixture, home bool) {
28 _, record := f.emailRecord(t, address)
29 paths := []string{"/signin", "/signin?ways=1", "/signin?way=email", "/signin?err=expired", "/signin?sent=1", "/signin?reauth=1", "/signin?force=1"}
30 if home {
31 paths = append(paths, "/signin/local", "/signin?way=oficina")
32 }
33 for _, p := range paths {
34 for _, jar := range []*http.Cookie{nil, record} {
35 w := f.request("GET", p, "", nil, jar)
36 if w.Code != http.StatusOK {
37 t.Fatalf("GET %s: %d", p, w.Code)
38 }
39 screenOnlyHoldsTheDoor(t, p, w.Body.String())
40 }
41 }
42 f.remove(t, address)
43 screenOnlyHoldsTheDoor(t, "refusal", f.request("GET", "/signin", "", nil, f.local(t, address)).Body.String())
44 })
45}
46
47func screenOnlyHoldsTheDoor(t *testing.T, name, body string) {
48 t.Helper()
49 children, err := screenChildren(body)
50 if err != nil {
51 t.Fatal(name, err)
52 }
53 if len(children) != 1 || !strings.HasPrefix(children[0], "<div rst-signin-door") {
54 t.Fatalf("%s: [estilo-signin-screen] > section has %d element children %q, want only the door", name, len(children), children)
55 }
56}
57
58var startTag = regexp.MustCompile(`<(/?)([a-zA-Z][a-zA-Z0-9-]*)([^>]*)>`)
59
60var 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}
61
62// screenChildren is the start tags of the element children of
63// [estilo-signin-screen] > section, which must be rastrillo's.
64func screenChildren(body string) ([]string, error) {
65 at := strings.Index(body, "<div estilo-signin-screen>")
66 if at < 0 {
67 return nil, fmt.Errorf("no [estilo-signin-screen]")
68 }
69 rest := body[at+len("<div estilo-signin-screen>"):]
70 tags := startTag.FindAllStringSubmatchIndex(rest, -1)
71 if len(tags) == 0 {
72 return nil, fmt.Errorf("[estilo-signin-screen] is empty")
73 }
74 if first := rest[tags[0][0]:tags[0][1]]; strings.TrimSpace(rest[:tags[0][0]]) != "" || !strings.HasPrefix(first, "<section rst-signin") {
75 return nil, fmt.Errorf("[estilo-signin-screen]'s first child is not rastrillo's section: %q", first)
76 }
77 depth := 0
78 var children []string
79 for _, m := range tags[1:] {
80 closing, name, tag := rest[m[2]:m[3]] == "/", strings.ToLower(rest[m[4]:m[5]]), rest[m[0]:m[1]]
81 if closing {
82 if depth == 0 {
83 if name != "section" {
84 return nil, fmt.Errorf("unbalanced markup: %q closes the section", tag)
85 }
86 return children, nil
87 }
88 depth--
89 continue
90 }
91 if depth == 0 {
92 children = append(children, tag)
93 }
94 if !voidElements[name] && !strings.HasSuffix(tag, "/>") {
95 depth++
96 }
97 }
98 return nil, fmt.Errorf("the section never closes")
99}
100
101// The check sees what HideBrand removes: a brand column beside the door.
102func TestScreenChildrenSeesABrandColumn(t *testing.T) {
103 branded := `<div estilo-signin-screen>
104<section rst-signin aria-labelledby="rst-signin-heading">
105<div rst-signin-brand><div rst-signin-mark><img src="x" alt=""></div><p rst-signin-name>Equipo</p></div>
106<div rst-signin-door="one-way"><h1>Sign in to Equipo</h1><input name="address"><br></div>
107</section>
108</div>`
109 children, err := screenChildren(branded)
110 if err != nil || len(children) != 2 || !strings.HasPrefix(children[0], "<div rst-signin-brand") {
111 t.Fatalf("children %q, %v: want the brand column and the door", children, err)
112 }
113}
114
115// Without Oficina ID's sign-out, Equipo's signed-out card is estilo's
116// estilo-signed-out, byte for byte: Equipo adds that one link and nothing
117// else.
118func TestDoorSignedOutIsEstilos(t *testing.T) {
119 t.Parallel()
120 modes(t, func(t *testing.T, f *fixture, home bool) {
121 var hidden []chrome.Hidden
122 if home {
123 hidden = []chrome.Hidden{{Name: "chain", Value: "oficina"}}
124 }
125 out := chrome.SignInNotice{Icon: door.Icon(), App: "Equipo", Sky: sky.For("equipo.example")}.SignedOut("/signin").Forgetting("/auth/forget", "", hidden...)
126 tpl := template.Must(template.Must(template.New("estilo").Funcs(ui.Funcs()).ParseFS(ui.Templates(), "*.html")).ParseFS(chrome.Templates(), "templates/*.html"))
127 var want strings.Builder
128 if err := tpl.ExecuteTemplate(&want, "estilo-signed-out", out); err != nil {
129 t.Fatal(err)
130 }
131 page := f.request("GET", "/signed-out", "", nil).Body.String()
132 if !strings.Contains(page, want.String()) {
133 t.Fatalf("the signed-out card is not estilo's:\n%s\nwant within:\n%s", page, want.String())
134 }
135 })
136}
137