1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 adminHeaders,
4 createAdminUser,
5 } from "../../../../helpers/create-admin-user"
6
7 jest.setTimeout(50000)
8
9 medusaIntegrationTestRunner({
10 env: {},
11 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
12 describe("Publishable Keys - Admin", () => {
13 let pubKey1
14 let pubKey2
15
16 beforeAll(async () => {
17 await createAdminUser(dbConnection, adminHeaders, getContainer())
18
19 // BREAKING: Before the ID of the token was used in request headers, now there is a separate `token` field that should be used
20 pubKey1 = (
21 await api.post(
22 "/admin/api-keys",
23 { title: "sample key", type: "publishable" },
24 adminHeaders
25 )
26 ).data.api_key
27 pubKey2 = (
28 await api.post(
29 "/admin/api-keys",
30 // BREAKING: The type field is now required
31 { title: "just a title", type: "publishable" },
32 adminHeaders
33 )
34 ).data.api_key
35
36 await dbUtils.snapshot()
37 })
38
39 // BREAKING: The URL changed from /admin/publishable-api-keys to /admin/api-keys, as well as the response field
40 describe("GET /admin/api-keys/:id", () => {
41 it("retrieve a publishable key by id ", async () => {
42 const response = await api.get(
43 `/admin/api-keys/${pubKey1.id}`,
44 adminHeaders
45 )
46
47 expect(response.status).toBe(200)
48
49 expect(response.data.api_key).toMatchObject({
50 id: pubKey1.id,
51 created_at: expect.any(String),
52 created_by: expect.stringContaining("user_"),
53 revoked_by: null,
54 revoked_at: null,
55 })
56 })
57 })
58
59 describe("GET /admin/api-keys", () => {
60 it("list publishable keys", async () => {
61 const response = await api.get(
62 `/admin/api-keys?limit=2`,
63 adminHeaders
64 )
65
66 expect(response.data.count).toBe(3) // two created keys and the default publishable api key
67 expect(response.data.limit).toBe(2)
68 expect(response.data.offset).toBe(0)
69 expect(response.data.api_keys).toHaveLength(2)
70 })
71
72 it("list publishable keys with query search", async () => {
73 const response = await api.get(
74 `/admin/api-keys?q=sample`,
75 adminHeaders
76 )
77
78 expect(response.data.count).toBe(1)
79 expect(response.data.limit).toBe(20)
80 expect(response.data.offset).toBe(0)
81 expect(response.data.api_keys).toHaveLength(1)
82 expect(response.data.api_keys).toEqual(
83 expect.arrayContaining([
84 expect.objectContaining({
85 title: "sample key",
86 }),
87 ])
88 )
89 })
90 })
91
92 describe("POST /admin/api-keys", () => {
93 it("crete a publishable keys", async () => {
94 const response = await api.post(
95 `/admin/api-keys`,
96 { title: "Store api key", type: "publishable" },
97 adminHeaders
98 )
99
100 expect(response.status).toBe(200)
101 expect(response.data.api_key).toMatchObject({
102 created_by: expect.any(String),
103 id: expect.any(String),
104 title: "Store api key",
105 revoked_by: null,
106 revoked_at: null,
107 created_at: expect.any(String),
108 })
109 })
110 })
111
112 describe("POST /admin/api-keys/:id", () => {
113 it("update a publishable key", async () => {
114 const response = await api.post(
115 `/admin/api-keys/${pubKey1.id}`,
116 { title: "Changed title" },
117 adminHeaders
118 )
119
120 expect(response.status).toBe(200)
121 expect(response.data.api_key).toMatchObject({
122 id: pubKey1.id,
123 title: "Changed title",
124 revoked_by: null,
125 revoked_at: null,
126 created_at: expect.any(String),
127 updated_at: expect.any(String),
128 })
129 })
130 })
131
132 describe("DELETE /admin/api-keys/:id", () => {
133 it("delete a publishable key", async () => {
134 await api.post(
135 `/admin/api-keys/${pubKey1.id}/revoke`,
136 {},
137 adminHeaders
138 )
139
140 const response1 = await api.delete(
141 `/admin/api-keys/${pubKey1.id}`,
142 adminHeaders
143 )
144
145 expect(response1.status).toBe(200)
146 expect(response1.data).toEqual({
147 id: pubKey1.id,
148 object: "api_key",
149 deleted: true,
150 })
151
152 const err = await api
153 .get(`/admin/api-keys/${pubKey1.id}`, adminHeaders)
154 .catch((e) => e)
155
156 expect(err.response.status).toBe(404)
157 })
158 })
159
160 describe("POST /admin/api-keys/:id/revoke", () => {
161 it("revoke a publishable key", async () => {
162 const response = await api.post(
163 `/admin/api-keys/${pubKey1.id}/revoke`,
164 {},
165 adminHeaders
166 )
167
168 expect(response.status).toBe(200)
169
170 expect(response.data.api_key).toMatchObject({
171 id: pubKey1.id,
172 created_at: expect.any(String),
173 updated_at: expect.any(String),
174 revoked_by: expect.stringContaining("user_"),
175 revoked_at: expect.any(String),
176 })
177 })
178 })
179
180 // BREAKING: The GET /admin/api-keys/:id/sales-channels endpoint was removed.
181 // It was replaced by the GET /admin/sales-channels endpoint where you can filter by publishable key
182 // BREAKING: Batch route and input changed (no more batch suffix, and the input takes ids to add and remove)
183 describe("Add /admin/api-keys/:id/sales-channels", () => {
184 let salesChannel1
185 let salesChannel2
186
187 beforeEach(async () => {
188 salesChannel1 = (
189 await api.post(
190 "/admin/sales-channels",
191 {
192 name: "test name",
193 description: "test description",
194 },
195 adminHeaders
196 )
197 ).data.sales_channel
198
199 salesChannel2 = (
200 await api.post(
201 "/admin/sales-channels",
202 {
203 name: "test name 2",
204 description: "test description 2",
205 },
206 adminHeaders
207 )
208 ).data.sales_channel
209 })
210
211 it("add sales channels to the publishable api key scope", async () => {
212 const response = await api.post(
213 `/admin/api-keys/${pubKey1.id}/sales-channels`,
214 {
215 add: [salesChannel1.id, salesChannel2.id],
216 },
217 adminHeaders
218 )
219
220 expect(response.status).toBe(200)
221 const keyWithChannels = (
222 await api.get(`/admin/api-keys/${pubKey1.id}`, adminHeaders)
223 ).data.api_key
224
225 expect(keyWithChannels.sales_channels).toEqual(
226 expect.arrayContaining([
227 expect.objectContaining({
228 id: salesChannel1.id,
229 }),
230 expect.objectContaining({
231 id: salesChannel2.id,
232 }),
233 ])
234 )
235 })
236
237 it("remove sales channels from the publishable api key scope", async () => {
238 await api.post(
239 `/admin/api-keys/${pubKey1.id}/sales-channels`,
240 {
241 add: [salesChannel1.id, salesChannel2.id],
242 },
243 adminHeaders
244 )
245
246 const response = await api.post(
247 `/admin/api-keys/${pubKey1.id}/sales-channels`,
248 {
249 remove: [salesChannel1.id],
250 },
251 adminHeaders
252 )
253
254 expect(response.status).toBe(200)
255 const keyWithChannels = (
256 await api.get(`/admin/api-keys/${pubKey1.id}`, adminHeaders)
257 ).data.api_key
258
259 expect(keyWithChannels.sales_channels).toEqual([
260 expect.objectContaining({
261 id: salesChannel2.id,
262 }),
263 ])
264 })
265
266 it("list sales channels from the publishable api key", async () => {
267 await api.post(
268 `/admin/api-keys/${pubKey1.id}/sales-channels`,
269 {
270 add: [salesChannel1.id, salesChannel2.id],
271 },
272 adminHeaders
273 )
274
275 const response = await api.get(
276 `/admin/api-keys/${pubKey1.id}`,
277 adminHeaders
278 )
279
280 const salesChannels = response.data.api_key.sales_channels
281 expect(response.status).toBe(200)
282 expect(salesChannels.length).toEqual(2)
283 expect(salesChannels).toEqual(
284 expect.arrayContaining([
285 expect.objectContaining({
286 id: salesChannel1.id,
287 name: "test name",
288 }),
289 expect.objectContaining({
290 id: salesChannel2.id,
291 name: "test name 2",
292 }),
293 ])
294 )
295 })
296 })
297 })
298 },
299 })