Upping user test cases to cover all of controller.

This commit is contained in:
mike12345567 2021-03-09 17:09:18 +00:00
parent d9151cca0a
commit 929db83e99
3 changed files with 78 additions and 10 deletions

View File

@ -52,7 +52,7 @@ exports.create = async function(ctx) {
const response = await db.post(user)
ctx.status = 200
ctx.message = "User created successfully."
ctx.userId = response._id
ctx.userId = response.id
ctx.body = {
_rev: response.rev,
email,
@ -70,6 +70,9 @@ exports.update = async function(ctx) {
const db = new CouchDB(ctx.user.appId)
const user = ctx.request.body
let dbUser
if (user.email && !user._id) {
user._id = generateUserID(user.email)
}
// get user incase password removed
if (user._id) {
dbUser = await db.get(user._id)
@ -87,14 +90,15 @@ exports.update = async function(ctx) {
user._rev = response.rev
ctx.status = 200
ctx.message = `User ${ctx.request.body.email} updated successfully.`
ctx.body = response
}
exports.destroy = async function(ctx) {
const database = new CouchDB(ctx.user.appId)
await database.destroy(generateUserID(ctx.params.email))
ctx.message = `User ${ctx.params.email} deleted.`
ctx.body = {
message: `User ${ctx.params.email} deleted.`,
}
ctx.status = 200
}

View File

@ -42,15 +42,19 @@ describe("/users", () => {
})
describe("create", () => {
async function create(user, status = 200) {
return request
.post(`/api/users`)
.set(config.defaultHeaders())
.send(user)
.expect(status)
.expect("Content-Type", /json/)
}
it("returns a success message when a user is successfully created", async () => {
const body = basicUser(BUILTIN_ROLE_IDS.POWER)
body.email = "bill@budibase.com"
const res = await request
.post(`/api/users`)
.set(config.defaultHeaders())
.send(body)
.expect(200)
.expect("Content-Type", /json/)
const res = await create(body)
expect(res.res.statusMessage).toEqual("User created successfully.")
expect(res.body._id).toBeUndefined()
@ -68,5 +72,65 @@ describe("/users", () => {
failRole: BUILTIN_ROLE_IDS.PUBLIC,
})
})
it("should error if no email provided", async () => {
const user = basicUser(BUILTIN_ROLE_IDS.POWER)
delete user.email
await create(user, 400)
})
it("should error if no role provided", async () => {
const user = basicUser(null)
await create(user, 400)
})
it("should throw error if user exists already", async () => {
await config.createUser("test@test.com")
const user = basicUser(BUILTIN_ROLE_IDS.POWER)
user.email = "test@test.com"
await create(user, 400)
})
})
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
.put(`/api/users`)
.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 () => {
const email = "test@test.com"
await config.createUser(email)
const res = await request
.delete(`/api/users/${email}`)
.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 () => {
const email = "test@test.com"
await config.createUser(email)
const res = await request
.get(`/api/users/${email}`)
.set(config.defaultHeaders())
.expect(200)
.expect("Content-Type", /json/)
expect(res.body.email).toEqual(email)
expect(res.body.roleId).toEqual(BUILTIN_ROLE_IDS.POWER)
expect(res.body.tableId).toBeDefined()
})
})
})

View File

@ -21,7 +21,7 @@ router
controller.find
)
.put(
"/api/users/",
"/api/users",
authorized(PermissionTypes.USER, PermissionLevels.WRITE),
controller.update
)