From 742f0d6457ee08e36bc17c63fd4af9c5fd4cd73e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lud=C4=9Bk=20Nov=C3=BD?= <13610612+ludeknovy@users.noreply.github.com> Date: Fri, 19 Apr 2024 14:47:32 +0200 Subject: [PATCH] Update exception handling in initUserController 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. --- .../auth/init-user-controller.spec.ts | 73 +++++++++++-------- .../controllers/auth/init-user-controller.ts | 3 +- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/src/server/controllers/auth/init-user-controller.spec.ts b/src/server/controllers/auth/init-user-controller.spec.ts index fa9b293e..3f992a5d 100644 --- a/src/server/controllers/auth/init-user-controller.spec.ts +++ b/src/server/controllers/auth/init-user-controller.spec.ts @@ -6,37 +6,50 @@ import { AllowedRoles } from "../../middleware/authorization-middleware" jest.mock("../../../db/db") const mockResponse = () => { - const res: Partial = {} - res.send = jest.fn().mockReturnValue(res) - res.status = jest.fn().mockReturnValue(res) - return res + const res: Partial = {} + 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.") + }) }) diff --git a/src/server/controllers/auth/init-user-controller.ts b/src/server/controllers/auth/init-user-controller.ts index 4410adf1..7c44ceb1 100644 --- a/src/server/controllers/auth/init-user-controller.ts +++ b/src/server/controllers/auth/init-user-controller.ts @@ -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) {