| 1 | package idtest |
| 2 | |
| 3 | // Every route and state of the sign-in door, standalone and with Home. |
| 4 | |
| 5 | import ( |
| 6 | "html" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "regexp" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func TestDoorRoutesAndStates(t *testing.T) { |
| 15 | t.Parallel() |
| 16 | modes(t, func(t *testing.T, f *fixture, home bool) { |
| 17 | for _, c := range []struct { |
| 18 | path, title string |
| 19 | oficina bool // offers Continue with Oficina ID, as the one way or a row |
| 20 | has []string |
| 21 | }{ |
| 22 | {"/signin", "Sign in to Equipo", home, nil}, |
| 23 | {"/signin?return_to=%2Fwork", "Sign in to Equipo", home, nil}, |
| 24 | {"/signin?ways=1", "Other ways to sign in to Equipo", false, nil}, |
| 25 | {"/signin?way=email", "Sign in to Equipo", false, []string{`name="address"`}}, |
| 26 | {"/signin?err=expired", "Problem signing in to Equipo", false, []string{`name="address"`}}, |
| 27 | {"/signin?force=1", "Sign in to Equipo", false, []string{`name="address"`}}, |
| 28 | {"/signin?sent=1", "Check your email to sign in to Equipo", false, nil}, |
| 29 | {"/signin?reauth=1", "Sign in to Equipo", false, nil}, |
| 30 | } { |
| 31 | w := f.request("GET", c.path, "", nil) |
| 32 | body := w.Body.String() |
| 33 | if w.Code != http.StatusOK || title(body) != c.title { |
| 34 | t.Errorf("%s: %d %q, want 200 %q", c.path, w.Code, title(body), c.title) |
| 35 | } |
| 36 | if got := strings.Contains(body, `href="/signin/oficina`); got != c.oficina { |
| 37 | t.Errorf("%s: Continue with Oficina ID %v, want %v", c.path, got, c.oficina) |
| 38 | } |
| 39 | for _, want := range c.has { |
| 40 | if !strings.Contains(body, want) { |
| 41 | t.Errorf("%s: missing %s", c.path, want) |
| 42 | } |
| 43 | } |
| 44 | if !regexp.MustCompile(`src="/static/rastrillo\.[0-9a-f]+\.js"></script>\n<script defer src="/static/fiesta\.`).MatchString(body) { |
| 45 | t.Errorf("%s: fiesta.js is not linked right after rastrillo.js (rastrillo v0.34)", c.path) |
| 46 | } |
| 47 | if w.Header().Get("Cache-Control") != "no-store" || strings.Contains(body, "rst-shell") { |
| 48 | t.Errorf("%s: cacheable, or drawn with the app's shell", c.path) |
| 49 | } |
| 50 | } |
| 51 | if home { |
| 52 | w := f.request("GET", "/signin?return_to=%2Fwork", "", nil) |
| 53 | if !strings.Contains(w.Body.String(), `href="/signin/oficina?return_to=%2Fwork"`) { |
| 54 | t.Error("Continue with Oficina ID lost the page", w.Body.String()) |
| 55 | } |
| 56 | if ways := f.request("GET", "/signin?ways=1", "", nil).Body.String(); !strings.Contains(ways, `href="/signin?way=email"`) { |
| 57 | t.Error("the chooser lacks Email", ways) |
| 58 | } |
| 59 | if way := f.request("GET", "/signin?way=oficina", "", nil).Body.String(); !strings.Contains(way, `href="/signin/oficina"`) { |
| 60 | t.Error("Oficina ID's own page lacks Continue with Oficina ID") |
| 61 | } |
| 62 | // Once Email is chosen, Other ways lists Oficina ID. |
| 63 | email := f.request("GET", "/signin?way=email", "", nil).Body.String() |
| 64 | other := regexp.MustCompile(`href="(/signin\?[^"]*ways=1[^"]*)"`).FindStringSubmatch(email) |
| 65 | if other == nil { |
| 66 | t.Fatal("no Other ways on the email page", email) |
| 67 | } |
| 68 | if ways := f.request("GET", html.UnescapeString(other[1]), "", nil).Body.String(); !strings.Contains(ways, "Oficina ID") || !strings.Contains(ways, `href="/signin?way=oficina"`) { |
| 69 | t.Error("the chooser lacks Oficina ID", ways) |
| 70 | } |
| 71 | } |
| 72 | }) |
| 73 | } |
| 74 | |
| 75 | // With Home, /signin/local is the door with email as the one way in; and |
| 76 | // /signin/oficina starts Equipo's own exchange. Standalone, neither exists. |
| 77 | func TestDoorEmailOnlyAndOficinaRoutes(t *testing.T) { |
| 78 | t.Parallel() |
| 79 | modes(t, func(t *testing.T, f *fixture, home bool) { |
| 80 | local := f.request("GET", "/signin/local", "", nil) |
| 81 | oficina := f.request("GET", "/signin/oficina", "", nil) |
| 82 | if !home { |
| 83 | if local.Code == http.StatusOK || oficina.Code == http.StatusOK || strings.Contains(oficina.Header().Get("Location"), "/auth/home") { |
| 84 | t.Fatal("standalone door routes answer", local.Code, oficina.Code) |
| 85 | } |
| 86 | return |
| 87 | } |
| 88 | if local.Code != 200 || strings.Contains(local.Body.String(), "Oficina ID") || !strings.Contains(local.Body.String(), `name="address"`) { |
| 89 | t.Error("/signin/local offers Oficina ID, or no email", local.Code) |
| 90 | } |
| 91 | for target, want := range map[string]string{ |
| 92 | "/signin/oficina": "/auth/home?return_to=%2F", |
| 93 | "/signin/oficina?return_to=%2Fwork": "/auth/home?return_to=%2Fwork", |
| 94 | "/signin/oficina?return_to=%2Fsigned-out": "/auth/home?return_to=%2F", |
| 95 | "/signin/oficina?return_to=%2F%2Fevil.example": "/auth/home?return_to=%2F", |
| 96 | "/signin/oficina?return_to=%2Faccount%2Fhome%2Fconfirm": "/auth/home?return_to=%2F", |
| 97 | "/signin/oficina?reauth=1&return_to=%2Fwork": "/signin/local?reauth=1&return_to=%2Fwork", |
| 98 | } { |
| 99 | w := f.request("GET", target, "", nil) |
| 100 | if w.Code != http.StatusSeeOther || w.Header().Get("Location") != want { |
| 101 | t.Errorf("%s: %d %s, want %s", target, w.Code, w.Header().Get("Location"), want) |
| 102 | } |
| 103 | } |
| 104 | // And on through Equipo's exchange to Oficina ID's authorize. |
| 105 | start := f.request("GET", "/auth/home?return_to=%2Fwork", "", nil) |
| 106 | if !strings.HasPrefix(start.Header().Get("Location"), issuer+"/authorize?") { |
| 107 | t.Error("start", start.Header().Get("Location")) |
| 108 | } |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | // A browser already signed in and admitted never sees the door unless the |
| 113 | // query names one of its states. |
| 114 | func TestDoorSignedInGoesOn(t *testing.T) { |
| 115 | t.Parallel() |
| 116 | modes(t, func(t *testing.T, f *fixture, home bool) { |
| 117 | s := f.local(t, address) |
| 118 | for target, want := range map[string]string{ |
| 119 | "/signin": "/", |
| 120 | "/signin?return_to=%2Fwork": "/work", |
| 121 | "/signin?return_to=%2Fsigned-out": "/", |
| 122 | "/signin?return_to=https%3A%2F%2Fevil.example%2F": "/", |
| 123 | } { |
| 124 | w := f.request("GET", target, "", nil, s) |
| 125 | if w.Code != http.StatusSeeOther || w.Header().Get("Location") != want { |
| 126 | t.Errorf("%s: %d %s, want %s", target, w.Code, w.Header().Get("Location"), want) |
| 127 | } |
| 128 | } |
| 129 | for _, state := range []string{"sent=1", "err=expired", "force=1", "reauth=1"} { |
| 130 | if w := f.request("GET", "/signin?"+state, "", nil, s); w.Code != http.StatusOK { |
| 131 | t.Errorf("%s for a signed-in browser: %d, want the door", state, w.Code) |
| 132 | } |
| 133 | } |
| 134 | if home { |
| 135 | if w := f.request("GET", "/signin/oficina?return_to=%2Fwork", "", nil, s); w.Header().Get("Location") != "/work" { |
| 136 | t.Error("/signin/oficina for a signed-in browser", w.Header().Get("Location")) |
| 137 | } |
| 138 | if w := f.request("GET", "/signin/local", "", nil, s); w.Code != http.StatusOK { |
| 139 | t.Error("/signin/local has no signed-in shortcut", w.Code) |
| 140 | } |
| 141 | } |
| 142 | if !f.sessionAlive(t, s) { |
| 143 | t.Fatal("the door ended an admitted session") |
| 144 | } |
| 145 | }) |
| 146 | } |
| 147 | |
| 148 | // A step-up offers local ways only: linking needs a fresh email sign-in. |
| 149 | func TestDoorStepUpIsLocal(t *testing.T) { |
| 150 | t.Parallel() |
| 151 | f := newFixture(t, true) |
| 152 | s := f.local(t, address) |
| 153 | begin := f.request("POST", "/account/home/reauth", f.origin, url.Values{"purpose": {"link"}}, s) |
| 154 | to := begin.Header().Get("Location") |
| 155 | if begin.Code != http.StatusSeeOther || !strings.HasPrefix(to, "/signin/local?") || !strings.Contains(to, "reauth=1") { |
| 156 | t.Fatal("step-up", begin.Code, to) |
| 157 | } |
| 158 | door := f.request("GET", to, "", nil, s).Body.String() |
| 159 | if strings.Contains(door, "Oficina ID") || !strings.Contains(door, `name="address"`) || !strings.Contains(door, `name="reauth"`) { |
| 160 | t.Fatal("the step-up offers Oficina ID, or no email", door) |
| 161 | } |
| 162 | } |
| 163 | |