From afdefce55d3eabf62704751870ab0259f088bdb1 Mon Sep 17 00:00:00 2001 From: adrinr Date: Mon, 13 Mar 2023 17:44:29 +0100 Subject: [PATCH] Add find endpoint tests --- .../routes/global/tests/scim/users.spec.ts | 103 +++++++++++++----- packages/worker/src/tests/api/scim/users.ts | 9 ++ 2 files changed, 86 insertions(+), 26 deletions(-) diff --git a/packages/worker/src/api/routes/global/tests/scim/users.spec.ts b/packages/worker/src/api/routes/global/tests/scim/users.spec.ts index 6d3e04213e..86339f4ea6 100644 --- a/packages/worker/src/api/routes/global/tests/scim/users.spec.ts +++ b/packages/worker/src/api/routes/global/tests/scim/users.spec.ts @@ -1,8 +1,49 @@ import tk from "timekeeper" import { mocks, structures } from "@budibase/backend-core/tests" -import { ScimCreateUserRequest } from "@budibase/types" +import { ScimCreateUserRequest, ScimUserResponse } from "@budibase/types" import { TestConfiguration } from "../../../../../tests" +function createScimCreateUserRequest(userData?: { + externalId?: string + email?: string + firstName?: string + lastName?: string +}) { + const { + externalId = structures.uuid(), + email = structures.generator.email(), + firstName = structures.generator.first(), + lastName = structures.generator.last(), + } = userData || {} + + const user: ScimCreateUserRequest = { + schemas: [ + "urn:ietf:params:scim:schemas:core:2.0:User", + "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User", + ], + externalId, + userName: structures.generator.name(), + active: true, + emails: [ + { + primary: true, + type: "work", + value: email, + }, + ], + meta: { + resourceType: "User", + }, + name: { + formatted: structures.generator.name(), + familyName: lastName, + givenName: firstName, + }, + roles: [], + } + return user +} + describe("/api/global/scim/v2/users", () => { let mockedTime = new Date(structures.generator.timestamp()) @@ -102,31 +143,7 @@ describe("/api/global/scim/v2/users", () => { firstName: structures.generator.first(), lastName: structures.generator.last(), } - const body: ScimCreateUserRequest = { - schemas: [ - "urn:ietf:params:scim:schemas:core:2.0:User", - "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User", - ], - externalId: userData.externalId, - userName: structures.generator.name(), - active: true, - emails: [ - { - primary: true, - type: "work", - value: userData.email, - }, - ], - meta: { - resourceType: "User", - }, - name: { - formatted: structures.generator.name(), - familyName: userData.lastName, - givenName: userData.firstName, - }, - roles: [], - } + const body = createScimCreateUserRequest(userData) const response = await postScimUser({ body }) @@ -166,4 +183,38 @@ describe("/api/global/scim/v2/users", () => { }) }) }) + + describe("GET /api/global/scim/v2/users/:id", () => { + let user: ScimUserResponse + + beforeEach(async () => { + const body = createScimCreateUserRequest() + + user = await config.api.scimUsersAPI.post({ body }) + }) + + const findScimUser = config.api.scimUsersAPI.find + + it("unauthorised calls are not allowed", async () => { + const response = await findScimUser(user.id, { + setHeaders: false, + expect: 403, + }) + + expect(response).toEqual({ message: "Tenant id not set", status: 403 }) + }) + + it("cannot be called when feature is disabled", async () => { + mocks.licenses.useCloudFree() + const response = await findScimUser(user.id, { expect: 400 }) + + expect(response).toEqual(featureDisabledResponse) + }) + + it("should return existing user", async () => { + const response = await findScimUser(user.id) + + expect(response).toEqual(user) + }) + }) }) diff --git a/packages/worker/src/tests/api/scim/users.ts b/packages/worker/src/tests/api/scim/users.ts index adef49faf6..34fdf5f95a 100644 --- a/packages/worker/src/tests/api/scim/users.ts +++ b/packages/worker/src/tests/api/scim/users.ts @@ -48,6 +48,15 @@ export class ScimUsersAPI extends TestAPI { return res.body as ScimUserListResponse } + find = async (id: string, requestSettings?: Partial) => { + const res = await this.#createRequest( + `/api/global/scim/v2/users/${id}`, + "get", + requestSettings + ) + return res.body as ScimUserResponse + } + post = async ( { body,