|
1
|
import { IRbacModuleService } from "@medusajs/framework/types" |
|
2
|
import { Module, Modules } from "@medusajs/framework/utils" |
|
3
|
import { |
|
4
|
MockEventBusService, |
|
5
|
moduleIntegrationTestRunner, |
|
6
|
} from "@medusajs/test-utils" |
|
7
|
import { RbacModuleService } from "@services" |
|
8
|
|
|
9
|
jest.setTimeout(30000) |
|
10
|
|
|
11
|
moduleIntegrationTestRunner<IRbacModuleService>({ |
|
12
|
moduleName: Modules.RBAC, |
|
13
|
debug: true, |
|
14
|
moduleOptions: {}, |
|
15
|
injectedDependencies: { |
|
16
|
[Modules.EVENT_BUS]: new MockEventBusService(), |
|
17
|
}, |
|
18
|
testSuite: ({ service }) => { |
|
19
|
it.skip(`should export the appropriate linkable configuration`, () => { |
|
20
|
const linkable = Module(Modules.RBAC, { |
|
21
|
service: RbacModuleService, |
|
22
|
}).linkable |
|
23
|
|
|
24
|
expect(Object.keys(linkable)).toEqual([ |
|
25
|
"rbacRole", |
|
26
|
"rbacPolicy", |
|
27
|
"rbacRoleParent", |
|
28
|
"rbacRolePolicy", |
|
29
|
]) |
|
30
|
|
|
31
|
Object.keys(linkable).forEach((key) => { |
|
32
|
delete linkable[key].toJSON |
|
33
|
}) |
|
34
|
|
|
35
|
expect(linkable).toEqual({ |
|
36
|
rbacRole: { |
|
37
|
id: { |
|
38
|
linkable: "rbac_role_id", |
|
39
|
entity: "RbacRole", |
|
40
|
primaryKey: "id", |
|
41
|
serviceName: "rbac", |
|
42
|
field: "rbacRole", |
|
43
|
}, |
|
44
|
}, |
|
45
|
rbacPolicy: { |
|
46
|
id: { |
|
47
|
linkable: "rbac_policy_id", |
|
48
|
entity: "RbacPolicy", |
|
49
|
primaryKey: "id", |
|
50
|
serviceName: "rbac", |
|
51
|
field: "rbacPolicy", |
|
52
|
}, |
|
53
|
}, |
|
54
|
rbacRoleParent: { |
|
55
|
id: { |
|
56
|
linkable: "rbac_role_parent_id", |
|
57
|
entity: "RbacRoleParent", |
|
58
|
primaryKey: "id", |
|
59
|
serviceName: "rbac", |
|
60
|
field: "rbacRoleParent", |
|
61
|
}, |
|
62
|
}, |
|
63
|
rbacRolePolicy: { |
|
64
|
id: { |
|
65
|
linkable: "rbac_role_policy_id", |
|
66
|
entity: "RbacRolePolicy", |
|
67
|
primaryKey: "id", |
|
68
|
serviceName: "rbac", |
|
69
|
field: "rbacRolePolicy", |
|
70
|
}, |
|
71
|
}, |
|
72
|
}) |
|
73
|
}) |
|
74
|
|
|
75
|
describe("RBAC Module Service", () => { |
|
76
|
it("should create and manage roles and policies", async () => { |
|
77
|
// Create a custom policy |
|
78
|
const customPolicy = await service.createRbacPolicies({ |
|
79
|
key: "read:product", |
|
80
|
resource: "product", |
|
81
|
operation: "read", |
|
82
|
name: "Read Products", |
|
83
|
description: "Permission to read products", |
|
84
|
}) |
|
85
|
|
|
86
|
expect(customPolicy.id).toBeDefined() |
|
87
|
expect(customPolicy.key).toBe("read:product") |
|
88
|
expect(customPolicy.resource).toBe("product") |
|
89
|
expect(customPolicy.operation).toBe("read") |
|
90
|
|
|
91
|
// Create a custom role |
|
92
|
const customRole = await service.createRbacRoles({ |
|
93
|
name: "Product Viewer", |
|
94
|
description: "Can view products", |
|
95
|
}) |
|
96
|
|
|
97
|
expect(customRole.id).toBeDefined() |
|
98
|
expect(customRole.name).toBe("Product Viewer") |
|
99
|
|
|
100
|
// Link policy to role |
|
101
|
const rolePolicyLink = await service.createRbacRolePolicies({ |
|
102
|
role_id: customRole.id, |
|
103
|
policy_id: customPolicy.id, |
|
104
|
}) |
|
105
|
|
|
106
|
expect(rolePolicyLink.id).toBeDefined() |
|
107
|
expect(rolePolicyLink.role_id).toBe(customRole.id) |
|
108
|
expect(rolePolicyLink.policy_id).toBe(customPolicy.id) |
|
109
|
|
|
110
|
// Verify the link |
|
111
|
const roleWithPolicies = await service.listRbacRoles( |
|
112
|
{ |
|
113
|
id: customRole.id, |
|
114
|
}, |
|
115
|
{ relations: ["policies"] } |
|
116
|
) |
|
117
|
|
|
118
|
expect(roleWithPolicies).toHaveLength(1) |
|
119
|
expect(roleWithPolicies[0].policies).toHaveLength(1) |
|
120
|
expect(roleWithPolicies[0].policies![0].key).toBe("read:product") |
|
121
|
}) |
|
122
|
|
|
123
|
it("should list policies for role", async () => { |
|
124
|
// Create role and policy |
|
125
|
const role = await service.createRbacRoles({ |
|
126
|
name: "Test Role", |
|
127
|
description: "Test role for listing policies", |
|
128
|
}) |
|
129
|
|
|
130
|
const policy = await service.createRbacPolicies({ |
|
131
|
key: "test:action", |
|
132
|
resource: "test", |
|
133
|
operation: "test", |
|
134
|
name: "Test Policy", |
|
135
|
description: "Test policy", |
|
136
|
}) |
|
137
|
|
|
138
|
// Link them |
|
139
|
await service.createRbacRolePolicies({ |
|
140
|
role_id: role.id, |
|
141
|
policy_id: policy.id, |
|
142
|
}) |
|
143
|
|
|
144
|
// List policies for role |
|
145
|
const policies = await service.listPoliciesForRole(role.id) |
|
146
|
|
|
147
|
expect(policies).toHaveLength(1) |
|
148
|
expect(policies[0].key).toBe("test:action") |
|
149
|
expect(policies[0].resource).toBe("test") |
|
150
|
expect(policies[0].operation).toBe("test") |
|
151
|
}) |
|
152
|
|
|
153
|
it("should handle soft delete and restore", async () => { |
|
154
|
// Create a role |
|
155
|
const role = await service.createRbacRoles({ |
|
156
|
name: "Test Role", |
|
157
|
description: "Test role for soft delete", |
|
158
|
}) |
|
159
|
|
|
160
|
// Soft delete the role |
|
161
|
await service.softDeleteRbacRoles(role.id) |
|
162
|
|
|
163
|
// Verify it's soft deleted |
|
164
|
const deletedRoles = await service.listRbacRoles( |
|
165
|
{ |
|
166
|
id: role.id, |
|
167
|
}, |
|
168
|
{ withDeleted: true } |
|
169
|
) |
|
170
|
|
|
171
|
expect(deletedRoles).toHaveLength(1) |
|
172
|
expect(deletedRoles[0].deleted_at).toBeDefined() |
|
173
|
|
|
174
|
// Try to list without deleted - should not appear |
|
175
|
const activeRoles = await service.listRbacRoles({ |
|
176
|
id: role.id, |
|
177
|
}) |
|
178
|
|
|
179
|
expect(activeRoles).toHaveLength(0) |
|
180
|
|
|
181
|
// Restore the role |
|
182
|
await service.restoreRbacRoles(role.id) |
|
183
|
|
|
184
|
// Verify it's restored |
|
185
|
const restoredRoles = await service.listRbacRoles({ |
|
186
|
id: role.id, |
|
187
|
}) |
|
188
|
|
|
189
|
expect(restoredRoles).toHaveLength(1) |
|
190
|
expect(restoredRoles[0].deleted_at).toBeNull() |
|
191
|
}) |
|
192
|
|
|
193
|
it("should prevent duplicate creation", async () => { |
|
194
|
// Create a role |
|
195
|
const role = await service.createRbacRoles({ |
|
196
|
name: "Test Role", |
|
197
|
description: "Test role for duplicate prevention", |
|
198
|
}) |
|
199
|
|
|
200
|
// Try to create another role with the same ID - should fail |
|
201
|
await expect( |
|
202
|
service.createRbacRoles({ |
|
203
|
id: role.id, |
|
204
|
name: "Duplicate Role", |
|
205
|
description: "This should not be created", |
|
206
|
}) |
|
207
|
).rejects.toThrow() |
|
208
|
|
|
209
|
// Still only one role should exist |
|
210
|
const roles = await service.listRbacRoles({ |
|
211
|
id: role.id, |
|
212
|
}) |
|
213
|
|
|
214
|
expect(roles).toHaveLength(1) |
|
215
|
expect(roles[0].name).toBe("Test Role") // Original name preserved |
|
216
|
}) |
|
217
|
|
|
218
|
it("should verify super admin role structure", async () => { |
|
219
|
// Check if the super admin role was created |
|
220
|
const existingSuperAdminRoles = await service.listRbacRoles() |
|
221
|
|
|
222
|
expect(existingSuperAdminRoles).toHaveLength(1) |
|
223
|
expect(existingSuperAdminRoles[0].name).toBe("Super Admin") |
|
224
|
expect(existingSuperAdminRoles[0].description).toBe( |
|
225
|
"Super admin role with full access to all resources and operations" |
|
226
|
) |
|
227
|
|
|
228
|
// Check if the wildcard policy was created |
|
229
|
const existingWildcardPolicies = await service.listRbacPolicies() |
|
230
|
|
|
231
|
expect(existingWildcardPolicies).toHaveLength(1) |
|
232
|
expect(existingWildcardPolicies[0].key).toBe("*:*") |
|
233
|
expect(existingWildcardPolicies[0].resource).toBe("*") |
|
234
|
expect(existingWildcardPolicies[0].operation).toBe("*") |
|
235
|
expect(existingWildcardPolicies[0].name).toBe("Super Admin") |
|
236
|
expect(existingWildcardPolicies[0].description).toBe( |
|
237
|
"Super admin policy with full access to all resources and operations" |
|
238
|
) |
|
239
|
|
|
240
|
// Check if the role-policy link was created |
|
241
|
const existingRolePolicyLinks = await service.listRbacRolePolicies() |
|
242
|
|
|
243
|
expect(existingRolePolicyLinks).toHaveLength(1) |
|
244
|
expect(existingRolePolicyLinks[0]).toEqual( |
|
245
|
expect.objectContaining({ |
|
246
|
role_id: expect.any(String), |
|
247
|
policy_id: expect.any(String), |
|
248
|
}) |
|
249
|
) |
|
250
|
|
|
251
|
// Verify the role has the wildcard policy |
|
252
|
const roleWithPolicies = await service.listRbacRoles( |
|
253
|
{ |
|
254
|
id: "role_super_admin", |
|
255
|
}, |
|
256
|
{ relations: ["policies"] } |
|
257
|
) |
|
258
|
|
|
259
|
expect(roleWithPolicies).toHaveLength(1) |
|
260
|
expect(roleWithPolicies[0].policies).toHaveLength(1) |
|
261
|
expect(roleWithPolicies[0].policies![0].resource).toBe("*") |
|
262
|
expect(roleWithPolicies[0].policies![0].operation).toBe("*") |
|
263
|
expect(roleWithPolicies[0].policies![0].key).toBe("*:*") |
|
264
|
}) |
|
265
|
|
|
266
|
it("should upsert roles correctly", async () => { |
|
267
|
// Create a new role |
|
268
|
const newRole = await service.createRbacRoles([ |
|
269
|
{ |
|
270
|
name: "Test Role", |
|
271
|
description: "Test role description", |
|
272
|
}, |
|
273
|
]) |
|
274
|
|
|
275
|
expect(newRole[0].id).toBeDefined() |
|
276
|
expect(newRole[0].name).toBe("Test Role") |
|
277
|
expect(newRole[0].description).toBe("Test role description") |
|
278
|
|
|
279
|
// Update the same role |
|
280
|
const updatedRoles = await service.updateRbacRoles([ |
|
281
|
{ |
|
282
|
id: newRole[0].id, |
|
283
|
name: "Updated Test Role", |
|
284
|
description: "Updated test role description", |
|
285
|
}, |
|
286
|
]) |
|
287
|
|
|
288
|
expect(updatedRoles[0].id).toBe(newRole[0].id) |
|
289
|
expect(updatedRoles[0].name).toBe("Updated Test Role") |
|
290
|
expect(updatedRoles[0].description).toBe( |
|
291
|
"Updated test role description" |
|
292
|
) |
|
293
|
|
|
294
|
// Verify only one role exists |
|
295
|
const roles = await service.listRbacRoles({ |
|
296
|
id: newRole[0].id, |
|
297
|
}) |
|
298
|
|
|
299
|
expect(roles).toHaveLength(1) |
|
300
|
expect(roles[0].name).toBe("Updated Test Role") |
|
301
|
}) |
|
302
|
}) |
|
303
|
}, |
|
304
|
}) |