From 929db83e99018c7bd14246fbe58ea708eeb51047 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 9 Mar 2021 17:09:18 +0000 Subject: [PATCH] Upping user test cases to cover all of controller. --- packages/server/src/api/controllers/user.js | 10 ++- .../server/src/api/routes/tests/user.spec.js | 76 +++++++++++++++++-- packages/server/src/api/routes/user.js | 2 +- 3 files changed, 78 insertions(+), 10 deletions(-) diff --git a/packages/server/src/api/controllers/user.js b/packages/server/src/api/controllers/user.js index c100f43d88..3dd28284be 100644 --- a/packages/server/src/api/controllers/user.js +++ b/packages/server/src/api/controllers/user.js @@ -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 } diff --git a/packages/server/src/api/routes/tests/user.spec.js b/packages/server/src/api/routes/tests/user.spec.js index 6ec607a093..5e7ec9e9d4 100644 --- a/packages/server/src/api/routes/tests/user.spec.js +++ b/packages/server/src/api/routes/tests/user.spec.js @@ -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() + }) }) }) diff --git a/packages/server/src/api/routes/user.js b/packages/server/src/api/routes/user.js index 1ad1d2363e..cdaab0cc5b 100644 --- a/packages/server/src/api/routes/user.js +++ b/packages/server/src/api/routes/user.js @@ -21,7 +21,7 @@ router controller.find ) .put( - "/api/users/", + "/api/users", authorized(PermissionTypes.USER, PermissionLevels.WRITE), controller.update )