1
0
Fork 0
mirror of synced 2024-09-29 08:41:16 +13:00
budibase/packages/server/src/api/routes/tests/user.spec.js

93 lines
2.5 KiB
JavaScript
Raw Normal View History

const {
2020-05-28 04:23:01 +12:00
testPermissionsForEndpoint,
2020-05-15 02:12:30 +12:00
} = require("./couchTestUtils")
const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles")
2021-03-05 05:54:44 +13:00
const TestConfig = require("./utilities/TestConfiguration")
const { cloneDeep } = require("lodash/fp")
const baseBody = {
email: "bill@bill.com",
password: "yeeooo",
roleId: BUILTIN_ROLE_IDS.POWER,
}
2020-04-10 03:53:48 +12:00
describe("/users", () => {
2020-05-15 02:12:30 +12:00
let request
let server
let app
let appId
2021-03-05 05:54:44 +13:00
let config
2020-04-10 03:53:48 +12:00
beforeAll(async () => {
2021-03-05 05:54:44 +13:00
config = new TestConfig()
request = config.request
})
2020-05-15 02:12:30 +12:00
beforeEach(async () => {
2021-03-05 05:54:44 +13:00
app = await config.init()
appId = app.instance._id
})
2020-04-10 03:53:48 +12:00
2020-10-29 11:37:58 +13:00
afterAll(() => {
2021-03-05 05:54:44 +13:00
config.end()
2020-10-29 11:37:58 +13:00
})
describe("fetch", () => {
2020-05-15 02:12:30 +12:00
it("returns a list of users from an instance db", async () => {
2021-03-05 05:54:44 +13:00
await config.createUser("brenda@brenda.com", "brendas_password")
await config.createUser("pam@pam.com", "pam_password")
2020-05-15 02:12:30 +12:00
const res = await request
2020-06-19 03:59:31 +12:00
.get(`/api/users`)
2021-03-05 05:54:44 +13:00
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
2020-04-11 03:37:59 +12:00
.expect(200)
2020-05-15 02:12:30 +12:00
expect(res.body.length).toBe(2)
2020-12-05 01:22:45 +13:00
expect(res.body.find(u => u.email === "brenda@brenda.com")).toBeDefined()
expect(res.body.find(u => u.email === "pam@pam.com")).toBeDefined()
2020-05-15 02:12:30 +12:00
})
2020-04-25 05:02:51 +12:00
2020-05-28 04:23:01 +12:00
it("should apply authorization to endpoint", async () => {
2021-03-05 05:54:44 +13:00
await config.createUser("brenda@brenda.com", "brendas_password")
2020-05-28 04:23:01 +12:00
await testPermissionsForEndpoint({
request,
method: "GET",
2020-06-19 07:41:37 +12:00
url: `/api/users`,
appId: appId,
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
2020-05-15 02:12:30 +12:00
describe("create", () => {
it("returns a success message when a user is successfully created", async () => {
const body = cloneDeep(baseBody)
body.email = "bill@budibase.com"
2020-05-15 02:12:30 +12:00
const res = await request
2020-06-19 07:41:37 +12:00
.post(`/api/users`)
.set(defaultHeaders(appId))
.send(body)
2020-04-10 03:53:48 +12:00
.expect(200)
.expect("Content-Type", /json/)
2020-05-15 02:12:30 +12:00
expect(res.res.statusMessage).toEqual("User created successfully.")
2020-05-15 02:12:30 +12:00
expect(res.body._id).toBeUndefined()
})
2020-05-28 04:23:01 +12:00
it("should apply authorization to endpoint", async () => {
const body = cloneDeep(baseBody)
body.email = "brandNewUser@user.com"
2020-05-28 04:23:01 +12:00
await testPermissionsForEndpoint({
request,
method: "POST",
body,
2020-06-19 07:41:37 +12:00
url: `/api/users`,
appId: appId,
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
})