package idtest // Helpers for the sign-in door's tests (door_*_test.go): Equipo's real // router, its real guard, both modes. import ( "net/http" "net/http/httptest" "net/url" "regexp" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/pow" "amadan.net/rastrillo/rastrillo/sessions" ) const ( sessionCookie = "__Host-rastrillo_session" jarCookie = "__Host-rastrillo_last_signin" keptCookie = "__Host-rastrillo_return_to" refusal = "That email can’t sign in to Equipo. Use a different email, or ask whoever runs this team to add it." ) // modes runs fn standalone and with Home configured. func modes(t *testing.T, fn func(t *testing.T, f *fixture, home bool)) { for _, home := range []bool{false, true} { name := "standalone" if home { name = "home" } t.Run(name, func(t *testing.T) { t.Parallel(); fn(t, newFixture(t, home), home) }) } } // remove takes who off the team, as People's Remove does. func (f *fixture) remove(t *testing.T, who string) { t.Helper() if _, err := f.db.Writer().Exec("UPDATE members SET active=0 WHERE address=?", who); err != nil { t.Fatal(err) } } // member adds who to the team. func (f *fixture) member(t *testing.T, who string) { t.Helper() 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 { t.Fatal(err) } } // breakRoster makes the roster unreadable, as a storage failure would. func (f *fixture) breakRoster(t *testing.T) { t.Helper() if _, err := f.db.Writer().Exec("ALTER TABLE members RENAME TO members_unreadable"); err != nil { t.Fatal(err) } } // sessionAlive reports whether c's session row is still stored. func (f *fixture) sessionAlive(t *testing.T, c *http.Cookie) bool { t.Helper() var n int if err := f.db.Writer().QueryRow("SELECT count(*) FROM sessions WHERE token_hash=?", sessions.HashToken(c.Value)).Scan(&n); err != nil { t.Fatal(err) } return n == 1 } // sessionAs stores a session for subject signed in by method. func (f *fixture) sessionAs(t *testing.T, subject, method string) *http.Cookie { t.Helper() now := time.Now().UTC() token, hash, err := sessions.NewToken() if err != nil { t.Fatal(err) } 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 { t.Fatal(err) } return &http.Cookie{Name: sessionCookie, Value: token} } // set reports the cookie w sets by name (nil when none), and whether it // clears it. func set(w *httptest.ResponseRecorder, name string) (c *http.Cookie, clears bool) { for _, c := range w.Result().Cookies() { if c.Name == name { return c, c.MaxAge < 0 || c.Value == "" } } return nil, false } // emailRecord is a remembered email way, from a real email sign-in. func (f *fixture) emailRecord(t *testing.T, who string) (*http.Cookie, *http.Cookie) { t.Helper() page := f.request("GET", "/signin?way=email", "", nil) form := fields(page.Body.String(), "/auth/begin") form.Set("address", who) time.Sleep(pow.DefaultMinAge + 20*time.Millisecond) begin := f.request("POST", "/auth/begin", f.origin, form) link := f.mailedLink(t) jar := mergeCookies(page.Result().Cookies(), begin.Result().Cookies()) confirm := f.request("GET", link, "", nil, jar...) done := f.request("POST", "/auth/verify", f.origin, fields(confirm.Body.String(), "/auth/verify"), mergeCookies(jar, confirm.Result().Cookies())...) if done.Code != 303 { t.Fatalf("email sign-in %d %s", done.Code, done.Body.String()) } return cookie(t, done, sessionCookie), cookie(t, done, jarCookie) } func (f *fixture) mailedLink(t *testing.T) string { t.Helper() f.mail.mu.Lock() body := f.mail.body f.mail.body = "" f.mail.mu.Unlock() for _, word := range strings.Fields(body) { if strings.Contains(word, "/auth/verify?") { u, err := url.Parse(word) if err != nil { t.Fatal(err) } return u.RequestURI() } } t.Fatal("no emailed link") return "" } // oficinaSignin signs the linked person in through Oficina ID, with any // extra cookies the browser holds, and returns the finish's answer. func (f *fixture) oficinaSignin(t *testing.T, extra ...*http.Cookie) *httptest.ResponseRecorder { t.Helper() start := f.request("GET", "/auth/home?return_to=%2Fwork", "", nil, extra...) if start.Code != 303 { t.Fatalf("start %d", start.Code) } u, _ := url.Parse(start.Header().Get("Location")) receipt := f.request("POST", "/auth/assertion", f.issuer, url.Values{"assertion": {f.signed(t, u.Query().Get("nonce"), person, time.Now())}}) cookies := append([]*http.Cookie{cookie(t, start, "__Host-equipo-id-pending"), cookie(t, receipt, "__Host-equipo-id-inbox")}, extra...) done := f.request("GET", "/auth/finish", "", nil, cookies...) if done.Code != 303 { t.Fatalf("finish %d %s", done.Code, done.Body.String()) } return done } var titleTag = regexp.MustCompile(`([^<]*)`) func title(body string) string { m := titleTag.FindStringSubmatch(body) if m == nil { return "" } return strings.ReplaceAll(m[1], "’", "’") }