oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1package webtest
2
3import (
4 "net/http"
5 "regexp"
6 "strings"
7 "testing"
8)
9
10// The browser suite (internal/browsertest) runs the ceremonies. These check
11// the wiring a browser cannot see break: what the pages ship before any
12// script runs, and who the endpoints answer.
13
14func TestSigninOffersAHiddenPasskeyButton(t *testing.T) {
15 a := newApp(t)
16 page := a.get("/signin")
17 if page.Code != 200 {
18 t.Fatalf("signin: %d", page.Code)
19 }
20 door := regexp.MustCompile(`<button[^>]*data-rst-passkey[^>]*>Or use a passkey</button>`).FindString(page.Body)
21 if door == "" {
22 t.Fatalf("no passkey button in:\n%s", page.Body)
23 }
24 for _, want := range []string{` hidden `, `data-rst-passkey-begin="/passkey/discover/begin"`,
25 `data-rst-passkey-finish="/passkey/discover/finish"`, `data-rst-passkey-module="/passkey/webauthn.mjs"`} {
26 if !strings.Contains(door, want) {
27 t.Errorf("passkey button lacks %s: %s", want, door)
28 }
29 }
30 if !strings.Contains(page.Body, `<script type="module" src="/passkey/signin.mjs"></script>`) {
31 t.Error("the sign-in page does not load the passkey button's script")
32 }
33 // The email form is there whatever the scripts do.
34 if !strings.Contains(page.Body, `action="/auth/begin"`) {
35 t.Error("no email form beside the passkey button")
36 }
37}
38
39func TestPasskeyButtonCarriesReturnTo(t *testing.T) {
40 a := newApp(t)
41 page := a.get("/signin?return_to=%2Fstore")
42 if !strings.Contains(page.Body, `data-rst-passkey-finish="/passkey/discover/finish?return_to=%2Fstore"`) {
43 t.Fatalf("the passkey finish URL does not carry return_to:\n%s", page.Body)
44 }
45}
46
47func TestPasskeyScriptsServed(t *testing.T) {
48 a := newApp(t)
49 for _, path := range []string{"/passkey/webauthn.mjs", "/passkey/signin.mjs"} {
50 r := a.get(path)
51 if r.Code != 200 || !strings.HasPrefix(r.Header.Get("Content-Type"), "text/javascript") || !strings.Contains(r.Body, "export") {
52 t.Errorf("%s: %d %q", path, r.Code, r.Header.Get("Content-Type"))
53 }
54 }
55}
56
57func TestSettingsOffersAHiddenAddPasskeyButton(t *testing.T) {
58 a := newApp(t)
59 a.signedInOwner()
60 page := a.get("/settings")
61 if page.Code != 200 {
62 t.Fatalf("settings: %d", page.Code)
63 }
64 if !strings.Contains(page.Body, `<small id="passkeys">None yet</small>`) {
65 t.Errorf("Settings does not say there are no passkeys:\n%s", page.Body)
66 }
67 btn := regexp.MustCompile(`<button[^>]*data-passkey-add[^>]*>Add a passkey</button>`).FindString(page.Body)
68 if btn == "" || !strings.Contains(btn, ` hidden `) {
69 t.Fatalf("no hidden add-a-passkey button in:\n%s", page.Body)
70 }
71 // The user handle is the member's random id, never the address.
72 if strings.Contains(btn, `data-user-id="`+ownerAddress) || !regexp.MustCompile(`data-user-id="[A-Za-z0-9_-]+"`).MatchString(btn) {
73 t.Errorf("bad user handle: %s", btn)
74 }
75 if !regexp.MustCompile(`<script type="module" src="/static/passkey-add\.[^"]*js"></script>`).MatchString(page.Body) {
76 t.Errorf("Settings does not load the add-a-passkey script:\n%s", page.Body)
77 }
78 // A query alone does not claim a passkey was added.
79 if strings.Contains(a.get("/settings?passkey=added").Body, "Passkey added") {
80 t.Error("Settings says a passkey was added when there is none")
81 }
82}
83
84// Registering needs a member's session; discovery is open, but only to the
85// page's own origin.
86func TestPasskeyEndpointsGuarded(t *testing.T) {
87 a := newApp(t)
88 jsonPost := func(path, origin string) Resp {
89 req, _ := http.NewRequest(http.MethodPost, a.srv.URL+path, strings.NewReader("{}"))
90 req.Header.Set("Content-Type", "application/json")
91 req.Header.Set("Origin", origin)
92 return a.do(req)
93 }
94 if r := jsonPost("/passkey/register/begin", testOrigin); r.Code != http.StatusForbidden {
95 t.Errorf("register begin, signed out: %d", r.Code)
96 }
97 if r := jsonPost("/passkey/discover/begin", "https://evil.example"); r.Code != http.StatusForbidden {
98 t.Errorf("discover begin, cross-origin: %d", r.Code)
99 }
100 if r := jsonPost("/passkey/discover/begin", testOrigin); r.Code != 200 || !strings.Contains(r.Body, `"challenge"`) {
101 t.Errorf("discover begin: %d %s", r.Code, r.Body)
102 }
103 a.signedInOwner()
104 if r := jsonPost("/passkey/register/begin", testOrigin); r.Code != 200 || !strings.Contains(r.Body, `"challenge"`) {
105 t.Errorf("register begin, signed in: %d %s", r.Code, r.Body)
106 }
107}
108