Skip to content

Commit

Permalink
Update exception handling in initUserController
Browse files Browse the repository at this point in the history
Refactored the initUserController by improving exception handling for migration not finished scenarios. Updated the test cases to include checks for unfinished migrations and removed a redundant console.log statement. This will enhance the error handling capability of the initialization process, making it more robust and informative to the end-user.
  • Loading branch information
ludeknovy committed Apr 19, 2024
1 parent b4a384a commit 742f0d6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
73 changes: 43 additions & 30 deletions src/server/controllers/auth/init-user-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,50 @@ import { AllowedRoles } from "../../middleware/authorization-middleware"

jest.mock("../../../db/db")
const mockResponse = () => {
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}

describe("initUserController", () => {
it("should call createNewUserController if no user exists", async function () {
const querySpy = jest.spyOn(require("../users/create-new-user-controller"), "createNewUserController");
(db.manyOrNone as any).mockResolvedValue([])
const nextFunction: NextFunction = jest.fn()
const response = mockResponse()
const request = { body: { username: "test", password: "123" } }
await initUserController(
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response, nextFunction)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(request.body).toEqual({ username: "test", password: "123", role: AllowedRoles.Admin })
})
it("should return an error if user already exists", async function () {
const querySpy = jest.spyOn(require("boom"), "forbidden");

(db.manyOrNone as any).mockResolvedValue(["test"])
const nextFunction: NextFunction = jest.fn()
const response = mockResponse()
const request = {}
await initUserController(
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response, nextFunction)
expect(nextFunction).toHaveBeenCalledTimes(1)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(querySpy).toHaveBeenCalledWith("User was already initialized")
})
it("should call createNewUserController if no user exists", async function () {
const querySpy = jest.spyOn(require("../users/create-new-user-controller"), "createNewUserController");
(db.oneOrNone as any).mockResolvedValueOnce({}); // migration has finished
(db.manyOrNone as any).mockResolvedValue([])
const nextFunction: NextFunction = jest.fn()
const response = mockResponse()
const request = { body: { username: "test", password: "123" } }
await initUserController(
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response, nextFunction)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(request.body).toEqual({ username: "test", password: "123", role: AllowedRoles.Admin })
})
it("should return an error if user already exists", async function () {
const querySpy = jest.spyOn(require("boom"), "forbidden");
(db.oneOrNone as any).mockResolvedValueOnce({}); // migration has finished
(db.manyOrNone as any).mockResolvedValue(["test"])
const nextFunction: NextFunction = jest.fn()
const response = mockResponse()
const request = {}
await initUserController(
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response, nextFunction)
expect(nextFunction).toHaveBeenCalledTimes(1)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(querySpy).toHaveBeenCalledWith("User was already initialized")
})
it("should throw exception when migrations have not finished", async () => {
const spy = jest.spyOn(require("boom"), "preconditionRequired")
const nextFunction: NextFunction = jest.fn()
const response = mockResponse()
const request = {}
await initUserController(
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response, nextFunction)
expect(nextFunction).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith("Migrations were not finished, please try it again later.")
})
})
3 changes: 1 addition & 2 deletions src/server/controllers/auth/init-user-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { MigrationNotFinishedException } from "../../errors/migration-not-finish
export const initUserController = async (req: Request, res: Response, next: NextFunction) => {
try {
const roleMigration = await db.oneOrNone(getRoleMigration())
console.log(roleMigration)
if (!roleMigration) {
throw MigrationNotFinishedException
throw new MigrationNotFinishedException("role migration has not finished")
}
const users = await db.manyOrNone(getUsers())
if (users && users.length > 0) {
Expand Down

0 comments on commit 742f0d6

Please sign in to comment.