Skip to content

Commit 5af8fc0

Browse files
committed
test: fix flaky test
1 parent 76a6bc4 commit 5af8fc0

File tree

1 file changed

+35
-38
lines changed

1 file changed

+35
-38
lines changed

src/users/integration.test.ts

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import weaviate, { ApiKey } from '..';
1+
import weaviate, { ApiKey, WeaviateClient } from '..';
22
import { requireAtLeast } from '../../test/version.js';
33
import { WeaviateUserTypeDB } from '../openapi/types.js';
4-
import { UserDB } from './types.js';
4+
import { GetUserOptions, UserDB } from './types.js';
55

66
requireAtLeast(
77
1,
@@ -67,6 +67,17 @@ requireAtLeast(
6767
30,
6868
0
6969
)('dynamic user management', () => {
70+
/** List dynamic DB users. */
71+
const listDBUsers = (c: WeaviateClient, opts?: GetUserOptions) =>
72+
c.users.db.listAll(opts).then((all) => all.filter((u) => u.userType == 'db_user'));
73+
74+
const deleteAllUsers = async (c: WeaviateClient) => {
75+
const users = await listDBUsers(c);
76+
await Promise.all(users.map((user) => c.users.db.delete(user.id)));
77+
};
78+
79+
beforeAll(() => makeClient('admin-key').then(deleteAllUsers));
80+
7081
it('should be able to manage "db" user lifecycle', async () => {
7182
const admin = await makeClient('admin-key');
7283

@@ -199,46 +210,32 @@ requireAtLeast(
199210
it('should be able to list all users with additional info', async () => {
200211
const admin = await makeClient('admin-key');
201212

202-
await admin.users.db.listAll({ includeLastUsedTime: true }).then((res) => {
203-
res
204-
// Users created in environment (db_env_user) do not have
205-
// createdAt and apiKeyFirstLetters fields.
206-
.filter((user) => user.userType == 'db_user')
207-
.map(async (user) => {
208-
// Use each user at least once
209-
const key = await admin.users.db.rotateKey(user.id);
210-
await makeClient(key).then((c) => c.users.getMyUser());
211-
return user;
213+
// Create test users and use each at least once
214+
const created = await Promise.all(
215+
['A', 'B', 'C'].map(async (user) => {
216+
const key = await admin.users.db.create(user);
217+
await makeClient(key).then((c) => c.users.getMyUser());
218+
return user;
219+
})
220+
);
221+
222+
const users = await listDBUsers(admin, { includeLastUsedTime: true }).then((users) =>
223+
users.filter((user) => created.includes(user.id))
224+
);
225+
226+
users.forEach((user) =>
227+
expect(user).toEqual(
228+
expect.objectContaining({
229+
createdAt: expect.any(Date),
230+
lastUsedAt: expect.any(Date),
231+
apiKeyFirstLetters: expect.any(String),
212232
})
213-
.forEach(async (user, _) =>
214-
expect(await user).toEqual(
215-
expect.objectContaining({
216-
createdAt: expect.any(Date),
217-
lastUsedAt: expect.any(Date),
218-
apiKeyFirstLetters: expect.any(String),
219-
})
220-
)
221-
);
222-
});
233+
)
234+
);
223235
});
224236
});
225237

226-
afterAll(() =>
227-
makeClient('admin-key').then(async (c) => {
228-
await Promise.all(
229-
[
230-
'jim',
231-
'pam',
232-
'dwight',
233-
'dynamic-dave',
234-
'api-ashley',
235-
'role-rick',
236-
'permission-peter',
237-
'timely-tim',
238-
].map((n) => c.users.db.delete(n))
239-
);
240-
})
241-
);
238+
afterAll(() => makeClient('admin-key').then(deleteAllUsers));
242239
});
243240

244241
afterAll(() => makeClient('admin-key').then((c) => c.roles.delete('test')));

0 commit comments

Comments
 (0)