Skip to content

Commit

Permalink
linter fixes, unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeknovy committed Oct 13, 2023
1 parent e957b0b commit f4d0619
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getGlobalSettingsController } from "./get-global-settings-controller"
import { Response } from "express"
import { AllowedRoles } from "../../middleware/authorization-middleware"
import { IGetUserAuthInfoRequest } from "../../middleware/request.model"
import { db } from "../../../db/db"

jest.mock("../../../db/db")
const mockResponse = () => {
const res: Partial<Response> = {}
res.json = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}
describe("getGlobalSettingsController", () => {
it("should return global settings", async () => {
const response = mockResponse()
const querySpy = jest.spyOn(require("../../queries/global-settings"), "getGlobalSettings")
const request = {
user: {
role: AllowedRoles.Admin,
},
}
db.one = jest.fn().mockReturnValueOnce({ project_auto_provisioning: true })
await getGlobalSettingsController(
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(response.json).toHaveBeenCalledTimes(1)
expect(response.json).toHaveBeenCalledWith({ projectAutoProvisioning: true })
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { db } from "../../../db/db"
import { AllowedRoles } from "../../middleware/authorization-middleware"
import { IGetUserAuthInfoRequest } from "../../middleware/request.model"
import { Response } from "express"
import { updateGlobalSettingsController } from "./update-global-settings-controller"

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

describe("updateGlobalSettingsController", () => {
it("should save the new settings into database", async () => {
const response = mockResponse()
const querySpy = jest.spyOn(require("../../queries/global-settings"), "updateGlobalSettings")
const request = {
user: {
role: AllowedRoles.Admin,
},
body: {
projectAutoProvisioning: true,
},
}
db.none = jest.fn().mockResolvedValueOnce(null)

await updateGlobalSettingsController(
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(querySpy).toHaveBeenCalledWith(true)
expect(response.send).toHaveBeenCalledTimes(1)
})
})
2 changes: 1 addition & 1 deletion src/server/middleware/project-exists-middleware.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IGetUserAuthInfoRequest } from "./request.model"
import { Response, NextFunction } from "express"
import {projectAutoProvisioningMiddleware, projectExistsMiddleware} from "./project-exists-middleware"
import { projectAutoProvisioningMiddleware, projectExistsMiddleware } from "./project-exists-middleware"
import { db } from "../../db/db"
import Boom = require("boom")

Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { getItemChartSettingsController } from "../controllers/item/get-item-cha
import { AllowedRoles, authorizationMiddleware } from "../middleware/authorization-middleware"
import { authenticationMiddleware } from "../middleware/authentication-middleware"
import { getRequestStatsExportController } from "../controllers/item/get-request-stats-export-controller"
import {projectAutoProvisioningMiddleware, projectExistsMiddleware} from "../middleware/project-exists-middleware"
import { projectAutoProvisioningMiddleware, projectExistsMiddleware } from "../middleware/project-exists-middleware"

export class ItemsRoutes {

Expand Down

0 comments on commit f4d0619

Please sign in to comment.