ec-nest/test/app.e2e-spec.ts
liuyun 6937f9ece0 test: 修复 e2e + 补单元测试
- e2e 改为登录链路(成功返回 token / 密码错误 401)
- 补 maskSensitive、resolveDataScope 单测

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-11 17:01:30 +08:00

46 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<App>;
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();
});
});