oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package idtest
2
3// Helpers for the sign-in door's tests (door_*_test.go): Equipo's real
4// router, its real guard, both modes.
5
6import (
7 "net/http"
8 "net/http/httptest"
9 "net/url"
10 "regexp"
11 "strings"
12 "testing"
13 "time"
14
15 "amadan.net/rastrillo/rastrillo/pow"
16 "amadan.net/rastrillo/rastrillo/sessions"
17)
18
19const (
20 sessionCookie = "__Host-rastrillo_session"
21 jarCookie = "__Host-rastrillo_last_signin"
22 keptCookie = "__Host-rastrillo_return_to"
23 refusal = "That email can’t sign in to Equipo. Use a different email, or ask whoever runs this team to add it."
24)
25
26// modes runs fn standalone and with Home configured.
27func modes(t *testing.T, fn func(t *testing.T, f *fixture, home bool)) {
28 for _, home := range []bool{false, true} {
29 name := "standalone"
30 if home {
31 name = "home"
32 }
33 t.Run(name, func(t *testing.T) { t.Parallel(); fn(t, newFixture(t, home), home) })
34 }
35}
36
37// remove takes who off the team, as People's Remove does.
38func (f *fixture) remove(t *testing.T, who string) {
39 t.Helper()
40 if _, err := f.db.Writer().Exec("UPDATE members SET active=0 WHERE address=?", who); err != nil {
41 t.Fatal(err)
42 }
43}
44
45// member adds who to the team.
46func (f *fixture) member(t *testing.T, who string) {
47 t.Helper()
48 if _, err := f.db.Writer().Exec("INSERT INTO members(address,role,active,revision,session_epoch,identity_key) SELECT ?,'member',1,1,0,'door-test-'||? WHERE NOT EXISTS (SELECT 1 FROM members WHERE address=?)", who, who, who); err != nil {
49 t.Fatal(err)
50 }
51}
52
53// breakRoster makes the roster unreadable, as a storage failure would.
54func (f *fixture) breakRoster(t *testing.T) {
55 t.Helper()
56 if _, err := f.db.Writer().Exec("ALTER TABLE members RENAME TO members_unreadable"); err != nil {
57 t.Fatal(err)
58 }
59}
60
61// sessionAlive reports whether c's session row is still stored.
62func (f *fixture) sessionAlive(t *testing.T, c *http.Cookie) bool {
63 t.Helper()
64 var n int
65 if err := f.db.Writer().QueryRow("SELECT count(*) FROM sessions WHERE token_hash=?", sessions.HashToken(c.Value)).Scan(&n); err != nil {
66 t.Fatal(err)
67 }
68 return n == 1
69}
70
71// sessionAs stores a session for subject signed in by method.
72func (f *fixture) sessionAs(t *testing.T, subject, method string) *http.Cookie {
73 t.Helper()
74 now := time.Now().UTC()
75 token, hash, err := sessions.NewToken()
76 if err != nil {
77 t.Fatal(err)
78 }
79 if _, err = f.db.Writer().Exec("INSERT INTO sessions(token_hash,subject,method,auth_time,created_at,expires_at) VALUES(?,?,?,?,?,?)", hash, subject, method, now.Format(time.RFC3339), now.Format(time.RFC3339), now.Add(time.Hour).Format(time.RFC3339)); err != nil {
80 t.Fatal(err)
81 }
82 return &http.Cookie{Name: sessionCookie, Value: token}
83}
84
85// set reports the cookie w sets by name (nil when none), and whether it
86// clears it.
87func set(w *httptest.ResponseRecorder, name string) (c *http.Cookie, clears bool) {
88 for _, c := range w.Result().Cookies() {
89 if c.Name == name {
90 return c, c.MaxAge < 0 || c.Value == ""
91 }
92 }
93 return nil, false
94}
95
96// emailRecord is a remembered email way, from a real email sign-in.
97func (f *fixture) emailRecord(t *testing.T, who string) (*http.Cookie, *http.Cookie) {
98 t.Helper()
99 page := f.request("GET", "/signin?way=email", "", nil)
100 form := fields(page.Body.String(), "/auth/begin")
101 form.Set("address", who)
102 time.Sleep(pow.DefaultMinAge + 20*time.Millisecond)
103 begin := f.request("POST", "/auth/begin", f.origin, form)
104 link := f.mailedLink(t)
105 jar := mergeCookies(page.Result().Cookies(), begin.Result().Cookies())
106 confirm := f.request("GET", link, "", nil, jar...)
107 done := f.request("POST", "/auth/verify", f.origin, fields(confirm.Body.String(), "/auth/verify"), mergeCookies(jar, confirm.Result().Cookies())...)
108 if done.Code != 303 {
109 t.Fatalf("email sign-in %d %s", done.Code, done.Body.String())
110 }
111 return cookie(t, done, sessionCookie), cookie(t, done, jarCookie)
112}
113
114func (f *fixture) mailedLink(t *testing.T) string {
115 t.Helper()
116 f.mail.mu.Lock()
117 body := f.mail.body
118 f.mail.body = ""
119 f.mail.mu.Unlock()
120 for _, word := range strings.Fields(body) {
121 if strings.Contains(word, "/auth/verify?") {
122 u, err := url.Parse(word)
123 if err != nil {
124 t.Fatal(err)
125 }
126 return u.RequestURI()
127 }
128 }
129 t.Fatal("no emailed link")
130 return ""
131}
132
133// oficinaSignin signs the linked person in through Oficina ID, with any
134// extra cookies the browser holds, and returns the finish's answer.
135func (f *fixture) oficinaSignin(t *testing.T, extra ...*http.Cookie) *httptest.ResponseRecorder {
136 t.Helper()
137 start := f.request("GET", "/auth/home?return_to=%2Fwork", "", nil, extra...)
138 if start.Code != 303 {
139 t.Fatalf("start %d", start.Code)
140 }
141 u, _ := url.Parse(start.Header().Get("Location"))
142 receipt := f.request("POST", "/auth/assertion", f.issuer, url.Values{"assertion": {f.signed(t, u.Query().Get("nonce"), person, time.Now())}})
143 cookies := append([]*http.Cookie{cookie(t, start, "__Host-equipo-id-pending"), cookie(t, receipt, "__Host-equipo-id-inbox")}, extra...)
144 done := f.request("GET", "/auth/finish", "", nil, cookies...)
145 if done.Code != 303 {
146 t.Fatalf("finish %d %s", done.Code, done.Body.String())
147 }
148 return done
149}
150
151var titleTag = regexp.MustCompile(`<title>([^<]*)</title>`)
152
153func title(body string) string {
154 m := titleTag.FindStringSubmatch(body)
155 if m == nil {
156 return ""
157 }
158 return strings.ReplaceAll(m[1], "&rsquo;", "’")
159}
160