Changes to types and user generation

This commit is contained in:
Pedro Silva 2022-12-09 17:34:59 +00:00
parent 4467d2b005
commit 6d87a38ab3
4 changed files with 118 additions and 89 deletions

View File

@ -13,7 +13,7 @@ export default class TestConfiguration<T> {
context: T context: T
tables: TablesApi tables: TablesApi
rows: RowApi rows: RowApi
userManagement: UserManagementApi users: UserManagementApi
constructor(apiClient: InternalAPIClient) { constructor(apiClient: InternalAPIClient) {
this.applications = new ApplicationApi(apiClient) this.applications = new ApplicationApi(apiClient)
@ -21,15 +21,15 @@ export default class TestConfiguration<T> {
this.rows = new RowApi(apiClient) this.rows = new RowApi(apiClient)
this.auth = new AuthApi(apiClient) this.auth = new AuthApi(apiClient)
this.screen = new ScreenApi(apiClient) this.screen = new ScreenApi(apiClient)
this.userManagement = new UserManagementApi(apiClient) this.users = new UserManagementApi(apiClient)
this.context = <T>{} this.context = <T>{}
} }
async loginAsAdmin() { async loginAsAdmin() {
await this.auth.login(<String>process.env.BB_ADMIN_USER_EMAIL, <String>process.env.BB_ADMIN_USER_PASSWORD) await this.auth.login(<string>process.env.BB_ADMIN_USER_EMAIL, <string>process.env.BB_ADMIN_USER_PASSWORD)
} }
async login(email: String, password: String) { async login(email: string, password: string) {
await this.auth.login(email, password) await this.auth.login(email, password)
} }

View File

@ -33,7 +33,13 @@ export default class UserManagementApi {
return [response, json] return [response, json]
} }
async addUsers(body: any): Promise<[Response, any]> { async addMultipleUsers(userList: User[]): Promise<[Response, any]> {
const body = {
create: {
users: userList,
groups: []
}
}
const response = await this.api.post(`/global/users/bulk`, { 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)

View File

@ -1,63 +1,51 @@
import generator from "../../generator"; import generator from "../../generator";
import { User } from "@budibase/types";
import { generateUserMetadataID } from "@budibase/backend-core/src/db";
export const generateDeveloper = (): Object => {
const generateDeveloper = (): User => {
const randomId = generator.guid(); const randomId = generator.guid();
return ({ return ({
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: []
} }
}) })
} }
export const generateAdmin = (): Object => { const generateAdmin = (): User => {
const randomId = generator.guid(); const randomId = generator.guid();
return ({ return ({
create: { email: `pedro+${randomId}@budibase.com`,
users: [{ password: randomId,
email: `pedro+${randomId}@budibase.com`, roles: {},
password: randomId, forceResetPassword: true,
roles: {}, admin: {
forceResetPassword: true, global: true
admin: { },
global: true builder: {
}, global: true
builder: {
global: true
}
}],
groups: []
} }
}) })
} }
export const generateAppUser = (): Object => { const generateAppUser = (): User => {
const randomId = generator.guid(); const randomId = generator.guid();
const user = { return ({
create: { email: `pedro+${randomId}@budibase.com`,
users: [{ password: randomId,
email: `pedro+${randomId}@budibase.com`, roles: {},
password: randomId, forceResetPassword: true,
roles: {}, admin: {
forceResetPassword: true, global: false
admin: { },
global: false builder: {
}, global: false
builder: {
global: false
}
}],
groups: []
} }
} })
return user
} }
export const generateInviteUser = (): Object[] => { export const generateInviteUser = (): Object[] => {
@ -69,4 +57,25 @@ export const generateInviteUser = (): Object[] => {
} }
}] }]
}
export const generateUser = (amount: number = 1, role?: string): User[] => {
const userList: User[] = [];
for (let i = 0; i < amount; i++) {
switch (role) {
case "admin":
userList.push(generateAdmin());
break;
case "developer":
userList.push(generateDeveloper());
break;
case "appUser":
userList.push(generateAppUser());
break;
default:
userList.push(generateAppUser());
break;
}
}
return userList
} }

View File

@ -2,7 +2,7 @@ import TestConfiguration from "../../../config/internal-api/TestConfiguration"
import { Application } from "@budibase/server/api/controllers/public/mapping/types" import { Application } from "@budibase/server/api/controllers/public/mapping/types"
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 { generateAdmin, generateAppUser, generateDeveloper, generateInviteUser } from "../../../config/internal-api/fixtures/userManagement" import { generateUser } from "../../../config/internal-api/fixtures/userManagement"
import { User } from "@budibase/types" import { User } from "@budibase/types"
describe("Internal API - User Management & Permissions", () => { describe("Internal API - User Management & Permissions", () => {
@ -18,19 +18,24 @@ describe("Internal API - User Management & Permissions", () => {
}) })
it("Add Users with different roles", async () => { it("Add Users with different roles", async () => {
await config.userManagement.searchUsers() await config.users.searchUsers()
await config.userManagement.getRoles() await config.users.getRoles()
// These need to be saved to the context so the passwords can be used to login // These need to be saved to the context so the passwords can be used to login
const admin = generateAdmin() const admin = generateUser(1, "admin")
const developer = generateDeveloper() expect(admin[0].builder?.global).toEqual(true)
const appUser = generateAppUser() expect(admin[0].admin?.global).toEqual(true)
const developer = generateUser(1, "developer")
expect(developer[0].builder?.global).toEqual(true)
const appUser = generateUser(1, "appUser")
expect(appUser[0].builder?.global).toEqual(false)
expect(appUser[0].admin?.global).toEqual(false)
await config.userManagement.addUsers(admin) await config.users.addMultipleUsers(admin)
await config.userManagement.addUsers(developer) await config.users.addMultipleUsers(developer)
await config.userManagement.addUsers(appUser) await config.users.addMultipleUsers(appUser)
const [allUsersResponse, allUsersJson] = await config.userManagement.getAllUsers() const [allUsersResponse, allUsersJson] = await config.users.getAllUsers()
expect(allUsersJson.length).toBeGreaterThan(0) expect(allUsersJson.length).toBeGreaterThan(0)
@ -38,28 +43,34 @@ describe("Internal API - User Management & Permissions", () => {
}) })
it("Delete User", async () => { it("Delete User", async () => {
const appUser = generateAppUser() const appUser = generateUser()
const [userResponse, userJson] = await config.userManagement.addUsers(appUser) expect(appUser[0].builder?.global).toEqual(false)
expect(appUser[0].admin?.global).toEqual(false)
const [userResponse, userJson] = await config.users.addMultipleUsers(appUser)
const userId = userJson.created.successful[0]._id const userId = userJson.created.successful[0]._id
await config.userManagement.deleteUser(<string>userId) await config.users.deleteUser(<string>userId)
}) })
it("Reset Password", async () => { it("Reset Password", async () => {
const appUser = generateAppUser() const appUser = generateUser()
const [userResponse, userJson] = await config.userManagement.addUsers(appUser) expect(appUser[0].builder?.global).toEqual(false)
const [userInfoResponse, userInfoJson] = await config.userManagement.getUserInformation(userJson.created.successful[0]._id) expect(appUser[0].admin?.global).toEqual(false)
const [userResponse, userJson] = await config.users.addMultipleUsers(appUser)
const [userInfoResponse, userInfoJson] = await config.users.getUserInformation(userJson.created.successful[0]._id)
const body: User = { const body: User = {
...userInfoJson, ...userInfoJson,
password: "newPassword" password: "newPassword"
} }
await config.userManagement.forcePasswordReset(body) await config.users.forcePasswordReset(body)
}) })
it("Change User information", async () => { it("Change User information", async () => {
const appUser = generateAppUser() const appUser = generateUser()
const [userResponse, userJson] = await config.userManagement.addUsers(appUser) expect(appUser[0].builder?.global).toEqual(false)
const [userInfoResponse, userInfoJson] = await config.userManagement.getUserInformation(userJson.created.successful[0]._id) expect(appUser[0].admin?.global).toEqual(false)
const [userResponse, userJson] = await config.users.addMultipleUsers(appUser)
const [userInfoResponse, userInfoJson] = await config.users.getUserInformation(userJson.created.successful[0]._id)
const body: User = { const body: User = {
...userInfoJson, ...userInfoJson,
firstName: "newFirstName", firstName: "newFirstName",
@ -68,77 +79,80 @@ describe("Internal API - User Management & Permissions", () => {
global: true global: true
} }
} }
await config.userManagement.changeUserInformation(body) await config.users.changeUserInformation(body)
const [changedUserInfoResponse, changedUserInfoJson] = await config.userManagement.getUserInformation(userJson.created.successful[0]._id) const [changedUserInfoResponse, changedUserInfoJson] = await config.users.getUserInformation(userJson.created.successful[0]._id)
expect(changedUserInfoJson.builder?.global).toBeDefined() expect(changedUserInfoJson.builder?.global).toBeDefined()
expect(changedUserInfoJson.builder?.global).toEqual(true) expect(changedUserInfoJson.builder?.global).toEqual(true)
}) })
it("Add BASIC user to app", async () => { it("Add BASIC user to app", async () => {
const basicUser = generateAppUser() const appUser = generateUser()
expect(appUser[0].builder?.global).toEqual(false)
const [createUserResponse, createUserJson] = await config.userManagement.addUsers(basicUser) expect(appUser[0].admin?.global).toEqual(false)
const [createUserResponse, createUserJson] = await config.users.addMultipleUsers(appUser)
const app = await config.applications.create(generateApp()) const app = await config.applications.create(generateApp())
config.applications.api.appId = app.appId config.applications.api.appId = app.appId
const [userInfoResponse, userInfoJson] = await config.userManagement.getUserInformation(createUserJson.created.successful[0]._id) const [userInfoResponse, userInfoJson] = await config.users.getUserInformation(createUserJson.created.successful[0]._id)
const body: User = { const body: User = {
...userInfoJson, ...userInfoJson,
roles: { roles: {
[app.appId?.toString() || ""]: "BASIC", [app.appId?.toString() || ""]: "BASIC",
} }
} }
await config.userManagement.changeUserInformation(body) await config.users.changeUserInformation(body)
const [changedUserInfoResponse, changedUserInfoJson] = await config.userManagement.getUserInformation(createUserJson.created.successful[0]._id) const [changedUserInfoResponse, changedUserInfoJson] = await config.users.getUserInformation(createUserJson.created.successful[0]._id)
expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toBeDefined() expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toBeDefined()
expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toEqual("BASIC") expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toEqual("BASIC")
}) })
it("Add ADMIN user to app", async () => { it("Add ADMIN user to app", async () => {
const adminUser = generateAdmin() const adminUser = generateUser(1, "admin")
expect(adminUser[0].builder?.global).toEqual(true)
const [createUserResponse, createUserJson] = await config.userManagement.addUsers(adminUser) expect(adminUser[0].admin?.global).toEqual(true)
const [createUserResponse, createUserJson] = await config.users.addMultipleUsers(adminUser)
const app = await config.applications.create(generateApp()) const app = await config.applications.create(generateApp())
config.applications.api.appId = app.appId config.applications.api.appId = app.appId
const [userInfoResponse, userInfoJson] = await config.userManagement.getUserInformation(createUserJson.created.successful[0]._id) const [userInfoResponse, userInfoJson] = await config.users.getUserInformation(createUserJson.created.successful[0]._id)
const body: User = { const body: User = {
...userInfoJson, ...userInfoJson,
roles: { roles: {
[app.appId?.toString() || ""]: "ADMIN", [app.appId?.toString() || ""]: "ADMIN",
} }
} }
await config.userManagement.changeUserInformation(body) await config.users.changeUserInformation(body)
const [changedUserInfoResponse, changedUserInfoJson] = await config.userManagement.getUserInformation(createUserJson.created.successful[0]._id) const [changedUserInfoResponse, changedUserInfoJson] = await config.users.getUserInformation(createUserJson.created.successful[0]._id)
expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toBeDefined() expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toBeDefined()
expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toEqual("ADMIN") expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toEqual("ADMIN")
}) })
it("Add POWER user to app", async () => { it("Add POWER user to app", async () => {
const powerUser = generateDeveloper() const powerUser = generateUser(1, 'Developer')
expect(powerUser[0].builder?.global).toEqual(true)
const [createUserResponse, createUserJson] = await config.userManagement.addUsers(powerUser) const [createUserResponse, createUserJson] = await config.users.addMultipleUsers(powerUser)
const app = await config.applications.create(generateApp()) const app = await config.applications.create(generateApp())
config.applications.api.appId = app.appId config.applications.api.appId = app.appId
const [userInfoResponse, userInfoJson] = await config.userManagement.getUserInformation(createUserJson.created.successful[0]._id) const [userInfoResponse, userInfoJson] = await config.users.getUserInformation(createUserJson.created.successful[0]._id)
const body: User = { const body: User = {
...userInfoJson, ...userInfoJson,
roles: { roles: {
[app.appId?.toString() || ""]: "POWER", [app.appId?.toString() || ""]: "POWER",
} }
} }
await config.userManagement.changeUserInformation(body) await config.users.changeUserInformation(body)
const [changedUserInfoResponse, changedUserInfoJson] = await config.userManagement.getUserInformation(createUserJson.created.successful[0]._id) const [changedUserInfoResponse, changedUserInfoJson] = await config.users.getUserInformation(createUserJson.created.successful[0]._id)
expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toBeDefined() expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toBeDefined()
expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toEqual("POWER") expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toEqual("POWER")