Skip to content

Commit

Permalink
project exists middleware (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeknovy authored Sep 6, 2023
1 parent 43509f7 commit f802d0f
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 55 deletions.
1 change: 1 addition & 0 deletions integration.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ module.exports = {
verbose: true,
setupFilesAfterEnv: ["<rootDir>/src/tests/integration/helper/setup.ts"],
reporters: ["default"],
testTimeout: 20000,
}
2 changes: 1 addition & 1 deletion src/server/middleware/authentication-middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("AuthenticationMiddleware", () => {
})

describe("Query token", () => {
it("should return unathorized when invalid query token provided", async () => {
it("should return unauthorized when invalid query token provided", async () => {
await authenticationMiddleware({
headers: {},
query: { token: "123" },
Expand Down
35 changes: 35 additions & 0 deletions src/server/middleware/project-exists-middleware.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { IGetUserAuthInfoRequest } from "./request.model"
import { Response, NextFunction } from "express"
import { projectExistsMiddleware } from "./project-exists-middleware"
import { db } from "../../db/db"
import Boom = require("boom")

jest.mock("../../db/db")

describe("projectExistsMiddleware", () => {
const nextFunction: NextFunction = jest.fn()

beforeEach(() => {
jest.resetAllMocks()
})
it("should return 404 when no project found", async () => {
const request: any = { params: { projectName: "does not exist" } }
db.oneOrNone = jest.fn().mockReturnValueOnce(null)

await projectExistsMiddleware(request as unknown as IGetUserAuthInfoRequest,
{} as unknown as Response, nextFunction)
expect(nextFunction).toHaveBeenCalledTimes(1)
expect(nextFunction).toHaveBeenCalledWith(Boom.notFound("Project not found"))
})

it("should proceed when project found", async () => {
const request: any = { params: { projectName: "my-project" } }
db.oneOrNone = jest.fn().mockReturnValueOnce("projectId")

await projectExistsMiddleware(request as unknown as IGetUserAuthInfoRequest,
{} as unknown as Response, nextFunction)
expect(nextFunction).toHaveBeenCalledTimes(1)
expect(nextFunction).toHaveBeenCalledWith()
})

})
15 changes: 15 additions & 0 deletions src/server/middleware/project-exists-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IGetUserAuthInfoRequest } from "./request.model"
import { NextFunction, Response } from "express"
import { db } from "../../db/db"
import * as boom from "boom"
import { findProjectId } from "../queries/projects"

export const projectExistsMiddleware = async (req: IGetUserAuthInfoRequest, res: Response, next: NextFunction) => {

const { projectName } = req.params
const project = await db.oneOrNone(findProjectId(projectName))
if (!project) {
return next(boom.notFound("Project not found"))
}
next()
}
2 changes: 1 addition & 1 deletion src/server/queries/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const createNewProject = projectName => {
export const findProjectId = projectName => {
return {
name: "find-project-id",
text: "SELECT id FROM jtl.projects WHERE project_name = $1 AND",
text: "SELECT id FROM jtl.projects WHERE project_name = $1",
values: [projectName],
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/server/routes/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +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 { projectExistsMiddleware } from "../middleware/project-exists-middleware"

export class ItemsRoutes {

Expand All @@ -44,12 +45,14 @@ export class ItemsRoutes {
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(scenarioParamsSchema),
queryParamsValidator(querySchema),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response) => getItemsController(req, res)))

.post(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(newItemParamSchema),
projectExistsMiddleware,
createItemController)

app.route("/api/projects/:projectName/scenarios/:scenarioName/items/start-async")
Expand All @@ -58,6 +61,7 @@ export class ItemsRoutes {
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
bodySchemaValidator(newAsyncItemStartBodySchema),
paramsSchemaValidator(newItemParamSchema),
projectExistsMiddleware,
createItemAsyncController)

app.route("/api/projects/:projectName/scenarios/:scenarioName/items/:itemId")
Expand All @@ -66,26 +70,30 @@ export class ItemsRoutes {
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getItemController(req, res)))

.put(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
bodySchemaValidator(updateItemBodySchema),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response, next: NextFunction) => updateItemController(req, res, next)))

.delete(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response) => deleteItemController(req, res)))

app.route("/api/projects/:projectName/scenarios/:scenarioName/items/:itemId/request-stats-export")
.get(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response) => getRequestStatsExportController(req, res)))


Expand All @@ -95,26 +103,30 @@ export class ItemsRoutes {
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
bodySchemaValidator(stopItemAsyncBodySchema),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => stopItemAsyncController(req, res)))

app.route("/api/projects/:projectName/scenarios/:scenarioName/items/:itemId/share-tokens")
.get(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getItemLinksController(req, res)))

.post(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => createItemLinkController(req, res)))

app.route("/api/projects/:projectName/scenarios/:scenarioName/items/:itemId/share-tokens/:tokenId")
.delete(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(shareTokenSchema),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => deleteItemShareTokenController(req, res)))

app.route("/api/projects/:projectName/scenarios/:scenarioName/processing-items")
Expand All @@ -123,6 +135,7 @@ export class ItemsRoutes {
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(scenarioParamsSchema),
queryParamsValidator(environmentQuerySchema),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response) => getProcessingItemsController(req, res)))

app.route("/api/projects/:projectName/scenarios/:scenarioName/items/:itemId/custom-chart-settings")
Expand All @@ -131,12 +144,14 @@ export class ItemsRoutes {
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
bodySchemaValidator(upsertUserItemChartSettings),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => upsertItemChartSettingsController(req, res))
)
.get(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getItemChartSettingsController(req, res)))
}
}
4 changes: 4 additions & 0 deletions src/server/routes/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getLabelVirtualUsersController } from "../controllers/label/get-label-v
import { getLabelErrorsController } from "../controllers/label/get-label-errors-controller"
import { AllowedRoles, authorizationMiddleware } from "../middleware/authorization-middleware"
import { authenticationMiddleware } from "../middleware/authentication-middleware"
import { projectExistsMiddleware } from "../middleware/project-exists-middleware"

export class LabelRoutes {

Expand All @@ -19,6 +20,7 @@ export class LabelRoutes {
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(labelParamSchema),
queryParamsValidator(labelQuerySchema),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response) => getLabelTrendController(req, res)))

app.route("/api/projects/:projectName/scenarios/:scenarioName/items/:itemId/label/:label/virtual-users")
Expand All @@ -27,6 +29,7 @@ export class LabelRoutes {
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(labelParamSchema),
queryParamsValidator(labelQuerySchema),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response, next: NextFunction) =>
getLabelVirtualUsersController(req, res, next)))

Expand All @@ -35,6 +38,7 @@ export class LabelRoutes {
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(labelParamSchema),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response) => getLabelErrorsController(req, res)))
}
}
7 changes: 4 additions & 3 deletions src/server/routes/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getProjectController } from "../controllers/project/get-project-control
import { AllowedRoles, authorizationMiddleware } from "../middleware/authorization-middleware"
import { authenticationMiddleware } from "../middleware/authentication-middleware"
import { IGetUserAuthInfoRequest } from "../middleware/request.model"
import { projectExistsMiddleware } from "../middleware/project-exists-middleware"

export class ProjectRoutes {
routes(app: express.Application): void {
Expand Down Expand Up @@ -47,12 +48,14 @@ export class ProjectRoutes {
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(projectNameParam),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response) => deleteProjectController(req, res)))

.get(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(projectNameParam),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getProjectController(req, res)))


Expand All @@ -61,11 +64,9 @@ export class ProjectRoutes {
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(projectNameParam),
bodySchemaValidator(updateProjectSchema),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => updateProjectController(req, res)))


app.route("/api/projects/:projectName/allowed-users")
.get()
}

}
12 changes: 12 additions & 0 deletions src/server/routes/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { AllowedRoles, authorizationMiddleware } from "../middleware/authorizati
import { IGetUserAuthInfoRequest } from "../middleware/request.model"
import { postScenarioTrendsSettings } from "../controllers/scenario/trends/update-scenario-trends-settings-controller"
import { getScenarioEnvironmentController } from "../controllers/scenario/get-scenario-environment-controller"
import { projectExistsMiddleware } from "../middleware/project-exists-middleware"

export class ScenarioRoutes {

Expand All @@ -38,13 +39,15 @@ export class ScenarioRoutes {
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(projectNameParam),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response) => getScenariosController(req, res)))

.post(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(projectNameParam),
bodySchemaValidator(scenarioSchema),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response, next: NextFunction) =>
createScenarioController(req, res, next)))

Expand All @@ -53,6 +56,7 @@ export class ScenarioRoutes {
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getScenarioController(req, res))
)

Expand All @@ -61,26 +65,30 @@ export class ScenarioRoutes {
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
bodySchemaValidator(updateScenarioSchema),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => updateScenarioController(req, res)))

.delete(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response) => deleteScenarioController(req, res)))

app.route("/api/projects/:projectName/scenarios/:scenarioName/notifications")
.get(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response) => getScenarioNotificationsController(req, res)))

.post(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
bodySchemaValidator(scenarioNotificationBodySchema),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response) => createScenarioNotificationController(req, res)))


Expand All @@ -89,6 +97,7 @@ export class ScenarioRoutes {
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramSchemaNotification),
projectExistsMiddleware,
wrapAsync((req: Request, res: Response) => deleteScenarioNotificationController(req, res)))

app.route("/api/projects/:projectName/scenarios/:scenarioName/trends")
Expand All @@ -97,6 +106,7 @@ export class ScenarioRoutes {
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
queryParamsValidator(environmentQuerySchema),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getScenarioTrendsController(req, res)))

app.route("/api/projects/:projectName/scenarios/:scenarioName/trends/settings")
Expand All @@ -105,13 +115,15 @@ export class ScenarioRoutes {
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
bodySchemaValidator(scenarioTrendsSettings),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => postScenarioTrendsSettings(req, res)))

app.route("/api/projects/:projectName/scenarios/:scenarioName/environment")
.get(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(paramsSchema),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getScenarioEnvironmentController(req, res)))
}
}
Loading

0 comments on commit f802d0f

Please sign in to comment.