| 1 | package sheets |
| 2 | |
| 3 | // The contract between estilo's door and rastrillo's screen. |
| 4 | |
| 5 | import ( |
| 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 Sheets' mark and name around the card), so |
| 19 | // [estilo-signin-screen] > section has the door as its only element child, |
| 20 | // in every state. |
| 21 | func TestSmartDoorScreenHoldsOnlyTheDoor(t *testing.T) { |
| 22 | f := newHomeFixture(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 | {"sent", "/signin?sent=1", email}, |
| 35 | {"step-up", "/signin?reauth=1", oficina}, |
| 36 | {"email only", "/signin/local", nil}, |
| 37 | } { |
| 38 | t.Run(c.name, func(t *testing.T) { |
| 39 | b := f.browser() |
| 40 | if c.record != nil { |
| 41 | b.remember(t, *c.record) |
| 42 | } |
| 43 | rec := b.request(t, "GET", c.path, nil, false) |
| 44 | if rec.Code != http.StatusOK { |
| 45 | t.Fatalf("GET %s: %d", c.path, rec.Code) |
| 46 | } |
| 47 | screenOnlyHoldsTheDoor(t, rec.Body.String()) |
| 48 | }) |
| 49 | } |
| 50 | // And the confirm step an emailed link opens (auth's RenderConfirm). |
| 51 | b := f.browser() |
| 52 | b.request(t, "POST", "/signin", url.Values{"address": {signedInAddress}}, false) |
| 53 | u, _ := url.Parse(f.app.testMailer(t).lastLink(t)) |
| 54 | confirm := b.request(t, "GET", u.RequestURI(), nil, false) |
| 55 | if confirm.Code != http.StatusOK { |
| 56 | t.Fatalf("confirm: %d", confirm.Code) |
| 57 | } |
| 58 | screenOnlyHoldsTheDoor(t, confirm.Body.String()) |
| 59 | } |
| 60 | |
| 61 | // startTag is one start or end tag. |
| 62 | var startTag = regexp.MustCompile(`<(/?)([a-zA-Z][a-zA-Z0-9-]*)([^>]*)>`) |
| 63 | |
| 64 | // voidElements have no end tag. |
| 65 | var 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} |
| 66 | |
| 67 | // screenOnlyHoldsTheDoor checks that the first section inside |
| 68 | // [estilo-signin-screen] is rastrillo's [rst-signin] and that its only |
| 69 | // element child is the door, [rst-signin-door]. |
| 70 | func screenOnlyHoldsTheDoor(t *testing.T, body string) { |
| 71 | t.Helper() |
| 72 | children, err := screenChildren(body) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | if len(children) != 1 || !strings.HasPrefix(children[0], "<div rst-signin-door") { |
| 77 | t.Fatalf("[estilo-signin-screen] > section has %d element children %q, want only the door", len(children), children) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // screenChildren is the start tags of the element children of |
| 82 | // [estilo-signin-screen] > section, which must be rastrillo's. |
| 83 | func screenChildren(body string) ([]string, error) { |
| 84 | at := strings.Index(body, "<div estilo-signin-screen>") |
| 85 | if at < 0 { |
| 86 | return nil, fmt.Errorf("no [estilo-signin-screen]") |
| 87 | } |
| 88 | rest := body[at+len("<div estilo-signin-screen>"):] |
| 89 | tags := startTag.FindAllStringSubmatchIndex(rest, -1) |
| 90 | if len(tags) == 0 { |
| 91 | return nil, fmt.Errorf("[estilo-signin-screen] is empty") |
| 92 | } |
| 93 | first := rest[tags[0][0]:tags[0][1]] |
| 94 | if strings.TrimSpace(rest[:tags[0][0]]) != "" || !strings.HasPrefix(first, "<section rst-signin") { |
| 95 | return nil, fmt.Errorf("[estilo-signin-screen]'s first child is not rastrillo's section: %q", first) |
| 96 | } |
| 97 | depth := 0 |
| 98 | var children []string |
| 99 | for _, m := range tags[1:] { |
| 100 | closing, name, tag := rest[m[2]:m[3]] == "/", strings.ToLower(rest[m[4]:m[5]]), rest[m[0]:m[1]] |
| 101 | if closing { |
| 102 | if depth == 0 { |
| 103 | if name != "section" { |
| 104 | return nil, fmt.Errorf("unbalanced markup: %q closes the section", tag) |
| 105 | } |
| 106 | return children, nil |
| 107 | } |
| 108 | depth-- |
| 109 | continue |
| 110 | } |
| 111 | if depth == 0 { |
| 112 | children = append(children, tag) |
| 113 | } |
| 114 | if !voidElements[name] && !strings.HasSuffix(tag, "/>") { |
| 115 | depth++ |
| 116 | } |
| 117 | } |
| 118 | return nil, fmt.Errorf("the section never closes") |
| 119 | } |
| 120 | |
| 121 | // The check sees what HideBrand removes: a brand column beside the door, |
| 122 | // as the partial draws without it, is a second child. |
| 123 | func TestScreenChildrenSeesABrandColumn(t *testing.T) { |
| 124 | branded := `<div estilo-signin-screen> |
| 125 | <section rst-signin aria-labelledby="rst-signin-heading"> |
| 126 | <div rst-signin-brand><div rst-signin-mark><img src="x" alt=""></div><p rst-signin-name>Sheets</p></div> |
| 127 | <div rst-signin-door="one-way"><h1>Sign in to Sheets</h1><input name="address"><br></div> |
| 128 | </section> |
| 129 | </div>` |
| 130 | children, err := screenChildren(branded) |
| 131 | if err != nil || len(children) != 2 || !strings.HasPrefix(children[0], "<div rst-signin-brand") { |
| 132 | t.Fatalf("children %q, %v: want the brand column and the door", children, err) |
| 133 | } |
| 134 | } |
| 135 | |