|
1
|
import { IAuthModuleService } from "@medusajs/types" |
|
2
|
import { Modules } from "@medusajs/utils" |
|
3
|
import jwt from "jsonwebtoken" |
|
4
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
5
|
import { |
|
6
|
adminHeaders, |
|
7
|
createAdminUser, |
|
8
|
generatePublishableKey, |
|
9
|
generateStoreHeaders, |
|
10
|
} from "../../../../helpers/create-admin-user" |
|
11
|
|
|
12
|
jest.setTimeout(30000) |
|
13
|
|
|
14
|
medusaIntegrationTestRunner({ |
|
15
|
testSuite: ({ dbConnection, api, getContainer, dbUtils }) => { |
|
16
|
let customer1 |
|
17
|
let customer2 |
|
18
|
let customer3 |
|
19
|
let customer4 |
|
20
|
let customer5 |
|
21
|
let container |
|
22
|
let storeHeaders |
|
23
|
|
|
24
|
beforeAll(async () => { |
|
25
|
container = getContainer() |
|
26
|
await createAdminUser(dbConnection, adminHeaders, container) |
|
27
|
const publishableKey = await generatePublishableKey(container) |
|
28
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
29
|
|
|
30
|
customer1 = ( |
|
31
|
await api.post( |
|
32
|
"/admin/customers", |
|
33
|
{ |
|
34
|
email: "test1@email.com", |
|
35
|
}, |
|
36
|
adminHeaders |
|
37
|
) |
|
38
|
).data.customer |
|
39
|
|
|
40
|
customer2 = ( |
|
41
|
await api.post( |
|
42
|
"/admin/customers", |
|
43
|
{ |
|
44
|
email: "test2@email.com", |
|
45
|
}, |
|
46
|
adminHeaders |
|
47
|
) |
|
48
|
).data.customer |
|
49
|
|
|
50
|
customer3 = ( |
|
51
|
await api.post( |
|
52
|
"/admin/customers", |
|
53
|
{ |
|
54
|
email: "test3@email.com", |
|
55
|
}, |
|
56
|
adminHeaders |
|
57
|
) |
|
58
|
).data.customer |
|
59
|
|
|
60
|
customer4 = ( |
|
61
|
await api.post( |
|
62
|
"/admin/customers", |
|
63
|
{ |
|
64
|
email: "test4@email.com", |
|
65
|
}, |
|
66
|
adminHeaders |
|
67
|
) |
|
68
|
).data.customer |
|
69
|
|
|
70
|
await dbUtils.snapshot() |
|
71
|
}) |
|
72
|
|
|
73
|
describe("GET /admin/customers", () => { |
|
74
|
it("should list customers and query count", async () => { |
|
75
|
const response = await api |
|
76
|
.get("/admin/customers", adminHeaders) |
|
77
|
.catch((err) => { |
|
78
|
console.log(err) |
|
79
|
}) |
|
80
|
|
|
81
|
expect(response.status).toEqual(200) |
|
82
|
expect(response.data.count).toEqual(4) |
|
83
|
expect(response.data.customers).toEqual([ |
|
84
|
expect.objectContaining({ |
|
85
|
email: "test1@email.com", |
|
86
|
}), |
|
87
|
expect.objectContaining({ |
|
88
|
email: "test2@email.com", |
|
89
|
}), |
|
90
|
expect.objectContaining({ |
|
91
|
email: "test3@email.com", |
|
92
|
}), |
|
93
|
expect.objectContaining({ |
|
94
|
email: "test4@email.com", |
|
95
|
// BREAKING: You cannot create customers with an account directly. This will happen upon customer registration. |
|
96
|
// has_account: true, |
|
97
|
}), |
|
98
|
]) |
|
99
|
}) |
|
100
|
|
|
101
|
it("should list customers in group and count", async () => { |
|
102
|
const customerGroup = ( |
|
103
|
await api.post( |
|
104
|
"/admin/customer-groups", |
|
105
|
{ |
|
106
|
name: "VIP", |
|
107
|
}, |
|
108
|
adminHeaders |
|
109
|
) |
|
110
|
).data.customer_group |
|
111
|
|
|
112
|
await api.post( |
|
113
|
`/admin/customer-groups/${customerGroup.id}/customers`, |
|
114
|
{ |
|
115
|
add: [customer1.id, customer2.id, customer3.id], |
|
116
|
}, |
|
117
|
adminHeaders |
|
118
|
) |
|
119
|
|
|
120
|
const response = await api.get( |
|
121
|
`/admin/customers?groups[]=${customerGroup.id}`, |
|
122
|
adminHeaders |
|
123
|
) |
|
124
|
|
|
125
|
expect(response.status).toEqual(200) |
|
126
|
expect(response.data.count).toEqual(3) |
|
127
|
expect(response.data.customers).toEqual([ |
|
128
|
expect.objectContaining({ |
|
129
|
id: customer1.id, |
|
130
|
}), |
|
131
|
expect.objectContaining({ |
|
132
|
id: customer2.id, |
|
133
|
}), |
|
134
|
expect.objectContaining({ |
|
135
|
id: customer3.id, |
|
136
|
}), |
|
137
|
]) |
|
138
|
}) |
|
139
|
|
|
140
|
it("should list customers with specific query", async () => { |
|
141
|
const response = await api.get("/admin/customers?q=est2@", adminHeaders) |
|
142
|
|
|
143
|
expect(response.status).toEqual(200) |
|
144
|
expect(response.data.count).toEqual(1) |
|
145
|
expect(response.data.customers).toEqual( |
|
146
|
expect.arrayContaining([ |
|
147
|
expect.objectContaining({ |
|
148
|
email: "test2@email.com", |
|
149
|
}), |
|
150
|
]) |
|
151
|
) |
|
152
|
}) |
|
153
|
|
|
154
|
it("should list customers with expand query", async () => { |
|
155
|
await api.post( |
|
156
|
`/admin/customers/${customer1.id}/addresses`, |
|
157
|
{ |
|
158
|
first_name: "Lebron", |
|
159
|
last_name: "James", |
|
160
|
}, |
|
161
|
adminHeaders |
|
162
|
) |
|
163
|
// BREAKING: Customers no longer carry shipping_addresses, they have addresses |
|
164
|
const response = await api.get( |
|
165
|
"/admin/customers?q=test1@email.com&fields=*addresses", |
|
166
|
adminHeaders |
|
167
|
) |
|
168
|
|
|
169
|
expect(response.status).toEqual(200) |
|
170
|
expect(response.data.count).toEqual(1) |
|
171
|
expect(response.data.customers).toEqual( |
|
172
|
expect.arrayContaining([ |
|
173
|
expect.objectContaining({ |
|
174
|
id: customer1.id, |
|
175
|
addresses: expect.arrayContaining([ |
|
176
|
expect.objectContaining({ |
|
177
|
first_name: "Lebron", |
|
178
|
last_name: "James", |
|
179
|
}), |
|
180
|
]), |
|
181
|
}), |
|
182
|
]) |
|
183
|
) |
|
184
|
}) |
|
185
|
}) |
|
186
|
|
|
187
|
describe("POST /admin/customers", () => { |
|
188
|
it("should create a customer", async () => { |
|
189
|
const response = await api.post( |
|
190
|
"/admin/customers", |
|
191
|
{ |
|
192
|
first_name: "newf", |
|
193
|
last_name: "newl", |
|
194
|
email: "new@email.com", |
|
195
|
metadata: { foo: "bar" }, |
|
196
|
}, |
|
197
|
adminHeaders |
|
198
|
) |
|
199
|
|
|
200
|
expect(response.status).toEqual(200) |
|
201
|
expect(response.data.customer).toEqual( |
|
202
|
expect.objectContaining({ |
|
203
|
first_name: "newf", |
|
204
|
last_name: "newl", |
|
205
|
email: "new@email.com", |
|
206
|
metadata: { foo: "bar" }, |
|
207
|
}) |
|
208
|
) |
|
209
|
}) |
|
210
|
}) |
|
211
|
|
|
212
|
describe("POST /admin/customers/:id", () => { |
|
213
|
it("should correctly update customer", async () => { |
|
214
|
const response = await api |
|
215
|
.post( |
|
216
|
`/admin/customers/${customer3.id}`, |
|
217
|
{ |
|
218
|
first_name: "newf", |
|
219
|
last_name: "newl", |
|
220
|
email: "new@email.com", |
|
221
|
metadata: { foo: "bar", bar: "bar", baz: "baz" }, |
|
222
|
}, |
|
223
|
adminHeaders |
|
224
|
) |
|
225
|
.catch((err) => { |
|
226
|
console.log(err) |
|
227
|
}) |
|
228
|
|
|
229
|
expect(response.status).toEqual(200) |
|
230
|
expect(response.data.customer).toEqual( |
|
231
|
expect.objectContaining({ |
|
232
|
first_name: "newf", |
|
233
|
last_name: "newl", |
|
234
|
email: "new@email.com", |
|
235
|
metadata: { foo: "bar", bar: "bar", baz: "baz" }, |
|
236
|
}) |
|
237
|
) |
|
238
|
}) |
|
239
|
|
|
240
|
it("should correctly update customer metadata", async () => { |
|
241
|
let response = await api.post( |
|
242
|
`/admin/customers/${customer3.id}`, |
|
243
|
{ |
|
244
|
first_name: "newf", |
|
245
|
last_name: "newl", |
|
246
|
email: "new@email.com", |
|
247
|
metadata: { foo: "bar", bar: "bar", baz: "baz" }, |
|
248
|
}, |
|
249
|
adminHeaders |
|
250
|
) |
|
251
|
|
|
252
|
expect(response.data.customer).toEqual( |
|
253
|
expect.objectContaining({ |
|
254
|
first_name: "newf", |
|
255
|
last_name: "newl", |
|
256
|
email: "new@email.com", |
|
257
|
metadata: { foo: "bar", bar: "bar", baz: "baz" }, |
|
258
|
}) |
|
259
|
) |
|
260
|
|
|
261
|
response = await api |
|
262
|
.post( |
|
263
|
`/admin/customers/${customer3.id}`, |
|
264
|
{ |
|
265
|
metadata: { foo: "", bar: "bar2", baz2: "baz2" }, |
|
266
|
}, |
|
267
|
adminHeaders |
|
268
|
) |
|
269
|
.catch((err) => { |
|
270
|
console.log(err) |
|
271
|
}) |
|
272
|
|
|
273
|
expect(response.status).toEqual(200) |
|
274
|
expect(response.data.customer).toEqual( |
|
275
|
expect.objectContaining({ |
|
276
|
first_name: "newf", |
|
277
|
last_name: "newl", |
|
278
|
email: "new@email.com", |
|
279
|
metadata: { bar: "bar2", baz: "baz", baz2: "baz2" }, |
|
280
|
}) |
|
281
|
) |
|
282
|
}) |
|
283
|
|
|
284
|
// BREAKING: You can no longer update groups on a customer directly. Use dedicated customer groups endpoint |
|
285
|
it("should fail when adding a customer group which doesn't exist", async () => { |
|
286
|
expect.assertions(3) |
|
287
|
|
|
288
|
await api |
|
289
|
.post( |
|
290
|
"/admin/customer-groups/does-not-exist/customers", |
|
291
|
{ |
|
292
|
add: [customer1.id], |
|
293
|
}, |
|
294
|
adminHeaders |
|
295
|
) |
|
296
|
.catch((error) => { |
|
297
|
expect(error.response.status).toEqual(404) |
|
298
|
expect(error.response.data.type).toEqual("not_found") |
|
299
|
expect(error.response.data.message).toEqual( |
|
300
|
"You tried to set relationship customer_group_id: does-not-exist, but such entity does not exist" |
|
301
|
) |
|
302
|
}) |
|
303
|
}) |
|
304
|
|
|
305
|
// BREAKING: You can no longer update groups on a customer directly. Use dedicated customer groups endpoint |
|
306
|
it("should correctly update customer groups", async () => { |
|
307
|
const customerGroup = ( |
|
308
|
await api.post( |
|
309
|
"/admin/customer-groups", |
|
310
|
{ |
|
311
|
name: "VIP", |
|
312
|
}, |
|
313
|
adminHeaders |
|
314
|
) |
|
315
|
).data.customer_group |
|
316
|
|
|
317
|
await api.post( |
|
318
|
`/admin/customer-groups/${customerGroup.id}/customers`, |
|
319
|
{ |
|
320
|
add: [customer3.id], |
|
321
|
}, |
|
322
|
adminHeaders |
|
323
|
) |
|
324
|
|
|
325
|
let response = await api.get( |
|
326
|
`/admin/customers/${customer3.id}?fields=*groups`, |
|
327
|
adminHeaders |
|
328
|
) |
|
329
|
|
|
330
|
expect(response.status).toEqual(200) |
|
331
|
expect(response.data.customer.groups).toEqual( |
|
332
|
expect.arrayContaining([ |
|
333
|
expect.objectContaining({ |
|
334
|
id: customerGroup.id, |
|
335
|
}), |
|
336
|
]) |
|
337
|
) |
|
338
|
|
|
339
|
// Adding a group to a customer with already existing groups. |
|
340
|
|
|
341
|
const otherCustomerGroup = ( |
|
342
|
await api.post( |
|
343
|
"/admin/customer-groups", |
|
344
|
{ |
|
345
|
name: "Other VIP", |
|
346
|
}, |
|
347
|
adminHeaders |
|
348
|
) |
|
349
|
).data.customer_group |
|
350
|
|
|
351
|
await api.post( |
|
352
|
`/admin/customer-groups/${otherCustomerGroup.id}/customers`, |
|
353
|
{ |
|
354
|
add: [customer3.id], |
|
355
|
}, |
|
356
|
adminHeaders |
|
357
|
) |
|
358
|
|
|
359
|
response = await api.get( |
|
360
|
`/admin/customers/${customer3.id}?fields=*groups`, |
|
361
|
adminHeaders |
|
362
|
) |
|
363
|
|
|
364
|
expect(response.status).toEqual(200) |
|
365
|
expect(response.data.customer.groups.length).toEqual(2) |
|
366
|
expect(response.data.customer.groups).toEqual( |
|
367
|
expect.arrayContaining([ |
|
368
|
expect.objectContaining({ |
|
369
|
id: customerGroup.id, |
|
370
|
}), |
|
371
|
expect.objectContaining({ |
|
372
|
id: otherCustomerGroup.id, |
|
373
|
}), |
|
374
|
]) |
|
375
|
) |
|
376
|
|
|
377
|
// Remove groups |
|
378
|
await api.post( |
|
379
|
`/admin/customer-groups/${customerGroup.id}/customers`, |
|
380
|
{ |
|
381
|
remove: [customer3.id], |
|
382
|
}, |
|
383
|
adminHeaders |
|
384
|
) |
|
385
|
await api.post( |
|
386
|
`/admin/customer-groups/${otherCustomerGroup.id}/customers`, |
|
387
|
{ |
|
388
|
remove: [customer3.id], |
|
389
|
}, |
|
390
|
adminHeaders |
|
391
|
) |
|
392
|
|
|
393
|
response = await api.get( |
|
394
|
`/admin/customers/${customer3.id}?fields=*groups`, |
|
395
|
adminHeaders |
|
396
|
) |
|
397
|
|
|
398
|
expect(response.status).toEqual(200) |
|
399
|
expect(response.data.customer.groups.length).toEqual(0) |
|
400
|
}) |
|
401
|
}) |
|
402
|
|
|
403
|
describe("POST /admin/customers/:id/customer-groups", () => { |
|
404
|
it("should batch add and remove customer to/from customer groups", async () => { |
|
405
|
const group1 = ( |
|
406
|
await api.post( |
|
407
|
"/admin/customer-groups", |
|
408
|
{ |
|
409
|
name: "VIP 1", |
|
410
|
}, |
|
411
|
adminHeaders |
|
412
|
) |
|
413
|
).data.customer_group |
|
414
|
|
|
415
|
const group2 = ( |
|
416
|
await api.post( |
|
417
|
"/admin/customer-groups", |
|
418
|
{ |
|
419
|
name: "VIP 2", |
|
420
|
}, |
|
421
|
adminHeaders |
|
422
|
) |
|
423
|
).data.customer_group |
|
424
|
|
|
425
|
const group3 = ( |
|
426
|
await api.post( |
|
427
|
"/admin/customer-groups", |
|
428
|
{ |
|
429
|
name: "VIP 3", |
|
430
|
}, |
|
431
|
adminHeaders |
|
432
|
) |
|
433
|
).data.customer_group |
|
434
|
|
|
435
|
// Add with cg endpoint so we can test remove |
|
436
|
await api.post( |
|
437
|
`/admin/customer-groups/${group1.id}/customers`, |
|
438
|
{ |
|
439
|
add: [customer1.id], |
|
440
|
}, |
|
441
|
adminHeaders |
|
442
|
) |
|
443
|
|
|
444
|
const response = await api.post( |
|
445
|
`/admin/customers/${customer1.id}/customer-groups?fields=groups.id`, |
|
446
|
{ |
|
447
|
remove: [group1.id], |
|
448
|
add: [group2.id, group3.id], |
|
449
|
}, |
|
450
|
adminHeaders |
|
451
|
) |
|
452
|
|
|
453
|
expect(response.status).toEqual(200) |
|
454
|
|
|
455
|
expect(response.data.customer.groups.length).toEqual(2) |
|
456
|
expect(response.data.customer.groups).toEqual( |
|
457
|
expect.arrayContaining([ |
|
458
|
expect.objectContaining({ id: group2.id }), |
|
459
|
expect.objectContaining({ id: group3.id }), |
|
460
|
]) |
|
461
|
) |
|
462
|
}) |
|
463
|
}) |
|
464
|
|
|
465
|
describe("GET /admin/customers/:id", () => { |
|
466
|
it("should fetch a customer", async () => { |
|
467
|
const response = await api.get( |
|
468
|
`/admin/customers/${customer1.id}`, |
|
469
|
adminHeaders |
|
470
|
) |
|
471
|
|
|
472
|
expect(response.status).toEqual(200) |
|
473
|
expect(response.data.customer).toEqual( |
|
474
|
expect.objectContaining({ |
|
475
|
id: customer1.id, |
|
476
|
}) |
|
477
|
) |
|
478
|
}) |
|
479
|
|
|
480
|
it("should fetch a customer with expand query", async () => { |
|
481
|
await api.post( |
|
482
|
`/admin/customers/${customer1.id}/addresses`, |
|
483
|
{ |
|
484
|
first_name: "Lebron", |
|
485
|
last_name: "James", |
|
486
|
is_default_billing: true, |
|
487
|
}, |
|
488
|
adminHeaders |
|
489
|
) |
|
490
|
|
|
491
|
// BREAKING: Customers no longer carry billing_address, they have addresses |
|
492
|
const response = await api.get( |
|
493
|
`/admin/customers/${customer1.id}?fields=*addresses,*groups`, |
|
494
|
adminHeaders |
|
495
|
) |
|
496
|
|
|
497
|
expect(response.status).toEqual(200) |
|
498
|
expect(response.data.customer).toEqual( |
|
499
|
expect.objectContaining({ |
|
500
|
id: customer1.id, |
|
501
|
addresses: [ |
|
502
|
expect.objectContaining({ |
|
503
|
is_default_billing: true, |
|
504
|
first_name: "Lebron", |
|
505
|
last_name: "James", |
|
506
|
}), |
|
507
|
], |
|
508
|
groups: [], |
|
509
|
}) |
|
510
|
) |
|
511
|
}) |
|
512
|
}) |
|
513
|
|
|
514
|
describe("DELETE /admin/customers/:id", () => { |
|
515
|
it("should delete a customer and its auth identity when the customer is the only associated actor", async () => { |
|
516
|
const email = "test@email.com" |
|
517
|
const registeredCustomerToken = ( |
|
518
|
await api.post("/auth/customer/emailpass/register", { |
|
519
|
email, |
|
520
|
password: "password", |
|
521
|
}) |
|
522
|
).data.token |
|
523
|
|
|
524
|
const customer = ( |
|
525
|
await api.post( |
|
526
|
"/store/customers", |
|
527
|
{ |
|
528
|
email, |
|
529
|
}, |
|
530
|
{ |
|
531
|
headers: { |
|
532
|
Authorization: `Bearer ${registeredCustomerToken}`, |
|
533
|
...storeHeaders.headers, |
|
534
|
}, |
|
535
|
} |
|
536
|
) |
|
537
|
).data.customer |
|
538
|
|
|
539
|
const { auth_identity_id } = jwt.decode(registeredCustomerToken) |
|
540
|
const authModule: IAuthModuleService = container.resolve(Modules.AUTH) |
|
541
|
|
|
542
|
const response = await api.delete( |
|
543
|
`/admin/customers/${customer.id}`, |
|
544
|
adminHeaders |
|
545
|
) |
|
546
|
|
|
547
|
expect(response.status).toEqual(200) |
|
548
|
expect(response.data).toEqual( |
|
549
|
expect.objectContaining({ |
|
550
|
id: customer.id, |
|
551
|
deleted: true, |
|
552
|
object: "customer", |
|
553
|
}) |
|
554
|
) |
|
555
|
|
|
556
|
// The customer was the only actor associated with the auth identity, so |
|
557
|
// the whole auth identity should be deleted. |
|
558
|
await expect( |
|
559
|
authModule.retrieveAuthIdentity(auth_identity_id) |
|
560
|
).rejects.toThrow() |
|
561
|
|
|
562
|
// And its provider identity should be cascade-deleted along with it. |
|
563
|
const providerIdentities = await authModule.listProviderIdentities({ |
|
564
|
entity_id: email, |
|
565
|
}) |
|
566
|
expect(providerIdentities).toHaveLength(0) |
|
567
|
}) |
|
568
|
|
|
569
|
it("should keep the auth identity when another non-null actor type is still associated", async () => { |
|
570
|
const email = "test-multi@email.com" |
|
571
|
const registeredCustomerToken = ( |
|
572
|
await api.post("/auth/customer/emailpass/register", { |
|
573
|
email, |
|
574
|
password: "password", |
|
575
|
}) |
|
576
|
).data.token |
|
577
|
|
|
578
|
const customer = ( |
|
579
|
await api.post( |
|
580
|
"/store/customers", |
|
581
|
{ |
|
582
|
email, |
|
583
|
}, |
|
584
|
{ |
|
585
|
headers: { |
|
586
|
Authorization: `Bearer ${registeredCustomerToken}`, |
|
587
|
...storeHeaders.headers, |
|
588
|
}, |
|
589
|
} |
|
590
|
) |
|
591
|
).data.customer |
|
592
|
|
|
593
|
const { auth_identity_id } = jwt.decode(registeredCustomerToken) |
|
594
|
const authModule: IAuthModuleService = container.resolve(Modules.AUTH) |
|
595
|
|
|
596
|
// Associate another (non-null) actor type with the same auth identity. |
|
597
|
await authModule.updateAuthIdentities({ |
|
598
|
id: auth_identity_id, |
|
599
|
app_metadata: { |
|
600
|
customer_id: customer.id, |
|
601
|
user_id: "user_123", |
|
602
|
}, |
|
603
|
}) |
|
604
|
|
|
605
|
const response = await api.delete( |
|
606
|
`/admin/customers/${customer.id}`, |
|
607
|
adminHeaders |
|
608
|
) |
|
609
|
|
|
610
|
expect(response.status).toEqual(200) |
|
611
|
|
|
612
|
// The auth identity must be kept since another actor is still associated. |
|
613
|
const authIdentity = await authModule.retrieveAuthIdentity( |
|
614
|
auth_identity_id |
|
615
|
) |
|
616
|
|
|
617
|
expect(authIdentity).toEqual( |
|
618
|
expect.objectContaining({ |
|
619
|
id: auth_identity_id, |
|
620
|
app_metadata: expect.objectContaining({ |
|
621
|
// The other actor type keeps its value... |
|
622
|
user_id: "user_123", |
|
623
|
// ...while the deleted actor's entry is set to null. |
|
624
|
customer_id: null, |
|
625
|
}), |
|
626
|
}) |
|
627
|
) |
|
628
|
|
|
629
|
// The provider identity should still exist since the auth identity was kept. |
|
630
|
const providerIdentities = await authModule.listProviderIdentities({ |
|
631
|
entity_id: email, |
|
632
|
}) |
|
633
|
expect(providerIdentities).toHaveLength(1) |
|
634
|
}) |
|
635
|
}) |
|
636
|
}, |
|
637
|
}) |