|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { IRegionModuleService } from "@medusajs/types" |
|
3
|
import { Modules } from "@medusajs/utils" |
|
4
|
import { createAdminUser } from "../../../../helpers/create-admin-user" |
|
5
|
|
|
6
|
jest.setTimeout(50000) |
|
7
|
|
|
8
|
const env = {} |
|
9
|
const adminHeaders = { |
|
10
|
headers: { "x-medusa-access-token": "test_token" }, |
|
11
|
} |
|
12
|
|
|
13
|
medusaIntegrationTestRunner({ |
|
14
|
env, |
|
15
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
16
|
describe("Regions - Admin", () => { |
|
17
|
let appContainer |
|
18
|
let service: IRegionModuleService |
|
19
|
|
|
20
|
beforeAll(async () => { |
|
21
|
appContainer = getContainer() |
|
22
|
service = appContainer.resolve(Modules.REGION) |
|
23
|
}) |
|
24
|
|
|
25
|
beforeAll(async () => { |
|
26
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
27
|
|
|
28
|
await dbUtils.snapshot() |
|
29
|
}) |
|
30
|
|
|
31
|
it("should create, update, and delete a region", async () => { |
|
32
|
const created = await api.post( |
|
33
|
`/admin/regions`, |
|
34
|
{ |
|
35
|
name: "Test Region", |
|
36
|
currency_code: "usd", |
|
37
|
countries: ["us", "ca"], |
|
38
|
metadata: { foo: "bar" }, |
|
39
|
}, |
|
40
|
adminHeaders |
|
41
|
) |
|
42
|
|
|
43
|
expect(created.status).toEqual(200) |
|
44
|
expect(created.data.region).toEqual( |
|
45
|
expect.objectContaining({ |
|
46
|
id: created.data.region.id, |
|
47
|
name: "Test Region", |
|
48
|
currency_code: "usd", |
|
49
|
metadata: { foo: "bar" }, |
|
50
|
}) |
|
51
|
) |
|
52
|
expect( |
|
53
|
created.data.region.countries.map((c) => c.iso_2).sort() |
|
54
|
).toEqual(["ca", "us"]) |
|
55
|
|
|
56
|
const updated = await api.post( |
|
57
|
`/admin/regions/${created.data.region.id}`, |
|
58
|
{ |
|
59
|
name: "United States", |
|
60
|
currency_code: "usd", |
|
61
|
countries: ["us"], |
|
62
|
metadata: { foo: "baz" }, |
|
63
|
}, |
|
64
|
adminHeaders |
|
65
|
) |
|
66
|
|
|
67
|
expect(updated.status).toEqual(200) |
|
68
|
expect(updated.data.region).toEqual( |
|
69
|
expect.objectContaining({ |
|
70
|
id: updated.data.region.id, |
|
71
|
name: "United States", |
|
72
|
currency_code: "usd", |
|
73
|
metadata: { foo: "baz" }, |
|
74
|
}) |
|
75
|
) |
|
76
|
expect(updated.data.region.countries.map((c) => c.iso_2)).toEqual([ |
|
77
|
"us", |
|
78
|
]) |
|
79
|
|
|
80
|
const deleted = await api.delete( |
|
81
|
`/admin/regions/${updated.data.region.id}`, |
|
82
|
adminHeaders |
|
83
|
) |
|
84
|
|
|
85
|
expect(deleted.status).toEqual(200) |
|
86
|
expect(deleted.data).toEqual({ |
|
87
|
id: updated.data.region.id, |
|
88
|
object: "region", |
|
89
|
deleted: true, |
|
90
|
}) |
|
91
|
|
|
92
|
const deletedRegion = await service.retrieveRegion( |
|
93
|
updated.data.region.id, |
|
94
|
{ |
|
95
|
withDeleted: true, |
|
96
|
} |
|
97
|
) |
|
98
|
|
|
99
|
// @ts-ignore |
|
100
|
expect(deletedRegion.deleted_at).toBeTruthy() |
|
101
|
}) |
|
102
|
|
|
103
|
it("should create the region with the available payment providers if the providers exists", async () => { |
|
104
|
const paymentProviderId = "pp_system_default" |
|
105
|
|
|
106
|
const created = await api.post( |
|
107
|
`/admin/regions?fields=*payment_providers`, |
|
108
|
{ |
|
109
|
name: "Test Region", |
|
110
|
currency_code: "usd", |
|
111
|
countries: ["us", "ca"], |
|
112
|
metadata: { foo: "bar" }, |
|
113
|
payment_providers: [paymentProviderId], |
|
114
|
}, |
|
115
|
adminHeaders |
|
116
|
) |
|
117
|
|
|
118
|
expect(created.status).toEqual(200) |
|
119
|
expect(created.data.region).toEqual( |
|
120
|
expect.objectContaining({ |
|
121
|
id: created.data.region.id, |
|
122
|
name: "Test Region", |
|
123
|
currency_code: "usd", |
|
124
|
metadata: { foo: "bar" }, |
|
125
|
payment_providers: [ |
|
126
|
expect.objectContaining({ |
|
127
|
id: paymentProviderId, |
|
128
|
}), |
|
129
|
], |
|
130
|
}) |
|
131
|
) |
|
132
|
}) |
|
133
|
|
|
134
|
it("should update the region available payment providers", async () => { |
|
135
|
const paymentProviderId = "pp_system_default" |
|
136
|
const paymentProvider2Id = "pp_system_default_2" |
|
137
|
|
|
138
|
const created = await api.post( |
|
139
|
`/admin/regions`, |
|
140
|
{ |
|
141
|
name: "Test Region", |
|
142
|
currency_code: "usd", |
|
143
|
countries: ["us", "ca"], |
|
144
|
metadata: { foo: "bar" }, |
|
145
|
}, |
|
146
|
adminHeaders |
|
147
|
) |
|
148
|
|
|
149
|
/** |
|
150
|
* Assign a new payment provider |
|
151
|
*/ |
|
152
|
|
|
153
|
await api.post( |
|
154
|
`/admin/regions/${created.data.region.id}`, |
|
155
|
{ |
|
156
|
payment_providers: [paymentProviderId], |
|
157
|
}, |
|
158
|
adminHeaders |
|
159
|
) |
|
160
|
|
|
161
|
let regionResponse = await api.get( |
|
162
|
`/admin/regions/${created.data.region.id}?fields=*payment_providers`, |
|
163
|
adminHeaders |
|
164
|
) |
|
165
|
|
|
166
|
expect(regionResponse.status).toEqual(200) |
|
167
|
expect(regionResponse.data.region.payment_providers).toHaveLength(1) |
|
168
|
expect(regionResponse.data.region).toEqual( |
|
169
|
expect.objectContaining({ |
|
170
|
id: regionResponse.data.region.id, |
|
171
|
payment_providers: [ |
|
172
|
expect.objectContaining({ |
|
173
|
id: paymentProviderId, |
|
174
|
}), |
|
175
|
], |
|
176
|
}) |
|
177
|
) |
|
178
|
|
|
179
|
/** |
|
180
|
* Replace the region payment providers by a new one set of providers |
|
181
|
*/ |
|
182
|
|
|
183
|
await api.post( |
|
184
|
`/admin/regions/${created.data.region.id}`, |
|
185
|
{ |
|
186
|
payment_providers: [paymentProvider2Id], |
|
187
|
}, |
|
188
|
adminHeaders |
|
189
|
) |
|
190
|
|
|
191
|
regionResponse = await api.get( |
|
192
|
`/admin/regions/${created.data.region.id}?fields=*payment_providers`, |
|
193
|
adminHeaders |
|
194
|
) |
|
195
|
|
|
196
|
expect(regionResponse.status).toEqual(200) |
|
197
|
expect(regionResponse.data.region.payment_providers).toHaveLength(1) |
|
198
|
expect(regionResponse.data.region).toEqual( |
|
199
|
expect.objectContaining({ |
|
200
|
id: regionResponse.data.region.id, |
|
201
|
payment_providers: [ |
|
202
|
expect.objectContaining({ |
|
203
|
id: paymentProvider2Id, |
|
204
|
}), |
|
205
|
], |
|
206
|
}) |
|
207
|
) |
|
208
|
|
|
209
|
/** |
|
210
|
* Replace the region payment providers with both providers |
|
211
|
*/ |
|
212
|
|
|
213
|
await api.post( |
|
214
|
`/admin/regions/${created.data.region.id}`, |
|
215
|
{ |
|
216
|
payment_providers: [paymentProviderId, paymentProvider2Id], |
|
217
|
}, |
|
218
|
adminHeaders |
|
219
|
) |
|
220
|
|
|
221
|
regionResponse = await api.get( |
|
222
|
`/admin/regions/${created.data.region.id}?fields=*payment_providers`, |
|
223
|
adminHeaders |
|
224
|
) |
|
225
|
|
|
226
|
expect(regionResponse.status).toEqual(200) |
|
227
|
expect(regionResponse.data.region.payment_providers).toHaveLength(2) |
|
228
|
expect(regionResponse.data.region).toEqual( |
|
229
|
expect.objectContaining({ |
|
230
|
id: regionResponse.data.region.id, |
|
231
|
payment_providers: expect.arrayContaining([ |
|
232
|
expect.objectContaining({ |
|
233
|
id: paymentProvider2Id, |
|
234
|
}), |
|
235
|
expect.objectContaining({ |
|
236
|
id: paymentProviderId, |
|
237
|
}), |
|
238
|
]), |
|
239
|
}) |
|
240
|
) |
|
241
|
|
|
242
|
/** |
|
243
|
* Omitting payment_providers should leave the existing links untouched |
|
244
|
*/ |
|
245
|
|
|
246
|
await api.post( |
|
247
|
`/admin/regions/${created.data.region.id}`, |
|
248
|
{ |
|
249
|
name: "Test Region updated", |
|
250
|
}, |
|
251
|
adminHeaders |
|
252
|
) |
|
253
|
|
|
254
|
regionResponse = await api.get( |
|
255
|
`/admin/regions/${created.data.region.id}?fields=*payment_providers`, |
|
256
|
adminHeaders |
|
257
|
) |
|
258
|
|
|
259
|
expect(regionResponse.status).toEqual(200) |
|
260
|
expect(regionResponse.data.region.payment_providers).toHaveLength(2) |
|
261
|
|
|
262
|
/** |
|
263
|
* Passing an empty payment_providers list should clear all links |
|
264
|
*/ |
|
265
|
|
|
266
|
await api.post( |
|
267
|
`/admin/regions/${created.data.region.id}`, |
|
268
|
{ |
|
269
|
payment_providers: [], |
|
270
|
}, |
|
271
|
adminHeaders |
|
272
|
) |
|
273
|
|
|
274
|
regionResponse = await api.get( |
|
275
|
`/admin/regions/${created.data.region.id}?fields=*payment_providers`, |
|
276
|
adminHeaders |
|
277
|
) |
|
278
|
|
|
279
|
expect(regionResponse.status).toEqual(200) |
|
280
|
expect(regionResponse.data.region.payment_providers).toEqual([]) |
|
281
|
}) |
|
282
|
|
|
283
|
it("should throw on update if the given payment providers does not exists", async () => { |
|
284
|
const created = await api.post( |
|
285
|
`/admin/regions`, |
|
286
|
{ |
|
287
|
name: "Test Region", |
|
288
|
currency_code: "usd", |
|
289
|
countries: ["us", "ca"], |
|
290
|
metadata: { foo: "bar" }, |
|
291
|
}, |
|
292
|
adminHeaders |
|
293
|
) |
|
294
|
|
|
295
|
const updateErr = await api |
|
296
|
.post( |
|
297
|
`/admin/regions/${created.data.region.id}`, |
|
298
|
{ |
|
299
|
payment_providers: ["test"], |
|
300
|
}, |
|
301
|
adminHeaders |
|
302
|
) |
|
303
|
.catch((e) => e) |
|
304
|
|
|
305
|
expect(updateErr.response.status).toEqual(404) |
|
306
|
expect(updateErr.response.data.message).toEqual( |
|
307
|
"Payment providers with ids test not found or not enabled" |
|
308
|
) |
|
309
|
}) |
|
310
|
|
|
311
|
it("should throw on missing required properties in create", async () => { |
|
312
|
const err = await api |
|
313
|
.post(`/admin/regions`, {}, adminHeaders) |
|
314
|
.catch((e) => e) |
|
315
|
|
|
316
|
expect(err.response.status).toEqual(400) |
|
317
|
// expect(err.response.data.message).toEqual( |
|
318
|
// "name must be a string, currency_code must be a string" |
|
319
|
// ) |
|
320
|
}) |
|
321
|
|
|
322
|
it("should throw on unknown properties in create", async () => { |
|
323
|
const error = await api |
|
324
|
.post( |
|
325
|
`/admin/regions`, |
|
326
|
{ |
|
327
|
foo: "bar", |
|
328
|
currency_code: "usd", |
|
329
|
name: "Test Region", |
|
330
|
}, |
|
331
|
adminHeaders |
|
332
|
) |
|
333
|
.catch((e) => e) |
|
334
|
|
|
335
|
expect(error.response.status).toEqual(400) |
|
336
|
// expect(error.response.data.message).toEqual( |
|
337
|
// "property foo should not exist" |
|
338
|
// ) |
|
339
|
}) |
|
340
|
|
|
341
|
it("should throw on unknown properties in update", async () => { |
|
342
|
const created = await service.createRegions({ |
|
343
|
name: "Test Region", |
|
344
|
currency_code: "usd", |
|
345
|
}) |
|
346
|
|
|
347
|
const error = await api |
|
348
|
.post( |
|
349
|
`/admin/regions/${created.id}`, |
|
350
|
{ |
|
351
|
foo: "bar", |
|
352
|
currency_code: "usd", |
|
353
|
name: "Test Region", |
|
354
|
}, |
|
355
|
adminHeaders |
|
356
|
) |
|
357
|
.catch((e) => e) |
|
358
|
|
|
359
|
expect(error.response.status).toEqual(400) |
|
360
|
// expect(error.response.data.message).toEqual( |
|
361
|
// "property foo should not exist" |
|
362
|
// ) |
|
363
|
}) |
|
364
|
|
|
365
|
it("should get all regions and count", async () => { |
|
366
|
await service.createRegions([ |
|
367
|
{ |
|
368
|
name: "Test", |
|
369
|
currency_code: "usd", |
|
370
|
countries: ["jp"], |
|
371
|
metadata: { foo: "bar" }, |
|
372
|
}, |
|
373
|
]) |
|
374
|
|
|
375
|
const response = await api.get(`/admin/regions`, adminHeaders) |
|
376
|
|
|
377
|
expect(response.status).toEqual(200) |
|
378
|
expect(response.data.regions).toEqual([ |
|
379
|
expect.objectContaining({ |
|
380
|
id: expect.any(String), |
|
381
|
name: "Test", |
|
382
|
currency_code: "usd", |
|
383
|
metadata: { foo: "bar" }, |
|
384
|
}), |
|
385
|
]) |
|
386
|
expect(response.data.regions[0].countries.map((c) => c.iso_2)).toEqual([ |
|
387
|
"jp", |
|
388
|
]) |
|
389
|
}) |
|
390
|
|
|
391
|
it("should support searching of regions", async () => { |
|
392
|
await service.createRegions([ |
|
393
|
{ |
|
394
|
name: "APAC", |
|
395
|
currency_code: "usd", |
|
396
|
countries: ["jp"], |
|
397
|
}, |
|
398
|
{ |
|
399
|
name: "Europe", |
|
400
|
currency_code: "eur", |
|
401
|
countries: ["de"], |
|
402
|
}, |
|
403
|
]) |
|
404
|
|
|
405
|
const response = await api.get(`/admin/regions?q=eu`, adminHeaders) |
|
406
|
|
|
407
|
expect(response.status).toEqual(200) |
|
408
|
expect(response.data.regions).toEqual([ |
|
409
|
expect.objectContaining({ |
|
410
|
name: "Europe", |
|
411
|
currency_code: "eur", |
|
412
|
}), |
|
413
|
]) |
|
414
|
}) |
|
415
|
|
|
416
|
it("should get a region", async () => { |
|
417
|
const [region] = await service.createRegions([ |
|
418
|
{ |
|
419
|
name: "Test", |
|
420
|
currency_code: "usd", |
|
421
|
countries: ["jp"], |
|
422
|
metadata: { foo: "bar" }, |
|
423
|
}, |
|
424
|
]) |
|
425
|
|
|
426
|
const response = await api.get( |
|
427
|
`/admin/regions/${region.id}`, |
|
428
|
adminHeaders |
|
429
|
) |
|
430
|
|
|
431
|
expect(response.status).toEqual(200) |
|
432
|
expect(response.data.region).toEqual( |
|
433
|
expect.objectContaining({ |
|
434
|
id: region.id, |
|
435
|
name: "Test", |
|
436
|
currency_code: "usd", |
|
437
|
metadata: { foo: "bar" }, |
|
438
|
}) |
|
439
|
) |
|
440
|
expect(response.data.region.countries.map((c) => c.iso_2)).toEqual([ |
|
441
|
"jp", |
|
442
|
]) |
|
443
|
}) |
|
444
|
}) |
|
445
|
}, |
|
446
|
}) |