| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "html" |
| 6 | "io/fs" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "regexp" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "amadan.net/oficina/home/handoff" |
| 18 | "amadan.net/oficina/home/internal/home/sealtest" |
| 19 | "amadan.net/oficina/home/internal/operator" |
| 20 | "amadan.net/rastrillo/rastrillo/assertion" |
| 21 | "amadan.net/rastrillo/rastrillo/sessions" |
| 22 | ) |
| 23 | |
| 24 | // teamFixture is both Home roles with one team, provisioned through the |
| 25 | // operator endpoint exactly as Alta will: label canarylabel, name |
| 26 | // CANARY-TEAM-NAME-2b71, the canary address's person as its one owner. |
| 27 | type teamFixture struct { |
| 28 | *operatorFixture |
| 29 | sqid string |
| 30 | owner string // the owner's person ref |
| 31 | ownerCred *http.Cookie // the owner's credential session |
| 32 | } |
| 33 | |
| 34 | func newTeamFixture(t *testing.T) *teamFixture { |
| 35 | t.Helper() |
| 36 | f := newOperatorFixture(t) |
| 37 | cred := signInFully(t, f.credential, sealtest.CanaryAddress) |
| 38 | f.provisionTeam(t) |
| 39 | sqid, _ := operator.TeamSQID(1) |
| 40 | tf := &teamFixture{operatorFixture: f, sqid: sqid, ownerCred: cred} |
| 41 | tf.owner = tf.person(t, sealtest.CanaryAddress) |
| 42 | return tf |
| 43 | } |
| 44 | |
| 45 | // person is addr's person ref on the credential role. |
| 46 | func (f *teamFixture) person(t *testing.T, addr string) string { |
| 47 | t.Helper() |
| 48 | ref, err := findOrMintPersonOn(context.Background(), f.credential.db.Writer(), f.credential.secrets.Pepper, addr, time.Now()) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | return ref |
| 53 | } |
| 54 | |
| 55 | // session mints a directory session directly, with the auth_time an |
| 56 | // assertion would have carried. |
| 57 | func (f *teamFixture) session(t *testing.T, subject string, authTime time.Time) *http.Cookie { |
| 58 | t.Helper() |
| 59 | rec := httptest.NewRecorder() |
| 60 | if err := f.directory.sessions.SignIn(rec, httptest.NewRequest("GET", directoryOrigin+"/", nil), sessions.Session{Subject: subject, Method: "assertion", AuthTime: authTime}); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | return sessionCookie(rec) |
| 64 | } |
| 65 | |
| 66 | // exchange runs a real assertion exchange from start (the ordinary or the |
| 67 | // fresh consumer) back to returnTo, and returns the directory session. |
| 68 | func (f *teamFixture) exchange(t *testing.T, cred *http.Cookie, start, returnTo string) *http.Cookie { |
| 69 | t.Helper() |
| 70 | begin := exchangeRequest(f.directory, "GET", directoryOrigin+start+"?"+url.Values{"return_to": {returnTo}}.Encode(), "", nil) |
| 71 | if begin.Code != 303 { |
| 72 | t.Fatal("start", begin.Code) |
| 73 | } |
| 74 | pending := namedCookie(t, begin, "__Host-oficina-assertion-pending") |
| 75 | authorize := exchangeRequest(f.credential.App, "GET", begin.Header().Get("Location"), "", nil, cred) |
| 76 | intent := namedCookie(t, authorize, assertionIntentCookie) |
| 77 | if strings.Contains(begin.Header().Get("Location"), "max_age=300") { |
| 78 | cred = reauthenticateCredential(t, f.credential, cred) |
| 79 | } |
| 80 | resume := exchangeRequest(f.credential.App, "POST", Origin+"/authorize/resume", Origin, nil, intent, cred) |
| 81 | m := regexp.MustCompile(`name="assertion" value="([^"]+)"`).FindStringSubmatch(resume.Body.String()) |
| 82 | if resume.Code != 200 || len(m) != 2 { |
| 83 | t.Fatal("resume", resume.Code, resume.Header().Get("Location")) |
| 84 | } |
| 85 | callback := exchangeRequest(f.directory, "POST", directoryOrigin+handoff.CallbackPath, Origin, url.Values{"assertion": {html.UnescapeString(m[1])}}) |
| 86 | inbox := namedCookie(t, callback, "__Host-oficina-assertion-inbox") |
| 87 | finish := exchangeRequest(f.directory, "GET", directoryOrigin+handoff.FinishPath, "", nil, pending, inbox) |
| 88 | if finish.Code != 303 || finish.Header().Get("Location") != returnTo || sessionCookie(finish) == nil { |
| 89 | t.Fatal("finish", finish.Code, finish.Header().Get("Location")) |
| 90 | } |
| 91 | return sessionCookie(finish) |
| 92 | } |
| 93 | |
| 94 | func (f *teamFixture) member(t *testing.T, sqid, ref, role string) { |
| 95 | t.Helper() |
| 96 | if _, err := f.directory.db.Writer().Exec(`INSERT INTO memberships(team_id,person_ref,role) SELECT id,?,? FROM teams WHERE sqid=?`, ref, role, sqid); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // secondTeam is Beta, owned by owner, seeded directly (test setup only). |
| 102 | func (f *teamFixture) secondTeam(t *testing.T, owner string) string { |
| 103 | t.Helper() |
| 104 | sqid, _ := operator.TeamSQID(2) |
| 105 | name, err := f.directory.directory.store.SealTeamName(2, "Beta") |
| 106 | if err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | if _, err = f.directory.db.Writer().Exec(`INSERT INTO teams(id,sqid,label,sealed_name,created_at) VALUES(2,?,'beta',?,1)`, sqid, name); err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | f.member(t, sqid, owner, "owner") |
| 113 | return sqid |
| 114 | } |
| 115 | |
| 116 | func (f *teamFixture) get(path string, cookies ...*http.Cookie) *httptest.ResponseRecorder { |
| 117 | return exchangeRequest(f.directory, "GET", directoryOrigin+path, "", nil, cookies...) |
| 118 | } |
| 119 | func (f *teamFixture) post(path string, form url.Values, cookies ...*http.Cookie) *httptest.ResponseRecorder { |
| 120 | return exchangeRequest(f.directory, "POST", directoryOrigin+path, directoryOrigin, form, cookies...) |
| 121 | } |
| 122 | |
| 123 | func (f *teamFixture) roleOf(t *testing.T, ref string) string { |
| 124 | t.Helper() |
| 125 | var role string |
| 126 | if err := f.directory.db.Writer().QueryRow(`SELECT m.role FROM memberships m JOIN teams t ON t.id=m.team_id WHERE t.sqid=? AND m.person_ref=?`, f.sqid, ref).Scan(&role); err != nil { |
| 127 | return "" |
| 128 | } |
| 129 | return role |
| 130 | } |
| 131 | func (f *teamFixture) epoch(t *testing.T) int64 { |
| 132 | t.Helper() |
| 133 | var n int64 |
| 134 | if err := f.directory.db.Writer().QueryRow(`SELECT roster_epoch FROM teams WHERE sqid=?`, f.sqid).Scan(&n); err != nil { |
| 135 | t.Fatal(err) |
| 136 | } |
| 137 | return n |
| 138 | } |
| 139 | func (f *teamFixture) names(t *testing.T) int { |
| 140 | t.Helper() |
| 141 | return f.countRows(t, f.directory, "membership_names") |
| 142 | } |
| 143 | |
| 144 | // continuesTo reports whether rec is the continuation page to want. |
| 145 | func continuesTo(rec *httptest.ResponseRecorder, want string) bool { |
| 146 | return rec.Code == 200 && strings.Contains(html.UnescapeString(rec.Body.String()), `href="`+want+`"`) |
| 147 | } |
| 148 | |
| 149 | var teamWriteForm = regexp.MustCompile(`(?i)<form[^>]*method="post"[^>]*action="/team`) |
| 150 | |
| 151 | func (f *teamFixture) teamURL() string { return "/team?team=" + f.sqid } |
| 152 | func (f *teamFixture) freshStart() string { |
| 153 | return directoryOrigin + freshStartPath + "?return_to=" + url.QueryEscape(f.teamURL()) |
| 154 | } |
| 155 | |
| 156 | // ---- reading ---- |
| 157 | |
| 158 | // Signed out, a shared team link survives sign-in (spec §5.6): 303 to the |
| 159 | // ordinary start, the query kept. A malformed link is an ordinary 404. |
| 160 | func TestTeamPageAnonymousFollowsTheChooser(t *testing.T) { |
| 161 | f := newTeamFixture(t) |
| 162 | r := f.get(f.teamURL()) |
| 163 | if r.Code != 303 || r.Header().Get("Location") != handoff.StartPath+"?return_to="+url.QueryEscape(f.teamURL()) { |
| 164 | t.Fatal(r.Code, r.Header().Get("Location")) |
| 165 | } |
| 166 | for _, path := range []string{"/team", "/team?team=", "/team?team=a-b", "/team?team=" + f.sqid + "&x=1", "/team?team=" + f.sqid + "&team=" + f.sqid} { |
| 167 | if r := f.get(path); r.Code != 404 { |
| 168 | t.Fatal(path, r.Code) |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Gate 4 and gate 21's read half: every member reads the list, an owner |
| 174 | // sees the controls, a member sees none; everyone else — another team's |
| 175 | // member, a person on no team (a guest of an instance has no roster row), |
| 176 | // an unknown or malformed sqid — gets the 404 an unknown route gets. |
| 177 | func TestTeamPageMembersReadOthersGet404(t *testing.T) { |
| 178 | f := newTeamFixture(t) |
| 179 | member := f.person(t, "member@example.invalid") |
| 180 | f.member(t, f.sqid, member, "member") |
| 181 | betaOwner := f.person(t, "beta-owner@example.invalid") |
| 182 | beta := f.secondTeam(t, betaOwner) |
| 183 | guest := f.person(t, "guest@example.invalid") |
| 184 | now := time.Now() |
| 185 | |
| 186 | owner := f.get(f.teamURL(), f.session(t, f.owner, now.Add(-time.Hour))) |
| 187 | if owner.Code != 200 || !strings.Contains(owner.Body.String(), operatorName) || !teamWriteForm.MatchString(owner.Body.String()) || !strings.Contains(owner.Body.String(), "Member "+shortRef(member)) { |
| 188 | t.Fatal("owner view", owner.Code) |
| 189 | } |
| 190 | mem := f.get(f.teamURL(), f.session(t, member, now.Add(-time.Hour))) |
| 191 | body := mem.Body.String() |
| 192 | if mem.Code != 200 || !strings.Contains(body, operatorName) || !strings.Contains(body, "Only owners can change who is on this team.") || !strings.Contains(body, "Owner "+shortRef(f.owner)) { |
| 193 | t.Fatal("member view", mem.Code) |
| 194 | } |
| 195 | if teamWriteForm.MatchString(body) || strings.Contains(body, `href="#add"`) { |
| 196 | t.Fatal("a member was drawn a write control") |
| 197 | } |
| 198 | for name, c := range map[string]struct { |
| 199 | cookie *http.Cookie |
| 200 | path string |
| 201 | }{ |
| 202 | "other team's member": {f.session(t, betaOwner, now), f.teamURL()}, |
| 203 | "member on beta's link": {f.session(t, member, now), "/team?team=" + beta}, |
| 204 | "guest, no roster row": {f.session(t, guest, now), f.teamURL()}, |
| 205 | "unknown sqid": {f.session(t, member, now), "/team?team=zzzz"}, |
| 206 | "malformed sqid": {f.session(t, member, now), "/team?team=-"}, |
| 207 | } { |
| 208 | r := f.get(c.path, c.cookie) |
| 209 | if r.Code != 404 || strings.Contains(r.Body.String(), operatorName) { |
| 210 | t.Fatal(name, r.Code) |
| 211 | } |
| 212 | if r.Body.String() != f.get("/no-such-route", c.cookie).Body.String() { |
| 213 | t.Fatal(name, "differs from an unknown route's 404") |
| 214 | } |
| 215 | } |
| 216 | // The account menu links the team for its members only. |
| 217 | if !strings.Contains(f.get("/help/open-source", f.session(t, member, now)).Body.String(), `href="/team?team=`+f.sqid+`"`) { |
| 218 | t.Fatal("no Team link for a member") |
| 219 | } |
| 220 | if strings.Contains(f.get("/help/open-source", f.session(t, guest, now)).Body.String(), `/team?team=`) { |
| 221 | t.Fatal("Team link for a person on no team") |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // ---- writing (gate 21) ---- |
| 226 | |
| 227 | type teamWriteCase struct { |
| 228 | name, path string |
| 229 | form url.Values |
| 230 | changed func(*testing.T) bool |
| 231 | } |
| 232 | |
| 233 | func (f *teamFixture) writeCases(t *testing.T, target string) []teamWriteCase { |
| 234 | return []teamWriteCase{ |
| 235 | {"promote", teamRolePath, url.Values{"team": {f.sqid}, "person": {target}, "role": {"owner"}}, func(t *testing.T) bool { return f.roleOf(t, target) == "owner" }}, |
| 236 | {"name", teamNamePath, url.Values{"team": {f.sqid}, "person": {target}, "name": {sealtest.CanaryMemberName}}, func(t *testing.T) bool { return f.names(t) != 0 }}, |
| 237 | {"remove", teamRemovePath, url.Values{"team": {f.sqid}, "person": {target}}, func(t *testing.T) bool { return f.roleOf(t, target) == "" }}, |
| 238 | {"add", teamAddPath, url.Values{"team": {f.sqid}, "role": {"member"}, "name": {""}}, func(t *testing.T) bool { return f.countRows(t, f.directory, "team_add_intents") != 0 }}, |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // Every write is owner-only and fresh: a member's, a stranger's and another |
| 243 | // team's owner's POST answer the same 404 as an unknown team; an owner's |
| 244 | // stale (301 s) or future auth_time is sent to /auth/fresh/start with the |
| 245 | // change not kept; only a fresh owner's POST commits. |
| 246 | func TestTeamWritesAreOwnerOnlyAndFresh(t *testing.T) { |
| 247 | for _, c := range []string{"promote", "name", "remove", "add"} { |
| 248 | t.Run(c, func(t *testing.T) { |
| 249 | f := newTeamFixture(t) |
| 250 | target := f.person(t, "target@example.invalid") |
| 251 | f.member(t, f.sqid, target, "member") |
| 252 | betaOwner := f.person(t, "beta-owner@example.invalid") |
| 253 | f.secondTeam(t, betaOwner) |
| 254 | var tc teamWriteCase |
| 255 | for _, w := range f.writeCases(t, target) { |
| 256 | if w.name == c { |
| 257 | tc = w |
| 258 | } |
| 259 | } |
| 260 | now := time.Now() |
| 261 | epoch := f.epoch(t) |
| 262 | for name, cookie := range map[string]*http.Cookie{ |
| 263 | "member": f.session(t, target, now), |
| 264 | "other team owner": f.session(t, betaOwner, now), |
| 265 | "stranger": f.session(t, f.person(t, "s@example.invalid"), now), |
| 266 | } { |
| 267 | if r := f.post(tc.path, tc.form, cookie); r.Code != 404 { |
| 268 | t.Fatal(name, r.Code) |
| 269 | } |
| 270 | } |
| 271 | // A team the caller does not own, named by an owner of another. |
| 272 | other := url.Values{} |
| 273 | for k, v := range tc.form { |
| 274 | other[k] = v |
| 275 | } |
| 276 | other.Set("team", "zzzz") |
| 277 | if r := f.post(tc.path, other, f.session(t, f.owner, now)); r.Code != 404 { |
| 278 | t.Fatal("unknown team", r.Code) |
| 279 | } |
| 280 | for name, at := range map[string]time.Time{"301 s": now.Add(-301 * time.Second), "future": now.Add(30 * time.Second)} { |
| 281 | r := f.post(tc.path, tc.form, f.session(t, f.owner, at)) |
| 282 | if !continuesTo(r, f.freshStart()) { |
| 283 | t.Fatal(name, "stale owner not sent to the fresh start", r.Code, r.Body.String()) |
| 284 | } |
| 285 | } |
| 286 | if tc.changed(t) || f.epoch(t) != epoch { |
| 287 | t.Fatal("a refused write changed something") |
| 288 | } |
| 289 | r := f.post(tc.path, tc.form, f.session(t, f.owner, time.Now().Add(-240*time.Second))) |
| 290 | if c == "add" { |
| 291 | if r.Code != 200 || !strings.Contains(html.UnescapeString(r.Body.String()), Origin+resolvePath+"?consumer=directory&intent=") { |
| 292 | t.Fatal("add did not continue to the credential origin", r.Code) |
| 293 | } |
| 294 | } else if r.Code != 303 || r.Header().Get("Location") != f.teamURL() { |
| 295 | t.Fatal("fresh owner write", r.Code, r.Body.String()) |
| 296 | } |
| 297 | if !tc.changed(t) { |
| 298 | t.Fatal("fresh owner write did not commit") |
| 299 | } |
| 300 | }) |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // #93's requirement: a session minted by the ordinary consumer (/auth/start) |
| 305 | // from a credential session older than five minutes carries that old |
| 306 | // auth_time; a Team-page POST from it is refused and sent to |
| 307 | // /auth/fresh/start, whose authorize asks max_age=300, and the issuer then |
| 308 | // demands a new sign-in instead of minting from the stale session. |
| 309 | func TestOrdinarySessionPostGoesToFreshStart(t *testing.T) { |
| 310 | f := newTeamFixture(t) |
| 311 | target := f.person(t, "target@example.invalid") |
| 312 | f.member(t, f.sqid, target, "member") |
| 313 | stale := time.Now().Add(-10 * time.Minute).UTC().Format(time.RFC3339) |
| 314 | if _, err := f.credential.db.Writer().Exec(`UPDATE sessions SET auth_time=? WHERE subject=?`, stale, f.owner); err != nil { |
| 315 | t.Fatal(err) |
| 316 | } |
| 317 | ordinary := f.exchange(t, f.ownerCred, handoff.StartPath, f.teamURL()) |
| 318 | if r := f.get(f.teamURL(), ordinary); r.Code != 200 { |
| 319 | t.Fatal("an ordinary session cannot read", r.Code) |
| 320 | } |
| 321 | r := f.post(teamRolePath, url.Values{"team": {f.sqid}, "person": {target}, "role": {"owner"}}, ordinary) |
| 322 | if !continuesTo(r, f.freshStart()) || f.roleOf(t, target) != "member" { |
| 323 | t.Fatal("ordinary session wrote", r.Code) |
| 324 | } |
| 325 | start := exchangeRequest(f.directory, "GET", f.freshStart(), "", nil, ordinary) |
| 326 | if start.Code != 303 || !strings.Contains(start.Header().Get("Location"), "max_age=300") { |
| 327 | t.Fatal("fresh start", start.Code, start.Header().Get("Location")) |
| 328 | } |
| 329 | authorize := exchangeRequest(f.credential.App, "GET", start.Header().Get("Location"), "", nil, f.ownerCred) |
| 330 | resume := exchangeRequest(f.credential.App, "GET", Origin+"/authorize/resume", "", nil, namedCookie(t, authorize, assertionIntentCookie), f.ownerCred) |
| 331 | if resume.Code != 303 || !strings.HasPrefix(resume.Header().Get("Location"), "/signin") { |
| 332 | t.Fatal("the issuer minted a fresh assertion from a stale session", resume.Code) |
| 333 | } |
| 334 | // After a real sign-in, the fresh exchange returns to the team page and |
| 335 | // the repeated write commits. |
| 336 | cred := signInFully(t, f.credential, sealtest.CanaryAddress) |
| 337 | fresh := f.exchange(t, cred, freshStartPath, f.teamURL()) |
| 338 | if r := f.post(teamRolePath, url.Values{"team": {f.sqid}, "person": {target}, "role": {"owner"}}, fresh); r.Code != 303 || f.roleOf(t, target) != "owner" { |
| 339 | t.Fatal("fresh write", r.Code) |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // The step-up route itself: an assertion whose auth_time is 301 s old, |
| 344 | // carried through /auth/fresh/start's pending row, is refused at finish. |
| 345 | func TestFreshStartRefusesA301SecondAssertion(t *testing.T) { |
| 346 | f := newTeamFixture(t) |
| 347 | begin := exchangeRequest(f.directory, "GET", f.freshStart(), "", nil) |
| 348 | pending := namedCookie(t, begin, "__Host-oficina-assertion-pending") |
| 349 | loc, _ := url.Parse(begin.Header().Get("Location")) |
| 350 | now := time.Now() |
| 351 | id, _ := assertion.RandomID() |
| 352 | token, err := assertion.Sign(f.credential.runtime.signer, assertion.Claims{Version: 1, Issuer: Origin, Audience: directoryOrigin, Subject: f.owner, KeyID: f.credential.runtime.keyID, ID: id, Nonce: loc.Query().Get("nonce"), IssuedAt: now.Unix(), ExpiresAt: now.Unix() + 120, AuthTime: now.Unix() - 301}) |
| 353 | if err != nil { |
| 354 | t.Fatal(err) |
| 355 | } |
| 356 | callback := exchangeRequest(f.directory, "POST", directoryOrigin+handoff.CallbackPath, Origin, url.Values{"assertion": {token}}) |
| 357 | inbox := namedCookie(t, callback, "__Host-oficina-assertion-inbox") |
| 358 | finish := exchangeRequest(f.directory, "GET", directoryOrigin+handoff.FinishPath, "", nil, pending, inbox) |
| 359 | if finish.Code == 303 || sessionCookie(finish) != nil { |
| 360 | t.Fatal("a 301 s assertion finished the fresh exchange", finish.Code) |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | func TestTeamLastOwnerAndEpoch(t *testing.T) { |
| 365 | f := newTeamFixture(t) |
| 366 | now := time.Now() |
| 367 | owner := f.session(t, f.owner, now) |
| 368 | e := f.epoch(t) |
| 369 | for _, c := range []struct { |
| 370 | path string |
| 371 | form url.Values |
| 372 | }{ |
| 373 | {teamRolePath, url.Values{"team": {f.sqid}, "person": {f.owner}, "role": {"member"}}}, |
| 374 | {teamRemovePath, url.Values{"team": {f.sqid}, "person": {f.owner}}}, |
| 375 | } { |
| 376 | r := f.post(c.path, c.form, owner) |
| 377 | if r.Code != 409 || !strings.Contains(r.Body.String(), "at least one owner") || f.roleOf(t, f.owner) != "owner" { |
| 378 | t.Fatal("sole owner", c.path, r.Code) |
| 379 | } |
| 380 | } |
| 381 | // The sole owner is drawn no demote or remove control. |
| 382 | if body := f.get(f.teamURL(), owner).Body.String(); strings.Contains(body, `action="/team/remove"`) || strings.Contains(body, "Make member") { |
| 383 | t.Fatal("sole owner offered a refusal") |
| 384 | } |
| 385 | second := f.person(t, "second@example.invalid") |
| 386 | f.member(t, f.sqid, second, "member") |
| 387 | e = f.epoch(t) |
| 388 | if r := f.post(teamRolePath, url.Values{"team": {f.sqid}, "person": {second}, "role": {"owner"}}, owner); r.Code != 303 || f.epoch(t) != e+1 { |
| 389 | t.Fatal("promote", r.Code) |
| 390 | } |
| 391 | // With two owners, either may leave: the first removes themself. |
| 392 | if r := f.post(teamRemovePath, url.Values{"team": {f.sqid}, "person": {f.owner}}, owner); r.Code != 303 || r.Header().Get("Location") != "/" || f.roleOf(t, f.owner) != "" || f.epoch(t) != e+2 { |
| 393 | t.Fatal("leave", r.Code) |
| 394 | } |
| 395 | // Removed: the former owner's list, and writes, are gone. |
| 396 | if r := f.get(f.teamURL(), owner); r.Code != 404 { |
| 397 | t.Fatal("a removed person still reads the list", r.Code) |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | func TestTeamNamesOnThePage(t *testing.T) { |
| 402 | f := newTeamFixture(t) |
| 403 | target := f.person(t, "target@example.invalid") |
| 404 | f.member(t, f.sqid, target, "member") |
| 405 | owner := f.session(t, f.owner, time.Now()) |
| 406 | e := f.epoch(t) |
| 407 | for _, bad := range []string{"till@lagged.biz", "till@lagged", strings.Repeat("n", 65)} { |
| 408 | r := f.post(teamNamePath, url.Values{"team": {f.sqid}, "person": {target}, "name": {bad}}, owner) |
| 409 | if r.Code != 400 || f.names(t) != 0 { |
| 410 | t.Fatal("bad name", bad, r.Code) |
| 411 | } |
| 412 | if strings.Contains(bad, "lagged") && !strings.Contains(r.Body.String(), "can’t contain @") { |
| 413 | t.Fatal("the @ refusal did not say why") |
| 414 | } |
| 415 | } |
| 416 | if r := f.post(teamNamePath, url.Values{"team": {f.sqid}, "person": {target}, "name": {sealtest.CanaryMemberName}}, owner); r.Code != 303 { |
| 417 | t.Fatal(r.Code) |
| 418 | } |
| 419 | member := f.get(f.teamURL(), f.session(t, target, time.Now())).Body.String() |
| 420 | if !strings.Contains(member, sealtest.CanaryMemberName) { |
| 421 | t.Fatal("a member does not see the name") |
| 422 | } |
| 423 | if f.epoch(t) != e { |
| 424 | t.Fatal("a name bumped the roster epoch") |
| 425 | } |
| 426 | if r := f.post(teamNamePath, url.Values{"team": {f.sqid}, "person": {target}, "name": {""}}, owner); r.Code != 303 || f.names(t) != 0 { |
| 427 | t.Fatal("clearing", r.Code) |
| 428 | } |
| 429 | f.post(teamNamePath, url.Values{"team": {f.sqid}, "person": {target}, "name": {sealtest.CanaryMemberName}}, owner) |
| 430 | if r := f.post(teamRemovePath, url.Values{"team": {f.sqid}, "person": {target}}, owner); r.Code != 303 || f.names(t) != 0 { |
| 431 | t.Fatal("name outlived removal", r.Code) |
| 432 | } |
| 433 | sealtest.AssertNoPlaintext(t, f.directoryPath) |
| 434 | } |
| 435 | |
| 436 | // Gate 21: call 2 cannot write memberships or names. Call 2 is not built; |
| 437 | // this pins that no production code outside the Team page's store and the |
| 438 | // operator's seed verb writes either table, so it cannot be added quietly. |
| 439 | func TestOnlyTheTeamStoreAndSeedWriteMemberships(t *testing.T) { |
| 440 | writes := regexp.MustCompile(`(?i)(INSERT(\s+OR\s+\w+)?\s+INTO|REPLACE\s+INTO|UPDATE(\s+OR\s+\w+)?|DELETE\s+FROM)\s+(memberships|membership_names)\b`) |
| 441 | for _, sql := range []string{"INSERT INTO memberships", "insert or ignore into memberships", "REPLACE INTO membership_names", "UPDATE OR REPLACE memberships", "DELETE FROM membership_names"} { |
| 442 | if !writes.MatchString(sql) { |
| 443 | t.Fatal("the scan misses", sql) |
| 444 | } |
| 445 | } |
| 446 | allowed := map[string]bool{ |
| 447 | "internal/home/directorydata/members.go": true, |
| 448 | "internal/operator/directory.go": true, |
| 449 | } |
| 450 | root := "../.." |
| 451 | files := grepGoFiles(t, root) |
| 452 | for path, src := range files { |
| 453 | if strings.HasSuffix(path, "_test.go") || allowed[path] { |
| 454 | continue |
| 455 | } |
| 456 | if writes.MatchString(src) { |
| 457 | t.Errorf("%s writes the roster; only the Team page and the seed verb may", path) |
| 458 | } |
| 459 | } |
| 460 | if len(files) < 20 { |
| 461 | t.Fatal("scanned too few files; the guard would pass by not looking") |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // grepGoFiles is every .go file under root, by slash path relative to it. |
| 466 | func grepGoFiles(t *testing.T, root string) map[string]string { |
| 467 | t.Helper() |
| 468 | out := map[string]string{} |
| 469 | err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { |
| 470 | if err != nil { |
| 471 | return err |
| 472 | } |
| 473 | if d.IsDir() && (d.Name() == ".git" || d.Name() == "node_modules") { |
| 474 | return filepath.SkipDir |
| 475 | } |
| 476 | if !d.IsDir() && strings.HasSuffix(path, ".go") { |
| 477 | b, err := os.ReadFile(path) |
| 478 | if err != nil { |
| 479 | return err |
| 480 | } |
| 481 | rel, _ := filepath.Rel(root, path) |
| 482 | out[filepath.ToSlash(rel)] = string(b) |
| 483 | } |
| 484 | return nil |
| 485 | }) |
| 486 | if err != nil { |
| 487 | t.Fatal(err) |
| 488 | } |
| 489 | return out |
| 490 | } |
| 491 | |
| 492 | // §3 rule 6: 30 writes per owner per hour, every owner POST counted; the |
| 493 | // 31st is refused with the list redrawn, a confirm included. |
| 494 | func TestTeamWriteBudget(t *testing.T) { |
| 495 | f := newTeamFixture(t) |
| 496 | target := f.person(t, "target@example.invalid") |
| 497 | f.member(t, f.sqid, target, "member") |
| 498 | owner := f.session(t, f.owner, time.Now()) |
| 499 | a := f.startAdd(t, owner, f.ownerCred, "member", "") // write 1 |
| 500 | f.resolveAddress(t, a, "budget@example.invalid") |
| 501 | if r := f.resolved(a.receipt, owner, a.intent); r.Code != 303 { |
| 502 | t.Fatal("resolved", r.Code) |
| 503 | } |
| 504 | form := url.Values{"team": {f.sqid}, "person": {target}, "name": {"Till"}} |
| 505 | for i := 1; i < teamWriteBudget; i++ { |
| 506 | if r := f.post(teamNamePath, form, owner); r.Code != 303 { |
| 507 | t.Fatal(i, r.Code) |
| 508 | } |
| 509 | } |
| 510 | r := f.post(teamNamePath, url.Values{"team": {f.sqid}, "person": {target}, "name": {"Other"}}, owner) |
| 511 | if r.Code != 429 || !strings.Contains(r.Body.String(), "Too many changes") || !strings.Contains(f.get(f.teamURL(), owner).Body.String(), "Till") { |
| 512 | t.Fatal("31st write", r.Code) |
| 513 | } |
| 514 | members := f.countRows(t, f.directory, "memberships") |
| 515 | if r := f.post(teamConfirmPath, url.Values{}, owner, a.intent); r.Code != 429 || f.countRows(t, f.directory, "memberships") != members { |
| 516 | t.Fatal("a confirm past the budget", r.Code) |
| 517 | } |
| 518 | // A member's refused POST does not spend an owner's budget, and is 404. |
| 519 | if r := f.post(teamNamePath, form, f.session(t, target, time.Now())); r.Code != 404 { |
| 520 | t.Fatal(r.Code) |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // A team with a live Equipo or Base says, on the Team page, that a change |
| 525 | // there does not reach those two (oficina/alta design/team-bundle, spec |
| 526 | // §5.1 rule 1); every Team page names who can see the team's data. |
| 527 | func TestTeamPageSaysSeededAppsKeepTheirOwnPeople(t *testing.T) { |
| 528 | f := newTeamFixture(t) |
| 529 | member := f.person(t, "member@example.invalid") |
| 530 | f.member(t, f.sqid, member, "member") |
| 531 | ownerCookie := f.session(t, f.owner, time.Now().Add(-time.Hour)) |
| 532 | const ( |
| 533 | removeSeeded = "except Equipo and Base, which keep their own list of people" |
| 534 | noteSeeded = "change who is on those in Equipo and Base themselves" |
| 535 | removePlain = "<p>They lose this team in every app within a minute.</p>" |
| 536 | notePlain = "Changes reach every app in this team within a minute.</p>" |
| 537 | operatorLine = "Paul Campbell runs this Oficina and can see everything this team keeps in it." |
| 538 | ) |
| 539 | body := f.get(f.teamURL(), ownerCookie).Body.String() |
| 540 | if !strings.Contains(body, removePlain) || !strings.Contains(body, notePlain) || strings.Contains(body, removeSeeded) || strings.Contains(body, noteSeeded) || !strings.Contains(body, operatorLine) { |
| 541 | t.Fatal("team without Equipo or Base: wrong lines") |
| 542 | } |
| 543 | // A pending Equipo is not yet one: only a live instance counts. |
| 544 | exec := func(state string) { |
| 545 | t.Helper() |
| 546 | if _, err := f.directory.db.Writer().Exec("INSERT OR REPLACE INTO team_instances VALUES(?,?,?,?,?)", 1, "equipo", "https://canarylabel.equipo.oficina.works", "canarylabel-equipo", state); err != nil { |
| 547 | t.Fatal(err) |
| 548 | } |
| 549 | } |
| 550 | exec("pending") |
| 551 | if body = f.get(f.teamURL(), ownerCookie).Body.String(); strings.Contains(body, removeSeeded) { |
| 552 | t.Fatal("a pending Equipo changed the lines") |
| 553 | } |
| 554 | exec("live") |
| 555 | body = f.get(f.teamURL(), ownerCookie).Body.String() |
| 556 | if !strings.Contains(body, removeSeeded) || !strings.Contains(body, noteSeeded) || strings.Contains(body, removePlain) || strings.Contains(body, notePlain) || !strings.Contains(body, operatorLine) { |
| 557 | t.Fatal("team with a live Equipo: wrong lines") |
| 558 | } |
| 559 | mem := f.get(f.teamURL(), f.session(t, member, time.Now())).Body.String() |
| 560 | if !strings.Contains(mem, noteSeeded) || !strings.Contains(mem, operatorLine) || strings.Contains(mem, "@") && strings.Contains(mem, "member@example.invalid") { |
| 561 | t.Fatal("member view: wrong lines, or an address") |
| 562 | } |
| 563 | } |
| 564 | |