|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { |
|
3
|
adminHeaders, |
|
4
|
createAdminUser, |
|
5
|
} from "../../../../helpers/create-admin-user" |
|
6
|
|
|
7
|
jest.setTimeout(30000) |
|
8
|
|
|
9
|
medusaIntegrationTestRunner({ |
|
10
|
testSuite: ({ dbConnection, api, getContainer, dbUtils }) => { |
|
11
|
let customer1 |
|
12
|
let group |
|
13
|
|
|
14
|
beforeAll(async () => { |
|
15
|
const appContainer = getContainer() |
|
16
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
17
|
|
|
18
|
group = ( |
|
19
|
await api.post( |
|
20
|
"/admin/customer-groups", |
|
21
|
{ |
|
22
|
name: "vip-customers", |
|
23
|
metadata: { |
|
24
|
data1: "value1", |
|
25
|
}, |
|
26
|
}, |
|
27
|
adminHeaders |
|
28
|
) |
|
29
|
).data.customer_group |
|
30
|
|
|
31
|
customer1 = ( |
|
32
|
await api.post( |
|
33
|
"/admin/customers", |
|
34
|
{ |
|
35
|
email: "test1@email.com", |
|
36
|
}, |
|
37
|
adminHeaders |
|
38
|
) |
|
39
|
).data.customer |
|
40
|
|
|
41
|
await dbUtils.snapshot() |
|
42
|
}) |
|
43
|
|
|
44
|
describe("POST /admin/customer-groups", () => { |
|
45
|
it("creates customer group", async () => { |
|
46
|
const payload = { |
|
47
|
name: "test group", |
|
48
|
} |
|
49
|
|
|
50
|
const response = await api.post( |
|
51
|
"/admin/customer-groups", |
|
52
|
payload, |
|
53
|
adminHeaders |
|
54
|
) |
|
55
|
|
|
56
|
expect(response.status).toEqual(200) |
|
57
|
expect(response.data.customer_group).toEqual( |
|
58
|
expect.objectContaining({ |
|
59
|
name: "test group", |
|
60
|
}) |
|
61
|
) |
|
62
|
}) |
|
63
|
|
|
64
|
it("should fail to create duplicate customer group", async () => { |
|
65
|
expect.assertions(3) |
|
66
|
|
|
67
|
const payload = { |
|
68
|
name: "vip-customers", |
|
69
|
} |
|
70
|
|
|
71
|
await api |
|
72
|
.post("/admin/customer-groups", payload, adminHeaders) |
|
73
|
.catch((err) => { |
|
74
|
// BREAKING: Duplicate error is now 400 |
|
75
|
expect(err.response.status).toEqual(400) |
|
76
|
expect(err.response.data.type).toEqual("invalid_data") |
|
77
|
expect(err.response.data.message).toEqual( |
|
78
|
"Customer group with name: vip-customers, already exists." |
|
79
|
) |
|
80
|
}) |
|
81
|
}) |
|
82
|
}) |
|
83
|
|
|
84
|
describe("DELETE /admin/customer-groups", () => { |
|
85
|
it("should remove customer group", async () => { |
|
86
|
expect.assertions(3) |
|
87
|
|
|
88
|
const deleteResponse = await api.delete( |
|
89
|
`/admin/customer-groups/${group.id}`, |
|
90
|
adminHeaders |
|
91
|
) |
|
92
|
|
|
93
|
expect(deleteResponse.data).toEqual({ |
|
94
|
id: group.id, |
|
95
|
object: "customer_group", |
|
96
|
deleted: true, |
|
97
|
}) |
|
98
|
|
|
99
|
await api |
|
100
|
.get(`/admin/customer-groups/${group.id}`, adminHeaders) |
|
101
|
.catch((error) => { |
|
102
|
expect(error.response.data.type).toEqual("not_found") |
|
103
|
expect(error.response.data.message).toEqual( |
|
104
|
`Customer group with id: ${group.id} not found` |
|
105
|
) |
|
106
|
}) |
|
107
|
}) |
|
108
|
|
|
109
|
it("should remove customer group from customer upon deletion", async () => { |
|
110
|
expect.assertions(3) |
|
111
|
|
|
112
|
await api.post( |
|
113
|
`/admin/customer-groups/${group.id}/customers`, |
|
114
|
{ add: [customer1.id] }, |
|
115
|
adminHeaders |
|
116
|
) |
|
117
|
|
|
118
|
const customerPreDeletion = await api.get( |
|
119
|
`/admin/customers/${customer1.id}?fields=*groups`, |
|
120
|
adminHeaders |
|
121
|
) |
|
122
|
|
|
123
|
expect(customerPreDeletion.data.customer).toEqual( |
|
124
|
expect.objectContaining({ |
|
125
|
groups: [ |
|
126
|
expect.objectContaining({ |
|
127
|
id: group.id, |
|
128
|
}), |
|
129
|
], |
|
130
|
}) |
|
131
|
) |
|
132
|
|
|
133
|
const deleteResponse = await api |
|
134
|
.delete(`/admin/customer-groups/${group.id}`, adminHeaders) |
|
135
|
.catch((err) => console.log(err)) |
|
136
|
|
|
137
|
expect(deleteResponse.data).toEqual({ |
|
138
|
id: group.id, |
|
139
|
object: "customer_group", |
|
140
|
deleted: true, |
|
141
|
}) |
|
142
|
|
|
143
|
const customerRes = await api.get( |
|
144
|
`/admin/customers/${customer1.id}?fields=*groups`, |
|
145
|
adminHeaders |
|
146
|
) |
|
147
|
|
|
148
|
expect(customerRes.data.customer).toEqual( |
|
149
|
expect.objectContaining({ |
|
150
|
groups: [], |
|
151
|
}) |
|
152
|
) |
|
153
|
}) |
|
154
|
}) |
|
155
|
|
|
156
|
// BREAKING: This endpoint has been removed in favor of `GET /admin/customers?customer_group_id=...` |
|
157
|
// Keeping this test to keep a record of it |
|
158
|
describe("GET /admin/customer-groups/{id}/customers", () => { |
|
159
|
it("should list customers in group and count", async () => { |
|
160
|
await api.post( |
|
161
|
`/admin/customer-groups/${group.id}/customers`, |
|
162
|
{ add: [customer1.id] }, |
|
163
|
adminHeaders |
|
164
|
) |
|
165
|
|
|
166
|
const response = await api.get( |
|
167
|
`/admin/customers?groups=${group.id}`, |
|
168
|
adminHeaders |
|
169
|
) |
|
170
|
|
|
171
|
expect(response.status).toEqual(200) |
|
172
|
expect(response.data.count).toEqual(1) |
|
173
|
expect(response.data.customers).toEqual( |
|
174
|
expect.arrayContaining([ |
|
175
|
expect.objectContaining({ |
|
176
|
id: customer1.id, |
|
177
|
}), |
|
178
|
]) |
|
179
|
) |
|
180
|
}) |
|
181
|
}) |
|
182
|
|
|
183
|
describe("POST /admin/customer-groups/:id", () => { |
|
184
|
it("should update group name & metadata", async () => { |
|
185
|
const body = { |
|
186
|
name: "vip-customers-v2", |
|
187
|
metadata: { |
|
188
|
metaKey1: `metaValue1`, |
|
189
|
}, |
|
190
|
} |
|
191
|
|
|
192
|
const response = await api.post( |
|
193
|
`/admin/customer-groups/${group.id}`, |
|
194
|
body, |
|
195
|
adminHeaders |
|
196
|
) |
|
197
|
|
|
198
|
expect(response.status).toEqual(200) |
|
199
|
expect(response.data.customer_group).toEqual( |
|
200
|
expect.objectContaining({ |
|
201
|
id: group.id, |
|
202
|
name: "vip-customers-v2", |
|
203
|
metadata: { |
|
204
|
data1: "value1", |
|
205
|
metaKey1: `metaValue1`, |
|
206
|
}, |
|
207
|
}) |
|
208
|
) |
|
209
|
}) |
|
210
|
|
|
211
|
it("should delete `metadata` nested key", async () => { |
|
212
|
const body = { |
|
213
|
name: "vip-customers-v2", |
|
214
|
metadata: { |
|
215
|
data1: null, // delete |
|
216
|
data2: "val2", // insert |
|
217
|
}, |
|
218
|
} |
|
219
|
|
|
220
|
const response = await api.post( |
|
221
|
`/admin/customer-groups/${group.id}?fields=*customers`, |
|
222
|
body, |
|
223
|
adminHeaders |
|
224
|
) |
|
225
|
|
|
226
|
expect(response.status).toEqual(200) |
|
227
|
expect(response.data.customer_group).toEqual( |
|
228
|
expect.objectContaining({ |
|
229
|
id: group.id, |
|
230
|
name: "vip-customers-v2", |
|
231
|
metadata: { data1: null, data2: "val2" }, |
|
232
|
customers: [], |
|
233
|
}) |
|
234
|
) |
|
235
|
}) |
|
236
|
}) |
|
237
|
|
|
238
|
describe("GET /admin/customer-groups", () => { |
|
239
|
it("should retreive a list of customer groups", async () => { |
|
240
|
const response = await api.get( |
|
241
|
`/admin/customer-groups?fields=*customers`, |
|
242
|
adminHeaders |
|
243
|
) |
|
244
|
|
|
245
|
expect(response.status).toEqual(200) |
|
246
|
expect(response.data.count).toEqual(1) |
|
247
|
expect(response.data.customer_groups[0]).toEqual( |
|
248
|
expect.objectContaining({ id: group.id }) |
|
249
|
) |
|
250
|
expect(response.data.customer_groups[0]).toHaveProperty("customers") |
|
251
|
}) |
|
252
|
|
|
253
|
it("should retrieve a list of customer groups filtered by name using `q` param", async () => { |
|
254
|
const response = await api.get( |
|
255
|
`/admin/customer-groups?q=vip-customers`, |
|
256
|
adminHeaders |
|
257
|
) |
|
258
|
|
|
259
|
expect(response.status).toEqual(200) |
|
260
|
expect(response.data.count).toEqual(1) |
|
261
|
expect(response.data.customer_groups).toEqual([ |
|
262
|
expect.objectContaining({ id: group.id }), |
|
263
|
]) |
|
264
|
expect(response.data.customer_groups[0]).not.toHaveProperty("customers") |
|
265
|
}) |
|
266
|
}) |
|
267
|
|
|
268
|
describe("GET /admin/customer-groups/:id", () => { |
|
269
|
it("should get customer group", async () => { |
|
270
|
const response = await api.get( |
|
271
|
`/admin/customer-groups/${group.id}`, |
|
272
|
adminHeaders |
|
273
|
) |
|
274
|
|
|
275
|
expect(response.status).toEqual(200) |
|
276
|
expect(response.data.customer_group).toEqual( |
|
277
|
expect.objectContaining({ |
|
278
|
id: group.id, |
|
279
|
name: "vip-customers", |
|
280
|
}) |
|
281
|
) |
|
282
|
expect(response.data.customer_group).not.toHaveProperty("customers") |
|
283
|
}) |
|
284
|
|
|
285
|
it("gets customer group with `customers` prop", async () => { |
|
286
|
const response = await api.get( |
|
287
|
`/admin/customer-groups/${group.id}?fields=*customers`, |
|
288
|
adminHeaders |
|
289
|
) |
|
290
|
|
|
291
|
expect(response.status).toEqual(200) |
|
292
|
expect(response.data.customer_group).toEqual( |
|
293
|
expect.objectContaining({ |
|
294
|
id: group.id, |
|
295
|
name: "vip-customers", |
|
296
|
}) |
|
297
|
) |
|
298
|
expect(response.data.customer_group.customers).toEqual([]) |
|
299
|
}) |
|
300
|
|
|
301
|
it("throws error when a customer group doesn't exist", async () => { |
|
302
|
await api |
|
303
|
.get(`/admin/customer-groups/does-not-exist`, adminHeaders) |
|
304
|
.catch((err) => { |
|
305
|
expect(err.response.status).toEqual(404) |
|
306
|
expect(err.response.data.type).toEqual("not_found") |
|
307
|
expect(err.response.data.message).toEqual( |
|
308
|
`Customer group with id: does-not-exist not found` |
|
309
|
) |
|
310
|
}) |
|
311
|
}) |
|
312
|
}) |
|
313
|
}, |
|
314
|
}) |