From 0d33608b56381692e5cb0bc90b60e05377fc04d0 Mon Sep 17 00:00:00 2001 From: am-ons <171134774+am-ons@users.noreply.github.com> Date: Thu, 20 Jun 2024 15:36:04 +0100 Subject: [PATCH] feat: Add all tests to cover the code in /api/users DELETE endpoint --- server/BlaiseAPI/index.ts | 3 ++ server/tests/index.test.ts | 60 +++++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/server/BlaiseAPI/index.ts b/server/BlaiseAPI/index.ts index 9ae46cd1..46a45e41 100644 --- a/server/BlaiseAPI/index.ts +++ b/server/BlaiseAPI/index.ts @@ -47,6 +47,9 @@ export default function BlaiseAPIRouter(config: CustomConfig, auth: Auth, blaise router.post("/api/users", auth.Middleware, async function (req: Request, res: Response) { const data = req.body; + if(!data.role){ + return res.status(400).json(); + } const roleServerParksOverride = config.RoleToServerParksMap[data.role]; if (roleServerParksOverride != null) { data.serverParks = roleServerParksOverride; diff --git a/server/tests/index.test.ts b/server/tests/index.test.ts index 1ee711dc..4b1b5588 100644 --- a/server/tests/index.test.ts +++ b/server/tests/index.test.ts @@ -50,15 +50,7 @@ describe("app engine start", () => { import role_to_serverparks_map from "../role-to-serverparks-map.json"; import { size } from "lodash"; describe("Test /api/users POST endpoint", () => { - beforeEach(() => { - blaiseApiMock.reset(); - }); - - afterAll(() => { - blaiseApiMock.reset(); - }); - - it("should call Blaise API createUser endpoint with correct serverParks for each role EXISTING in server/role-to-serverparks-map.json and return http status 200", async () => { + it("should call Blaise API createUser endpoint with correct serverParks for each role EXISTING in server/role-to-serverparks-map.json AND return http status OK_200", async () => { let currentRoleNo = 0; const totalRoleCount = size(role_to_serverparks_map); for(const roleName in role_to_serverparks_map) @@ -90,7 +82,7 @@ describe("Test /api/users POST endpoint", () => { } }); - it("should call Blaise API createUser endpoint with DEFAULT serverParks for a role MISSING in server/role-to-serverparks-map.json and return http status 200", async () => { + it("should call Blaise API createUser endpoint with DEFAULT serverParks for a role MISSING in server/role-to-serverparks-map.json AND return http status OK_200)", async () => { const roleName = "this role is missing in server/role-to-serverparks-map.json file"; const spmap = role_to_serverparks_map.DEFAULT; const newUser : NewUser = { @@ -114,4 +106,52 @@ describe("Test /api/users POST endpoint", () => { )), Times.exactly(1)); expect(response.body).toStrictEqual(newUser); }); + + it("should return http status BAD_REQUEST_400 if role is empty OR hasn't been specified in the request", async () => { + let response = await sut.post("/api/users") + .field("role", ""); + expect(response.statusCode).toEqual(400); + + response = await sut.post("/api/users") + expect(response.statusCode).toEqual(400); + }); +}); + +describe("Test /api/users DELETE endpoint", () => { + beforeEach(() => { + blaiseApiMock.reset(); + }); + + afterAll(() => { + blaiseApiMock.reset(); + }); + + it("should call Blaise API deleteUser endpoint for VALID user header field AND return http status NO_CONTENT_204", async () => { + blaiseApiMock.setup((api) => api.deleteUser(It.isAny())).returns(_ => Promise.resolve(null)); + const username = "user-123"; + let response = await sut.delete("/api/users") + .set("user", username); + + expect(response.statusCode).toEqual(204); + blaiseApiMock.verify(a => a.deleteUser(It.isValue(username)), Times.once()); + }); + + it("should call Blaise API deleteUser endpoint for INVALID or MISSING user header field AND return http status BAD_REQUEST_400", async () => { + let response = await sut.delete("/api/users") + .set("user", ""); + expect(response.statusCode).toEqual(400); + + response = await sut.delete("/api/users"); + expect(response.statusCode).toEqual(400); + }); +}); + +/* +beforeEach(() => { + blaiseApiMock.reset(); +}); + +afterAll(() => { + blaiseApiMock.reset(); }); +*/