From 07a8913b35048a9c7197fbb91a556cfa1dc84e49 Mon Sep 17 00:00:00 2001 From: Antariksh Date: Wed, 11 Nov 2020 10:31:58 +0800 Subject: [PATCH 1/4] ref: convert hashedFields to Set --- src/app/controllers/admin-forms.server.controller.js | 4 ++-- .../controllers/email-submissions.server.controller.js | 10 +++++----- src/app/controllers/myinfo.server.controller.js | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/app/controllers/admin-forms.server.controller.js b/src/app/controllers/admin-forms.server.controller.js index f53d332a12..be19ca6d43 100644 --- a/src/app/controllers/admin-forms.server.controller.js +++ b/src/app/controllers/admin-forms.server.controller.js @@ -598,13 +598,13 @@ function makeModule(connection) { switch (authType) { case 'SP': { res.locals.uinFin = 'S1234567A' - req.hashedFields = {} + req.hashedFields = new Set() let actualFormFields = req.form.form_fields let actualMyInfoFields = actualFormFields.filter( (field) => field.myInfo && field.myInfo.attr, ) for (let field of actualMyInfoFields) { - req.hashedFields[field.myInfo.attr] = true + req.hashedFields.add(field.myInfo.attr) } break } diff --git a/src/app/controllers/email-submissions.server.controller.js b/src/app/controllers/email-submissions.server.controller.js index 1521b7ae88..bda45c0600 100644 --- a/src/app/controllers/email-submissions.server.controller.js +++ b/src/app/controllers/email-submissions.server.controller.js @@ -332,11 +332,11 @@ exports.prepareEmailSubmission = (req, res, next) => { * @param {String} response.fieldType * @param {Object} response.myInfo * @param {String} response.myInfo.attr - * @param {Object} hashedFields req.hashedFields + * @param {Set} hashedFields req.hashedFields * @returns {Boolean} true if response is verified */ const isMyInfoVerifiedResponse = (response, hashedFields) => { - return !!(hashedFields && hashedFields[_.get(response, 'myInfo.attr')]) + return !!(hashedFields && hashedFields.has(_.get(response, 'myInfo.attr'))) } /** @@ -382,7 +382,7 @@ const getAnswerForCheckbox = (response) => { * @param {String} response.answer * @param {String} response.fieldType * @param {Boolean} response.isVisible - * @param {Boolean} hashedFields Fields hashed to verify answers provided by MyInfo + * @param {Set} hashedFields Fields hashed to verify answers provided by MyInfo * @returns {Object} an object containing three sets of formatted responses */ const getFormattedResponse = (response, hashedFields) => { @@ -418,7 +418,7 @@ const getFormattedResponse = (response, hashedFields) => { /** * Transforms a question for inclusion in the admin email table. * @param {Object} response - * @param {Object} hashedFields + * @param {Set} hashedFields */ const getFormDataPrefixedQuestion = (response, hashedFields) => { const { question, fieldType, isUserVerified } = response @@ -472,7 +472,7 @@ const getFieldTypePrefix = (fieldType) => { * Determines the prefix for a question based on whether it is verified * by MyInfo. * @param {Object} response - * @param {Object} hashedFields Hash for verifying MyInfo fields + * @param {Set} hashedFields Hash for verifying MyInfo fields * @returns {String} */ const getMyInfoPrefix = (response, hashedFields) => { diff --git a/src/app/controllers/myinfo.server.controller.js b/src/app/controllers/myinfo.server.controller.js index 1a818b192f..9409f0a7c3 100644 --- a/src/app/controllers/myinfo.server.controller.js +++ b/src/app/controllers/myinfo.server.controller.js @@ -218,7 +218,7 @@ exports.verifyMyInfoVals = async function (req, res, next) { _.uniq(clientMyInfoFields.map((field) => field.attr)), _.keys(hashedFields), ) - req.hashedFields = _.pick(hashedFields, verifiedKeys) + req.hashedFields = new Set(verifiedKeys) return next() } }) From 27730870fd717c34e76bbdb405412f469ad83f92 Mon Sep 17 00:00:00 2001 From: Antariksh Date: Wed, 11 Nov 2020 10:32:05 +0800 Subject: [PATCH 2/4] test: fix tests --- .../controllers/email-submissions.server.controller.spec.js | 4 +--- .../backend/controllers/myinfo.server.controller.spec.js | 5 +---- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/unit/backend/controllers/email-submissions.server.controller.spec.js b/tests/unit/backend/controllers/email-submissions.server.controller.spec.js index a406fbcebc..ca92b82306 100644 --- a/tests/unit/backend/controllers/email-submissions.server.controller.spec.js +++ b/tests/unit/backend/controllers/email-submissions.server.controller.spec.js @@ -981,9 +981,7 @@ describe('Email Submissions Controller', () => { const fieldId = new ObjectID() const attr = 'passportnumber' - reqFixtures.hashedFields = { - [attr]: 'foobar', - } + reqFixtures.hashedFields = new Set([attr]) const responseField = { _id: String(fieldId), question: 'myinfo', diff --git a/tests/unit/backend/controllers/myinfo.server.controller.spec.js b/tests/unit/backend/controllers/myinfo.server.controller.spec.js index 9335766ad1..6d9701ad37 100644 --- a/tests/unit/backend/controllers/myinfo.server.controller.spec.js +++ b/tests/unit/backend/controllers/myinfo.server.controller.spec.js @@ -677,10 +677,7 @@ describe('MyInfo Controller', () => { next = jasmine.createSpy().and.callFake(() => { expect( - _.isEqual( - new Set(_.keys(req.hashedFields)), - new Set(_.keys(ALL_MYINFO_HASHES)), - ), + _.isEqual(req.hashedFields, new Set(_.keys(ALL_MYINFO_HASHES))), ).toBeTruthy() expect(res.send).not.toHaveBeenCalled() expect(res.json).not.toHaveBeenCalled() From ba4d7761a12304751aa72af8458a744d4ae82482 Mon Sep 17 00:00:00 2001 From: Antariksh Date: Wed, 11 Nov 2020 10:38:33 +0800 Subject: [PATCH 3/4] ref: move hashedFields to res.locals --- src/app/controllers/admin-forms.server.controller.js | 4 ++-- src/app/controllers/email-submissions.server.controller.js | 2 +- src/app/controllers/myinfo.server.controller.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/controllers/admin-forms.server.controller.js b/src/app/controllers/admin-forms.server.controller.js index be19ca6d43..520b570add 100644 --- a/src/app/controllers/admin-forms.server.controller.js +++ b/src/app/controllers/admin-forms.server.controller.js @@ -598,13 +598,13 @@ function makeModule(connection) { switch (authType) { case 'SP': { res.locals.uinFin = 'S1234567A' - req.hashedFields = new Set() + res.locals.hashedFields = new Set() let actualFormFields = req.form.form_fields let actualMyInfoFields = actualFormFields.filter( (field) => field.myInfo && field.myInfo.attr, ) for (let field of actualMyInfoFields) { - req.hashedFields.add(field.myInfo.attr) + res.locals.hashedFields.add(field.myInfo.attr) } break } diff --git a/src/app/controllers/email-submissions.server.controller.js b/src/app/controllers/email-submissions.server.controller.js index bda45c0600..df59150e23 100644 --- a/src/app/controllers/email-submissions.server.controller.js +++ b/src/app/controllers/email-submissions.server.controller.js @@ -281,7 +281,7 @@ exports.validateEmailSubmission = function (req, res, next) { * @param {Function} next - Express next middleware function */ exports.prepareEmailSubmission = (req, res, next) => { - const { hashedFields } = req + const { hashedFields } = res.locals const concatArray = (dataValue, srcValue) => { if (_.isArray(dataValue)) { diff --git a/src/app/controllers/myinfo.server.controller.js b/src/app/controllers/myinfo.server.controller.js index 9409f0a7c3..5b10d5654b 100644 --- a/src/app/controllers/myinfo.server.controller.js +++ b/src/app/controllers/myinfo.server.controller.js @@ -218,7 +218,7 @@ exports.verifyMyInfoVals = async function (req, res, next) { _.uniq(clientMyInfoFields.map((field) => field.attr)), _.keys(hashedFields), ) - req.hashedFields = new Set(verifiedKeys) + res.locals.hashedFields = new Set(verifiedKeys) return next() } }) From c64e76d44074fc3d7df697849be37a0f25306625 Mon Sep 17 00:00:00 2001 From: Antariksh Date: Wed, 11 Nov 2020 10:38:49 +0800 Subject: [PATCH 4/4] test: update tests --- .../controllers/email-submissions.server.controller.spec.js | 2 +- .../backend/controllers/myinfo.server.controller.spec.js | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/unit/backend/controllers/email-submissions.server.controller.spec.js b/tests/unit/backend/controllers/email-submissions.server.controller.spec.js index ca92b82306..1b5316cd94 100644 --- a/tests/unit/backend/controllers/email-submissions.server.controller.spec.js +++ b/tests/unit/backend/controllers/email-submissions.server.controller.spec.js @@ -981,7 +981,7 @@ describe('Email Submissions Controller', () => { const fieldId = new ObjectID() const attr = 'passportnumber' - reqFixtures.hashedFields = new Set([attr]) + resLocalFixtures.hashedFields = new Set([attr]) const responseField = { _id: String(fieldId), question: 'myinfo', diff --git a/tests/unit/backend/controllers/myinfo.server.controller.spec.js b/tests/unit/backend/controllers/myinfo.server.controller.spec.js index 6d9701ad37..23b6ab640f 100644 --- a/tests/unit/backend/controllers/myinfo.server.controller.spec.js +++ b/tests/unit/backend/controllers/myinfo.server.controller.spec.js @@ -677,7 +677,10 @@ describe('MyInfo Controller', () => { next = jasmine.createSpy().and.callFake(() => { expect( - _.isEqual(req.hashedFields, new Set(_.keys(ALL_MYINFO_HASHES))), + _.isEqual( + res.locals.hashedFields, + new Set(_.keys(ALL_MYINFO_HASHES)), + ), ).toBeTruthy() expect(res.send).not.toHaveBeenCalled() expect(res.json).not.toHaveBeenCalled()