80 lines
2.6 KiB
TypeScript
80 lines
2.6 KiB
TypeScript
const request = require('supertest');
|
|
import { INestApplication } from '@nestjs/common';
|
|
import { PrismaService } from 'src/prisma/prisma.service';
|
|
import { createApp } from './utils/testing-app';
|
|
|
|
// NB: le controller est @Controller('Expenses') (E majuscule)
|
|
const BASE = '/Expenses';
|
|
|
|
describe('Expenses approval (e2e)', () => {
|
|
let app: INestApplication;
|
|
let prisma: PrismaService;
|
|
|
|
let timesheetId: number;
|
|
let bankCodeExpenseId: number;
|
|
let expenseId: number;
|
|
|
|
beforeAll(async () => {
|
|
app = await createApp();
|
|
prisma = app.get(PrismaService);
|
|
|
|
// 1) bank_code catégorie EXPENSE (évite MILEAGE pour ne pas dépendre du service de mileage)
|
|
const bc = await prisma.bankCodes.findFirst({
|
|
where: { categorie: 'EXPENSE' },
|
|
select: { id: true },
|
|
});
|
|
if (!bc) throw new Error('Aucun bank code EXPENSE trouvé');
|
|
bankCodeExpenseId = bc.id;
|
|
|
|
// 2) timesheet existant
|
|
const ts = await prisma.timesheets.findFirst({ select: { id: true } });
|
|
if (!ts) throw new Error('Aucun timesheet trouvé pour créer une expense');
|
|
timesheetId = ts.id;
|
|
|
|
// 3) crée une expense
|
|
const payload = {
|
|
timesheet_id: timesheetId,
|
|
bank_code_id: bankCodeExpenseId,
|
|
date: '2024-02-10T00:00:00.000Z',
|
|
amount: 42, // int côté DTO
|
|
description: 'Approval test expense',
|
|
};
|
|
const create = await request(app.getHttpServer()).post(BASE).send(payload);
|
|
if (create.status !== 201) {
|
|
// eslint-disable-next-line no-console
|
|
console.log('Create expense error:', create.body || create.text);
|
|
}
|
|
expect(create.status).toBe(201);
|
|
expenseId = 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}/${expenseId}/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}/${expenseId}/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}/${expenseId}/approval`)
|
|
.send({ is_approved: 'nope' });
|
|
expect(res.status).toBeGreaterThanOrEqual(400);
|
|
expect(res.status).toBeLessThan(500);
|
|
});
|
|
});
|