|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { |
|
3
|
adminHeaders, |
|
4
|
createAdminUser, |
|
5
|
} from "../../../helpers/create-admin-user" |
|
6
|
|
|
7
|
jest.setTimeout(120000) |
|
8
|
|
|
9
|
/** |
|
10
|
* Covers GET /admin/search for entities that have no search index — the |
|
11
|
* graph-query fan-out the Search Module cannot serve. |
|
12
|
*/ |
|
13
|
medusaIntegrationTestRunner({ |
|
14
|
testSuite: ({ dbConnection, getContainer, api }) => { |
|
15
|
const groupFor = (data: any, entity: string) => |
|
16
|
data.results.find((group: any) => group.entity === entity) |
|
17
|
|
|
18
|
beforeEach(async () => { |
|
19
|
await createAdminUser(dbConnection, adminHeaders, getContainer()) |
|
20
|
}) |
|
21
|
|
|
22
|
describe("GET /admin/search (graph fallback)", () => { |
|
23
|
beforeEach(async () => { |
|
24
|
await api.post( |
|
25
|
"/admin/regions", |
|
26
|
{ |
|
27
|
name: "Zephyr North", |
|
28
|
currency_code: "usd", |
|
29
|
countries: ["nz"], |
|
30
|
}, |
|
31
|
adminHeaders |
|
32
|
) |
|
33
|
|
|
34
|
await api.post( |
|
35
|
"/admin/regions", |
|
36
|
{ |
|
37
|
name: "Zephyr South", |
|
38
|
currency_code: "eur", |
|
39
|
countries: ["is"], |
|
40
|
}, |
|
41
|
adminHeaders |
|
42
|
) |
|
43
|
|
|
44
|
await api.post( |
|
45
|
"/admin/regions", |
|
46
|
{ |
|
47
|
name: "Aurora", |
|
48
|
currency_code: "gbp", |
|
49
|
countries: ["gb"], |
|
50
|
}, |
|
51
|
adminHeaders |
|
52
|
) |
|
53
|
}) |
|
54
|
|
|
55
|
it("searches unindexed entities and groups the results", async () => { |
|
56
|
const response = await api.get( |
|
57
|
"/admin/search?q=zephyr&entity=region,return_reason", |
|
58
|
adminHeaders |
|
59
|
) |
|
60
|
|
|
61
|
expect(response.status).toEqual(200) |
|
62
|
expect(response.data.results.map((g: any) => g.entity)).toEqual([ |
|
63
|
"region", |
|
64
|
"return_reason", |
|
65
|
]) |
|
66
|
|
|
67
|
expect(groupFor(response.data, "region").data).toEqual( |
|
68
|
expect.arrayContaining([ |
|
69
|
expect.objectContaining({ name: "Zephyr North" }), |
|
70
|
expect.objectContaining({ name: "Zephyr South" }), |
|
71
|
]) |
|
72
|
) |
|
73
|
expect(groupFor(response.data, "region").data).toHaveLength(2) |
|
74
|
expect(groupFor(response.data, "return_reason")).toEqual( |
|
75
|
expect.objectContaining({ |
|
76
|
entity: "return_reason", |
|
77
|
data: [], |
|
78
|
count: 0, |
|
79
|
}) |
|
80
|
) |
|
81
|
}) |
|
82
|
|
|
83
|
it("returns a group with no hits rather than dropping the entity", async () => { |
|
84
|
const response = await api.get( |
|
85
|
"/admin/search?q=aurora&entity=region,return_reason", |
|
86
|
adminHeaders |
|
87
|
) |
|
88
|
|
|
89
|
expect(response.status).toEqual(200) |
|
90
|
expect(groupFor(response.data, "region").data).toEqual([ |
|
91
|
expect.objectContaining({ name: "Aurora" }), |
|
92
|
]) |
|
93
|
expect(groupFor(response.data, "return_reason")).toEqual( |
|
94
|
expect.objectContaining({ |
|
95
|
entity: "return_reason", |
|
96
|
data: [], |
|
97
|
count: 0, |
|
98
|
}) |
|
99
|
) |
|
100
|
}) |
|
101
|
|
|
102
|
it("restricts the search to the requested entities", async () => { |
|
103
|
const response = await api.get( |
|
104
|
"/admin/search?q=zephyr&entity=region", |
|
105
|
adminHeaders |
|
106
|
) |
|
107
|
|
|
108
|
expect(response.status).toEqual(200) |
|
109
|
expect(response.data.results.length).toEqual(1) |
|
110
|
expect(response.data.results[0]).toEqual( |
|
111
|
expect.objectContaining({ entity: "region", offset: 0, limit: 20 }) |
|
112
|
) |
|
113
|
}) |
|
114
|
|
|
115
|
it("paginates each group on its own", async () => { |
|
116
|
const first = await api.get( |
|
117
|
"/admin/search?q=zephyr&entity=region&limit=1", |
|
118
|
adminHeaders |
|
119
|
) |
|
120
|
|
|
121
|
expect(groupFor(first.data, "region").data).toHaveLength(1) |
|
122
|
expect(groupFor(first.data, "region")).toEqual( |
|
123
|
expect.objectContaining({ limit: 1, offset: 0, count: 2 }) |
|
124
|
) |
|
125
|
|
|
126
|
const second = await api.get( |
|
127
|
"/admin/search?q=zephyr&entity=region&limit=1&offset=1", |
|
128
|
adminHeaders |
|
129
|
) |
|
130
|
|
|
131
|
expect(groupFor(second.data, "region").data).toHaveLength(1) |
|
132
|
expect(groupFor(second.data, "region").offset).toEqual(1) |
|
133
|
expect(groupFor(second.data, "region").data[0].id).not.toEqual( |
|
134
|
groupFor(first.data, "region").data[0].id |
|
135
|
) |
|
136
|
}) |
|
137
|
|
|
138
|
it("fails on an unknown entity", async () => { |
|
139
|
const error = await api |
|
140
|
.get("/admin/search?entity=nope", adminHeaders) |
|
141
|
.catch((e: any) => e) |
|
142
|
|
|
143
|
expect(error.response.status).toEqual(404) |
|
144
|
expect(error.response.data.message).toContain("nope") |
|
145
|
}) |
|
146
|
|
|
147
|
it("requires authentication", async () => { |
|
148
|
const error = await api.get("/admin/search?q=zephyr").catch((e: any) => e) |
|
149
|
|
|
150
|
expect(error.response.status).toEqual(401) |
|
151
|
}) |
|
152
|
}) |
|
153
|
}, |
|
154
|
}) |