Add immutable social audit records

This commit is contained in:
ZacharyZhang-NY committed 2026-05-23 21:23:44 -04:00
1 parent eab282d8b1
commit a21d35a165
12 files changed
+254 -6

No files matched your search

@@ -23,7 +23,7 @@ describe("domain services", () => {
const connection = createConnection();
const rows = connection.db.prepare("select version from schema_migrations order by version asc").all() as Array<{ version: number }>;
expect(rows.map((row) => row.version)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]);
expect(rows.map((row) => row.version)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]);
connection.close();
});
@@ -150,6 +150,7 @@ describe("migration service", () => {
create table browser_action_policies(id text primary key, org_id text, social_account_id text);
create table social_approval_routes(id text primary key, org_id text, social_account_id text, route_to_id text);
create table browser_actions(id text primary key, org_id text, social_account_id text, evidence_file_id text);
create table social_audit_records(id text primary key, org_id text, social_account_id text, evidence_file_id text);
create table scheduled_posts(id text primary key, org_id text, social_account_id text, approval_id text, created_by_employee_id text);
create table policy_rules(id text primary key, org_id text);
create table compliance_exports(id text primary key, org_id text, requested_by_employee_id text);
@@ -218,6 +219,7 @@ describe("migration service", () => {
insert into browser_action_policies(id, org_id, social_account_id) values ('browser-policy-1', 'org-1', 'social-1');
insert into social_approval_routes(id, org_id, social_account_id, route_to_id) values ('social-route-1', 'org-1', 'social-1', 'emp-1');
insert into browser_actions(id, org_id, social_account_id, evidence_file_id) values ('browser-1', 'org-1', 'social-1', 'file-1');
insert into social_audit_records(id, org_id, social_account_id, evidence_file_id) values ('social-audit-1', 'org-1', 'social-1', 'file-1');
insert into scheduled_posts(id, org_id, social_account_id, approval_id, created_by_employee_id) values ('post-1', 'org-1', 'social-1', 'approval-1', 'emp-1');
insert into policy_rules(id, org_id) values ('policy-1', 'org-1');
insert into compliance_exports(id, org_id, requested_by_employee_id) values ('cmp-1', 'org-1', 'emp-1');
@@ -256,6 +258,7 @@ describe("migration service", () => {
expect(body.tableCounts.browser_session_containers).toBe(1);
expect(body.tableCounts.browser_action_policies).toBe(1);
expect(body.tableCounts.social_approval_routes).toBe(1);
expect(body.tableCounts.social_audit_records).toBe(1);
expect(body.tableCounts.compliance_exports).toBe(1);
expect(body.tableCounts.audit_replay_runs).toBe(1);
expect(body.tableCounts.rollback_snapshots).toBe(1);
@@ -278,6 +281,7 @@ describe("migration service", () => {
expect(body.idMaps.browser_session_containers).toEqual(["container-1"]);
expect(body.idMaps.browser_action_policies).toEqual(["browser-policy-1"]);
expect(body.idMaps.social_approval_routes).toEqual(["social-route-1"]);
expect(body.idMaps.social_audit_records).toEqual(["social-audit-1"]);
connection.close();
});
@@ -81,6 +81,14 @@ describe("social service", () => {
approvalKind: "social_publish",
routeToId: employeeId,
});
expect(state.socialAuditRecords[0]).toMatchObject({
socialAccountId: state.socialAccounts[0]?.id,
sourceType: "social_account",
eventType: "account_created",
actorType: "employee",
actorId: employeeId,
policyDecision: "allowed_employee_authorization",
});
expect(state.auditEvents.find((event) => event.entityType === "social_account")?.actorId).toBe(employeeId);
expect(grantScope.actions).toEqual(["login", "draft", "approve"]);
expect(grantScope.riskCeiling).toBe("medium");
@@ -153,6 +161,47 @@ describe("social service", () => {
expect(state.scheduledPosts[0]?.state).toBe("published");
expect(state.scheduledPosts[0]?.socialAccountId).toBe(socialAccountId);
expect(state.scheduledPosts[0]?.approvalId).toBe(state.approvals[0]?.id);
expect(state.socialAuditRecords.map((record) => record.eventType).sort()).toEqual([
"account_created",
"draft_created",
"state_published",
"state_scheduled",
]);
expect(state.socialAuditRecords.find((record) => record.eventType === "state_scheduled")).toMatchObject({
sourceId: postId,
policyDecision: "approval_requested",
});
expect(state.socialAuditRecords.find((record) => record.eventType === "state_published")).toMatchObject({
sourceId: postId,
policyDecision: "allowed_after_approval",
});
connection.close();
});
test("keeps social audit records immutable", () => {
const connection = createConnection();
const organization = new OrganizationService(connection.db);
const social = new SocialService(connection.db);
let auditId = "";
connection.transaction(() => {
const orgId = createOrganization(organization);
const employeeId = firstEmployeeId(connection, orgId);
social.createAccount({
orgId,
platform: SOCIAL_PLATFORM.X,
handle: "@acme",
displayName: "Acme",
createdByEmployeeId: employeeId,
authorizedEmployeeId: employeeId,
});
auditId = (connection.db.prepare("select id from social_audit_records limit 1").get() as { id: string }).id;
});
expect(() => connection.db.prepare("update social_audit_records set policy_decision = ? where id = ?")
.run("changed", auditId)).toThrow("Social audit records are immutable.");
expect(() => connection.db.prepare("delete from social_audit_records where id = ?")
.run(auditId)).toThrow("Social audit records are immutable.");
connection.close();
});