|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { SearchTypes } from "@medusajs/types" |
|
3
|
import { Modules } from "@medusajs/utils" |
|
4
|
import { |
|
5
|
adminHeaders, |
|
6
|
createAdminUser, |
|
7
|
generatePublishableKey, |
|
8
|
generateStoreHeaders, |
|
9
|
} from "../../../helpers/create-admin-user" |
|
10
|
|
|
11
|
jest.setTimeout(120000) |
|
12
|
|
|
13
|
/** |
|
14
|
* Covers POST /store/search. The app under test opts in `product` only — see its |
|
15
|
* `src/api/middlewares.ts`. Narrowing to published products in the key's sales |
|
16
|
* channels is the endpoint's own, since the index declares both fields. |
|
17
|
*/ |
|
18
|
medusaIntegrationTestRunner({ |
|
19
|
testSuite: ({ dbConnection, getContainer, api }) => { |
|
20
|
let searchModule: SearchTypes.ISearchModuleService |
|
21
|
let storeHeaders |
|
22
|
let salesChannel |
|
23
|
let otherSalesChannel |
|
24
|
let shirt |
|
25
|
let shoe |
|
26
|
let draft |
|
27
|
let hidden |
|
28
|
let customer |
|
29
|
|
|
30
|
const createProduct = async (data) => { |
|
31
|
const response = await api.post("/admin/products", data, adminHeaders) |
|
32
|
return response.data.product |
|
33
|
} |
|
34
|
|
|
35
|
const createSalesChannel = async (name, productIds) => { |
|
36
|
const { data } = await api.post( |
|
37
|
"/admin/sales-channels", |
|
38
|
{ name }, |
|
39
|
adminHeaders |
|
40
|
) |
|
41
|
|
|
42
|
await api.post( |
|
43
|
`/admin/sales-channels/${data.sales_channel.id}/products`, |
|
44
|
{ add: productIds }, |
|
45
|
adminHeaders |
|
46
|
) |
|
47
|
|
|
48
|
return data.sales_channel |
|
49
|
} |
|
50
|
|
|
51
|
const waitForIndexReady = async (name: string) => { |
|
52
|
const deadline = Date.now() + 10000 |
|
53
|
while (Date.now() < deadline) { |
|
54
|
const listed = await api.get("/admin/search-indexes", adminHeaders) |
|
55
|
const index = listed.data.search_indexes.find((i) => i.name === name) |
|
56
|
if (index?.status === "ready") { |
|
57
|
return index |
|
58
|
} |
|
59
|
await new Promise((resolve) => setTimeout(resolve, 100)) |
|
60
|
} |
|
61
|
throw new Error(`Index "${name}" did not become ready in time`) |
|
62
|
} |
|
63
|
|
|
64
|
beforeAll(async () => { |
|
65
|
const container = getContainer() |
|
66
|
await createAdminUser(dbConnection, adminHeaders, container) |
|
67
|
|
|
68
|
searchModule = container.resolve(Modules.SEARCH) |
|
69
|
|
|
70
|
shirt = await createProduct({ |
|
71
|
title: "Zephyr Shirt", |
|
72
|
handle: "zephyr-shirt", |
|
73
|
description: "A shirt for windy days", |
|
74
|
status: "published", |
|
75
|
options: [{ title: "Size", values: ["M"] }], |
|
76
|
variants: [ |
|
77
|
{ |
|
78
|
title: "Medium", |
|
79
|
sku: "ZEPHYR-M", |
|
80
|
options: { Size: "M" }, |
|
81
|
prices: [{ amount: 20, currency_code: "usd" }], |
|
82
|
}, |
|
83
|
], |
|
84
|
}) |
|
85
|
|
|
86
|
shoe = await createProduct({ |
|
87
|
title: "Aurora Shoe", |
|
88
|
handle: "aurora-shoe", |
|
89
|
description: "A shoe for long walks", |
|
90
|
status: "published", |
|
91
|
options: [{ title: "Size", values: ["42"] }], |
|
92
|
}) |
|
93
|
|
|
94
|
draft = await createProduct({ |
|
95
|
title: "Zephyr Draft", |
|
96
|
handle: "zephyr-draft", |
|
97
|
status: "draft", |
|
98
|
options: [{ title: "Size", values: ["M"] }], |
|
99
|
}) |
|
100
|
|
|
101
|
hidden = await createProduct({ |
|
102
|
title: "Zephyr Elsewhere", |
|
103
|
handle: "zephyr-elsewhere", |
|
104
|
status: "published", |
|
105
|
options: [{ title: "Size", values: ["M"] }], |
|
106
|
}) |
|
107
|
|
|
108
|
salesChannel = await createSalesChannel("Web", [ |
|
109
|
shirt.id, |
|
110
|
shoe.id, |
|
111
|
draft.id, |
|
112
|
]) |
|
113
|
otherSalesChannel = await createSalesChannel("Retail", [hidden.id]) |
|
114
|
|
|
115
|
customer = ( |
|
116
|
await api.post( |
|
117
|
"/admin/customers", |
|
118
|
{ |
|
119
|
email: "wilhelmina@zephyr.test", |
|
120
|
first_name: "Wilhelmina", |
|
121
|
last_name: "Zephyr", |
|
122
|
}, |
|
123
|
adminHeaders |
|
124
|
) |
|
125
|
).data.customer |
|
126
|
|
|
127
|
const publishableKey = await generatePublishableKey(container) |
|
128
|
await api.post( |
|
129
|
`/admin/api-keys/${publishableKey.id}/sales-channels`, |
|
130
|
{ add: [salesChannel.id] }, |
|
131
|
adminHeaders |
|
132
|
) |
|
133
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
134
|
|
|
135
|
// The sales channel links landed after the products were ingested off |
|
136
|
// their creation events, so the index is rebuilt from the seed. |
|
137
|
await searchModule.reindex() |
|
138
|
await waitForIndexReady("product") |
|
139
|
await waitForIndexReady("customer") |
|
140
|
}) |
|
141
|
|
|
142
|
const search = (body) => api.post("/store/search", body, storeHeaders) |
|
143
|
|
|
144
|
describe("POST /store/search", () => { |
|
145
|
it("returns one result per posted query, in order", async () => { |
|
146
|
const response = await search({ |
|
147
|
queries: [ |
|
148
|
{ entity: "product", filters: { handle: "zephyr-shirt" } }, |
|
149
|
{ entity: "product", filters: { handle: "aurora-shoe" } }, |
|
150
|
], |
|
151
|
}) |
|
152
|
|
|
153
|
expect(response.status).toEqual(200) |
|
154
|
expect(response.data.results).toHaveLength(2) |
|
155
|
expect(response.data.results[0].hits).toEqual([ |
|
156
|
expect.objectContaining({ |
|
157
|
id: shirt.id, |
|
158
|
document: expect.objectContaining({ |
|
159
|
id: shirt.id, |
|
160
|
title: "Zephyr Shirt", |
|
161
|
handle: "zephyr-shirt", |
|
162
|
}), |
|
163
|
}), |
|
164
|
]) |
|
165
|
expect(response.data.results[1].hits).toEqual([ |
|
166
|
expect.objectContaining({ id: shoe.id }), |
|
167
|
]) |
|
168
|
expect(response.data.results[0].metadata).toEqual( |
|
169
|
expect.objectContaining({ skip: 0, take: 20 }) |
|
170
|
) |
|
171
|
}) |
|
172
|
|
|
173
|
it("accepts a single query outside of a batch", async () => { |
|
174
|
const response = await search({ |
|
175
|
entity: "product", |
|
176
|
filters: { handle: "zephyr-shirt" }, |
|
177
|
}) |
|
178
|
|
|
179
|
expect(response.status).toEqual(200) |
|
180
|
expect(response.data.results).toHaveLength(1) |
|
181
|
expect(response.data.results[0].hits).toEqual([ |
|
182
|
expect.objectContaining({ id: shirt.id }), |
|
183
|
]) |
|
184
|
}) |
|
185
|
|
|
186
|
it("404s on a registered index the store hasn't opted in", async () => { |
|
187
|
const listed = await api.get( |
|
188
|
"/admin/search?q=wilhelmina&entity=customer", |
|
189
|
adminHeaders |
|
190
|
) |
|
191
|
expect(listed.data.results[0].data).toEqual([ |
|
192
|
expect.objectContaining({ id: customer.id }), |
|
193
|
]) |
|
194
|
|
|
195
|
const error = await search({ |
|
196
|
queries: [{ entity: "customer" }], |
|
197
|
}).catch((e) => e) |
|
198
|
|
|
199
|
expect(error.response.status).toEqual(404) |
|
200
|
expect(error.response.data.message).toEqual( |
|
201
|
'No search index named "customer"' |
|
202
|
) |
|
203
|
}) |
|
204
|
|
|
205
|
it("404s on an index that isn't registered", async () => { |
|
206
|
const error = await search({ |
|
207
|
queries: [{ entity: "nope" }], |
|
208
|
}).catch((e) => e) |
|
209
|
|
|
210
|
expect(error.response.status).toEqual(404) |
|
211
|
expect(error.response.data.message).toEqual( |
|
212
|
'No search index named "nope"' |
|
213
|
) |
|
214
|
}) |
|
215
|
|
|
216
|
it("narrows products to published ones the key can see", async () => { |
|
217
|
const response = await search({ |
|
218
|
queries: [{ entity: "product", filters: { q: "zephyr" } }], |
|
219
|
}) |
|
220
|
|
|
221
|
expect(response.status).toEqual(200) |
|
222
|
// "Zephyr Draft" is out on status, "Zephyr Elsewhere" on sales channel. |
|
223
|
expect(response.data.results[0].hits).toEqual([ |
|
224
|
expect.objectContaining({ id: shirt.id }), |
|
225
|
]) |
|
226
|
}) |
|
227
|
|
|
228
|
it("scopes products to the key's sales channels", async () => { |
|
229
|
const response = await search({ |
|
230
|
queries: [ |
|
231
|
{ entity: "product", filters: { handle: "zephyr-elsewhere" } }, |
|
232
|
], |
|
233
|
}) |
|
234
|
|
|
235
|
expect(response.status).toEqual(200) |
|
236
|
// Published, so only the sales channel it lives in excludes it. |
|
237
|
expect(response.data.results[0].hits).toEqual([]) |
|
238
|
}) |
|
239
|
|
|
240
|
it("can't widen past the endpoint's product filters", async () => { |
|
241
|
const response = await search({ |
|
242
|
queries: [ |
|
243
|
{ entity: "product", filters: { status: "draft" } }, |
|
244
|
{ |
|
245
|
entity: "product", |
|
246
|
filters: { sales_channel_ids: otherSalesChannel.id }, |
|
247
|
}, |
|
248
|
], |
|
249
|
}) |
|
250
|
|
|
251
|
expect(response.status).toEqual(200) |
|
252
|
expect(response.data.results[0].hits).toEqual([]) |
|
253
|
expect(response.data.results[1].hits).toEqual([]) |
|
254
|
}) |
|
255
|
|
|
256
|
it("returns exactly the fields the index can retrieve", async () => { |
|
257
|
const response = await search({ |
|
258
|
queries: [{ entity: "product", filters: { handle: "zephyr-shirt" } }], |
|
259
|
}) |
|
260
|
|
|
261
|
expect(response.status).toEqual(200) |
|
262
|
const [hit] = response.data.results[0].hits |
|
263
|
expect(Object.keys(hit.document).sort()).toEqual( |
|
264
|
searchModule.listRetrievableFields("product").sort() |
|
265
|
) |
|
266
|
expect(hit.document).toEqual({ |
|
267
|
id: shirt.id, |
|
268
|
title: "Zephyr Shirt", |
|
269
|
handle: "zephyr-shirt", |
|
270
|
status: "published", |
|
271
|
sales_channel_ids: [salesChannel.id], |
|
272
|
}) |
|
273
|
}) |
|
274
|
|
|
275
|
it("narrows the hits to the selected fields", async () => { |
|
276
|
const response = await search({ |
|
277
|
queries: [ |
|
278
|
{ |
|
279
|
entity: "product", |
|
280
|
filters: { handle: "zephyr-shirt" }, |
|
281
|
fields: ["id", "title"], |
|
282
|
}, |
|
283
|
], |
|
284
|
}) |
|
285
|
|
|
286
|
expect(response.status).toEqual(200) |
|
287
|
expect(response.data.results[0].hits[0].document).toEqual({ |
|
288
|
id: shirt.id, |
|
289
|
title: "Zephyr Shirt", |
|
290
|
}) |
|
291
|
}) |
|
292
|
|
|
293
|
it("rejects fields the index can't return", async () => { |
|
294
|
const batched = await search({ |
|
295
|
queries: [ |
|
296
|
{ |
|
297
|
entity: "product", |
|
298
|
filters: { handle: "zephyr-shirt" }, |
|
299
|
// On the product, but not on the index. |
|
300
|
fields: ["id", "description", "variants.*"], |
|
301
|
}, |
|
302
|
], |
|
303
|
}).catch((e) => e) |
|
304
|
|
|
305
|
expect(batched.response.status).toEqual(400) |
|
306
|
expect(batched.response.data.message).toEqual( |
|
307
|
`Search index "product" can't return the fields: description, variants.*` |
|
308
|
) |
|
309
|
|
|
310
|
const single = await search({ |
|
311
|
entity: "product", |
|
312
|
fields: ["variants.sku"], |
|
313
|
}).catch((e) => e) |
|
314
|
|
|
315
|
expect(single.response.status).toEqual(400) |
|
316
|
}) |
|
317
|
|
|
318
|
it("filters on the index' own fields", async () => { |
|
319
|
const response = await search({ |
|
320
|
queries: [{ entity: "product", filters: { handle: "aurora-shoe" } }], |
|
321
|
}) |
|
322
|
|
|
323
|
expect(response.status).toEqual(200) |
|
324
|
expect(response.data.results[0].hits).toEqual([ |
|
325
|
expect.objectContaining({ id: shoe.id }), |
|
326
|
]) |
|
327
|
}) |
|
328
|
|
|
329
|
it("sorts and paginates", async () => { |
|
330
|
const first = await search({ |
|
331
|
queries: [ |
|
332
|
{ |
|
333
|
entity: "product", |
|
334
|
pagination: { take: 1, order: { title: "ASC" } }, |
|
335
|
}, |
|
336
|
], |
|
337
|
}) |
|
338
|
|
|
339
|
expect(first.data.results[0].hits).toEqual([ |
|
340
|
expect.objectContaining({ |
|
341
|
document: expect.objectContaining({ title: "Aurora Shoe" }), |
|
342
|
}), |
|
343
|
]) |
|
344
|
expect(first.data.results[0].metadata.take).toEqual(1) |
|
345
|
|
|
346
|
const second = await search({ |
|
347
|
queries: [ |
|
348
|
{ |
|
349
|
entity: "product", |
|
350
|
pagination: { skip: 1, take: 1, order: { title: "ASC" } }, |
|
351
|
}, |
|
352
|
], |
|
353
|
}) |
|
354
|
|
|
355
|
expect(second.data.results[0].hits).toEqual([ |
|
356
|
expect.objectContaining({ |
|
357
|
document: expect.objectContaining({ title: "Zephyr Shirt" }), |
|
358
|
}), |
|
359
|
]) |
|
360
|
expect(second.data.results[0].metadata.skip).toEqual(1) |
|
361
|
}) |
|
362
|
|
|
363
|
it("returns the facets a query asks for", async () => { |
|
364
|
const response = await search({ |
|
365
|
queries: [ |
|
366
|
{ |
|
367
|
entity: "product", |
|
368
|
search_options: { facets: [{ field: "status", type: "value" }] }, |
|
369
|
}, |
|
370
|
], |
|
371
|
}) |
|
372
|
|
|
373
|
expect(response.status).toEqual(200) |
|
374
|
expect(response.data.results[0].facets.status).toEqual({ |
|
375
|
type: "value", |
|
376
|
values: [{ value: "published", count: 2 }], |
|
377
|
}) |
|
378
|
}) |
|
379
|
|
|
380
|
it("rejects an unknown search option", async () => { |
|
381
|
const error = await search({ |
|
382
|
queries: [ |
|
383
|
{ |
|
384
|
entity: "product", |
|
385
|
search_options: { provider_options: { "search-medusa": {} } }, |
|
386
|
}, |
|
387
|
], |
|
388
|
}).catch((e) => e) |
|
389
|
|
|
390
|
expect(error.response.status).toEqual(400) |
|
391
|
}) |
|
392
|
}) |
|
393
|
}, |
|
394
|
}) |