-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2105 from govuk-one-login/DFC-663-add-channel-mid…
…dleware feat(DFC-663): Add Channel Middleware
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); | ||
}); |