WMS_MAIN/test/app/service/systemMethods/user.getAccountInfo.test.js
liufumingren 640ce501cb feat(user): 新增账号用户名/密码修改与自定义密码标记
- wms_account 新增 is_custom_password(TINYINT(1) NOT NULL DEFAULT 0),同步模型与迁移 20261025135000;开发库、生产库均已执行

- user.getAccountInfo 返回 is_custom_password,仍不返回 password_hash/salt/last_login_ip

- 新增 user.updateUsername:仅改当前账号,服务端重名校验并排除自己,重名返回 409,更新后清理用户缓存

- 新增 user.changePassword:is_custom_password=0 时旧密码可不传,修改后置 1;再次修改必须校验旧密码;已排除操作日志避免明文密码落库

- 单测覆盖 getAccountInfo/updateUsername/changePassword,20 通过;同步 AI_METHOD_ROUTING

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-27 03:08:51 +08:00

140 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,
is_custom_password: 0,
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', 'is_custom_password', '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, '账号不存在');
});
});