From f88299309184b92eb268c6e472c563cba63abbe6 Mon Sep 17 00:00:00 2001 From: adrinr Date: Tue, 11 Apr 2023 16:12:18 +0100 Subject: [PATCH] Name and its fields are optional --- .../tests/utilities/structures/scim.ts | 52 ++++++++++++------- .../types/src/api/web/global/scim/users.ts | 10 ++-- .../src/api/routes/global/tests/scim.spec.ts | 41 ++++++++++++++- 3 files changed, 77 insertions(+), 26 deletions(-) diff --git a/packages/backend-core/tests/utilities/structures/scim.ts b/packages/backend-core/tests/utilities/structures/scim.ts index 6657bb90b5..741cff165e 100644 --- a/packages/backend-core/tests/utilities/structures/scim.ts +++ b/packages/backend-core/tests/utilities/structures/scim.ts @@ -1,23 +1,31 @@ import { ScimCreateGroupRequest, ScimCreateUserRequest } from "@budibase/types" import { uuid } from "./common" import { generator } from "./generator" +import _ from "lodash" -export function createUserRequest(userData?: { - externalId?: string - email?: string - firstName?: string - lastName?: string - username?: string -}) { - const { - externalId = uuid(), - email = generator.email(), - firstName = generator.first(), - lastName = generator.last(), - username = generator.name(), - } = userData || {} +interface CreateUserRequestFields { + externalId: string + email: string + firstName: string + lastName: string + username: string +} - const user: ScimCreateUserRequest = { +export function createUserRequest(userData?: Partial) { + const defaultValues = { + externalId: uuid(), + email: generator.email(), + firstName: generator.first(), + lastName: generator.last(), + username: generator.name(), + } + + const { externalId, email, firstName, lastName, username } = _.assign( + defaultValues, + userData + ) + + let user: ScimCreateUserRequest = { schemas: [ "urn:ietf:params:scim:schemas:core:2.0:User", "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User", @@ -35,13 +43,17 @@ export function createUserRequest(userData?: { meta: { resourceType: "User", }, - name: { - formatted: generator.name(), - familyName: lastName, - givenName: firstName, - }, roles: [], } + + if (firstName || lastName) { + user.name = { + formatted: [firstName, lastName].filter(s => s).join(" "), + familyName: lastName, + givenName: firstName, + } + } + return user } diff --git a/packages/types/src/api/web/global/scim/users.ts b/packages/types/src/api/web/global/scim/users.ts index 53424934ff..8269d392b0 100644 --- a/packages/types/src/api/web/global/scim/users.ts +++ b/packages/types/src/api/web/global/scim/users.ts @@ -19,10 +19,10 @@ export interface ScimUserResponse extends ScimResource { } userName: string displayName?: string - name: { - formatted: string - familyName: string - givenName: string + name?: { + formatted?: string + familyName?: string + givenName?: string } active: BooleanString emails?: Emails @@ -41,7 +41,7 @@ export interface ScimCreateUserRequest { resourceType: "User" } displayName?: string - name: { + name?: { formatted: string familyName: string givenName: string diff --git a/packages/worker/src/api/routes/global/tests/scim.spec.ts b/packages/worker/src/api/routes/global/tests/scim.spec.ts index e76e6b6cda..3352951152 100644 --- a/packages/worker/src/api/routes/global/tests/scim.spec.ts +++ b/packages/worker/src/api/routes/global/tests/scim.spec.ts @@ -255,6 +255,45 @@ describe("scim", () => { ) }) + it("a new user can minim information", async () => { + const userData = { + externalId: structures.uuid(), + email: structures.generator.email(), + username: structures.generator.name(), + firstName: undefined, + lastName: undefined, + } + const body = structures.scim.createUserRequest(userData) + + const response = await postScimUser({ body }) + + const expectedScimUser = { + schemas: ["urn:ietf:params:scim:schemas:core:2.0:User"], + id: expect.any(String), + externalId: userData.externalId, + meta: { + resourceType: "User", + created: mocks.date.MOCK_DATE.toISOString(), + lastModified: mocks.date.MOCK_DATE.toISOString(), + }, + userName: userData.username, + active: true, + emails: [ + { + value: userData.email, + type: "work", + primary: true, + }, + ], + } + expect(response).toEqual(expectedScimUser) + + const persistedUsers = await config.api.scimUsersAPI.get() + expect(persistedUsers.Resources).toEqual( + expect.arrayContaining([expectedScimUser]) + ) + }) + it("an event is dispatched", async () => { const body = structures.scim.createUserRequest() @@ -398,7 +437,7 @@ describe("scim", () => { name: { ...user.name, familyName: newFamilyName, - formatted: `${user.name.givenName} ${newFamilyName}`, + formatted: `${user.name!.givenName} ${newFamilyName}`, }, } expect(response).toEqual(expectedScimUser)