Skip to content

Commit

Permalink
feat: Add all tests to cover the code in /api/users DELETE endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
am-ons committed Jun 20, 2024
1 parent e578bd6 commit 0d33608
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
3 changes: 3 additions & 0 deletions server/BlaiseAPI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
60 changes: 50 additions & 10 deletions server/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = {
Expand All @@ -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<string>(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();
});
*/

0 comments on commit 0d33608

Please sign in to comment.