Skip to content

Commit

Permalink
test new session stubbing fix with dedicated route
Browse files Browse the repository at this point in the history
  • Loading branch information
freemvmt committed Aug 8, 2024
1 parent 86db019 commit 1054ed9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api.planx.uk/modules/test/controller.ts
Original file line number Diff line number Diff line change
@@ -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 });
};
28 changes: 28 additions & 0 deletions api.planx.uk/modules/test/docs.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions api.planx.uk/modules/test/routes.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
9 changes: 9 additions & 0 deletions api.planx.uk/modules/test/routes.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions api.planx.uk/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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" } = (() => {
Expand Down

0 comments on commit 1054ed9

Please sign in to comment.