| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "amadan.net/rastrillo/rastrillo/db" |
| 6 | "amadan.net/rastrillo/rastrillo/migrate" |
| 7 | "amadan.net/rastrillo/rastrillo/sessions" |
| 8 | "crypto/rand" |
| 9 | "errors" |
| 10 | "io" |
| 11 | "log/slog" |
| 12 | "net/http" |
| 13 | "net/http/httptest" |
| 14 | "net/url" |
| 15 | "path/filepath" |
| 16 | "strings" |
| 17 | "testing" |
| 18 | "time" |
| 19 | ) |
| 20 | |
| 21 | var invitee = equipo.Actor{Address: "invited@keymail.dev"} |
| 22 | |
| 23 | func issueInvite(t *testing.T, f *fixture, address string) (equipo.Invitation, string) { |
| 24 | t.Helper() |
| 25 | in, err := f.Store.CreateInvitation(ctx, owner, address, equipo.RoleMember, equipo.Mutation{RequestID: rand.Text()}) |
| 26 | must(t, err) |
| 27 | u, err := url.Parse(in.ShareURL) |
| 28 | must(t, err) |
| 29 | secret := u.Query().Get("secret") |
| 30 | if secret == "" || in.Revision != 1 || in.State != "pending" || time.Until(in.ExpiresAt) < 167*time.Hour || time.Until(in.ExpiresAt) > 168*time.Hour { |
| 31 | t.Fatal("invalid invitation") |
| 32 | } |
| 33 | return in, secret |
| 34 | } |
| 35 | func inviteSession(t *testing.T, f *fixture, a equipo.Actor) { |
| 36 | t.Helper() |
| 37 | token := rand.Text() |
| 38 | now := time.Now().UTC() |
| 39 | f.exec(t, "INSERT INTO sessions(token_hash,subject,method,auth_time,created_at,expires_at) VALUES(?,?,?,?,?,?)", sessions.HashToken(token), a.Address, "keymail", now.Format(time.RFC3339Nano), now.Format(time.RFC3339Nano), now.Add(time.Hour).Format(time.RFC3339Nano)) |
| 40 | f.Cookies[a.Address] = &http.Cookie{Name: "rastrillo_session", Value: token, Path: "/"} |
| 41 | } |
| 42 | func inviteForm(key string) url.Values { |
| 43 | return url.Values{"request_id": {key}, "revision": {"1"}} |
| 44 | } |
| 45 | |
| 46 | func verifyInviteIdentity(t *testing.T, f *fixture, address string, returnTo ...string) *httptest.ResponseRecorder { |
| 47 | t.Helper() |
| 48 | token := rand.Text() |
| 49 | f.exec(t, "INSERT INTO auth_links(hash,address,purpose,expires_at) VALUES(?,?,?,?)", sessions.HashToken(token), address, "signin", time.Now().Add(time.Hour).UTC().Format(time.RFC3339)) |
| 50 | values := url.Values{"token": {token}} |
| 51 | if len(returnTo) != 0 { |
| 52 | values.Set("return_to", returnTo[0]) |
| 53 | } |
| 54 | r := httptest.NewRequest("POST", "/auth/verify", strings.NewReader(values.Encode())) |
| 55 | r.Header.Set("Origin", origin) |
| 56 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 57 | w := httptest.NewRecorder() |
| 58 | f.Handler.ServeHTTP(w, r) |
| 59 | for _, c := range w.Result().Cookies() { |
| 60 | if c.Name == "rastrillo_session" && c.Value != "" { |
| 61 | f.Cookies[address] = c |
| 62 | } |
| 63 | } |
| 64 | return w |
| 65 | } |
| 66 | |
| 67 | func inviteUpgradeFixture(t *testing.T) *fixture { |
| 68 | t.Helper() |
| 69 | d, err := db.Open(filepath.Join(t.TempDir(), "invites.db"), slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 70 | must(t, err) |
| 71 | t.Cleanup(func() { must(t, d.Close()) }) |
| 72 | old := &migrate.Set{} |
| 73 | for _, m := range equipo.BootSchema.All() { |
| 74 | if m.ID != "equipo/0027_invitations" { |
| 75 | old.Add(m) |
| 76 | } |
| 77 | } |
| 78 | _, err = migrate.Apply(ctx, d, old) |
| 79 | must(t, err) |
| 80 | f := &fixture{DB: d} |
| 81 | f.exec(t, "INSERT INTO teams(id,name,roster_epoch) VALUES('saved','Saved',7)") |
| 82 | f.exec(t, "INSERT INTO members(address,role,revision,active,session_epoch) VALUES('saved@example.com','owner',4,1,123)") |
| 83 | f.exec(t, "INSERT INTO audit_events(actor,kind,record_id) VALUES('saved@example.com','old.audit','old')") |
| 84 | return f |
| 85 | } |
| 86 | |
| 87 | func inviteRequest(t *testing.T, f *fixture, actor equipo.Actor, method, path string, values url.Values, cookie *http.Cookie, sites ...string) *httptest.ResponseRecorder { |
| 88 | t.Helper() |
| 89 | r := httptest.NewRequest(method, path, strings.NewReader(values.Encode())) |
| 90 | if c := f.Cookies[actor.Address]; c != nil { |
| 91 | r.AddCookie(c) |
| 92 | } |
| 93 | if cookie != nil { |
| 94 | r.AddCookie(cookie) |
| 95 | } |
| 96 | if len(sites) == 0 { |
| 97 | sites = []string{origin} |
| 98 | } |
| 99 | r.Header.Set("Origin", sites[0]) |
| 100 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 101 | w := httptest.NewRecorder() |
| 102 | f.Handler.ServeHTTP(w, r) |
| 103 | return w |
| 104 | } |
| 105 | func exchangeInvite(t *testing.T, f *fixture, in equipo.Invitation) *http.Cookie { |
| 106 | t.Helper() |
| 107 | w := inviteRequest(t, f, equipo.Actor{}, "GET", in.ShareURL, nil, nil) |
| 108 | expectHTTP(t, w, 303) |
| 109 | if w.Header().Get("Location") != "/invites/"+in.ID { |
| 110 | t.Fatal("exchange redirect contains secret or wrong path") |
| 111 | } |
| 112 | for _, c := range w.Result().Cookies() { |
| 113 | if c.Path == "/invites/"+in.ID { |
| 114 | if !c.HttpOnly || !c.Secure || c.SameSite != http.SameSiteLaxMode || c.MaxAge <= 0 || c.MaxAge > 900 { |
| 115 | t.Fatal("unsafe invite cookie", c.Name) |
| 116 | } |
| 117 | return c |
| 118 | } |
| 119 | } |
| 120 | t.Fatal("missing path-scoped invite cookie") |
| 121 | return nil |
| 122 | } |
| 123 | |
| 124 | func wantInviteError(t *testing.T, got, want error) { |
| 125 | t.Helper() |
| 126 | if !errors.Is(got, want) { |
| 127 | t.Fatalf("got %v, want %v", got, want) |
| 128 | } |
| 129 | } |
| 130 | func inviteCSRF(t *testing.T, f *fixture, actor equipo.Actor, path string, values url.Values, cookie *http.Cookie) { |
| 131 | t.Helper() |
| 132 | for _, site := range []string{"https://evil.example", ""} { |
| 133 | expectHTTP(t, inviteRequest(t, f, actor, "POST", path, values, cookie, site), 403) |
| 134 | } |
| 135 | } |
| 136 | func invalidateInvite(t *testing.T, f *fixture, in equipo.Invitation, state string) { |
| 137 | t.Helper() |
| 138 | switch state { |
| 139 | case "expired": |
| 140 | f.exec(t, "UPDATE invitations SET expires_at=? WHERE id=?", time.Now().Add(-time.Hour), in.ID) |
| 141 | case "revoked": |
| 142 | for range 2 { |
| 143 | must(t, f.Store.RevokeInvitation(ctx, owner, in.ID, equipo.Mutation{RequestID: "revoke", Revision: 1})) |
| 144 | } |
| 145 | case "removed inviter": |
| 146 | must(t, f.Store.SetMember(ctx, peer, owner.Address, equipo.RoleMember, false, 1)) |
| 147 | case "unavailable roster": |
| 148 | f.exec(t, "ALTER TABLE members RENAME TO unavailable_members") |
| 149 | case "unavailable invites": |
| 150 | f.exec(t, "ALTER TABLE invitations RENAME TO unavailable_invitations") |
| 151 | default: |
| 152 | t.Fatal("unknown invitation fixture state", state) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func inviteText(t *testing.T, body string, present bool, texts ...string) { |
| 157 | t.Helper() |
| 158 | for _, text := range texts { |
| 159 | if strings.Contains(body, text) != present { |
| 160 | t.Fatalf("page text %q: want present=%t", text, present) |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |