1 import { Modules } from "@medusajs/framework/utils"
2 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
3 import {
4 adminHeaders,
5 createAdminUser,
6 } from "../../../../helpers/create-admin-user"
7
8 jest.setTimeout(60000)
9
10 process.env.MEDUSA_FF_RBAC = "true"
11
12 medusaIntegrationTestRunner({
13 testSuite: ({ dbConnection, api, getContainer, dbUtils }) => {
14 let container
15
16 // Headers for users holding different scoped roles.
17 const productManagerHeaders = { headers: { ...adminHeaders.headers } }
18 const universalReaderHeaders = { headers: { ...adminHeaders.headers } }
19 const productReaderHeaders = { headers: { ...adminHeaders.headers } }
20 const noRolesUserHeaders = { headers: { ...adminHeaders.headers } }
21
22 // Policy IDs created in setup, used to compose roles below.
23 let productReadId: string
24 let customerCreateId: string
25
26 beforeAll(async () => {
27 container = getContainer()
28 await createAdminUser(dbConnection, adminHeaders, container)
29
30 const rbacModule = container.resolve(Modules.RBAC)
31
32 // Look up an existing policy by key, or create it if absent.
33 const ensurePolicy = async (params: {
34 key: string
35 resource: string
36 operation: string
37 name: string
38 }) => {
39 const [existing] = await rbacModule.listRbacPolicies({
40 key: params.key,
41 })
42 if (existing) {
43 return existing
44 }
45 const [created] = await rbacModule.createRbacPolicies([params])
46 return created
47 }
48
49 // Concrete policies — these form the "universe" the endpoint expands
50 // wildcard grants against.
51 const productRead = await ensurePolicy({
52 key: "product:read",
53 resource: "product",
54 operation: "read",
55 name: "Read Products",
56 })
57 await ensurePolicy({
58 key: "product:create",
59 resource: "product",
60 operation: "create",
61 name: "Create Products",
62 })
63 await ensurePolicy({
64 key: "product:update",
65 resource: "product",
66 operation: "update",
67 name: "Update Products",
68 })
69 await ensurePolicy({
70 key: "customer:read",
71 resource: "customer",
72 operation: "read",
73 name: "Read Customers",
74 })
75 const customerCreate = await ensurePolicy({
76 key: "customer:create",
77 resource: "customer",
78 operation: "create",
79 name: "Create Customers",
80 })
81
82 productReadId = productRead.id
83 customerCreateId = customerCreate.id
84
85 // Wildcard grants attached to scoped roles below.
86 const productWildcard = await ensurePolicy({
87 key: "product:*",
88 resource: "product",
89 operation: "*",
90 name: "Manage Products",
91 })
92 const universalRead = await ensurePolicy({
93 key: "*:read",
94 resource: "*",
95 operation: "read",
96 name: "Read Anything",
97 })
98
99 const productManagerRole = await rbacModule.createRbacRoles({
100 name: "Product Manager",
101 })
102 const universalReaderRole = await rbacModule.createRbacRoles({
103 name: "Universal Reader",
104 })
105 const productReaderRole = await rbacModule.createRbacRoles({
106 name: "Product Reader",
107 })
108
109 await rbacModule.createRbacRolePolicies([
110 { role_id: productManagerRole.id, policy_id: productWildcard.id },
111 { role_id: universalReaderRole.id, policy_id: universalRead.id },
112 { role_id: productReaderRole.id, policy_id: productReadId },
113 ])
114
115 await createAdminUser(dbConnection, productManagerHeaders, container, {
116 email: "product-manager@medusa.js",
117 roles: [productManagerRole.id],
118 })
119 await createAdminUser(dbConnection, universalReaderHeaders, container, {
120 email: "universal-reader@medusa.js",
121 roles: [universalReaderRole.id],
122 })
123 await createAdminUser(dbConnection, productReaderHeaders, container, {
124 email: "product-reader@medusa.js",
125 roles: [productReaderRole.id],
126 })
127 // User with no roles linked at all — `createAdminUser` honors an empty
128 // `roles` array by skipping the rbac link step.
129 await createAdminUser(dbConnection, noRolesUserHeaders, container, {
130 email: "no-roles@medusa.js",
131 roles: [],
132 })
133
134 await dbUtils.snapshot()
135 })
136
137 afterAll(async () => {
138 delete process.env.MEDUSA_FF_RBAC
139 })
140
141 describe("GET /admin/rbac/me/permissions", () => {
142 it("returns the full universe for a super-admin (`*:*`)", async () => {
143 const response = await api.get(
144 "/admin/rbac/me/permissions",
145 adminHeaders
146 )
147
148 expect(response.status).toEqual(200)
149 // Super-admin should receive every concrete permission seeded above.
150 expect(response.data.permissions).toEqual(
151 expect.arrayContaining([
152 "product:read",
153 "product:create",
154 "product:update",
155 "customer:read",
156 "customer:create",
157 ])
158 )
159 })
160
161 it("does not include wildcard-only entries in the response", async () => {
162 const response = await api.get(
163 "/admin/rbac/me/permissions",
164 adminHeaders
165 )
166
167 // `*:*`, `*:read`, `product:*` are grants — the response only carries
168 // concrete `resource:operation` entries.
169 for (const entry of response.data.permissions) {
170 expect(entry).not.toContain("*")
171 }
172 })
173
174 it("returns sorted output for deterministic ordering", async () => {
175 const response = await api.get(
176 "/admin/rbac/me/permissions",
177 adminHeaders
178 )
179
180 const sorted = [...response.data.permissions].sort()
181 expect(response.data.permissions).toEqual(sorted)
182 })
183
184 it("expands `resource:*` to all ops on that resource only", async () => {
185 const response = await api.get(
186 "/admin/rbac/me/permissions",
187 productManagerHeaders
188 )
189
190 expect(response.status).toEqual(200)
191 // product:* should cover every product:op in the universe.
192 expect(response.data.permissions).toEqual(
193 expect.arrayContaining([
194 "product:read",
195 "product:create",
196 "product:update",
197 ])
198 )
199 // ...but never reach into a different resource.
200 expect(response.data.permissions).not.toContain("customer:read")
201 expect(response.data.permissions).not.toContain("customer:create")
202 })
203
204 it("expands `*:op` to that op on every resource in the universe", async () => {
205 const response = await api.get(
206 "/admin/rbac/me/permissions",
207 universalReaderHeaders
208 )
209
210 expect(response.status).toEqual(200)
211 // *:read should grant read on each resource that has a read entry.
212 expect(response.data.permissions).toEqual(
213 expect.arrayContaining(["product:read", "customer:read"])
214 )
215 // ...and nothing else.
216 expect(response.data.permissions).not.toContain("product:create")
217 expect(response.data.permissions).not.toContain("customer:create")
218 })
219
220 it("returns only the literal grant for an exact-match holder", async () => {
221 const response = await api.get(
222 "/admin/rbac/me/permissions",
223 productReaderHeaders
224 )
225
226 expect(response.status).toEqual(200)
227 expect(response.data.permissions).toEqual(["product:read"])
228 })
229
230 it("returns an empty array when the actor has no roles", async () => {
231 const response = await api.get(
232 "/admin/rbac/me/permissions",
233 noRolesUserHeaders
234 )
235
236 expect(response.status).toEqual(200)
237 expect(response.data.permissions).toEqual([])
238 })
239
240 it("includes resources persisted at runtime in the universe", async () => {
241 // Simulate a plugin- or admin-created policy for a resource not known
242 // at build time. A super-admin (`*:*`) should pick it up because the
243 // route unions the global `Policy` registry with rbac_policy rows.
244 const rbacModule = container.resolve(Modules.RBAC)
245 const [existing] = await rbacModule.listRbacPolicies({
246 key: "shipment:dispatch",
247 })
248 if (!existing) {
249 await rbacModule.createRbacPolicies([
250 {
251 key: "shipment:dispatch",
252 resource: "shipment",
253 operation: "dispatch",
254 name: "Dispatch Shipments",
255 },
256 ])
257 }
258
259 const response = await api.get(
260 "/admin/rbac/me/permissions",
261 adminHeaders
262 )
263
264 expect(response.data.permissions).toContain("shipment:dispatch")
265 })
266
267 it("aggregates grants when an actor holds multiple roles", async () => {
268 const rbacModule = container.resolve(Modules.RBAC)
269
270 // Two scoped roles: one grants product:read, the other customer:create.
271 // The aggregation rule under test is that an actor holding both ends
272 // up with the union of their grants in the response.
273 const productReaderOnly = await rbacModule.createRbacRoles({
274 name: "Product Reader Only",
275 })
276 const customerCreatorOnly = await rbacModule.createRbacRoles({
277 name: "Customer Creator Only",
278 })
279 await rbacModule.createRbacRolePolicies([
280 { role_id: productReaderOnly.id, policy_id: productReadId },
281 { role_id: customerCreatorOnly.id, policy_id: customerCreateId },
282 ])
283
284 const multiRoleHeaders = { headers: { ...adminHeaders.headers } }
285 await createAdminUser(dbConnection, multiRoleHeaders, container, {
286 email: "multi-role@medusa.js",
287 roles: [productReaderOnly.id, customerCreatorOnly.id],
288 })
289
290 const response = await api.get(
291 "/admin/rbac/me/permissions",
292 multiRoleHeaders
293 )
294
295 expect(response.status).toEqual(200)
296 expect(response.data.permissions).toEqual(
297 expect.arrayContaining(["product:read", "customer:create"])
298 )
299 // No grants beyond what the two roles provide.
300 expect(response.data.permissions).not.toContain("product:create")
301 expect(response.data.permissions).not.toContain("customer:read")
302 })
303 })
304 },
305 })