Move creators to structures

This commit is contained in:
adrinr 2023-03-17 16:35:12 +01:00
parent 586275ed89
commit 4ac682a3c2
4 changed files with 88 additions and 77 deletions

View File

@ -9,3 +9,4 @@ export * as sso from "./sso"
export * as tenant from "./tenants"
export * as users from "./users"
export { generator } from "./generator"
export * as scim from "./scim"

View File

@ -0,0 +1,67 @@
import { ScimCreateGroupRequest, ScimCreateUserRequest } from "@budibase/types"
import { uuid } from "./common"
import { generator } from "./generator"
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 || {}
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: username,
active: true,
emails: [
{
primary: true,
type: "work",
value: email,
},
],
meta: {
resourceType: "User",
},
name: {
formatted: generator.name(),
familyName: lastName,
givenName: firstName,
},
roles: [],
}
return user
}
export function createGroupRequest(groupData?: {
externalId?: string
displayName?: string
}) {
const { externalId = uuid(), displayName = generator.word() } =
groupData || {}
const group: ScimCreateGroupRequest = {
schemas: [
"urn:ietf:params:scim:schemas:core:2.0:Group",
"http://schemas.microsoft.com/2006/11/ResourceManagement/ADSCIM/2.0/Group",
],
externalId: externalId,
displayName: displayName,
meta: {
resourceType: "Group",
},
}
return group
}

View File

@ -2,38 +2,17 @@ import tk from "timekeeper"
import _ from "lodash"
import { mocks, structures } from "@budibase/backend-core/tests"
import {
ScimCreateGroupRequest,
ScimGroupResponse,
ScimUpdateRequest,
ScimUserResponse,
} from "@budibase/types"
import { TestConfiguration } from "../../../../../tests"
mocks.licenses.useScimIntegration()
function createScimCreateGroupRequest(groupData?: {
externalId?: string
displayName?: string
}) {
const {
externalId = structures.uuid(),
displayName = structures.generator.word(),
} = groupData || {}
const group: ScimCreateGroupRequest = {
schemas: [
"urn:ietf:params:scim:schemas:core:2.0:Group",
"http://schemas.microsoft.com/2006/11/ResourceManagement/ADSCIM/2.0/Group",
],
externalId: externalId,
displayName: displayName,
meta: {
resourceType: "Group",
},
}
return group
}
describe("/api/global/scim/v2/groups", () => {
let users: ScimUserResponse[]
beforeEach(() => {
tk.freeze(mocks.date.MOCK_DATE)
@ -44,6 +23,13 @@ describe("/api/global/scim/v2/groups", () => {
beforeAll(async () => {
await config.beforeAll()
for (let i = 0; i < 30; i++) {
const body = structures.scim.createUserRequest()
users.push(await config.api.scimUsersAPI.post({ body }))
}
users = users.sort((a, b) => (a.id > b.id ? 1 : -1))
})
afterAll(async () => {
@ -100,7 +86,7 @@ describe("/api/global/scim/v2/groups", () => {
groups = []
for (let i = 0; i < groupCount; i++) {
const body = createScimCreateGroupRequest()
const body = structures.scim.createGroupRequest()
groups.push(await config.api.scimGroupsAPI.post({ body }))
}
@ -156,7 +142,7 @@ describe("/api/global/scim/v2/groups", () => {
externalId: structures.uuid(),
displayName: structures.generator.word(),
}
const body = createScimCreateGroupRequest(groupData)
const body = structures.scim.createGroupRequest(groupData)
const response = await postScimGroup({ body })
@ -188,7 +174,7 @@ describe("/api/global/scim/v2/groups", () => {
let group: ScimGroupResponse
beforeEach(async () => {
const body = createScimCreateGroupRequest()
const body = structures.scim.createGroupRequest()
group = await config.api.scimGroupsAPI.post({ body })
})
@ -233,7 +219,7 @@ describe("/api/global/scim/v2/groups", () => {
let group: ScimGroupResponse
beforeEach(async () => {
const body = createScimCreateGroupRequest()
const body = structures.scim.createGroupRequest()
group = await config.api.scimGroupsAPI.post({ body })
})
@ -273,7 +259,7 @@ describe("/api/global/scim/v2/groups", () => {
let group: ScimGroupResponse
beforeEach(async () => {
const body = createScimCreateGroupRequest()
const body = structures.scim.createGroupRequest()
group = await config.api.scimGroupsAPI.post({ body })
})

View File

@ -10,49 +10,6 @@ import { TestConfiguration } from "../../../../../tests"
mocks.licenses.useScimIntegration()
function createScimCreateUserRequest(userData?: {
externalId?: string
email?: string
firstName?: string
lastName?: string
username?: string
}) {
const {
externalId = structures.uuid(),
email = structures.generator.email(),
firstName = structures.generator.first(),
lastName = structures.generator.last(),
username = structures.generator.name(),
} = 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: username,
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())
@ -124,7 +81,7 @@ describe("/api/global/scim/v2/users", () => {
users = []
for (let i = 0; i < userCount; i++) {
const body = createScimCreateUserRequest()
const body = structures.scim.createUserRequest()
users.push(await config.api.scimUsersAPI.post({ body }))
}
@ -248,7 +205,7 @@ describe("/api/global/scim/v2/users", () => {
lastName: structures.generator.last(),
username: structures.generator.name(),
}
const body = createScimCreateUserRequest(userData)
const body = structures.scim.createUserRequest(userData)
const response = await postScimUser({ body })
@ -293,7 +250,7 @@ describe("/api/global/scim/v2/users", () => {
let user: ScimUserResponse
beforeEach(async () => {
const body = createScimCreateUserRequest()
const body = structures.scim.createUserRequest()
user = await config.api.scimUsersAPI.post({ body })
})
@ -338,7 +295,7 @@ describe("/api/global/scim/v2/users", () => {
let user: ScimUserResponse
beforeEach(async () => {
const body = createScimCreateUserRequest()
const body = structures.scim.createUserRequest()
user = await config.api.scimUsersAPI.post({ body })
})
@ -481,7 +438,7 @@ describe("/api/global/scim/v2/users", () => {
let user: ScimUserResponse
beforeEach(async () => {
const body = createScimCreateUserRequest()
const body = structures.scim.createUserRequest()
user = await config.api.scimUsersAPI.post({ body })
})