diff --git a/packages/worker/src/api/routes/global/tests/groups.spec.ts b/packages/worker/src/api/routes/global/tests/groups.spec.ts index c93f7a76e4..2ad54fc219 100644 --- a/packages/worker/src/api/routes/global/tests/groups.spec.ts +++ b/packages/worker/src/api/routes/global/tests/groups.spec.ts @@ -1,5 +1,10 @@ import { events } from "@budibase/backend-core" -import { structures, TestConfiguration, mocks } from "../../../../tests" +import { + structures, + TestConfiguration, + mocks, + generator, +} from "../../../../tests" import { UserGroup } from "@budibase/types" mocks.licenses.useGroups() @@ -134,5 +139,52 @@ describe("/api/global/groups", () => { }) }) }) + + describe("existing users", () => { + let groupId: string + let users: { _id: string; email: string }[] = [] + + beforeAll(async () => { + groupId = ( + await config.api.groups.saveGroup(structures.groups.UserGroup()) + ).body._id + + await Promise.all( + Array.from({ length: 30 }).map(async (_, i) => { + const email = `user${i}@${generator.domain()}` + const user = await config.api.users.saveUser({ + ...structures.users.user(), + email, + }) + users.push({ _id: user.body._id, email }) + }) + ) + users = users.sort((a, b) => a._id.localeCompare(b._id)) + await config.api.groups.updateGroupUsers(groupId, { + add: users.map(u => u._id), + remove: [], + }) + }) + + it("should return first page", async () => { + const result = await config.api.groups.searchUsers(groupId) + expect(result.body).toEqual({ + users: users.slice(0, 10), + bookmark: users[10]._id, + hasNextPage: true, + }) + }) + + it("given a bookmark, should return skip items", async () => { + const result = await config.api.groups.searchUsers(groupId, { + bookmark: users[7]._id, + }) + expect(result.body).toEqual({ + users: users.slice(7, 17), + bookmark: users[17]._id, + hasNextPage: true, + }) + }) + }) }) }) diff --git a/packages/worker/src/tests/api/groups.ts b/packages/worker/src/tests/api/groups.ts index a0d7184360..950d3b1364 100644 --- a/packages/worker/src/tests/api/groups.ts +++ b/packages/worker/src/tests/api/groups.ts @@ -24,9 +24,25 @@ export class GroupsAPI extends TestAPI { .expect(200) } - searchUsers = (id: string) => { + searchUsers = (id: string, params?: { bookmark?: string }) => { + let url = `/api/global/groups/${id}/users?` + if (params?.bookmark) { + url += `bookmark=${params.bookmark}&` + } return this.request - .get(`/api/global/groups/${id}/users`) + .get(url) + .set(this.config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + } + + updateGroupUsers = ( + id: string, + body: { add: string[]; remove: string[] } + ) => { + return this.request + .post(`/api/global/groups/${id}/users`) + .send(body) .set(this.config.defaultHeaders()) .expect("Content-Type", /json/) .expect(200)