acs_role.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import {api} from "./restful";
  2. import {Resp} from "@util/Resp";
  3. import {models} from "@core-models/Models";
  4. import {AcsModule} from "@core-models/AcsModule";
  5. import {AcsRole} from "@core-models/AcsRole";
  6. import {AcsFunction} from "@core-models/AcsFunction";
  7. import {AcsUserRole} from "@core-models/AcsUserRole";
  8. const globalAny: any = global;
  9. let sqls = [
  10. `insert into ${AcsModule.table_name} (id, tag, name, pid, path, path_name)
  11. values (200000, 'test1', '一级', null, '200000', 'test1'),
  12. (200001, 'test2', '一级', 200000, '200000.200001', 'test1.test2') ON CONFLICT DO NOTHING`,
  13. `insert into ${AcsFunction.table_name} (id, tag, name, module_id, api, check_auth)
  14. values (200000, 'function1', '功能项', 200001, 'test1.test2.function1', true),
  15. (200001, 'function2', '功能项', 200001, 'test1.test2.function2', true) ON CONFLICT DO NOTHING`
  16. ]
  17. export const acs_role_test = () => {
  18. beforeAll(async (done) => {
  19. globalAny.user_id = 'admin';
  20. await globalAny.svr.init_data_models(models);
  21. let sequelize = AcsRole.sequelize!;
  22. for (let i = 0; i < sqls.length; i++) {
  23. await sequelize.query(sqls[i]);
  24. }
  25. done();
  26. }, 30000);
  27. test('添加角色', async () => {
  28. let result = await api('/api/acs.role.add', {
  29. role_id: 'role_id',
  30. role_name: 'role_name'
  31. });
  32. expect(result.code).toBe(Resp.OK.code);
  33. });
  34. test('获取角色列表', async () => {
  35. let result = await api('/api/acs.role.get_list', {
  36. });
  37. expect(result.code).toBe(Resp.OK.code);
  38. expect(result.data).toBeDefined();
  39. expect(result.data.roles).toBeDefined();
  40. expect(result.data.roles.length).toBe(2);
  41. expect(result.data.roles).toContainEqual({id: 'role_id', name: 'role_name'})
  42. });
  43. test('修改角色', async () => {
  44. let result = await api('/api/acs.role.modify', {
  45. role_id: 'role_id',
  46. role_name: 'role_name_new'
  47. });
  48. expect(result.code).toBe(Resp.OK.code);
  49. });
  50. test('获取角色列表2', async () => {
  51. let result = await api('/api/acs.role.get_list', {
  52. });
  53. expect(result.code).toBe(Resp.OK.code);
  54. expect(result.data).toBeDefined();
  55. expect(result.data.roles).toBeDefined();
  56. expect(result.data.roles).toContainEqual({id: 'role_id', name: 'role_name_new'})
  57. });
  58. test('获取角色列表2', async () => {
  59. let result = await api('/api/acs.role.get_list', {
  60. keyword: 'role_name'
  61. });
  62. expect(result.code).toBe(Resp.OK.code);
  63. expect(result.data).toBeDefined();
  64. expect(result.data.roles).toBeDefined();
  65. expect(result.data.roles.length).toBe(1);
  66. expect(result.data.roles).toContainEqual({id: 'role_id', name: 'role_name_new'})
  67. });
  68. test('获取角色对应的功能项', async () => {
  69. // await AcsUserRole.sequelize!.query(`insert into ${AcsUserRole.table_name} values ('admin', 'role_id')`);
  70. let result = await api('/api/acs.role.get_funcs', {
  71. role_id: 'role_id',
  72. module_id: 6
  73. });
  74. expect(result.code).toBe(Resp.OK.code);
  75. expect(result.data).toBeDefined();
  76. expect(result.data.funcs).toBeDefined();
  77. });
  78. test('设置角色对应的功能项', async () => {
  79. let result = await api('/api/acs.role.set_funcs', {
  80. role_id: 'role_id',
  81. funcs: [
  82. {
  83. id: 35, enabled: true
  84. },
  85. {
  86. id: 36, enabled: true
  87. }
  88. ]
  89. });
  90. expect(result.code).toBe(Resp.OK.code);
  91. });
  92. test('获取角色对应的功能项', async () => {
  93. let result = await api('/api/acs.role.get_funcs', {
  94. role_id: 'role_id',
  95. module_id: 6
  96. });
  97. expect(result.code).toBe(Resp.OK.code);
  98. expect(result.data).toBeDefined();
  99. expect(result.data.funcs).toBeDefined();
  100. expect(result.data.funcs).toContainEqual(
  101. {
  102. api: 'acs.role.get_funcs', name: '获取角色对应的功能项', id: 35, enabled: true
  103. }
  104. );
  105. expect(result.data.funcs).toContainEqual(
  106. {
  107. api: 'acs.role.set_funcs', name: '设置角色对应的功能项', id: 36, enabled: true
  108. }
  109. );
  110. });
  111. test('设置角色对应的功能项', async () => {
  112. let result = await api('/api/acs.role.set_funcs', {
  113. role_id: 'role_id',
  114. funcs: [
  115. {
  116. id: 35, enabled: true
  117. },
  118. {
  119. id: 36, enabled: false
  120. }
  121. ]
  122. });
  123. expect(result.code).toBe(Resp.OK.code);
  124. });
  125. test('获取角色对应的功能项', async () => {
  126. let result = await api('/api/acs.role.get_funcs', {
  127. role_id: 'role_id',
  128. module_id: 6
  129. });
  130. expect(result.code).toBe(Resp.OK.code);
  131. expect(result.data).toBeDefined();
  132. expect(result.data.funcs).toBeDefined();
  133. expect(result.data.funcs).toContainEqual(
  134. {
  135. api: 'acs.role.get_funcs', name: '获取角色对应的功能项', id: 35, enabled: true
  136. }
  137. );
  138. expect(result.data.funcs).toContainEqual(
  139. {
  140. api: 'acs.role.set_funcs', name: '设置角色对应的功能项', id: 36, enabled: false
  141. }
  142. );
  143. });
  144. test('删除角色', async () => {
  145. let result = await api('/api/acs.role.remove', {
  146. role_id: 'role_id'
  147. });
  148. expect(result.code).toBe(Resp.OK.code);
  149. });
  150. test('获取角色列表', async () => {
  151. let result = await api('/api/acs.role.get_list', {
  152. });
  153. expect(result.code).toBe(Resp.OK.code);
  154. expect(result.data).toBeDefined();
  155. expect(result.data.roles).toBeDefined();
  156. expect(result.data.roles.length).toBe(1);
  157. });
  158. }