import {api} from "./restful"; import {Resp} from "@util/Resp"; import {models} from "@core-models/Models"; import {AcsModule} from "@core-models/AcsModule"; import {AcsRole} from "@core-models/AcsRole"; import {AcsFunction} from "@core-models/AcsFunction"; import {AcsUserRole} from "@core-models/AcsUserRole"; const globalAny: any = global; let sqls = [ `insert into ${AcsModule.table_name} (id, tag, name, pid, path, path_name) values (200000, 'test1', '一级', null, '200000', 'test1'), (200001, 'test2', '一级', 200000, '200000.200001', 'test1.test2') ON CONFLICT DO NOTHING`, `insert into ${AcsFunction.table_name} (id, tag, name, module_id, api, check_auth) values (200000, 'function1', '功能项', 200001, 'test1.test2.function1', true), (200001, 'function2', '功能项', 200001, 'test1.test2.function2', true) ON CONFLICT DO NOTHING` ] export const acs_role_test = () => { beforeAll(async (done) => { globalAny.user_id = 'admin'; await globalAny.svr.init_data_models(models); let sequelize = AcsRole.sequelize!; for (let i = 0; i < sqls.length; i++) { await sequelize.query(sqls[i]); } done(); }, 30000); test('添加角色', async () => { let result = await api('/api/acs.role.add', { role_id: 'role_id', role_name: 'role_name' }); expect(result.code).toBe(Resp.OK.code); }); test('获取角色列表', async () => { let result = await api('/api/acs.role.get_list', { }); expect(result.code).toBe(Resp.OK.code); expect(result.data).toBeDefined(); expect(result.data.roles).toBeDefined(); expect(result.data.roles.length).toBe(2); expect(result.data.roles).toContainEqual({id: 'role_id', name: 'role_name'}) }); test('修改角色', async () => { let result = await api('/api/acs.role.modify', { role_id: 'role_id', role_name: 'role_name_new' }); expect(result.code).toBe(Resp.OK.code); }); test('获取角色列表2', async () => { let result = await api('/api/acs.role.get_list', { }); expect(result.code).toBe(Resp.OK.code); expect(result.data).toBeDefined(); expect(result.data.roles).toBeDefined(); expect(result.data.roles).toContainEqual({id: 'role_id', name: 'role_name_new'}) }); test('获取角色列表2', async () => { let result = await api('/api/acs.role.get_list', { keyword: 'role_name' }); expect(result.code).toBe(Resp.OK.code); expect(result.data).toBeDefined(); expect(result.data.roles).toBeDefined(); expect(result.data.roles.length).toBe(1); expect(result.data.roles).toContainEqual({id: 'role_id', name: 'role_name_new'}) }); test('获取角色对应的功能项', async () => { // await AcsUserRole.sequelize!.query(`insert into ${AcsUserRole.table_name} values ('admin', 'role_id')`); let result = await api('/api/acs.role.get_funcs', { role_id: 'role_id', module_id: 6 }); expect(result.code).toBe(Resp.OK.code); expect(result.data).toBeDefined(); expect(result.data.funcs).toBeDefined(); }); test('设置角色对应的功能项', async () => { let result = await api('/api/acs.role.set_funcs', { role_id: 'role_id', funcs: [ { id: 35, enabled: true }, { id: 36, enabled: true } ] }); expect(result.code).toBe(Resp.OK.code); }); test('获取角色对应的功能项', async () => { let result = await api('/api/acs.role.get_funcs', { role_id: 'role_id', module_id: 6 }); expect(result.code).toBe(Resp.OK.code); expect(result.data).toBeDefined(); expect(result.data.funcs).toBeDefined(); expect(result.data.funcs).toContainEqual( { api: 'acs.role.get_funcs', name: '获取角色对应的功能项', id: 35, enabled: true } ); expect(result.data.funcs).toContainEqual( { api: 'acs.role.set_funcs', name: '设置角色对应的功能项', id: 36, enabled: true } ); }); test('设置角色对应的功能项', async () => { let result = await api('/api/acs.role.set_funcs', { role_id: 'role_id', funcs: [ { id: 35, enabled: true }, { id: 36, enabled: false } ] }); expect(result.code).toBe(Resp.OK.code); }); test('获取角色对应的功能项', async () => { let result = await api('/api/acs.role.get_funcs', { role_id: 'role_id', module_id: 6 }); expect(result.code).toBe(Resp.OK.code); expect(result.data).toBeDefined(); expect(result.data.funcs).toBeDefined(); expect(result.data.funcs).toContainEqual( { api: 'acs.role.get_funcs', name: '获取角色对应的功能项', id: 35, enabled: true } ); expect(result.data.funcs).toContainEqual( { api: 'acs.role.set_funcs', name: '设置角色对应的功能项', id: 36, enabled: false } ); }); test('删除角色', async () => { let result = await api('/api/acs.role.remove', { role_id: 'role_id' }); expect(result.code).toBe(Resp.OK.code); }); test('获取角色列表', async () => { let result = await api('/api/acs.role.get_list', { }); expect(result.code).toBe(Resp.OK.code); expect(result.data).toBeDefined(); expect(result.data.roles).toBeDefined(); expect(result.data.roles.length).toBe(1); }); }