diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1eed1c4e2..2e687c285 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -355,6 +355,18 @@ jobs: WARMBLY_TEST_DB: postgres://warmbly:warmbly@127.0.0.1:5432/warmbly_test?sslmode=disable run: go test -race ./internal/repository -run '^TestLiveInvitedSignup$' -count=1 + - name: Tester initial password and sample data regressions (PostgreSQL) + if: matrix.suite == 'live-1' + env: + WARMBLY_TEST_DB: postgres://warmbly:warmbly@127.0.0.1:5432/warmbly_test?sslmode=disable + run: | + set -o pipefail + go test -race -v -p 1 ./internal/repository -run '^TestLive(InitialPasswordLifecycle|ResetPasswordClearsAnIssuedExpiry|ReissueTesterPassword|TesterSampleData(IsSafeAndPreservesEdits|RollsBackAndSerializesRetries|RejectsNonTestAndInactiveWorkspaces|SurvivesInitialPasswordReplacement|ListsTheSeedableWorkspace))$' -count=1 | tee tester-lifecycle.log + for test in TestLiveInitialPasswordLifecycle TestLiveResetPasswordClearsAnIssuedExpiry TestLiveReissueTesterPassword TestLiveTesterSampleDataIsSafeAndPreservesEdits TestLiveTesterSampleDataRollsBackAndSerializesRetries TestLiveTesterSampleDataRejectsNonTestAndInactiveWorkspaces TestLiveTesterSampleDataSurvivesInitialPasswordReplacement TestLiveTesterSampleDataListsTheSeedableWorkspace; do + grep -q -- "--- PASS: $test " tester-lifecycle.log + done + if grep -q -- '--- SKIP:' tester-lifecycle.log; then exit 1; fi + - name: Mailbox batch and folder identity regressions (PostgreSQL) if: matrix.suite == 'live-1' env: diff --git a/admin/src/app/dashboard/TestersPage.test.tsx b/admin/src/app/dashboard/TestersPage.test.tsx index a6d7ff0b4..6da3121cd 100644 --- a/admin/src/app/dashboard/TestersPage.test.tsx +++ b/admin/src/app/dashboard/TestersPage.test.tsx @@ -38,7 +38,7 @@ describe("tester sample-data controls", () => { beforeEach(() => { access.canManage = true; }); it("offers sample data for an active owned Test workspace", () => { - const document = render({ test_workspace_id: "test-workspace" }); + const document = render({ test_workspace_id: "test-workspace", sample_data_eligible: true }); const button = document.querySelector('button[aria-label="Add sample data for reviewer@example.test"]'); expect(button?.disabled).toBe(false); expect(button?.textContent).toBe("Add sample data"); @@ -51,9 +51,17 @@ describe("tester sample-data controls", () => { expect(render({ test_workspace_id: "test-workspace" }).querySelector('button[aria-label^="Add sample data for"]')).toBeNull(); }); - it("disables populated, expired and passwordless Test workspaces", () => { + it("keeps offering sample data after the tester replaces the initial password", () => { + const document = render({ test_workspace_id: "test-workspace", password_expires_at: null, sample_data_eligible: true }); + const button = document.querySelector('button[aria-label="Add sample data for reviewer@example.test"]'); + expect(button?.disabled).toBe(false); + }); + + it("disables populated and ineligible Test workspaces", () => { for (const tester of [ - { sample_data_seeded_at: "2026-01-01T00:00:00Z" }, + { sample_data_eligible: true, sample_data_seeded_at: "2026-01-01T00:00:00Z" }, + { sample_data_eligible: false }, + // An older backend sends no eligibility; the outstanding password decides. { password_expires_at: "2020-01-01T00:00:00Z" }, { password_expires_at: null }, ]) { diff --git a/admin/src/app/dashboard/TestersPage.tsx b/admin/src/app/dashboard/TestersPage.tsx index c3ca32730..4acf7b958 100644 --- a/admin/src/app/dashboard/TestersPage.tsx +++ b/admin/src/app/dashboard/TestersPage.tsx @@ -334,6 +334,8 @@ export default function TestersPage() { {rows.map((t) => { const expired = !!t.password_expires_at && new Date(t.password_expires_at) <= new Date(); + // A backend from before sample_data_eligible gated seeding on the outstanding password. + const canSeed = t.sample_data_eligible ?? (!!t.password_expires_at && !expired); return (
  • setSampleTarget({ organizationID: t.test_workspace_id!, email: t.email })} > diff --git a/admin/src/lib/api/models/admin.ts b/admin/src/lib/api/models/admin.ts index 30a4cfb66..16ed21b70 100644 --- a/admin/src/lib/api/models/admin.ts +++ b/admin/src/lib/api/models/admin.ts @@ -1015,6 +1015,8 @@ export interface LoginCodeExemption { password_expires_at?: string | null; test_workspace_id?: string | null; sample_data_seeded_at?: string | null; + /** The backend's seeding check: an active tester whose Test workspace grant is unexpired. */ + sample_data_eligible?: boolean; /** Created through the tester flow, so it can be issued a new password. */ created_as_tester?: boolean; } diff --git a/docs/content/docs/api/endpoints.mdx b/docs/content/docs/api/endpoints.mdx index a174cb577..19dcb4695 100644 --- a/docs/content/docs/api/endpoints.mdx +++ b/docs/content/docs/api/endpoints.mdx @@ -760,8 +760,8 @@ Alongside them, `api_url` is this API's own public base (for a copyable example - `GET /admin/organizations/:id/self-execution/mailboxes` (`view_organizations`) lists the workspace's mailboxes whose credential stays on a linked instance, as an array of `{email_account_id, email, trust_state, reported_samples, divergence, evaluated_at, last_leased_at}`. `trust_state` is `trusted`, `probation` or `untrusted`; `divergence` is the absolute difference between the mailbox's reported spam rate and the rate Cloud-run recipients saw from the same senders, as a fraction from 0 to 1, and is null before the first audit with samples. `last_leased_at` is when the instance last leased commands. - `GET /admin/organizations` and `GET /admin/organizations/:id` require `view_organizations`. Their acquisition fields include the nullable `referrer_host` saved at signup, alongside UTM source, medium, campaign and landing path. The list accepts an exact `referrer_host` filter as well as its existing acquisition filters. An absent source is unknown, not proof of a direct visit. See [signup source attribution](/guides/analytics/#signup-source-attribution-platform-admins). - `GET /admin/warmup-content/overview` includes effective generation controls and requires `view_warmup_pool`; `PUT /admin/warmup-content/settings` writes effective controls with `manage_settings`. These are platform-admin routes, not organization API-key scopes. See [effective generation policy and stopping](/development/warmup-generation/). - - `POST /admin/organizations/:id/sample-data`: `manage_testers` and an MFA-verified admin session. Adds the [reviewer sample dataset](/development/accounts-and-access/#adding-sample-data) only to an active dedicated Test workspace. No request body. Returns `200` with `organization_id`, `created` (false when already seeded) and `seeded_at`; retries preserve edits and deletions. An ordinary workspace, revoked or expired tester, or missing/expired managed Test grant returns `400`. Inserts and the admin audit marker commit together. No mailbox connection, email delivery, external request or credit spend occurs. - - `GET /admin/testers`: `view_users` and an MFA-verified admin session. Each tester in `data` may include `test_workspace_id` for its owned dedicated Test workspace and `sample_data_seeded_at` when populated. These fields are absent for a tester merely attached to an ordinary workspace. Every tester carries `created_as_tester`, true for an account the tester flow created. + - `POST /admin/organizations/:id/sample-data`: `manage_testers` and an MFA-verified admin session. Adds the [reviewer sample dataset](/development/accounts-and-access/#adding-sample-data) only to an active dedicated Test workspace. No request body. Returns `200` with `organization_id`, `created` (false when already seeded) and `seeded_at`; retries preserve edits and deletions. An ordinary workspace, revoked tester, or missing/expired managed Test grant returns `400`; whether the owner's initial password is outstanding, replaced or expired does not matter. Inserts and the admin audit marker commit together. No mailbox connection, email delivery, external request or credit spend occurs. + - `GET /admin/testers`: `view_users` and an MFA-verified admin session. Each tester in `data` may include `test_workspace_id` for its owned dedicated Test workspace (with several, the one that can still take sample data, else the oldest) and `sample_data_seeded_at` when populated. `sample_data_eligible` is true when that workspace passes the same check as `POST /admin/organizations/:id/sample-data`. These fields are absent for a tester merely attached to an ordinary workspace. Every tester carries `created_as_tester`, true for an account the tester flow created. - `POST /admin/testers/:id/password`: `manage_testers`, an MFA-verified admin session and a recent confirmation. No request body. Gives an account created as a tester a newly generated [initial password](/development/accounts-and-access/#initial-passwords), valid 48 hours and to be changed at first sign-in. It replaces whatever password the account held and signs out its sessions and remembered devices first. Returns `200` with `user_id`, `password` (shown only in this response), `password_expires_at` and `password_change_required: true`, and writes `reissue_tester_password` to the admin audit log. An account that does not exist or was not created as a tester returns `404` and nothing changes. Not idempotent by design: every call issues a different password, and only the last one works. - `PUT /admin/users/:id/login-code-exempt`: `manage_testers`, an MFA-verified admin session and a recent confirmation. Body `{"exempt": true, "reason": "..."}` excuses that account from the [emailed login code](/development/accounts-and-access/#excusing-one-account); the reason is required (at most 500 characters) and the acting admin is recorded as the grantor. `{"exempt": false}` ends the exemption, clears the password of an account created as a tester and signs out every session the account holds. Both write the admin audit log, and setting the same state again is safe. An unknown account returns `404`. `GET /admin/users/:id` and `/preview` carry `login_code_exempt`, `login_code_exempt_reason` and `login_code_exempt_at` - `GET /admin/sends/holds`: `view_campaigns` and an MFA-verified admin session. Lists mailboxes held from sending (up to `limit`, default 100, at most 500), switched-on first, with a `summary` of `total`, `active`, `inactive` and `by_reason`. Each row carries the held send's `held_task_id`, `recipients`, `message_id` and timestamps, and `resolvable` with a `blocker` when it is not. An unknown hold younger than ten minutes is a send in progress and is not listed. diff --git a/docs/content/docs/development/accounts-and-access.mdx b/docs/content/docs/development/accounts-and-access.mdx index a05842942..21d263ef8 100644 --- a/docs/content/docs/development/accounts-and-access.mdx +++ b/docs/content/docs/development/accounts-and-access.mdx @@ -143,7 +143,7 @@ The managed plan expires when the issued password would have expired, whether or ### Adding sample data -In **Admins > Testers**, select **Add sample data** beside an active tester who owns a dedicated **Test** workspace, then confirm. This works for both newly created workspaces and existing dedicated Test workspaces after upgrading. Nothing is added automatically during deployment or account creation. The action requires the platform `manage_testers` permission and an MFA-verified admin session. The backend checks the Test category, the owner's current exemption, that the owner's issued password is still outstanding (not yet expired or replaced), and an unexpired managed Test grant. Testers attached to ordinary customer workspaces cannot use it. +In **Admins > Testers**, select **Add sample data** beside an active tester who owns a dedicated **Test** workspace, then confirm. This works for both newly created workspaces and existing dedicated Test workspaces after upgrading. Nothing is added automatically during deployment or account creation. The action requires the platform `manage_testers` permission and an MFA-verified admin session. The backend checks the Test category, the owner's current exemption and an unexpired managed Test grant. The owner's initial password does not matter: the action stays available after the tester replaces it at first sign-in, or after you issue a new one, until the grant ends. Revoking the tester, or the grant expiring, ends it. A tester who owns more than one Test workspace is listed with the one that can still take sample data. Testers attached to ordinary customer workspaces cannot use it. The dataset contains: diff --git a/internal/models/user.go b/internal/models/user.go index a47c420f3..552c785aa 100644 --- a/internal/models/user.go +++ b/internal/models/user.go @@ -77,6 +77,8 @@ type LoginCodeExemption struct { PasswordExpiresAt *time.Time `json:"password_expires_at,omitempty"` TestWorkspaceID *uuid.UUID `json:"test_workspace_id,omitempty"` SampleDataSeededAt *time.Time `json:"sample_data_seeded_at,omitempty"` + // SampleDataEligible is the seeding check itself: an active tester with an unexpired Test grant. + SampleDataEligible bool `json:"sample_data_eligible"` // CreatedAsTester marks an account the tester flow created, which alone can be issued a new password. CreatedAsTester bool `json:"created_as_tester"` } diff --git a/internal/repository/pg_tester_sample_data.go b/internal/repository/pg_tester_sample_data.go index 8598e3fce..4e08a88ff 100644 --- a/internal/repository/pg_tester_sample_data.go +++ b/internal/repository/pg_tester_sample_data.go @@ -11,6 +11,15 @@ import ( var ErrTesterWorkspaceInactive = errors.New("sample data requires an active dedicated Test workspace") +// testerWorkspaceActiveSQL is the seeding authority over workspace o and owner u: an active tester +// and an unexpired managed Test grant, whatever state the owner's initial password is in. +func testerWorkspaceActiveSQL(planParam string) string { + return `o.category = 'test' AND u.login_code_exempt AND EXISTS ( + SELECT 1 FROM subscriptions s WHERE s.organization_id = o.id + AND s.managed_plan_id = ` + planParam + ` AND s.managed_at IS NOT NULL AND s.managed_until > now() + )` +} + func (r *organizationRepository) SeedTesterWorkspace(ctx context.Context, orgID, adminID uuid.UUID, ip, userAgent string) (*models.TesterSampleData, error) { tx, err := r.db.Begin(ctx) if err != nil { @@ -21,12 +30,7 @@ func (r *organizationRepository) SeedTesterWorkspace(ctx context.Context, orgID, var ownerID uuid.UUID err = tx.QueryRow(ctx, ` SELECT o.owner_user_id FROM organizations o JOIN users u ON u.id = o.owner_user_id - WHERE o.id = $1 AND o.category = 'test' AND u.login_code_exempt - AND u.password_expires_at > now() - AND EXISTS ( - SELECT 1 FROM subscriptions s WHERE s.organization_id = o.id - AND s.managed_plan_id = $2 AND s.managed_at IS NOT NULL AND s.managed_until > now() - ) + WHERE o.id = $1 AND `+testerWorkspaceActiveSQL("$2")+` FOR UPDATE OF o, u`, orgID, models.TestPlanID).Scan(&ownerID) if errors.Is(err, pgx.ErrNoRows) { return nil, ErrTesterWorkspaceInactive diff --git a/internal/repository/pg_user.go b/internal/repository/pg_user.go index f93415220..0b82a1aa9 100644 --- a/internal/repository/pg_user.go +++ b/internal/repository/pg_user.go @@ -384,13 +384,17 @@ func (r *userRepository) SetLoginCodeExempt(ctx context.Context, id uuid.UUID, e func (r *userRepository) ListLoginCodeExempt(ctx context.Context) ([]models.LoginCodeExemption, error) { rows, err := r.DB.Query(ctx, ` SELECT u.id, u.email, u.login_code_exempt_reason, u.login_code_exempt_at, u.password_expires_at, - workspace.id, seeded.created_at, + workspace.id, seeded.created_at, COALESCE(workspace.active, false), EXISTS (SELECT 1 FROM admin_audit_logs t WHERE t.action = 'create_tester' AND t.target_id = u.id) FROM users u LEFT JOIN LATERAL ( - SELECT o.id FROM organizations o + SELECT o.id, `+testerWorkspaceActiveSQL("$1")+` AS active FROM organizations o WHERE o.owner_user_id = u.id AND o.category = 'test' - ORDER BY o.created_at, o.id LIMIT 1 + -- Of several owned Test workspaces, list the one sample data can still be added to. + ORDER BY active DESC, EXISTS ( + SELECT 1 FROM admin_audit_logs a + WHERE a.target_type = 'organization' AND a.target_id = o.id AND a.action = 'seed_tester_workspace' + ), o.created_at, o.id LIMIT 1 ) workspace ON true LEFT JOIN LATERAL ( SELECT a.created_at FROM admin_audit_logs a @@ -399,7 +403,7 @@ func (r *userRepository) ListLoginCodeExempt(ctx context.Context) ([]models.Logi ORDER BY a.created_at LIMIT 1 ) seeded ON true WHERE u.login_code_exempt - ORDER BY u.login_code_exempt_at NULLS FIRST`) + ORDER BY u.login_code_exempt_at NULLS FIRST`, models.TestPlanID) if err != nil { return nil, err } @@ -408,7 +412,7 @@ func (r *userRepository) ListLoginCodeExempt(ctx context.Context) ([]models.Logi out := []models.LoginCodeExemption{} for rows.Next() { var e models.LoginCodeExemption - if err := rows.Scan(&e.UserID, &e.Email, &e.Reason, &e.GrantedAt, &e.PasswordExpiresAt, &e.TestWorkspaceID, &e.SampleDataSeededAt, &e.CreatedAsTester); err != nil { + if err := rows.Scan(&e.UserID, &e.Email, &e.Reason, &e.GrantedAt, &e.PasswordExpiresAt, &e.TestWorkspaceID, &e.SampleDataSeededAt, &e.SampleDataEligible, &e.CreatedAsTester); err != nil { return nil, err } out = append(out, e) diff --git a/internal/repository/tester_sample_data_live_test.go b/internal/repository/tester_sample_data_live_test.go index 33c704b10..57810804d 100644 --- a/internal/repository/tester_sample_data_live_test.go +++ b/internal/repository/tester_sample_data_live_test.go @@ -200,7 +200,6 @@ func TestLiveTesterSampleDataRejectsNonTestAndInactiveWorkspaces(t *testing.T) { name, query string }{ {"standard workspace", `UPDATE organizations SET category = 'standard' WHERE id = $1`}, - {"expired password", `UPDATE users SET password_expires_at = now() - interval '1 hour' WHERE id = (SELECT owner_user_id FROM organizations WHERE id = $1)`}, {"revoked exemption", `UPDATE users SET login_code_exempt = false WHERE id = (SELECT owner_user_id FROM organizations WHERE id = $1)`}, {"expired grant", `UPDATE subscriptions SET managed_until = now() - interval '1 hour' WHERE organization_id = $1`}, {"different managed plan", `UPDATE subscriptions SET managed_plan_id = '00000000-0000-0000-0000-000000000001' WHERE organization_id = $1`}, @@ -210,6 +209,9 @@ func TestLiveTesterSampleDataRejectsNonTestAndInactiveWorkspaces(t *testing.T) { if _, err := pool.Exec(ctx, tc.query, f.orgID); err != nil { t.Fatal(err) } + if testerListEntry(t, f).SampleDataEligible { + t.Fatal("tester list offers sample data for an ineligible workspace") + } if _, err := f.orgs.SeedTesterWorkspace(ctx, f.orgID, f.userID, "", ""); !errors.Is(err, ErrTesterWorkspaceInactive) { t.Fatalf("ineligible workspace returned %v", err) } @@ -217,3 +219,110 @@ func TestLiveTesterSampleDataRejectsNonTestAndInactiveWorkspaces(t *testing.T) { }) } } + +func testerListEntry(t *testing.T, f testerSampleFixture) models.LoginCodeExemption { + t.Helper() + list, err := f.users.ListLoginCodeExempt(context.Background()) + if err != nil { + t.Fatal(err) + } + for _, tester := range list { + if tester.UserID == f.userID { + return tester + } + } + return models.LoginCodeExemption{UserID: f.userID} +} + +func testerGrantUntil(t *testing.T, pool *pgxpool.Pool, orgID uuid.UUID) time.Time { + t.Helper() + var until time.Time + if err := pool.QueryRow(context.Background(), `SELECT managed_until FROM subscriptions WHERE organization_id = $1`, orgID).Scan(&until); err != nil { + t.Fatal(err) + } + return until +} + +// The grant, not the initial password, decides seeding: replacing or reissuing it keeps the workspace eligible. +func TestLiveTesterSampleDataSurvivesInitialPasswordReplacement(t *testing.T) { + db, pool := liveContactDB(t) + requireSchemaVersion(t, pool, 336) + ctx := context.Background() + f := newTesterSampleFixture(t, pool, NewUserRepostory(db, nil)) + auth := NewAuthRepostory(db) + if _, err := pool.Exec(ctx, `INSERT INTO admin_audit_logs (admin_user_id, action, target_type, target_id, details) + VALUES ($1, 'create_tester', 'user', $1, '{}'::jsonb)`, f.userID); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _, _ = pool.Exec(ctx, `DELETE FROM admin_audit_logs WHERE target_id = $1`, f.userID) }) + grant := testerGrantUntil(t, pool, f.orgID) + + if ok, xerr := auth.ReplaceInitialPassword(ctx, f.userID, "hash", "chosen-hash"); xerr != nil || !ok { + t.Fatalf("replace = %v, %v", ok, xerr) + } + if entry := testerListEntry(t, f); entry.PasswordExpiresAt != nil || !entry.SampleDataEligible { + t.Fatalf("after replacement: password_expires_at %v, eligible %v", entry.PasswordExpiresAt, entry.SampleDataEligible) + } + if _, err := f.users.ReissueTesterPassword(ctx, f.userID, "reissued-hash"); err != nil { + t.Fatal(err) + } + if ok, xerr := auth.ReplaceInitialPassword(ctx, f.userID, "reissued-hash", "second-chosen-hash"); xerr != nil || !ok { + t.Fatalf("replace reissued = %v, %v", ok, xerr) + } + if got := testerGrantUntil(t, pool, f.orgID); !got.Equal(grant) { + t.Fatalf("grant expiry moved from %v to %v", grant, got) + } + if !testerListEntry(t, f).SampleDataEligible { + t.Fatal("tester list withholds sample data from an active Test workspace") + } + + result, err := f.orgs.SeedTesterWorkspace(ctx, f.orgID, f.userID, "", "") + if err != nil || result == nil || !result.Created { + t.Fatalf("seed after replacement = %+v, %v", result, err) + } + assertTesterSampleCounts(t, pool, f.orgID, 1) + + if _, err := f.users.RevokeTester(ctx, f.userID); err != nil { + t.Fatal(err) + } + if _, err := f.orgs.SeedTesterWorkspace(ctx, f.orgID, f.userID, "", ""); !errors.Is(err, ErrTesterWorkspaceInactive) { + t.Fatalf("revoked tester returned %v", err) + } +} + +// A tester owning several Test workspaces is listed with the one sample data can still be added to. +func TestLiveTesterSampleDataListsTheSeedableWorkspace(t *testing.T) { + db, pool := liveContactDB(t) + requireSchemaVersion(t, pool, 336) + ctx := context.Background() + f := newTesterSampleFixture(t, pool, NewUserRepostory(db, nil)) + second := newTesterSampleFixture(t, pool, f.users) + for _, q := range []string{ + `UPDATE organizations SET created_at = now() - interval '1 day' WHERE id = $1`, + `UPDATE subscriptions SET managed_until = now() - interval '1 hour' WHERE organization_id = $1`, + } { + if _, err := pool.Exec(ctx, q, f.orgID); err != nil { + t.Fatal(err) + } + } + if _, err := pool.Exec(ctx, `UPDATE organizations SET owner_user_id = $2 WHERE id = $1`, second.orgID, f.userID); err != nil { + t.Fatal(err) + } + listed := func(want uuid.UUID, seeded bool) { + t.Helper() + entry := testerListEntry(t, f) + if entry.TestWorkspaceID == nil || *entry.TestWorkspaceID != want || !entry.SampleDataEligible || (entry.SampleDataSeededAt != nil) != seeded { + t.Fatalf("listed %v, eligible %v, seeded %v; want %v", entry.TestWorkspaceID, entry.SampleDataEligible, entry.SampleDataSeededAt, want) + } + } + + listed(second.orgID, false) + if _, err := f.orgs.SeedTesterWorkspace(ctx, second.orgID, f.userID, "", ""); err != nil { + t.Fatal(err) + } + listed(second.orgID, true) + if _, err := pool.Exec(ctx, `UPDATE subscriptions SET managed_until = now() + interval '1 day' WHERE organization_id = $1`, f.orgID); err != nil { + t.Fatal(err) + } + listed(f.orgID, false) +}