This commit is contained in:
gr
2026-11-16 09:28:25 +08:00
parent 6a2b387edc
commit 44f26dc038
152 changed files with 15331 additions and 1538 deletions
@@ -60,16 +60,31 @@ ant.Table = vue.defineComponent({ props: ['dataSource', 'columns'], setup(props,
const catalog = [
{ code: 'user:read', name: '查看用户', group_name: '系统管理', page_name: '用户管理', kind: 'page' },
{ code: 'user:write', name: '修改用户角色', group_name: '系统管理', page_name: '用户管理', kind: 'action' },
{ code: 'custom:report', name: '自定义报表', group_name: '报表', page_name: '报表', kind: 'link' },
{ code: 'knowledge:read', name: '查看知识中心', group_name: '知识中心', page_name: '知识加工与检索', kind: 'page' },
{ code: 'knowledge:scope:own', name: '数据权限:仅本人账号的知识', group_name: '知识中心', page_name: '知识加工与检索', kind: 'action' },
];
const rows = [
{ code: 'admin', name: '管理员', permissions: ['user:read'], builtin: false, user_count: 0 },
{ code: 'zyt_basic', name: '集团基础用户', permissions: [], builtin: false, user_count: 0 },
{ code: 'reviewer', name: '审核员', permissions: ['user:read', 'custom:report', 'removed:code'], builtin: false, user_count: 0 },
{ code: 'operator', name: '操作员', permissions: ['user:read'], builtin: true, user_count: 0 },
{ code: 'viewer', name: '只读用户', permissions: ['user:read'], builtin: true, user_count: 0 },
];
async function page(codes, configure = () => {}) {
const calls = { save: [], remove: [], success: [] };
const calls = { save: [], remove: [], success: [], requests: [] };
// Exercise the actual API adapter so grant and revoke assertions cover the
// posted role payload, including an explicit empty permissions array.
const api = load(await readFile(resolve(workspace, 'apps/web-antd/src/api/console.ts'), 'utf8'), {
'#/api/request': { requestClient: {
async post(path, data) {
calls.requests.push({ path, data }); calls.save.push(data); return { roles: rows };
},
} },
'#/locales/t': translate, '#/utils/file-sha256': {},
});
let state;
const component = load(script.content, {
vue, 'vue-router': { useRouter: () => ({ push() {} }) },
@@ -78,7 +93,7 @@ async function page(codes, configure = () => {}) {
'#/locales/t': translate, './permission-menu': helpers,
'#/api/console': {
async fetchRoles() { return { roles: rows }; }, async fetchPermissions() { return { permissions: catalog }; },
async saveRole(data) { calls.save.push(data); return { roles: rows }; },
saveRole: api.saveRole,
async deleteRole(code) { calls.remove.push(code); return { roles: rows }; },
},
}).default;
@@ -95,8 +110,20 @@ async function page(codes, configure = () => {}) {
return { state, calls, html };
}
test('管理员和集团基础角色实际组件均为只读:名称/权限禁用、查看与关闭、禁止写删', async () => {
for (const row of rows.slice(0, 2)) {
test('知识数据权限按既有action协议保存,取消可撤回且不单独授予审核或正文权限', async () => {
const { state, calls, html } = await page(['role:read', 'role:write'], state => state.openEdit(rows[1]));
assert.match(html, /勾选「仅本人账号的知识」后,仅显示本人 ZYT 账号名下的知识/);
state.onCheck(['knowledge:scope:own']);
await state.submit();
assert.deepEqual([...calls.requests[0].data.permissions].sort(), ['knowledge:read', 'knowledge:scope:own']);
state.openEdit({ ...rows[1], permissions: [...state.form.permissions] });
state.onCheck(['knowledge:read']);
await state.submit();
assert.deepEqual([...calls.requests[1].data.permissions], ['knowledge:read']);
});
test('管理员实际组件仍只读:名称/权限禁用、查看与关闭、禁止写删', async () => {
for (const row of rows.slice(0, 1)) {
const { state, calls, html } = await page(['role:write', 'role:delete'], state => state.openEdit(row));
assert.equal(state.readonly.value, true);
assert.match(html, /data-modal-title="查看角色"/);
@@ -104,7 +131,6 @@ test('管理员和集团基础角色实际组件均为只读:名称/权限禁
assert.match(html, new RegExp(`value="${row.name}" disabled`));
assert.match(html, /data-permission-tree="true"/);
assert.match(html, new RegExp(`data-role="${row.code}"[\\s\\S]*?<button[^>]*>查看<\\/button>[\\s\\S]*?<button[^>]*disabled`));
if (row.code === 'zyt_basic') assert.match(html, /请到「系统管理 → 用户管理」为账号分配其他业务角色/);
const before = [...state.form.permissions];
state.onCheck(['user:read']);
assert.deepEqual([...state.form.permissions], before);
@@ -115,6 +141,57 @@ test('管理员和集团基础角色实际组件均为只读:名称/权限禁
}
});
test('集团基础角色有编辑和保存入口,选定菜单与操作会提交真实 API,空数组可全部撤销', async () => {
const row = rows[1];
const { state, calls, html } = await page(['role:read', 'role:write', 'role:delete'], state => state.openEdit(row));
assert.equal(state.readonly.value, false);
assert.match(html, /data-modal-title="编辑角色"/);
assert.match(html, /data-modal-action(?:="")?>保存/);
assert.match(html, /value="zyt_basic" disabled/);
assert.match(html, /value="集团基础用户"(?! disabled)/);
assert.match(html, /data-permission-tree="false"/);
assert.match(html, /data-role="zyt_basic"[\s\S]*?<button[^>]*>编辑<\/button>[\s\S]*?<button[^>]*disabled/);
assert.match(html, /此角色的权限对所有使用它的账号生效;新集团账号默认使用此角色/);
assert.doesNotMatch(html, /集团基础用户角色只读|固定无业务权限|不能修改。请到/);
assert.deepEqual([...state.form.permissions], [], 'opening the basic role must not grant permissions');
await state.submit();
assert.deepEqual([...calls.requests[0].data.permissions], [], 'saving an unchanged basic role must not grant permissions');
state.openEdit(row);
state.form.name = '集团客服用户';
state.onCheck(['user:write', 'custom:report']);
assert.deepEqual([...state.form.permissions].sort(), ['custom:report', 'user:read', 'user:write']);
await state.submit();
assert.equal(calls.requests[1].path, '/roles');
assert.deepEqual(JSON.parse(JSON.stringify(calls.requests[1].data)), {
code: 'zyt_basic', name: '集团客服用户', permissions: ['user:write', 'user:read', 'custom:report'],
});
state.openEdit({ ...row, permissions: [...state.form.permissions] });
state.onCheck([]);
await state.submit();
assert.equal(calls.requests[2].path, '/roles');
assert.equal(calls.requests[2].data.code, 'zyt_basic');
assert.deepEqual([...calls.requests[2].data.permissions], []);
await state.remove(row);
assert.deepEqual(calls.remove, [], 'reserved basic role cannot be deleted even if builtin metadata is missing');
});
test('内置业务角色仍可编辑但不可删除,在用角色也不可删除', async () => {
for (const row of [{ ...rows[1], builtin: true }, rows[3], rows[4], { ...rows[2], builtin: true }, { ...rows[2], user_count: 1 }]) {
const { state, calls, html } = await page(['role:write', 'role:delete'], state => state.openEdit(row));
assert.equal(state.readonly.value, false);
assert.match(html, /data-modal-title="编辑角色"/);
await state.remove(row);
assert.deepEqual(calls.remove, []);
}
});
test('有角色写权限但没有删除权限的用户仍不能删除普通角色', async () => {
const { state, calls } = await page(['role:read', 'role:write'], state => state.openEdit(rows[2]));
assert.equal(state.readonly.value, false);
await state.remove(rows[2]);
assert.deepEqual(calls.remove, []);
});
test('普通业务角色仍可编辑并保留自定义权限,保存只过滤失效码并说明在线用户自动同步', async () => {
const { state, calls, html } = await page(['role:read', 'role:write', 'role:delete'], state => state.openEdit(rows[2]));
assert.equal(state.readonly.value, false);
@@ -131,10 +208,56 @@ test('普通业务角色仍可编辑并保留自定义权限,保存只过滤
});
test('角色只读用户不能通过组件方法保存或删除业务角色', async () => {
const { state, calls } = await page(['role:read'], state => state.openEdit(rows[2]));
assert.equal(state.readonly.value, true);
await state.submit();
await state.remove(rows[2]);
assert.deepEqual(calls.save, []);
assert.deepEqual(calls.remove, []);
for (const row of [rows[1], rows[2]]) {
const { state, calls, html } = await page(['role:read', 'role:create'], state => state.openEdit(row));
assert.equal(state.readonly.value, true);
assert.match(html, /data-modal-title="查看角色"/);
assert.match(html, /data-modal-action(?:="")?>关闭/);
assert.match(html, /data-permission-tree="true"/);
assert.match(html, new RegExp(`value="${row.name}" disabled`));
const before = [...state.form.permissions];
state.onCheck(['user:write']);
assert.deepEqual([...state.form.permissions], before);
await state.submit();
await state.remove(row);
assert.deepEqual(calls.save, []);
assert.deepEqual(calls.remove, []);
}
});
test('角色编辑、共享授权说明和无权限欢迎提示都有英文映射', async () => {
const permissions = load(await readFile(new URL('../src/locales/en/permissions.ts', import.meta.url), 'utf8'), {}).default;
const users = load(await readFile(new URL('../src/locales/en/users.ts', import.meta.url), 'utf8'), {}).default;
const roleSource = descriptor.template.content;
const description = roleSource.match(/:description="t\('([^']+)'\)"/)[1];
assert.match(permissions[description], /administrator role is read-only/);
assert.match(permissions['此角色的权限对所有使用它的账号生效;新集团账号默认使用此角色。'], /every account using it/);
const welcome = await readFile(new URL('../src/views/group/welcome.vue', import.meta.url), 'utf8');
const noPermissions = welcome.match(/:message="t\('([^']+)'\)"/)[1];
assert.match(users[noPermissions], /configure role permissions or assign another business role/);
});
test('基础角色欢迎页按真实权限显示提示,不再显示可能已更名的固定角色名称', async () => {
const filename = fileURLToPath(new URL('../src/views/group/welcome.vue', import.meta.url));
const { descriptor } = parse(await readFile(filename, 'utf8'), { filename });
const script = compileScript(descriptor, { id: 'basic-role-welcome-test' });
const template = compileTemplate({ source: descriptor.template.content, filename,
id: 'basic-role-welcome-test', compilerOptions: { bindingMetadata: script.bindings } });
assert.deepEqual(template.errors, []);
for (const roles of [[], ['user:read']]) {
const component = load(script.content, {
vue, '#/locales/t': translate, 'ant-design-vue': ant,
'@vben/stores': { useUserStore: () => ({ userInfo: { username: 'fixture', desc: 'zyt_basic', roles } }) },
}).default;
component.render = load(template.code, { vue }).render;
const html = await renderToString(vue.createSSRApp(component));
assert.doesNotMatch(html, /当前角色:集团基础用户/);
if (roles.length) {
assert.match(html, /您的角色已授权,可从左侧菜单进入相应功能/);
assert.doesNotMatch(html, /当前尚未获得业务权限/);
} else {
assert.match(html, /当前尚未获得业务权限。请联系本系统管理员配置角色权限或分配其他业务角色/);
assert.doesNotMatch(html, /您的角色已授权/);
}
}
});