import { Test, TestingModule } from '@nestjs/testing'; import { INestApplication, ValidationPipe } from '@nestjs/common'; import request from 'supertest'; import { App } from 'supertest/types.js'; import { AppModule } from './../src/app.module.js'; /** * 登录链路 e2e:需要已启动的 MySQL/Redis 且已执行 pnpm seed(默认账号 touyun / Touyun@123)。 */ describe('Auth (e2e)', () => { let app: INestApplication; beforeAll(async () => { const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule], }).compile(); app = moduleFixture.createNestApplication(); app.setGlobalPrefix('api/v1'); app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true })); await app.init(); }); it('登录成功返回 token(按用户名自动探测租户)', async () => { const res = await request(app.getHttpServer()) .post('/api/v1/auth/login') .send({ username: 'touyun', password: 'Touyun@123' }) .expect(201); expect(res.body.code).toBe(0); expect(res.body.data.accessToken).toBeDefined(); expect(res.body.data.refreshToken).toBeDefined(); }); it('密码错误返回 401', async () => { const res = await request(app.getHttpServer()) .post('/api/v1/auth/login') .send({ username: 'touyun', password: 'wrong-password' }) .expect(401); expect(res.body.code).toBe(40100); }); afterAll(async () => { await app.close(); }); });