oficina / calendar Public

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

Plain git — no account needed to clone.

Download

Download this file

1package calendar
2
3// The return memory, and the step-up, across the door.
4
5import (
6 "encoding/base64"
7 "net/http"
8 "net/http/httptest"
9 "net/url"
10 "strings"
11 "testing"
12 "time"
13
14 "amadan.net/oficina/home/connect"
15)
16
17// With Home, a local sign-in begun from a page lands on it after
18// /auth/local/complete (where connect could resume a link), once; one
19// begun from the plain door lands on "/". Standalone, rastrillo carries
20// the page itself.
21func TestALocalSigninLandsOnThePageItBeganFrom(t *testing.T) {
22 f := newHomeRouterFixture(t)
23 f.get("/signin/local?return_to=%2Fday%2F2026-09-10")
24 if got := f.emailSignin(t, url.Values{"address": {"alice@example.test"}, "return_to": {"/day/2026-09-10"}}); got != "/day/2026-09-10" {
25 t.Fatalf("landed on %q, want the page", got)
26 }
27 if f.cookies[homeReturnCookie] != nil {
28 t.Error("the landing kept the return memory: it would land a later sign-in there too")
29 }
30 g := f.fresh()
31 g.get("/signin")
32 if got := g.emailSignin(t, url.Values{"address": {"alice@example.test"}}); got != "/" {
33 t.Fatalf("a plain sign-in landed on %q", got)
34 }
35
36 s := newStandaloneDoor(t)
37 if got := s.emailSignin(t, url.Values{"address": {"alice@example.test"}, "return_to": {"/day/2026-09-10"}}); got != "/day/2026-09-10" {
38 t.Fatalf("standalone landed on %q, want the page", got)
39 }
40 if s.cookies[homeReturnCookie] != nil {
41 t.Error("standalone set the Home return memory")
42 }
43}
44
45// A plain /signin/local (connect's failed, unknown and connect pages send
46// people there) keeps the page the sign-in began from: it remembers
47// nothing new, and its form posts the remembered page. With its own
48// return_to it remembers that.
49func TestSigninLocalKeepsTheRememberedPage(t *testing.T) {
50 f := newHomeRouterFixture(t)
51 f.get("/signin?return_to=%2Fday%2F2026-09-10")
52 if got := f.returnPath(); got != "/day/2026-09-10" {
53 t.Fatalf("the door remembered %q", got)
54 }
55 card := door(t, f.get("/signin/local").Body.String())
56 if got := f.returnPath(); got != "/day/2026-09-10" {
57 t.Fatalf("a plain /signin/local overwrote the page with %q", got)
58 }
59 if !strings.Contains(card, `name="return_to" value="/day/2026-09-10"`) {
60 t.Fatalf("/signin/local's form does not carry the kept page:\n%s", card)
61 }
62 if got := f.emailSignin(t, url.Values{"address": {"alice@example.test"}, "return_to": {"/day/2026-09-10"}}); got != "/day/2026-09-10" {
63 t.Fatalf("landed on %q", got)
64 }
65 g := f.fresh()
66 g.get("/signin/local?return_to=%2Fagenda")
67 if got := g.returnPath(); got != "/agenda" {
68 t.Fatalf("/signin/local with its own return_to remembered %q", got)
69 }
70 // A plain /signin is a fresh start: "/".
71 g.get("/signin")
72 if got := g.returnPath(); got != "/" {
73 t.Fatalf("a plain /signin kept %q", got)
74 }
75}
76
77// A post with no return_to is a sign-in for "/": a page another tab
78// remembered does not win.
79func TestAPostWithoutAReturnForgetsAnotherTabsPage(t *testing.T) {
80 f := newHomeRouterFixture(t)
81 f.get("/signin/local?return_to=%2Fday%2F2026-09-10")
82 if got := f.emailSignin(t, url.Values{"address": {"alice@example.test"}}); got != "/" {
83 t.Fatalf("landed on %q, want /", got)
84 }
85}
86
87// return_to comes off Begin's form whether or not the body parses: Begin
88// must never read a destination of its own (Fable on Docs #139, A5), and a
89// body that did not parse is a sign-in for "/".
90func TestReturnToIsTakenOffEvenWhenTheBodyFailsToParse(t *testing.T) {
91 f := newHomeRouterFixture(t)
92 r := httptest.NewRequest(http.MethodPost, homeTestOrigin+"/signin", strings.NewReader("return_to=%2Fday%2F2026-09-10&address=a%zz"))
93 r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
94 r.Header.Set("Origin", homeTestOrigin)
95 seen := "unread"
96 w := httptest.NewRecorder()
97 f.app.beginSignin(func(w http.ResponseWriter, r *http.Request) {
98 seen = r.FormValue("return_to") + r.PostFormValue("return_to")
99 http.Redirect(w, r, "/signin?err=address", http.StatusSeeOther)
100 })(w, r)
101 if seen != "" {
102 t.Fatalf("Begin read return_to %q", seen)
103 }
104 if r.PostForm == nil {
105 t.Fatal("the body was never parsed")
106 }
107 if loc := w.Header().Get("Location"); loc != "/signin?err=address" {
108 t.Fatalf("Begin's answer = %q, want it as Begin gave it", loc)
109 }
110 g := f.fresh()
111 for _, c := range w.Result().Cookies() {
112 g.cookies[c.Name] = c
113 }
114 if got := g.returnPath(); got != "/" {
115 t.Fatalf("remembered %q, want /", got)
116 }
117}
118
119// What is remembered is only ever one safe local path: an off-site or
120// protocol-relative return_to, a backslash, a sign-in page, or the
121// signed-out page (which would land a fresh sign-in on "You're signed
122// out") lands on "/".
123func TestTheReturnMemoryRefusesWhatIsNotAPage(t *testing.T) {
124 for _, bad := range []string{"//evil.example/x", "https://evil.example/", `/\evil.example`, "/signed-out", "/signed-out?x=1", "/signin", "/auth/local/complete"} {
125 // A fixture each: Begin's per-address budget would refuse the
126 // sixth link to the same address.
127 f := newHomeRouterFixture(t)
128 g := f.fresh()
129 g.get("/signin")
130 if landed := g.emailSignin(t, url.Values{"address": {"alice@example.test"}, "return_to": {bad}}); landed != "/" {
131 t.Errorf("return_to %q landed on %q, want /", bad, landed)
132 }
133 w := httptest.NewRecorder()
134 f.app.returns.remember(w, bad)
135 h := f.fresh()
136 for _, c := range w.Result().Cookies() {
137 h.cookies[c.Name] = c
138 }
139 if got := h.returnPath(); got != "/" {
140 t.Errorf("remember(%q) kept %q, want /", bad, got)
141 }
142 }
143}
144
145// A tampered, an expired and a duplicated return cookie each read as "/"
146// at the landing.
147func TestABadReturnCookieLandsOnTheList(t *testing.T) {
148 f := newHomeRouterFixture(t)
149 f.session(t, "alice@example.test", "magiclink", time.Now())
150 session := f.sessionCookie()
151 land := func(cookies ...*http.Cookie) string {
152 r := httptest.NewRequest(http.MethodGet, homeTestOrigin+connect.LocalCompletePath, nil)
153 r.AddCookie(session)
154 for _, c := range cookies {
155 r.AddCookie(c)
156 }
157 w := httptest.NewRecorder()
158 f.app.mux.ServeHTTP(w, r)
159 if w.Code != http.StatusSeeOther {
160 t.Fatalf("landing: %d", w.Code)
161 }
162 return w.Header().Get("Location")
163 }
164 good := func(path string, now time.Time) *http.Cookie {
165 was := f.app.returns.now
166 f.app.returns.now = func() time.Time { return now }
167 defer func() { f.app.returns.now = was }()
168 w := httptest.NewRecorder()
169 f.app.returns.remember(w, path)
170 return w.Result().Cookies()[0]
171 }
172 if got := land(good("/day/2026-09-10", time.Now())); got != "/day/2026-09-10" {
173 t.Fatalf("a good cookie landed on %q", got)
174 }
175 tampered := good("/day/2026-09-10", time.Now())
176 raw, _ := base64.RawURLEncoding.DecodeString(tampered.Value)
177 raw[len(raw)-1] ^= 1
178 tampered.Value = base64.RawURLEncoding.EncodeToString(raw)
179 expired := good("/day/2026-09-10", time.Now().Add(-11*time.Minute))
180 first, second := good("/day/2026-09-10", time.Now()), good("/agenda", time.Now())
181 for name, cookies := range map[string][]*http.Cookie{"tampered": {tampered}, "expired": {expired}, "duplicated": {first, second}} {
182 if got := land(cookies...); got != "/" {
183 t.Errorf("%s: landed on %q, want /", name, got)
184 }
185 }
186}
187
188// Forget and Sign out end what a sign-in in progress had kept.
189func TestForgetAndSignOutClearTheReturnMemory(t *testing.T) {
190 f := newHomeRouterFixture(t)
191 f.get("/signin?return_to=%2Fday%2F2026-09-10")
192 if w := f.do(http.MethodPost, signinForgetPath, ""); !clears(w, homeReturnCookie) {
193 t.Error("Forget kept the return memory")
194 }
195 f.get("/signin?return_to=%2Fday%2F2026-09-10")
196 f.session(t, "alice@example.test", "magiclink", time.Now())
197 if w := f.do(http.MethodPost, "/signout", ""); !clears(w, homeReturnCookie) {
198 t.Error("Sign out kept the return memory")
199 }
200}
201
202// A step-up's reauth=1 survives Begin's answer with the page the sign-in
203// began from, for a bad address and for Sent alike: the next door is still
204// local ways only, for the same page.
205func TestAStepUpPostKeepsReauthAndTheReturn(t *testing.T) {
206 f := newHomeRouterFixture(t)
207 for _, address := range []string{"not an address", "alice@example.test"} {
208 g := f.fresh()
209 rec := g.do(http.MethodPost, "/signin", url.Values{"address": {address}, "reauth": {"1"}, "return_to": {"/day/2026-09-10"}}.Encode())
210 loc := rec.Header().Get("Location")
211 target, err := url.Parse(loc)
212 if rec.Code != http.StatusSeeOther || err != nil || target.Path != "/signin" || target.Query().Get("reauth") != "1" || target.Query().Get("return_to") != "/day/2026-09-10" {
213 t.Fatalf("%q: %d %q, want the door with reauth=1 and return_to", address, rec.Code, loc)
214 }
215 card := door(t, g.get(loc).Body.String())
216 if strings.Contains(card, "Oficina ID") || !strings.Contains(card, `name="return_to" value="/day/2026-09-10"`) {
217 t.Fatalf("%q: the step-up's next door:\n%s", address, card)
218 }
219 if address != "alice@example.test" && !strings.Contains(card, `name="reauth" value="1"`) {
220 t.Fatalf("%q: the step-up's next form drops reauth:\n%s", address, card)
221 }
222 }
223}
224
225// The link step-up (connect's Account, for a local sign-in too old to
226// link) end to end: it lands on the email-only door with reauth=1, the
227// fresh sign-in through it lands back where linking continues, and Oficina
228// ID is never offered on the way.
229func TestTheLinkStepUpEndToEnd(t *testing.T) {
230 f := newHomeRouterFixture(t)
231 if _, err := f.app.store.EnsurePersonSetup(t.Context(), "alice@example.test"); err != nil {
232 t.Fatal(err)
233 }
234 // A sign-in an hour old is too old to link.
235 f.session(t, "alice@example.test", "magiclink", time.Now().Add(-time.Hour))
236 start := f.get("/account/home")
237 loc := start.Header().Get("Location")
238 target, _ := url.Parse(loc)
239 if start.Code != http.StatusSeeOther || target.Path != connect.SigninLocalPath || target.Query().Get("reauth") != "1" {
240 t.Fatalf("a stale link start: %d %q, want the email-only door with reauth=1", start.Code, loc)
241 }
242 card := door(t, f.get(loc).Body.String())
243 if strings.Contains(card, "Oficina ID") || !strings.Contains(card, "Sign in again to continue.") || !strings.Contains(card, `name="reauth" value="1"`) {
244 t.Fatalf("the step-up door:\n%s", card)
245 }
246 if landed := f.emailSignin(t, url.Values{"address": {"alice@example.test"}, "reauth": {"1"}}); landed != "/account/home" {
247 t.Fatalf("the fresh sign-in landed on %q, want /account/home to go on linking", landed)
248 }
249 if page := f.get("/account/home"); page.Code != http.StatusOK || !strings.Contains(page.Body.String(), "Connect your Oficina account") {
250 t.Fatalf("the link page after the step-up: %d", page.Code)
251 }
252}
253