1 import { Modules } from "@medusajs/utils"
2 import {
3 adminHeaders,
4 createAdminUser,
5 } from "../../../../helpers/create-admin-user"
6
7 const { medusaIntegrationTestRunner } = require("@medusajs/test-utils")
8
9 jest.setTimeout(30000)
10
11 medusaIntegrationTestRunner({
12 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
13 let location1
14 let location2
15 let salesChannel1
16 let salesChannel2
17
18 beforeAll(async () => {
19 await createAdminUser(dbConnection, adminHeaders, getContainer())
20
21 location1 = (
22 await api.post(
23 `/admin/stock-locations`,
24 {
25 name: "Test Location 1",
26 address: {
27 address_1: "Test Address",
28 country_code: "US",
29 },
30 },
31 adminHeaders
32 )
33 ).data.stock_location
34
35 location2 = (
36 await api.post(
37 `/admin/stock-locations`,
38 {
39 name: "Test Location 2",
40 address: {
41 address_1: "Test Address",
42 country_code: "US",
43 },
44 },
45 adminHeaders
46 )
47 ).data.stock_location
48
49 salesChannel1 = (
50 await api.post(
51 `/admin/sales-channels`,
52 {
53 name: "Test SC",
54 },
55 adminHeaders
56 )
57 ).data.sales_channel
58
59 salesChannel2 = (
60 await api.post(
61 `/admin/sales-channels`,
62 {
63 name: "Test SC 2",
64 },
65 adminHeaders
66 )
67 ).data.sales_channel
68
69 await dbUtils.snapshot()
70 })
71
72 describe("create stock location", () => {
73 it("should create a stock location with a name and address", async () => {
74 const response = await api.post(
75 "/admin/stock-locations",
76 {
77 name: "Test Location",
78 address: {
79 address_1: "Test Address",
80 country_code: "US",
81 },
82 },
83 adminHeaders
84 )
85
86 expect(response.status).toEqual(200)
87 expect(response.data.stock_location).toEqual(
88 expect.objectContaining({
89 name: "Test Location",
90 address: expect.objectContaining({
91 address_1: "Test Address",
92 country_code: "US",
93 }),
94 })
95 )
96 })
97
98 it("should create a stock location with address metadata", async () => {
99 const response = await api.post(
100 "/admin/stock-locations",
101 {
102 name: "Metadata Test Location",
103 address: {
104 address_1: "123 Test St",
105 country_code: "US",
106 metadata: {
107 internal_id: "LOC-123",
108 gate_code: "9999",
109 },
110 },
111 },
112 adminHeaders
113 )
114
115 expect(response.status).toEqual(200)
116 expect(response.data.stock_location).toEqual(
117 expect.objectContaining({
118 name: "Metadata Test Location",
119 address: expect.objectContaining({
120 address_1: "123 Test St",
121 country_code: "US",
122 metadata: {
123 internal_id: "LOC-123",
124 gate_code: "9999",
125 },
126 }),
127 })
128 )
129 })
130
131 it("should create and retrieve a stock location with metadata", async () => {
132 const metadata = {
133 internal_id: "LOC-789",
134 priority: "high",
135 }
136
137 const response = await api.post(
138 "/admin/stock-locations",
139 {
140 name: "Stock Location Metadata Test",
141 metadata,
142 address: {
143 address_1: "456 Test St",
144 country_code: "US",
145 },
146 },
147 adminHeaders
148 )
149
150 expect(response.status).toEqual(200)
151 expect(response.data.stock_location).toEqual(
152 expect.objectContaining({
153 name: "Stock Location Metadata Test",
154 metadata,
155 })
156 )
157
158 const retrieveResponse = await api.get(
159 `/admin/stock-locations/${response.data.stock_location.id}`,
160 adminHeaders
161 )
162
163 expect(retrieveResponse.status).toEqual(200)
164 expect(retrieveResponse.data.stock_location).toEqual(
165 expect.objectContaining({
166 id: response.data.stock_location.id,
167 metadata,
168 })
169 )
170 })
171 })
172
173 describe("list stock locations", () => {
174 it("should list stock locations", async () => {
175 const listLocationsResponse = await api.get(
176 "/admin/stock-locations",
177 adminHeaders
178 )
179
180 expect(listLocationsResponse.status).toEqual(200)
181 expect(listLocationsResponse.data.stock_locations).toEqual([
182 expect.objectContaining(location1),
183 expect.objectContaining(location2),
184 ])
185 })
186
187 it("should filter stock locations by name", async () => {
188 const listLocationsResponse = await api.get(
189 "/admin/stock-locations?name=Test%20Location%201",
190 adminHeaders
191 )
192
193 expect(listLocationsResponse.status).toEqual(200)
194 expect(listLocationsResponse.data.stock_locations).toEqual([
195 expect.objectContaining(location1),
196 ])
197 })
198
199 it("should filter stock locations by partial name with q parameter", async () => {
200 const listLocationsResponse = await api.get(
201 "/admin/stock-locations?q=ation%201",
202 adminHeaders
203 )
204
205 expect(listLocationsResponse.status).toEqual(200)
206 expect(listLocationsResponse.data.stock_locations).toEqual([
207 expect.objectContaining(location1),
208 ])
209 })
210
211 it("should filter stock locations on sales_channel_id", async () => {
212 await api.post(
213 `/admin/stock-locations/${location1.id}/sales-channels`,
214 {
215 add: [salesChannel1.id],
216 },
217 adminHeaders
218 )
219
220 const listLocationsResponse = await api.get(
221 `/admin/stock-locations?sales_channel_id=${salesChannel1.id}`,
222 adminHeaders
223 )
224
225 expect(listLocationsResponse.status).toEqual(200)
226 expect(listLocationsResponse.data.stock_locations).toEqual([
227 expect.objectContaining(location1),
228 ])
229 })
230 })
231
232 describe("Update stock locations", () => {
233 it("should update stock location name", async () => {
234 const response = await api.post(
235 `/admin/stock-locations/${location1.id}`,
236 {
237 name: "new name",
238 },
239 adminHeaders
240 )
241 expect(response.status).toEqual(200)
242 expect(response.data.stock_location.name).toEqual("new name")
243 })
244
245 it("should update stock location address without creating new addresses", async () => {
246 const response = await api.post(
247 `/admin/stock-locations/${location1.id}`,
248 {
249 name: "new name",
250 address: {
251 address_1: "test",
252 country_code: "dk",
253 },
254 },
255 adminHeaders
256 )
257
258 const firstAddressId = response.data.stock_location.address.id
259
260 expect(response.status).toEqual(200)
261 expect(response.data.stock_location).toEqual(
262 expect.objectContaining({
263 name: "new name",
264 address: expect.objectContaining({
265 id: firstAddressId,
266 address_1: "test",
267 country_code: "dk",
268 }),
269 })
270 )
271
272 const response2 = await api.post(
273 `/admin/stock-locations/${location1.id}`,
274 {
275 name: "new name 2",
276 address: {
277 address_1: "test 2",
278 country_code: "dk",
279 },
280 },
281 adminHeaders
282 )
283
284 expect(response2.status).toEqual(200)
285 expect(response2.data.stock_location).toEqual(
286 expect.objectContaining({
287 name: "new name 2",
288 address: expect.objectContaining({
289 id: firstAddressId,
290 address_1: "test 2",
291 country_code: "dk",
292 }),
293 })
294 )
295 })
296
297 it("should update stock location address metadata", async () => {
298 const response = await api.post(
299 `/admin/stock-locations/${location1.id}`,
300 {
301 address: {
302 address_1: "123 Test St",
303 country_code: "US",
304 metadata: {
305 updated_id: "LOC-456",
306 },
307 },
308 },
309 adminHeaders
310 )
311
312 expect(response.status).toEqual(200)
313 expect(response.data.stock_location.address).toEqual(
314 expect.objectContaining({
315 address_1: "123 Test St",
316 country_code: "US",
317 metadata: {
318 updated_id: "LOC-456",
319 },
320 })
321 )
322 })
323 })
324
325 describe("Get stock location", () => {
326 it("should get a stock location", async () => {
327 const response = await api.get(
328 `/admin/stock-locations/${location1.id}`,
329 adminHeaders
330 )
331
332 expect(response.status).toEqual(200)
333 expect(response.data.stock_location).toEqual(
334 expect.objectContaining({ id: location1.id, name: "Test Location 1" })
335 )
336 })
337
338 it("should throw an error when a stock location does not exist", async () => {
339 let error
340 await api
341 .get(`/admin/stock-locations/does-not-exist`, adminHeaders)
342 .catch((e) => (error = e))
343
344 expect(error.response.status).toEqual(404)
345 expect(error.response.data.message).toEqual(
346 `Stock location with id: does-not-exist was not found`
347 )
348 })
349 })
350
351 describe("Delete stock location", () => {
352 it("should successfully delete stock location", async () => {
353 const stockLocationDeleteResponse = await api.delete(
354 `/admin/stock-locations/${location1.id}`,
355 adminHeaders
356 )
357
358 expect(stockLocationDeleteResponse.status).toEqual(200)
359 expect(stockLocationDeleteResponse.data).toEqual({
360 id: location1.id,
361 object: "stock_location",
362 deleted: true,
363 })
364
365 const stockLocations = await api.get(
366 `/admin/stock-locations`,
367 adminHeaders
368 )
369
370 expect(stockLocations.status).toEqual(200)
371 expect(stockLocations.data.stock_locations).toEqual([
372 expect.objectContaining(location2),
373 ])
374 })
375
376 it("should successfully delete stock location associations", async () => {
377 await api.post(
378 `/admin/stock-locations/${location1.id}/sales-channels`,
379 {
380 add: [salesChannel1.id],
381 },
382 adminHeaders
383 )
384
385 await api.delete(`/admin/stock-locations/${location1.id}`, adminHeaders)
386
387 const scWithAssociation = (
388 await api.get(
389 `/admin/sales-channels/${salesChannel1.id}?fields=*stock_locations`,
390 adminHeaders
391 )
392 ).data.sales_channel
393
394 expect(scWithAssociation.stock_locations).toEqual([])
395 })
396 })
397
398 describe("Add sales channels", () => {
399 it("should add sales channels to a location", async () => {
400 const salesChannelResponse = await api.post(
401 `/admin/stock-locations/${location1.id}/sales-channels?fields=*sales_channels`,
402 { add: [salesChannel1.id] },
403 adminHeaders
404 )
405
406 expect(
407 salesChannelResponse.data.stock_location.sales_channels
408 ).toHaveLength(1)
409 })
410 })
411
412 describe("Remove sales channels", () => {
413 beforeEach(async () => {
414 await api.post(
415 `/admin/stock-locations/${location1.id}/sales-channels?fields=*sales_channels`,
416 { add: [salesChannel1.id, salesChannel2.id] },
417 adminHeaders
418 )
419 })
420
421 it("should remove sales channels from a location", async () => {
422 const salesChannelResponse = await api.post(
423 `/admin/stock-locations/${location1.id}/sales-channels?fields=*sales_channels`,
424 { remove: [salesChannel1.id] },
425 adminHeaders
426 )
427
428 expect(
429 salesChannelResponse.data.stock_location.sales_channels
430 ).toHaveLength(1)
431 })
432 })
433
434 describe("Location fulfillment sets", () => {
435 it("should create a fulfillment set for the location and then delete it and its associations", async () => {
436 const response = await api.post(
437 `/admin/stock-locations/${location1.id}/fulfillment-sets?fields=id,*fulfillment_sets`,
438 {
439 name: "Fulfillment Set",
440 type: "shipping",
441 },
442 adminHeaders
443 )
444
445 expect(response.status).toEqual(200)
446
447 expect(response.data.stock_location.fulfillment_sets).toEqual([
448 expect.objectContaining({
449 id: expect.any(String),
450 }),
451 ])
452
453 await api.delete(`/admin/stock-locations/${location1.id}`, adminHeaders)
454
455 // TODO: Ideally we use HTTP here, maybe we should have a get endpoint for fulfillment sets?
456 const fulfillmentModule = getContainer().resolve(Modules.FULFILLMENT)
457 const sets = await fulfillmentModule.listFulfillmentSets()
458 expect(sets).toHaveLength(0)
459 })
460
461 // This is really just to test the new Zod middleware. We don't need more of these.
462 it("should throw a validation error on wrong input", async () => {
463 const errorResponse = await api
464 .post(
465 `/admin/stock-locations/${location1.id}/fulfillment-sets?fields=id,*fulfillment_sets`,
466 {
467 name: "Fulfillment Set",
468 type: "shipping",
469 foo: "bar",
470 },
471 adminHeaders
472 )
473 .catch((e) => e.response)
474
475 expect(errorResponse.status).toEqual(400)
476
477 expect(errorResponse.data.message).toEqual(
478 "Invalid request: Unrecognized fields: 'foo'"
479 )
480 })
481 })
482 },
483 })