182 lines
11 KiB
TypeScript
182 lines
11 KiB
TypeScript
// @vitest-environment node
|
|
import { createDbClient, getWriteDbClient } from "@hivegrid/db/client.server";
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
addGroupUsersAndSync,
|
|
deletePermissionGroupAndSync,
|
|
removeGroupUsersAndSync,
|
|
} from "./userGroups.db.server";
|
|
|
|
const ADMIN_URL = process.env.RLS_TEST_DATABASE_URL;
|
|
const describeIfDatabase = ADMIN_URL ? describe : describe.skip;
|
|
|
|
const TENANT_A = "chat_group_sync_rls_tenant_a";
|
|
const TENANT_B = "chat_group_sync_rls_tenant_b";
|
|
const ACTOR_A = "chat_group_sync_rls_actor_a";
|
|
const MEMBER_A = "chat_group_sync_rls_member_a";
|
|
const ACTOR_B = "chat_group_sync_rls_actor_b";
|
|
const MEMBER_B = "chat_group_sync_rls_member_b";
|
|
const GROUP_A = "chat_group_sync_rls_group_a";
|
|
const GROUP_B = "chat_group_sync_rls_group_b";
|
|
const CHAT_A = "chat_group_sync_rls_chat_a";
|
|
const CHAT_B = "chat_group_sync_rls_chat_b";
|
|
const PROJECT_A = "chat_group_sync_rls_project_a";
|
|
const PROJECT_B = "chat_group_sync_rls_project_b";
|
|
|
|
describeIfDatabase("permission-group chat sync with the restricted app role", () => {
|
|
const admin = createDbClient({ connectionString: ADMIN_URL });
|
|
const app = getWriteDbClient();
|
|
|
|
beforeAll(async () => {
|
|
await cleanupFixtures(admin);
|
|
await admin.user.createMany({
|
|
data: [
|
|
{ id: ACTOR_A, email: "chat-group-sync-actor-a@example.test", firstName: "Actor", lastName: "A", active: true },
|
|
{ id: MEMBER_A, email: "chat-group-sync-member-a@example.test", firstName: "Member", lastName: "A", active: true },
|
|
{ id: ACTOR_B, email: "chat-group-sync-actor-b@example.test", firstName: "Actor", lastName: "B", active: true },
|
|
{ id: MEMBER_B, email: "chat-group-sync-member-b@example.test", firstName: "Member", lastName: "B", active: true },
|
|
],
|
|
});
|
|
await admin.tenant.createMany({
|
|
data: [
|
|
{ id: TENANT_A, slug: TENANT_A, name: "Group sync RLS tenant A", active: true },
|
|
{ id: TENANT_B, slug: TENANT_B, name: "Group sync RLS tenant B", active: true },
|
|
],
|
|
});
|
|
await admin.tenantUser.createMany({
|
|
data: [
|
|
{ id: `${TENANT_A}_actor`, tenantId: TENANT_A, userId: ACTOR_A, type: 0, joined: 1, status: 2 },
|
|
{ id: `${TENANT_A}_member`, tenantId: TENANT_A, userId: MEMBER_A, type: 0, joined: 1, status: 2 },
|
|
{ id: `${TENANT_B}_actor`, tenantId: TENANT_B, userId: ACTOR_B, type: 0, joined: 1, status: 2 },
|
|
{ id: `${TENANT_B}_member`, tenantId: TENANT_B, userId: MEMBER_B, type: 0, joined: 1, status: 2 },
|
|
],
|
|
});
|
|
await admin.group.createMany({
|
|
data: [
|
|
{ id: GROUP_A, tenantId: TENANT_A, createdByUserId: ACTOR_A, name: "Tenant A Dispatch", description: "A", color: 1 },
|
|
{ id: GROUP_B, tenantId: TENANT_B, createdByUserId: ACTOR_B, name: "Tenant B Dispatch", description: "B", color: 2 },
|
|
],
|
|
});
|
|
await admin.groupUser.create({ data: { id: `${GROUP_B}_member`, groupId: GROUP_B, userId: MEMBER_B } });
|
|
await admin.teamChat.createMany({
|
|
data: [
|
|
{ id: CHAT_A, tenantId: TENANT_A, createdByUserId: ACTOR_A, kind: "group", name: "A", isPrivate: true, eventStreamId: `${CHAT_A}_stream` },
|
|
{ id: CHAT_B, tenantId: TENANT_B, createdByUserId: ACTOR_B, kind: "group", name: "B", isPrivate: true, eventStreamId: `${CHAT_B}_stream` },
|
|
],
|
|
});
|
|
await admin.teamChatGroupSource.createMany({
|
|
data: [
|
|
{ id: `${CHAT_A}_source`, tenantId: TENANT_A, chatId: CHAT_A, groupId: GROUP_A, syncEnabled: true },
|
|
{ id: `${CHAT_B}_source`, tenantId: TENANT_B, chatId: CHAT_B, groupId: GROUP_B, syncEnabled: true },
|
|
],
|
|
});
|
|
await admin.teamChatMember.createMany({
|
|
data: [
|
|
{ id: `${CHAT_A}_actor`, tenantId: TENANT_A, chatId: CHAT_A, userId: ACTOR_A, roleInChat: "admin", notificationPref: "all_messages", isManualMember: true, manualRoleInChat: "admin" },
|
|
{ id: `${CHAT_B}_actor`, tenantId: TENANT_B, chatId: CHAT_B, userId: ACTOR_B, roleInChat: "admin", notificationPref: "all_messages", isManualMember: true, manualRoleInChat: "admin" },
|
|
{ id: `${CHAT_B}_member`, tenantId: TENANT_B, chatId: CHAT_B, userId: MEMBER_B, notificationPref: "all_messages", isManualMember: false, isGroupManaged: true },
|
|
],
|
|
});
|
|
await createProjectFixtures(admin);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await cleanupFixtures(admin);
|
|
await Promise.all([admin.$disconnect(), app.$disconnect()]);
|
|
});
|
|
|
|
it("adds, removes, and deletes only tenant-A group sources while preserving project history", async () => {
|
|
const currentRole = await app.$queryRaw<Array<{ currentUser: string }>>`SELECT current_user AS "currentUser"`;
|
|
expect(currentRole).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 ('Group', 'GroupUser', 'ProjektMitglied', 'TeamChat', 'TeamChatGroupSource', 'TeamChatMember')
|
|
ORDER BY relname ASC
|
|
`;
|
|
expect(rlsState).toHaveLength(6);
|
|
expect(rlsState.every((table) => table.enabled && table.forced)).toBe(true);
|
|
await expect(app.group.findMany({ where: { id: GROUP_A }, select: { id: true } })).resolves.toEqual([]);
|
|
|
|
await addGroupUsersAndSync(GROUP_A, TENANT_A, [MEMBER_A], { actorUserId: ACTOR_A, requestId: "group-rls-add-a" });
|
|
await expect(admin.teamChatMember.findUnique({ where: { chatId_userId: { chatId: CHAT_A, userId: MEMBER_A } } })).resolves.toMatchObject({
|
|
isGroupManaged: true,
|
|
leftAt: null,
|
|
});
|
|
|
|
await removeGroupUsersAndSync(GROUP_A, TENANT_A, [MEMBER_A], { actorUserId: ACTOR_A, requestId: "group-rls-remove-a" });
|
|
await expect(admin.teamChatMember.findUnique({ where: { chatId_userId: { chatId: CHAT_A, userId: MEMBER_A } } })).resolves.toMatchObject({
|
|
isGroupManaged: false,
|
|
leftAt: expect.any(Date),
|
|
});
|
|
|
|
await addGroupUsersAndSync(GROUP_A, TENANT_A, [MEMBER_A], { actorUserId: ACTOR_A, requestId: "group-rls-readd-a" });
|
|
await deletePermissionGroupAndSync(GROUP_A, TENANT_A, { actorUserId: ACTOR_A, requestId: "group-rls-delete-a" });
|
|
|
|
await expect(admin.group.findUnique({ where: { id: GROUP_A } })).resolves.toBeNull();
|
|
await expect(admin.teamChatGroupSource.count({ where: { tenantId: TENANT_A, groupId: GROUP_A } })).resolves.toBe(0);
|
|
await expect(admin.projektMitglied.findUnique({ where: { id: `${PROJECT_A}_group_member` } })).resolves.toMatchObject({
|
|
groupId: GROUP_A,
|
|
archivedAt: expect.any(Date),
|
|
});
|
|
await expect(admin.activity.count({ where: { tenantId: TENANT_A, subjectType: "project", subjectId: PROJECT_A, activityType: "project_member_removed" } })).resolves.toBeGreaterThan(0);
|
|
await expect(admin.outboxEvent.count({ where: { tenantId: TENANT_A } })).resolves.toBeGreaterThan(0);
|
|
|
|
await expect(admin.group.findUnique({ where: { id: GROUP_B } })).resolves.toMatchObject({ tenantId: TENANT_B });
|
|
await expect(admin.groupUser.findUnique({ where: { group_user_membership_key: { groupId: GROUP_B, userId: MEMBER_B } } })).resolves.not.toBeNull();
|
|
await expect(admin.teamChatGroupSource.findUnique({ where: { chatId_groupId: { chatId: CHAT_B, groupId: GROUP_B } } })).resolves.toMatchObject({ tenantId: TENANT_B, syncEnabled: true });
|
|
await expect(admin.teamChatMember.findUnique({ where: { chatId_userId: { chatId: CHAT_B, userId: MEMBER_B } } })).resolves.toMatchObject({ isGroupManaged: true, leftAt: null });
|
|
await expect(admin.projektMitglied.findUnique({ where: { id: `${PROJECT_B}_group_member` } })).resolves.toMatchObject({ archivedAt: null });
|
|
});
|
|
});
|
|
|
|
async function createProjectFixtures(admin: ReturnType<typeof createDbClient>): Promise<void> {
|
|
await admin.pipeline.createMany({
|
|
data: [
|
|
{ id: `${PROJECT_A}_pipeline`, tenantId: TENANT_A, createdByUserId: ACTOR_A, name: "A Pipeline", typHint: "auftrag" },
|
|
{ id: `${PROJECT_B}_pipeline`, tenantId: TENANT_B, createdByUserId: ACTOR_B, name: "B Pipeline", typHint: "auftrag" },
|
|
],
|
|
});
|
|
await admin.pipelineStage.createMany({
|
|
data: [
|
|
{ id: `${PROJECT_A}_stage`, tenantId: TENANT_A, pipelineId: `${PROJECT_A}_pipeline`, name: "A Active", stageKind: "offen" },
|
|
{ id: `${PROJECT_B}_stage`, tenantId: TENANT_B, pipelineId: `${PROJECT_B}_pipeline`, name: "B Active", stageKind: "offen" },
|
|
],
|
|
});
|
|
await admin.projekt.createMany({
|
|
data: [
|
|
{ id: PROJECT_A, tenantId: TENANT_A, createdByUserId: ACTOR_A, projektNummer: "RLS-A", projektname: "RLS A", pipelineId: `${PROJECT_A}_pipeline`, stageId: `${PROJECT_A}_stage` },
|
|
{ id: PROJECT_B, tenantId: TENANT_B, createdByUserId: ACTOR_B, projektNummer: "RLS-B", projektname: "RLS B", pipelineId: `${PROJECT_B}_pipeline`, stageId: `${PROJECT_B}_stage` },
|
|
],
|
|
});
|
|
await admin.projektMitglied.createMany({
|
|
data: [
|
|
{ id: `${PROJECT_A}_group_member`, tenantId: TENANT_A, projektId: PROJECT_A, groupId: GROUP_A, rolle: "monteur", createdByUserId: ACTOR_A },
|
|
{ id: `${PROJECT_B}_group_member`, tenantId: TENANT_B, projektId: PROJECT_B, groupId: GROUP_B, rolle: "monteur", createdByUserId: ACTOR_B },
|
|
],
|
|
});
|
|
}
|
|
|
|
async function cleanupFixtures(admin: ReturnType<typeof createDbClient>): Promise<void> {
|
|
const tenantIds = [TENANT_A, TENANT_B];
|
|
await admin.teamChatMember.updateMany({ where: { tenantId: { in: tenantIds } }, data: { lastReadMessageId: null, lastReadAt: null } });
|
|
await admin.teamChatEvent.deleteMany({ where: { tenantId: { in: tenantIds } } });
|
|
await admin.teamChatMessage.deleteMany({ where: { tenantId: { in: tenantIds } } });
|
|
await admin.teamChatMember.deleteMany({ where: { tenantId: { in: tenantIds } } });
|
|
await admin.teamChatGroupSource.deleteMany({ where: { tenantId: { in: tenantIds } } });
|
|
await admin.teamChat.deleteMany({ where: { tenantId: { in: tenantIds } } });
|
|
await admin.activity.deleteMany({ where: { tenantId: { in: tenantIds } } });
|
|
await admin.outboxEvent.deleteMany({ where: { tenantId: { in: tenantIds } } });
|
|
await admin.projektMitglied.deleteMany({ where: { tenantId: { in: tenantIds } } });
|
|
await admin.projekt.deleteMany({ where: { tenantId: { in: tenantIds } } });
|
|
await admin.pipelineStage.deleteMany({ where: { tenantId: { in: tenantIds } } });
|
|
await admin.pipeline.deleteMany({ where: { tenantId: { in: tenantIds } } });
|
|
await admin.groupUser.deleteMany({ where: { groupId: { in: [GROUP_A, GROUP_B] } } });
|
|
await admin.group.deleteMany({ where: { id: { in: [GROUP_A, GROUP_B] } } });
|
|
await admin.tenantUser.deleteMany({ where: { tenantId: { in: tenantIds } } });
|
|
await admin.tenant.deleteMany({ where: { id: { in: tenantIds } } });
|
|
await admin.user.deleteMany({ where: { id: { in: [ACTOR_A, MEMBER_A, ACTOR_B, MEMBER_B] } } });
|
|
}
|