Add tests for Roles

This commit is contained in:
Pedro Silva 2022-12-06 16:14:00 +00:00
parent b2dcd93008
commit d2a8923d0d
2 changed files with 91 additions and 8 deletions

View File

@ -13,12 +13,13 @@ export default class AppApi {
constructor(apiClient: InternalAPIClient) { constructor(apiClient: InternalAPIClient) {
this.api = apiClient this.api = apiClient
} }
// TODO Fix the fetch apps to receive an optional number of apps and compare if the received app is more or less.
// each possible scenario should have its own method.
async fetchEmptyAppList(): Promise<[Response, Application[]]> { async fetchEmptyAppList(): Promise<[Response, Application[]]> {
const response = await this.api.get(`/applications?status=all`) const response = await this.api.get(`/applications?status=all`)
const json = await response.json() const json = await response.json()
expect(response).toHaveStatusCode(200) expect(response).toHaveStatusCode(200)
expect(json.length).toEqual(0) expect(json.length).toBeGreaterThanOrEqual(0)
return [response, json] return [response, json]
} }

View File

@ -1,7 +1,9 @@
import TestConfiguration from "../../../config/internal-api/TestConfiguration" 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 { generateAdmin, generateAppUser, generateDeveloper, generateInviteUser } from "../../../config/internal-api/fixtures/userManagement" import { generateAdmin, generateAppUser, generateDeveloper, generateInviteUser } from "../../../config/internal-api/fixtures/userManagement"
import { User } from "@budibase/types"
describe("Internal API - User Management & Permissions", () => { describe("Internal API - User Management & Permissions", () => {
const api = new InternalAPIClient() const api = new InternalAPIClient()
@ -46,7 +48,7 @@ describe("Internal API - User Management & Permissions", () => {
const appUser = generateAppUser() const appUser = generateAppUser()
const [userResponse, userData] = await config.userManagement.addUsers(appUser) const [userResponse, userData] = await config.userManagement.addUsers(appUser)
const [userInfoResponse, userInfoJson] = await config.userManagement.getUserInformation(userData.created.successful[0]._id) const [userInfoResponse, userInfoJson] = await config.userManagement.getUserInformation(userData.created.successful[0]._id)
const body = { const body: User = {
...userInfoJson, ...userInfoJson,
password: "newPassword" password: "newPassword"
@ -56,18 +58,98 @@ describe("Internal API - User Management & Permissions", () => {
it("Change User information", async () => { it("Change User information", async () => {
const appUser = generateAppUser() const appUser = generateAppUser()
const [userResponse, userData] = await config.userManagement.addUsers(appUser) const [userResponse, userJson] = await config.userManagement.addUsers(appUser)
const [userInfoResponse, userInfoJson] = await config.userManagement.getUserInformation(userData.created.successful[0]._id) const [userInfoResponse, userInfoJson] = await config.userManagement.getUserInformation(userJson.created.successful[0]._id)
const body = { const body: User = {
...userInfoJson, ...userInfoJson,
firstName: "newFirstName",
lastName: "newLastName",
builder: { builder: {
global: true global: true
} }
} }
const [changedUserResponse, changedUserJson] = await config.userManagement.changeUserInformation(body) const [changedUserResponse, changedUserJson] = await config.userManagement.changeUserInformation(body)
expect(changedUserJson.builder?.global).toBeDefined() expect(changedUserJson._id).toEqual(userJson.created.successful[0]._id)
expect(changedUserJson.builder?.global).toEqual(true) expect(changedUserJson._rev).not.toEqual(userJson.created.successful[0]._rev)
const [changedUserInfoResponse, changedUserInfoJson] = await config.userManagement.getUserInformation(userJson.created.successful[0]._id)
expect(changedUserInfoJson.builder?.global).toBeDefined()
expect(changedUserInfoJson.builder?.global).toEqual(true)
}) })
it("Add BASIC user to app", async () => {
const basicUser = generateAppUser()
const [createUserResponse, createUserJson] = await config.userManagement.addUsers(basicUser)
const app = await config.applications.create(generateApp())
config.applications.api.appId = app.appId
const [userInfoResponse, userInfoJson] = await config.userManagement.getUserInformation(createUserJson.created.successful[0]._id)
const body: User = {
...userInfoJson,
roles: {
[app.appId?.toString() || ""]: "BASIC",
}
}
const [changedUserResponse, changedUserJson] = await config.userManagement.changeUserInformation(body)
expect(changedUserJson._id).toEqual(createUserJson.created.successful[0]._id)
expect(changedUserJson._rev).not.toEqual(createUserJson.created.successful[0]._rev)
const [changedUserInfoResponse, changedUserInfoJson] = await config.userManagement.getUserInformation(createUserJson.created.successful[0]._id)
expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toBeDefined()
expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toEqual("BASIC")
})
it("Add ADMIN user to app", async () => {
const adminUser = generateAdmin()
const [createUserResponse, createUserJson] = await config.userManagement.addUsers(adminUser)
const app = await config.applications.create(generateApp())
config.applications.api.appId = app.appId
const [userInfoResponse, userInfoJson] = await config.userManagement.getUserInformation(createUserJson.created.successful[0]._id)
const body: User = {
...userInfoJson,
roles: {
[app.appId?.toString() || ""]: "ADMIN",
}
}
const [changedUserResponse, changedUserJson] = await config.userManagement.changeUserInformation(body)
expect(changedUserJson._id).toEqual(createUserJson.created.successful[0]._id)
expect(changedUserJson._rev).not.toEqual(createUserJson.created.successful[0]._rev)
const [changedUserInfoResponse, changedUserInfoJson] = await config.userManagement.getUserInformation(createUserJson.created.successful[0]._id)
expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toBeDefined()
expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toEqual("ADMIN")
})
it("Add POWER user to app", async () => {
const powerUser = generateDeveloper()
const [createUserResponse, createUserJson] = await config.userManagement.addUsers(powerUser)
const app = await config.applications.create(generateApp())
config.applications.api.appId = app.appId
const [userInfoResponse, userInfoJson] = await config.userManagement.getUserInformation(createUserJson.created.successful[0]._id)
const body: User = {
...userInfoJson,
roles: {
[app.appId?.toString() || ""]: "POWER",
}
}
const [changedUserResponse, changedUserJson] = await config.userManagement.changeUserInformation(body)
expect(changedUserJson._id).toEqual(createUserJson.created.successful[0]._id)
expect(changedUserJson._rev).not.toEqual(createUserJson.created.successful[0]._rev)
const [changedUserInfoResponse, changedUserInfoJson] = await config.userManagement.getUserInformation(createUserJson.created.successful[0]._id)
expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toBeDefined()
expect(changedUserInfoJson.roles[app.appId?.toString() || ""]).toEqual("POWER")
})
}) })