-
Notifications
You must be signed in to change notification settings - Fork 1
Created admin handlers and corresponding unit tests #19
base: main
Are you sure you want to change the base?
Changes from 1 commit
a1a89c9
533f9fa
408caae
2835bb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
}); | ||
|
||
|
||
}) |
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() | ||
|
||
return res.status(200).json(deregistered_list); | ||
|
||
} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than |
||
}); | ||
|
||
|
||
}) |
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); | ||
|
||
} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
}); | ||
|
||
|
||
}) |
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); | ||
|
||
} | ||
|
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) | ||
}); | ||
|
||
|
||
}) |
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); | ||
|
||
} | ||
|
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); | ||
}); | ||
|
||
}) |
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); | ||
} |
There was a problem hiding this comment.
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)