WMS_MAIN/test/app/service/systemMethods/user.getAccountInfo.test.js
liufumingren 0e7545bd9a feat(user): 新增 user.getAccountInfo 账号级信息查询
- 新增 user.getAccountInfo:仅查询当前登录账号的 wms_account 信息

- 白名单返回 id/username/real_name/phone/email/status/last_login_at/created_at/updated_at,排除 password_hash、salt、last_login_ip

- 未登录返回 401,账号不存在返回 404;不接收账号ID,避免越权查询

- 同步 methods/system.json(0.3.304 → 0.3.305)、变更记录、AI_METHOD_ROUTING 与方法文档库表

- 新增单测 test/app/service/systemMethods/user.getAccountInfo.test.js(5 通过)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-25 13:43:26 +08:00

139 lines
3.9 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const UserMethodService = require('../../../../app/service/systemMethods/user');
const UserSystemService = require('../../../../app/service/systems/auth/user');
function createMethodHarness(getAccountInfo) {
const calls = [];
const ctx = {
app: { config: {}, Sequelize: {} },
logger: { error() {} },
service: {
systems: {
auth: {
user: {
async getAccountInfo(data) {
calls.push(data);
return getAccountInfo(data);
},
},
},
},
},
};
return {
service: new UserMethodService(ctx),
calls,
};
}
function createSystemHarness(options = {}) {
const calls = [];
const ctx = {
app: { config: {}, Sequelize: {} },
userId: options.userId,
user: options.user,
logger: { error() {} },
model: {
Account: {
async findOne(args) {
calls.push(args);
return options.account === undefined ? null : options.account;
},
},
},
};
return {
service: new UserSystemService(ctx),
calls,
};
}
describe('test/app/service/systemMethods/user.getAccountInfo.test.js', () => {
it('方法层应透传系统层结果并返回成功结构', async () => {
const payload = {
success: true,
data: { id: 1, username: 'admin' },
code: 200,
message: '获取账号信息成功',
};
const { service, calls } = createMethodHarness(async () => payload);
const result = await service.getAccountInfo({ id: 99 });
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.deepEqual(result.data, payload.data);
assert.deepEqual(calls, [{ id: 99 }]);
});
it('方法层应保留系统层业务失败码', async () => {
const { service } = createMethodHarness(async () => ({
success: false,
data: null,
code: 404,
message: '账号不存在',
}));
const result = await service.getAccountInfo({});
assert.equal(result.flag, 'failure');
assert.equal(result.code, 404);
assert.equal(result.message, '账号不存在');
});
it('系统层只按登录账号查询并排除敏感字段', async () => {
const account = {
id: 7,
username: 'demo',
real_name: '演示账号',
phone: '13800000000',
email: 'demo@example.com',
status: 1,
last_login_at: new Date('2026-10-25T05:00:00.000Z'),
created_at: new Date('2026-01-01T00:00:00.000Z'),
updated_at: new Date('2026-10-25T05:00:00.000Z'),
};
const { service, calls } = createSystemHarness({ userId: 7, account });
const result = await service.getAccountInfo({ id: 999 });
assert.equal(result.success, true);
assert.equal(result.code, 200);
assert.deepEqual(result.data, account);
assert.equal(calls.length, 1);
assert.deepEqual(calls[0].where, { id: 7 });
assert.equal(calls[0].raw, true);
assert.deepEqual(calls[0].attributes, [
'id', 'username', 'real_name', 'phone', 'email',
'status', 'last_login_at', 'created_at', 'updated_at',
]);
for (const field of [ 'password_hash', 'salt', 'last_login_ip' ]) {
assert.ok(!calls[0].attributes.includes(field));
}
});
it('系统层未登录时返回 401 且不查库', async () => {
const { service, calls } = createSystemHarness({ account: { id: 1 } });
const result = await service.getAccountInfo();
assert.equal(result.success, false);
assert.equal(result.code, 401);
assert.equal(calls.length, 0);
});
it('系统层账号不存在时返回 404', async () => {
const { service } = createSystemHarness({ userId: 7, account: null });
const result = await service.getAccountInfo();
assert.equal(result.success, false);
assert.equal(result.code, 404);
assert.equal(result.message, '账号不存在');
});
});