Add tests for the onboarding endpoint.

This commit is contained in:
Sam Rose 2023-11-09 15:13:59 +00:00
parent 822c03b0ef
commit 7f530eeab5
No known key found for this signature in database
5 changed files with 52 additions and 6 deletions

View File

@ -303,7 +303,7 @@ export class UserDB {
static async bulkCreate(
newUsersRequested: User[],
groups: string[]
groups?: string[]
): Promise<BulkUserCreated> {
const tenantId = getTenantId()
@ -328,7 +328,7 @@ export class UserDB {
})
continue
}
newUser.userGroups = groups
newUser.userGroups = groups || []
newUsers.push(newUser)
if (isCreator(newUser)) {
newCreators.push(newUser)

View File

@ -50,6 +50,7 @@ export type InviteUsersRequest = InviteUserRequest[]
export interface InviteUsersResponse {
successful: { email: string }[]
unsuccessful: { email: string; reason: string }[]
created?: boolean
}
export interface SearchUsersRequest {

View File

@ -17,6 +17,7 @@ import {
Ctx,
InviteUserRequest,
InviteUsersRequest,
InviteUsersResponse,
MigrationType,
SaveUserResponse,
SearchUsersRequest,
@ -250,7 +251,9 @@ export const tenantUserLookup = async (ctx: any) => {
/*
Encapsulate the app user onboarding flows here.
*/
export const onboardUsers = async (ctx: Ctx<InviteUsersRequest>) => {
export const onboardUsers = async (
ctx: Ctx<InviteUsersRequest, InviteUsersResponse>
) => {
if (await isEmailConfigured()) {
await inviteMultiple(ctx)
return
@ -272,10 +275,10 @@ export const onboardUsers = async (ctx: Ctx<InviteUsersRequest>) => {
}
})
let resp = await userSdk.db.bulkCreate(users, [])
resp.successful.forEach(user => {
let resp = await userSdk.db.bulkCreate(users)
for (const user of resp.successful) {
user.password = createdPasswords[user.email]
})
}
ctx.body = { ...resp, created: true }
}

View File

@ -669,4 +669,25 @@ describe("/api/global/users", () => {
expect(response.body.message).toBe("Unable to delete self.")
})
})
describe("POST /api/global/users/onboard", () => {
it("should successfully onboard a user", async () => {
const response = await config.api.users.onboardUser([
{ email: structures.users.newEmail(), userInfo: {} },
])
expect(response.successful.length).toBe(1)
expect(response.unsuccessful.length).toBe(0)
})
it("should not onboard a user who has been invited", async () => {
const email = structures.users.newEmail()
await config.api.users.sendUserInvite(sendMailMock, email)
const response = await config.api.users.onboardUser([
{ email, userInfo: {} },
])
expect(response.successful.length).toBe(0)
expect(response.unsuccessful.length).toBe(1)
})
})
})

View File

@ -5,6 +5,7 @@ import {
User,
CreateAdminUserRequest,
SearchQuery,
InviteUsersResponse,
} from "@budibase/types"
import structures from "../structures"
import { generator } from "@budibase/backend-core/tests"
@ -176,4 +177,24 @@ export class UserAPI extends TestAPI {
.expect("Content-Type", /json/)
.expect(200)
}
onboardUser = async (
req: InviteUsersRequest
): Promise<InviteUsersResponse> => {
const resp = await this.request
.post(`/api/global/users/onboard`)
.send(req)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
if (resp.status !== 200) {
throw new Error(
`request failed with status ${resp.status} and body ${JSON.stringify(
resp.body
)}`
)
}
return resp.body as InviteUsersResponse
}
}