diff --git a/src/app.js b/src/app.js index 180b35a4..db10f889 100644 --- a/src/app.js +++ b/src/app.js @@ -17,6 +17,8 @@ const { setGTM } = require("di-ipv-cri-common-express/src/lib/settings"); const { getGTM } = require("di-ipv-cri-common-express/src/lib/locals"); const steps = require("./app/drivingLicence/steps"); const fields = require("./app/drivingLicence/fields"); +const featureSets = require("./app/drivingLicence/featureSets"); + const { API, @@ -117,6 +119,7 @@ router.use(getGTM); router.use(setScenarioHeaders); router.use(setAxiosDefaults); +router.use(featureSets); router.use("/oauth2", commonExpress.routes.oauth2); diff --git a/src/app/drivingLicence/controllers/featureSets.js b/src/app/drivingLicence/controllers/featureSets.js new file mode 100644 index 00000000..7a50fed7 --- /dev/null +++ b/src/app/drivingLicence/controllers/featureSets.js @@ -0,0 +1,19 @@ +const logger = require("hmpo-logger").get(); + +module.exports = function (req, res, next) { + try { + const featureSet = req.query.featureSet; + const isValidFeatureSet = /^\w{1,32}$/.test(featureSet); + if (!isValidFeatureSet) { + throw new Error("Invalid feature set ID"); + } + + if (featureSet !== undefined) { + logger.info("feature set is " + featureSet); + req.session.featureSet = featureSet; + } + next(); + } catch (error) { + return next(error); + } +}; diff --git a/src/app/drivingLicence/controllers/featureSets.test.js b/src/app/drivingLicence/controllers/featureSets.test.js new file mode 100644 index 00000000..56fffdb3 --- /dev/null +++ b/src/app/drivingLicence/controllers/featureSets.test.js @@ -0,0 +1,46 @@ +const FeatureSets = require("./featureSets"); + +describe("feature sets", () => { + let req; + let res; + let next; + let sandbox; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + const setup = setupDefaultMocks(); + req = setup.req; + res = setup.res; + next = setup.next; + }); + afterEach(() => sandbox.restore()); + + context("validateFeatureSet", () => { + beforeEach(() => { + req = { + query: {}, + session: {}, + }; + res = {}; + next = sinon.stub(); + }); + + it("should call next if featureSet is valid", async () => { + req.query.featureSet = "F01"; + await FeatureSets(req, res, next); + expect(req.session.featureSet).to.equal("F01"); + expect(next).to.have.been.calledOnce; + }); + + it("should throw an error if featureSet is invalid", async () => { + req.query.featureSet = "invalid-featureset"; + await FeatureSets(req, res, next); + expect(next).to.have.been.calledWith( + sinon.match + .instanceOf(Error) + .and(sinon.match.has("message", "Invalid feature set ID")) + ); + expect(req.session.featureSet).to.be.undefined; + }); + }); +}); diff --git a/src/app/drivingLicence/controllers/validate.js b/src/app/drivingLicence/controllers/validate.js index a0b94805..587ad585 100644 --- a/src/app/drivingLicence/controllers/validate.js +++ b/src/app/drivingLicence/controllers/validate.js @@ -38,6 +38,10 @@ class ValidateController extends BaseController { session_id: req.session.tokenId, }; + if (req.session.featureSet === "direct") { + headers["document-checking-route"] = "direct"; + } + logger.info("validate: calling check-driving-licence lambda", { req, res, diff --git a/src/app/drivingLicence/controllers/validate.test.js b/src/app/drivingLicence/controllers/validate.test.js index d80110dc..5f476eb0 100644 --- a/src/app/drivingLicence/controllers/validate.test.js +++ b/src/app/drivingLicence/controllers/validate.test.js @@ -76,6 +76,65 @@ describe("validate controller", () => { expect(req.sessionModel.get("redirect_url")).to.eq(data.redirectUrl); }); + it("should add a document check routing header if a feature set has been set", async () => { + const sessionId = "drivingLicence123"; + + req.sessionModel.set("drivingLicenceNumber", "SMITH9702105LN99"); + req.sessionModel.set("issueNumber", "12"); + req.sessionModel.set("postcode", "SW1 EQR"); + req.session.tokenId = sessionId; + req.sessionModel.set("surname", "Smith"); + req.sessionModel.set("firstName", "Dan"); + req.sessionModel.set("middleNames", "Joe"); + req.sessionModel.set("dateOfBirth", "10/02/1975"); + req.sessionModel.set("expiryDate", "15/01/2035"); + req.sessionModel.set("issueDate", "10/02/2005"); + req.sessionModel.set("dateOfIssue", "10/02/2005"); + req.sessionModel.set("licenceIssuer", "DVLA"); + req.session.authParams = { + redirect_uri: "A VALUE", + state: "A VALUE", + }; + req.session.featureSet = "hmpoDVAD"; + + const data = { + redirectUrl: + "https://client.example.com/cb?id=DrivingLicenceIssuer&code=1234", + retry: false, + }; + + const resolvedPromise = new Promise((resolve) => resolve({ data })); + sandbox.stub(axios, "post").returns(resolvedPromise); + + await validate.saveValues(req, res, next); + + sandbox.assert.calledWith( + stub, + "check-driving-licence", + { + drivingLicenceNumber: "SMITH9702105LN99", + issueNumber: "12", + postcode: "SW1 EQR", + surname: "Smith", + forenames: ["Dan", "Joe"], + dateOfBirth: "10/02/1975", + expiryDate: "15/01/2035", + issueDate: "10/02/2005", + licenceIssuer: "DVLA", + }, + { + headers: { + "document-checking-route": "direct", + session_id: sessionId, + }, + } + ); + + expect(req.session.authParams.redirect_uri).to.eq( + "https://client.example.com" + ); + }); + it("should set an error object in the session if redirect url is missing", async () => { req.sessionModel.set("drivingLicenceNumber", "SMITH9702105LN99"); req.sessionModel.set("issueNumber", "12");