oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3// The return memory, and the step-up, across the door.
4
5import (
6 "net/http"
7 "net/http/httptest"
8 "net/url"
9 "strings"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/home/connect"
14 "amadan.net/rastrillo/rastrillo/sessions"
15)
16
17// A step-up's reauth=1 survives Begin's answer and Sheets' return_to, for a
18// bad address and for Sent alike: the next door is still local ways only,
19// for the same page.
20func TestAStepUpPostKeepsReauthAndTheReturn(t *testing.T) {
21 f := newHomeFixture(t)
22 b := f.browser()
23 for _, address := range []string{"not an address", signedInAddress} {
24 rec := b.request(t, "POST", "/signin", url.Values{"address": {address}, "reauth": {"1"}, "return_to": {"/sheets/7"}}, false)
25 loc := rec.Header().Get("Location")
26 u, err := url.Parse(loc)
27 if rec.Code != http.StatusSeeOther || err != nil || u.Path != "/signin" || u.Query().Get("reauth") != "1" || u.Query().Get("return_to") != "/sheets/7" {
28 t.Fatalf("%q: %d %q, want the door with reauth=1 and return_to=/sheets/7", address, rec.Code, loc)
29 }
30 card := door(t, b.request(t, "GET", loc, nil, false).Body.String())
31 if strings.Contains(card, "Oficina ID") || !strings.Contains(card, `name="return_to" value="/sheets/7"`) {
32 t.Fatalf("%q: the step-up's next door:\n%s", address, card)
33 }
34 }
35}
36
37// The link step-up (connect's Account, for a local sign-in too old to
38// link) end to end: it lands on the email-only door with reauth=1, the
39// fresh sign-in through it lands back where linking continues, and Oficina
40// ID is never offered on the way.
41func TestTheLinkStepUpEndToEnd(t *testing.T) {
42 f := newHomeFixture(t)
43 if _, err := f.app.memberFor(signedInAddress); err != nil {
44 t.Fatal(err)
45 }
46 token, err := f.app.homeSessions.Mint(sessions.Session{Subject: signedInAddress, Method: "magiclink", AuthTime: time.Now().Add(-time.Hour)})
47 if err != nil {
48 t.Fatal(err)
49 }
50 // A sign-in an hour old is too old to link.
51 f.jar[f.app.homeSessions.CookieName()] = &http.Cookie{Name: f.app.homeSessions.CookieName(), Value: token}
52 start := f.request(t, "GET", "/account/home", nil, false)
53 loc := start.Header().Get("Location")
54 u, _ := url.Parse(loc)
55 if start.Code != http.StatusSeeOther || u.Path != connect.SigninLocalPath || u.Query().Get("reauth") != "1" {
56 t.Fatalf("a stale link start: %d %q, want the email-only door with reauth=1", start.Code, loc)
57 }
58 card := door(t, f.request(t, "GET", loc, nil, false).Body.String())
59 if strings.Contains(card, "Oficina ID") || !strings.Contains(card, "Sign in again to continue.") || !strings.Contains(card, `name="reauth" value="1"`) {
60 t.Fatalf("the step-up door:\n%s", card)
61 }
62 if landed := f.emailSignin(t, url.Values{"address": {signedInAddress}, "reauth": {"1"}}); landed != "/account/home" {
63 t.Fatalf("the fresh sign-in landed on %q, want /account/home to go on linking", landed)
64 }
65}
66
67// The return memory, with Home: a local sign-in begun from a page lands on
68// it after /auth/local/complete, where connect could resume a link; one
69// begun from the plain door lands on the list.
70func TestALocalSigninLandsOnThePageItBeganFrom(t *testing.T) {
71 f := newHomeFixture(t)
72 b := f.browser()
73 card := door(t, b.request(t, "GET", "/signin/local?return_to=%2Fsheets%2F7", nil, false).Body.String())
74 if !strings.Contains(card, `name="return_to" value="/sheets/7"`) {
75 t.Fatalf("the door's form does not post the page:\n%s", card)
76 }
77 if landed := b.emailSignin(t, url.Values{"address": {signedInAddress}, "return_to": {"/sheets/7"}}); landed != "/sheets/7" {
78 t.Fatalf("landed on %q, want /sheets/7", landed)
79 }
80 if _, kept := b.jar[homeReturnCookie]; kept {
81 t.Fatal("the landing kept the remembered page")
82 }
83 c := f.browser()
84 c.request(t, "GET", "/signin", nil, false)
85 if landed := c.emailSignin(t, url.Values{"address": {signedInAddress}}); landed != "/sheets" {
86 t.Fatalf("a plain sign-in landed on %q, want /sheets", landed)
87 }
88}
89
90// A plain /signin/local (connect's failed, unknown and connect pages send
91// people there) keeps the page the sign-in began from: it remembers
92// nothing new, and its form posts the remembered page.
93func TestSigninLocalKeepsTheRememberedPage(t *testing.T) {
94 f := newHomeFixture(t)
95 b := f.browser()
96 b.request(t, "GET", "/signin?return_to=%2Fsheets%2F7", nil, false)
97 kept := b.jar[homeReturnCookie]
98 if kept == nil {
99 t.Fatal("the door did not remember the page")
100 }
101 rec := b.request(t, "GET", "/signin/local", nil, false)
102 for _, c := range rec.Result().Cookies() {
103 if c.Name == homeReturnCookie {
104 t.Fatal("a plain /signin/local overwrote the remembered page")
105 }
106 }
107 if card := door(t, rec.Body.String()); !strings.Contains(card, `name="return_to" value="/sheets/7"`) {
108 t.Fatalf("/signin/local does not carry the remembered page:\n%s", card)
109 }
110 // With its own return_to it remembers that.
111 b.request(t, "GET", "/signin/local?return_to=%2Fsheets%2F9", nil, false)
112 if b.jar[homeReturnCookie] == nil || b.jar[homeReturnCookie].Value == kept.Value {
113 t.Fatal("/signin/local?return_to= did not remember its page")
114 }
115}
116
117// A post with no return_to is a sign-in for the list: a page another tab
118// remembered does not win.
119func TestAPostWithoutAReturnForgetsAnotherTabsPage(t *testing.T) {
120 f := newHomeFixture(t)
121 b := f.browser()
122 b.request(t, "GET", "/signin?return_to=%2Fsheets%2F7", nil, false)
123 if landed := b.emailSignin(t, url.Values{"address": {signedInAddress}}); landed != "/sheets" {
124 t.Fatalf("landed on %q, want /sheets", landed)
125 }
126}
127
128// return_to comes off Begin's form whether or not the body parses: Begin
129// must never read a destination of its own (Fable on Docs #139, A5). The
130// body's last pair fails to parse; the return_to before it is still read.
131func TestReturnToIsTakenOffEvenWhenTheBodyFailsToParse(t *testing.T) {
132 f := newHomeFixture(t)
133 r := httptest.NewRequest(http.MethodPost, testOrigin+"/signin", strings.NewReader("return_to=%2Fsheets%2F7&address=a%zz"))
134 r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
135 r.Header.Set("Origin", testOrigin)
136 w := httptest.NewRecorder()
137 f.app.beginSignin(w, r)
138 if r.PostForm == nil || r.PostForm.Get("return_to") != "" || r.Form.Get("return_to") != "" {
139 t.Fatalf("Begin could read return_to: %v", r.PostForm)
140 }
141 for _, c := range w.Result().Cookies() {
142 if strings.Contains(c.Name, "rastrillo_return_to") && c.MaxAge >= 0 {
143 t.Fatalf("Begin kept a destination: %+v", c)
144 }
145 }
146 if loc := w.Header().Get("Location"); strings.Contains(loc, "return_to") {
147 t.Fatalf("Begin's answer carries a return_to: %q", loc)
148 }
149}
150
151// Standalone, rastrillo carries the page itself: the door's form posts it,
152// and the new session lands there.
153func TestStandaloneSigninLandsOnThePageItBeganFrom(t *testing.T) {
154 app, mail := appWithOrigin(t, testOrigin)
155 card := door(t, page(t, app, nil, "/signin?return_to=%2Fsheets%2F7").Body.String())
156 if !strings.Contains(card, `name="return_to" value="/sheets/7"`) {
157 t.Fatalf("the door's form does not post the page:\n%s", card)
158 }
159 req := httptest.NewRequest(http.MethodPost, testOrigin+"/signin", strings.NewReader(url.Values{"address": {signedInAddress}, "return_to": {"/sheets/7"}}.Encode()))
160 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
161 req.Header.Set("Origin", testOrigin)
162 begin := do(t, app, req)
163 action, token := openLink(t, app.Router(), mail.lastLink(t))
164 if rec := postLink(t, app.Router(), action, token, begin.Result().Cookies()...); rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/sheets/7" {
165 t.Fatalf("landed %d %q, want /sheets/7", rec.Code, rec.Header().Get("Location"))
166 }
167}
168