From 1054ed93d26cf01629d61a324de49fa126400980 Mon Sep 17 00:00:00 2001 From: Dan Goss Date: Fri, 9 Aug 2024 00:34:28 +0100 Subject: [PATCH] test new session stubbing fix with dedicated route --- api.planx.uk/modules/test/controller.ts | 7 ++++++ api.planx.uk/modules/test/docs.yaml | 28 ++++++++++++++++++++++++ api.planx.uk/modules/test/routes.test.ts | 14 ++++++++++++ api.planx.uk/modules/test/routes.ts | 9 ++++++++ api.planx.uk/server.ts | 2 ++ 5 files changed, 60 insertions(+) create mode 100644 api.planx.uk/modules/test/controller.ts create mode 100644 api.planx.uk/modules/test/docs.yaml create mode 100644 api.planx.uk/modules/test/routes.test.ts create mode 100644 api.planx.uk/modules/test/routes.ts diff --git a/api.planx.uk/modules/test/controller.ts b/api.planx.uk/modules/test/controller.ts new file mode 100644 index 0000000000..4ddda88468 --- /dev/null +++ b/api.planx.uk/modules/test/controller.ts @@ -0,0 +1,7 @@ +import { RequestHandler } from "express"; + +export const testSessionMethods: RequestHandler = (req, res) => { + const hasRegenerate = typeof req.session?.regenerate === "function"; + const hasSave = typeof req.session?.save === "function"; + res.status(200).json({ hasRegenerate, hasSave }); +}; diff --git a/api.planx.uk/modules/test/docs.yaml b/api.planx.uk/modules/test/docs.yaml new file mode 100644 index 0000000000..f8f960a65f --- /dev/null +++ b/api.planx.uk/modules/test/docs.yaml @@ -0,0 +1,28 @@ +openapi: 3.1.0 +info: + title: Planâś• API + version: 0.1.0 +tags: + - name: test + description: Requests for testing purposes +paths: + /test-session: + get: + summary: Test req.session object + description: Confirms session has necessary dummy methods registered + tags: ["test"] + responses: + "200": + description: OK + content: + application/json: + schema: + type: object + properties: + hasRegenerate: + type: boolean + hasSave: + type: boolean + example: + hasRegenerate: true + hasSave: true diff --git a/api.planx.uk/modules/test/routes.test.ts b/api.planx.uk/modules/test/routes.test.ts new file mode 100644 index 0000000000..f951b9816c --- /dev/null +++ b/api.planx.uk/modules/test/routes.test.ts @@ -0,0 +1,14 @@ +import supertest from "supertest"; +import app from "../../server"; + +describe("Session setup", () => { + test("adds dummy methods to req.session to avoid passport/cookie-session incompatibility issue", async () => { + await supertest(app) + .get("/test-session") + .expect(200) + .then((res) => { + expect(res.body.hasRegenerate).toBe(true); + expect(res.body.hasSave).toBe(true); + }); + }); +}); diff --git a/api.planx.uk/modules/test/routes.ts b/api.planx.uk/modules/test/routes.ts new file mode 100644 index 0000000000..43fd8dacaf --- /dev/null +++ b/api.planx.uk/modules/test/routes.ts @@ -0,0 +1,9 @@ +import { Router } from "express"; +import { testSessionMethods } from "./controller"; + +const router = Router(); + +// in order to test the session setup, we need a dedicated test route +router.get("/test-session", testSessionMethods); + +export default router; diff --git a/api.planx.uk/server.ts b/api.planx.uk/server.ts index 69214a79e5..068d86818e 100644 --- a/api.planx.uk/server.ts +++ b/api.planx.uk/server.ts @@ -31,6 +31,7 @@ import fileRoutes from "./modules/file/routes"; import gisRoutes from "./modules/gis/routes"; import payRoutes from "./modules/pay/routes"; import sendRoutes from "./modules/send/routes"; +import testRoutes from "./modules/test/routes"; import { useSwaggerDocs } from "./docs"; import { Role } from "@opensystemslab/planx-core/types"; @@ -149,6 +150,7 @@ app.use(sendRoutes); app.use(teamRoutes); app.use(userRoutes); app.use(webhookRoutes); +app.use(testRoutes); const errorHandler: ErrorRequestHandler = (errorObject, _req, res, _next) => { const { status = 500, message = "Something went wrong" } = (() => {