2022-01-11 08:33:00 +13:00
|
|
|
const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles")
|
2021-03-05 06:06:33 +13:00
|
|
|
const { checkPermissionsEndpoint } = require("./utilities/TestFunctions")
|
2021-03-06 01:11:44 +13:00
|
|
|
const setup = require("./utilities")
|
2021-04-14 07:25:43 +12:00
|
|
|
|
|
|
|
jest.mock("../../../utilities/workerRequests", () => ({
|
2021-04-24 05:07:39 +12:00
|
|
|
getGlobalUsers: jest.fn(() => {
|
|
|
|
return {}
|
|
|
|
}),
|
2021-05-28 02:40:55 +12:00
|
|
|
getGlobalSelf: jest.fn(() => {
|
|
|
|
return {}
|
|
|
|
}),
|
2021-04-14 07:25:43 +12:00
|
|
|
deleteGlobalUser: jest.fn(),
|
|
|
|
}))
|
2020-04-10 03:53:48 +12:00
|
|
|
|
|
|
|
describe("/users", () => {
|
2021-03-06 01:11:44 +13:00
|
|
|
let request = setup.getRequest()
|
|
|
|
let config = setup.getConfig()
|
2020-04-10 03:53:48 +12:00
|
|
|
|
2021-03-06 01:11:44 +13:00
|
|
|
afterAll(setup.afterAll)
|
2020-05-15 02:12:30 +12:00
|
|
|
|
|
|
|
beforeEach(async () => {
|
2021-03-05 06:06:33 +13:00
|
|
|
await config.init()
|
2020-12-03 06:08:25 +13:00
|
|
|
})
|
2020-04-10 03:53:48 +12:00
|
|
|
|
2020-10-29 09:35:06 +13:00
|
|
|
describe("fetch", () => {
|
2022-09-15 11:58:08 +12:00
|
|
|
|
2022-07-27 07:04:29 +12:00
|
|
|
it("returns a list of users from an instance db", async () => {
|
|
|
|
await config.createUser("uuidx")
|
|
|
|
await config.createUser("uuidy")
|
|
|
|
const res = await request
|
|
|
|
.get(`/api/users/metadata`)
|
|
|
|
.set(config.defaultHeaders())
|
|
|
|
.expect("Content-Type", /json/)
|
|
|
|
.expect(200)
|
|
|
|
|
|
|
|
expect(res.body.length).toBe(3)
|
|
|
|
expect(res.body.find(u => u._id === `ro_ta_users_us_uuidx`)).toBeDefined()
|
|
|
|
expect(res.body.find(u => u._id === `ro_ta_users_us_uuidy`)).toBeDefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-05-28 04:23:01 +12:00
|
|
|
it("should apply authorization to endpoint", async () => {
|
2021-10-26 04:59:09 +13:00
|
|
|
await config.createUser()
|
2021-03-05 06:06:33 +13:00
|
|
|
await checkPermissionsEndpoint({
|
|
|
|
config,
|
2020-05-28 04:23:01 +12:00
|
|
|
request,
|
|
|
|
method: "GET",
|
2021-04-14 07:25:43 +12:00
|
|
|
url: `/api/users/metadata`,
|
2020-12-03 06:08:25 +13:00
|
|
|
passRole: BUILTIN_ROLE_IDS.ADMIN,
|
|
|
|
failRole: BUILTIN_ROLE_IDS.PUBLIC,
|
2020-05-28 04:23:01 +12:00
|
|
|
})
|
|
|
|
})
|
2020-05-15 02:12:30 +12:00
|
|
|
})
|
2020-04-11 03:37:59 +12:00
|
|
|
|
2021-03-10 06:09:18 +13:00
|
|
|
describe("update", () => {
|
|
|
|
it("should be able to update the user", async () => {
|
|
|
|
const user = await config.createUser()
|
|
|
|
user.roleId = BUILTIN_ROLE_IDS.BASIC
|
|
|
|
const res = await request
|
2021-04-14 07:25:43 +12:00
|
|
|
.put(`/api/users/metadata`)
|
2021-03-10 06:09:18 +13:00
|
|
|
.set(config.defaultHeaders())
|
|
|
|
.send(user)
|
|
|
|
.expect(200)
|
|
|
|
.expect("Content-Type", /json/)
|
|
|
|
expect(res.body.ok).toEqual(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("destroy", () => {
|
|
|
|
it("should be able to delete the user", async () => {
|
2021-04-20 03:26:33 +12:00
|
|
|
const user = await config.createUser()
|
2021-03-10 06:09:18 +13:00
|
|
|
const res = await request
|
2021-04-20 03:26:33 +12:00
|
|
|
.delete(`/api/users/metadata/${user._id}`)
|
2021-03-10 06:09:18 +13:00
|
|
|
.set(config.defaultHeaders())
|
|
|
|
.expect(200)
|
|
|
|
.expect("Content-Type", /json/)
|
|
|
|
expect(res.body.message).toBeDefined()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("find", () => {
|
|
|
|
it("should be able to find the user", async () => {
|
2021-04-20 03:26:33 +12:00
|
|
|
const user = await config.createUser()
|
2021-03-10 06:09:18 +13:00
|
|
|
const res = await request
|
2021-04-20 03:26:33 +12:00
|
|
|
.get(`/api/users/metadata/${user._id}`)
|
2021-03-10 06:09:18 +13:00
|
|
|
.set(config.defaultHeaders())
|
|
|
|
.expect(200)
|
|
|
|
.expect("Content-Type", /json/)
|
2021-04-20 03:26:33 +12:00
|
|
|
expect(res.body._id).toEqual(user._id)
|
2021-06-09 03:06:30 +12:00
|
|
|
expect(res.body.roleId).toEqual(BUILTIN_ROLE_IDS.ADMIN)
|
2021-03-10 06:09:18 +13:00
|
|
|
expect(res.body.tableId).toBeDefined()
|
|
|
|
})
|
2020-12-03 06:08:25 +13:00
|
|
|
})
|
2020-05-15 02:12:30 +12:00
|
|
|
})
|