oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "strings"
8 "testing"
9)
10
11// The door's forms post return_to, and beginSignin keeps it Docs' way: in
12// the sealed return memory read at /auth/local/complete, off the form Begin
13// reads (so rastrillo lands the session on localLinkCompletePath, where
14// connect resumes a pending link), and back on Begin's redirect to the door.
15func TestBeginSigninKeepsTheReturnDocsWay(t *testing.T) {
16 app := newAllowlistedTestApp(t, "alice@example.test")
17 for _, tc := range []struct {
18 name, posted, location, want string
19 remembered bool
20 }{
21 {name: "sent", posted: "/d/abc", location: "/signin?sent=1&attempt=x", want: "/signin?attempt=x&return_to=%2Fd%2Fabc&sent=1", remembered: true},
22 {name: "problem", posted: "/d/abc?x=1", location: "/signin?err=rate", want: "/signin?err=rate&return_to=%2Fd%2Fabc%3Fx%3D1", remembered: true},
23 {name: "continuation", posted: "/d/abc", location: "/signin?continue=id", want: "/signin?continue=id&return_to=%2Fd%2Fabc", remembered: true},
24 {name: "already carried", posted: "/d/abc", location: "/signin?err=rate&return_to=%2Fother", want: "/signin?err=rate&return_to=%2Fother", remembered: true},
25 {name: "elsewhere", posted: "/d/abc", location: "/auth/local/complete", want: "/auth/local/complete", remembered: true},
26 // A post without a (safe) destination is a sign-in for the list:
27 // it remembers "/", so another tab's page does not win.
28 {name: "off-site", posted: "//evil.example/x", location: "/signin?sent=1", want: "/signin?sent=1", remembered: true},
29 {name: "none", posted: "", location: "/signin?sent=1", want: "/signin?sent=1", remembered: true},
30 } {
31 t.Run(tc.name, func(t *testing.T) {
32 seen := "unset"
33 next := func(w http.ResponseWriter, r *http.Request) {
34 seen = r.PostFormValue("return_to") + r.FormValue("return_to")
35 http.Redirect(w, r, tc.location, http.StatusSeeOther)
36 }
37 form := url.Values{"address": {"alice@example.test"}}
38 if tc.posted != "" {
39 form.Set("return_to", tc.posted)
40 }
41 req := httptest.NewRequest("POST", "/signin", strings.NewReader(form.Encode()))
42 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
43 rec := httptest.NewRecorder()
44 app.handlers.beginSignin(next)(rec, req)
45 if seen != "" {
46 t.Fatalf("Begin read return_to %q; it must land on /auth/local/complete", seen)
47 }
48 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != tc.want {
49 t.Fatalf("redirect %d %q, want %q", rec.Code, rec.Header().Get("Location"), tc.want)
50 }
51 remembered := false
52 for _, c := range rec.Result().Cookies() {
53 remembered = remembered || c.Name == homeReturnCookie && c.MaxAge > 0
54 }
55 if remembered != tc.remembered {
56 t.Fatalf("return remembered = %v, want %v", remembered, tc.remembered)
57 }
58 })
59 }
60}
61
62func TestSigninEmailFallbackIsExplicit(t *testing.T) {
63 app := newAllowlistedTestApp(t, "alice@example.test")
64 for _, path := range []string{"/signin", "/signin?force=1&err=keymail"} {
65 rec := httptest.NewRecorder()
66 app.Handler().ServeHTTP(rec, httptest.NewRequest("GET", path, nil))
67 if rec.Code != 200 {
68 t.Fatal(rec.Code)
69 }
70 forced := strings.Contains(rec.Body.String(), `name="force" value="1"`)
71 if forced != strings.Contains(path, "force=1") {
72 t.Fatalf("email force for %s = %v", path, forced)
73 }
74 }
75}
76