import * as request from 'supertest'; import { INestApplication } from '@nestjs/common'; import { createApp } from './utils/testing-app'; import { PrismaService } from 'src/prisma/prisma.service'; type CustomerPayload = { user_id?: string; first_name: string; last_name: string; email?: string; phone_number: number; residence?: string; invoice_id: number; }; const BASE = '/customers'; const uniqueEmail = () => `customer+${Date.now()}_${Math.random().toString(36).slice(2,8)}@test.local`; const uniquePhone = () => Math.floor(100_000_000 + Math.random() * 900_000_000); function makeCustomerPayload(overrides: Partial = {}): CustomerPayload { return { first_name: 'Gandalf', last_name: 'TheGray', email: uniqueEmail(), phone_number: uniquePhone(), residence: '1 Ringbearer’s Way, Mount Doom, ME', invoice_id: Math.floor(1_000_000 + Math.random() * 9_000_000), ...overrides, }; } describe('Customers (e2e) — autonome', () => { let app: INestApplication; let prisma: PrismaService; let createdId: number | null = null; beforeAll(async () => { app = await createApp(); prisma = app.get(PrismaService); }); afterAll(async () => { if (createdId) { try { await prisma.customers.delete({ where: { id: createdId } }); } catch {} } await app.close(); await prisma.$disconnect(); }); it(`GET ${BASE} → 200 (array)`, async () => { const res = await request(app.getHttpServer()).get(BASE); expect(res.status).toBe(200); expect(Array.isArray(res.body)).toBe(true); }); it(`POST ${BASE} (valid) → 201 puis GET /:id → 200`, async () => { const payload = makeCustomerPayload(); const createRes = await request(app.getHttpServer()).post(BASE).send(payload); if (createRes.status !== 201) { console.log('Create error:', createRes.body || createRes.text); } expect(createRes.status).toBe(201); expect(createRes.body).toEqual( expect.objectContaining({ id: expect.any(Number), user_id: expect.any(String), invoice_id: payload.invoice_id, }) ); expect(createRes.body.user_id).toMatch(/^[0-9a-fA-F-]{36}$/); createdId = createRes.body.id; const getRes = await request(app.getHttpServer()).get(`${BASE}/${createdId}`); expect(getRes.status).toBe(200); expect(getRes.body).toEqual(expect.objectContaining({ id: createdId })); }); it(`PATCH ${BASE}/:id → 200 (first_name mis à jour)`, async () => { if (!createdId) { const create = await request(app.getHttpServer()).post(BASE).send(makeCustomerPayload()); expect(create.status).toBe(201); createdId = create.body.id; } const patchRes = await request(app.getHttpServer()) .patch(`${BASE}/${createdId}`) .send({ first_name: 'Mithrandir' }); expect([200, 204]).toContain(patchRes.status); const getRes = await request(app.getHttpServer()).get(`${BASE}/${createdId}`); expect(getRes.status).toBe(200); expect(getRes.body.first_name ?? 'Mithrandir').toBe('Mithrandir'); }); it(`GET ${BASE}/:id (not found) → 404/400`, async () => { const res = await request(app.getHttpServer()).get(`${BASE}/999999`); expect([404, 400]).toContain(res.status); }); it(`POST ${BASE} (invalid payload) → 400`, async () => { const res = await request(app.getHttpServer()).post(BASE).send({}); expect(res.status).toBeGreaterThanOrEqual(400); expect(res.status).toBeLessThan(500); }); it(`DELETE ${BASE}/:id → 200/204`, async () => { let id = createdId; if (!id) { const create = await request(app.getHttpServer()).post(BASE).send(makeCustomerPayload()); expect(create.status).toBe(201); id = create.body.id; } const del = await request(app.getHttpServer()).delete(`${BASE}/${id}`); expect([200, 204]).toContain(del.status); if (createdId === id) createdId = null; }); });