From b6e6f81d15743d9615c043c0dd3d13d1f1445bf2 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 25 Feb 2022 19:01:17 +0000 Subject: [PATCH] Adding test cases for user implementation with mocks. --- .../server/src/api/controllers/public/tables.ts | 5 ++++- .../server/src/api/controllers/public/users.ts | 2 +- .../src/api/routes/public/tests/compare.spec.js | 14 ++++++++++---- packages/server/src/utilities/workerRequests.js | 12 +++++++++--- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/server/src/api/controllers/public/tables.ts b/packages/server/src/api/controllers/public/tables.ts index 931cfeecf4..7d09e9820d 100644 --- a/packages/server/src/api/controllers/public/tables.ts +++ b/packages/server/src/api/controllers/public/tables.ts @@ -31,7 +31,10 @@ export async function read(ctx: any) { } export async function update(ctx: any) { - ctx.request.body = await addRev(fixTable(ctx.request.body, ctx.params), ctx.params.tableId) + ctx.request.body = await addRev( + fixTable(ctx.request.body, ctx.params), + ctx.params.tableId + ) await controller.save(ctx) ctx.body = { table: ctx.body } } diff --git a/packages/server/src/api/controllers/public/users.ts b/packages/server/src/api/controllers/public/users.ts index afb17b50e2..c8728c7de7 100644 --- a/packages/server/src/api/controllers/public/users.ts +++ b/packages/server/src/api/controllers/public/users.ts @@ -21,7 +21,7 @@ function fixUser(ctx: any) { function getUser(ctx: any, userId?: string) { if (userId) { - ctx.params = {userId} + ctx.params = { userId } } else if (!ctx.params?.userId) { throw "No user ID provided for getting" } diff --git a/packages/server/src/api/routes/public/tests/compare.spec.js b/packages/server/src/api/routes/public/tests/compare.spec.js index c91be18217..d84204acdc 100644 --- a/packages/server/src/api/routes/public/tests/compare.spec.js +++ b/packages/server/src/api/routes/public/tests/compare.spec.js @@ -147,25 +147,31 @@ describe("check the rows endpoints", () => { }) describe("check the users endpoints", () => { + let user it("should allow retrieving users through search", async () => { + user = await config.createUser() const res = await makeRequest("post", "/users/search") expect(res).toSatisfyApiSpec() }) it("should allow creating a user", async () => { - + const res = await makeRequest("post", "/users") + expect(res).toSatisfyApiSpec() }) it("should allow updating a user", async () => { - + const res = await makeRequest("put", `/users/${user._id}`) + expect(res).toSatisfyApiSpec() }) it("should allow retrieving a user", async () => { - + const res = await makeRequest("get", `/users/${user._id}`) + expect(res).toSatisfyApiSpec() }) it("should allow deleting a user", async () => { - + const res = await makeRequest("delete", `/users/${user._id}`) + expect(res).toSatisfyApiSpec() }) }) diff --git a/packages/server/src/utilities/workerRequests.js b/packages/server/src/utilities/workerRequests.js index d98e8eb8d1..1ee975f13e 100644 --- a/packages/server/src/utilities/workerRequests.js +++ b/packages/server/src/utilities/workerRequests.js @@ -39,7 +39,9 @@ async function checkResponse(response, errorMsg, { ctx } = {}) { } catch (err) { error = await response.text() } - const msg = `Unable to ${errorMsg} - ${error.message ? error.message : error}` + const msg = `Unable to ${errorMsg} - ${ + error.message ? error.message : error + }` if (ctx) { ctx.throw(400, msg) } else { @@ -116,7 +118,9 @@ exports.saveGlobalUser = async ctx => { exports.deleteGlobalUser = async ctx => { const response = await fetch( - checkSlashesInUrl(env.WORKER_URL + `/api/global/users/${ctx.params.userId}`), + checkSlashesInUrl( + env.WORKER_URL + `/api/global/users/${ctx.params.userId}` + ), // we don't want to use API key when getting self request(ctx, { method: "DELETE" }) ) @@ -125,7 +129,9 @@ exports.deleteGlobalUser = async ctx => { exports.readGlobalUser = async ctx => { const response = await fetch( - checkSlashesInUrl(env.WORKER_URL + `/api/global/users/${ctx.params.userId}`), + checkSlashesInUrl( + env.WORKER_URL + `/api/global/users/${ctx.params.userId}` + ), // we don't want to use API key when getting self request(ctx, { method: "GET" }) )