From 524f5009d3fc5b2407479a40df4484c0fbec52a1 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:55:28 +0100 Subject: [PATCH] test(migrations): intake reports keep their workspace (0030) TestSeed_canInsertAndQuery deleted a tenant that had intake reports, which 0030 forbids on purpose (reports outlive their workspace). The intake rows now sit on a workspace of their own; the test checks that its delete is refused and the rows stay, and the cascade check keeps the other tenant. Co-Authored-By: Claude Opus 5.5 --- migrations/migrations_test.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/migrations/migrations_test.go b/migrations/migrations_test.go index fc71a7d..3f4a956 100644 --- a/migrations/migrations_test.go +++ b/migrations/migrations_test.go @@ -340,17 +340,22 @@ func TestSeed_canInsertAndQuery(t *testing.T) { } // 0028: the R1 intake, insert-only, one capture per report, one - // settling receipt per entry. + // settling receipt per entry. On a workspace of its own: since 0030 a + // workspace with intake reports cannot be deleted (checked below). + var itid string + if err := db.QueryRowContext(ctx, `INSERT INTO tenants (slug, name, plan, status, kind) VALUES ('intake-co', 'Intake Co', 'professional', 'active', 'customer') RETURNING id`).Scan(&itid); err != nil { + t.Fatalf("insert intake tenant: %v", err) + } if _, err := db.ExecContext(ctx, `INSERT INTO intake_reports (tenant_id, reference, seq, received_at, channel, reporter_sub, about, danger) - VALUES ($1, 'SH-2026-00001', 1, now(), 'portal', 'u-1', 'product', 'no')`, tid); err != nil { + VALUES ($1, 'SH-2026-00001', 1, now(), 'portal', 'u-1', 'product', 'no')`, itid); err != nil { t.Fatalf("insert intake report: %v", err) } insEntry := `INSERT INTO intake_entries (tenant_id, reference, kind, content, by_sub, at) VALUES ($1, 'SH-2026-00001', $2, '{"what":"x"}', 'u-1', now()) RETURNING id` var entryID int64 - if err := db.QueryRowContext(ctx, insEntry, tid, "capture").Scan(&entryID); err != nil { + if err := db.QueryRowContext(ctx, insEntry, itid, "capture").Scan(&entryID); err != nil { t.Fatalf("insert intake capture: %v", err) } - if _, err := db.ExecContext(ctx, insEntry, tid, "capture"); err == nil { + if _, err := db.ExecContext(ctx, insEntry, itid, "capture"); err == nil { t.Error("a second capture of the same report was stored") } insReceipt := `INSERT INTO intake_receipts (entry_id, status, by_client, at) VALUES ($1, $2, 'werkmeister-svc', now())` @@ -359,7 +364,7 @@ func TestSeed_canInsertAndQuery(t *testing.T) { } for name, stmt := range map[string]string{ "second settling receipt": `INSERT INTO intake_receipts (entry_id, status, by_client, at) VALUES (` + strconv.FormatInt(entryID, 10) + `, 'refused', 'w', now())`, - "bad reference": `INSERT INTO intake_reports (tenant_id, reference, seq, received_at, channel, reporter_sub, about, danger) VALUES ('` + tid + `', 'X-1', 2, now(), 'portal', 'u', 'product', 'no')`, + "bad reference": `INSERT INTO intake_reports (tenant_id, reference, seq, received_at, channel, reporter_sub, about, danger) VALUES ('` + itid + `', 'X-1', 2, now(), 'portal', 'u', 'product', 'no')`, "changed report": `UPDATE intake_reports SET danger = 'yes'`, "changed entry": `UPDATE intake_entries SET content = '{}'`, "changed receipt": `UPDATE intake_receipts SET status = 'refused'`, @@ -369,15 +374,23 @@ func TestSeed_canInsertAndQuery(t *testing.T) { } } + // 0030: security reports outlive their workspace, so the database + // refuses to delete a workspace that has intake reports. + if _, err := db.ExecContext(ctx, `DELETE FROM tenants WHERE id = $1`, itid); err == nil { + t.Error("a workspace with intake reports was deleted") + } + var nReports, nEntries int + _ = db.QueryRowContext(ctx, `SELECT COUNT(*) FROM intake_reports WHERE tenant_id = $1`, itid).Scan(&nReports) + _ = db.QueryRowContext(ctx, `SELECT COUNT(*) FROM intake_entries WHERE tenant_id = $1`, itid).Scan(&nEntries) + if nReports != 1 || nEntries != 1 { + t.Errorf("intake after a refused delete: reports=%d entries=%d, want 1/1", nReports, nEntries) + } + // FK cascade — delete tenant, projects/products/keys/audit_log handling. if _, err := db.ExecContext(ctx, `DELETE FROM tenants WHERE id = $1`, tid); err != nil { t.Fatalf("delete tenant: %v", err) } - var nServices, nSettings, nContractDocs, nIntake int - _ = db.QueryRowContext(ctx, `SELECT COUNT(*) FROM intake_entries WHERE tenant_id = $1`, tid).Scan(&nIntake) - if nIntake != 0 { - t.Errorf("FK cascade incomplete: intake_entries=%d", nIntake) - } + var nServices, nSettings, nContractDocs int _ = db.QueryRowContext(ctx, `SELECT COUNT(*) FROM managed_services WHERE tenant_id = $1`, tid).Scan(&nServices) _ = db.QueryRowContext(ctx, `SELECT COUNT(*) FROM tenant_product_settings WHERE tenant_id = $1`, tid).Scan(&nSettings) _ = db.QueryRowContext(ctx, `SELECT COUNT(*) FROM tenant_contract_documents WHERE tenant_id = $1`, tid).Scan(&nContractDocs)