Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Created admin handlers and corresponding unit tests #19

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const customJestConfig = {
// if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
moduleDirectories: ["node_modules", "<rootDir>/"],
testEnvironment: "jest-environment-jsdom",
modulePathIgnorePatterns: ["old"]
modulePathIgnorePatterns: ["old", "<rootDir>/lib/buildings"]
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
Expand Down
53 changes: 53 additions & 0 deletions pages/api/admin/deregisterAll.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { NextApiRequest, NextApiResponse } from "next";
import { initDB } from "../../../lib/db/init";
import handler from "./deregisterAll";
import { register } from "../../../lib/api/users/user";
import { ECS_ID, ELW_ID } from "../../../lib/locker_constants";

const mockedJson = jest.fn();

const mockedRes = {
status: jest.fn().mockReturnValue({ json: mockedJson }),
} as unknown as jest.Mocked<NextApiResponse>;

initDB();

describe("Testing deregisterAll handler", () => {
it("Returns HTTP 200 when all deregistrations were successful", () => {

for (let i = 65; i <= 90; i++) {
let name = String.fromCharCode(i)
let email = String.fromCharCode(i) + "@example.ca"
let registrations = register(ELW_ID, i, name, email, false);
}

const req = {
method: "GET",
query: {}
} as unknown as NextApiRequest;

handler(req, mockedRes)

expect(mockedRes.status).toHaveBeenCalledWith(200)
});

it("Returns 26 deregistrations", () => {

const req = {
method: "GET",
query: {}
} as unknown as NextApiRequest;

for (let i = 65; i <= 90; i++) {
let name = String.fromCharCode(i) + String.fromCharCode(i)
let email = String.fromCharCode(i) + "@thing.ca"
let registrations = register(ECS_ID, i, name, email, false);
}

handler(req, mockedRes)

expect(mockedJson).toHaveBeenLastCalledWith(26)
});


})
14 changes: 14 additions & 0 deletions pages/api/admin/deregisterAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {NextApiRequest, NextApiResponse} from 'next';
import { deregisterAll } from '../../../lib/api/admin/admin';


export default function handler (req: NextApiRequest, res: NextApiResponse) {
if (req.method !== "GET") {
return res.status(405).json({ message: "Method not allowed" });
}
const deregistered_list = deregisterAll()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should rename deregistered_list to something more descriptive (e.g. deregister_count)


return res.status(200).json(deregistered_list);

}

39 changes: 39 additions & 0 deletions pages/api/admin/getLockers.test.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should include a test case for calling with a non-GET method for better coverage.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { NextApiRequest, NextApiResponse } from "next";
import { initDB } from "../../../lib/db/init";
import handler from "./getLockers";
import { ECS_ID, ELW_ID } from "../../../lib/locker_constants";


const mockedJson = jest.fn();

const mockedRes = {
status: jest.fn().mockReturnValue({ json: mockedJson }),
} as unknown as jest.Mocked<NextApiResponse>;

initDB();

describe("Testing getLockers handler", () => {
it("Returns HTTP 200 when it retrieves all lockers lockers in the database", () => {
const req = {
method: "GET",
query: {}
} as unknown as NextApiRequest;

handler(req, mockedRes)

expect(mockedRes.status).toHaveBeenCalledWith(200)
});

it("Returns 838 lockers in the database", () => {
const req = {
method: "GET",
query: {}
} as unknown as NextApiRequest;

handler(req, mockedRes)

expect(mockedJson.mock.calls[1][0]).toHaveLength(838)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use ECS_COUNT and ELW_COUNT whenever possible

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than 838, we should use ECS_COUNT + ELW_COUNT to make it more clear how we get 838.

});


})
14 changes: 14 additions & 0 deletions pages/api/admin/getLockers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {NextApiRequest, NextApiResponse} from 'next';
import { getLockers } from '../../../lib/api/admin/admin';


export default function handler (req: NextApiRequest, res: NextApiResponse) {
if (req.method !== "GET") {
return res.status(405).json({ message: "Method not allowed" });
}
const lockers = getLockers()

return res.status(200).json(lockers);

}

46 changes: 46 additions & 0 deletions pages/api/admin/getRegistrations.test.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should include a test case for calling with a non-GET method for better coverage.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { NextApiRequest, NextApiResponse } from "next";
import { initDB } from "../../../lib/db/init";
import { register } from "../../../lib/api/users/user";
import handler from "./getRegistrations";
import { ECS_ID, ELW_ID } from "../../../lib/locker_constants";


const mockedJson = jest.fn();

const mockedRes = {
status: jest.fn().mockReturnValue({ json: mockedJson }),
} as unknown as jest.Mocked<NextApiResponse>;

initDB();
edorableraf marked this conversation as resolved.
Show resolved Hide resolved

describe("Testing getRegistration handler", () => {
it("Returns HTTP 200 when it retrieves all registrations in the database", () => {

let registrations = register(ELW_ID, 100, "Jeremy Pine", "jpine@example.com", false);

const req = {
method: "GET",
query: {}
} as unknown as NextApiRequest;

handler(req, mockedRes)

expect(mockedRes.status).toHaveBeenCalledWith(200)
});

it("Returns 2 registrations in the database", () => {

let registrations = register(ELW_ID, 101, "Paul Willow", "pwillow@example.com", false);

const req = {
method: "GET",
query: {}
} as unknown as NextApiRequest;

handler(req, mockedRes)

expect(mockedJson).toHaveBeenNthCalledWith(1, [{"building_id": 1, "num": 100, "reported_at": null, "user_id": 1}])
edorableraf marked this conversation as resolved.
Show resolved Hide resolved
});


})
14 changes: 14 additions & 0 deletions pages/api/admin/getRegistrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {NextApiRequest, NextApiResponse} from 'next';
import { getRegistrations } from '../../../lib/api/admin/admin';


export default function handler (req: NextApiRequest, res: NextApiResponse) {
if (req.method !== "GET") {
return res.status(405).json({ message: "Method not allowed" });
}
const registrations = getRegistrations()

return res.status(200).json(registrations);

}

46 changes: 46 additions & 0 deletions pages/api/admin/getUsers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { NextApiRequest, NextApiResponse } from "next";
import { initDB } from "../../../lib/db/init";
import { register } from "../../../lib/api/users/user";
import handler from "./getRegistrations";
import { ECS_ID, ELW_ID } from "../../../lib/locker_constants";


const mockedJson = jest.fn();

const mockedRes = {
status: jest.fn().mockReturnValue({ json: mockedJson }),
} as unknown as jest.Mocked<NextApiResponse>;

initDB();

describe("Testing getUsers handler", () => {
it("Returns HTTP 200 when it retrieves all registered users in the database", () => {

let registrations = register(ELW_ID, 100, "Jeremy Pine", "jpine@example.com", false);

const req = {
method: "GET",
query: {}
} as unknown as NextApiRequest;

handler(req, mockedRes)

expect(mockedRes.status).toHaveBeenCalledWith(200)
});

it("Returns 2 users in the database", () => {

let registrations = register(ELW_ID, 102, "Paul Willow", "pwillow@example.com", false);

const req = {
method: "GET",
query: {}
} as unknown as NextApiRequest;

handler(req, mockedRes)

expect(mockedJson.mock.calls[1][0]).toHaveLength(2)
});


})
14 changes: 14 additions & 0 deletions pages/api/admin/getUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {NextApiRequest, NextApiResponse} from 'next';
import { getUsers } from '../../../lib/api/admin/admin';


export default function handler (req: NextApiRequest, res: NextApiResponse) {
if (req.method !== "GET") {
return res.status(405).json({ message: "Method not allowed" });
}
const users = getUsers()

return res.status(200).json(users);

}

95 changes: 95 additions & 0 deletions pages/api/admin/resolve.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { NextApiRequest, NextApiResponse } from "next";
import { initDB } from "../../../lib/db/init";
import handler from "./resolve";
import { register, report } from "../../../lib/api/users/user";
import { ECS_ID, ELW_ID } from "../../../lib/locker_constants";

const mockedJson = jest.fn();

const mockedRes = {
status: jest.fn().mockReturnValue({ json: mockedJson }),
} as unknown as jest.Mocked<NextApiResponse>;

initDB();
edorableraf marked this conversation as resolved.
Show resolved Hide resolved


describe("Testing resolve handler", () => {

it("Returns HTTP 200 when the locker is resolved successfully", async () => {
const req = {
method: "GET",
query: {
building: ECS_ID,
number: 200
},
} as unknown as NextApiRequest;

let registrations = register(ECS_ID, 200, "Liam Oak", "loak@example.com", false);
let reported_val= report(ECS_ID, 200);

handler(req, mockedRes);

expect(mockedRes.status).toHaveBeenCalledWith(200);
});

it("Returns true when the locker is resolved successfully", async () => {

let registrations = register(ECS_ID, 250, "Paula Kettle", "pkettle@uvic.ca", false);
let reported_val= report(ECS_ID, 250);

const req = {
method: "GET",
query: {
building: ECS_ID,
number: 250
},
} as unknown as NextApiRequest;

handler(req, mockedRes);

expect(mockedJson).toHaveBeenLastCalledWith(true);
});

it("Returns an error message when the locker was not reported", async () => {
const req = {
method: "GET",
query: {
building: ELW_ID,
number: 100
},
} as unknown as NextApiRequest;

handler(req, mockedRes);

expect(mockedJson).toHaveBeenLastCalledWith({ message: "Unable to resolve locker because it doesn't exist or it has not been reported" });
});

it("Returns HTTP 404 when the locker does not exist", async () => {
const req = {
method: "GET",
query: {
building: ELW_ID,
number: 1000
},
} as unknown as NextApiRequest;

handler(req, mockedRes);

expect(mockedRes.status).toHaveBeenLastCalledWith(404);
});

it("Returns HTTP 400 when the query is invalid", async () => {
const req = {
method: "GET",
query: {
building: "hello",
number: "world"
},
} as unknown as NextApiRequest;

handler(req, mockedRes);

expect(mockedRes.status).toHaveBeenLastCalledWith(400);
});

})
23 changes: 23 additions & 0 deletions pages/api/admin/resolve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { NextApiRequest, NextApiResponse } from "next";
import { resolve } from "../../../lib/api/admin/admin";

export default function handler(req:NextApiRequest, res:NextApiResponse) {
if (req.method !== "GET") {
return res.status(405).json({message: "Method not allowed"})
}

const building = parseInt(req.query.building as string);
const number = parseInt(req.query.number as string);

if (isNaN(building) || isNaN(number)) {
return res.status(400).json({ message: "Invalid query" });
}

const resolution= resolve(building, number);

if (!resolution) {
return res.status(404).json({ message: "Unable to resolve locker because it doesn't exist or it has not been reported" });
}

return res.status(200).json(resolution);
}