2020-10-29 09:35:06 +13:00
|
|
|
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
|
2020-10-29 09:35:06 +13:00
|
|
|
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 () => {
|
2020-10-29 09:35:06 +13:00
|
|
|
app = await createApplication(request)
|
|
|
|
instanceId = app.instances[0]._id
|
2020-04-10 03:53:48 +12:00
|
|
|
});
|
|
|
|
|
2020-10-29 09:35:06 +13:00
|
|
|
describe("fetch", () => {
|
2020-05-15 02:12:30 +12:00
|
|
|
it("returns a list of users from an instance db", async () => {
|
2020-10-29 09:35:06 +13:00
|
|
|
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`)
|
2020-10-29 09:35:06 +13:00
|
|
|
.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 () => {
|
2020-10-29 09:35:06 +13:00
|
|
|
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`,
|
2020-10-29 09:35:06 +13:00
|
|
|
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`)
|
2020-10-29 09:35:06 +13:00
|
|
|
.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`,
|
2020-10-29 09:35:06 +13:00
|
|
|
instanceId: instanceId,
|
2020-05-28 04:23:01 +12:00
|
|
|
permissionName: USER_MANAGEMENT,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-15 02:12:30 +12:00
|
|
|
});
|
|
|
|
})
|