Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+447
@@ -0,0 +1,447 @@
|
||||
// @vitest-environment node
|
||||
import { createDbClient, getWriteDbClient } from "@hivegrid/db/client.server";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
forceArchiveChatAuditChannel,
|
||||
forceRemoveChatAuditMember,
|
||||
forceSetChatAuditMemberRole,
|
||||
getChatAuditChannelDetail,
|
||||
listChatAuditChannels,
|
||||
recordChatAuditSessionStarted,
|
||||
} from "./audit.server.js";
|
||||
import { CHAT_PERMISSIONS } from "./permissions.js";
|
||||
|
||||
const ADMIN_URL = process.env.RLS_TEST_DATABASE_URL;
|
||||
const describeIfDatabase = ADMIN_URL ? describe : describe.skip;
|
||||
|
||||
const TENANT_A = "chat_audit_rls_tenant_a";
|
||||
const TENANT_B = "chat_audit_rls_tenant_b";
|
||||
const AUDITOR_A = "chat_audit_rls_auditor_a";
|
||||
const MEMBER_A = "chat_audit_rls_member_a";
|
||||
const MEMBER_B = "chat_audit_rls_member_b";
|
||||
const CHAT_A = "chat_audit_rls_channel_a";
|
||||
const CHAT_B = "chat_audit_rls_channel_b";
|
||||
const MESSAGE_A = "chat_audit_rls_message_a";
|
||||
const MESSAGE_B = "chat_audit_rls_message_b";
|
||||
const ROLE_ID = "chat_audit_rls_role";
|
||||
const READ_OWN_PERMISSION_ID = "chat_audit_rls_read_own_permission";
|
||||
const READ_ALL_PERMISSION_ID = "chat_audit_rls_read_all_permission";
|
||||
const ADMIN_PERMISSION_ID = "chat_audit_rls_admin_permission";
|
||||
const READ_OWN_ROLE_PERMISSION_ID = "chat_audit_rls_role_read_own";
|
||||
const READ_ALL_ROLE_PERMISSION_ID = "chat_audit_rls_role_read_all";
|
||||
const ADMIN_ROLE_PERMISSION_ID = "chat_audit_rls_role_admin";
|
||||
const USER_ROLE_ID = "chat_audit_rls_user_role";
|
||||
const SESSION_ID = "chat_audit_rls_session";
|
||||
const LIST_REQUEST_ID = "chat_audit_rls_list_request";
|
||||
const SESSION_REQUEST_ID = "chat_audit_rls_session_request";
|
||||
const OPEN_REQUEST_ID = "chat_audit_rls_open_request";
|
||||
const ROLE_REQUEST_ID = "chat_audit_rls_role_request";
|
||||
const REMOVE_REQUEST_ID = "chat_audit_rls_remove_request";
|
||||
const ARCHIVE_REQUEST_ID = "chat_audit_rls_archive_request";
|
||||
const AUDIT_REASON = "Kundensupport-Fall 4711 pruefen";
|
||||
|
||||
const STALE_ROUTE_PERMISSIONS = [CHAT_PERMISSIONS.readOwnChannels, CHAT_PERMISSIONS.readAllChannels, CHAT_PERMISSIONS.admin] as const;
|
||||
|
||||
describeIfDatabase("chat admin audit with restricted FORCE RLS", () => {
|
||||
const admin = createDbClient({ connectionString: ADMIN_URL });
|
||||
const app = getWriteDbClient();
|
||||
let createdReadOwnPermission = false;
|
||||
let createdReadAllPermission = false;
|
||||
let createdAdminPermission = false;
|
||||
|
||||
beforeAll(async () => {
|
||||
await cleanup(admin);
|
||||
|
||||
await admin.user.createMany({
|
||||
data: [
|
||||
{
|
||||
id: AUDITOR_A,
|
||||
email: "chat-audit-auditor-a@example.test",
|
||||
firstName: "Audit",
|
||||
lastName: "Admin",
|
||||
active: true,
|
||||
},
|
||||
{
|
||||
id: MEMBER_A,
|
||||
email: "chat-audit-member-a@example.test",
|
||||
firstName: "Member",
|
||||
lastName: "A",
|
||||
active: true,
|
||||
},
|
||||
{
|
||||
id: MEMBER_B,
|
||||
email: "chat-audit-member-b@example.test",
|
||||
firstName: "Member",
|
||||
lastName: "B",
|
||||
active: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
await admin.tenant.createMany({
|
||||
data: [
|
||||
{ id: TENANT_A, slug: TENANT_A, name: "Chat audit RLS tenant A", active: true },
|
||||
{ id: TENANT_B, slug: TENANT_B, name: "Chat audit RLS tenant B", active: true },
|
||||
],
|
||||
});
|
||||
await admin.tenantUser.createMany({
|
||||
data: [
|
||||
{ id: "chat_audit_rls_tu_auditor_a", tenantId: TENANT_A, userId: AUDITOR_A, type: 0, joined: 1, status: 2 },
|
||||
{ id: "chat_audit_rls_tu_member_a", tenantId: TENANT_A, userId: MEMBER_A, type: 0, joined: 1, status: 2 },
|
||||
{ id: "chat_audit_rls_tu_member_b", tenantId: TENANT_B, userId: MEMBER_B, type: 0, joined: 1, status: 2 },
|
||||
],
|
||||
});
|
||||
await admin.teamChat.createMany({
|
||||
data: [
|
||||
{
|
||||
id: CHAT_A,
|
||||
tenantId: TENANT_A,
|
||||
createdByUserId: MEMBER_A,
|
||||
kind: "dm",
|
||||
name: null,
|
||||
isPrivate: true,
|
||||
eventStreamId: "chat_audit_rls_stream_a",
|
||||
dmPairKey: "chat_audit_rls_pair_a",
|
||||
lastMessageAt: new Date("2026-07-16T10:00:00.000Z"),
|
||||
},
|
||||
{
|
||||
id: CHAT_B,
|
||||
tenantId: TENANT_B,
|
||||
createdByUserId: MEMBER_B,
|
||||
kind: "group",
|
||||
name: "Foreign tenant channel",
|
||||
isPrivate: true,
|
||||
eventStreamId: "chat_audit_rls_stream_b",
|
||||
lastMessageAt: new Date("2026-07-16T11:00:00.000Z"),
|
||||
},
|
||||
],
|
||||
});
|
||||
await admin.teamChatMember.createMany({
|
||||
data: [
|
||||
{
|
||||
id: "chat_audit_rls_member_row_a",
|
||||
tenantId: TENANT_A,
|
||||
chatId: CHAT_A,
|
||||
userId: MEMBER_A,
|
||||
notificationPref: "all_messages",
|
||||
},
|
||||
{
|
||||
id: "chat_audit_rls_member_row_b",
|
||||
tenantId: TENANT_B,
|
||||
chatId: CHAT_B,
|
||||
userId: MEMBER_B,
|
||||
notificationPref: "all_messages",
|
||||
},
|
||||
],
|
||||
});
|
||||
await admin.teamChatMessage.createMany({
|
||||
data: [
|
||||
{
|
||||
id: MESSAGE_A,
|
||||
tenantId: TENANT_A,
|
||||
chatId: CHAT_A,
|
||||
authorUserId: MEMBER_A,
|
||||
clientMessageId: `${MESSAGE_A}_client`,
|
||||
body: "Tenant A confidential audit message",
|
||||
mentions: [],
|
||||
linkPreviews: [],
|
||||
},
|
||||
{
|
||||
id: MESSAGE_B,
|
||||
tenantId: TENANT_B,
|
||||
chatId: CHAT_B,
|
||||
authorUserId: MEMBER_B,
|
||||
clientMessageId: `${MESSAGE_B}_client`,
|
||||
body: "Tenant B must stay invisible",
|
||||
mentions: [],
|
||||
linkPreviews: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const readOwnPermission = await ensurePermission(admin, CHAT_PERMISSIONS.readOwnChannels, READ_OWN_PERMISSION_ID, 99_970);
|
||||
createdReadOwnPermission = readOwnPermission.created;
|
||||
const readAllPermission = await ensurePermission(admin, CHAT_PERMISSIONS.readAllChannels, READ_ALL_PERMISSION_ID, 99_971);
|
||||
createdReadAllPermission = readAllPermission.created;
|
||||
const adminPermission = await ensurePermission(admin, CHAT_PERMISSIONS.admin, ADMIN_PERMISSION_ID, 99_972);
|
||||
createdAdminPermission = adminPermission.created;
|
||||
|
||||
await admin.role.create({
|
||||
data: {
|
||||
id: ROLE_ID,
|
||||
tenantId: null,
|
||||
name: "Chat audit RLS role",
|
||||
description: "Restricted-role proof for chat admin audit",
|
||||
type: "app",
|
||||
assignToNewUsers: false,
|
||||
isDefault: false,
|
||||
order: 99_970,
|
||||
},
|
||||
});
|
||||
await admin.rolePermission.createMany({
|
||||
data: [
|
||||
{ id: READ_OWN_ROLE_PERMISSION_ID, roleId: ROLE_ID, permissionId: readOwnPermission.id },
|
||||
{ id: READ_ALL_ROLE_PERMISSION_ID, roleId: ROLE_ID, permissionId: readAllPermission.id },
|
||||
{ id: ADMIN_ROLE_PERMISSION_ID, roleId: ROLE_ID, permissionId: adminPermission.id },
|
||||
],
|
||||
});
|
||||
await admin.userRole.create({
|
||||
data: { id: USER_ROLE_ID, tenantId: TENANT_A, userId: AUDITOR_A, roleId: ROLE_ID },
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await cleanup(admin);
|
||||
if (createdReadOwnPermission) {
|
||||
await admin.permission.deleteMany({
|
||||
where: { id: READ_OWN_PERMISSION_ID, name: CHAT_PERMISSIONS.readOwnChannels },
|
||||
});
|
||||
}
|
||||
if (createdReadAllPermission) {
|
||||
await admin.permission.deleteMany({
|
||||
where: { id: READ_ALL_PERMISSION_ID, name: CHAT_PERMISSIONS.readAllChannels },
|
||||
});
|
||||
}
|
||||
if (createdAdminPermission) {
|
||||
await admin.permission.deleteMany({
|
||||
where: { id: ADMIN_PERMISSION_ID, name: CHAT_PERMISSIONS.admin },
|
||||
});
|
||||
}
|
||||
await Promise.all([admin.$disconnect(), app.$disconnect()]);
|
||||
});
|
||||
|
||||
it("reads every channel in its own tenant, records every audit access, and rechecks revoked permission", async () => {
|
||||
const role = await app.$queryRaw<Array<{ currentUser: string }>>`SELECT current_user AS "currentUser"`;
|
||||
expect(role).toEqual([{ currentUser: "hivegrid_app" }]);
|
||||
const rlsState = await app.$queryRaw<Array<{ tableName: string; enabled: boolean; forced: boolean }>>`
|
||||
SELECT relname AS "tableName", relrowsecurity AS enabled, relforcerowsecurity AS forced
|
||||
FROM pg_catalog.pg_class
|
||||
WHERE relnamespace = 'public'::regnamespace
|
||||
AND relname IN ('TeamChat', 'TeamChatMember', 'TeamChatMessage', 'OutboxEvent')
|
||||
ORDER BY relname ASC
|
||||
`;
|
||||
expect(rlsState).toHaveLength(4);
|
||||
expect(rlsState.every((table) => table.enabled && table.forced)).toBe(true);
|
||||
|
||||
await expect(app.teamChat.findMany({ select: { id: true } })).resolves.toEqual([]);
|
||||
await expect(app.teamChatMessage.findMany({ select: { id: true } })).resolves.toEqual([]);
|
||||
|
||||
const context = {
|
||||
tenantId: TENANT_A,
|
||||
userId: AUDITOR_A,
|
||||
permissions: STALE_ROUTE_PERMISSIONS,
|
||||
} as const;
|
||||
const auditSession = {
|
||||
sessionId: SESSION_ID,
|
||||
reason: AUDIT_REASON,
|
||||
expiresAtIso: "2026-07-16T10:15:00.000Z",
|
||||
} as const;
|
||||
const channels = await listChatAuditChannels({
|
||||
...context,
|
||||
requestId: LIST_REQUEST_ID,
|
||||
auditSession,
|
||||
});
|
||||
expect(channels.map(({ id }) => id)).toEqual([CHAT_A]);
|
||||
expect(channels[0]).toMatchObject({
|
||||
kind: "dm",
|
||||
memberCount: 1,
|
||||
participants: [{ userId: MEMBER_A, displayName: "Member A", avatarUrl: null }],
|
||||
});
|
||||
|
||||
await recordChatAuditSessionStarted({
|
||||
...context,
|
||||
requestId: SESSION_REQUEST_ID,
|
||||
auditSession,
|
||||
});
|
||||
await expect(
|
||||
getChatAuditChannelDetail({
|
||||
...context,
|
||||
chatId: CHAT_B,
|
||||
requestId: "chat_audit_rls_foreign_open_request",
|
||||
auditSession,
|
||||
})
|
||||
).rejects.toMatchObject({ code: "foundation.not_found" });
|
||||
await expect(
|
||||
forceArchiveChatAuditChannel({
|
||||
...context,
|
||||
chatId: CHAT_B,
|
||||
requestId: "chat_audit_rls_foreign_archive_request",
|
||||
auditSession,
|
||||
})
|
||||
).rejects.toMatchObject({ code: "foundation.not_found" });
|
||||
|
||||
const detail = await getChatAuditChannelDetail({
|
||||
...context,
|
||||
chatId: CHAT_A,
|
||||
requestId: OPEN_REQUEST_ID,
|
||||
auditSession,
|
||||
});
|
||||
expect(detail).toMatchObject({
|
||||
channel: {
|
||||
id: CHAT_A,
|
||||
kind: "dm",
|
||||
member: { canWrite: false, notificationPreference: "muted" },
|
||||
},
|
||||
messages: [{ id: MESSAGE_A, body: "Tenant A confidential audit message" }],
|
||||
readReceipts: [],
|
||||
messageFeatures: [],
|
||||
});
|
||||
|
||||
await forceSetChatAuditMemberRole({
|
||||
...context,
|
||||
chatId: CHAT_A,
|
||||
memberUserId: MEMBER_A,
|
||||
roleInChat: "admin",
|
||||
requestId: ROLE_REQUEST_ID,
|
||||
auditSession,
|
||||
});
|
||||
await forceRemoveChatAuditMember({
|
||||
...context,
|
||||
chatId: CHAT_A,
|
||||
memberUserId: MEMBER_A,
|
||||
requestId: REMOVE_REQUEST_ID,
|
||||
auditSession,
|
||||
});
|
||||
await forceArchiveChatAuditChannel({
|
||||
...context,
|
||||
chatId: CHAT_A,
|
||||
requestId: ARCHIVE_REQUEST_ID,
|
||||
auditSession,
|
||||
});
|
||||
await expect(
|
||||
admin.teamChatMember.findUnique({
|
||||
where: { chatId_userId: { chatId: CHAT_A, userId: MEMBER_A } },
|
||||
select: { leftAt: true, writeRevokedAt: true, isManualMember: true, isProjectManaged: true, isGroupManaged: true },
|
||||
})
|
||||
).resolves.toMatchObject({
|
||||
leftAt: expect.any(Date),
|
||||
writeRevokedAt: expect.any(Date),
|
||||
isManualMember: false,
|
||||
isProjectManaged: false,
|
||||
isGroupManaged: false,
|
||||
});
|
||||
await expect(admin.teamChat.findUnique({ where: { id: CHAT_A }, select: { archivedAt: true, archivedReason: true } })).resolves.toMatchObject({
|
||||
archivedAt: expect.any(Date),
|
||||
archivedReason: "manual",
|
||||
});
|
||||
|
||||
const outboxRows = await admin.outboxEvent.findMany({
|
||||
where: {
|
||||
tenantId: TENANT_A,
|
||||
eventType: "audit.event.create",
|
||||
OR: [
|
||||
{ payload: { path: ["requestId"], equals: SESSION_REQUEST_ID } },
|
||||
{ payload: { path: ["requestId"], equals: LIST_REQUEST_ID } },
|
||||
{ payload: { path: ["requestId"], equals: OPEN_REQUEST_ID } },
|
||||
{ payload: { path: ["requestId"], equals: ROLE_REQUEST_ID } },
|
||||
{ payload: { path: ["requestId"], equals: REMOVE_REQUEST_ID } },
|
||||
{ payload: { path: ["requestId"], equals: ARCHIVE_REQUEST_ID } },
|
||||
],
|
||||
},
|
||||
orderBy: { createdAt: "asc" },
|
||||
select: { payload: true },
|
||||
});
|
||||
expect(outboxRows).toHaveLength(6);
|
||||
expect(outboxRows.map(({ payload }) => payload)).toEqual([
|
||||
expect.objectContaining({
|
||||
action: "chat.audit.channel_listed",
|
||||
requestId: LIST_REQUEST_ID,
|
||||
metadata: expect.objectContaining({ auditSessionId: SESSION_ID, reason: AUDIT_REASON, readOnly: true }),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
action: "chat.audit.session_started",
|
||||
requestId: SESSION_REQUEST_ID,
|
||||
metadata: expect.objectContaining({ auditSessionId: SESSION_ID, reason: AUDIT_REASON, readOnly: true }),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
action: "chat.audit.channel_opened",
|
||||
requestId: OPEN_REQUEST_ID,
|
||||
targetId: CHAT_A,
|
||||
dataSensitivity: "confidential",
|
||||
metadata: expect.objectContaining({ auditSessionId: SESSION_ID, reason: AUDIT_REASON, readOnly: true }),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
action: "chat.audit.member_role_forced",
|
||||
requestId: ROLE_REQUEST_ID,
|
||||
metadata: expect.objectContaining({ auditSessionId: SESSION_ID, reason: AUDIT_REASON, memberUserId: MEMBER_A, roleInChat: "admin" }),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
action: "chat.audit.member_removed",
|
||||
requestId: REMOVE_REQUEST_ID,
|
||||
metadata: expect.objectContaining({ auditSessionId: SESSION_ID, reason: AUDIT_REASON, memberUserId: MEMBER_A }),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
action: "chat.audit.channel_archived",
|
||||
requestId: ARCHIVE_REQUEST_ID,
|
||||
metadata: expect.objectContaining({ auditSessionId: SESSION_ID, reason: AUDIT_REASON, channelId: CHAT_A }),
|
||||
}),
|
||||
]);
|
||||
await expect(
|
||||
admin.outboxEvent.count({
|
||||
where: {
|
||||
tenantId: TENANT_B,
|
||||
eventType: "audit.event.create",
|
||||
payload: { path: ["requestId"], equals: OPEN_REQUEST_ID },
|
||||
},
|
||||
})
|
||||
).resolves.toBe(0);
|
||||
|
||||
await admin.rolePermission.delete({ where: { id: ADMIN_ROLE_PERMISSION_ID } });
|
||||
await expect(
|
||||
forceArchiveChatAuditChannel({
|
||||
...context,
|
||||
chatId: CHAT_A,
|
||||
requestId: "chat_audit_rls_revoked_admin_request",
|
||||
auditSession,
|
||||
})
|
||||
).rejects.toMatchObject({ code: "foundation.not_found" });
|
||||
await admin.rolePermission.delete({ where: { id: READ_ALL_ROLE_PERMISSION_ID } });
|
||||
await expect(
|
||||
listChatAuditChannels({
|
||||
...context,
|
||||
requestId: "chat_audit_rls_revoked_list_request",
|
||||
auditSession,
|
||||
})
|
||||
).rejects.toMatchObject({ code: "foundation.not_found" });
|
||||
});
|
||||
});
|
||||
|
||||
async function ensurePermission(
|
||||
admin: ReturnType<typeof createDbClient>,
|
||||
name: string,
|
||||
fixtureId: string,
|
||||
order: number
|
||||
): Promise<{ id: string; created: boolean }> {
|
||||
const existing = await admin.permission.findUnique({ where: { name }, select: { id: true } });
|
||||
if (existing) return { id: existing.id, created: false };
|
||||
const created = await admin.permission.create({
|
||||
data: {
|
||||
id: fixtureId,
|
||||
name,
|
||||
description: "Chat admin audit RLS fixture",
|
||||
type: "app",
|
||||
isDefault: false,
|
||||
order,
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
return { id: created.id, created: true };
|
||||
}
|
||||
|
||||
async function cleanup(admin: ReturnType<typeof createDbClient>): Promise<void> {
|
||||
await admin.outboxEvent.deleteMany({ where: { tenantId: { in: [TENANT_A, TENANT_B] } } });
|
||||
await admin.userRole.deleteMany({ where: { id: USER_ROLE_ID } });
|
||||
await admin.rolePermission.deleteMany({
|
||||
where: { id: { in: [READ_OWN_ROLE_PERMISSION_ID, READ_ALL_ROLE_PERMISSION_ID, ADMIN_ROLE_PERMISSION_ID] } },
|
||||
});
|
||||
await admin.role.deleteMany({ where: { id: ROLE_ID } });
|
||||
await admin.teamChatMessage.deleteMany({ where: { id: { in: [MESSAGE_A, MESSAGE_B] } } });
|
||||
await admin.teamChatEvent.deleteMany({ where: { tenantId: { in: [TENANT_A, TENANT_B] } } });
|
||||
await admin.teamChatMember.deleteMany({ where: { id: { in: ["chat_audit_rls_member_row_a", "chat_audit_rls_member_row_b"] } } });
|
||||
await admin.teamChat.deleteMany({ where: { id: { in: [CHAT_A, CHAT_B] } } });
|
||||
await admin.tenantUser.deleteMany({
|
||||
where: { id: { in: ["chat_audit_rls_tu_auditor_a", "chat_audit_rls_tu_member_a", "chat_audit_rls_tu_member_b"] } },
|
||||
});
|
||||
await admin.tenant.deleteMany({ where: { id: { in: [TENANT_A, TENANT_B] } } });
|
||||
await admin.user.deleteMany({ where: { id: { in: [AUDITOR_A, MEMBER_A, MEMBER_B] } } });
|
||||
}
|
||||
Reference in new issue
Block a user