import { ConflictException, NotFoundException } from '@nestjs/common'; import { UsersService } from './users.service'; import { PrismaService } from '../prisma/prisma.service'; import { AuditLogService } from '../audit/audit-log.service'; describe('UsersService', () => { let usersService: UsersService; let prisma: { user: { findMany: jest.Mock; findUnique: jest.Mock; create: jest.Mock; update: jest.Mock; }; role: { findUnique: jest.Mock; }; }; let auditLog: { record: jest.Mock }; const staffRole = { id: 'role-staff', key: 'STAFF', name: 'Mitarbeiter', status: 'ACTIVE', }; const existingUser = { id: 'user-1', email: 'existing@example.com', passwordHash: 'hashed', name: 'Existing User', roleId: staffRole.id, role: staffRole, isActive: true, createdAt: new Date(), deactivatedAt: null, }; beforeEach(() => { prisma = { user: { findMany: jest.fn(), findUnique: jest.fn(), create: jest.fn(), update: jest.fn(), }, role: { findUnique: jest.fn().mockResolvedValue(staffRole), }, }; auditLog = { record: jest.fn().mockResolvedValue(undefined) }; usersService = new UsersService( prisma as unknown as PrismaService, auditLog as unknown as AuditLogService, ); }); it('findAll returns summaries without the password hash', async () => { prisma.user.findMany.mockResolvedValue([existingUser]); const result = await usersService.findAll(); expect(result).toEqual([ { id: existingUser.id, email: existingUser.email, name: existingUser.name, roleId: staffRole.id, roleKey: staffRole.key, roleName: staffRole.name, isActive: existingUser.isActive, createdAt: existingUser.createdAt, deactivatedAt: existingUser.deactivatedAt, }, ]); expect(result[0]).not.toHaveProperty('passwordHash'); }); it('findById throws NotFoundException when the user is missing', async () => { prisma.user.findUnique.mockResolvedValue(null); await expect(usersService.findById('missing')).rejects.toBeInstanceOf( NotFoundException, ); }); it('create throws ConflictException when the email is already in use', async () => { prisma.user.findUnique.mockResolvedValue(existingUser); await expect( usersService.create({ email: existingUser.email, password: 'password123', name: 'New', roleId: staffRole.id, }), ).rejects.toBeInstanceOf(ConflictException); }); it('create throws NotFoundException when the role does not exist', async () => { prisma.user.findUnique.mockResolvedValue(null); prisma.role.findUnique.mockResolvedValue(null); await expect( usersService.create({ email: 'new@example.com', password: 'password123', name: 'New User', roleId: 'missing-role', }), ).rejects.toBeInstanceOf(NotFoundException); }); it('create throws ConflictException when the role is not active', async () => { prisma.user.findUnique.mockResolvedValue(null); prisma.role.findUnique.mockResolvedValue({ ...staffRole, status: 'INACTIVE', }); await expect( usersService.create({ email: 'new@example.com', password: 'password123', name: 'New User', roleId: staffRole.id, }), ).rejects.toBeInstanceOf(ConflictException); }); it('create hashes the password, persists the user, and records an audit entry', async () => { prisma.user.findUnique.mockResolvedValue(null); prisma.user.create.mockResolvedValue({ ...existingUser, id: 'user-2', email: 'new@example.com', }); const result = await usersService.create({ email: 'new@example.com', password: 'password123', name: 'New User', roleId: staffRole.id, }); expect(result.id).toBe('user-2'); const [[createArg]] = prisma.user.create.mock.calls as [ [{ data: { email: string; passwordHash: string } }], ]; expect(createArg.data.email).toBe('new@example.com'); expect(createArg.data.passwordHash).not.toBe('password123'); expect(auditLog.record).toHaveBeenCalledWith( expect.objectContaining({ entityType: 'User', action: 'CREATE' }), ); }); it('deactivate sets isActive to false and records an audit entry', async () => { prisma.user.findUnique.mockResolvedValue(existingUser); prisma.user.update.mockResolvedValue({ ...existingUser, isActive: false, deactivatedAt: new Date(), }); const result = await usersService.deactivate(existingUser.id); expect(result.isActive).toBe(false); expect(result.deactivatedAt).not.toBeNull(); expect(auditLog.record).toHaveBeenCalledWith( expect.objectContaining({ entityType: 'User', action: 'DEACTIVATE' }), ); }); it('activate throws ConflictException when already active', async () => { prisma.user.findUnique.mockResolvedValue(existingUser); await expect(usersService.activate(existingUser.id)).rejects.toBeInstanceOf( ConflictException, ); }); it('activate sets isActive to true and clears deactivatedAt', async () => { prisma.user.findUnique.mockResolvedValue({ ...existingUser, isActive: false, deactivatedAt: new Date(), }); prisma.user.update.mockResolvedValue({ ...existingUser, isActive: true, deactivatedAt: null, }); const result = await usersService.activate(existingUser.id); expect(result.isActive).toBe(true); expect(result.deactivatedAt).toBeNull(); expect(auditLog.record).toHaveBeenCalledWith( expect.objectContaining({ entityType: 'User', action: 'UPDATE' }), ); }); });