Add tests for user management

This commit is contained in:
Pedro Silva 2022-11-30 11:02:11 +00:00
parent 6baa5d2aad
commit b9186cac9d
3 changed files with 103 additions and 62 deletions

View File

@ -1,5 +1,5 @@
import { Response } from "node-fetch" import { Response } from "node-fetch"
import { User, UserDeletedEvent } from "@budibase/types" import { Role, User, UserDeletedEvent } from "@budibase/types"
import InternalAPIClient from "./InternalAPIClient" import InternalAPIClient from "./InternalAPIClient"
import { responseMessage } from "../fixtures/types/responseMessage" import { responseMessage } from "../fixtures/types/responseMessage"
@ -15,7 +15,6 @@ export default class UserManagementApi {
const json = await response.json() const json = await response.json()
expect(response).toHaveStatusCode(200) expect(response).toHaveStatusCode(200)
expect(json.data.length).toBeGreaterThan(0) expect(json.data.length).toBeGreaterThan(0)
expect(json.hasNextPage).toBe(false)
return [response, json] return [response, json]
} }
@ -26,19 +25,20 @@ export default class UserManagementApi {
return [response, json] return [response, json]
} }
async getAllUsers(): Promise<[Response, User]> { async getAllUsers(): Promise<[Response, User[]]> {
const response = await this.api.get(`/global/users`) const response = await this.api.get(`/global/users`)
const json = await response.json() const json = await response.json()
expect(response).toHaveStatusCode(200) expect(response).toHaveStatusCode(200)
expect(json.length).toBeGreaterThan(0)
return [response, json] return [response, json]
} }
async inviteUsers(body: User[]): Promise<[Response, responseMessage]> { async addUsers(body: any): Promise<[Response, responseMessage]> {
const response = await this.api.post(`/global/users/multi/invite`, { body }) const response = await this.api.post(`/global/users/bulk`, { body })
const json = await response.json() const json = await response.json()
expect(response).toHaveStatusCode(200) expect(response).toHaveStatusCode(200)
expect(json.successful.length).toEqual(body.length) expect(json.created.unsuccessful.length).toEqual(0)
expect(json.unsuccessful.length).toEqual(0) expect(json.created.successful.length).toEqual(body.create.users.length)
return [response, json] return [response, json]
} }
@ -58,4 +58,21 @@ export default class UserManagementApi {
expect(json.deleted.successful[0].userId).toEqual(userId) expect(json.deleted.successful[0].userId).toEqual(userId)
return [response, json] return [response, json]
} }
async inviteUser(body: any): Promise<[Response, responseMessage]> {
const response = await this.api.post(`/global/users/multi/invite`, { body })
const json = await response.json()
expect(response).toHaveStatusCode(200)
expect(json.created.successful.length).toEqual(body.length)
expect(json.created.unsuccessful.length).toEqual(0)
return [response, json]
}
async getRoles(): Promise<[Response, Role[]]> {
const response = await this.api.get(`/roles`)
const json = await response.json()
expect(response).toHaveStatusCode(200)
expect(json.length).toEqual(4)
return [response, json]
}
} }

View File

@ -1,59 +1,69 @@
import generator from "../../generator"; import generator from "../../generator";
const randomId = generator.guid; export const generateDeveloper = (): Object => {
export const generateDeveloper = (): any => ({ const randomId = generator.guid();
create: { return ({
users: [{ create: {
email: `pedro+${randomId()}@budibase.com`, users: [{
password: randomId, email: `pedro+${randomId}@budibase.com`,
roles: {}, password: randomId,
forceResetPassword: true, roles: {},
builder: { forceResetPassword: true,
global: true builder: {
} global: true
}], }
groups: [] }],
} groups: []
}) }
})
}
export const generateAdmin = (): any => ({ export const generateAdmin = (): Object => {
create: { const randomId = generator.guid();
users: [{ return ({
email: `pedro+${randomId()}@budibase.com`, create: {
password: randomId, users: [{
roles: {}, email: `pedro+${randomId}@budibase.com`,
forceResetPassword: true, password: randomId,
admin: { roles: {},
global: true forceResetPassword: true,
}, admin: {
builder: { global: true
global: true },
} builder: {
}], global: true
groups: [] }
}],
groups: []
}
})
}
export const generateAppUser = (): Object => {
const randomId = generator.guid();
const user = {
create: {
users: [{
email: `pedro+${randomId}@budibase.com`,
password: randomId,
roles: {},
forceResetPassword: true,
admin: {
global: false
},
builder: {
global: false
}
}],
groups: []
}
} }
}) return user
export const generateAppUser = (): any => ({ }
create: {
users: [{
email: `pedro+${randomId()}@budibase.com`,
password: randomId,
roles: {},
forceResetPassword: true,
admin: {
global: false
},
builder: {
global: false
}
}],
groups: []
}
})
export const generateInviteUser = (): any => ( export const generateInviteUser = (): Object[] => {
[{ const randomId = generator.guid();
email: `pedro+${randomId()}@budibase.com`, return [{
email: `pedro+${randomId}@budibase.com`,
userInfo: { userInfo: {
admin: { admin: {
global: true global: true
@ -64,4 +74,5 @@ export const generateInviteUser = (): any => (
userGroups: [] userGroups: []
} }
}] }]
)
}

View File

@ -4,9 +4,9 @@ import { db } from "@budibase/backend-core"
import InternalAPIClient from "../../../config/internal-api/TestConfiguration/InternalAPIClient" import InternalAPIClient from "../../../config/internal-api/TestConfiguration/InternalAPIClient"
import generateApp from "../../../config/internal-api/fixtures/applications" import generateApp from "../../../config/internal-api/fixtures/applications"
import generator from "../../../config/generator" import generator from "../../../config/generator"
import generateScreen from "../../../config/internal-api/fixtures/screens" import { generateAdmin, generateAppUser, generateDeveloper, generateInviteUser } from "../../../config/internal-api/fixtures/userManagement"
describe("Internal API - User Management", () => { describe("Internal API - User Management & Permissions", () => {
const api = new InternalAPIClient() const api = new InternalAPIClient()
const config = new TestConfiguration<Application>(api) const config = new TestConfiguration<Application>(api)
@ -18,7 +18,20 @@ describe("Internal API - User Management", () => {
await config.afterAll() await config.afterAll()
}) })
it("Get all users", async () => { it("Add Users with different roles", async () => {
await config.userManagement.searchUsers() await config.userManagement.searchUsers()
await config.userManagement.getRoles()
const [adminResponse, adminData] = await config.userManagement.addUsers(generateAdmin())
const [devResponse, devData] = await config.userManagement.addUsers(generateDeveloper())
const [userResponse, userData] = await config.userManagement.addUsers(generateAppUser())
const [invitedUserResponse, invitedUserData] = await config.userManagement.addUsers(generateInviteUser())
const [allUsersResponse, allUsersData] = await config.userManagement.getAllUsers()
expect(allUsersData.length).toEqual(4)
}) })
}) })