235 lines
8.8 KiB
TypeScript
235 lines
8.8 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest';
|
|
import {
|
|
addMembership,
|
|
deleteUser,
|
|
deleteGym,
|
|
updateMembership,
|
|
updateUser,
|
|
} from '../../src/services/admin.js';
|
|
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() },
|
|
gymClaim: { findMany: vi.fn().mockResolvedValue([]) },
|
|
userRoleGrant: {
|
|
findFirst: vi.fn().mockResolvedValue(null),
|
|
findMany: vi.fn().mockResolvedValue([]),
|
|
},
|
|
};
|
|
}
|
|
|
|
const prismaNotFound = Object.assign(new Error('Record not found'), { code: 'P2025' });
|
|
|
|
/** Uprawnienia pełnego admina (pii:read + roles:manage) dla testów poza strażnikami. */
|
|
const ADMIN_PERMS = [PERMISSIONS.PII_READ, PERMISSIONS.ROLES_MANAGE];
|
|
|
|
describe('updateUser', () => {
|
|
it('404 when the user does not exist', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue(null);
|
|
await expect(updateUser(prisma, 'u1', { firstName: 'X' })).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'User not found',
|
|
});
|
|
});
|
|
|
|
it('403 STAFF_MUTATION_FORBIDDEN when the target holds a platform grant', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ id: 'u1' });
|
|
prisma.userRoleGrant.findFirst.mockResolvedValue({ id: 'grant-1' });
|
|
await expect(
|
|
updateUser(prisma, 'u1', { firstName: 'X' }, { permissions: [PERMISSIONS.USERS_MANAGE] }),
|
|
).rejects.toMatchObject({ status: 403, code: 'STAFF_MUTATION_FORBIDDEN' });
|
|
expect(prisma.user.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('allows mutating a staff target for a roles:manage holder', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ id: 'u1' });
|
|
prisma.user.update.mockResolvedValue({ id: 'u1', email: 'u@x.local', createdAt: new Date() });
|
|
await expect(
|
|
updateUser(
|
|
prisma,
|
|
'u1',
|
|
{ firstName: 'X' },
|
|
{ permissions: [PERMISSIONS.USERS_MANAGE, PERMISSIONS.ROLES_MANAGE] },
|
|
),
|
|
).resolves.toBeDefined();
|
|
// roles:manage przeskakuje zapytanie o grant celu
|
|
expect(prisma.userRoleGrant.findFirst).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('403 PII_FORBIDDEN when email/phone/birthDate arrive without pii:read', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ id: 'u1' });
|
|
await expect(
|
|
updateUser(prisma, 'u1', { birthDate: null }, { permissions: [PERMISSIONS.USERS_MANAGE] }),
|
|
).rejects.toMatchObject({ status: 403, code: 'PII_FORBIDDEN' });
|
|
expect(prisma.user.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('increments tokenVersion when the account status changes', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ id: 'u1', status: 'ACTIVE' });
|
|
prisma.user.update.mockResolvedValue({ id: 'u1', email: 'u@x.local', createdAt: new Date() });
|
|
await updateUser(prisma, 'u1', { status: 'SUSPENDED' }, { permissions: ADMIN_PERMS });
|
|
expect(prisma.user.update).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
data: expect.objectContaining({ status: 'SUSPENDED', tokenVersion: { increment: 1 } }),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('409 when the new email is already taken', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique
|
|
.mockResolvedValueOnce({ id: 'u1', email: 'old@x.local' })
|
|
.mockResolvedValueOnce({ id: 'u2', email: 'new@x.local' });
|
|
await expect(
|
|
updateUser(prisma, 'u1', { email: 'new@x.local' }, { permissions: ADMIN_PERMS }),
|
|
).rejects.toMatchObject({
|
|
status: 409,
|
|
message: 'Email already registered',
|
|
});
|
|
});
|
|
|
|
it('updates the email (lowercased) without a conflict check when unchanged', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ id: 'u1', email: 'u@x.local' });
|
|
prisma.user.update.mockResolvedValue({ id: 'u1', email: 'u@x.local', createdAt: new Date() });
|
|
await updateUser(prisma, 'u1', { email: 'U@X.local' }, { permissions: ADMIN_PERMS });
|
|
expect(prisma.user.update).toHaveBeenCalledWith(
|
|
expect.objectContaining({ data: expect.objectContaining({ email: 'u@x.local' }) }),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('updateMembership', () => {
|
|
it('404 when the membership does not exist', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.gymMembership.findUnique.mockResolvedValue(null);
|
|
await expect(updateMembership(prisma, 'u1', 'g1', ['OWNER'])).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'Membership not found',
|
|
});
|
|
});
|
|
|
|
it('replaces OWNER/TRAINER system grants and bumps tokenVersion', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.gymMembership.findUnique.mockResolvedValue({
|
|
id: 'm1',
|
|
userId: 'u1',
|
|
gymId: 'g1',
|
|
roles: ['MEMBER'],
|
|
});
|
|
prisma.$tx.gymMembership.findUniqueOrThrow.mockResolvedValue({
|
|
id: 'm1',
|
|
userId: 'u1',
|
|
gymId: 'g1',
|
|
});
|
|
const result = await updateMembership(prisma, 'u1', 'g1', ['OWNER', 'TRAINER']);
|
|
expect(result.membership).toMatchObject({ 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 } } }),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('P2025 mapping', () => {
|
|
it('deleteGym: maps P2025 to 404', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.gym.delete.mockRejectedValue(prismaNotFound);
|
|
await expect(deleteGym(prisma, 'g1')).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'Gym not found',
|
|
});
|
|
});
|
|
|
|
it('deleteUser: maps P2025 to 404', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ email: 'u@x.local' });
|
|
prisma.user.delete.mockRejectedValue(prismaNotFound);
|
|
await expect(deleteUser(prisma, 'u1', { permissions: ADMIN_PERMS })).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'User not found',
|
|
});
|
|
});
|
|
|
|
it('deleteUser: blocks deleting own account (403)', async () => {
|
|
const prisma = mockPrisma();
|
|
await expect(
|
|
deleteUser(prisma, 'u1', { actorId: 'u1', permissions: ADMIN_PERMS }),
|
|
).rejects.toMatchObject({ status: 403 });
|
|
expect(prisma.user.delete).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('deleteUser: 403 STAFF_MUTATION_FORBIDDEN for staff target without roles:manage', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ email: 'u@x.local' });
|
|
prisma.userRoleGrant.findFirst.mockResolvedValue({ id: 'grant-1' });
|
|
await expect(
|
|
deleteUser(prisma, 'u1', { actorId: 'u2', permissions: [PERMISSIONS.USERS_MANAGE] }),
|
|
).rejects.toMatchObject({ status: 403, code: 'STAFF_MUTATION_FORBIDDEN' });
|
|
expect(prisma.user.delete).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('deleteUser: blocks deleting the last roles:manage holder (403 LAST_ROLES_MANAGER)', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ email: 'u@x.local' });
|
|
prisma.userRoleGrant.findMany.mockResolvedValue([{ userId: 'u1' }]);
|
|
await expect(
|
|
deleteUser(prisma, 'u1', { actorId: 'u2', permissions: ADMIN_PERMS }),
|
|
).rejects.toMatchObject({ status: 403, code: 'LAST_ROLES_MANAGER' });
|
|
expect(prisma.user.delete).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('addMembership', () => {
|
|
it('404 when the gym does not exist', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.gym.findUnique.mockResolvedValue(null);
|
|
await expect(addMembership(prisma, 'u1', 'g1', 'MEMBER')).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'Gym not found',
|
|
});
|
|
});
|
|
|
|
it('409 when the membership already exists', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.gym.findUnique.mockResolvedValue({ id: 'g1' });
|
|
prisma.user.findUnique.mockResolvedValue({ id: 'u1' });
|
|
prisma.gymMembership.findUnique.mockResolvedValue({ id: 'm1' });
|
|
await expect(addMembership(prisma, 'u1', 'g1', ['MEMBER'])).rejects.toMatchObject({
|
|
status: 409,
|
|
message: 'Membership already exists',
|
|
});
|
|
});
|
|
});
|