179 lines
6.6 KiB
TypeScript
179 lines
6.6 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest';
|
|
import {
|
|
addMembership,
|
|
deleteUser,
|
|
deleteGym,
|
|
updateMembership,
|
|
updateUser,
|
|
} from '../../src/services/admin.js';
|
|
|
|
function mockPrisma() {
|
|
return {
|
|
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([]) },
|
|
};
|
|
}
|
|
|
|
const prismaNotFound = Object.assign(new Error('Record not found'), { code: 'P2025' });
|
|
|
|
describe('updateUser', () => {
|
|
it('404 when the user does not exist', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue(null);
|
|
await expect(updateUser(prisma, 'u1', { isAdmin: true })).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'User not found',
|
|
});
|
|
});
|
|
|
|
it('increments tokenVersion when the role changes', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ id: 'u1', isAdmin: false });
|
|
prisma.user.update.mockResolvedValue({ id: 'u1', email: 'u@x.local', createdAt: new Date() });
|
|
await updateUser(prisma, 'u1', { isAdmin: true });
|
|
expect(prisma.user.update).toHaveBeenCalledWith({
|
|
where: { id: 'u1' },
|
|
data: expect.objectContaining({ isAdmin: true, tokenVersion: { increment: 1 } }),
|
|
});
|
|
});
|
|
|
|
it('does not touch tokenVersion when the role is unchanged', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ id: 'u1', isAdmin: true });
|
|
prisma.user.update.mockResolvedValue({ id: 'u1', email: 'u@x.local', createdAt: new Date() });
|
|
await updateUser(prisma, 'u1', { isAdmin: true });
|
|
const call = prisma.user.update.mock.calls[0][0];
|
|
expect(call.data.tokenVersion).toBeUndefined();
|
|
});
|
|
|
|
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' })).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' });
|
|
expect(prisma.user.update).toHaveBeenCalledWith(
|
|
expect.objectContaining({ data: expect.objectContaining({ email: 'u@x.local' }) }),
|
|
);
|
|
});
|
|
|
|
it('blocks demoting the last admin (403)', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ id: 'u1', isAdmin: true });
|
|
prisma.user.count.mockResolvedValue(0);
|
|
await expect(updateUser(prisma, 'u1', { isAdmin: false }, 'u1')).rejects.toMatchObject({
|
|
status: 403,
|
|
});
|
|
expect(prisma.user.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('allows demoting an admin when another admin exists', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ id: 'u1', isAdmin: true });
|
|
prisma.user.count.mockResolvedValue(1);
|
|
prisma.user.update.mockResolvedValue({ id: 'u1', email: 'u@x.local', createdAt: new Date() });
|
|
await expect(updateUser(prisma, 'u1', { isAdmin: false }, 'u2')).resolves.toBeDefined();
|
|
});
|
|
});
|
|
|
|
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('sets the roles list of an existing membership', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.gymMembership.findUnique.mockResolvedValue({
|
|
id: 'm1',
|
|
userId: 'u1',
|
|
gymId: 'g1',
|
|
roles: ['MEMBER'],
|
|
});
|
|
prisma.gymMembership.update.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'] },
|
|
});
|
|
});
|
|
});
|
|
|
|
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.delete.mockRejectedValue(prismaNotFound);
|
|
await expect(deleteUser(prisma, 'u1')).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'User not found',
|
|
});
|
|
});
|
|
|
|
it('deleteUser: blocks deleting own account (403)', async () => {
|
|
const prisma = mockPrisma();
|
|
await expect(deleteUser(prisma, 'u1', 'u1')).rejects.toMatchObject({ status: 403 });
|
|
expect(prisma.user.delete).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('deleteUser: blocks deleting the last admin (403)', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.user.findUnique.mockResolvedValue({ isAdmin: true });
|
|
prisma.user.count.mockResolvedValue(0);
|
|
await expect(deleteUser(prisma, 'u1', 'u2')).rejects.toMatchObject({ status: 403 });
|
|
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',
|
|
});
|
|
});
|
|
});
|