targo-backend/test/shifts-aproval.e2e-spec.ts

102 lines
3.4 KiB
TypeScript

const request = require('supertest');
import { INestApplication } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { createApp } from './utils/testing-app';
import { makeEmployee } from './factories/employee.factory';
import { makeTimesheet } from './factories/timesheet.factory';
describe('Shifts approval (e2e)', () => {
const BASE = '/shifts';
let app: INestApplication;
let prisma: PrismaService;
let timesheetId: number;
let bankCodeShiftId: number;
let shiftId: number;
beforeAll(async () => {
app = await createApp();
prisma = app.get(PrismaService);
// 1) bank_code SHIFT
const bc = await prisma.bankCodes.findFirst({
where: { categorie: 'SHIFT' },
select: { id: true },
});
if (!bc) throw new Error('Aucun bank code SHIFT trouvé');
bankCodeShiftId = bc.id;
// 2) timesheet existant ou création rapide
const ts = await prisma.timesheets.findFirst({ select: { id: true } });
if (ts) {
timesheetId = ts.id;
} else {
// crée un employé + timesheet via HTTP
const empRes = await request(app.getHttpServer())
.post('/employees')
.send(makeEmployee());
if (empRes.status !== 201) {
// eslint-disable-next-line no-console
console.warn('Création employé échouée:', empRes.body || empRes.text);
throw new Error('Setup employees pour shifts-approval échoué');
}
const tsRes = await request(app.getHttpServer())
.post('/timesheets')
.send(makeTimesheet(empRes.body.id));
if (tsRes.status !== 201) {
// eslint-disable-next-line no-console
console.warn('Création timesheet échouée:', tsRes.body || tsRes.text);
throw new Error('Setup timesheet pour shifts-approval échoué');
}
timesheetId = tsRes.body.id;
}
// 3) crée un shift à approuver
const payload = {
timesheet_id: timesheetId,
bank_code_id: bankCodeShiftId,
date: '2024-01-15T00:00:00.000Z',
start_time: '2024-01-15T08:00:00.000Z',
end_time: '2024-01-15T16:00:00.000Z',
description: 'Approval test shift',
};
const create = await request(app.getHttpServer()).post(BASE).send(payload);
if (create.status !== 201) {
// eslint-disable-next-line no-console
console.log('Create shift error:', create.body || create.text);
}
expect(create.status).toBe(201);
shiftId = create.body.id;
});
afterAll(async () => {
await app.close();
await prisma.$disconnect();
});
it(`PATCH ${BASE}/:id/approval → 200 (true)`, async () => {
const res = await request(app.getHttpServer())
.patch(`${BASE}/${shiftId}/approval`)
.send({ is_approved: true });
expect(res.status).toBe(200);
expect(res.body?.is_approved).toBe(true);
});
it(`PATCH ${BASE}/:id/approval → 200 (false)`, async () => {
const res = await request(app.getHttpServer())
.patch(`${BASE}/${shiftId}/approval`)
.send({ is_approved: false });
expect(res.status).toBe(200);
expect(res.body?.is_approved).toBe(false);
});
it(`PATCH ${BASE}/:id/approval (invalid) → 400`, async () => {
const res = await request(app.getHttpServer())
.patch(`${BASE}/${shiftId}/approval`)
// ParseBoolPipe doit rejeter une string non "true/false"
.send({ is_approved: 'notabool' });
expect(res.status).toBeGreaterThanOrEqual(400);
expect(res.status).toBeLessThan(500);
});
});