Name and its fields are optional

This commit is contained in:
adrinr 2023-04-11 16:12:18 +01:00
parent 2e60ca7526
commit f882993091
3 changed files with 77 additions and 26 deletions

View File

@ -1,23 +1,31 @@
import { ScimCreateGroupRequest, ScimCreateUserRequest } from "@budibase/types" import { ScimCreateGroupRequest, ScimCreateUserRequest } from "@budibase/types"
import { uuid } from "./common" import { uuid } from "./common"
import { generator } from "./generator" import { generator } from "./generator"
import _ from "lodash"
export function createUserRequest(userData?: { interface CreateUserRequestFields {
externalId?: string externalId: string
email?: string email: string
firstName?: string firstName: string
lastName?: string lastName: string
username?: string username: string
}) { }
const {
externalId = uuid(),
email = generator.email(),
firstName = generator.first(),
lastName = generator.last(),
username = generator.name(),
} = userData || {}
const user: ScimCreateUserRequest = { export function createUserRequest(userData?: Partial<CreateUserRequestFields>) {
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: [ schemas: [
"urn:ietf:params:scim:schemas:core:2.0:User", "urn:ietf:params:scim:schemas:core:2.0:User",
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User", "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",
@ -35,13 +43,17 @@ export function createUserRequest(userData?: {
meta: { meta: {
resourceType: "User", resourceType: "User",
}, },
name: {
formatted: generator.name(),
familyName: lastName,
givenName: firstName,
},
roles: [], roles: [],
} }
if (firstName || lastName) {
user.name = {
formatted: [firstName, lastName].filter(s => s).join(" "),
familyName: lastName,
givenName: firstName,
}
}
return user return user
} }

View File

@ -19,10 +19,10 @@ export interface ScimUserResponse extends ScimResource {
} }
userName: string userName: string
displayName?: string displayName?: string
name: { name?: {
formatted: string formatted?: string
familyName: string familyName?: string
givenName: string givenName?: string
} }
active: BooleanString active: BooleanString
emails?: Emails emails?: Emails
@ -41,7 +41,7 @@ export interface ScimCreateUserRequest {
resourceType: "User" resourceType: "User"
} }
displayName?: string displayName?: string
name: { name?: {
formatted: string formatted: string
familyName: string familyName: string
givenName: string givenName: string

View File

@ -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 () => { it("an event is dispatched", async () => {
const body = structures.scim.createUserRequest() const body = structures.scim.createUserRequest()
@ -398,7 +437,7 @@ describe("scim", () => {
name: { name: {
...user.name, ...user.name,
familyName: newFamilyName, familyName: newFamilyName,
formatted: `${user.name.givenName} ${newFamilyName}`, formatted: `${user.name!.givenName} ${newFamilyName}`,
}, },
} }
expect(response).toEqual(expectedScimUser) expect(response).toEqual(expectedScimUser)