forked from Imfdj/egg-beehive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
role_permissions.test.js
89 lines (84 loc) · 2.97 KB
/
role_permissions.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict';
const { assert, app } = require('egg-mock/bootstrap');
describe('test/app/controller/role_permissions.test.js', () => {
let permissionResRows;
let roleResRows;
let createUserRoleTargetIds = [];
before(async () => {});
describe('POST /api/v1/role_permissions/bulk_permission 创建 单角色-多资源关系', () => {
it('should work', async () => {
// 分别获取已有用户和角色
app.mockCsrf();
app.mockCookies({ EGG_SESS: app.__cookies });
const permissionRes = await app.httpRequest()
.get('/api/v1/permissions/list')
.query({ limit: 2 })
.set('authorization', app.__authorization);
assert(permissionRes.status === 200);
assert(permissionRes.body.data);
permissionResRows = permissionRes.body.data.rows;
const roleRes = await app.httpRequest()
.get('/api/v1/roles/list')
.query({ limit: 2 })
.set('authorization', app.__authorization);
assert(roleRes.status === 200);
assert(roleRes.body.data);
roleResRows = roleRes.body.data.rows;
const res = await app
.httpRequest()
.post('/api/v1/role_permissions/bulk_permission')
.set('authorization', app.__authorization)
.send({
role_id: roleResRows[0].id,
permission_ids: [permissionResRows[0].id, permissionResRows[1].id],
});
assert(res.status === 201);
assert(res.body.code === 0);
});
});
describe('GET /api/v1/role_permissions/list', () => {
it('should work', async () => {
app.mockCsrf();
app.mockCookies({ EGG_SESS: app.__cookies });
const res = await app
.httpRequest()
.get('/api/v1/role_permissions/list')
.query({
limit: 1000,
permission_id: permissionResRows[0].id,
role_id: roleResRows[0].id,
})
.set('authorization', app.__authorization);
assert(res.status === 200);
assert(res.body.data);
assert(res.body.code === 0);
const res2 = await app
.httpRequest()
.get('/api/v1/role_permissions/list')
.query({
limit: 1000,
permission_id: permissionResRows[1].id,
role_id: roleResRows[0].id,
})
.set('authorization', app.__authorization);
assert(res2.status === 200);
assert(res2.body.data);
assert(res2.body.code === 0);
// 获取测试时添加的id用于删除
createUserRoleTargetIds = [res.body.data.rows[res.body.data.rows.length - 1].id, res2.body.data.rows[res2.body.data.rows.length - 1].id];
});
});
describe('DELETE /api/v1/role_permissions', () => {
it('should work', async () => {
app.mockCsrf();
app.mockCookies({ EGG_SESS: app.__cookies });
const res = await app.httpRequest()
.delete('/api/v1/role_permissions')
.set('authorization', app.__authorization)
.send({
ids: createUserRoleTargetIds,
});
assert(res.status === 204);
});
});
});