oficina / fichas Public

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

Plain git — no account needed to clone.

Download

Download this file

1package fichastest
2
3// The smart door's routes and states (Home spec 2026-11-19-smart-door.md,
4// step 6). Fichas has no Home: email is the one way in.
5
6import (
7 "net/http"
8 "net/url"
9 "strings"
10 "testing"
11)
12
13// Every route the door answers, and the two a Home-derived app mounts
14// that Fichas does not.
15func TestTheDoorsRoutes(t *testing.T) {
16 h, _, _ := wiredApp(t)
17 for path, want := range map[string]int{
18 "/signin": http.StatusOK,
19 "/signed-out": http.StatusOK,
20 "/signin/local": http.StatusNotFound,
21 "/signin/oficina": http.StatusNotFound,
22 } {
23 rec := get(t, h, path)
24 if rec.Code != want {
25 t.Errorf("GET %s: %d, want %d", path, rec.Code, want)
26 }
27 if want == http.StatusOK && rec.Header().Get("Cache-Control") != "no-store" {
28 t.Errorf("GET %s: Cache-Control %q, want no-store", path, rec.Header().Get("Cache-Control"))
29 }
30 }
31}
32
33// Each state of the door, signed out, with its tab title, which is what
34// its heading says.
35func TestEveryDoorStateHasItsTitle(t *testing.T) {
36 h, _, _ := wiredApp(t)
37 for path, title := range map[string]string{
38 "/signin": "Sign in to Fichas",
39 "/signin?err=rate": "Problem signing in to Fichas",
40 "/signin?err=address": "Problem signing in to Fichas",
41 "/signin?err=expired": "Problem signing in to Fichas",
42 "/signin?err=keymail&force=1": "Problem signing in to Fichas",
43 "/signin?err=1": "Problem signing in to Fichas",
44 "/signin?continue=nothing": "Problem signing in to Fichas",
45 "/signin?reauth=1": "Sign in to Fichas",
46 "/signin?force=1": "Sign in to Fichas",
47 "/signed-out": "Signed out · Fichas",
48 } {
49 rec := get(t, h, path)
50 if rec.Code != http.StatusOK {
51 t.Errorf("GET %s: %d", path, rec.Code)
52 continue
53 }
54 body := rec.Body.String()
55 if got := titleOf(body); got != title {
56 t.Errorf("GET %s: title %q, want %q", path, got, title)
57 }
58 if !strings.Contains(body, "estilo-door") || !strings.Contains(body, "<span>Fichas</span>") {
59 t.Errorf("GET %s is not the door", path)
60 }
61 if strings.Contains(body, "Oficina ID") {
62 t.Errorf("GET %s offers Oficina ID", path)
63 }
64 }
65 // A failed Keymail round trip keeps the plain-email choice it offered.
66 if body := get(t, h, "/signin?err=keymail&force=1").Body.String(); !strings.Contains(body, `<input type="hidden" name="force" value="1">`) {
67 t.Error("after a Keymail failure the door does not keep force=1")
68 }
69 // A step-up keeps reauth on the post.
70 if body := get(t, h, "/signin?reauth=1").Body.String(); !strings.Contains(body, `<input type="hidden" name="reauth" value="1">`) {
71 t.Error("a step-up's door does not post reauth=1")
72 }
73}
74
75// Signed in and admitted, a bare /signin is no door: it goes back to the
76// page asked for, or the list, and never to a page that is no page.
77func TestASignedInOwnerIsSentOn(t *testing.T) {
78 h, m, _ := wiredApp(t)
79 b := newBrowser()
80 signIn(t, h, b, m, TestOwner, nil)
81 for target, want := range map[string]string{
82 "/signin": "/",
83 "/signin?return_to=%2Fcards%2Fnew": "/cards/new",
84 "/signin?return_to=%2Fsigned-out": "/",
85 "/signin?return_to=%2F%2Fevil.example%2F": "/",
86 "/signin?return_to=https%3A%2F%2Fevil.example%2F": "/",
87 "/signin?return_to=%2Fa&return_to=%2Fb": "/",
88 } {
89 rec := b.get(t, h, target)
90 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != want {
91 t.Errorf("GET %s signed in: %d to %q, want 303 to %q", target, rec.Code, rec.Header().Get("Location"), want)
92 }
93 }
94 // A state of the door is drawn for a signed-in browser too, and every
95 // door is drawn signed out: no account menu, no address.
96 for _, target := range []string{"/signin?sent=1", "/signin?err=expired", "/signin?reauth=1", "/signed-out"} {
97 rec := b.get(t, h, target)
98 body := rec.Body.String()
99 if rec.Code != http.StatusOK {
100 t.Errorf("GET %s signed in: %d", target, rec.Code)
101 }
102 for _, account := range []string{"estilo-account", "Signed in as", "Sign out"} {
103 if strings.Contains(body, account) {
104 t.Errorf("GET %s draws the account (%q): every door is drawn signed out", target, account)
105 }
106 }
107 }
108 if !b.has(sessionCookie) {
109 t.Error("an admitted session was ended by the door")
110 }
111}
112
113// The email way is remembered: back on the door after signing out, the
114// one way in is "Continue as {address}", with "Use a different email"
115// under it; that forgets it, and the field comes back.
116func TestTheEmailWayIsRemembered(t *testing.T) {
117 h, m, _ := wiredApp(t)
118 b := newBrowser()
119 signIn(t, h, b, m, TestOwner, nil)
120 if !b.has(rememberName) {
121 t.Fatal("signing in remembered nothing")
122 }
123 out := b.post(t, h, "/signout", url.Values{})
124 if out.Code != http.StatusSeeOther || out.Header().Get("Location") != "/signed-out" {
125 t.Fatalf("Sign out: %d to %q, want 303 to /signed-out", out.Code, out.Header().Get("Location"))
126 }
127 if b.has(sessionCookie) {
128 t.Fatal("Sign out kept the session cookie")
129 }
130 door := b.get(t, h, "/signin").Body.String()
131 for _, want := range []string{TestOwner, "Use a different email", `action="/signin/forget"`} {
132 if !strings.Contains(door, want) {
133 t.Errorf("the returning door lacks %q:\n%s", want, door)
134 }
135 }
136 if strings.Contains(door, "Other ways to sign in") {
137 t.Error("an email-only door offers Other ways")
138 }
139 forget := b.post(t, h, "/signin/forget", url.Values{"way": {"email"}})
140 if forget.Code != http.StatusSeeOther {
141 t.Fatalf("Forget: %d", forget.Code)
142 }
143 if b.has(rememberName) {
144 t.Error("Forget kept the remembered way")
145 }
146 if door := b.get(t, h, "/signin").Body.String(); strings.Contains(door, TestOwner) || !strings.Contains(door, `name="address" required`) {
147 t.Errorf("after Forget the door is not the empty field:\n%s", door)
148 }
149}
150
151// Where Sign out lands: "You’re signed out", Sign in, and the quiet
152// "Forget me on this device", a POST to auth's Forget. Fichas has no Home,
153// so nothing chains on to Oficina ID.
154func TestSignedOutOffersForget(t *testing.T) {
155 h, m, _ := wiredApp(t)
156 b := newBrowser()
157 signIn(t, h, b, m, TestOwner, nil)
158 b.post(t, h, "/signout", url.Values{})
159 page := b.get(t, h, "/signed-out").Body.String()
160 for _, want := range []string{
161 "<h1>You’re signed out</h1>",
162 "You’ve signed out of Fichas on this device.",
163 `<a rst-btn="primary" estilo-signin-action href="/signin">Sign in</a>`,
164 `<form estilo-signin-forget method="post" action="/signin/forget">`,
165 ">Forget me on this device</button>",
166 } {
167 if !strings.Contains(page, want) {
168 t.Errorf("the signed-out page lacks %q:\n%s", want, page)
169 }
170 }
171 for _, gone := range []string{"chain", "oficina", "id.oficina.works"} {
172 if strings.Contains(page, gone) {
173 t.Errorf("the signed-out page chains on (%q), with no Home to chain to", gone)
174 }
175 }
176 rec := b.post(t, h, "/signin/forget", url.Values{})
177 if rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), "/signin") {
178 t.Errorf("Forget: %d to %q, want the door", rec.Code, rec.Header().Get("Location"))
179 }
180 if b.has(rememberName) {
181 t.Error("Forget me on this device kept the remembered way")
182 }
183 // Same-origin only, as Begin is: a cross-site form forgets nothing.
184 cross := newBrowser()
185 signIn(t, h, cross, m, TestOwner, nil)
186 if r := crossSitePost(t, h, cross, "/signin/forget"); r.Code != http.StatusForbidden {
187 t.Errorf("a cross-site Forget answered %d, want 403", r.Code)
188 }
189 if !cross.has(rememberName) {
190 t.Error("a cross-site Forget forgot")
191 }
192}
193
194// The confirm step an emailed link opens is the door, with one Sign in
195// button that POSTs the token back; its GET spends nothing and sets
196// nothing, and it is kept out of caches and referrers.
197func TestTheConfirmStepIsTheDoor(t *testing.T) {
198 h, m, _ := wiredApp(t)
199 b := newBrowser()
200 tok := linkFor(t, h, b, m, TestOwner, nil)
201 rec := get(t, h, "/auth/verify?token="+url.QueryEscape(tok))
202 if rec.Code != http.StatusOK {
203 t.Fatalf("GET /auth/verify: %d", rec.Code)
204 }
205 if len(rec.Result().Cookies()) != 0 {
206 t.Error("the confirm step's GET set a cookie")
207 }
208 if rec.Header().Get("Cache-Control") != "no-store" || rec.Header().Get("Referrer-Policy") == "" {
209 t.Errorf("confirm headers: Cache-Control %q, Referrer-Policy %q", rec.Header().Get("Cache-Control"), rec.Header().Get("Referrer-Policy"))
210 }
211 body := rec.Body.String()
212 if got := titleOf(body); got != "Sign in to Fichas" {
213 t.Errorf("confirm title %q", got)
214 }
215 // On the one-way door, so its Sign in is the door's pill like every
216 // other state's main action.
217 for _, want := range []string{"<div estilo-signin-screen>", `<div rst-signin-door="one-way">`, `<form rst-signin-form method="post" action="/auth/verify">`, `name="token"`, ">Sign in</button>"} {
218 if !strings.Contains(body, want) {
219 t.Errorf("the confirm step lacks %q:\n%s", want, body)
220 }
221 }
222 if strings.Contains(body, TestOwner) {
223 t.Error("the confirm step names the address: the link may be open in another browser")
224 }
225}
226
227// Begin's own problems reach the door as they are, for any address: a
228// mistyped address is "That doesn't look like an email address", never a
229// cheerful Sent (no sameAnswer), with nothing mailed and no dial, and it
230// keeps force and reauth.
231func TestAMistypedAddressIsSaidSo(t *testing.T) {
232 h, m, _ := wiredApp(t)
233 for _, extra := range []url.Values{{}, {"force": {"1"}}, {"reauth": {"1"}}} {
234 form := url.Values{"address": {"not an address"}}
235 for k, v := range extra {
236 form[k] = v
237 }
238 rec := post(t, h, "/auth/begin", form)
239 loc := rec.Header().Get("Location")
240 if rec.Code != http.StatusSeeOther || !strings.HasPrefix(loc, "/signin?err=address") {
241 t.Errorf("%v: %d to %q, want the door with err=address", extra, rec.Code, loc)
242 }
243 for k := range extra {
244 if !strings.Contains(loc, k+"=1") {
245 t.Errorf("%v: %q dropped %s", extra, loc, k)
246 }
247 }
248 if body := get(t, h, loc).Body.String(); !strings.Contains(body, "That doesn&#39;t look like an email address.") {
249 t.Errorf("%q does not say so", loc)
250 }
251 }
252 if len(m.sent) != 0 {
253 t.Errorf("%d mails sent for mistyped addresses", len(m.sent))
254 }
255}
256