Skip to content

Commit 6bae892

Browse files
bidoubiwaniemannd
andauthored
Add client tests (#556)
* feat: improved header handling * fix: use types and set property directly * add tests for custom headers * Improve test validation * Add tests to check if client is still usable Co-authored-by: hazeglide <[email protected]> Co-authored-by: niemannd <[email protected]>
1 parent 496b6b7 commit 6bae892

File tree

3 files changed

+336
-270
lines changed

3 files changed

+336
-270
lines changed

tests/client_tests.ts

Lines changed: 48 additions & 270 deletions
Original file line numberDiff line numberDiff line change
@@ -1,286 +1,64 @@
1-
import MeiliSearch, * as Types from '../src/types'
21
import {
32
clearAllIndexes,
43
config,
5-
masterClient,
6-
privateClient,
7-
publicClient,
8-
anonymousClient,
94
PUBLIC_KEY,
5+
MeiliSearch,
6+
MASTER_KEY,
7+
PRIVATE_KEY,
108
} from './meilisearch-test-utils'
119

12-
const uidNoPrimaryKey = {
13-
uid: 'movies_test',
14-
}
15-
const uidAndPrimaryKey = {
16-
uid: 'movies_test2',
17-
primaryKey: 'id',
18-
}
19-
2010
afterAll(() => {
2111
return clearAllIndexes(config)
2212
})
2313

2414
describe.each([
25-
{ client: masterClient, permission: 'Master' },
26-
{ client: privateClient, permission: 'Private' },
27-
])('Test on client', ({ client, permission }) => {
28-
describe('Test on indexes', () => {
29-
beforeAll(() => {
30-
return clearAllIndexes(config)
31-
})
32-
test(`${permission} key: get all indexes when empty`, async () => {
33-
const expected: Types.IndexResponse[] = []
34-
await client.listIndexes().then((response: Types.IndexResponse[]) => {
35-
expect(response).toEqual(expected)
36-
})
37-
await expect(client.listIndexes()).resolves.toHaveLength(0)
38-
})
39-
test(`${permission} key: create with no primary key`, async () => {
40-
await client.createIndex(uidNoPrimaryKey.uid).then((response) => {
41-
expect(response).toHaveProperty('uid', uidNoPrimaryKey.uid)
42-
})
43-
44-
await client
45-
.getIndex(uidNoPrimaryKey.uid)
46-
.show()
47-
.then((response: Types.IndexResponse) => {
48-
expect(response).toHaveProperty('uid', uidNoPrimaryKey.uid)
49-
expect(response).toHaveProperty('primaryKey', null)
50-
expect(response).toHaveProperty('createdAt', expect.any(String))
51-
expect(response).toHaveProperty('updatedAt', expect.any(String))
52-
})
53-
})
54-
test(`${permission} key: create with primary key`, async () => {
55-
await client
56-
.createIndex(uidAndPrimaryKey.uid, {
57-
primaryKey: uidAndPrimaryKey.primaryKey,
58-
})
59-
.then((response) => {
60-
expect(response).toHaveProperty('uid', uidAndPrimaryKey.uid)
61-
})
62-
await client
63-
.getIndex(uidAndPrimaryKey.uid)
64-
.show()
65-
.then((response: Types.IndexResponse) => {
66-
expect(response).toHaveProperty(
67-
'primaryKey',
68-
uidAndPrimaryKey.primaryKey
69-
)
70-
expect(response).toHaveProperty('createdAt', expect.any(String))
71-
expect(response).toHaveProperty('updatedAt', expect.any(String))
72-
})
73-
})
74-
test(`${permission} key: get all indexes when not empty`, async () => {
75-
await client.listIndexes().then((response: Types.IndexResponse[]) => {
76-
const indexes = response.map((index) => index.uid)
77-
expect(indexes).toEqual(expect.arrayContaining([uidAndPrimaryKey.uid]))
78-
expect(indexes).toEqual(expect.arrayContaining([uidNoPrimaryKey.uid]))
79-
expect(indexes.length).toEqual(2)
80-
})
81-
})
82-
test(`${permission} key: show index with primary key`, async () => {
83-
const index = client.getIndex(uidAndPrimaryKey.uid)
84-
await index.show().then((response: Types.IndexResponse) => {
85-
expect(response).toHaveProperty('uid', uidAndPrimaryKey.uid)
86-
expect(response).toHaveProperty(
87-
'primaryKey',
88-
uidAndPrimaryKey.primaryKey
89-
)
90-
})
91-
})
92-
93-
test(`${permission} key: show index with NO primary key`, async () => {
94-
const index = client.getIndex(uidNoPrimaryKey.uid)
95-
await index.show().then((response: Types.IndexResponse) => {
96-
expect(response).toHaveProperty('uid', uidNoPrimaryKey.uid)
97-
expect(response).toHaveProperty('primaryKey', null)
98-
})
99-
})
100-
101-
test(`${permission} key: update primary key on an index that has no primary key already`, async () => {
102-
const index = client.getIndex(uidNoPrimaryKey.uid)
103-
await index
104-
.updateIndex({ primaryKey: 'newPrimaryKey' })
105-
.then((response: Types.IndexResponse) => {
106-
expect(response).toHaveProperty('uid', uidNoPrimaryKey.uid)
107-
expect(response).toHaveProperty('primaryKey', 'newPrimaryKey')
108-
})
109-
})
110-
111-
test(`${permission} key: update primary key on an index that has already a primary key and fail`, async () => {
112-
const index = client.getIndex(uidAndPrimaryKey.uid)
113-
await expect(
114-
index.updateIndex({ primaryKey: 'newPrimaryKey' })
115-
).rejects.toThrowError(
116-
`The schema already have an primary key. It's impossible to update it`
117-
) // see issue in meilisearch/meilisearch
118-
})
119-
120-
test(`${permission} key: delete index`, async () => {
121-
const index = client.getIndex(uidNoPrimaryKey.uid)
122-
await index.deleteIndex().then((response: string) => {
123-
expect(response).toBe('')
124-
})
125-
await expect(client.listIndexes()).resolves.toHaveLength(1)
126-
})
127-
test(`${permission} key: bad host should raise CommunicationError`, async () => {
128-
const client = new MeiliSearch({ host: 'badHost' })
129-
try {
130-
await client.version()
131-
} catch (e) {
132-
expect(e.type).toEqual('MeiliSearchCommunicationError')
133-
}
134-
})
135-
test(`${permission} key: show deleted index should fail`, async () => {
136-
const index = client.getIndex(uidNoPrimaryKey.uid)
137-
await expect(index.show()).rejects.toThrowError(
138-
`Index ${uidNoPrimaryKey.uid} not found`
139-
)
140-
})
15+
{ key: MASTER_KEY, permission: 'Master' },
16+
{ key: PRIVATE_KEY, permission: 'Private' },
17+
{ key: PUBLIC_KEY, permission: 'Public' },
18+
])('Test on client', ({ key, permission }) => {
19+
test(`${permission} key: Create client with api key`, async () => {
20+
const client = new MeiliSearch({
21+
...config,
22+
apiKey: key,
23+
})
24+
const health = await client.isHealthy()
25+
expect(health).toBe(true)
26+
})
14127

142-
test(`${permission} key: create index with already existing uid should fail`, async () => {
143-
await expect(
144-
client.createIndex(uidAndPrimaryKey.uid, {
145-
primaryKey: uidAndPrimaryKey.primaryKey,
146-
})
147-
).rejects.toThrowError(`index already exists`)
148-
})
28+
test(`${permission} key: Create client with custom headers`, async () => {
29+
const client = new MeiliSearch({
30+
...config,
31+
apiKey: key,
32+
headers: {
33+
Expect: '200-OK',
34+
},
35+
})
36+
expect(client.config.headers).toStrictEqual({ Expect: '200-OK' })
37+
const health = await client.isHealthy()
38+
expect(health).toBe(true)
39+
})
40+
})
14941

150-
test(`${permission} key: delete index with uid that does not exist should fail`, async () => {
151-
const index = client.getIndex(uidNoPrimaryKey.uid)
152-
await expect(index.deleteIndex()).rejects.toThrowError(
153-
`Index ${uidNoPrimaryKey.uid} not found`
154-
)
155-
})
42+
describe.each([
43+
{ key: MASTER_KEY, permission: 'Master' },
44+
{ key: PRIVATE_KEY, permission: 'Private' },
45+
])('Test on client', ({ key, permission }) => {
46+
beforeEach(async () => {
47+
await clearAllIndexes(config)
15648
})
157-
describe('Test on base routes', () => {
158-
test(`${permission} key: get health`, async () => {
159-
await client.isHealthy().then((response: boolean) => {
160-
expect(response).toBe(true)
161-
})
162-
})
163-
test(`${permission} key: get version`, async () => {
164-
await client.version().then((response: Types.Version) => {
165-
expect(response).toHaveProperty('commitSha', expect.any(String))
166-
expect(response).toHaveProperty('buildDate', expect.any(String))
167-
expect(response).toHaveProperty('pkgVersion', expect.any(String))
168-
})
169-
})
170-
test(`${permission} key: get /stats information`, async () => {
171-
await client.stats().then((response: Types.Stats) => {
172-
expect(response).toHaveProperty('databaseSize', expect.any(Number))
173-
expect(response).toHaveProperty('lastUpdate') // TODO: Could be null, find out why
174-
expect(response).toHaveProperty('indexes', expect.any(Object))
175-
})
176-
})
49+
test(`${permission} key: Create client with custom headers`, async () => {
50+
const client = new MeiliSearch({
51+
...config,
52+
apiKey: key,
53+
headers: {
54+
Expect: '200-OK',
55+
},
56+
})
57+
expect(client.config.headers).toStrictEqual({ Expect: '200-OK' })
58+
const health = await client.isHealthy()
59+
expect(health).toBe(true)
60+
await client.getOrCreateIndex('test')
61+
const indexes = await client.listIndexes()
62+
expect(indexes.length).toBe(1)
17763
})
17864
})
179-
180-
describe.each([{ client: publicClient, permission: 'Public' }])(
181-
'Test on routes where public key should not have access',
182-
({ client, permission }) => {
183-
describe('Test on indexes', () => {
184-
test(`${permission} key: try to get all indexes and be denied`, async () => {
185-
await expect(client.listIndexes()).rejects.toThrowError(
186-
`Invalid API key: ${PUBLIC_KEY}`
187-
)
188-
})
189-
test(`${permission} key: try to create Index with primary key and be denied`, async () => {
190-
await expect(
191-
client.createIndex(uidAndPrimaryKey.uid, {
192-
primaryKey: uidAndPrimaryKey.primaryKey,
193-
})
194-
).rejects.toThrowError(`Invalid API key: ${PUBLIC_KEY}`)
195-
})
196-
test(`${permission} key: try to create Index with NO primary key and be denied`, async () => {
197-
await expect(
198-
client.createIndex(uidNoPrimaryKey.uid)
199-
).rejects.toThrowError(`Invalid API key: ${PUBLIC_KEY}`)
200-
})
201-
test(`${permission} key: try to get index info and be denied`, async () => {
202-
await expect(
203-
client.getIndex(uidNoPrimaryKey.uid).show()
204-
).rejects.toThrowError(`Invalid API key: ${PUBLIC_KEY}`)
205-
})
206-
test(`${permission} key: try to delete index and be denied`, async () => {
207-
await expect(
208-
client.getIndex(uidAndPrimaryKey.uid).deleteIndex()
209-
).rejects.toThrowError(`Invalid API key: ${PUBLIC_KEY}`)
210-
})
211-
test(`${permission} key: try to update index and be denied`, async () => {
212-
await expect(
213-
client
214-
.getIndex(uidAndPrimaryKey.uid)
215-
.updateIndex({ primaryKey: uidAndPrimaryKey.primaryKey })
216-
).rejects.toThrowError(`Invalid API key: ${PUBLIC_KEY}`)
217-
})
218-
})
219-
describe('Test on base routes', () => {
220-
test(`${permission} key: try to get version and be denied`, async () => {
221-
await expect(client.version()).rejects.toThrowError(
222-
`Invalid API key: ${PUBLIC_KEY}`
223-
)
224-
})
225-
test(`${permission} key: try to get /stats information and be denied`, async () => {
226-
await expect(client.stats()).rejects.toThrowError(
227-
`Invalid API key: ${PUBLIC_KEY}`
228-
)
229-
})
230-
})
231-
}
232-
)
233-
234-
describe.each([{ client: anonymousClient, permission: 'No' }])(
235-
'Test on routes where client without api key should not have access',
236-
({ client, permission }) => {
237-
describe('Test on indexes', () => {
238-
test(`${permission} key: try to get all indexes and be denied`, async () => {
239-
await expect(client.listIndexes()).rejects.toThrowError(
240-
`You must have an authorization token`
241-
)
242-
})
243-
test(`${permission} key: try to create an index with primary key and be denied`, async () => {
244-
await expect(
245-
client.createIndex(uidAndPrimaryKey.uid, {
246-
primaryKey: uidAndPrimaryKey.primaryKey,
247-
})
248-
).rejects.toThrowError(`You must have an authorization token`)
249-
})
250-
test(`${permission} key: try to create an index with NO primary key and be denied`, async () => {
251-
await expect(
252-
client.createIndex(uidNoPrimaryKey.uid)
253-
).rejects.toThrowError(`You must have an authorization token`)
254-
})
255-
test(`${permission} key: try to get index info and be denied`, async () => {
256-
await expect(
257-
client.getIndex(uidNoPrimaryKey.uid).show()
258-
).rejects.toThrowError(`You must have an authorization token`)
259-
})
260-
test(`${permission} key: try to delete index and be denied`, async () => {
261-
await expect(
262-
client.getIndex(uidAndPrimaryKey.uid).deleteIndex()
263-
).rejects.toThrowError(`You must have an authorization token`)
264-
})
265-
test(`${permission} key: try to update index and be denied`, async () => {
266-
await expect(
267-
client
268-
.getIndex(uidAndPrimaryKey.uid)
269-
.updateIndex({ primaryKey: uidAndPrimaryKey.primaryKey })
270-
).rejects.toThrowError(`You must have an authorization token`)
271-
})
272-
})
273-
describe('Test on base routes', () => {
274-
test(`${permission} key: try to get version and be denied`, async () => {
275-
await expect(client.version()).rejects.toThrowError(
276-
`You must have an authorization token`
277-
)
278-
})
279-
test(`${permission} key: try to get /stats information and be denied`, async () => {
280-
await expect(client.stats()).rejects.toThrowError(
281-
`You must have an authorization token`
282-
)
283-
})
284-
})
285-
}
286-
)

0 commit comments

Comments
 (0)