鉴权改为登录态回库校验,新增 tokenValidAfter 失效时间,支持密码变更与 seed 重置后旧 token 立即失效 患者字段由 idCardHash 统一迁移为 idCard,新增身份证标准化逻辑并同步 C 端生命周期查询参数 组织模块增加小组删除限制(有成员时返回 409)并补充中文错误消息 任务取消接口支持可选 reason 字段(先透传事件层) 补齐 Prisma 迁移、文档说明和 E2E 用例(含设备模块与 token 失效场景)
124 lines
3.8 KiB
TypeScript
124 lines
3.8 KiB
TypeScript
import request from 'supertest';
|
||
import { Role } from '../../../src/generated/prisma/enums.js';
|
||
import {
|
||
closeE2EContext,
|
||
createE2EContext,
|
||
type E2EContext,
|
||
} from '../helpers/e2e-context.helper.js';
|
||
import { assertRoleMatrix } from '../helpers/e2e-matrix.helper.js';
|
||
import {
|
||
expectErrorEnvelope,
|
||
expectSuccessEnvelope,
|
||
uniquePhone,
|
||
uniqueSeedValue,
|
||
} from '../helpers/e2e-http.helper.js';
|
||
|
||
describe('AuthController (e2e)', () => {
|
||
let ctx: E2EContext;
|
||
|
||
beforeAll(async () => {
|
||
ctx = await createE2EContext();
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await closeE2EContext(ctx);
|
||
});
|
||
|
||
describe('POST /auth/system-admin', () => {
|
||
it('成功:创建系统管理员账号', async () => {
|
||
const response = await request(ctx.app.getHttpServer())
|
||
.post('/auth/system-admin')
|
||
.send({
|
||
name: uniqueSeedValue('Auth 系统管理员'),
|
||
phone: uniquePhone(),
|
||
password: 'Seed@1234',
|
||
openId: uniqueSeedValue('auth-system-admin-openid'),
|
||
systemAdminBootstrapKey: process.env.SYSTEM_ADMIN_BOOTSTRAP_KEY,
|
||
});
|
||
|
||
expectSuccessEnvelope(response, 201);
|
||
expect(response.body.data.role).toBe(Role.SYSTEM_ADMIN);
|
||
});
|
||
|
||
it('失败:参数不合法返回 400', async () => {
|
||
const response = await request(ctx.app.getHttpServer())
|
||
.post('/auth/system-admin')
|
||
.send({
|
||
name: 'bad-system-admin',
|
||
phone: '13800009999',
|
||
password: '123',
|
||
systemAdminBootstrapKey: process.env.SYSTEM_ADMIN_BOOTSTRAP_KEY,
|
||
});
|
||
|
||
expectErrorEnvelope(response, 400, '密码长度至少 8 位');
|
||
});
|
||
});
|
||
|
||
describe('POST /auth/login', () => {
|
||
it('成功:seed 账号登录并拿到 token', async () => {
|
||
const response = await request(ctx.app.getHttpServer())
|
||
.post('/auth/login')
|
||
.send({
|
||
phone: '13800001004',
|
||
password: 'Seed@1234',
|
||
role: Role.DOCTOR,
|
||
hospitalId: ctx.fixtures.hospitalAId,
|
||
});
|
||
|
||
expectSuccessEnvelope(response, 201);
|
||
expect(response.body.data.accessToken).toEqual(expect.any(String));
|
||
expect(response.body.data.actor.role).toBe(Role.DOCTOR);
|
||
});
|
||
|
||
it('失败:密码错误返回 401', async () => {
|
||
const response = await request(ctx.app.getHttpServer())
|
||
.post('/auth/login')
|
||
.send({
|
||
phone: '13800001004',
|
||
password: 'Seed@12345',
|
||
role: Role.DOCTOR,
|
||
hospitalId: ctx.fixtures.hospitalAId,
|
||
});
|
||
|
||
expectErrorEnvelope(response, 401, '手机号、角色或密码错误');
|
||
});
|
||
});
|
||
|
||
describe('GET /auth/me', () => {
|
||
it('成功:已登录用户可读取当前信息', async () => {
|
||
const response = await request(ctx.app.getHttpServer())
|
||
.get('/auth/me')
|
||
.set('Authorization', `Bearer ${ctx.tokens[Role.DOCTOR]}`);
|
||
|
||
expectSuccessEnvelope(response, 200);
|
||
expect(response.body.data.role).toBe(Role.DOCTOR);
|
||
});
|
||
|
||
it('失败:未登录返回 401', async () => {
|
||
const response = await request(ctx.app.getHttpServer()).get('/auth/me');
|
||
expectErrorEnvelope(response, 401, '缺少 Bearer Token');
|
||
});
|
||
|
||
it('角色矩阵:6 角色都可访问,未登录 401', async () => {
|
||
await assertRoleMatrix({
|
||
name: 'GET /auth/me role matrix',
|
||
tokens: ctx.tokens,
|
||
expectedStatusByRole: {
|
||
[Role.SYSTEM_ADMIN]: 200,
|
||
[Role.HOSPITAL_ADMIN]: 200,
|
||
[Role.DIRECTOR]: 200,
|
||
[Role.LEADER]: 200,
|
||
[Role.DOCTOR]: 200,
|
||
[Role.ENGINEER]: 200,
|
||
},
|
||
sendAsRole: async (_role, token) =>
|
||
request(ctx.app.getHttpServer())
|
||
.get('/auth/me')
|
||
.set('Authorization', `Bearer ${token}`),
|
||
sendWithoutToken: async () =>
|
||
request(ctx.app.getHttpServer()).get('/auth/me'),
|
||
});
|
||
});
|
||
});
|
||
});
|