201 lines
6.8 KiB
JavaScript
201 lines
6.8 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const {
|
|
getAuthToken,
|
|
invokeSystemMethod,
|
|
invokeGatewayMethod,
|
|
} = require('../../support/system_method_client');
|
|
|
|
const YE4S_TEST_USER_ID = Number(process.env.SYSTEM_TEST_YE4S_USER_ID);
|
|
|
|
assert.ok(
|
|
Number.isInteger(YE4S_TEST_USER_ID) && YE4S_TEST_USER_ID > 0,
|
|
'SYSTEM_TEST_YE4S_USER_ID 必须显式提供且为正整数'
|
|
);
|
|
|
|
const EXTRA_HEADERS = {
|
|
platform: 'web',
|
|
project: 'all',
|
|
};
|
|
|
|
function buildTempPhone() {
|
|
return `139${String(Date.now()).slice(-8)}`;
|
|
}
|
|
|
|
function flattenRoleIds(roleNodes, collector = []) {
|
|
if (!Array.isArray(roleNodes)) {
|
|
return collector;
|
|
}
|
|
for (const item of roleNodes) {
|
|
const roleId = Number(item && item.role_id);
|
|
if (Number.isFinite(roleId) && roleId > 0) {
|
|
collector.push(roleId);
|
|
}
|
|
flattenRoleIds(item && item.children, collector);
|
|
}
|
|
return collector;
|
|
}
|
|
|
|
describe('test/app/controller/system.setting.account.getBindWechatCode.test.js', () => {
|
|
let token = '';
|
|
let tempEmployeeId = 0;
|
|
let tempAccountId = 0;
|
|
|
|
async function getAssignableRoleIds() {
|
|
const res = await invokeSystemMethod('setting.role.list', {
|
|
includeDisabled: false,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.flag, 'success');
|
|
assert.equal(res.body.code, 200);
|
|
|
|
const roleIds = Array.from(new Set(flattenRoleIds(res.body?.data?.roles || [])));
|
|
assert.ok(roleIds.length > 0, '当前账号没有可分配角色,无法创建测试账号');
|
|
return [ roleIds[0] ];
|
|
}
|
|
|
|
before(async () => {
|
|
const auth = await getAuthToken();
|
|
token = auth.token;
|
|
|
|
const roleIds = await getAssignableRoleIds();
|
|
const addRes = await invokeSystemMethod('setting.account.add', {
|
|
phone: buildTempPhone(),
|
|
username: `autotest_bind_wechat_${Date.now()}`,
|
|
password: '111111',
|
|
real_name: `微信代绑测试_${Date.now()}`,
|
|
ids: roleIds,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(addRes.status, 200);
|
|
assert.equal(addRes.body.flag, 'success');
|
|
assert.equal(addRes.body.code, 200);
|
|
|
|
tempEmployeeId = Number(addRes.body?.data?.employee_id || 0);
|
|
tempAccountId = Number(addRes.body?.data?.account_id || 0);
|
|
assert.ok(tempEmployeeId > 0);
|
|
assert.ok(tempAccountId > 0);
|
|
});
|
|
|
|
after(async () => {
|
|
if (!token || !tempEmployeeId) {
|
|
return;
|
|
}
|
|
|
|
const deleteRes = await invokeSystemMethod('setting.account.delete', {
|
|
employee_id: tempEmployeeId,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(deleteRes.status, 200);
|
|
assert.equal(deleteRes.body.code, 200);
|
|
});
|
|
|
|
it('should get bind wechat code for an operable account by user_id', async () => {
|
|
const res = await invokeSystemMethod('setting.account.getBindWechatCode', {
|
|
user_id: tempAccountId,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.flag, 'success');
|
|
assert.equal(res.body.code, 200);
|
|
assert.equal(Number(res.body?.data?.account_id || 0), tempAccountId);
|
|
assert.equal(res.body?.data?.status, 'pending');
|
|
assert.equal(res.body?.data?.is_bound, false);
|
|
assert.match(String(res.body?.data?.code || ''), /^(?=.*[A-Z])(?=.*\d)[A-Z\d]{6}$/);
|
|
assert.ok(Number(res.body?.data?.expire_after_ms || 0) > 0);
|
|
});
|
|
|
|
it('should include wechat bind status in setting.account.list rows', async () => {
|
|
const res = await invokeSystemMethod('setting.account.list', {
|
|
where: {
|
|
account_id: tempAccountId,
|
|
},
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.flag, 'success');
|
|
assert.equal(res.body.code, 200);
|
|
|
|
const list = Array.isArray(res.body?.data) ? res.body.data : [];
|
|
const target = list.find(item => Number(item && item.account_id) === tempAccountId);
|
|
assert.ok(target, '新建账号未出现在列表中');
|
|
assert.equal(typeof target.is_wechat_bound, 'boolean');
|
|
assert.equal(target.is_wechat_bound, false);
|
|
assert.equal(target.ye4s_user_id, null);
|
|
});
|
|
|
|
it('should unbind wechat for an operable account', async () => {
|
|
const bindCodeRes = await invokeSystemMethod('setting.account.getBindWechatCode', {
|
|
user_id: tempAccountId,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(bindCodeRes.status, 200);
|
|
assert.equal(bindCodeRes.body.flag, 'success');
|
|
const code = String(bindCodeRes.body?.data?.code || '');
|
|
assert.match(code, /^(?=.*[A-Z])(?=.*\d)[A-Z\d]{6}$/);
|
|
|
|
const scanRes = await invokeGatewayMethod('user.getUserByTempCode', {
|
|
code,
|
|
});
|
|
assert.equal(scanRes.status, 200);
|
|
assert.equal(scanRes.body.flag, 'success');
|
|
assert.equal(scanRes.body.code, 200);
|
|
assert.equal(Number(scanRes.body?.data?.account_id || 0), tempAccountId);
|
|
assert.equal(scanRes.body?.data?.status, 'scanned');
|
|
|
|
const bindRes = await invokeGatewayMethod('user.bindAccountByTempCode', {
|
|
code,
|
|
ye4s_user_id: YE4S_TEST_USER_ID,
|
|
});
|
|
assert.equal(bindRes.status, 200);
|
|
assert.equal(bindRes.body.flag, 'success');
|
|
assert.equal(bindRes.body.code, 200);
|
|
assert.equal(Number(bindRes.body?.data?.account_id || 0), tempAccountId);
|
|
|
|
const unbindRes = await invokeSystemMethod('setting.account.unbindWechat', {
|
|
user_id: tempAccountId,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(unbindRes.status, 200);
|
|
assert.equal(unbindRes.body.flag, 'success');
|
|
assert.equal(unbindRes.body.code, 200);
|
|
assert.equal(Number(unbindRes.body?.data?.account_id || 0), tempAccountId);
|
|
assert.equal(unbindRes.body?.data?.status, 'unbound');
|
|
assert.ok(Number(unbindRes.body?.data?.matched_rows || 0) >= 1);
|
|
|
|
const listRes = await invokeSystemMethod('setting.account.list', {
|
|
where: {
|
|
account_id: tempAccountId,
|
|
},
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(listRes.status, 200);
|
|
assert.equal(listRes.body.flag, 'success');
|
|
const list = Array.isArray(listRes.body?.data) ? listRes.body.data : [];
|
|
const target = list.find(item => Number(item && item.account_id) === tempAccountId);
|
|
assert.ok(target, '解绑后账号未出现在列表中');
|
|
assert.equal(target.is_wechat_bound, false);
|
|
assert.equal(target.ye4s_user_id, null);
|
|
});
|
|
|
|
it('should reject when user_id is missing', async () => {
|
|
const res = await invokeSystemMethod('setting.account.getBindWechatCode', {}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.flag, 'failure');
|
|
assert.equal(res.body.code, 400);
|
|
assert.match(String(res.body?.message || ''), /user_id 参数不能为空/);
|
|
});
|
|
|
|
it('should reject unbind when user_id is missing', async () => {
|
|
const res = await invokeSystemMethod('setting.account.unbindWechat', {}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.flag, 'failure');
|
|
assert.equal(res.body.code, 400);
|
|
assert.match(String(res.body?.message || ''), /user_id 参数不能为空/);
|
|
});
|
|
});
|