| new file mode 100644 |
| index 0000000..b7b9a23 |
| --- /dev/null |
| +++ b/internal/home/directorydata/roster_pending_test.go |
| @@ -0,0 +1,197 @@ |
| +package directorydata |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "crypto/sha256" |
| + "database/sql" |
| + "errors" |
| + "math" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func pendingFixture(t *testing.T) (*RosterSource, RosterRequest, string, int64) { |
| + t.Helper() |
| + s, r, path, team := rosterFixture(t, OperatorSchema) |
| + member(t, s.store, team, personA, "owner") |
| + digest := bytes.Repeat([]byte{33}, 32) |
| + tx, e := s.store.db.Begin() |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + defer tx.Rollback() |
| + for _, q := range []struct { |
| + sql string |
| + args []any |
| + }{ |
| + {`UPDATE team_instances SET state='pending' WHERE team_id=?`, []any{team}}, |
| + {`INSERT INTO roster_operator_receipts VALUES('provision-alpha',1,'provision','alpha-docs',?,?,?)`, []any{challenge(3), digest, rosterNow.Unix() - 1}}, |
| + {`INSERT INTO directory_operator_consumer_records VALUES('alpha-docs','provision-alpha',1,?,?)`, []any{challenge(3), digest}}, |
| + {`INSERT INTO verification_attempts SELECT ?,'alpha-docs',id,'docs','https://alpha.docs.oficina.works',?,1,?,roster_epoch,?,?,?,? FROM teams WHERE id=?`, []any{challenge(21), challenge(3), digest, personA, rosterNow.Unix(), rosterNow.Unix() + 600, bytes.Repeat([]byte{34}, 32), team}}, |
| + {`INSERT INTO verification_active VALUES('alpha-docs',?)`, []any{challenge(21)}}, |
| + } { |
| + if _, e = tx.Exec(q.sql, q.args...); e != nil { |
| + t.Fatal(e) |
| + } |
| + } |
| + if e = tx.Commit(); e != nil { |
| + t.Fatal(e) |
| + } |
| + return s, r, path, team |
| +} |
| +func pendingStatus(t *testing.T, s *RosterSource, r RosterRequest, at time.Time, want int) { |
| + t.Helper() |
| + p, status, e := s.Read(context.Background(), r, at) |
| + if status != want { |
| + t.Fatalf("status %d, want %d", status, want) |
| + } |
| + if want == 503 && (!errors.Is(e, ErrUnavailable) || p.Type != "") { |
| + t.Fatal("pending refusal differs") |
| + } |
| + if want == 200 && (e != nil || p.Type != "page") { |
| + t.Fatal("missing page") |
| + } |
| +} |
| +func TestPendingRosterWindowBoundariesAndChooser(t *testing.T) { |
| + s, r, _, _ := pendingFixture(t) |
| + for _, offset := range []int64{0, 599, 600, 601} { |
| + want := 200 |
| + if offset >= 600 { |
| + want = 503 |
| + } |
| + pendingStatus(t, s, r, rosterNow.Add(time.Duration(offset)*time.Second), want) |
| + } |
| + choices, e := s.store.Choices(context.Background(), personA, "docs") |
| + if e != nil || len(choices) != 0 { |
| + t.Fatal("pending binding visible") |
| + } |
| +} |
| +func TestPendingRosterRefusesBrokenAuthority(t *testing.T) { |
| + cases := []struct { |
| + name, sql string |
| + args []any |
| + }{ |
| + {"missing active", `DELETE FROM verification_active`, nil}, |
| + {"missing pointer", `DELETE FROM directory_operator_consumer_records`, nil}, |
| + {"wrong generation", `UPDATE verification_attempts SET generation=?`, []any{challenge(4)}}, |
| + {"changed current generation", `UPDATE roster_generation SET generation=?`, []any{challenge(4)}}, |
| + {"wrong record sequence", `UPDATE verification_attempts SET record_sequence=2`, nil}, |
| + {"wrong record digest", `UPDATE verification_attempts SET record_digest=?`, []any{bytes.Repeat([]byte{5}, 32)}}, |
| + {"wrong pointer generation", `UPDATE directory_operator_consumer_records SET generation=?`, []any{challenge(4)}}, |
| + {"wrong pointer receipt", `UPDATE roster_operator_receipts SET sequence=2`, nil}, |
| + {"wrong owner", `UPDATE verification_attempts SET owner_ref=?`, []any{personB}}, |
| + {"owner removed", `DELETE FROM memberships WHERE person_ref=?`, []any{personA}}, |
| + {"owner demoted", `UPDATE memberships SET role='member' WHERE person_ref=?`, []any{personA}}, |
| + {"wrong team", `UPDATE verification_attempts SET team_id=(SELECT id FROM teams WHERE sqid='B2')`, nil}, |
| + {"wrong app", `UPDATE verification_attempts SET app='sheets'`, nil}, |
| + {"wrong origin", `UPDATE verification_attempts SET origin='https://beta.docs.oficina.works'`, nil}, |
| + {"future issued", `UPDATE verification_attempts SET issued_at=issued_at+1,expires_at=expires_at+1`, nil}, |
| + {"negative issued", `UPDATE verification_attempts SET issued_at=-1`, nil}, |
| + {"zero issued", `UPDATE verification_attempts SET issued_at=0`, nil}, |
| + {"expired", `UPDATE verification_attempts SET issued_at=issued_at-600,expires_at=expires_at-600`, nil}, |
| + {"long lifetime", `UPDATE verification_attempts SET expires_at=expires_at+1`, nil}, |
| + {"overflow lifetime", `UPDATE verification_attempts SET issued_at=?,expires_at=?`, []any{int64(-1), int64(math.MaxInt64)}}, |
| + {"reversed lifetime", `UPDATE verification_attempts SET expires_at=issued_at`, nil}, |
| + {"partial authorization", `UPDATE verification_attempts SET authorization_digest=x'00'`, nil}, |
| + } |
| + for _, tc := range cases { |
| + t.Run(tc.name, func(t *testing.T) { |
| + s, r, _, _ := pendingFixture(t) |
| + exec(t, s.store.db, `PRAGMA ignore_check_constraints=ON`) |
| + exec(t, s.store.db, tc.sql, tc.args...) |
| + pendingStatus(t, s, r, rosterNow, 503) |
| + }) |
| + } |
| +} |
| +func TestPendingRosterTokenScopeAndLiveUnchanged(t *testing.T) { |
| + s, r, _, team := pendingFixture(t) |
| + bad := r |
| + bad.Token = challenge(17) |
| + pendingStatus(t, s, bad, rosterNow, 401) |
| + bad = r |
| + bad.Team = "B2" |
| + pendingStatus(t, s, bad, rosterNow, 404) |
| + bad.Team = "Missing" |
| + pendingStatus(t, s, bad, rosterNow, 404) |
| + token := challenge(18) |
| + digest := sha256.Sum256([]byte(token)) |
| + exec(t, s.store.db, `UPDATE roster_tokens SET slot='previous',issued_at=?,expires_at=?`, rosterNow.Unix(), rosterNow.Unix()+300) |
| + exec(t, s.store.db, `INSERT INTO roster_tokens VALUES(?,'alpha-docs','current',?,NULL)`, digest[:], rosterNow.Unix()) |
| + pendingStatus(t, s, r, rosterNow.Add(299*time.Second), 200) |
| + pendingStatus(t, s, r, rosterNow.Add(300*time.Second), 401) |
| + r.Token = token |
| + pendingStatus(t, s, r, rosterNow.Add(599*time.Second), 200) |
| + exec(t, s.store.db, `UPDATE roster_consumers SET enabled=0`) |
| + pendingStatus(t, s, r, rosterNow, 401) |
| + exec(t, s.store.db, `UPDATE roster_consumers SET enabled=1`) |
| + exec(t, s.store.db, `UPDATE team_instances SET state='live' WHERE team_id=?`, team) |
| + exec(t, s.store.db, `DELETE FROM verification_active`) |
| + pendingStatus(t, s, r, rosterNow.Add(601*time.Second), 200) |
| +} |
| +func TestPendingRosterEpochAndExpiryOnCursor(t *testing.T) { |
| + s, r, _, team := pendingFixture(t) |
| + exec(t, s.store.db, `DELETE FROM memberships WHERE team_id=?`, team) |
| + addRoster(t, s, team, 200) |
| + exec(t, s.store.db, `UPDATE memberships SET role='owner' WHERE team_id=? AND person_ref=?`, team, personA) |
| + p, status, e := s.Read(context.Background(), r, rosterNow.Add(595*time.Second)) |
| + if e != nil || status != 200 || p.Cursor == nil { |
| + t.Fatal("missing first page") |
| + } |
| + r.Cursor = *p.Cursor |
| + pendingStatus(t, s, r, rosterNow.Add(599*time.Second), 200) |
| + pendingStatus(t, s, r, rosterNow.Add(600*time.Second), 503) |
| + r.Cursor = "" |
| + p, _, e = s.Read(context.Background(), r, rosterNow) |
| + if e != nil || p.Cursor == nil { |
| + t.Fatal("missing cursor") |
| + } |
| + r.Cursor = *p.Cursor |
| + member(t, s.store, team, personB, "member") |
| + p, status, e = s.Read(context.Background(), r, rosterNow) |
| + if e != nil || status != 409 || p.Members != nil { |
| + t.Fatal("epoch drift was not conflict") |
| + } |
| + r.Cursor = "" |
| + pendingStatus(t, s, r, rosterNow, 200) |
| + exec(t, s.store.db, `DELETE FROM memberships WHERE person_ref=?`, personA) |
| + pendingStatus(t, s, r, rosterNow, 503) |
| +} |
| + |
| +func TestPendingRosterWindowCannotCrossConsumers(t *testing.T) { |
| + s, r, _, _ := pendingFixture(t) |
| + var beta int64 |
| + if e := s.store.db.QueryRow(`SELECT id FROM teams WHERE sqid='B2'`).Scan(&beta); e != nil { |
| + t.Fatal(e) |
| + } |
| + exec(t, s.store.db, `UPDATE team_instances SET state='pending' WHERE team_id=?`, beta) |
| + exec(t, s.store.db, `INSERT INTO roster_consumers VALUES('beta-docs',?,'docs','https://beta.docs.oficina.works',1)`, beta) |
| + token := challenge(19) |
| + digest := sha256.Sum256([]byte(token)) |
| + exec(t, s.store.db, `INSERT INTO roster_tokens VALUES(?,'beta-docs','current',?,NULL)`, digest[:], rosterNow.Unix()) |
| + exec(t, s.store.db, `DELETE FROM verification_active`) |
| + exec(t, s.store.db, `INSERT INTO verification_active VALUES('beta-docs',?)`, challenge(21)) |
| + r.Team = "B2" |
| + r.Token = token |
| + pendingStatus(t, s, r, rosterNow, 503) |
| +} |
| + |
| +func TestPendingRosterReadSnapshotIncludesWindow(t *testing.T) { |
| + s, r, path, team := pendingFixture(t) |
| + exec(t, s.store.db, `DELETE FROM memberships WHERE team_id=?`, team) |
| + addRoster(t, s, team, 201) |
| + exec(t, s.store.db, `UPDATE memberships SET role='owner' WHERE team_id=? AND person_ref=?`, team, personA) |
| + p, _, e := s.Read(context.Background(), r, rosterNow) |
| + if e != nil || p.Cursor == nil { |
| + t.Fatal("missing page") |
| + } |
| + r.Cursor = *p.Cursor |
| + writer, e := sql.Open("sqlite", path) |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + defer writer.Close() |
| + s.store.sealer = &onOpenSealer{Sealer: s.store.sealer, once: func() { exec(t, writer, `DELETE FROM verification_active`) }} |
| + pendingStatus(t, s, r, rosterNow, 200) |
| + pendingStatus(t, s, r, rosterNow, 503) |
| +} |