Skip to content

Commit

Permalink
project exists middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeknovy committed Sep 6, 2023
1 parent 3bf497b commit b65b104
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/server/middleware/project-scenario-exists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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"

Check warning on line 5 in src/server/middleware/project-scenario-exists.ts

View check run for this annotation

Codecov / codecov/patch

src/server/middleware/project-scenario-exists.ts#L3-L5

Added lines #L3 - L5 were not covered by tests

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

Check warning on line 7 in src/server/middleware/project-scenario-exists.ts

View check run for this annotation

Codecov / codecov/patch

src/server/middleware/project-scenario-exists.ts#L7

Added line #L7 was not covered by tests

const { projectName } = req.params
const project = await db.oneOrNone(findProjectId(projectName))

Check warning on line 10 in src/server/middleware/project-scenario-exists.ts

View check run for this annotation

Codecov / codecov/patch

src/server/middleware/project-scenario-exists.ts#L9-L10

Added lines #L9 - L10 were not covered by tests
if (!project) {
return next(boom.notFound("Project not found"))

Check warning on line 12 in src/server/middleware/project-scenario-exists.ts

View check run for this annotation

Codecov / codecov/patch

src/server/middleware/project-scenario-exists.ts#L12

Added line #L12 was not covered by tests
}
}
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-scenario-exists"

Check warning on line 36 in src/server/routes/item.ts

View check run for this annotation

Codecov / codecov/patch

src/server/routes/item.ts#L36

Added line #L36 was not covered by tests

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-scenario-exists"

Check warning on line 11 in src/server/routes/label.ts

View check run for this annotation

Codecov / codecov/patch

src/server/routes/label.ts#L11

Added line #L11 was not covered by tests

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)))
}
}
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-scenario-exists";

Check failure on line 31 in src/server/routes/scenario.ts

View workflow job for this annotation

GitHub Actions / build

A space is required after '{'

Check failure on line 31 in src/server/routes/scenario.ts

View workflow job for this annotation

GitHub Actions / build

A space is required before '}'

Check failure on line 31 in src/server/routes/scenario.ts

View workflow job for this annotation

GitHub Actions / build

Extra semicolon

Check warning on line 31 in src/server/routes/scenario.ts

View check run for this annotation

Codecov / codecov/patch

src/server/routes/scenario.ts#L31

Added line #L31 was not covered by tests

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)))
}
}

0 comments on commit b65b104

Please sign in to comment.