ApiariesModule: CRUD + Deactivate. Lesen fuer alle authentifizierten Rollen offen, Schreiben (Anlegen/Aendern/Deaktivieren) auf ADMIN und BEEKEEPER beschraenkt (Lastenheft §3.2: Imker verwaltet Bienenstaende). Audit-Log-Eintraege bei Create/Update/Deactivate ueber AuditLogService. 4 Unit-Tests + 4 e2e-Tests (inkl. Rollenpruefung READONLY vs. BEEKEEPER), Lint, Typecheck und Build gruen.
79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { NotFoundException } from '@nestjs/common';
|
|
import { ApiariesService } from './apiaries.service';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
import { AuditLogService } from '../audit/audit-log.service';
|
|
|
|
describe('ApiariesService', () => {
|
|
let service: ApiariesService;
|
|
let prisma: {
|
|
apiary: {
|
|
findMany: jest.Mock;
|
|
findUnique: jest.Mock;
|
|
create: jest.Mock;
|
|
update: jest.Mock;
|
|
};
|
|
};
|
|
let auditLog: { record: jest.Mock };
|
|
|
|
const existingApiary = {
|
|
id: 'apiary-1',
|
|
name: 'Heimstand',
|
|
status: 'ACTIVE',
|
|
deactivatedAt: null,
|
|
};
|
|
|
|
beforeEach(() => {
|
|
prisma = {
|
|
apiary: {
|
|
findMany: jest.fn(),
|
|
findUnique: jest.fn(),
|
|
create: jest.fn(),
|
|
update: jest.fn(),
|
|
},
|
|
};
|
|
auditLog = { record: jest.fn().mockResolvedValue(undefined) };
|
|
service = new ApiariesService(
|
|
prisma as unknown as PrismaService,
|
|
auditLog as unknown as AuditLogService,
|
|
);
|
|
});
|
|
|
|
it('findById throws NotFoundException when missing', async () => {
|
|
prisma.apiary.findUnique.mockResolvedValue(null);
|
|
await expect(service.findById('missing')).rejects.toBeInstanceOf(
|
|
NotFoundException,
|
|
);
|
|
});
|
|
|
|
it('create persists the apiary and records an audit entry', async () => {
|
|
prisma.apiary.create.mockResolvedValue(existingApiary);
|
|
|
|
const result = await service.create({ name: 'Heimstand' }, 'user-1');
|
|
|
|
expect(result).toEqual(existingApiary);
|
|
expect(auditLog.record).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
entityType: 'Apiary',
|
|
action: 'CREATE',
|
|
userId: 'user-1',
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('deactivate sets status to INACTIVE and records an audit entry', async () => {
|
|
prisma.apiary.findUnique.mockResolvedValue(existingApiary);
|
|
prisma.apiary.update.mockResolvedValue({
|
|
...existingApiary,
|
|
status: 'INACTIVE',
|
|
deactivatedAt: new Date(),
|
|
});
|
|
|
|
const result = await service.deactivate('apiary-1', 'user-1');
|
|
|
|
expect(result.status).toBe('INACTIVE');
|
|
expect(auditLog.record).toHaveBeenCalledWith(
|
|
expect.objectContaining({ entityType: 'Apiary', action: 'DEACTIVATE' }),
|
|
);
|
|
});
|
|
});
|