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

View File

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

View File

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

View File

@ -669,4 +669,25 @@ describe("/api/global/users", () => {
expect(response.body.message).toBe("Unable to delete self.") 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, User,
CreateAdminUserRequest, CreateAdminUserRequest,
SearchQuery, SearchQuery,
InviteUsersResponse,
} from "@budibase/types" } from "@budibase/types"
import structures from "../structures" import structures from "../structures"
import { generator } from "@budibase/backend-core/tests" import { generator } from "@budibase/backend-core/tests"
@ -176,4 +177,24 @@ export class UserAPI extends TestAPI {
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
.expect(200) .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
}
} }