- 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>
209 lines
6.1 KiB
JavaScript
209 lines
6.1 KiB
JavaScript
'use strict';
|
||
|
||
const assert = require('node:assert/strict');
|
||
const crypto = require('node:crypto');
|
||
const UserMethodService = require('../../../../app/service/systemMethods/user');
|
||
const UserSystemService = require('../../../../app/service/systems/auth/user');
|
||
|
||
const SALT = '0123456789abcdef0123';
|
||
const OLD_PASSWORD = 'old-pass';
|
||
const OLD_HASH = crypto.createHash('sha256').update(OLD_PASSWORD + SALT).digest('hex');
|
||
|
||
function createSystemHarness(options = {}) {
|
||
const calls = {
|
||
findOne: [],
|
||
update: [],
|
||
cleared: [],
|
||
};
|
||
const findOneResults = [ ...(options.findOneResults || []) ];
|
||
const ctx = {
|
||
app: { config: {}, Sequelize: {} },
|
||
userId: options.userId,
|
||
user: options.user,
|
||
logger: { error() {} },
|
||
model: {
|
||
Account: {
|
||
async findOne(args) {
|
||
calls.findOne.push(args);
|
||
return findOneResults.shift();
|
||
},
|
||
async update(values, updateOptions) {
|
||
calls.update.push({ values, updateOptions });
|
||
return [ 1 ];
|
||
},
|
||
},
|
||
},
|
||
};
|
||
|
||
const service = new UserSystemService(ctx);
|
||
service.clearUserCache = async userId => {
|
||
calls.cleared.push(userId);
|
||
return true;
|
||
};
|
||
|
||
return { service, calls };
|
||
}
|
||
|
||
function createMethodHarness(changePassword) {
|
||
const calls = [];
|
||
const ctx = {
|
||
app: { config: {}, Sequelize: {} },
|
||
logger: { error() {} },
|
||
service: {
|
||
systems: {
|
||
auth: {
|
||
user: {
|
||
async changePassword(data) {
|
||
calls.push(data);
|
||
return changePassword(data);
|
||
},
|
||
},
|
||
},
|
||
},
|
||
},
|
||
};
|
||
|
||
return {
|
||
service: new UserMethodService(ctx),
|
||
calls,
|
||
};
|
||
}
|
||
|
||
function assertNewPasswordHash(values, newPassword) {
|
||
assert.equal(values.is_custom_password, 1);
|
||
assert.match(values.salt, /^[0-9a-f]{20}$/);
|
||
const expected = crypto.createHash('sha256').update(newPassword + values.salt).digest('hex');
|
||
assert.equal(values.password_hash, expected);
|
||
}
|
||
|
||
describe('test/app/service/systemMethods/user.changePassword.test.js', () => {
|
||
it('首次自定义密码无需旧密码,并更新 is_custom_password', async () => {
|
||
const { service, calls } = createSystemHarness({
|
||
userId: 7,
|
||
findOneResults: [
|
||
{ id: 7, password_hash: OLD_HASH, salt: SALT, is_custom_password: 0 },
|
||
],
|
||
});
|
||
|
||
const result = await service.changePassword({ new_password: 'new-pass' });
|
||
|
||
assert.equal(result.success, true);
|
||
assert.equal(result.code, 200);
|
||
assert.deepEqual(result.data, { is_custom_password: 1 });
|
||
assert.equal(calls.update.length, 1);
|
||
assert.deepEqual(calls.update[0].updateOptions, { where: { id: 7 } });
|
||
assertNewPasswordHash(calls.update[0].values, 'new-pass');
|
||
assert.deepEqual(calls.cleared, [ 7 ]);
|
||
});
|
||
|
||
it('已自定义密码后必须校验旧密码', async () => {
|
||
const { service, calls } = createSystemHarness({
|
||
userId: 7,
|
||
findOneResults: [
|
||
{ id: 7, password_hash: OLD_HASH, salt: SALT, is_custom_password: 1 },
|
||
],
|
||
});
|
||
|
||
const result = await service.changePassword({
|
||
old_password: OLD_PASSWORD,
|
||
new_password: 'new-pass',
|
||
});
|
||
|
||
assert.equal(result.success, true);
|
||
assert.equal(calls.update.length, 1);
|
||
assertNewPasswordHash(calls.update[0].values, 'new-pass');
|
||
});
|
||
|
||
it('已自定义密码但未传旧密码返回 400', async () => {
|
||
const { service, calls } = createSystemHarness({
|
||
userId: 7,
|
||
findOneResults: [
|
||
{ id: 7, password_hash: OLD_HASH, salt: SALT, is_custom_password: 1 },
|
||
],
|
||
});
|
||
|
||
const result = await service.changePassword({ new_password: 'new-pass' });
|
||
|
||
assert.equal(result.success, false);
|
||
assert.equal(result.code, 400);
|
||
assert.equal(result.message, '请输入旧密码');
|
||
assert.equal(calls.update.length, 0);
|
||
});
|
||
|
||
it('旧密码错误返回 400', async () => {
|
||
const { service, calls } = createSystemHarness({
|
||
userId: 7,
|
||
findOneResults: [
|
||
{ id: 7, password_hash: OLD_HASH, salt: SALT, is_custom_password: 1 },
|
||
],
|
||
});
|
||
|
||
const result = await service.changePassword({
|
||
old_password: 'wrong-pass',
|
||
new_password: 'new-pass',
|
||
});
|
||
|
||
assert.equal(result.success, false);
|
||
assert.equal(result.code, 400);
|
||
assert.equal(result.message, '旧密码错误');
|
||
assert.equal(calls.update.length, 0);
|
||
});
|
||
|
||
it('支持旧密码/新密码驼峰别名', async () => {
|
||
const { service, calls } = createSystemHarness({
|
||
userId: 7,
|
||
findOneResults: [
|
||
{ id: 7, password_hash: OLD_HASH, salt: SALT, is_custom_password: 1 },
|
||
],
|
||
});
|
||
|
||
const result = await service.changePassword({
|
||
oldPassword: OLD_PASSWORD,
|
||
newPassword: 'new-pass',
|
||
});
|
||
|
||
assert.equal(result.success, true);
|
||
assert.equal(calls.update.length, 1);
|
||
});
|
||
|
||
it('新密码为空返回 400', async () => {
|
||
const { service, calls } = createSystemHarness({ userId: 7 });
|
||
|
||
const result = await service.changePassword({ new_password: ' ' });
|
||
|
||
assert.equal(result.success, false);
|
||
assert.equal(result.code, 400);
|
||
assert.equal(result.message, '新密码不能为空');
|
||
assert.equal(calls.findOne.length, 0);
|
||
});
|
||
|
||
it('未登录返回 401,账号不存在返回 404', async () => {
|
||
const anonymous = createSystemHarness({});
|
||
const missing = createSystemHarness({
|
||
userId: 7,
|
||
findOneResults: [ null ],
|
||
});
|
||
|
||
const anonymousResult = await anonymous.service.changePassword({ new_password: 'new-pass' });
|
||
const missingResult = await missing.service.changePassword({ new_password: 'new-pass' });
|
||
|
||
assert.equal(anonymousResult.code, 401);
|
||
assert.equal(missingResult.code, 404);
|
||
});
|
||
|
||
it('方法层应保留业务失败码', async () => {
|
||
const { service, calls } = createMethodHarness(async () => ({
|
||
success: false,
|
||
data: null,
|
||
code: 400,
|
||
message: '旧密码错误',
|
||
}));
|
||
|
||
const result = await service.changePassword({ old_password: 'bad', new_password: 'new-pass' });
|
||
|
||
assert.equal(result.flag, 'failure');
|
||
assert.equal(result.code, 400);
|
||
assert.deepEqual(calls, [{ old_password: 'bad', new_password: 'new-pass' }]);
|
||
});
|
||
});
|