Skip to content

Commit

Permalink
HOFF-659: Update NRM to use version of HOF that uses axios
Browse files Browse the repository at this point in the history
- update hof version to use axios
- remove request module in package.json
vulnerabilities
- update yarn.lock
- update node images and dependencies to resolve vulnerabilities
- remove deprecated request-native-promise from package.json and acceptance tests and replace with axios
- refactored code in save-form-session, resume-form-session, delete-form-session, are-you-sure and continue-report behaviours to use hof model instead of request
- update icasework-resolver image
  • Loading branch information
Rhodine-orleans-lindsay committed Sep 24, 2024
1 parent 9c156c5 commit b5eb942
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 397 deletions.
2 changes: 1 addition & 1 deletion Dockerfile-acceptance
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18-alpine@sha256:2322b1bb3917b313f2e9308395aa5c39d51b91cc92a5d4d5be6d0451fcfb4d24
FROM node:20.17.0-alpine@sha256:2cc3d19887bfea8bf52574716d5f16d4668e35158de866099711ddfb2b16b6e0

USER root

Expand Down
4 changes: 2 additions & 2 deletions acceptance-test/bootstrap/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

const puppeteer = require('puppeteer');
const request = require('request-promise-native');
const axios = require('axios');
const getContainerIP = require('../user-pathways/util/get-container-ip');

/* Constants */
Expand Down Expand Up @@ -42,7 +42,7 @@ const getBrowserContainerResponse = async () => {
resolveWithFullResponse: true
};

const response = await request(options);
const response = await axios(options);
return response;
};

Expand Down
26 changes: 14 additions & 12 deletions apps/nrm/behaviours/are-you-sure.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

const request = require('request');
const { model: Model } = require('hof');
const config = require('../../../config');
const baseUrl = config.saveService.host + ':' + config.saveService.port + '/reports/';
const logger = require('hof/lib/logger')({ env: config.env });

const encodeEmail = email => Buffer.from(email).toString('hex');

Expand All @@ -20,18 +21,19 @@ module.exports = superclass => class extends superclass {
});
}

saveValues(req, res, next) {
async saveValues(req, res, next) {
const id = req.body.confirm;

if (id) {
const email = encodeEmail(req.sessionModel.get('user-email'));

return request.del(baseUrl + email + '/' + id, err => {
if (err) {
return next(err);
}
return super.saveValues(req, res, next);
});
const email = encodeEmail(req.sessionModel.get('user-email'));
try {
const model = new Model();
const params = {
url: `${baseUrl}${email}/${id}`,
method: 'DELETE'
};
await model._request(params);
} catch (err) {
logger.error(`Error deleting data: ${err.message}`);
return next(new Error(err.body || ''));
}
return super.saveValues(req, res, next);
}
Expand Down
30 changes: 16 additions & 14 deletions apps/nrm/behaviours/continue-report.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

const request = require('request');
const { model: Model } = require('hof');
const config = require('../../../config');
const baseUrl = config.saveService.host + ':' + config.saveService.port + '/reports/';
const logger = require('hof/lib/logger')({ env: config.env });

const encodeEmail = email => Buffer.from(email).toString('hex');

Expand All @@ -17,22 +18,21 @@ module.exports = superclass => class extends superclass {
req.sessionModel.set('steps', ['/start', '/reports']);
}

getValues(req, res, next) {
async getValues(req, res, next) {
const id = req.query.id;

if (!id) {
return res.redirect('/nrm/reports');
}

this.cleanSession(req);

const getUrl = baseUrl + encodeEmail(req.sessionModel.get('user-email')) + '/' + id;

return request.get(getUrl, (error, response, body) => {
if (error) {
return next(error);
}
const resBody = JSON.parse(body);
try {
const model = new Model();
const params = {
url: `${baseUrl}${encodeEmail(req.sessionModel.get('user-email'))}/${id}`,
method: 'GET'
};
const response = await model._request(params);
const resBody = response.data;

if (resBody && resBody.length && resBody[0].session) {
if (resBody[0].session.hasOwnProperty('alertUser')) {
Expand All @@ -50,9 +50,11 @@ module.exports = superclass => class extends superclass {
req.sessionModel.set('id', id);
req.sessionModel.set('redirect-to-reports', true);
}

return super.getValues(req, res, next);
});
} catch (error) {
logger.error(`Error getting data: ${error.message}`);
return next(error);
}
return super.getValues(req, res, next);
}

saveValues(req, res, next) {
Expand Down
21 changes: 17 additions & 4 deletions apps/nrm/behaviours/delete-form-session.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
/* eslint-disable max-len */
'use strict';
const request = require('request');
const { model: Model } = require('hof');
const config = require('../../../config');
const logger = require('hof/lib/logger')({ env: config.env });

const encodeEmail = email => Buffer.from(email).toString('hex');

module.exports = superclass => class extends superclass {
saveValues(req, res, next) {
super.saveValues(req, res, err => {
super.saveValues(req, res, async err => {
if (err) {
next(err);
}
request.del(config.saveService.host + ':' + config.saveService.port
+ '/reports/' + encodeEmail(req.sessionModel.get('user-email')) + '/' + req.sessionModel.get('id'), next);

try {
const model = new Model();
const params = {
url: `${config.saveService.host}:${config.saveService.port}/reports/${encodeEmail(req.sessionModel.get('user-email'))}/${req.sessionModel.get('id')}`,
method: 'DELETE'
};
await model._request(params);
return next();
} catch (error) {
logger.error(`Error deleting data: ${error.message}`);
return next(error);
}
});
}
};
23 changes: 14 additions & 9 deletions apps/nrm/behaviours/resume-form-session.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable consistent-return */
'use strict';

const request = require('request');
const { model: Model } = require('hof');
const config = require('../../../config');
const moment = require('moment');
const baseUrl = config.saveService.host + ':' + config.saveService.port + '/reports/';
Expand Down Expand Up @@ -36,7 +36,7 @@ module.exports = superclass => class extends superclass {
return locals;
}

getValues(req, res, next) {
async getValues(req, res, next) {
this.cleanSession(req);

const externalID = req.sessionModel.get('externalID');
Expand All @@ -46,12 +46,14 @@ module.exports = superclass => class extends superclass {
return super.getValues(req, res, next);
}

return request.get(baseUrl + encodeEmail(req.sessionModel.get('user-email')), (err, response, body) => {
if (err) {
return next(err);
}

const resBody = JSON.parse(body);
try {
const model = new Model();
const params = {
url: `${baseUrl}${encodeEmail(req.sessionModel.get('user-email'))}`,
method: 'GET'
};
const response = await model._request(params);
const resBody = response.data;

if (resBody && resBody.length && resBody[0].session) {
req.previousReports = [];
Expand All @@ -78,6 +80,9 @@ module.exports = superclass => class extends superclass {
}
req.sessionModel.set('redirect-to-reports', false);
return super.getValues(req, res, next);
});
} catch (error) {
req.log('info', `External ID: ${externalID}, Error Saving Session: ${error}`);
return next(error);
}
}
};
43 changes: 23 additions & 20 deletions apps/nrm/behaviours/save-form-session.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable consistent-return */
'use strict';

const request = require('request');
const { model: Model } = require('hof');
const config = require('../../../config');

module.exports = superclass => class extends superclass {
saveValues(req, res, next) {
super.saveValues(req, res, err => {
super.saveValues(req, res, async err => {
if (err) {
next(err);
}
Expand Down Expand Up @@ -39,22 +39,23 @@ module.exports = superclass => class extends superclass {
const externalID = req.sessionModel.get('externalID');
req.log('info', `External ID: ${externalID}, Saving Form Session: ${req.sessionModel.get('id')}`);

request.post({
headers: {'content-type': 'application/json'},
url: config.saveService.host + ':' + config.saveService.port + '/reports',
body: JSON.stringify({
email: req.sessionModel.get('user-email'),
id: req.sessionModel.get('id'),
session: session
})
}, (error, response, body) => {
if (error) {
req.log('info', `External ID: ${externalID}, Error Saving Session: ${error}`);
next(error);
}
const resBody = JSON.parse(body);
try {
const model = new Model();
const params = {
method: 'POST',
url: `${config.saveService.host}:${config.saveService.port}/reports`,
data: {
email: req.sessionModel.get('user-email'),
id: req.sessionModel.get('id'),
session: session
}
};
const response = await model._request(params);

const resBody = response.data;

if (resBody && resBody.length && resBody[0].id) {
req.sessionModel.set('id', JSON.parse(body)[0].id);
req.sessionModel.set('id', resBody[0].id);
} else {
req.sessionModel.unset('id');
}
Expand All @@ -68,9 +69,11 @@ module.exports = superclass => class extends superclass {
if (req.sessionModel.get('redirect-to-reports') && noEditContinue) {
return res.redirect(`/nrm/continue-report?id=${req.sessionModel.get('id')}`);
}

next();
});
return next();
} catch (error) {
req.log('info', `External ID: ${externalID}, Error Saving Session: ${error}`);
return next(error);
}
});
}
};
3 changes: 2 additions & 1 deletion apps/nrm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ module.exports = {
],
locals: { showSaveAndExit: true },
fields: ['were-they-taken-somewhere-by-their-exploiter',
'were-they-taken-somewhere-by-their-exploiter-journey-details'],
'were-they-taken-somewhere-by-their-exploiter-journey-details'
],
next: '/how-they-were-treated'
},
'/how-they-were-treated': {
Expand Down
2 changes: 1 addition & 1 deletion browsers/chrome/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16.16.0 as base
FROM node:20.17.0 as base

EXPOSE 9222

Expand Down
2 changes: 1 addition & 1 deletion kube/icasework/icasework-resolver-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ spec:
containers:
- name: icasework-resolver
# release v4.2.0
image: quay.io/ukhomeofficedigital/icasework-resolver:8ea571f25fe8d928ef97ba60a5b832e8f3fcc4ea
image: quay.io/ukhomeofficedigital/icasework-resolver:f4ec7aeed867952d08ed02763cdbb4c5b6715dfe
imagePullPolicy: Always
envFrom:
- configMapRef:
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
"postinstall": "yarn run build"
},
"dependencies": {
"axios": "^1.7.4",
"accessible-autocomplete": "^2.0.2",
"govuk-frontend": "^2.7.0",
"hof": "~20.5.1",
"hof": "~21.1.0",
"ioredis": "^4.0.0",
"jquery": "^3.5.1",
"knex": "^3.1.0",
Expand All @@ -50,7 +51,6 @@
"ms-uk-regions": "^2.1.1",
"notifications-node-client": "^8.0.0",
"pg": "^8.7.1",
"request": "^2.88.0",
"sqs-producer": "^1.6.3",
"typeahead-aria": "^1.0.4",
"url": "^0.11.0",
Expand All @@ -70,7 +70,6 @@
"proxyquire": "^2.1.3",
"puppeteer": "^22.5.0",
"reqres": "^3.0.1",
"request-promise-native": "^1.0.9",
"sinon": "^11.1.2",
"sinon-chai": "^3.7.0",
"supertest": "^5.0.0",
Expand Down
Loading

0 comments on commit b5eb942

Please sign in to comment.