199 lines
7.3 KiB
JavaScript
199 lines
7.3 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const {
|
|
getAuthToken,
|
|
invokeSystemMethod,
|
|
} = require('../../support/system_method_client');
|
|
|
|
const EXTRA_HEADERS = {
|
|
platform: 'web',
|
|
project: 'all',
|
|
};
|
|
|
|
function findRoleNode(roleNodes, targetRoleId) {
|
|
if (!Array.isArray(roleNodes) || !Number.isFinite(Number(targetRoleId))) {
|
|
return null;
|
|
}
|
|
|
|
for (const item of roleNodes) {
|
|
if (Number(item && item.role_id) === Number(targetRoleId)) {
|
|
return item;
|
|
}
|
|
|
|
const childMatch = findRoleNode(item && item.children, targetRoleId);
|
|
if (childMatch) {
|
|
return childMatch;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
describe('test/app/controller/system.setting.role.test.js', () => {
|
|
let token = '';
|
|
let anchorPermissionId = 0;
|
|
let currentRoleId = 0;
|
|
const cleanupRoleIds = [];
|
|
|
|
before(async () => {
|
|
const auth = await getAuthToken({ forceLogin: true });
|
|
token = auth.token;
|
|
|
|
const permissionRes = await invokeSystemMethod('setting.permission.list', {
|
|
includeDisabled: false,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(permissionRes.status, 200);
|
|
assert.equal(permissionRes.body.flag, 'success');
|
|
assert.equal(permissionRes.body.code, 200);
|
|
|
|
const permissions = Array.isArray(permissionRes.body?.data?.permissions)
|
|
? permissionRes.body.data.permissions
|
|
: [];
|
|
assert.ok(permissions.length > 0, '当前系统没有可用权限,无法创建测试角色');
|
|
anchorPermissionId = Number(permissions[0].permission_id || 0);
|
|
assert.ok(anchorPermissionId > 0, '锚点权限ID无效');
|
|
|
|
currentRoleId = Number(permissionRes.body?.data?.role_id || 0);
|
|
assert.ok(currentRoleId > 0, '当前角色ID无效');
|
|
});
|
|
|
|
after(async () => {
|
|
if (!token || cleanupRoleIds.length === 0) {
|
|
return;
|
|
}
|
|
|
|
await invokeSystemMethod('setting.role.delete', {
|
|
ids: cleanupRoleIds,
|
|
}, token, EXTRA_HEADERS);
|
|
});
|
|
|
|
it('should support wildcard role permissions via ["*"]', async () => {
|
|
const suffix = Date.now();
|
|
const wildcardAddRes = await invokeSystemMethod('setting.role.add', {
|
|
role_code: `autotest_role_all_${suffix}`,
|
|
role_name: `autotest_role_all_${suffix}`,
|
|
description: 'role wildcard regression',
|
|
pid: currentRoleId,
|
|
permission_ids: [ '*' ],
|
|
status: 1,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(wildcardAddRes.status, 200);
|
|
assert.equal(wildcardAddRes.body.flag, 'success');
|
|
assert.equal(wildcardAddRes.body.code, 200);
|
|
|
|
const wildcardRoleId = Number(wildcardAddRes.body?.data?.role_id || 0);
|
|
assert.ok(wildcardRoleId > 0, '创建全权限角色失败');
|
|
cleanupRoleIds.push(wildcardRoleId);
|
|
|
|
const permissionRes = await invokeSystemMethod('setting.role.permission', {
|
|
role_id: wildcardRoleId,
|
|
includeDisabled: false,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(permissionRes.status, 200);
|
|
assert.equal(permissionRes.body.flag, 'success');
|
|
assert.equal(permissionRes.body.code, 200);
|
|
|
|
const permissions = Array.isArray(permissionRes.body?.data?.permissions)
|
|
? permissionRes.body.data.permissions
|
|
: [];
|
|
assert.ok(permissions.length > 0, '全权限角色应能看到权限列表');
|
|
assert.ok(permissions.every(item => item && item.selected === true), '当 permission_ids=["*"] 时,所有权限都应为选中状态');
|
|
|
|
const listRes = await invokeSystemMethod('setting.role.list', {
|
|
includeDisabled: false,
|
|
includeSystem: true,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(listRes.status, 200);
|
|
assert.equal(listRes.body.flag, 'success');
|
|
assert.equal(listRes.body.code, 200);
|
|
|
|
const roots = Array.isArray(listRes.body?.data?.roles)
|
|
? listRes.body.data.roles
|
|
: [];
|
|
const wildcardNode = findRoleNode(roots, wildcardRoleId);
|
|
assert.ok(wildcardNode, '返回结果中未包含新建的全权限角色');
|
|
assert.ok(Array.isArray(wildcardNode.permission_ids), '全权限角色应返回 permission_ids');
|
|
assert.ok(wildcardNode.permission_ids.includes('*'), '全权限角色应以 ["*"] 表示拥有全部权限');
|
|
});
|
|
|
|
it('should only return descendant roles and keep their children', async () => {
|
|
const suffix = Date.now();
|
|
|
|
const childAddRes = await invokeSystemMethod('setting.role.add', {
|
|
role_code: `autotest_role_child_${suffix}`,
|
|
role_name: `autotest_role_child_${suffix}`,
|
|
description: 'role tree child regression',
|
|
pid: currentRoleId,
|
|
permission_ids: [ anchorPermissionId ],
|
|
status: 1,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(childAddRes.status, 200);
|
|
assert.equal(childAddRes.body.flag, 'success');
|
|
assert.equal(childAddRes.body.code, 200);
|
|
|
|
const createdChildRoleId = Number(childAddRes.body?.data?.role_id || 0);
|
|
assert.ok(createdChildRoleId > 0, '创建下级角色失败');
|
|
cleanupRoleIds.push(createdChildRoleId);
|
|
|
|
const grandChildAddRes = await invokeSystemMethod('setting.role.add', {
|
|
role_code: `autotest_role_grandchild_${suffix}`,
|
|
role_name: `autotest_role_grandchild_${suffix}`,
|
|
description: 'role tree grandchild regression',
|
|
pid: createdChildRoleId,
|
|
permission_ids: [ anchorPermissionId ],
|
|
status: 1,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(grandChildAddRes.status, 200);
|
|
assert.equal(grandChildAddRes.body.flag, 'success');
|
|
assert.equal(grandChildAddRes.body.code, 200);
|
|
|
|
const createdGrandChildRoleId = Number(grandChildAddRes.body?.data?.role_id || 0);
|
|
assert.ok(createdGrandChildRoleId > 0, '创建孙级角色失败');
|
|
cleanupRoleIds.unshift(createdGrandChildRoleId);
|
|
|
|
const listRes = await invokeSystemMethod('setting.role.list', {
|
|
includeDisabled: false,
|
|
includeSystem: true,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(listRes.status, 200);
|
|
assert.equal(listRes.body.flag, 'success');
|
|
assert.equal(listRes.body.code, 200);
|
|
|
|
const roots = Array.isArray(listRes.body?.data?.roles)
|
|
? listRes.body.data.roles
|
|
: [];
|
|
assert.ok(roots.length > 0, '应返回至少一层下级角色');
|
|
assert.ok(
|
|
roots.every(item => Number(item && item.pid) === currentRoleId),
|
|
'setting.role.list 第一层只应返回当前角色的直接下级角色'
|
|
);
|
|
assert.ok(
|
|
!roots.some(item => Number(item && item.role_id) === currentRoleId),
|
|
'setting.role.list 不应返回当前角色本身'
|
|
);
|
|
|
|
const createdChildNode = findRoleNode(roots, createdChildRoleId);
|
|
assert.ok(createdChildNode, '返回结果中未包含新建的下级角色');
|
|
assert.ok(
|
|
Array.isArray(createdChildNode.children)
|
|
&& createdChildNode.children.some(item => Number(item && item.role_id) === createdGrandChildRoleId),
|
|
'下级角色的 children 组装异常:新建孙级角色未挂到对应父角色下'
|
|
);
|
|
assert.ok(Array.isArray(createdChildNode.permission_ids), '下级角色节点应返回 permission_ids 权限集');
|
|
assert.ok(createdChildNode.permission_ids.includes(anchorPermissionId), '下级角色未返回已绑定权限集');
|
|
|
|
const createdGrandChildNode = findRoleNode(roots, createdGrandChildRoleId);
|
|
assert.ok(createdGrandChildNode, '返回结果中未包含新建的孙级角色');
|
|
assert.ok(Array.isArray(createdGrandChildNode.permission_ids), '孙级角色节点应返回 permission_ids 权限集');
|
|
assert.ok(createdGrandChildNode.permission_ids.includes(anchorPermissionId), '孙级角色未返回已绑定权限集');
|
|
});
|
|
});
|