|
1
|
import { RemoteLink } from "@medusajs/modules-sdk" |
|
2
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
3
|
import { |
|
4
|
IApiKeyModuleService, |
|
5
|
ICartModuleService, |
|
6
|
ICustomerModuleService, |
|
7
|
IFulfillmentModuleService, |
|
8
|
IPaymentModuleService, |
|
9
|
IPricingModuleService, |
|
10
|
IProductModuleService, |
|
11
|
IPromotionModuleService, |
|
12
|
IRegionModuleService, |
|
13
|
ISalesChannelModuleService, |
|
14
|
IStoreModuleService, |
|
15
|
ITaxModuleService, |
|
16
|
} from "@medusajs/types" |
|
17
|
import { |
|
18
|
ContainerRegistrationKeys, |
|
19
|
MedusaError, |
|
20
|
Modules, |
|
21
|
ProductStatus, |
|
22
|
PromotionStatus, |
|
23
|
PromotionType, |
|
24
|
} from "@medusajs/utils" |
|
25
|
import { |
|
26
|
createAdminUser, |
|
27
|
generatePublishableKey, |
|
28
|
generateStoreHeaders, |
|
29
|
} from "../../../../helpers/create-admin-user" |
|
30
|
import { linkRegionPaymentProviders } from "../../../../helpers/link-region-payment-providers" |
|
31
|
import { seedStorefrontDefaults } from "../../../../helpers/seed-storefront-defaults" |
|
32
|
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer" |
|
33
|
import { setupTaxStructure } from "../../fixtures" |
|
34
|
|
|
35
|
jest.setTimeout(100000) |
|
36
|
|
|
37
|
const env = {} |
|
38
|
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } |
|
39
|
|
|
40
|
medusaIntegrationTestRunner({ |
|
41
|
env, |
|
42
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
43
|
describe("Store Carts API", () => { |
|
44
|
let appContainer |
|
45
|
let cartModule: ICartModuleService |
|
46
|
let regionModule: IRegionModuleService |
|
47
|
let scModule: ISalesChannelModuleService |
|
48
|
let customerModule: ICustomerModuleService |
|
49
|
let productModule: IProductModuleService |
|
50
|
let pricingModule: IPricingModuleService |
|
51
|
let remoteLink: RemoteLink |
|
52
|
let promotionModule: IPromotionModuleService |
|
53
|
let apiKeyModule: IApiKeyModuleService |
|
54
|
let taxModule: ITaxModuleService |
|
55
|
let fulfillmentModule: IFulfillmentModuleService |
|
56
|
let remoteLinkService |
|
57
|
let regionService: IRegionModuleService |
|
58
|
let paymentService: IPaymentModuleService |
|
59
|
let storeService: IStoreModuleService |
|
60
|
|
|
61
|
let region |
|
62
|
let store |
|
63
|
let storeHeaders |
|
64
|
|
|
65
|
beforeAll(async () => { |
|
66
|
appContainer = getContainer() |
|
67
|
cartModule = appContainer.resolve(Modules.CART) |
|
68
|
regionModule = appContainer.resolve(Modules.REGION) |
|
69
|
scModule = appContainer.resolve(Modules.SALES_CHANNEL) |
|
70
|
customerModule = appContainer.resolve(Modules.CUSTOMER) |
|
71
|
productModule = appContainer.resolve(Modules.PRODUCT) |
|
72
|
pricingModule = appContainer.resolve(Modules.PRICING) |
|
73
|
apiKeyModule = appContainer.resolve(Modules.API_KEY) |
|
74
|
remoteLink = appContainer.resolve(ContainerRegistrationKeys.REMOTE_LINK) |
|
75
|
promotionModule = appContainer.resolve(Modules.PROMOTION) |
|
76
|
taxModule = appContainer.resolve(Modules.TAX) |
|
77
|
regionService = appContainer.resolve(Modules.REGION) |
|
78
|
paymentService = appContainer.resolve(Modules.PAYMENT) |
|
79
|
storeService = appContainer.resolve(Modules.STORE) |
|
80
|
fulfillmentModule = appContainer.resolve(Modules.FULFILLMENT) |
|
81
|
remoteLinkService = appContainer.resolve( |
|
82
|
ContainerRegistrationKeys.REMOTE_LINK |
|
83
|
) |
|
84
|
}) |
|
85
|
|
|
86
|
beforeAll(async () => { |
|
87
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
88
|
const publishableKey = await generatePublishableKey(appContainer) |
|
89
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
90
|
|
|
91
|
const { store: defaultStore } = await seedStorefrontDefaults( |
|
92
|
appContainer, |
|
93
|
"dkk" |
|
94
|
) |
|
95
|
|
|
96
|
store = defaultStore |
|
97
|
|
|
98
|
await dbUtils.snapshot() |
|
99
|
}) |
|
100
|
|
|
101
|
describe("POST /store/carts", () => { |
|
102
|
it("should create a cart", async () => { |
|
103
|
const region = await regionModule.createRegions({ |
|
104
|
name: "US", |
|
105
|
currency_code: "usd", |
|
106
|
}) |
|
107
|
|
|
108
|
const salesChannel = await scModule.createSalesChannels({ |
|
109
|
name: "Webshop", |
|
110
|
}) |
|
111
|
|
|
112
|
const [product] = await productModule.createProducts([ |
|
113
|
{ |
|
114
|
title: "Test product", |
|
115
|
status: ProductStatus.PUBLISHED, |
|
116
|
variants: [ |
|
117
|
{ |
|
118
|
manage_inventory: false, |
|
119
|
title: "Test variant", |
|
120
|
}, |
|
121
|
{ |
|
122
|
manage_inventory: false, |
|
123
|
title: "Test variant 2", |
|
124
|
}, |
|
125
|
], |
|
126
|
}, |
|
127
|
]) |
|
128
|
|
|
129
|
const [priceSet, priceSetTwo] = await pricingModule.createPriceSets([ |
|
130
|
{ |
|
131
|
prices: [ |
|
132
|
{ |
|
133
|
amount: 3000, |
|
134
|
currency_code: "usd", |
|
135
|
}, |
|
136
|
], |
|
137
|
}, |
|
138
|
{ |
|
139
|
prices: [ |
|
140
|
{ |
|
141
|
amount: 4000, |
|
142
|
currency_code: "usd", |
|
143
|
}, |
|
144
|
], |
|
145
|
}, |
|
146
|
]) |
|
147
|
|
|
148
|
await remoteLink.create([ |
|
149
|
{ |
|
150
|
[Modules.PRODUCT]: { |
|
151
|
variant_id: product.variants[0].id, |
|
152
|
}, |
|
153
|
[Modules.PRICING]: { |
|
154
|
price_set_id: priceSet.id, |
|
155
|
}, |
|
156
|
}, |
|
157
|
{ |
|
158
|
[Modules.PRODUCT]: { |
|
159
|
variant_id: product.variants[1].id, |
|
160
|
}, |
|
161
|
[Modules.PRICING]: { |
|
162
|
price_set_id: priceSetTwo.id, |
|
163
|
}, |
|
164
|
}, |
|
165
|
]) |
|
166
|
|
|
167
|
const created = await api.post( |
|
168
|
`/store/carts`, |
|
169
|
{ |
|
170
|
email: "tony@stark.com", |
|
171
|
currency_code: "usd", |
|
172
|
region_id: region.id, |
|
173
|
sales_channel_id: salesChannel.id, |
|
174
|
items: [ |
|
175
|
{ |
|
176
|
variant_id: product.variants[0].id, |
|
177
|
quantity: 1, |
|
178
|
}, |
|
179
|
{ |
|
180
|
variant_id: product.variants[1].id, |
|
181
|
quantity: 2, |
|
182
|
}, |
|
183
|
], |
|
184
|
}, |
|
185
|
storeHeaders |
|
186
|
) |
|
187
|
|
|
188
|
expect(created.status).toEqual(200) |
|
189
|
expect(created.data.cart).toEqual( |
|
190
|
expect.objectContaining({ |
|
191
|
id: created.data.cart.id, |
|
192
|
currency_code: "usd", |
|
193
|
email: "tony@stark.com", |
|
194
|
region: expect.objectContaining({ |
|
195
|
id: region.id, |
|
196
|
currency_code: "usd", |
|
197
|
}), |
|
198
|
sales_channel_id: salesChannel.id, |
|
199
|
customer: expect.objectContaining({ |
|
200
|
email: "tony@stark.com", |
|
201
|
}), |
|
202
|
items: expect.arrayContaining([ |
|
203
|
expect.objectContaining({ |
|
204
|
quantity: 1, |
|
205
|
unit_price: 3000, |
|
206
|
}), |
|
207
|
expect.objectContaining({ |
|
208
|
quantity: 2, |
|
209
|
unit_price: 4000, |
|
210
|
}), |
|
211
|
]), |
|
212
|
}) |
|
213
|
) |
|
214
|
}) |
|
215
|
|
|
216
|
it("should create cart with customer from email and tax lines", async () => { |
|
217
|
await setupTaxStructure(taxModule) |
|
218
|
|
|
219
|
const [product] = await productModule.createProducts([ |
|
220
|
{ |
|
221
|
title: "Test product default tax", |
|
222
|
status: ProductStatus.PUBLISHED, |
|
223
|
variants: [ |
|
224
|
{ title: "Test variant default tax", manage_inventory: false }, |
|
225
|
], |
|
226
|
}, |
|
227
|
]) |
|
228
|
|
|
229
|
const salesChannel = await scModule.createSalesChannels({ |
|
230
|
name: "Webshop", |
|
231
|
}) |
|
232
|
|
|
233
|
const [priceSet] = await pricingModule.createPriceSets([ |
|
234
|
{ prices: [{ amount: 3000, currency_code: "usd" }] }, |
|
235
|
]) |
|
236
|
|
|
237
|
await remoteLink.create([ |
|
238
|
{ |
|
239
|
[Modules.PRODUCT]: { variant_id: product.variants[0].id }, |
|
240
|
[Modules.PRICING]: { price_set_id: priceSet.id }, |
|
241
|
}, |
|
242
|
]) |
|
243
|
|
|
244
|
const region = await regionModule.createRegions({ |
|
245
|
name: "US Tax", |
|
246
|
currency_code: "usd", |
|
247
|
}) |
|
248
|
|
|
249
|
const created = await api.post( |
|
250
|
`/store/carts`, |
|
251
|
{ |
|
252
|
currency_code: "usd", |
|
253
|
region_id: region.id, |
|
254
|
email: "tony@stark-industries.com", |
|
255
|
shipping_address: { |
|
256
|
address_1: "test address 1", |
|
257
|
address_2: "test address 2", |
|
258
|
city: "NY", |
|
259
|
country_code: "US", |
|
260
|
province: "NY", |
|
261
|
postal_code: "94016", |
|
262
|
}, |
|
263
|
sales_channel_id: salesChannel.id, |
|
264
|
items: [ |
|
265
|
{ |
|
266
|
quantity: 1, |
|
267
|
variant_id: product.variants[0].id, |
|
268
|
}, |
|
269
|
], |
|
270
|
}, |
|
271
|
storeHeaders |
|
272
|
) |
|
273
|
|
|
274
|
expect(created.status).toEqual(200) |
|
275
|
expect(created.data.cart).toEqual( |
|
276
|
expect.objectContaining({ |
|
277
|
id: created.data.cart.id, |
|
278
|
currency_code: "usd", |
|
279
|
email: "tony@stark-industries.com", |
|
280
|
customer: expect.objectContaining({ |
|
281
|
id: expect.any(String), |
|
282
|
email: "tony@stark-industries.com", |
|
283
|
}), |
|
284
|
items: [ |
|
285
|
expect.objectContaining({ |
|
286
|
tax_lines: [ |
|
287
|
expect.objectContaining({ |
|
288
|
description: "NY Default Rate", |
|
289
|
code: "NYDEFAULT", |
|
290
|
rate: 6, |
|
291
|
provider_id: "system", |
|
292
|
}), |
|
293
|
], |
|
294
|
adjustments: [], |
|
295
|
}), |
|
296
|
], |
|
297
|
}) |
|
298
|
) |
|
299
|
}) |
|
300
|
|
|
301
|
it("should create cart with any region", async () => { |
|
302
|
await regionModule.createRegions({ |
|
303
|
name: "US", |
|
304
|
currency_code: "usd", |
|
305
|
}) |
|
306
|
|
|
307
|
const response = await api.post( |
|
308
|
`/store/carts`, |
|
309
|
{ |
|
310
|
email: "tony@stark.com", |
|
311
|
}, |
|
312
|
storeHeaders |
|
313
|
) |
|
314
|
|
|
315
|
expect(response.status).toEqual(200) |
|
316
|
expect(response.data.cart).toEqual( |
|
317
|
expect.objectContaining({ |
|
318
|
id: response.data.cart.id, |
|
319
|
// currency follows whichever region is resolved |
|
320
|
currency_code: expect.any(String), |
|
321
|
email: "tony@stark.com", |
|
322
|
region: expect.objectContaining({ |
|
323
|
id: expect.any(String), |
|
324
|
}), |
|
325
|
}) |
|
326
|
) |
|
327
|
}) |
|
328
|
|
|
329
|
it("should create cart with shipping address country code when there is only one country assigned to the region", async () => { |
|
330
|
const region = await regionModule.createRegions({ |
|
331
|
name: "US", |
|
332
|
currency_code: "usd", |
|
333
|
countries: ["us"], |
|
334
|
}) |
|
335
|
|
|
336
|
const response = await api.post( |
|
337
|
`/store/carts`, |
|
338
|
{ |
|
339
|
email: "tony@stark.com", |
|
340
|
currency_code: "usd", |
|
341
|
region_id: region.id, |
|
342
|
}, |
|
343
|
storeHeaders |
|
344
|
) |
|
345
|
|
|
346
|
expect(response.status).toEqual(200) |
|
347
|
expect(response.data.cart).toEqual( |
|
348
|
expect.objectContaining({ |
|
349
|
id: response.data.cart.id, |
|
350
|
currency_code: "usd", |
|
351
|
email: "tony@stark.com", |
|
352
|
region: expect.objectContaining({ |
|
353
|
id: region.id, |
|
354
|
}), |
|
355
|
shipping_address: { |
|
356
|
id: expect.any(String), |
|
357
|
country_code: "us", |
|
358
|
first_name: null, |
|
359
|
last_name: null, |
|
360
|
company: null, |
|
361
|
address_1: null, |
|
362
|
address_2: null, |
|
363
|
city: null, |
|
364
|
province: null, |
|
365
|
postal_code: null, |
|
366
|
phone: null, |
|
367
|
}, |
|
368
|
}) |
|
369
|
) |
|
370
|
}) |
|
371
|
|
|
372
|
it("should create cart with default store sales channel", async () => { |
|
373
|
const sc = await scModule.createSalesChannels({ |
|
374
|
name: "Webshop", |
|
375
|
}) |
|
376
|
|
|
377
|
await storeService.updateStores(store.id, { |
|
378
|
default_sales_channel_id: sc.id, |
|
379
|
}) |
|
380
|
|
|
381
|
const response = await api.post( |
|
382
|
`/store/carts`, |
|
383
|
{ |
|
384
|
email: "tony@stark.com", |
|
385
|
}, |
|
386
|
storeHeaders |
|
387
|
) |
|
388
|
|
|
389
|
expect(response.status).toEqual(200) |
|
390
|
expect(response.data.cart).toEqual( |
|
391
|
expect.objectContaining({ |
|
392
|
id: response.data.cart.id, |
|
393
|
// currency follows whichever region is resolved |
|
394
|
currency_code: expect.any(String), |
|
395
|
email: "tony@stark.com", |
|
396
|
sales_channel_id: sc.id, |
|
397
|
}) |
|
398
|
) |
|
399
|
}) |
|
400
|
|
|
401
|
it("should create cart with region currency code", async () => { |
|
402
|
const region = await regionModule.createRegions({ |
|
403
|
name: "US", |
|
404
|
currency_code: "usd", |
|
405
|
}) |
|
406
|
|
|
407
|
const response = await api.post( |
|
408
|
`/store/carts`, |
|
409
|
{ |
|
410
|
email: "tony@stark.com", |
|
411
|
region_id: region.id, |
|
412
|
}, |
|
413
|
storeHeaders |
|
414
|
) |
|
415
|
|
|
416
|
expect(response.status).toEqual(200) |
|
417
|
expect(response.data.cart).toEqual( |
|
418
|
expect.objectContaining({ |
|
419
|
id: response.data.cart.id, |
|
420
|
currency_code: "usd", |
|
421
|
email: "tony@stark.com", |
|
422
|
region: expect.objectContaining({ |
|
423
|
id: region.id, |
|
424
|
}), |
|
425
|
}) |
|
426
|
) |
|
427
|
}) |
|
428
|
|
|
429
|
it("should create cart with logged-in customer", async () => { |
|
430
|
const { customer, jwt } = await createAuthenticatedCustomer( |
|
431
|
api, |
|
432
|
storeHeaders |
|
433
|
) |
|
434
|
|
|
435
|
const response = await api.post( |
|
436
|
`/store/carts`, |
|
437
|
{}, |
|
438
|
{ |
|
439
|
headers: { |
|
440
|
authorization: `Bearer ${jwt}`, |
|
441
|
...storeHeaders.headers, |
|
442
|
}, |
|
443
|
} |
|
444
|
) |
|
445
|
|
|
446
|
expect(response.status).toEqual(200) |
|
447
|
expect(response.data.cart).toEqual( |
|
448
|
expect.objectContaining({ |
|
449
|
id: response.data.cart.id, |
|
450
|
currency_code: "dkk", |
|
451
|
email: customer.email, |
|
452
|
customer: expect.objectContaining({ |
|
453
|
id: customer.id, |
|
454
|
email: customer.email, |
|
455
|
}), |
|
456
|
}) |
|
457
|
) |
|
458
|
}) |
|
459
|
|
|
460
|
it("throws if publishable key is not associated with sales channel", async () => { |
|
461
|
const salesChannel = await scModule.createSalesChannels({ |
|
462
|
name: "Retail Store", |
|
463
|
}) |
|
464
|
|
|
465
|
const salesChannel2 = await scModule.createSalesChannels({ |
|
466
|
name: "Webshop", |
|
467
|
}) |
|
468
|
|
|
469
|
const pubKey = await apiKeyModule.createApiKeys({ |
|
470
|
title: "Test key", |
|
471
|
type: "publishable", |
|
472
|
created_by: "test", |
|
473
|
}) |
|
474
|
|
|
475
|
await api.post( |
|
476
|
`/admin/api-keys/${pubKey.id}/sales-channels`, |
|
477
|
{ |
|
478
|
add: [salesChannel2.id], |
|
479
|
}, |
|
480
|
adminHeaders |
|
481
|
) |
|
482
|
|
|
483
|
const errorRes = await api |
|
484
|
.post( |
|
485
|
"/store/carts", |
|
486
|
{ |
|
487
|
sales_channel_id: salesChannel.id, |
|
488
|
}, |
|
489
|
{ |
|
490
|
headers: { "x-publishable-api-key": pubKey.token }, |
|
491
|
} |
|
492
|
) |
|
493
|
.catch((e) => e) |
|
494
|
|
|
495
|
expect(errorRes.response.status).toEqual(400) |
|
496
|
expect(errorRes.response.data).toEqual({ |
|
497
|
errors: expect.arrayContaining([ |
|
498
|
`Sales channel ID in payload ${salesChannel.id} is not associated with the Publishable API Key in the header.`, |
|
499
|
]), |
|
500
|
message: |
|
501
|
"Provided request body contains errors. Please check the data and retry the request", |
|
502
|
}) |
|
503
|
}) |
|
504
|
|
|
505
|
it("throws if publishable key has multiple associated sales channels", async () => { |
|
506
|
const salesChannel = await scModule.createSalesChannels({ |
|
507
|
name: "Retail Store", |
|
508
|
}) |
|
509
|
|
|
510
|
const salesChannel2 = await scModule.createSalesChannels({ |
|
511
|
name: "Webshop", |
|
512
|
}) |
|
513
|
|
|
514
|
const pubKey = await apiKeyModule.createApiKeys({ |
|
515
|
title: "Test key", |
|
516
|
type: "publishable", |
|
517
|
created_by: "test", |
|
518
|
}) |
|
519
|
|
|
520
|
await api.post( |
|
521
|
`/admin/api-keys/${pubKey.id}/sales-channels`, |
|
522
|
{ |
|
523
|
add: [salesChannel.id, salesChannel2.id], |
|
524
|
}, |
|
525
|
adminHeaders |
|
526
|
) |
|
527
|
|
|
528
|
const errorRes = await api |
|
529
|
.post( |
|
530
|
"/store/carts", |
|
531
|
{}, |
|
532
|
{ |
|
533
|
headers: { "x-publishable-api-key": pubKey.token }, |
|
534
|
} |
|
535
|
) |
|
536
|
.catch((e) => e) |
|
537
|
|
|
538
|
expect(errorRes.response.status).toEqual(400) |
|
539
|
expect(errorRes.response.data).toEqual({ |
|
540
|
errors: expect.arrayContaining([ |
|
541
|
`Cannot assign sales channel to cart. The Publishable API Key in the header has multiple associated sales channels. Please provide a sales channel ID in the request body.`, |
|
542
|
]), |
|
543
|
message: |
|
544
|
"Provided request body contains errors. Please check the data and retry the request", |
|
545
|
}) |
|
546
|
}) |
|
547
|
|
|
548
|
it("should create cart with sales channel if pub key does not have any scopes defined", async () => { |
|
549
|
const salesChannel = await scModule.createSalesChannels({ |
|
550
|
name: "Retail Store", |
|
551
|
}) |
|
552
|
|
|
553
|
const pubKey = await apiKeyModule.createApiKeys({ |
|
554
|
title: "Test key", |
|
555
|
type: "publishable", |
|
556
|
created_by: "test", |
|
557
|
}) |
|
558
|
|
|
559
|
const successRes = await api.post( |
|
560
|
"/store/carts", |
|
561
|
{ |
|
562
|
sales_channel_id: salesChannel.id, |
|
563
|
}, |
|
564
|
{ |
|
565
|
headers: { "x-publishable-api-key": pubKey.token }, |
|
566
|
} |
|
567
|
) |
|
568
|
|
|
569
|
expect(successRes.status).toEqual(200) |
|
570
|
expect(successRes.data.cart).toEqual( |
|
571
|
expect.objectContaining({ |
|
572
|
sales_channel_id: salesChannel.id, |
|
573
|
}) |
|
574
|
) |
|
575
|
}) |
|
576
|
|
|
577
|
it("should create cart with sales channel associated with pub key", async () => { |
|
578
|
const salesChannel = await scModule.createSalesChannels({ |
|
579
|
name: "Retail Store", |
|
580
|
}) |
|
581
|
|
|
582
|
const pubKey = await apiKeyModule.createApiKeys({ |
|
583
|
title: "Test key", |
|
584
|
type: "publishable", |
|
585
|
created_by: "test", |
|
586
|
}) |
|
587
|
|
|
588
|
await api.post( |
|
589
|
`/admin/api-keys/${pubKey.id}/sales-channels`, |
|
590
|
{ |
|
591
|
add: [salesChannel.id], |
|
592
|
}, |
|
593
|
adminHeaders |
|
594
|
) |
|
595
|
|
|
596
|
const successRes = await api.post( |
|
597
|
"/store/carts", |
|
598
|
{}, |
|
599
|
{ |
|
600
|
headers: { "x-publishable-api-key": pubKey.token }, |
|
601
|
} |
|
602
|
) |
|
603
|
|
|
604
|
expect(successRes.status).toEqual(200) |
|
605
|
expect(successRes.data.cart).toEqual( |
|
606
|
expect.objectContaining({ |
|
607
|
sales_channel_id: salesChannel.id, |
|
608
|
}) |
|
609
|
) |
|
610
|
}) |
|
611
|
|
|
612
|
it("should respond 400 bad request on unknown props", async () => { |
|
613
|
await expect( |
|
614
|
api.post(`/store/carts`, { foo: "bar" }, storeHeaders) |
|
615
|
).rejects.toThrow() |
|
616
|
}) |
|
617
|
}) |
|
618
|
|
|
619
|
describe("GET /store/carts", () => { |
|
620
|
it("should get cart", async () => { |
|
621
|
const region = await regionModule.createRegions({ |
|
622
|
name: "US", |
|
623
|
currency_code: "usd", |
|
624
|
}) |
|
625
|
|
|
626
|
const salesChannel = await scModule.createSalesChannels({ |
|
627
|
name: "Webshop", |
|
628
|
}) |
|
629
|
|
|
630
|
const cart = await cartModule.createCarts({ |
|
631
|
currency_code: "usd", |
|
632
|
items: [ |
|
633
|
{ |
|
634
|
unit_price: 1000, |
|
635
|
quantity: 1, |
|
636
|
title: "Test item", |
|
637
|
}, |
|
638
|
], |
|
639
|
region_id: region.id, |
|
640
|
sales_channel_id: salesChannel.id, |
|
641
|
}) |
|
642
|
|
|
643
|
const response = await api.get( |
|
644
|
`/store/carts/${cart.id}`, |
|
645
|
storeHeaders |
|
646
|
) |
|
647
|
|
|
648
|
expect(response.status).toEqual(200) |
|
649
|
expect(response.data.cart).toEqual( |
|
650
|
expect.objectContaining({ |
|
651
|
id: cart.id, |
|
652
|
currency_code: "usd", |
|
653
|
items: expect.arrayContaining([ |
|
654
|
expect.objectContaining({ |
|
655
|
unit_price: 1000, |
|
656
|
quantity: 1, |
|
657
|
title: "Test item", |
|
658
|
}), |
|
659
|
]), |
|
660
|
region: expect.objectContaining({ |
|
661
|
id: region.id, |
|
662
|
currency_code: "usd", |
|
663
|
}), |
|
664
|
sales_channel_id: salesChannel.id, |
|
665
|
}) |
|
666
|
) |
|
667
|
}) |
|
668
|
}) |
|
669
|
|
|
670
|
describe("POST /store/carts/:id/line-items", () => { |
|
671
|
let region |
|
672
|
|
|
673
|
const productData = { |
|
674
|
title: "Medusa T-Shirt", |
|
675
|
handle: "t-shirt", |
|
676
|
status: ProductStatus.PUBLISHED, |
|
677
|
options: [ |
|
678
|
{ |
|
679
|
title: "Size", |
|
680
|
values: ["S"], |
|
681
|
}, |
|
682
|
{ |
|
683
|
title: "Color", |
|
684
|
values: ["Black", "White"], |
|
685
|
}, |
|
686
|
], |
|
687
|
variants: [ |
|
688
|
{ |
|
689
|
title: "S / Black", |
|
690
|
sku: "SHIRT-S-BLACK", |
|
691
|
options: { |
|
692
|
Size: "S", |
|
693
|
Color: "Black", |
|
694
|
}, |
|
695
|
manage_inventory: false, |
|
696
|
prices: [ |
|
697
|
{ |
|
698
|
amount: 1500, |
|
699
|
currency_code: "usd", |
|
700
|
}, |
|
701
|
], |
|
702
|
}, |
|
703
|
{ |
|
704
|
title: "S / White", |
|
705
|
sku: "SHIRT-S-WHITE", |
|
706
|
options: { |
|
707
|
Size: "S", |
|
708
|
Color: "White", |
|
709
|
}, |
|
710
|
manage_inventory: false, |
|
711
|
prices: [ |
|
712
|
{ |
|
713
|
amount: 1500, |
|
714
|
currency_code: "usd", |
|
715
|
}, |
|
716
|
], |
|
717
|
}, |
|
718
|
], |
|
719
|
} |
|
720
|
|
|
721
|
beforeEach(async () => { |
|
722
|
await setupTaxStructure(taxModule) |
|
723
|
|
|
724
|
region = await regionModule.createRegions({ |
|
725
|
name: "US", |
|
726
|
currency_code: "usd", |
|
727
|
}) |
|
728
|
}) |
|
729
|
|
|
730
|
it("handle line item quantity edge cases", async () => { |
|
731
|
const shippingProfile = |
|
732
|
await fulfillmentModule.createShippingProfiles({ |
|
733
|
name: "Test", |
|
734
|
type: "default", |
|
735
|
}) |
|
736
|
|
|
737
|
const product = ( |
|
738
|
await api.post( |
|
739
|
`/admin/products`, |
|
740
|
{ |
|
741
|
...productData, |
|
742
|
shipping_profile_id: shippingProfile.id, |
|
743
|
}, |
|
744
|
adminHeaders |
|
745
|
) |
|
746
|
).data.product |
|
747
|
|
|
748
|
// cannot create a cart with a negative item quantity |
|
749
|
const errorRes = await api |
|
750
|
.post( |
|
751
|
`/store/carts`, |
|
752
|
{ |
|
753
|
email: "tony@stark.com", |
|
754
|
currency_code: region.currency_code, |
|
755
|
region_id: region.id, |
|
756
|
items: [ |
|
757
|
{ |
|
758
|
variant_id: product.variants[0].id, |
|
759
|
quantity: -2, |
|
760
|
}, |
|
761
|
], |
|
762
|
}, |
|
763
|
storeHeaders |
|
764
|
) |
|
765
|
.catch((e) => e) |
|
766
|
|
|
767
|
expect(errorRes.response.status).toEqual(400) |
|
768
|
expect(errorRes.response.data).toEqual({ |
|
769
|
message: |
|
770
|
"Invalid request: Value for field 'items, 0, quantity' too small, expected at least: '0'", |
|
771
|
type: "invalid_data", |
|
772
|
}) |
|
773
|
|
|
774
|
const cart = ( |
|
775
|
await api.post( |
|
776
|
`/store/carts`, |
|
777
|
{ |
|
778
|
email: "tony@stark.com", |
|
779
|
currency_code: region.currency_code, |
|
780
|
region_id: region.id, |
|
781
|
items: [ |
|
782
|
{ |
|
783
|
variant_id: product.variants[0].id, |
|
784
|
quantity: 5, |
|
785
|
}, |
|
786
|
], |
|
787
|
}, |
|
788
|
storeHeaders |
|
789
|
) |
|
790
|
).data.cart |
|
791
|
|
|
792
|
// cannot add a negative quantity item to the cart |
|
793
|
let response = await api |
|
794
|
.post( |
|
795
|
`/store/carts/${cart.id}/line-items`, |
|
796
|
{ |
|
797
|
variant_id: product.variants[1].id, |
|
798
|
quantity: -2, |
|
799
|
}, |
|
800
|
storeHeaders |
|
801
|
) |
|
802
|
.catch((e) => e) |
|
803
|
|
|
804
|
expect(response.response.status).toEqual(400) |
|
805
|
expect(response.response.data).toEqual({ |
|
806
|
message: |
|
807
|
"Invalid request: Value for field 'quantity' too small, expected at least: '0'", |
|
808
|
type: "invalid_data", |
|
809
|
}) |
|
810
|
|
|
811
|
// cannot update a negative quantity item on the cart |
|
812
|
response = await api |
|
813
|
.post( |
|
814
|
`/store/carts/${cart.id}/line-items/${cart.items[0].id}`, |
|
815
|
{ |
|
816
|
quantity: -1, |
|
817
|
}, |
|
818
|
storeHeaders |
|
819
|
) |
|
820
|
.catch((e) => e) |
|
821
|
|
|
822
|
expect(response.response.status).toEqual(400) |
|
823
|
expect(response.response.data).toEqual({ |
|
824
|
message: |
|
825
|
"Invalid request: Value for field 'quantity' too small, expected at least: '0'", |
|
826
|
type: "invalid_data", |
|
827
|
}) |
|
828
|
|
|
829
|
// should remove the item from the cart when quantity is 0 |
|
830
|
const cartResponse = await api.post( |
|
831
|
`/store/carts/${cart.id}/line-items/${cart.items[0].id}`, |
|
832
|
{ |
|
833
|
quantity: 0, |
|
834
|
}, |
|
835
|
storeHeaders |
|
836
|
) |
|
837
|
|
|
838
|
expect(cartResponse.status).toEqual(200) |
|
839
|
expect(cartResponse.data.cart).toEqual( |
|
840
|
expect.objectContaining({ |
|
841
|
items: expect.arrayContaining([]), |
|
842
|
}) |
|
843
|
) |
|
844
|
}) |
|
845
|
|
|
846
|
it("adding an existing variant should update or create line item depending on metadata", async () => { |
|
847
|
const shippingProfile = |
|
848
|
await fulfillmentModule.createShippingProfiles({ |
|
849
|
name: "Test", |
|
850
|
type: "default", |
|
851
|
}) |
|
852
|
|
|
853
|
const product = ( |
|
854
|
await api.post( |
|
855
|
`/admin/products`, |
|
856
|
{ |
|
857
|
...productData, |
|
858
|
shipping_profile_id: shippingProfile.id, |
|
859
|
}, |
|
860
|
adminHeaders |
|
861
|
) |
|
862
|
).data.product |
|
863
|
|
|
864
|
const cart = ( |
|
865
|
await api.post( |
|
866
|
`/store/carts`, |
|
867
|
{ |
|
868
|
email: "tony@stark.com", |
|
869
|
currency_code: region.currency_code, |
|
870
|
region_id: region.id, |
|
871
|
items: [ |
|
872
|
{ |
|
873
|
variant_id: product.variants[0].id, |
|
874
|
quantity: 1, |
|
875
|
metadata: { |
|
876
|
Size: "S", |
|
877
|
Color: "Black", |
|
878
|
}, |
|
879
|
}, |
|
880
|
], |
|
881
|
}, |
|
882
|
storeHeaders |
|
883
|
) |
|
884
|
).data.cart |
|
885
|
|
|
886
|
let response = await api.post( |
|
887
|
`/store/carts/${cart.id}/line-items`, |
|
888
|
{ |
|
889
|
variant_id: product.variants[0].id, |
|
890
|
quantity: 1, |
|
891
|
metadata: { |
|
892
|
Size: "S", |
|
893
|
Color: "Black", |
|
894
|
}, |
|
895
|
}, |
|
896
|
storeHeaders |
|
897
|
) |
|
898
|
|
|
899
|
expect(response.status).toEqual(200) |
|
900
|
expect(response.data.cart).toEqual( |
|
901
|
expect.objectContaining({ |
|
902
|
items: [ |
|
903
|
expect.objectContaining({ |
|
904
|
unit_price: 1500, |
|
905
|
quantity: 2, |
|
906
|
title: product.title, |
|
907
|
}), |
|
908
|
], |
|
909
|
subtotal: 3000, |
|
910
|
}) |
|
911
|
) |
|
912
|
|
|
913
|
response = await api.post( |
|
914
|
`/store/carts/${cart.id}/line-items`, |
|
915
|
{ |
|
916
|
variant_id: product.variants[0].id, |
|
917
|
quantity: 1, |
|
918
|
metadata: { |
|
919
|
Size: "S", |
|
920
|
Color: "White", |
|
921
|
Special: "attribute", |
|
922
|
}, |
|
923
|
}, |
|
924
|
storeHeaders |
|
925
|
) |
|
926
|
|
|
927
|
expect(response.status).toEqual(200) |
|
928
|
expect(response.data.cart.items).toHaveLength(2) |
|
929
|
expect(response.data.cart).toEqual( |
|
930
|
expect.objectContaining({ |
|
931
|
items: expect.arrayContaining([ |
|
932
|
expect.objectContaining({ |
|
933
|
unit_price: 1500, |
|
934
|
quantity: 2, |
|
935
|
title: product.title, |
|
936
|
}), |
|
937
|
expect.objectContaining({ |
|
938
|
unit_price: 1500, |
|
939
|
quantity: 1, |
|
940
|
title: product.title, |
|
941
|
}), |
|
942
|
]), |
|
943
|
subtotal: 4500, |
|
944
|
}) |
|
945
|
) |
|
946
|
}) |
|
947
|
}) |
|
948
|
|
|
949
|
describe("POST /store/payment-collections", () => { |
|
950
|
it("should create a payment collection for the cart", async () => { |
|
951
|
const region = await regionModule.createRegions({ |
|
952
|
name: "US", |
|
953
|
currency_code: "usd", |
|
954
|
}) |
|
955
|
|
|
956
|
const cart = await cartModule.createCarts({ |
|
957
|
currency_code: "usd", |
|
958
|
region_id: region.id, |
|
959
|
}) |
|
960
|
|
|
961
|
const response = await api.post( |
|
962
|
`/store/payment-collections`, |
|
963
|
{ cart_id: cart.id }, |
|
964
|
storeHeaders |
|
965
|
) |
|
966
|
|
|
967
|
expect(response.status).toEqual(200) |
|
968
|
expect(response.data.payment_collection).toEqual( |
|
969
|
expect.objectContaining({ |
|
970
|
id: expect.any(String), |
|
971
|
amount: 0, |
|
972
|
}) |
|
973
|
) |
|
974
|
}) |
|
975
|
|
|
976
|
it("should return an existing payment collection for the cart", async () => { |
|
977
|
const region = await regionModule.createRegions({ |
|
978
|
name: "US", |
|
979
|
currency_code: "usd", |
|
980
|
}) |
|
981
|
|
|
982
|
const cart = await cartModule.createCarts({ |
|
983
|
currency_code: "usd", |
|
984
|
region_id: region.id, |
|
985
|
}) |
|
986
|
|
|
987
|
const firstCollection = ( |
|
988
|
await api.post( |
|
989
|
`/store/payment-collections`, |
|
990
|
{ |
|
991
|
cart_id: cart.id, |
|
992
|
}, |
|
993
|
storeHeaders |
|
994
|
) |
|
995
|
).data.payment_collection |
|
996
|
|
|
997
|
const response = await api.post( |
|
998
|
`/store/payment-collections`, |
|
999
|
{ |
|
1000
|
cart_id: cart.id, |
|
1001
|
}, |
|
1002
|
storeHeaders |
|
1003
|
) |
|
1004
|
|
|
1005
|
expect(response.status).toEqual(200) |
|
1006
|
expect(response.data.payment_collection.id).toEqual( |
|
1007
|
firstCollection.id |
|
1008
|
) |
|
1009
|
}) |
|
1010
|
|
|
1011
|
it("should create a new payment collection for a new cart", async () => { |
|
1012
|
const region = await regionModule.createRegions({ |
|
1013
|
name: "US", |
|
1014
|
currency_code: "usd", |
|
1015
|
}) |
|
1016
|
|
|
1017
|
const firstCart = await cartModule.createCarts({ |
|
1018
|
currency_code: "usd", |
|
1019
|
region_id: region.id, |
|
1020
|
}) |
|
1021
|
|
|
1022
|
const secondCart = await cartModule.createCarts({ |
|
1023
|
currency_code: "usd", |
|
1024
|
region_id: region.id, |
|
1025
|
}) |
|
1026
|
|
|
1027
|
const firstCollection = ( |
|
1028
|
await api.post( |
|
1029
|
`/store/payment-collections`, |
|
1030
|
{ |
|
1031
|
cart_id: firstCart.id, |
|
1032
|
}, |
|
1033
|
storeHeaders |
|
1034
|
) |
|
1035
|
).data.payment_collection |
|
1036
|
|
|
1037
|
const secondCollection = ( |
|
1038
|
await api.post( |
|
1039
|
`/store/payment-collections`, |
|
1040
|
{ |
|
1041
|
cart_id: secondCart.id, |
|
1042
|
}, |
|
1043
|
storeHeaders |
|
1044
|
) |
|
1045
|
).data.payment_collection |
|
1046
|
|
|
1047
|
expect(firstCollection.id).toBeTruthy() |
|
1048
|
expect(firstCollection.id).not.toEqual(secondCollection.id) |
|
1049
|
}) |
|
1050
|
}) |
|
1051
|
|
|
1052
|
describe("POST /store/carts/:id/taxes", () => { |
|
1053
|
it("should update a carts tax lines when region.automatic_taxes is false", async () => { |
|
1054
|
await setupTaxStructure(taxModule) |
|
1055
|
|
|
1056
|
const region = await regionModule.createRegions({ |
|
1057
|
name: "US", |
|
1058
|
currency_code: "usd", |
|
1059
|
automatic_taxes: false, |
|
1060
|
countries: ["us"], |
|
1061
|
}) |
|
1062
|
|
|
1063
|
const cart = await cartModule.createCarts({ |
|
1064
|
currency_code: "usd", |
|
1065
|
region_id: region.id, |
|
1066
|
shipping_address: { |
|
1067
|
address_1: "test address 1", |
|
1068
|
address_2: "test address 2", |
|
1069
|
city: "NY", |
|
1070
|
country_code: "US", |
|
1071
|
province: "NY", |
|
1072
|
postal_code: "94016", |
|
1073
|
}, |
|
1074
|
items: [ |
|
1075
|
{ |
|
1076
|
id: "item-1", |
|
1077
|
unit_price: 2000, |
|
1078
|
quantity: 1, |
|
1079
|
title: "Test item", |
|
1080
|
product_id: "prod_tshirt", |
|
1081
|
} as any, |
|
1082
|
], |
|
1083
|
}) |
|
1084
|
|
|
1085
|
let updated = await api.post( |
|
1086
|
`/store/carts/${cart.id}/taxes`, |
|
1087
|
{}, |
|
1088
|
storeHeaders |
|
1089
|
) |
|
1090
|
|
|
1091
|
expect(updated.status).toEqual(200) |
|
1092
|
|
|
1093
|
// TODO: validate tax totals when calculations are ready |
|
1094
|
expect(updated.data.cart).toEqual( |
|
1095
|
expect.objectContaining({ |
|
1096
|
id: cart.id, |
|
1097
|
items: [ |
|
1098
|
expect.objectContaining({ |
|
1099
|
id: "item-1", |
|
1100
|
tax_lines: [ |
|
1101
|
expect.objectContaining({ |
|
1102
|
description: "NY Default Rate", |
|
1103
|
code: "NYDEFAULT", |
|
1104
|
rate: 6, |
|
1105
|
provider_id: "system", |
|
1106
|
}), |
|
1107
|
], |
|
1108
|
adjustments: [], |
|
1109
|
}), |
|
1110
|
], |
|
1111
|
}) |
|
1112
|
) |
|
1113
|
}) |
|
1114
|
|
|
1115
|
it("should update a carts tax lines with a reduced product type rate", async () => { |
|
1116
|
await setupTaxStructure(taxModule) |
|
1117
|
|
|
1118
|
const region = await regionModule.createRegions({ |
|
1119
|
name: "US", |
|
1120
|
currency_code: "usd", |
|
1121
|
automatic_taxes: false, |
|
1122
|
countries: ["ca"], |
|
1123
|
}) |
|
1124
|
|
|
1125
|
const cart = await cartModule.createCarts({ |
|
1126
|
currency_code: "usd", |
|
1127
|
region_id: region.id, |
|
1128
|
shipping_address: { |
|
1129
|
address_1: "test address 1", |
|
1130
|
address_2: "test address 2", |
|
1131
|
city: "CA", |
|
1132
|
country_code: "CA", |
|
1133
|
province: "QC", |
|
1134
|
postal_code: "94016", |
|
1135
|
}, |
|
1136
|
items: [ |
|
1137
|
{ |
|
1138
|
id: "item-1", |
|
1139
|
unit_price: 2000, |
|
1140
|
quantity: 1, |
|
1141
|
title: "Test item", |
|
1142
|
product_id: "prod_tshirt_reduced_tax", |
|
1143
|
product_type_id: "product_type_id_3", |
|
1144
|
} as any, |
|
1145
|
{ |
|
1146
|
id: "item-2", |
|
1147
|
unit_price: 1000, |
|
1148
|
quantity: 1, |
|
1149
|
title: "Test item two", |
|
1150
|
product_id: "prod_tshirt", |
|
1151
|
} as any, |
|
1152
|
], |
|
1153
|
}) |
|
1154
|
|
|
1155
|
let updated = await api.post( |
|
1156
|
`/store/carts/${cart.id}/taxes`, |
|
1157
|
{}, |
|
1158
|
storeHeaders |
|
1159
|
) |
|
1160
|
|
|
1161
|
expect(updated.status).toEqual(200) |
|
1162
|
|
|
1163
|
expect(updated.data.cart).toEqual( |
|
1164
|
expect.objectContaining({ |
|
1165
|
id: cart.id, |
|
1166
|
items: expect.arrayContaining([ |
|
1167
|
expect.objectContaining({ |
|
1168
|
id: "item-2", |
|
1169
|
tax_lines: expect.arrayContaining([ |
|
1170
|
// Regional rate for Quebec, Canada |
|
1171
|
expect.objectContaining({ |
|
1172
|
description: "QC Default Rate", |
|
1173
|
code: "QCDEFAULT", |
|
1174
|
rate: 2, |
|
1175
|
provider_id: "system", |
|
1176
|
}), |
|
1177
|
]), |
|
1178
|
adjustments: [], |
|
1179
|
}), |
|
1180
|
expect.objectContaining({ |
|
1181
|
id: "item-1", |
|
1182
|
tax_lines: expect.arrayContaining([ |
|
1183
|
// Reduced rate for product types in Quebec, Canada |
|
1184
|
expect.objectContaining({ |
|
1185
|
description: "QC Reduced Rate for Product Type", |
|
1186
|
code: "QCREDUCE_TYPE", |
|
1187
|
rate: 1, |
|
1188
|
provider_id: "system", |
|
1189
|
}), |
|
1190
|
]), |
|
1191
|
adjustments: [], |
|
1192
|
}), |
|
1193
|
]), |
|
1194
|
}) |
|
1195
|
) |
|
1196
|
}) |
|
1197
|
|
|
1198
|
it("should throw error when shipping is not present", async () => { |
|
1199
|
await setupTaxStructure(taxModule) |
|
1200
|
|
|
1201
|
const region = await regionModule.createRegions({ |
|
1202
|
name: "US", |
|
1203
|
currency_code: "usd", |
|
1204
|
automatic_taxes: false, |
|
1205
|
}) |
|
1206
|
|
|
1207
|
const cart = await cartModule.createCarts({ |
|
1208
|
currency_code: "usd", |
|
1209
|
region_id: region.id, |
|
1210
|
items: [ |
|
1211
|
{ |
|
1212
|
id: "item-1", |
|
1213
|
unit_price: 2000, |
|
1214
|
quantity: 1, |
|
1215
|
title: "Test item", |
|
1216
|
product_id: "prod_tshirt", |
|
1217
|
} as any, |
|
1218
|
], |
|
1219
|
}) |
|
1220
|
|
|
1221
|
let error = await api |
|
1222
|
.post(`/store/carts/${cart.id}/taxes`, {}, storeHeaders) |
|
1223
|
.catch((e) => e) |
|
1224
|
|
|
1225
|
expect(error.response.status).toEqual(400) |
|
1226
|
expect(error.response.data).toEqual({ |
|
1227
|
type: "invalid_data", |
|
1228
|
message: "country code is required to calculate taxes", |
|
1229
|
}) |
|
1230
|
}) |
|
1231
|
}) |
|
1232
|
|
|
1233
|
describe("POST /store/carts/:id/complete", () => { |
|
1234
|
let salesChannel |
|
1235
|
let product |
|
1236
|
let shippingProfile |
|
1237
|
let fulfillmentSet |
|
1238
|
let shippingOption |
|
1239
|
let paymentCollection |
|
1240
|
let paymentSession |
|
1241
|
let stockLocation |
|
1242
|
let inventoryItem |
|
1243
|
let promotion |
|
1244
|
|
|
1245
|
beforeEach(async () => { |
|
1246
|
await setupTaxStructure(taxModule) |
|
1247
|
|
|
1248
|
region = await regionService.createRegions({ |
|
1249
|
name: "Test region", |
|
1250
|
countries: ["US"], |
|
1251
|
currency_code: "usd", |
|
1252
|
}) |
|
1253
|
|
|
1254
|
await linkRegionPaymentProviders(appContainer, region.id) |
|
1255
|
|
|
1256
|
salesChannel = ( |
|
1257
|
await api.post( |
|
1258
|
"/admin/sales-channels", |
|
1259
|
{ name: "first channel", description: "channel" }, |
|
1260
|
adminHeaders |
|
1261
|
) |
|
1262
|
).data.sales_channel |
|
1263
|
|
|
1264
|
const salesChannel2 = ( |
|
1265
|
await api.post( |
|
1266
|
"/admin/sales-channels", |
|
1267
|
{ name: "second channel", description: "channel" }, |
|
1268
|
adminHeaders |
|
1269
|
) |
|
1270
|
).data.sales_channel |
|
1271
|
|
|
1272
|
stockLocation = ( |
|
1273
|
await api.post( |
|
1274
|
`/admin/stock-locations`, |
|
1275
|
{ name: "test location" }, |
|
1276
|
adminHeaders |
|
1277
|
) |
|
1278
|
).data.stock_location |
|
1279
|
|
|
1280
|
const stockLocation2 = ( |
|
1281
|
await api.post( |
|
1282
|
`/admin/stock-locations`, |
|
1283
|
{ name: "test location 2" }, |
|
1284
|
adminHeaders |
|
1285
|
) |
|
1286
|
).data.stock_location |
|
1287
|
|
|
1288
|
inventoryItem = ( |
|
1289
|
await api.post( |
|
1290
|
`/admin/inventory-items`, |
|
1291
|
{ |
|
1292
|
sku: "12345", |
|
1293
|
requires_shipping: false, |
|
1294
|
}, |
|
1295
|
adminHeaders |
|
1296
|
) |
|
1297
|
).data.inventory_item |
|
1298
|
|
|
1299
|
await api.post( |
|
1300
|
`/admin/inventory-items/${inventoryItem.id}/location-levels`, |
|
1301
|
{ |
|
1302
|
location_id: stockLocation.id, |
|
1303
|
stocked_quantity: 10, |
|
1304
|
}, |
|
1305
|
adminHeaders |
|
1306
|
) |
|
1307
|
|
|
1308
|
await api.post( |
|
1309
|
`/admin/inventory-items/${inventoryItem.id}/location-levels`, |
|
1310
|
{ |
|
1311
|
location_id: stockLocation2.id, |
|
1312
|
stocked_quantity: 10, |
|
1313
|
}, |
|
1314
|
adminHeaders |
|
1315
|
) |
|
1316
|
|
|
1317
|
await api.post( |
|
1318
|
`/admin/stock-locations/${stockLocation.id}/sales-channels`, |
|
1319
|
{ add: [salesChannel.id] }, |
|
1320
|
adminHeaders |
|
1321
|
) |
|
1322
|
|
|
1323
|
await api.post( |
|
1324
|
`/admin/stock-locations/${stockLocation2.id}/sales-channels`, |
|
1325
|
{ add: [salesChannel2.id] }, |
|
1326
|
adminHeaders |
|
1327
|
) |
|
1328
|
|
|
1329
|
shippingProfile = await fulfillmentModule.createShippingProfiles({ |
|
1330
|
name: "Test", |
|
1331
|
type: "default", |
|
1332
|
}) |
|
1333
|
|
|
1334
|
fulfillmentSet = await fulfillmentModule.createFulfillmentSets({ |
|
1335
|
name: "Test", |
|
1336
|
type: "test-type", |
|
1337
|
service_zones: [ |
|
1338
|
{ |
|
1339
|
name: "Test", |
|
1340
|
geo_zones: [{ type: "country", country_code: "us" }], |
|
1341
|
}, |
|
1342
|
], |
|
1343
|
}) |
|
1344
|
|
|
1345
|
product = ( |
|
1346
|
await api.post( |
|
1347
|
"/admin/products", |
|
1348
|
{ |
|
1349
|
title: "Test fixture", |
|
1350
|
status: ProductStatus.PUBLISHED, |
|
1351
|
options: [ |
|
1352
|
{ title: "size", values: ["large", "small"] }, |
|
1353
|
{ title: "color", values: ["green"] }, |
|
1354
|
], |
|
1355
|
variants: [ |
|
1356
|
{ |
|
1357
|
title: "Test variant", |
|
1358
|
inventory_items: [ |
|
1359
|
{ |
|
1360
|
inventory_item_id: inventoryItem.id, |
|
1361
|
required_quantity: 1, |
|
1362
|
}, |
|
1363
|
], |
|
1364
|
prices: [ |
|
1365
|
{ |
|
1366
|
currency_code: "usd", |
|
1367
|
amount: 100, |
|
1368
|
}, |
|
1369
|
], |
|
1370
|
options: { |
|
1371
|
size: "large", |
|
1372
|
color: "green", |
|
1373
|
}, |
|
1374
|
}, |
|
1375
|
], |
|
1376
|
}, |
|
1377
|
adminHeaders |
|
1378
|
) |
|
1379
|
).data.product |
|
1380
|
|
|
1381
|
await remoteLink.create([ |
|
1382
|
{ |
|
1383
|
[Modules.STOCK_LOCATION]: { |
|
1384
|
stock_location_id: stockLocation.id, |
|
1385
|
}, |
|
1386
|
[Modules.FULFILLMENT]: { |
|
1387
|
fulfillment_set_id: fulfillmentSet.id, |
|
1388
|
}, |
|
1389
|
}, |
|
1390
|
{ |
|
1391
|
[Modules.PRODUCT]: { |
|
1392
|
product_id: product.id, |
|
1393
|
}, |
|
1394
|
[Modules.SALES_CHANNEL]: { |
|
1395
|
sales_channel_id: salesChannel2.id, |
|
1396
|
}, |
|
1397
|
}, |
|
1398
|
// Add product to 2 sales channels to test that the right stock location is selected for reservations |
|
1399
|
{ |
|
1400
|
[Modules.PRODUCT]: { |
|
1401
|
product_id: product.id, |
|
1402
|
}, |
|
1403
|
[Modules.SALES_CHANNEL]: { |
|
1404
|
sales_channel_id: salesChannel.id, |
|
1405
|
}, |
|
1406
|
}, |
|
1407
|
]) |
|
1408
|
|
|
1409
|
promotion = ( |
|
1410
|
await api.post( |
|
1411
|
`/admin/promotions`, |
|
1412
|
{ |
|
1413
|
code: "TEST", |
|
1414
|
type: PromotionType.STANDARD, |
|
1415
|
status: PromotionStatus.ACTIVE, |
|
1416
|
is_automatic: true, |
|
1417
|
campaign: { |
|
1418
|
campaign_identifier: "test", |
|
1419
|
name: "test", |
|
1420
|
budget: { |
|
1421
|
type: "spend", |
|
1422
|
limit: 1000, |
|
1423
|
currency_code: region.currency_code, |
|
1424
|
}, |
|
1425
|
}, |
|
1426
|
application_method: { |
|
1427
|
target_type: "items", |
|
1428
|
type: "fixed", |
|
1429
|
allocation: "each", |
|
1430
|
currency_code: region.currency_code, |
|
1431
|
value: 10, |
|
1432
|
max_quantity: 1, |
|
1433
|
target_rules: [ |
|
1434
|
{ |
|
1435
|
attribute: "items.product_id", |
|
1436
|
operator: "eq", |
|
1437
|
values: [product.id], |
|
1438
|
}, |
|
1439
|
], |
|
1440
|
}, |
|
1441
|
rules: [], |
|
1442
|
}, |
|
1443
|
adminHeaders |
|
1444
|
) |
|
1445
|
).data.promotion |
|
1446
|
|
|
1447
|
await api.post( |
|
1448
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`, |
|
1449
|
{ add: ["manual_test-provider"] }, |
|
1450
|
adminHeaders |
|
1451
|
) |
|
1452
|
|
|
1453
|
shippingOption = ( |
|
1454
|
await api.post( |
|
1455
|
`/admin/shipping-options`, |
|
1456
|
{ |
|
1457
|
name: "Test shipping option", |
|
1458
|
service_zone_id: fulfillmentSet.service_zones[0].id, |
|
1459
|
shipping_profile_id: shippingProfile.id, |
|
1460
|
provider_id: "manual_test-provider", |
|
1461
|
price_type: "flat", |
|
1462
|
type: { |
|
1463
|
label: "Test type", |
|
1464
|
description: "Test description", |
|
1465
|
code: "test-code", |
|
1466
|
}, |
|
1467
|
prices: [ |
|
1468
|
{ currency_code: "usd", amount: 1000 }, |
|
1469
|
{ region_id: region.id, amount: 1100 }, |
|
1470
|
], |
|
1471
|
rules: [], |
|
1472
|
}, |
|
1473
|
adminHeaders |
|
1474
|
) |
|
1475
|
).data.shipping_option |
|
1476
|
|
|
1477
|
paymentCollection = await paymentService.createPaymentCollections({ |
|
1478
|
amount: 1000, |
|
1479
|
currency_code: "usd", |
|
1480
|
}) |
|
1481
|
|
|
1482
|
paymentSession = await paymentService.createPaymentSession( |
|
1483
|
paymentCollection.id, |
|
1484
|
{ |
|
1485
|
provider_id: "pp_system_default", |
|
1486
|
amount: 1000, |
|
1487
|
currency_code: "usd", |
|
1488
|
data: {}, |
|
1489
|
} |
|
1490
|
) |
|
1491
|
}) |
|
1492
|
|
|
1493
|
afterEach(async () => { |
|
1494
|
jest.clearAllMocks() |
|
1495
|
}) |
|
1496
|
|
|
1497
|
it("should create an order and create item reservations", async () => { |
|
1498
|
const cart = ( |
|
1499
|
await api.post( |
|
1500
|
`/store/carts`, |
|
1501
|
{ |
|
1502
|
currency_code: "usd", |
|
1503
|
region_id: region.id, |
|
1504
|
email: "tony@stark-industries.com", |
|
1505
|
shipping_address: { |
|
1506
|
address_1: "test address 1", |
|
1507
|
address_2: "test address 2", |
|
1508
|
city: "ny", |
|
1509
|
country_code: "us", |
|
1510
|
province: "ny", |
|
1511
|
postal_code: "94016", |
|
1512
|
}, |
|
1513
|
sales_channel_id: salesChannel.id, |
|
1514
|
items: [{ quantity: 1, variant_id: product.variants[0].id }], |
|
1515
|
}, |
|
1516
|
storeHeaders |
|
1517
|
) |
|
1518
|
).data.cart |
|
1519
|
|
|
1520
|
const paymentCollection = ( |
|
1521
|
await api.post( |
|
1522
|
`/store/payment-collections`, |
|
1523
|
{ cart_id: cart.id }, |
|
1524
|
storeHeaders |
|
1525
|
) |
|
1526
|
).data.payment_collection |
|
1527
|
|
|
1528
|
await api.post( |
|
1529
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
1530
|
{ provider_id: "pp_system_default" }, |
|
1531
|
storeHeaders |
|
1532
|
) |
|
1533
|
|
|
1534
|
const response = await api.post( |
|
1535
|
`/store/carts/${cart.id}/complete`, |
|
1536
|
{}, |
|
1537
|
storeHeaders |
|
1538
|
) |
|
1539
|
|
|
1540
|
expect(response.status).toEqual(200) |
|
1541
|
expect(response.data).toEqual({ |
|
1542
|
type: "order", |
|
1543
|
order: expect.objectContaining({ |
|
1544
|
id: expect.any(String), |
|
1545
|
total: 95.4, |
|
1546
|
subtotal: 100, |
|
1547
|
tax_total: 5.4, |
|
1548
|
discount_total: 10.6, |
|
1549
|
discount_subtotal: 10, |
|
1550
|
discount_tax_total: 0.6, |
|
1551
|
original_total: 106, |
|
1552
|
original_tax_total: 6, |
|
1553
|
item_total: 95.4, |
|
1554
|
item_subtotal: 100, |
|
1555
|
item_tax_total: 5.4, |
|
1556
|
original_item_total: 106, |
|
1557
|
original_item_subtotal: 100, |
|
1558
|
original_item_tax_total: 6, |
|
1559
|
shipping_total: 0, |
|
1560
|
shipping_subtotal: 0, |
|
1561
|
shipping_tax_total: 0, |
|
1562
|
original_shipping_tax_total: 0, |
|
1563
|
original_shipping_subtotal: 0, |
|
1564
|
original_shipping_total: 0, |
|
1565
|
items: [ |
|
1566
|
expect.objectContaining({ |
|
1567
|
id: expect.stringContaining("ordli_"), |
|
1568
|
product_id: product.id, |
|
1569
|
unit_price: 100, |
|
1570
|
quantity: 1, |
|
1571
|
tax_total: 5.4, |
|
1572
|
total: 95.4, |
|
1573
|
subtotal: 100, |
|
1574
|
original_total: 106, |
|
1575
|
discount_total: 10.6, |
|
1576
|
discount_subtotal: 10, |
|
1577
|
discount_tax_total: 0.6, |
|
1578
|
original_tax_total: 6, |
|
1579
|
tax_lines: [ |
|
1580
|
expect.objectContaining({ |
|
1581
|
rate: 6, |
|
1582
|
}), |
|
1583
|
], |
|
1584
|
adjustments: [ |
|
1585
|
expect.objectContaining({ |
|
1586
|
amount: 10, |
|
1587
|
promotion_id: promotion.id, |
|
1588
|
code: promotion.code, |
|
1589
|
subtotal: 10, |
|
1590
|
total: 10.6, |
|
1591
|
}), |
|
1592
|
], |
|
1593
|
}), |
|
1594
|
], |
|
1595
|
shipping_address: expect.objectContaining({ |
|
1596
|
city: "ny", |
|
1597
|
country_code: "us", |
|
1598
|
province: "ny", |
|
1599
|
postal_code: "94016", |
|
1600
|
}), |
|
1601
|
}), |
|
1602
|
}) |
|
1603
|
|
|
1604
|
promotion = ( |
|
1605
|
await api.get(`/admin/promotions/${promotion.id}`, adminHeaders) |
|
1606
|
).data.promotion |
|
1607
|
|
|
1608
|
expect(promotion.campaign.budget.used).toEqual(10) |
|
1609
|
|
|
1610
|
const reservation = await api.get(`/admin/reservations`, adminHeaders) |
|
1611
|
const reservationItem = reservation.data.reservations[0] |
|
1612
|
|
|
1613
|
expect(reservationItem).toEqual( |
|
1614
|
expect.objectContaining({ |
|
1615
|
id: expect.stringContaining("resitem_"), |
|
1616
|
location_id: stockLocation.id, |
|
1617
|
inventory_item_id: inventoryItem.id, |
|
1618
|
quantity: cart.items[0].quantity, |
|
1619
|
line_item_id: response.data.order.items[0].id, |
|
1620
|
}) |
|
1621
|
) |
|
1622
|
|
|
1623
|
const fullOrder = await api.get( |
|
1624
|
`/admin/orders/${response.data.order.id}`, |
|
1625
|
adminHeaders |
|
1626
|
) |
|
1627
|
const fullOrderDetail = fullOrder.data.order |
|
1628
|
|
|
1629
|
expect(fullOrderDetail).toEqual( |
|
1630
|
expect.objectContaining({ |
|
1631
|
payment_collections: [ |
|
1632
|
expect.objectContaining({ |
|
1633
|
currency_code: "usd", |
|
1634
|
amount: 95.4, |
|
1635
|
status: "authorized", |
|
1636
|
}), |
|
1637
|
], |
|
1638
|
}) |
|
1639
|
) |
|
1640
|
}) |
|
1641
|
|
|
1642
|
it("should throw an error when payment collection isn't created", async () => { |
|
1643
|
const cart = ( |
|
1644
|
await api.post( |
|
1645
|
`/store/carts`, |
|
1646
|
{ |
|
1647
|
currency_code: "usd", |
|
1648
|
region_id: region.id, |
|
1649
|
email: "tony@stark-industries.com", |
|
1650
|
sales_channel_id: salesChannel.id, |
|
1651
|
items: [{ quantity: 1, variant_id: product.variants[0].id }], |
|
1652
|
}, |
|
1653
|
storeHeaders |
|
1654
|
) |
|
1655
|
).data.cart |
|
1656
|
|
|
1657
|
const error = await api |
|
1658
|
.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
1659
|
.catch((e) => e) |
|
1660
|
|
|
1661
|
expect(error.response.status).toEqual(400) |
|
1662
|
expect(error.response.data).toEqual({ |
|
1663
|
type: "invalid_data", |
|
1664
|
message: "Payment collection has not been initiated for cart", |
|
1665
|
}) |
|
1666
|
}) |
|
1667
|
|
|
1668
|
it("should throw an error when payment collection isn't created", async () => { |
|
1669
|
const cart = ( |
|
1670
|
await api.post( |
|
1671
|
`/store/carts`, |
|
1672
|
{ |
|
1673
|
currency_code: "usd", |
|
1674
|
region_id: region.id, |
|
1675
|
email: "tony@stark-industries.com", |
|
1676
|
sales_channel_id: salesChannel.id, |
|
1677
|
items: [{ quantity: 1, variant_id: product.variants[0].id }], |
|
1678
|
}, |
|
1679
|
storeHeaders |
|
1680
|
) |
|
1681
|
).data.cart |
|
1682
|
|
|
1683
|
await api.post( |
|
1684
|
`/store/payment-collections`, |
|
1685
|
{ |
|
1686
|
cart_id: cart.id, |
|
1687
|
}, |
|
1688
|
storeHeaders |
|
1689
|
) |
|
1690
|
|
|
1691
|
const error = await api |
|
1692
|
.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
1693
|
.catch((e) => e) |
|
1694
|
|
|
1695
|
expect(error.response.status).toEqual(400) |
|
1696
|
expect(error.response.data).toEqual({ |
|
1697
|
type: "invalid_data", |
|
1698
|
message: "Payment sessions are required to complete cart", |
|
1699
|
}) |
|
1700
|
}) |
|
1701
|
|
|
1702
|
it("should fail to update cart when it is completed", async () => { |
|
1703
|
const cart = ( |
|
1704
|
await api.post( |
|
1705
|
`/store/carts`, |
|
1706
|
{ |
|
1707
|
currency_code: "usd", |
|
1708
|
region_id: region.id, |
|
1709
|
email: "tony@stark-industries.com", |
|
1710
|
shipping_address: { |
|
1711
|
address_1: "test address 1", |
|
1712
|
address_2: "test address 2", |
|
1713
|
city: "ny", |
|
1714
|
country_code: "us", |
|
1715
|
province: "ny", |
|
1716
|
postal_code: "94016", |
|
1717
|
}, |
|
1718
|
sales_channel_id: salesChannel.id, |
|
1719
|
items: [{ quantity: 1, variant_id: product.variants[0].id }], |
|
1720
|
}, |
|
1721
|
storeHeaders |
|
1722
|
) |
|
1723
|
).data.cart |
|
1724
|
|
|
1725
|
const paymentCollection = ( |
|
1726
|
await api.post( |
|
1727
|
`/store/payment-collections`, |
|
1728
|
{ |
|
1729
|
cart_id: cart.id, |
|
1730
|
}, |
|
1731
|
storeHeaders |
|
1732
|
) |
|
1733
|
).data.payment_collection |
|
1734
|
|
|
1735
|
await api.post( |
|
1736
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
1737
|
{ provider_id: "pp_system_default" }, |
|
1738
|
storeHeaders |
|
1739
|
) |
|
1740
|
|
|
1741
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
1742
|
|
|
1743
|
const cartRefetch = ( |
|
1744
|
await api.get(`/store/carts/${cart.id}`, storeHeaders) |
|
1745
|
).data.cart |
|
1746
|
|
|
1747
|
expect(cartRefetch.completed_at).toBeTruthy() |
|
1748
|
|
|
1749
|
await expect( |
|
1750
|
api.post( |
|
1751
|
`/store/carts/${cart.id}/shipping-methods`, |
|
1752
|
{ |
|
1753
|
option_id: shippingOption.id, |
|
1754
|
}, |
|
1755
|
storeHeaders |
|
1756
|
) |
|
1757
|
).rejects.toThrow() |
|
1758
|
|
|
1759
|
const error = await api |
|
1760
|
.post( |
|
1761
|
`/store/carts/${cart.id}/line-items`, |
|
1762
|
{ |
|
1763
|
variant_id: product.variants[0].id, |
|
1764
|
quantity: 1, |
|
1765
|
}, |
|
1766
|
storeHeaders |
|
1767
|
) |
|
1768
|
.catch((e) => e) |
|
1769
|
|
|
1770
|
expect(error.response.status).toEqual(400) |
|
1771
|
expect(error.response.data).toEqual({ |
|
1772
|
type: "invalid_data", |
|
1773
|
message: `Cart ${cart.id} is already completed.`, |
|
1774
|
}) |
|
1775
|
}) |
|
1776
|
|
|
1777
|
it("should create another guest customer and update the cart email when an different email is provided", async () => { |
|
1778
|
const cart = ( |
|
1779
|
await api.post( |
|
1780
|
`/store/carts`, |
|
1781
|
{ |
|
1782
|
currency_code: "usd", |
|
1783
|
region_id: region.id, |
|
1784
|
email: "tony@stark-industries.com", |
|
1785
|
shipping_address: { |
|
1786
|
address_1: "test address 1", |
|
1787
|
address_2: "test address 2", |
|
1788
|
city: "ny", |
|
1789
|
country_code: "us", |
|
1790
|
province: "ny", |
|
1791
|
postal_code: "94016", |
|
1792
|
}, |
|
1793
|
sales_channel_id: salesChannel.id, |
|
1794
|
}, |
|
1795
|
storeHeaders |
|
1796
|
) |
|
1797
|
).data.cart |
|
1798
|
|
|
1799
|
const originalCartCustomer = cart.customer |
|
1800
|
|
|
1801
|
// update the cart without providing an email |
|
1802
|
await api.post( |
|
1803
|
`/store/carts/${cart.id}`, |
|
1804
|
{ |
|
1805
|
metadata: { |
|
1806
|
test: "test updated", |
|
1807
|
}, |
|
1808
|
}, |
|
1809
|
storeHeaders |
|
1810
|
) |
|
1811
|
|
|
1812
|
let currentCart = await api.get( |
|
1813
|
`/store/carts/${cart.id}`, |
|
1814
|
storeHeaders |
|
1815
|
) |
|
1816
|
let currentCartCustomer = currentCart.data.cart.customer |
|
1817
|
|
|
1818
|
let customerData = ( |
|
1819
|
await api.get( |
|
1820
|
`/admin/customers/${currentCart.data.cart.customer_id}`, |
|
1821
|
adminHeaders |
|
1822
|
) |
|
1823
|
).data.customer |
|
1824
|
|
|
1825
|
expect(currentCartCustomer).toEqual(originalCartCustomer) |
|
1826
|
expect(customerData.email).toEqual(currentCart.data.cart.email) |
|
1827
|
expect(currentCart.data.cart.email).toEqual( |
|
1828
|
"tony@stark-industries.com" |
|
1829
|
) |
|
1830
|
expect(currentCart.data.cart.metadata).toEqual({ |
|
1831
|
test: "test updated", |
|
1832
|
}) |
|
1833
|
|
|
1834
|
// update the cart providing an email |
|
1835
|
await api.post( |
|
1836
|
`/store/carts/${cart.id}`, |
|
1837
|
{ |
|
1838
|
email: "foo@bar.com", |
|
1839
|
metadata: { |
|
1840
|
test: "test updated 2, new customer", |
|
1841
|
}, |
|
1842
|
}, |
|
1843
|
storeHeaders |
|
1844
|
) |
|
1845
|
|
|
1846
|
currentCart = await api.get(`/store/carts/${cart.id}`, storeHeaders) |
|
1847
|
|
|
1848
|
customerData = ( |
|
1849
|
await api.get( |
|
1850
|
`/admin/customers/${currentCart.data.cart.customer_id}`, |
|
1851
|
adminHeaders |
|
1852
|
) |
|
1853
|
).data.customer |
|
1854
|
|
|
1855
|
currentCartCustomer = currentCart.data.cart.customer |
|
1856
|
|
|
1857
|
expect(currentCartCustomer).not.toEqual(originalCartCustomer) |
|
1858
|
expect(currentCart.data.cart.customer_id).not.toEqual( |
|
1859
|
originalCartCustomer.id |
|
1860
|
) |
|
1861
|
expect(customerData.email).toEqual(currentCart.data.cart.email) |
|
1862
|
expect(currentCart.data.cart.email).toEqual("foo@bar.com") |
|
1863
|
expect(currentCart.data.cart.metadata).toEqual({ |
|
1864
|
test: "test updated 2, new customer", |
|
1865
|
}) |
|
1866
|
}) |
|
1867
|
|
|
1868
|
it("should update the cart email from one guest account to another", async () => { |
|
1869
|
const guestsMainEmail = "guest.main@acme.com" |
|
1870
|
const guestsSecondaryEmail = "guest.secondary@acme.com" |
|
1871
|
|
|
1872
|
const [guestMain, guestSecondary] = |
|
1873
|
await customerModule.createCustomers([ |
|
1874
|
{ |
|
1875
|
email: guestsMainEmail, |
|
1876
|
has_account: false, |
|
1877
|
}, |
|
1878
|
{ |
|
1879
|
email: guestsSecondaryEmail, |
|
1880
|
has_account: false, |
|
1881
|
}, |
|
1882
|
]) |
|
1883
|
|
|
1884
|
const cart = ( |
|
1885
|
await api.post( |
|
1886
|
`/store/carts`, |
|
1887
|
{ |
|
1888
|
currency_code: "usd", |
|
1889
|
region_id: region.id, |
|
1890
|
email: guestsSecondaryEmail, |
|
1891
|
shipping_address: { |
|
1892
|
address_1: "test address 1", |
|
1893
|
address_2: "test address 2", |
|
1894
|
city: "ny", |
|
1895
|
country_code: "us", |
|
1896
|
province: "ny", |
|
1897
|
postal_code: "94016", |
|
1898
|
}, |
|
1899
|
sales_channel_id: salesChannel.id, |
|
1900
|
}, |
|
1901
|
storeHeaders |
|
1902
|
) |
|
1903
|
).data.cart |
|
1904
|
|
|
1905
|
expect(cart.customer.id).toBe(guestSecondary.id) |
|
1906
|
|
|
1907
|
// update the cart without providing an email |
|
1908
|
await api.post( |
|
1909
|
`/store/carts/${cart.id}`, |
|
1910
|
{ |
|
1911
|
metadata: { |
|
1912
|
test: "test updated", |
|
1913
|
}, |
|
1914
|
}, |
|
1915
|
storeHeaders |
|
1916
|
) |
|
1917
|
|
|
1918
|
let currentCart = await api.get( |
|
1919
|
`/store/carts/${cart.id}`, |
|
1920
|
storeHeaders |
|
1921
|
) |
|
1922
|
let currentCartCustomer = currentCart.data.cart.customer |
|
1923
|
|
|
1924
|
expect(currentCartCustomer.id).toEqual(guestSecondary.id) |
|
1925
|
expect(currentCartCustomer.email).toEqual(guestSecondary.email) |
|
1926
|
expect(currentCart.data.cart.metadata).toEqual({ |
|
1927
|
test: "test updated", |
|
1928
|
}) |
|
1929
|
|
|
1930
|
// update the cart providing an email |
|
1931
|
await api.post( |
|
1932
|
`/store/carts/${cart.id}`, |
|
1933
|
{ |
|
1934
|
email: guestsMainEmail, |
|
1935
|
metadata: { |
|
1936
|
test: "test updated 2, new customer", |
|
1937
|
}, |
|
1938
|
}, |
|
1939
|
storeHeaders |
|
1940
|
) |
|
1941
|
|
|
1942
|
currentCart = await api.get(`/store/carts/${cart.id}`, storeHeaders) |
|
1943
|
|
|
1944
|
expect(currentCart.data.cart.customer.id).toEqual(guestMain.id) |
|
1945
|
expect(currentCart.data.cart.email).toEqual(guestMain.email) |
|
1946
|
expect(currentCart.data.cart.metadata).toEqual({ |
|
1947
|
test: "test updated 2, new customer", |
|
1948
|
}) |
|
1949
|
}) |
|
1950
|
|
|
1951
|
it("should persist customer on cart if updated with the same email", async () => { |
|
1952
|
const guestsMainEmail = "guest.main@acme.com" |
|
1953
|
|
|
1954
|
const cart = ( |
|
1955
|
await api.post( |
|
1956
|
`/store/carts`, |
|
1957
|
{ |
|
1958
|
currency_code: "usd", |
|
1959
|
region_id: region.id, |
|
1960
|
email: guestsMainEmail, |
|
1961
|
shipping_address: { |
|
1962
|
address_1: "test address 1", |
|
1963
|
address_2: "test address 2", |
|
1964
|
city: "ny", |
|
1965
|
country_code: "us", |
|
1966
|
province: "ny", |
|
1967
|
postal_code: "94016", |
|
1968
|
}, |
|
1969
|
sales_channel_id: salesChannel.id, |
|
1970
|
}, |
|
1971
|
storeHeaders |
|
1972
|
) |
|
1973
|
).data.cart |
|
1974
|
|
|
1975
|
const guestMain = cart.customer |
|
1976
|
|
|
1977
|
// update the cart providing an email |
|
1978
|
await api.post( |
|
1979
|
`/store/carts/${cart.id}`, |
|
1980
|
{ |
|
1981
|
email: guestsMainEmail, // update with the same mail |
|
1982
|
metadata: { |
|
1983
|
test: "test updated 2, same customer", |
|
1984
|
}, |
|
1985
|
}, |
|
1986
|
storeHeaders |
|
1987
|
) |
|
1988
|
|
|
1989
|
let currentCart = await api.get( |
|
1990
|
`/store/carts/${cart.id}`, |
|
1991
|
storeHeaders |
|
1992
|
) |
|
1993
|
|
|
1994
|
expect(currentCart.data.cart.customer.id).toEqual(guestMain.id) |
|
1995
|
expect(currentCart.data.cart.email).toEqual(guestMain.email) |
|
1996
|
expect(currentCart.data.cart.metadata).toEqual({ |
|
1997
|
test: "test updated 2, same customer", |
|
1998
|
}) |
|
1999
|
|
|
2000
|
// update the cart providing an email |
|
2001
|
await api.post( |
|
2002
|
`/store/carts/${cart.id}`, |
|
2003
|
{ |
|
2004
|
email: guestsMainEmail, // update with the same mail |
|
2005
|
metadata: { |
|
2006
|
test: "test updated 3, same customer", |
|
2007
|
}, |
|
2008
|
}, |
|
2009
|
storeHeaders |
|
2010
|
) |
|
2011
|
|
|
2012
|
currentCart = await api.get(`/store/carts/${cart.id}`, storeHeaders) |
|
2013
|
|
|
2014
|
expect(currentCart.data.cart.customer.id).toEqual(guestMain.id) |
|
2015
|
expect(currentCart.data.cart.email).toEqual(guestMain.email) |
|
2016
|
expect(currentCart.data.cart.metadata).toEqual({ |
|
2017
|
test: "test updated 3, same customer", |
|
2018
|
}) |
|
2019
|
}) |
|
2020
|
|
|
2021
|
it("should keep the same customer when updating the customer cart and update Cart's email if provided", async () => { |
|
2022
|
// create a customer |
|
2023
|
const mainEmail = "jhon.doe@acme.com" |
|
2024
|
await customerModule.createCustomers({ |
|
2025
|
email: mainEmail, |
|
2026
|
has_account: true, |
|
2027
|
}) |
|
2028
|
|
|
2029
|
const cart = ( |
|
2030
|
await api.post( |
|
2031
|
`/store/carts`, |
|
2032
|
{ |
|
2033
|
currency_code: "usd", |
|
2034
|
region_id: region.id, |
|
2035
|
email: mainEmail, |
|
2036
|
shipping_address: { |
|
2037
|
address_1: "test address 1", |
|
2038
|
address_2: "test address 2", |
|
2039
|
city: "ny", |
|
2040
|
country_code: "us", |
|
2041
|
province: "ny", |
|
2042
|
postal_code: "94016", |
|
2043
|
}, |
|
2044
|
sales_channel_id: salesChannel.id, |
|
2045
|
}, |
|
2046
|
storeHeaders |
|
2047
|
) |
|
2048
|
).data.cart |
|
2049
|
|
|
2050
|
const originalCartCustomer = cart.customer |
|
2051
|
|
|
2052
|
// update the cart without providing an email |
|
2053
|
await api.post( |
|
2054
|
`/store/carts/${cart.id}`, |
|
2055
|
{ |
|
2056
|
metadata: { |
|
2057
|
test: "test updated", |
|
2058
|
}, |
|
2059
|
}, |
|
2060
|
storeHeaders |
|
2061
|
) |
|
2062
|
|
|
2063
|
let currentCart = await api.get( |
|
2064
|
`/store/carts/${cart.id}`, |
|
2065
|
storeHeaders |
|
2066
|
) |
|
2067
|
let currentCartCustomer = currentCart.data.cart.customer |
|
2068
|
|
|
2069
|
let customerData = ( |
|
2070
|
await api.get( |
|
2071
|
`/admin/customers/${currentCart.data.cart.customer_id}`, |
|
2072
|
adminHeaders |
|
2073
|
) |
|
2074
|
).data.customer |
|
2075
|
|
|
2076
|
expect(currentCartCustomer).toEqual(originalCartCustomer) |
|
2077
|
expect(customerData.email).toEqual(currentCart.data.cart.email) |
|
2078
|
expect(currentCart.data.cart.email).toEqual(mainEmail) |
|
2079
|
expect(currentCart.data.cart.metadata).toEqual({ |
|
2080
|
test: "test updated", |
|
2081
|
}) |
|
2082
|
|
|
2083
|
// update the cart providing an email |
|
2084
|
await api.post( |
|
2085
|
`/store/carts/${cart.id}`, |
|
2086
|
{ |
|
2087
|
email: "foo@bar.com", |
|
2088
|
metadata: { |
|
2089
|
test: "test updated 2, new customer", |
|
2090
|
}, |
|
2091
|
}, |
|
2092
|
storeHeaders |
|
2093
|
) |
|
2094
|
|
|
2095
|
currentCart = await api.get(`/store/carts/${cart.id}`, storeHeaders) |
|
2096
|
|
|
2097
|
customerData = ( |
|
2098
|
await api.get( |
|
2099
|
`/admin/customers/${currentCart.data.cart.customer_id}`, |
|
2100
|
adminHeaders |
|
2101
|
) |
|
2102
|
).data.customer |
|
2103
|
|
|
2104
|
currentCartCustomer = currentCart.data.cart.customer |
|
2105
|
|
|
2106
|
expect(currentCartCustomer).toEqual(originalCartCustomer) |
|
2107
|
expect(currentCart.data.cart.customer_id).toEqual( |
|
2108
|
originalCartCustomer.id |
|
2109
|
) |
|
2110
|
expect(customerData.email).not.toEqual(currentCart.data.cart.email) |
|
2111
|
expect(customerData.email).toEqual(mainEmail) |
|
2112
|
expect(currentCart.data.cart.email).toEqual("foo@bar.com") |
|
2113
|
expect(currentCart.data.cart.metadata).toEqual({ |
|
2114
|
test: "test updated 2, new customer", |
|
2115
|
}) |
|
2116
|
}) |
|
2117
|
|
|
2118
|
it("should return order when cart is already completed", async () => { |
|
2119
|
const cart = ( |
|
2120
|
await api.post( |
|
2121
|
`/store/carts`, |
|
2122
|
{ |
|
2123
|
currency_code: "usd", |
|
2124
|
region_id: region.id, |
|
2125
|
email: "tony@stark-industries.com", |
|
2126
|
shipping_address: { |
|
2127
|
address_1: "test address 1", |
|
2128
|
address_2: "test address 2", |
|
2129
|
city: "ny", |
|
2130
|
country_code: "us", |
|
2131
|
province: "ny", |
|
2132
|
postal_code: "94016", |
|
2133
|
}, |
|
2134
|
sales_channel_id: salesChannel.id, |
|
2135
|
items: [{ quantity: 1, variant_id: product.variants[0].id }], |
|
2136
|
}, |
|
2137
|
storeHeaders |
|
2138
|
) |
|
2139
|
).data.cart |
|
2140
|
|
|
2141
|
const paymentCollection = ( |
|
2142
|
await api.post( |
|
2143
|
`/store/payment-collections`, |
|
2144
|
{ |
|
2145
|
cart_id: cart.id, |
|
2146
|
}, |
|
2147
|
storeHeaders |
|
2148
|
) |
|
2149
|
).data.payment_collection |
|
2150
|
|
|
2151
|
await api.post( |
|
2152
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
2153
|
{ provider_id: "pp_system_default" }, |
|
2154
|
storeHeaders |
|
2155
|
) |
|
2156
|
|
|
2157
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
2158
|
|
|
2159
|
const cartRefetch = ( |
|
2160
|
await api.get(`/store/carts/${cart.id}`, storeHeaders) |
|
2161
|
).data.cart |
|
2162
|
|
|
2163
|
expect(cartRefetch.completed_at).toBeTruthy() |
|
2164
|
|
|
2165
|
const order = await api.post( |
|
2166
|
`/store/carts/${cart.id}/complete`, |
|
2167
|
{}, |
|
2168
|
storeHeaders |
|
2169
|
) |
|
2170
|
|
|
2171
|
expect(order.status).toEqual(200) |
|
2172
|
expect(order.data).toEqual({ |
|
2173
|
type: "order", |
|
2174
|
order: expect.objectContaining({ |
|
2175
|
id: expect.any(String), |
|
2176
|
}), |
|
2177
|
}) |
|
2178
|
}) |
|
2179
|
|
|
2180
|
it("should return cart when payment authorization fails", async () => { |
|
2181
|
const paymentModuleService = appContainer.resolve(Modules.PAYMENT) |
|
2182
|
const authorizePaymentSessionSpy = jest.spyOn( |
|
2183
|
paymentModuleService, |
|
2184
|
"authorizePaymentSession" |
|
2185
|
) |
|
2186
|
|
|
2187
|
// Mock the authorizePaymentSession to throw error |
|
2188
|
authorizePaymentSessionSpy.mockImplementation( |
|
2189
|
(id, context, sharedContext) => { |
|
2190
|
throw new MedusaError( |
|
2191
|
MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, |
|
2192
|
`Payment authorization failed` |
|
2193
|
) |
|
2194
|
} |
|
2195
|
) |
|
2196
|
|
|
2197
|
const cart = ( |
|
2198
|
await api.post( |
|
2199
|
`/store/carts`, |
|
2200
|
{ |
|
2201
|
currency_code: "usd", |
|
2202
|
region_id: region.id, |
|
2203
|
email: "tony@stark-industries.com", |
|
2204
|
shipping_address: { |
|
2205
|
address_1: "test address 1", |
|
2206
|
address_2: "test address 2", |
|
2207
|
city: "ny", |
|
2208
|
country_code: "us", |
|
2209
|
province: "ny", |
|
2210
|
postal_code: "94016", |
|
2211
|
}, |
|
2212
|
sales_channel_id: salesChannel.id, |
|
2213
|
items: [{ quantity: 1, variant_id: product.variants[0].id }], |
|
2214
|
}, |
|
2215
|
storeHeaders |
|
2216
|
) |
|
2217
|
).data.cart |
|
2218
|
|
|
2219
|
const paymentCollection = ( |
|
2220
|
await api.post( |
|
2221
|
`/store/payment-collections`, |
|
2222
|
{ |
|
2223
|
cart_id: cart.id, |
|
2224
|
}, |
|
2225
|
storeHeaders |
|
2226
|
) |
|
2227
|
).data.payment_collection |
|
2228
|
|
|
2229
|
await api.post( |
|
2230
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
2231
|
{ provider_id: "pp_system_default" }, |
|
2232
|
storeHeaders |
|
2233
|
) |
|
2234
|
|
|
2235
|
const response = await api.post( |
|
2236
|
`/store/carts/${cart.id}/complete`, |
|
2237
|
{}, |
|
2238
|
storeHeaders |
|
2239
|
) |
|
2240
|
|
|
2241
|
expect(response.status).toEqual(200) |
|
2242
|
expect(response.data).toEqual({ |
|
2243
|
type: "cart", |
|
2244
|
cart: expect.objectContaining({}), |
|
2245
|
error: { |
|
2246
|
message: "Payment authorization failed", |
|
2247
|
name: "Error", |
|
2248
|
type: "payment_authorization_error", |
|
2249
|
}, |
|
2250
|
}) |
|
2251
|
}) |
|
2252
|
}) |
|
2253
|
}) |
|
2254
|
}, |
|
2255
|
}) |