| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | rdb "amadan.net/rastrillo/rastrillo/db" |
| 12 | "amadan.net/rastrillo/rastrillo/migrate" |
| 13 | ) |
| 14 | |
| 15 | // The sent state replaces the email field (spec §9.2), rather than a |
| 16 | // notice above a still-primary button that invites a second send, and |
| 17 | // keeps "Use a different email" as its way back. |
| 18 | func TestSigninSentReplacesTheForm(t *testing.T) { |
| 19 | app, _ := appWithRecorder(t) |
| 20 | body := page(t, app, nil, "/signin?sent=1").Body.String() |
| 21 | card := door(t, body) |
| 22 | if !strings.Contains(card, "Check your email") || titleOf(t, body) != "Check your email to sign in to Sheets" { |
| 23 | t.Fatalf("the sent state is not the Check your email page:\n%s", card) |
| 24 | } |
| 25 | if strings.Contains(card, `id="rst-signin-email" name="address"`) { |
| 26 | t.Fatal("the sent state still shows the email field") |
| 27 | } |
| 28 | if !strings.Contains(card, "Use a different email") { |
| 29 | t.Fatal("the sent state has no way back to the field") |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // TestSigninPageDoesNotEchoQuery: rastrillo selects a problem from a fixed |
| 34 | // set rather than echoing, so there is no path from the URL into the HTML. |
| 35 | func TestSigninPageDoesNotEchoQuery(t *testing.T) { |
| 36 | app, _ := appWithRecorder(t) |
| 37 | rec := page(t, app, nil, "/signin?err=%3Cscript%3Ealert(1)%3C/script%3E") |
| 38 | if rec.Code != http.StatusOK { |
| 39 | t.Fatalf("GET /signin = %d", rec.Code) |
| 40 | } |
| 41 | if strings.Contains(rec.Body.String(), "<script>alert") { |
| 42 | t.Error("query value reached the page") |
| 43 | } |
| 44 | if card := door(t, rec.Body.String()); !strings.Contains(card, "Something went wrong") || !strings.Contains(card, `id="rst-signin-email" name="address"`) { |
| 45 | t.Errorf("unknown err code did not get the fallback problem and the field:\n%s", card) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // testAuth builds a real *auth.Auth over a temp database, so route |
| 50 | // mounting is proven against the actual handlers rather than a stub. |
| 51 | func testAuth(t *testing.T) *App { |
| 52 | t.Helper() |
| 53 | d, err := rdb.Open(filepath.Join(t.TempDir(), "test.db"), quietLogger()) |
| 54 | if err != nil { |
| 55 | t.Fatalf("open db: %v", err) |
| 56 | } |
| 57 | t.Cleanup(func() { d.Close() }) |
| 58 | if _, err := migrate.Apply(context.Background(), d, BootSchema); err != nil { |
| 59 | t.Fatalf("migrate: %v", err) |
| 60 | } |
| 61 | app := New(Config{DB: d.G, Admit: Admits([]string{"paul@example.com"})}) |
| 62 | a, err := NewAuth(d.Writer(), AuthEnv{ |
| 63 | Origin: "https://sheets.example.com", |
| 64 | InstanceKey: "test-instance-key", |
| 65 | Owners: []string{"paul@example.com"}, |
| 66 | Mailer: nil, // auth falls back to a logging sender; no mail is sent in this test |
| 67 | }, quietLogger(), app) |
| 68 | if err != nil { |
| 69 | t.Fatalf("NewAuth: %v", err) |
| 70 | } |
| 71 | t.Cleanup(func() { CloseSigninMail(a) }) |
| 72 | app.UseAuth(a) |
| 73 | return app |
| 74 | } |
| 75 | |
| 76 | // TestAuthRoutesAreMounted proves auth's handlers are reachable — Verify |
| 77 | // on POST as well as GET, or the confirm door's button gets a 405. It |
| 78 | // exists because Router mounts them behind `if a.auth != nil`, and every |
| 79 | // other test in this package passes nil — so without this, the whole |
| 80 | // sign-in surface could be unmounted and the suite would stay green. |
| 81 | func TestAuthRoutesAreMounted(t *testing.T) { |
| 82 | app := testAuth(t) |
| 83 | for _, tc := range []struct{ method, path string }{ |
| 84 | {http.MethodPost, "/signin"}, |
| 85 | {http.MethodGet, "/auth/callback"}, |
| 86 | {http.MethodGet, "/auth/verify"}, |
| 87 | {http.MethodPost, "/auth/verify"}, |
| 88 | {http.MethodPost, "/signout"}, |
| 89 | } { |
| 90 | rec := httptest.NewRecorder() |
| 91 | app.Router().ServeHTTP(rec, httptest.NewRequest(tc.method, tc.path, nil)) |
| 92 | if rec.Code == http.StatusNotFound || rec.Code == http.StatusMethodNotAllowed { |
| 93 | t.Errorf("%s %s = %d, handler not mounted", tc.method, tc.path, rec.Code) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // TestAuthRoutesAbsentWithoutAuth is the control for the test above: it |
| 99 | // pins that those codes really do mean "not mounted", so the assertion |
| 100 | // there is measuring what it claims to. |
| 101 | func TestAuthRoutesAbsentWithoutAuth(t *testing.T) { |
| 102 | app := New(Config{}) |
| 103 | for _, tc := range []struct{ method, path string }{ |
| 104 | {http.MethodPost, "/signin"}, |
| 105 | {http.MethodGet, "/auth/verify"}, |
| 106 | } { |
| 107 | rec := httptest.NewRecorder() |
| 108 | app.Router().ServeHTTP(rec, httptest.NewRequest(tc.method, tc.path, nil)) |
| 109 | if rec.Code != http.StatusNotFound && rec.Code != http.StatusMethodNotAllowed { |
| 110 | t.Errorf("%s %s = %d with nil auth, want 404/405", tc.method, tc.path, rec.Code) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // TestSigninCarriesForce: when auth redirects a failed keymail sign-in |
| 116 | // to ?force=1&err=keymail, the page must post force back, or Begin |
| 117 | // bounces the user to keymail again and the loop has no exit. |
| 118 | func TestSigninCarriesForce(t *testing.T) { |
| 119 | app, _ := appWithRecorder(t) |
| 120 | with := page(t, app, nil, "/signin?force=1&err=keymail").Body.String() |
| 121 | if !strings.Contains(with, `name="force" value="1"`) { |
| 122 | t.Error("force=1 page does not carry the force field") |
| 123 | } |
| 124 | if without := page(t, app, nil, "/signin").Body.String(); strings.Contains(without, `name="force"`) { |
| 125 | t.Error("ordinary page carries a force field it was not given") |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Without auth (tests only) there is no door to draw. |
| 130 | func TestSigninWithoutAuthIsNotFound(t *testing.T) { |
| 131 | if rec := do(t, New(Config{}), httptest.NewRequest(http.MethodGet, "/signin", nil)); rec.Code != http.StatusNotFound { |
| 132 | t.Fatalf("GET /signin with nil auth = %d, want 404", rec.Code) |
| 133 | } |
| 134 | } |
| 135 | |