99 lines
3.2 KiB
JavaScript
99 lines
3.2 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const {
|
|
getAuthToken,
|
|
invokeSystemMethod,
|
|
} = require('../../support/system_method_client');
|
|
|
|
const RUNTIME_FILE = path.join(process.cwd(), 'test', '.runtime', 'system_user_change_role_web_all.json');
|
|
|
|
function saveRuntime(payload) {
|
|
fs.mkdirSync(path.dirname(RUNTIME_FILE), { recursive: true });
|
|
fs.writeFileSync(RUNTIME_FILE, JSON.stringify(payload, null, 2), 'utf8');
|
|
}
|
|
|
|
describe('test/app/controller/system.user.changeRole.test.js', () => {
|
|
it('should switch role via user.changeRole and persist response', async () => {
|
|
const { token } = await getAuthToken();
|
|
|
|
const beforeInfoRes = await invokeSystemMethod('user.info', {}, token, {
|
|
platform: 'web',
|
|
project: 'all',
|
|
});
|
|
|
|
assert.equal(beforeInfoRes.status, 200);
|
|
assert.equal(beforeInfoRes.body.flag, 'success');
|
|
assert.equal(beforeInfoRes.body.code, 200);
|
|
|
|
const beforeData = beforeInfoRes.body.data || {};
|
|
const employees = Array.isArray(beforeData.employees) ? beforeData.employees : [];
|
|
const currentRoleId = Number(beforeData.employees_id || 0);
|
|
const target = employees.find(item => Number(item.id) && Number(item.id) !== currentRoleId)
|
|
|| employees.find(item => Number(item.id));
|
|
|
|
assert.ok(target, '当前账号没有可用于切换的角色');
|
|
|
|
const changeRoleRes = await invokeSystemMethod('user.changeRole', {
|
|
roleId: Number(target.id),
|
|
}, token, {
|
|
platform: 'web',
|
|
project: 'all',
|
|
});
|
|
|
|
assert.equal(changeRoleRes.status, 200);
|
|
assert.equal(changeRoleRes.body.flag, 'success');
|
|
assert.equal(changeRoleRes.body.code, 200);
|
|
assert.equal(changeRoleRes.body.success, true);
|
|
assert.ok(changeRoleRes.body.data);
|
|
assert.ok(changeRoleRes.body.data.token);
|
|
|
|
const switchedToken = changeRoleRes.body.data.token;
|
|
const afterInfoRes = await invokeSystemMethod('user.info', {}, switchedToken, {
|
|
platform: 'web',
|
|
project: 'all',
|
|
});
|
|
|
|
assert.equal(afterInfoRes.status, 200);
|
|
assert.equal(afterInfoRes.body.flag, 'success');
|
|
assert.equal(afterInfoRes.body.code, 200);
|
|
assert.equal(Number(afterInfoRes.body.data.employees_id || 0), Number(target.id));
|
|
|
|
saveRuntime({
|
|
savedAt: new Date().toISOString(),
|
|
method: 'user.changeRole',
|
|
headers: {
|
|
platform: 'web',
|
|
project: 'all',
|
|
},
|
|
before: {
|
|
employees_id: currentRoleId,
|
|
employees_count: employees.length,
|
|
},
|
|
targetRoleId: Number(target.id),
|
|
response: changeRoleRes.body,
|
|
verify: {
|
|
employees_id: Number(afterInfoRes.body.data.employees_id || 0),
|
|
},
|
|
});
|
|
|
|
assert.equal(fs.existsSync(RUNTIME_FILE), true);
|
|
|
|
// 尽量恢复原角色,避免影响其他测试。
|
|
if (currentRoleId && currentRoleId !== Number(target.id)) {
|
|
const restoreRes = await invokeSystemMethod('user.changeRole', {
|
|
roleId: currentRoleId,
|
|
}, switchedToken, {
|
|
platform: 'web',
|
|
project: 'all',
|
|
});
|
|
|
|
assert.equal(restoreRes.status, 200);
|
|
assert.equal(restoreRes.body.flag, 'success');
|
|
assert.equal(restoreRes.body.code, 200);
|
|
}
|
|
});
|
|
});
|