Skip to content

Commit

Permalink
Merge pull request #2105 from govuk-one-login/DFC-663-add-channel-mid…
Browse files Browse the repository at this point in the history
…dleware

feat(DFC-663): Add Channel Middleware
  • Loading branch information
di-aholme authored Sep 27, 2024
2 parents 2245080 + 0f200b9 commit 1bbd192
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import { setGTM } from "./middleware/analytics-middleware";
import { setCurrentUrlMiddleware } from "./middleware/current-url-middleware";
import { getRedisConfig } from "./utils/redis";
import { csrfMissingHandler } from "./handlers/csrf-missing-handler";
import { channelMiddleware } from "./middleware/channel-middleware";

const APP_VIEWS = [
path.join(__dirname, "components"),
Expand Down Expand Up @@ -201,6 +202,7 @@ async function createApp(): Promise<express.Application> {
app.use(cookieParser());
app.use(csurf({ cookie: getCSRFCookieOptions(isProduction) }));

app.use(channelMiddleware);
app.use(getSessionIdMiddleware);
app.post("*", sanitizeRequestMiddleware);
app.use(csrfMiddleware);
Expand Down
13 changes: 13 additions & 0 deletions src/middleware/channel-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NextFunction, Request, Response } from "express";
import { CHANNEL } from "../app.constants";

export function channelMiddleware(
req: Request,
res: Response,
next: NextFunction
): void {
res.locals.strategicAppChannel =
req.session?.user?.channel === CHANNEL.STRATEGIC_APP;
res.locals.webChannel = req.session?.user?.channel === CHANNEL.WEB;
next();
}
54 changes: 54 additions & 0 deletions src/middleware/tests/channel-middleware.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import chai, { expect } from "chai";
import { Request, Response } from "express";
import sinon from "sinon";
import sinonChai from "sinon-chai";
import { channelMiddleware } from "../channel-middleware";
import { describe } from "mocha";
import { mockRequest, mockResponse } from "mock-req-res";

chai.use(sinonChai);

describe("session-middleware", () => {
let next: sinon.SinonSpy;
let res: Response;

describe("createSessionMiddleware", () => {
beforeEach(() => {
res = mockResponse();
next = sinon.fake();
});

it("should set strategicAppChannel to true for strategic app clients", () => {
const req = mockRequest({
session: { client: {}, user: { channel: "strategic_app" } },
});
channelMiddleware(req as Request, res as Response, next);

expect(res.locals.strategicAppChannel).to.equal(true);
expect(res.locals.webChannel).to.equal(false);
expect(next).to.be.calledOnce;
});

it("should set webChannel to true for web clients", () => {
const req = mockRequest({
session: { client: {}, user: { channel: "web" } },
});
channelMiddleware(req as Request, res as Response, next);

expect(res.locals.strategicAppChannel).to.equal(false);
expect(res.locals.webChannel).to.equal(true);
expect(next).to.be.calledOnce;
});

it("should set no channel to true if provided an unrecognised channel", () => {
const req = mockRequest({
session: { client: {}, user: { channel: "unknown" } },
});
channelMiddleware(req as Request, res as Response, next);

expect(res.locals.strategicAppChannel).to.equal(false);
expect(res.locals.webChannel).to.equal(false);
expect(next).to.be.calledOnce;
});
});
});

0 comments on commit 1bbd192

Please sign in to comment.