From 3500aabc8a61ca3fc8a1357b285aae036099f322 Mon Sep 17 00:00:00 2001 From: adrinr Date: Mon, 13 Mar 2023 17:24:36 +0100 Subject: [PATCH] Patch endpoint --- .../routes/global/tests/scim/users.spec.ts | 54 +++++++++++++++++++ packages/worker/src/tests/api/scim/users.ts | 22 +++++++- 2 files changed, 75 insertions(+), 1 deletion(-) 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 b86eb790ed..041f2f88e7 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 @@ -226,4 +226,58 @@ describe("/api/global/scim/v2/users", () => { }) }) }) + + describe("PATCH /api/global/scim/v2/users", () => { + const patchScimUser = config.api.scimUsersAPI.patch + + let user: ScimUser + + beforeEach(async () => { + const body = createScimCreateUserRequest() + + user = await config.api.scimUsersAPI.post({ body }) + }) + + it("unauthorised calls are not allowed", async () => { + const response = await patchScimUser({} as any, { + 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 patchScimUser({} as any, { expect: 400 }) + + expect(response).toEqual(featureDisabledResponse) + }) + + it("an existing user can be updated", async () => { + const body: ScimUpdateRequest = { + schemas: ["urn:ietf:params:scim:api:messages:2.0:PatchOp"], + Operations: [ + { + op: "add", + path: "string", + value: "string", + }, + ], + } + + const response = await patchScimUser({ id: user.id, body }) + + const expectedScimUser = { ...user } + expect(response).toEqual(expectedScimUser) + + const persistedUsers = await config.api.scimUsersAPI.get() + expect(persistedUsers).toEqual( + expect.objectContaining({ + totalResults: 1, + Resources: [expectedScimUser], + }) + ) + }) + }) }) diff --git a/packages/worker/src/tests/api/scim/users.ts b/packages/worker/src/tests/api/scim/users.ts index 3daf0f4fad..f0ebc9e6b6 100644 --- a/packages/worker/src/tests/api/scim/users.ts +++ b/packages/worker/src/tests/api/scim/users.ts @@ -20,7 +20,7 @@ export class ScimUsersAPI extends TestAPI { #createRequest = ( url: string, - method: "get" | "post", + method: "get" | "post" | "patch", requestSettings?: Partial, body?: object ) => { @@ -83,4 +83,24 @@ export class ScimUsersAPI extends TestAPI { return res.body as ScimUserResponse } + + patch = async ( + { + id, + body, + }: { + id: string + body: ScimUpdateRequest + }, + requestSettings?: Partial + ) => { + const res = await this.#createRequest( + `/api/global/scim/v2/users/${id}`, + "patch", + requestSettings, + body + ) + + return res.body as ScimUser + } }