Backend: RBAC faza 2 M1 - role gymowe z grantow zamiast kolumny roles (migracja: seed GYM_OWNER/GYM_TRAINER + backfill grantow i uzupelnienie Coaching.gymId; katalog 11 uprawnien gym z pinem gym:delegate tylko dla wlasciciela; resolver lib/gymPermissions i bramy per zasob w uslugach silowni, feedzie, obecnosciach i kontaktach; CRUD rol custom per silownia z kluczem serwerowym + PUT ról czlonka pod /admin/gyms; rekrutacja trenera wymaga wspolnej silowni (Coaching.gymId, 0 lub wiele = 400); DTO roles wyprowadzane z grantow - kolumna roles martwa do M4; wyszukiwanie admina po e-mailu wymaga pii:read; 15 unit testow resolvera, zestaw api 1009 green)

This commit is contained in:
tisachi
2026-11-26 00:37:17 +01:00
parent 61e28ff723
commit 654dbf97e0
45 changed files with 1648 additions and 426 deletions

View File

@@ -9,7 +9,27 @@ import {
import { PERMISSIONS } from '../../src/lib/permissions.js';
function mockPrisma() {
const tx = {
role: {
findMany: vi.fn().mockResolvedValue([
{ id: 'r-owner', key: 'GYM_OWNER' },
{ id: 'r-trainer', key: 'GYM_TRAINER' },
]),
},
userRoleGrant: {
findMany: vi.fn().mockResolvedValue([]),
create: vi.fn(),
delete: vi.fn(),
},
gymMembership: {
create: vi.fn(),
findUniqueOrThrow: vi.fn(),
},
user: { update: vi.fn() },
};
return {
$tx: tx,
$transaction: vi.fn(async (cb: (t: typeof tx) => unknown) => cb(tx)),
user: { findUnique: vi.fn(), count: vi.fn(), update: vi.fn(), delete: vi.fn() },
gym: { findUnique: vi.fn(), update: vi.fn(), delete: vi.fn() },
gymMembership: { findUnique: vi.fn(), create: vi.fn(), update: vi.fn(), delete: vi.fn() },
@@ -117,7 +137,7 @@ describe('updateMembership', () => {
});
});
it('sets the roles list of an existing membership', async () => {
it('replaces OWNER/TRAINER system grants and bumps tokenVersion', async () => {
const prisma = mockPrisma();
prisma.gymMembership.findUnique.mockResolvedValue({
id: 'm1',
@@ -125,18 +145,20 @@ describe('updateMembership', () => {
gymId: 'g1',
roles: ['MEMBER'],
});
prisma.gymMembership.update.mockResolvedValue({
prisma.$tx.gymMembership.findUniqueOrThrow.mockResolvedValue({
id: 'm1',
userId: 'u1',
gymId: 'g1',
roles: ['OWNER', 'TRAINER'],
});
const result = await updateMembership(prisma, 'u1', 'g1', ['OWNER', 'TRAINER']);
expect(result.membership).toMatchObject({ roles: ['OWNER', 'TRAINER'] });
expect(prisma.gymMembership.update).toHaveBeenCalledWith({
where: { userId_gymId: { userId: 'u1', gymId: 'g1' } },
data: { roles: ['OWNER', 'TRAINER'] },
expect(prisma.$tx.userRoleGrant.create).toHaveBeenCalledTimes(2);
expect(prisma.$tx.userRoleGrant.create).toHaveBeenCalledWith({
data: { userId: 'u1', roleId: 'r-owner', gymId: 'g1' },
});
expect(prisma.$tx.user.update).toHaveBeenCalledWith(
expect.objectContaining({ data: { tokenVersion: { increment: 1 } } }),
);
});
});