diff --git a/src/server/controllers/auth/init-user-controller.spec.ts b/src/server/controllers/auth/init-user-controller.spec.ts index fa9b293..3f992a5 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 4410adf..7c44ceb 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) {