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 door's refusal: what it says, where, to whom, and what it ends.
4
5import (
6 "net/http"
7 "net/url"
8 "strings"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/home/connect"
13 "amadan.net/rastrillo/rastrillo/lastsignin"
14)
15
16// Paul's wording, approved for Docs and Sheets: the same sentence with
17// Calendar's name.
18func TestTheRefusalIsPaulsWording(t *testing.T) {
19 const want = "That email can’t sign in to Calendar. Use a different email, or ask whoever runs this team to add it."
20 if signinRefusal != want {
21 t.Fatalf("signinRefusal = %q, want %q", signinRefusal, want)
22 }
23}
24
25// A session the signed-in routes no longer admit (alice, taken off the
26// allowlist) reaching the door: the refusal in the app-problem slot with
27// the email field; its cookie cleared on that very response and the
28// session ended; a remembered Oficina ID forgotten, a remembered email way
29// kept; and Other ways still reachable. No loop: the next /signin is the
30// plain door. /signin/oficina, asked by that session, starts Oficina ID
31// rather than shortcutting it to a page that refuses it.
32func TestARefusedSessionGetsTheRefusalOnTheDoor(t *testing.T) {
33 for _, path := range []string{"/signin", "/signin/local"} {
34 for name, rec := range map[string]lastsignin.Record{
35 "oficina": {Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"},
36 "email": {Method: lastsignin.MethodMagicLink, Address: "alice@example.test"},
37 } {
38 t.Run(path+" "+name, func(t *testing.T) {
39 f := newHomeRouterFixture(t)
40 f.session(t, "alice@example.test", "magiclink", time.Now())
41 f.remember(t, rec)
42 delete(f.app.home.admitted.addrs, "alice@example.test")
43 cookie := f.app.sessionCookie()
44 w := f.get(path)
45 card := door(t, w.Body.String())
46 if w.Code != http.StatusOK || !strings.Contains(card, appProblem) || !strings.Contains(card, signinRefusal) || !strings.Contains(card, emailField) {
47 t.Fatalf("GET %s: %d, want the refusal with the email field\n%s", path, w.Code, card)
48 }
49 if !clears(w, cookie) || f.sessionCookie() != nil {
50 t.Error("the refused session's cookie was not cleared on the door's response")
51 }
52 // The door is drawn signed out, whatever the request's
53 // context still holds: no nav, no account, no menu.
54 if b := w.Body.String(); strings.Contains(b, `href="/today"`) || strings.Contains(b, "estilo-account") || strings.Contains(b, "estilo-bar-menu") {
55 t.Error("the refusal's bar is drawn as signed in")
56 }
57 got, res := f.remembered()
58 switch {
59 case rec.Method == lastsignin.MethodFederated && res == lastsignin.Valid:
60 t.Error("a refused session's remembered Oficina ID was kept")
61 case rec.Method == lastsignin.MethodMagicLink && (res != lastsignin.Valid || got.Address != "alice@example.test"):
62 t.Error("a refused session's remembered email way was forgotten")
63 }
64 again := f.get(path)
65 if strings.Contains(again.Body.String(), signinRefusal) {
66 t.Errorf("the refusal stayed on the next %s: the session was not ended", path)
67 }
68 })
69 }
70 }
71
72 f := newHomeRouterFixture(t)
73 f.session(t, "alice@example.test", "magiclink", time.Now())
74 delete(f.app.home.admitted.addrs, "alice@example.test")
75 start := f.get(connect.SigninOficinaPath)
76 if loc := start.Header().Get("Location"); start.Code != http.StatusSeeOther || !strings.HasPrefix(loc, "/auth/start") {
77 t.Fatalf("/signin/oficina for a refused session = %d %q, want the Home exchange", start.Code, loc)
78 }
79}
80
81// The refusal is on the plain door only: the chooser and a way's own page
82// are how a refused person reaches another way in (Fable on Docs #139,
83// N1), in both modes.
84func TestARefusedSessionCanReachOtherWays(t *testing.T) {
85 f := newHomeRouterFixture(t)
86 f.session(t, "alice@example.test", "magiclink", time.Now())
87 delete(f.app.home.admitted.addrs, "alice@example.test")
88 w := f.get("/signin?ways=1&way=email")
89 card := door(t, w.Body.String())
90 if w.Code != http.StatusOK || strings.Contains(card, signinRefusal) || !strings.Contains(card, "<span>Oficina ID</span>") {
91 t.Fatalf("the chooser for a refused session: %d\n%s", w.Code, card)
92 }
93 if !clears(w, f.app.sessionCookie()) {
94 t.Error("the chooser left the refused session's cookie")
95 }
96
97 s := newStandaloneDoor(t)
98 // A Home session on an instance Home was taken off: requirePerson's 404.
99 s.session(t, "pr_xxxxxxxxxxxxxxxxxxxxxxxxxx", connect.HomeMethod, time.Now())
100 if card := door(t, s.get("/signin?way=email").Body.String()); strings.Contains(card, signinRefusal) {
101 t.Errorf("standalone way page drew the refusal:\n%s", card)
102 }
103}
104
105// Standalone, a session the signed-in routes refuse gets the same door.
106func TestARefusedSessionStandalone(t *testing.T) {
107 f := newStandaloneDoor(t)
108 f.session(t, "pr_xxxxxxxxxxxxxxxxxxxxxxxxxx", connect.HomeMethod, time.Now())
109 w := f.get("/signin")
110 if card := door(t, w.Body.String()); w.Code != http.StatusOK || !strings.Contains(card, signinRefusal) {
111 t.Fatalf("standalone refused session: %d\n%s", w.Code, card)
112 }
113 if !clears(w, f.app.sessionCookie()) {
114 t.Error("standalone: the refused session's cookie was not cleared")
115 }
116}
117
118// auth's Refused hook: a link mailed while the address was admitted and
119// confirmed after it was not is the door with the refusal, in a 403, and
120// no session.
121func TestAVerifiedButRefusedLinkIsTheDoorIn403(t *testing.T) {
122 f := newHomeRouterFixture(t)
123 homeRedirect(t, f.do(http.MethodPost, "/signin", "address=alice%40example.test"))
124 f.app.signinMail.wait()
125 target, err := linkPath(magicLinkPattern.FindString(f.app.mailRecorder.last()))
126 if err != nil {
127 t.Fatal(err)
128 }
129 delete(f.app.home.admitted.addrs, "alice@example.test")
130 action, token, ok := ConfirmFormForTest(f.get(target).Body.String())
131 if !ok {
132 t.Fatal("no confirm form")
133 }
134 w := f.do(http.MethodPost, action, url.Values{"token": {token}}.Encode())
135 if card := door(t, w.Body.String()); w.Code != http.StatusForbidden || !strings.Contains(card, signinRefusal) || !strings.Contains(card, emailField) {
136 t.Fatalf("refused confirm: %d\n%s", w.Code, card)
137 }
138 if f.sessionCookie() != nil {
139 t.Error("a refused address got a session")
140 }
141}
142
143// A gate that cannot decide is no refusal (MIGRATING.md item 2): with the
144// people table unreadable, a Home session at /signin is not told it cannot
145// sign in, keeps its session and its remembered Oficina ID, and the
146// response sets nothing; and the strict probe MintHomeSession asks
147// remembers nothing on it. The page itself answers a retryable 503, never
148// requirePerson's 404.
149func TestAnUndecidedGateIsNoRefusal(t *testing.T) {
150 f := newHomeRouterFixture(t)
151 f.linkAlice(t, "opaque-home-alice")
152 f.cookies = map[string]*http.Cookie{}
153 if got := homeRedirect(t, f.oficinaSignin(t, connect.SigninOficinaPath, "opaque-home-alice")); got != "/" {
154 t.Fatalf("Oficina ID sign-in landed on %q", got)
155 }
156 if err := f.app.db.G.Exec("ALTER TABLE people RENAME TO people_away").Error; err != nil {
157 t.Fatal(err)
158 }
159 if w := f.get("/day/2026-09-10"); w.Code != http.StatusServiceUnavailable {
160 t.Fatalf("a page with the people table unreadable = %d, want 503", w.Code)
161 }
162 w := f.get("/signin")
163 if strings.Contains(w.Body.String(), signinRefusal) {
164 t.Error("an unreadable people table was read as a refusal")
165 }
166 if len(w.Result().Cookies()) != 0 {
167 t.Errorf("an undecided /signin set cookies: %v", w.Result().Cookies())
168 }
169 if f.sessionCookie() == nil {
170 t.Error("an undecided /signin ended the session")
171 }
172 if rec, res := f.remembered(); res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated {
173 t.Error("an undecided /signin forgot Oficina ID")
174 }
175 if f.app.admission(f.jarRequest(), true).admitted {
176 t.Error("the strict probe admitted an undecided session")
177 }
178 if ad := f.app.admission(f.jarRequest(), false); !ad.signedIn || !ad.admitted {
179 t.Error("the door's probe refused an undecided session")
180 }
181}
182