1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 adminHeaders,
4 createAdminUser,
5 } from "../../../../helpers/create-admin-user"
6 import { Modules } from "@medusajs/framework/utils"
7
8 jest.setTimeout(60000)
9
10 medusaIntegrationTestRunner({
11 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
12 let salesChannel1
13 let salesChannel2
14 let container
15
16 beforeAll(async () => {
17 container = getContainer()
18 await createAdminUser(dbConnection, adminHeaders, container)
19
20 salesChannel1 = (
21 await api.post(
22 "/admin/sales-channels",
23 {
24 name: "test name",
25 description: "test description",
26 },
27 adminHeaders
28 )
29 ).data.sales_channel
30
31 salesChannel2 = (
32 await api.post(
33 "/admin/sales-channels",
34 {
35 name: "test name 2",
36 description: "test description 2",
37 },
38 adminHeaders
39 )
40 ).data.sales_channel
41
42 await dbUtils.snapshot()
43 })
44
45 describe("GET /admin/sales-channels/:id", () => {
46 it("should retrieve the requested sales channel", async () => {
47 const response = await api.get(
48 `/admin/sales-channels/${salesChannel1.id}`,
49 adminHeaders
50 )
51
52 expect(response.status).toEqual(200)
53 expect(response.data.sales_channel).toBeTruthy()
54 expect(response.data.sales_channel).toEqual(
55 expect.objectContaining({
56 id: expect.any(String),
57 name: salesChannel1.name,
58 description: salesChannel1.description,
59 created_at: expect.any(String),
60 updated_at: expect.any(String),
61 })
62 )
63 })
64 })
65
66 describe("GET /admin/sales-channels", () => {
67 it("should list the sales channel", async () => {
68 const response = await api.get(`/admin/sales-channels`, adminHeaders)
69
70 expect(response.status).toEqual(200)
71 expect(response.data.sales_channels).toBeTruthy()
72 expect(response.data.sales_channels.length).toBe(3) // includes the default sales channel
73 expect(response.data).toEqual(
74 expect.objectContaining({
75 sales_channels: expect.arrayContaining([
76 expect.objectContaining({
77 name: salesChannel1.name,
78 description: salesChannel1.description,
79 }),
80 expect.objectContaining({
81 name: salesChannel2.name,
82 description: salesChannel2.description,
83 }),
84 ]),
85 })
86 )
87 })
88
89 it("should list the sales channel using filters", async () => {
90 const response = await api.get(
91 `/admin/sales-channels?q=2`,
92 adminHeaders
93 )
94
95 expect(response.status).toEqual(200)
96 expect(response.data.sales_channels).toBeTruthy()
97 expect(response.data.sales_channels.length).toBe(1)
98 expect(response.data).toEqual({
99 count: 1,
100 limit: 20,
101 offset: 0,
102 sales_channels: expect.arrayContaining([
103 expect.objectContaining({
104 id: expect.any(String),
105 name: salesChannel2.name,
106 description: salesChannel2.description,
107 is_disabled: false,
108 deleted_at: null,
109 created_at: expect.any(String),
110 updated_at: expect.any(String),
111 }),
112 ]),
113 })
114 })
115
116 it("should list the sales channel using properties filters", async () => {
117 const response = await api.get(
118 `/admin/sales-channels?name=test+name`,
119 adminHeaders
120 )
121
122 expect(response.status).toEqual(200)
123 expect(response.data.sales_channels).toBeTruthy()
124 expect(response.data.sales_channels.length).toBe(1)
125 expect(response.data).toEqual({
126 count: 1,
127 limit: 20,
128 offset: 0,
129 sales_channels: expect.arrayContaining([
130 expect.objectContaining({
131 id: expect.any(String),
132 name: salesChannel1.name,
133 description: salesChannel1.description,
134 is_disabled: false,
135 deleted_at: null,
136 created_at: expect.any(String),
137 updated_at: expect.any(String),
138 }),
139 ]),
140 })
141 })
142
143 it("should support searching of sales channels", async () => {
144 await api.post(
145 "/admin/sales-channels",
146 { name: "first channel", description: "to fetch" },
147 adminHeaders
148 )
149
150 await api.post(
151 "/admin/sales-channels",
152 { name: "second channel", description: "not in response" },
153 adminHeaders
154 )
155
156 const response = await api.get(
157 `/admin/sales-channels?q=fetch`,
158 adminHeaders
159 )
160
161 expect(response.status).toEqual(200)
162 expect(response.data.sales_channels).toEqual([
163 expect.objectContaining({
164 name: "first channel",
165 }),
166 ])
167 })
168 })
169
170 describe("POST /admin/sales-channels/:id", () => {
171 it("updates sales channel properties", async () => {
172 const payload = {
173 name: "updated name",
174 description: "updated description",
175 is_disabled: true,
176 }
177
178 const response = await api.post(
179 `/admin/sales-channels/${salesChannel1.id}`,
180 payload,
181 adminHeaders
182 )
183
184 expect(response.status).toEqual(200)
185 expect(response.data.sales_channel).toEqual(
186 expect.objectContaining({
187 id: expect.any(String),
188 name: payload.name,
189 description: payload.description,
190 is_disabled: payload.is_disabled,
191 created_at: expect.any(String),
192 updated_at: expect.any(String),
193 })
194 )
195 })
196 })
197
198 describe("POST /admin/sales-channels", () => {
199 it("successfully creates a disabled sales channel", async () => {
200 const newSalesChannel = {
201 name: "sales channel name",
202 is_disabled: true,
203 }
204
205 const response = await api.post(
206 "/admin/sales-channels",
207 newSalesChannel,
208 adminHeaders
209 )
210
211 expect(response.status).toEqual(200)
212 expect(response.data.sales_channel).toBeTruthy()
213
214 expect(response.data).toEqual(
215 expect.objectContaining({
216 sales_channel: expect.objectContaining({
217 name: newSalesChannel.name,
218 is_disabled: true,
219 }),
220 })
221 )
222 })
223
224 it("successfully creates a sales channel", async () => {
225 const newSalesChannel = {
226 name: "sales channel name",
227 description: "sales channel description",
228 }
229
230 const response = await api.post(
231 "/admin/sales-channels",
232 newSalesChannel,
233 adminHeaders
234 )
235
236 expect(response.status).toEqual(200)
237 expect(response.data.sales_channel).toBeTruthy()
238
239 expect(response.data).toEqual(
240 expect.objectContaining({
241 sales_channel: expect.objectContaining({
242 name: newSalesChannel.name,
243 description: newSalesChannel.description,
244 is_disabled: false,
245 }),
246 })
247 )
248 })
249 })
250
251 describe("DELETE /admin/sales-channels/:id", () => {
252 it("should fail to delete the requested sales channel if it is used as a default sales channel", async () => {
253 const salesChannel = (
254 await api.post(
255 "/admin/sales-channels",
256 { name: "Test channel", description: "Test" },
257 adminHeaders
258 )
259 ).data.sales_channel
260
261 const storeModule = container.resolve(Modules.STORE)
262 await storeModule.createStores({
263 name: "New store",
264 supported_currencies: [
265 { currency_code: "usd", is_default: true },
266 { currency_code: "dkk" },
267 ],
268 default_sales_channel_id: salesChannel.id,
269 })
270
271 const errorResponse = await api
272 .delete(`/admin/sales-channels/${salesChannel.id}`, adminHeaders)
273 .catch((err) => err)
274
275 expect(errorResponse.response.data.message).toEqual(
276 `Cannot delete default sales channels: ${salesChannel.id}`
277 )
278 })
279
280 it("should delete the requested sales channel", async () => {
281 const toDelete = (
282 await api.get(
283 `/admin/sales-channels/${salesChannel1.id}`,
284 adminHeaders
285 )
286 ).data.sales_channel
287
288 expect(toDelete.id).toEqual(salesChannel1.id)
289 expect(toDelete.deleted_at).toEqual(null)
290
291 const response = await api.delete(
292 `/admin/sales-channels/${salesChannel1.id}`,
293 adminHeaders
294 )
295
296 expect(response.status).toEqual(200)
297 expect(response.data).toEqual({
298 deleted: true,
299 id: expect.any(String),
300 object: "sales-channel",
301 })
302
303 const err = await api
304 .get(
305 `/admin/sales-channels/${salesChannel1.id}?fields=id,deleted_at`,
306 adminHeaders
307 )
308 .catch((err) => {
309 return err
310 })
311
312 expect(err.response.data.type).toEqual("not_found")
313 expect(err.response.data.message).toEqual(
314 `Sales channel with id: ${salesChannel1.id} not found`
315 )
316 })
317
318 it("should successfully delete channel associations", async () => {
319 let location = (
320 await api.post(
321 `/admin/stock-locations`,
322 {
323 name: "test location",
324 },
325 adminHeaders
326 )
327 ).data.stock_location
328
329 await api.post(
330 `/admin/stock-locations/${location.id}/sales-channels`,
331 {
332 add: [salesChannel1.id, salesChannel2.id],
333 },
334 adminHeaders
335 )
336
337 await api.delete(
338 `/admin/sales-channels/${salesChannel1.id}`,
339 adminHeaders
340 )
341
342 location = (
343 await api.get(
344 `/admin/stock-locations/${location.id}?fields=*sales_channels`,
345 adminHeaders
346 )
347 ).data.stock_location
348
349 expect(location.sales_channels).toHaveLength(1)
350 })
351 })
352
353 describe("POST /admin/sales-channels/:id/products", () => {
354 // BREAKING CHANGE: Endpoint has changed
355 // from: /admin/sales-channels/:id/products/batch
356 // to: /admin/sales-channels/:id/products
357 let product
358 beforeEach(async () => {
359 const shippingProfile = (
360 await api.post(
361 `/admin/shipping-profiles`,
362 { name: "Test", type: "default" },
363 adminHeaders
364 )
365 ).data.shipping_profile
366
367 product = (
368 await api.post(
369 "/admin/products",
370 {
371 title: "test name",
372 shipping_profile_id: shippingProfile.id,
373 options: [{ title: "size", values: ["large"] }],
374 },
375 adminHeaders
376 )
377 ).data.product
378 })
379
380 it("should add products to a sales channel", async () => {
381 const response = await api.post(
382 `/admin/sales-channels/${salesChannel1.id}/products`,
383 { add: [product.id] },
384 adminHeaders
385 )
386
387 expect(response.status).toEqual(200)
388 expect(response.data.sales_channel).toEqual(
389 expect.objectContaining({
390 id: expect.any(String),
391 name: "test name",
392 description: "test description",
393 is_disabled: false,
394 created_at: expect.any(String),
395 updated_at: expect.any(String),
396 deleted_at: null,
397 })
398 )
399
400 product = (
401 await api.get(
402 `/admin/products/${product.id}?fields=*sales_channels`,
403 adminHeaders
404 )
405 ).data.product
406
407 expect(product.sales_channels.length).toBe(1)
408 expect(product.sales_channels).toEqual(
409 expect.arrayContaining([
410 expect.objectContaining({
411 id: expect.any(String),
412 name: "test name",
413 description: "test description",
414 is_disabled: false,
415 }),
416 ])
417 )
418 })
419
420 it("should remove products from a sales channel", async () => {
421 await api.post(
422 `/admin/sales-channels/${salesChannel1.id}/products`,
423 { add: [product.id] },
424 adminHeaders
425 )
426
427 product = (
428 await api.get(
429 `/admin/products/${product.id}?fields=*sales_channels`,
430 adminHeaders
431 )
432 ).data.product
433
434 expect(product.sales_channels.length).toBe(1)
435 expect(product.sales_channels).toEqual(
436 expect.arrayContaining([
437 expect.objectContaining({
438 id: expect.any(String),
439 name: "test name",
440 description: "test description",
441 is_disabled: false,
442 }),
443 ])
444 )
445
446 const response = await api.post(
447 `/admin/sales-channels/${salesChannel1.id}/products`,
448 { remove: [product.id] },
449 adminHeaders
450 )
451
452 expect(response.status).toEqual(200)
453 expect(response.data.sales_channel).toEqual(
454 expect.objectContaining({
455 id: expect.any(String),
456 name: "test name",
457 description: "test description",
458 is_disabled: false,
459 })
460 )
461
462 product = (
463 await api.get(
464 `/admin/products/${product.id}?fields=*sales_channels`,
465 adminHeaders
466 )
467 ).data.product
468
469 expect(product.sales_channels.length).toBe(0)
470 })
471 })
472
473 describe("Sales channels with publishable key", () => {
474 let pubKey1
475 beforeEach(async () => {
476 pubKey1 = (
477 await api.post(
478 "/admin/api-keys",
479 { title: "sample key", type: "publishable" },
480 adminHeaders
481 )
482 ).data.api_key
483
484 await api.post(
485 `/admin/api-keys/${pubKey1.id}/sales-channels`,
486 {
487 add: [salesChannel1.id, salesChannel2.id],
488 },
489 adminHeaders
490 )
491 })
492
493 it("list sales channels from the publishable api key with free text search filter", async () => {
494 const response = await api.get(
495 `/admin/sales-channels?q=2&publishable_key_id=${pubKey1.id}`,
496 adminHeaders
497 )
498
499 expect(response.status).toBe(200)
500 expect(response.data.sales_channels.length).toEqual(1)
501 expect(response.data.sales_channels).toEqual(
502 expect.arrayContaining([
503 expect.objectContaining({
504 id: salesChannel2.id,
505 deleted_at: null,
506 name: "test name 2",
507 description: "test description 2",
508 is_disabled: false,
509 }),
510 ])
511 )
512 })
513 })
514
515 // BREAKING: DELETED TESTS:
516 // - POST /admin/products/:id
517 // - Mutation sales channels on products
518 // - POST /admin/products
519 // - Creating a product with a sales channel
520 // - GET /admin/products
521 // - Filtering products by sales channel
522 // - Expanding with a sales channel
523 // - GET /admin/orders
524 // - Filtering orders by sales channel
525 // - Expanding with a sales channel
526 // - POST /admin/orders/:id/swaps
527 // - Creating a swap with a sales channel
528 //
529 },
530 })