package idtest // The contract between estilo's door and rastrillo's screen, as Equipo // draws it. import ( "fmt" "html/template" "net/http" "regexp" "strings" "testing" "amadan.net/oficina/equipo/internal/equipo/door" "amadan.net/oficina/estilo.git/chrome" "amadan.net/oficina/estilo.git/sky" "amadan.net/rastrillo/rastrillo/ui" ) // estilo-signin-screen over rastrillo's real states: HideBrand drops the // partial's brand column (estilo's door draws Equipo's mark and name // around the card), so [estilo-signin-screen] > section has the door as // its only element child in every state, both modes, the refusal and the // confirm step included. func TestDoorScreenHoldsOnlyTheDoor(t *testing.T) { t.Parallel() modes(t, func(t *testing.T, f *fixture, home bool) { _, record := f.emailRecord(t, address) paths := []string{"/signin", "/signin?ways=1", "/signin?way=email", "/signin?err=expired", "/signin?sent=1", "/signin?reauth=1", "/signin?force=1"} if home { paths = append(paths, "/signin/local", "/signin?way=oficina") } for _, p := range paths { for _, jar := range []*http.Cookie{nil, record} { w := f.request("GET", p, "", nil, jar) if w.Code != http.StatusOK { t.Fatalf("GET %s: %d", p, w.Code) } screenOnlyHoldsTheDoor(t, p, w.Body.String()) } } f.remove(t, address) screenOnlyHoldsTheDoor(t, "refusal", f.request("GET", "/signin", "", nil, f.local(t, address)).Body.String()) }) } func screenOnlyHoldsTheDoor(t *testing.T, name, body string) { t.Helper() children, err := screenChildren(body) if err != nil { t.Fatal(name, err) } if len(children) != 1 || !strings.HasPrefix(children[0], "
section has %d element children %q, want only the door", name, len(children), children) } } var startTag = regexp.MustCompile(`<(/?)([a-zA-Z][a-zA-Z0-9-]*)([^>]*)>`) 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} // screenChildren is the start tags of the element children of // [estilo-signin-screen] > section, which must be rastrillo's. func screenChildren(body string) ([]string, error) { at := strings.Index(body, "
") if at < 0 { return nil, fmt.Errorf("no [estilo-signin-screen]") } rest := body[at+len("
"):] tags := startTag.FindAllStringSubmatchIndex(rest, -1) if len(tags) == 0 { return nil, fmt.Errorf("[estilo-signin-screen] is empty") } if first := rest[tags[0][0]:tags[0][1]]; strings.TrimSpace(rest[:tags[0][0]]) != "" || !strings.HasPrefix(first, "
") { depth++ } } return nil, fmt.Errorf("the section never closes") } // The check sees what HideBrand removes: a brand column beside the door. func TestScreenChildrenSeesABrandColumn(t *testing.T) { branded := `

Equipo

Sign in to Equipo


` children, err := screenChildren(branded) if err != nil || len(children) != 2 || !strings.HasPrefix(children[0], "