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

89 lines
2.4 KiB
JavaScript
Raw Normal View History

const {
2020-05-15 02:12:30 +12:00
createApplication,
supertest,
defaultHeaders,
createUser,
2020-05-28 04:23:01 +12:00
testPermissionsForEndpoint,
2020-05-15 02:12:30 +12:00
} = require("./couchTestUtils")
2020-05-28 04:23:01 +12:00
const {
POWERUSER_LEVEL_ID,
LIST_USERS,
USER_MANAGEMENT
} = require("../../../utilities/accessLevels")
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 instanceId
2020-04-10 03:53:48 +12:00
beforeAll(async () => {
2020-05-15 02:12:30 +12:00
({ request, server } = await supertest(server))
});
beforeEach(async () => {
app = await createApplication(request)
instanceId = app.instance._id
2020-04-10 03:53:48 +12:00
});
2020-10-29 11:37:58 +13:00
afterAll(() => {
server.close()
server.destroy()
})
describe("fetch", () => {
2020-05-15 02:12:30 +12:00
it("returns a list of users from an instance db", async () => {
await createUser(request, instanceId, "brenda", "brendas_password")
await createUser(request, instanceId, "pam", "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`)
.set(defaultHeaders(instanceId))
2020-04-11 03:37:59 +12:00
.expect('Content-Type', /json/)
.expect(200)
2020-05-15 02:12:30 +12:00
expect(res.body.length).toBe(2)
expect(res.body.find(u => u.username === "brenda")).toBeDefined()
expect(res.body.find(u => u.username === "pam")).toBeDefined()
})
2020-04-25 05:02:51 +12:00
2020-05-28 04:23:01 +12:00
it("should apply authorization to endpoint", async () => {
await createUser(request, instanceId, "brenda", "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`,
instanceId: instanceId,
2020-05-28 04:23:01 +12:00
permissionName: LIST_USERS,
})
})
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", () => {
2020-04-11 03:37:59 +12:00
2020-05-15 02:12:30 +12:00
it("returns a success message when a user is successfully created", async () => {
const res = await request
2020-06-19 07:41:37 +12:00
.post(`/api/users`)
.set(defaultHeaders(instanceId))
2020-05-28 04:23:01 +12:00
.send({ name: "Bill", username: "bill", password: "bills_password", accessLevelId: POWERUSER_LEVEL_ID })
2020-04-10 03:53:48 +12:00
.expect(200)
2020-05-15 02:12:30 +12:00
.expect('Content-Type', /json/)
expect(res.res.statusMessage).toEqual("User created successfully.");
expect(res.body._id).toBeUndefined()
})
2020-05-28 04:23:01 +12:00
it("should apply authorization to endpoint", async () => {
await testPermissionsForEndpoint({
request,
method: "POST",
body: { name: "brandNewUser", username: "brandNewUser", password: "yeeooo", accessLevelId: POWERUSER_LEVEL_ID },
2020-06-19 07:41:37 +12:00
url: `/api/users`,
instanceId: instanceId,
2020-05-28 04:23:01 +12:00
permissionName: USER_MANAGEMENT,
})
})
2020-05-15 02:12:30 +12:00
});
})