forked from meilisearch/meilisearch-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynonyms_tests.ts
196 lines (186 loc) · 6.1 KB
/
synonyms_tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import * as Types from '../src/types'
import {
clearAllIndexes,
config,
masterClient,
privateClient,
publicClient,
anonymousClient,
badHostClient,
BAD_HOST,
} from './meilisearch-test-utils'
const index = {
uid: 'movies_test',
}
const dataset = [
{ id: 123, title: 'Pride and Prejudice', comment: 'A great book' },
{ id: 456, title: 'Le Petit Prince', comment: 'A french book' },
{ id: 2, title: 'Le Rouge et le Noir', comment: 'Another french book' },
{ id: 1, title: 'Alice In Wonderland', comment: 'A weird book' },
{ id: 1344, title: 'The Hobbit', comment: 'An awesome book' },
{
id: 4,
title: 'Harry Potter and the Half-Blood Prince',
comment: 'The best book',
},
{ id: 42, title: "The Hitchhiker's Guide to the Galaxy" },
]
jest.setTimeout(100 * 1000)
afterAll(() => {
return clearAllIndexes(config)
})
describe.each([
{ client: masterClient, permission: 'Master' },
{ client: privateClient, permission: 'Private' },
])('Test on synonyms', ({ client, permission }) => {
beforeAll(async () => {
await clearAllIndexes(config)
await masterClient.createIndex(index.uid)
const { updateId } = await masterClient
.index(index.uid)
.addDocuments(dataset)
await masterClient.index(index.uid).waitForPendingUpdate(updateId)
})
test(`${permission} key: Get default synonyms`, async () => {
await client
.index(index.uid)
.getSynonyms()
.then((response: object) => {
expect(response).toEqual({})
})
})
test(`${permission} key: Update synonyms`, async () => {
const newSynonyms = {
hp: ['harry potter'],
}
const { updateId } = await client
.index(index.uid)
.updateSynonyms(newSynonyms)
.then((response: Types.EnqueuedUpdate) => {
expect(response).toHaveProperty('updateId', expect.any(Number))
return response
})
await client.index(index.uid).waitForPendingUpdate(updateId)
await client
.index(index.uid)
.getSynonyms()
.then((response: object) => {
expect(response).toEqual(newSynonyms)
})
})
test(`${permission} key: Reset synonyms`, async () => {
const { updateId } = await client
.index(index.uid)
.resetSynonyms()
.then((response: Types.EnqueuedUpdate) => {
expect(response).toHaveProperty('updateId', expect.any(Number))
return response
})
await client.index(index.uid).waitForPendingUpdate(updateId)
await client
.index(index.uid)
.getSynonyms()
.then((response: object) => {
expect(response).toEqual({})
})
})
})
describe.each([{ client: publicClient, permission: 'Public' }])(
'Test on synonyms',
({ client, permission }) => {
beforeAll(async () => {
await clearAllIndexes(config)
await masterClient.createIndex(index.uid)
})
test(`${permission} key: try to get synonyms and be denied`, async () => {
await expect(
client.index(index.uid).getSynonyms()
).rejects.toHaveProperty('errorCode', Types.ErrorStatusCode.INVALID_TOKEN)
})
test(`${permission} key: try to update synonyms and be denied`, async () => {
await expect(
client.index(index.uid).updateSynonyms({})
).rejects.toHaveProperty('errorCode', Types.ErrorStatusCode.INVALID_TOKEN)
})
test(`${permission} key: try to reset synonyms and be denied`, async () => {
await expect(
client.index(index.uid).resetSynonyms()
).rejects.toHaveProperty('errorCode', Types.ErrorStatusCode.INVALID_TOKEN)
})
}
)
describe.each([{ client: anonymousClient, permission: 'No' }])(
'Test on synonyms',
({ client, permission }) => {
beforeAll(async () => {
await clearAllIndexes(config)
await masterClient.createIndex(index.uid)
})
test(`${permission} key: try to get synonyms and be denied`, async () => {
await expect(
client.index(index.uid).getSynonyms()
).rejects.toHaveProperty(
'errorCode',
Types.ErrorStatusCode.MISSING_AUTHORIZATION_HEADER
)
})
test(`${permission} key: try to update synonyms and be denied`, async () => {
await expect(
client.index(index.uid).updateSynonyms({})
).rejects.toHaveProperty(
'errorCode',
Types.ErrorStatusCode.MISSING_AUTHORIZATION_HEADER
)
})
test(`${permission} key: try to reset synonyms and be denied`, async () => {
await expect(
client.index(index.uid).resetSynonyms()
).rejects.toHaveProperty(
'errorCode',
Types.ErrorStatusCode.MISSING_AUTHORIZATION_HEADER
)
})
}
)
test(`Get request should not add double slash nor a trailing slash`, async () => {
try {
const res = await badHostClient.index(index.uid).getSynonyms()
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
} catch (e) {
expect(e.message).toMatch(
`${BAD_HOST}/indexes/movies_test/settings/synonyms`
)
expect(e.message).not.toMatch(
`${BAD_HOST}/indexes/movies_test/settings/synonyms/`
)
expect(e.type).toBe('MeiliSearchCommunicationError')
}
})
test(`Update request should not add double slash nor a trailing slash`, async () => {
try {
const res = await badHostClient.index(index.uid).updateSynonyms([])
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
} catch (e) {
expect(e.message).toMatch(
`${BAD_HOST}/indexes/movies_test/settings/synonyms`
)
expect(e.message).not.toMatch(
`${BAD_HOST}/indexes/movies_test/settings/synonyms/`
)
expect(e.type).toBe('MeiliSearchCommunicationError')
}
})
test(`Reset request should not add double slash nor a trailing slash`, async () => {
try {
const res = await badHostClient.index(index.uid).resetSynonyms()
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
} catch (e) {
expect(e.message).toMatch(
`${BAD_HOST}/indexes/movies_test/settings/synonyms`
)
expect(e.message).not.toMatch(
`${BAD_HOST}/indexes/movies_test/settings/synonyms/`
)
expect(e.type).toBe('MeiliSearchCommunicationError')
}
})