|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { |
|
3
|
CampaignBudgetType, |
|
4
|
PromotionStatus, |
|
5
|
PromotionType, |
|
6
|
} from "@medusajs/utils" |
|
7
|
import { createAdminUser } from "../../../../helpers/create-admin-user" |
|
8
|
|
|
9
|
jest.setTimeout(50000) |
|
10
|
|
|
11
|
export const campaignData = { |
|
12
|
name: "campaign 1", |
|
13
|
description: "test description", |
|
14
|
campaign_identifier: "test-1", |
|
15
|
starts_at: new Date("01/01/2023").toISOString(), |
|
16
|
ends_at: new Date("01/01/2024").toISOString(), |
|
17
|
budget: { |
|
18
|
type: CampaignBudgetType.SPEND, |
|
19
|
limit: 1000, |
|
20
|
currency_code: "usd", |
|
21
|
}, |
|
22
|
} |
|
23
|
|
|
24
|
export const campaignsData = [ |
|
25
|
{ |
|
26
|
name: "campaign 1", |
|
27
|
description: "test description", |
|
28
|
campaign_identifier: "test-1", |
|
29
|
starts_at: new Date("01/01/2023"), |
|
30
|
ends_at: new Date("01/01/2024"), |
|
31
|
budget: { |
|
32
|
type: CampaignBudgetType.SPEND, |
|
33
|
limit: 1000, |
|
34
|
currency_code: "usd", |
|
35
|
}, |
|
36
|
}, |
|
37
|
{ |
|
38
|
name: "campaign 2", |
|
39
|
description: "test description", |
|
40
|
campaign_identifier: "test-2", |
|
41
|
starts_at: new Date("01/01/2023"), |
|
42
|
ends_at: new Date("01/01/2024"), |
|
43
|
budget: { |
|
44
|
type: CampaignBudgetType.USAGE, |
|
45
|
limit: 1000, |
|
46
|
}, |
|
47
|
}, |
|
48
|
] |
|
49
|
|
|
50
|
const promotionData = { |
|
51
|
code: "TEST", |
|
52
|
type: PromotionType.STANDARD, |
|
53
|
status: PromotionStatus.ACTIVE, |
|
54
|
is_automatic: true, |
|
55
|
application_method: { |
|
56
|
target_type: "items", |
|
57
|
type: "fixed", |
|
58
|
allocation: "each", |
|
59
|
currency_code: "usd", |
|
60
|
value: 100, |
|
61
|
max_quantity: 100, |
|
62
|
target_rules: [ |
|
63
|
{ |
|
64
|
attribute: "test.test", |
|
65
|
operator: "eq", |
|
66
|
values: ["test1", "test2"], |
|
67
|
}, |
|
68
|
], |
|
69
|
}, |
|
70
|
rules: [ |
|
71
|
{ |
|
72
|
attribute: "test.test", |
|
73
|
operator: "eq", |
|
74
|
values: ["test1", "test2"], |
|
75
|
}, |
|
76
|
], |
|
77
|
} |
|
78
|
|
|
79
|
const env = {} |
|
80
|
const adminHeaders = { |
|
81
|
headers: { "x-medusa-access-token": "test_token" }, |
|
82
|
} |
|
83
|
|
|
84
|
medusaIntegrationTestRunner({ |
|
85
|
env, |
|
86
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
87
|
describe("Admin Campaigns API", () => { |
|
88
|
let appContainer |
|
89
|
let campaign1 |
|
90
|
let campaign2 |
|
91
|
let promotion |
|
92
|
|
|
93
|
beforeAll(async () => { |
|
94
|
appContainer = getContainer() |
|
95
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
96
|
campaign1 = ( |
|
97
|
await api.post(`/admin/campaigns`, campaignsData[0], adminHeaders) |
|
98
|
).data.campaign |
|
99
|
campaign2 = ( |
|
100
|
await api.post(`/admin/campaigns`, campaignsData[1], adminHeaders) |
|
101
|
).data.campaign |
|
102
|
promotion = ( |
|
103
|
await api.post(`/admin/promotions`, promotionData, adminHeaders) |
|
104
|
).data.promotion |
|
105
|
|
|
106
|
await dbUtils.snapshot() |
|
107
|
}) |
|
108
|
|
|
109
|
const generatePromotionData = () => { |
|
110
|
const code = Math.random().toString(36).substring(7) |
|
111
|
|
|
112
|
return { |
|
113
|
code, |
|
114
|
type: PromotionType.STANDARD, |
|
115
|
status: PromotionStatus.ACTIVE, |
|
116
|
is_automatic: true, |
|
117
|
application_method: { |
|
118
|
target_type: "items", |
|
119
|
type: "fixed", |
|
120
|
allocation: "each", |
|
121
|
value: 100, |
|
122
|
max_quantity: 100, |
|
123
|
target_rules: [], |
|
124
|
currency_code: "usd", |
|
125
|
}, |
|
126
|
rules: [], |
|
127
|
} |
|
128
|
} |
|
129
|
|
|
130
|
describe("GET /admin/campaigns", () => { |
|
131
|
it("should get all campaigns and its count", async () => { |
|
132
|
const response = await api.get(`/admin/campaigns`, adminHeaders) |
|
133
|
|
|
134
|
expect(response.status).toEqual(200) |
|
135
|
expect(response.data.count).toEqual(2) |
|
136
|
expect(response.data.campaigns).toEqual( |
|
137
|
expect.arrayContaining([ |
|
138
|
expect.objectContaining({ |
|
139
|
id: campaign1.id, |
|
140
|
}), |
|
141
|
expect.objectContaining({ |
|
142
|
id: campaign2.id, |
|
143
|
}), |
|
144
|
]) |
|
145
|
) |
|
146
|
}) |
|
147
|
|
|
148
|
it("should support search on campaigns", async () => { |
|
149
|
const response = await api.get( |
|
150
|
`/admin/campaigns?q=ign%202`, |
|
151
|
adminHeaders |
|
152
|
) |
|
153
|
|
|
154
|
expect(response.status).toEqual(200) |
|
155
|
expect(response.data.campaigns).toEqual([ |
|
156
|
expect.objectContaining({ |
|
157
|
name: "campaign 2", |
|
158
|
}), |
|
159
|
]) |
|
160
|
}) |
|
161
|
|
|
162
|
it("should get all campaigns and its count filtered", async () => { |
|
163
|
const response = await api.get( |
|
164
|
`/admin/campaigns?fields=name,created_at,budget.id`, |
|
165
|
adminHeaders |
|
166
|
) |
|
167
|
|
|
168
|
expect(response.status).toEqual(200) |
|
169
|
expect(response.data.count).toEqual(2) |
|
170
|
expect(response.data.campaigns).toEqual( |
|
171
|
expect.arrayContaining([ |
|
172
|
{ |
|
173
|
id: expect.any(String), |
|
174
|
name: "campaign 1", |
|
175
|
created_at: expect.any(String), |
|
176
|
budget: { |
|
177
|
id: expect.any(String), |
|
178
|
}, |
|
179
|
}, |
|
180
|
{ |
|
181
|
id: expect.any(String), |
|
182
|
name: "campaign 2", |
|
183
|
created_at: expect.any(String), |
|
184
|
budget: { |
|
185
|
id: expect.any(String), |
|
186
|
}, |
|
187
|
}, |
|
188
|
]) |
|
189
|
) |
|
190
|
}) |
|
191
|
}) |
|
192
|
|
|
193
|
describe("GET /admin/campaigns/:id", () => { |
|
194
|
it("should throw an error if id does not exist", async () => { |
|
195
|
const { response } = await api |
|
196
|
.get(`/admin/campaigns/does-not-exist`, adminHeaders) |
|
197
|
.catch((e) => e) |
|
198
|
|
|
199
|
expect(response.status).toEqual(404) |
|
200
|
expect(response.data.message).toEqual( |
|
201
|
"Campaign with id: does-not-exist was not found" |
|
202
|
) |
|
203
|
}) |
|
204
|
|
|
205
|
it("should get the requested campaign", async () => { |
|
206
|
const response = await api.get( |
|
207
|
`/admin/campaigns/${campaign1.id}`, |
|
208
|
adminHeaders |
|
209
|
) |
|
210
|
|
|
211
|
expect(response.status).toEqual(200) |
|
212
|
expect(response.data.campaign).toEqual( |
|
213
|
expect.objectContaining({ |
|
214
|
id: campaign1.id, |
|
215
|
}) |
|
216
|
) |
|
217
|
}) |
|
218
|
|
|
219
|
it("should get the requested campaign with filtered fields and relations", async () => { |
|
220
|
const response = await api.get( |
|
221
|
`/admin/campaigns/${campaign1.id}?fields=name`, |
|
222
|
adminHeaders |
|
223
|
) |
|
224
|
|
|
225
|
expect(response.status).toEqual(200) |
|
226
|
expect(response.data.campaign).toEqual({ |
|
227
|
id: campaign1.id, |
|
228
|
name: campaign1.name, |
|
229
|
}) |
|
230
|
}) |
|
231
|
}) |
|
232
|
|
|
233
|
describe("POST /admin/campaigns", () => { |
|
234
|
it("should throw an error if required params are not passed", async () => { |
|
235
|
const { response } = await api |
|
236
|
.post(`/admin/campaigns`, {}, adminHeaders) |
|
237
|
.catch((e) => e) |
|
238
|
|
|
239
|
expect(response.status).toEqual(400) |
|
240
|
// expect(response.data.message).toEqual( |
|
241
|
// "name must be a string, name should not be empty" |
|
242
|
// ) |
|
243
|
}) |
|
244
|
|
|
245
|
it("should create a campaign successfully", async () => { |
|
246
|
const response = await api.post( |
|
247
|
`/admin/campaigns?fields=*promotions`, |
|
248
|
{ |
|
249
|
name: "test", |
|
250
|
campaign_identifier: "test", |
|
251
|
starts_at: new Date("01/01/2024").toISOString(), |
|
252
|
ends_at: new Date("01/01/2029").toISOString(), |
|
253
|
budget: { |
|
254
|
limit: 1000, |
|
255
|
type: "usage", |
|
256
|
}, |
|
257
|
}, |
|
258
|
adminHeaders |
|
259
|
) |
|
260
|
|
|
261
|
expect(response.status).toEqual(200) |
|
262
|
expect(response.data.campaign).toEqual( |
|
263
|
expect.objectContaining({ |
|
264
|
id: expect.any(String), |
|
265
|
name: "test", |
|
266
|
campaign_identifier: "test", |
|
267
|
starts_at: expect.any(String), |
|
268
|
ends_at: expect.any(String), |
|
269
|
budget: expect.objectContaining({ |
|
270
|
limit: 1000, |
|
271
|
type: "usage", |
|
272
|
}), |
|
273
|
}) |
|
274
|
) |
|
275
|
}) |
|
276
|
}) |
|
277
|
|
|
278
|
describe("POST /admin/campaigns/:id", () => { |
|
279
|
it("should throw an error if id does not exist", async () => { |
|
280
|
const { response } = await api |
|
281
|
.post(`/admin/campaigns/does-not-exist`, {}, adminHeaders) |
|
282
|
.catch((e) => e) |
|
283
|
|
|
284
|
expect(response.status).toEqual(404) |
|
285
|
expect(response.data.message).toEqual( |
|
286
|
`Campaign with id "does-not-exist" not found` |
|
287
|
) |
|
288
|
}) |
|
289
|
|
|
290
|
it("should update a campaign successfully", async () => { |
|
291
|
await api.post( |
|
292
|
`admin/campaigns/${campaign1.id}/promotions`, |
|
293
|
{ add: [promotion.id] }, |
|
294
|
adminHeaders |
|
295
|
) |
|
296
|
|
|
297
|
const response = await api.post( |
|
298
|
`/admin/campaigns/${campaign1.id}?fields=*promotions`, |
|
299
|
{ |
|
300
|
name: "test-update", |
|
301
|
campaign_identifier: "test-update", |
|
302
|
budget: { |
|
303
|
limit: 2000, |
|
304
|
}, |
|
305
|
}, |
|
306
|
adminHeaders |
|
307
|
) |
|
308
|
|
|
309
|
expect(response.status).toEqual(200) |
|
310
|
expect(response.data.campaign).toEqual( |
|
311
|
expect.objectContaining({ |
|
312
|
id: campaign1.id, |
|
313
|
name: "test-update", |
|
314
|
campaign_identifier: "test-update", |
|
315
|
budget: expect.objectContaining({ |
|
316
|
limit: 2000, |
|
317
|
type: "spend", |
|
318
|
}), |
|
319
|
promotions: [ |
|
320
|
expect.objectContaining({ |
|
321
|
id: promotion.id, |
|
322
|
}), |
|
323
|
], |
|
324
|
}) |
|
325
|
) |
|
326
|
}) |
|
327
|
}) |
|
328
|
|
|
329
|
describe("DELETE /admin/campaigns/:id", () => { |
|
330
|
it("should delete campaign successfully", async () => { |
|
331
|
const deleteRes = await api.delete( |
|
332
|
`/admin/campaigns/${campaign1.id}`, |
|
333
|
adminHeaders |
|
334
|
) |
|
335
|
|
|
336
|
expect(deleteRes.status).toEqual(200) |
|
337
|
|
|
338
|
const { response } = await api |
|
339
|
.post(`/admin/campaigns/${campaign1.id}`, {}, adminHeaders) |
|
340
|
.catch((e) => e) |
|
341
|
|
|
342
|
expect(response.status).toEqual(404) |
|
343
|
expect(response.data.message).toEqual( |
|
344
|
`Campaign with id "${campaign1.id}" not found` |
|
345
|
) |
|
346
|
}) |
|
347
|
}) |
|
348
|
|
|
349
|
describe("POST /admin/campaigns/:id/promotions", () => { |
|
350
|
it("should add or remove promotions from campaign", async () => { |
|
351
|
const promotion1 = ( |
|
352
|
await api.post( |
|
353
|
`/admin/promotions`, |
|
354
|
generatePromotionData(), |
|
355
|
adminHeaders |
|
356
|
) |
|
357
|
).data.promotion |
|
358
|
|
|
359
|
let response = await api.post( |
|
360
|
`/admin/campaigns/${campaign1.id}/promotions`, |
|
361
|
{ add: [promotion1.id, promotion.id] }, |
|
362
|
adminHeaders |
|
363
|
) |
|
364
|
|
|
365
|
expect(response.status).toEqual(200) |
|
366
|
expect(response.data.campaign).toEqual( |
|
367
|
expect.objectContaining({ |
|
368
|
id: expect.any(String), |
|
369
|
}) |
|
370
|
) |
|
371
|
|
|
372
|
response = await api.get( |
|
373
|
`/admin/promotions?campaign_id=${campaign1.id}`, |
|
374
|
adminHeaders |
|
375
|
) |
|
376
|
|
|
377
|
expect(response.status).toEqual(200) |
|
378
|
expect(response.data.promotions).toHaveLength(2) |
|
379
|
expect(response.data.promotions).toEqual( |
|
380
|
expect.arrayContaining([ |
|
381
|
expect.objectContaining({ |
|
382
|
id: promotion1.id, |
|
383
|
}), |
|
384
|
expect.objectContaining({ |
|
385
|
id: promotion.id, |
|
386
|
}), |
|
387
|
]) |
|
388
|
) |
|
389
|
|
|
390
|
await api.post( |
|
391
|
`/admin/campaigns/${campaign1.id}/promotions`, |
|
392
|
{ remove: [promotion1.id] }, |
|
393
|
adminHeaders |
|
394
|
) |
|
395
|
|
|
396
|
response = await api.get( |
|
397
|
`/admin/promotions?campaign_id=${campaign1.id}`, |
|
398
|
adminHeaders |
|
399
|
) |
|
400
|
|
|
401
|
expect(response.status).toEqual(200) |
|
402
|
expect(response.data.promotions).toHaveLength(1) |
|
403
|
expect(response.data.promotions).toEqual( |
|
404
|
expect.arrayContaining([ |
|
405
|
expect.objectContaining({ |
|
406
|
id: promotion.id, |
|
407
|
}), |
|
408
|
]) |
|
409
|
) |
|
410
|
}) |
|
411
|
}) |
|
412
|
}) |
|
413
|
}, |
|
414
|
}) |