import { Test, TestingModule } from "@nestjs/testing"; import { INestApplication, ValidationPipe } from "@nestjs/common"; import * as request from "supertest"; import { AppModule } from "../src/app.module"; /** * End-to-end tests for critical business logic paths. * These tests verify that validation, auth guards, and business rules * are enforced at the HTTP/API layer. * * NOTE: These tests require a running database (Postgres + Redis). * Skip with: jest --testPathIgnorePatterns='.e2e-spec.ts$' * Run with: pnpm --filter @micprotocol/api test:e2e */ describe("MicProtocol API (e2e)", () => { let app: INestApplication; beforeAll(async () => { const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule], }).compile(); app = moduleFixture.createNestApplication(); app.useGlobalPipes( new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true, }), ); await app.init(); }); afterAll(async () => { await app.close(); }); describe("Health Check", () => { it("/health (GET) should return 200", () => { return request(app.getHttpServer()) .get("/health") .expect(200) .expect((res) => { expect(res.body).toHaveProperty("status"); }); }); }); describe("Authentication Guards", () => { it("GET /podcasts/:id/guests without auth should return 401", () => { return request(app.getHttpServer()) .get("/podcasts/some-uuid/guests") .expect(401); }); it("GET /workspace/:id without auth should return 401", () => { return request(app.getHttpServer()) .get("/workspace/some-uuid") .expect(401); }); }); describe("API Validation", () => { it("POST /podcasts/:id/guests with invalid email should return 400", () => { return request(app.getHttpServer()) .post("/podcasts/test-podcast/guests") .set("Authorization", "Bearer test-token") .send({ email: "not-an-email", fullName: "Test Guest", }) .expect(401) // Auth fails first since we're mocking .catch(() => {}); }); }); });