Skip to content

Commit

Permalink
# This is a combination of 5 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

pcq-1760 adding return to service path

# This is the commit message #2:

PCQ-1760 end page test

# This is the commit message #3:

PCQ-1760 end page test

# This is the commit message #4:

PCQ-1760 test and lint

# This is the commit message #5:

PCQ-1760 config semi
  • Loading branch information
priyankaVerma21 committed Jul 2, 2024
1 parent f68539e commit 1f783e3
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 63 deletions.
4 changes: 2 additions & 2 deletions app/components/app-insights.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ exports.initAppInsights = function initAppInsights() {
fs.mkdirSync(tempDir);
}
}
}
};

exports.trackTrace = function trackTrace(trace){
if(instrumentationKey && client){
client.trackTrace(trace);
}
}
};
17 changes: 3 additions & 14 deletions app/middleware/optOut.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const config = require('config');
const ServiceMapper = require('app/utils/ServiceMapper');
const moment = require('moment');
const appInsights = require('app/components/app-insights');
const validateUrl = require('app/middleware/validateUrl');

const setOptOut = (req, res) => {
const token = req.session.token;
Expand All @@ -27,7 +28,7 @@ const setOptOut = (req, res) => {
req.log.error(err);
})
.finally(() => {
res.redirect(validUrl(req))
res.redirect(validateUrl(req));
});
};

Expand All @@ -38,20 +39,8 @@ const optOut = (req, res) => {
if (!('optOut' in form)) {
return setOptOut(req, res);
}
res.redirect(validUrl(req));
res.redirect(validateUrl(req));

};

const validUrl = (req) => {
const redirect = req.session.returnUrl || '/offline';
let givenUrl ;
try {
givenUrl = new URL (redirect);
} catch (error) {
req.log.error(error);
givenUrl = '/offline';
}
return givenUrl;
};

module.exports = optOut;
11 changes: 11 additions & 0 deletions app/middleware/returnToService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const validateUrl = require('app/middleware/validateUrl');
const appInsights = require('app/components/app-insights');

const returnToService = (req, res) => {
appInsights.trackTrace({message: 'Returning to primary service', properties: {['ServiceId']:req.session.form.serviceId}});
res.redirect(validateUrl(req));
};

module.exports = returnToService;
15 changes: 15 additions & 0 deletions app/middleware/validateUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const validateUrl = (req) => {
const redirect = req.session.returnUrl || '/offline';
let givenUrl ;
try {
givenUrl = new URL (redirect);
} catch (error) {
req.log.error(error);
givenUrl = '/offline';
}
return givenUrl;
};

module.exports = validateUrl;
3 changes: 3 additions & 0 deletions app/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const initSession = require('app/middleware/initSession');
const registerIncomingService = require('app/registerIncomingService');
const validateParams = require('app/middleware/validateParams');
const optOut = require('app/middleware/optOut');
const returnToService = require('app/middleware/returnToService');
const featureToggle = new (require('app/utils/FeatureToggle'))();

router.use(initSession);
Expand All @@ -30,6 +31,8 @@ router.get('/', (req, res) => {

router.post('/opt-out', optOut);

router.get('/return-to-service', returnToService);

const allSteps = {
'en': initSteps([`${__dirname}/steps/ui`], 'en'),
'cy': initSteps([`${__dirname}/steps/ui`], 'cy')
Expand Down
2 changes: 1 addition & 1 deletion app/steps/ui/endpage/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{% if fields.returnUrl.value %}
{{ govukButton({
text: common.continueToNextSteps | safe,
href: fields.returnUrl.value
href: "/return-to-service"
}) }}
{% endif %}
</div>
Expand Down
63 changes: 35 additions & 28 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
import js from "@eslint/js";
import globals from "globals";
import js from '@eslint/js';
import globals from 'globals';

const config = {

languageOptions: {
ecmaVersion: 2020,
sourceType: 'module',
globals: {
...globals.browser,
...globals.mocha,
...globals.node,
'actor': true,
'Feature': true,
'Scenario': true,
'codecept_helper': true
}
languageOptions: {
ecmaVersion: 2020,
sourceType: 'module',
globals: {
...globals.browser,
...globals.mocha,
...globals.node,
'actor': true,
'Feature': true,
'Scenario': true,
'codecept_helper': true,
},
},

rules: {
...js.configs.recommended.rules,
'linebreak-style': ['error', 'unix'],
quotes: ['error', 'single'],
semi: ['error', 'always'],
'no-unused-vars': 0,
},
};

export default [
js.configs.recommended,
config,
{
ignores: [
'govuk/*',
'public/*',
'app/assets/javascripts/*.js',
'coverage',
'functional-output/*',
'.pnp.cjs',
'.pnp.loader.mjs',
'.stryker-tmp'
]
}
config,
{
ignores: [
'govuk/*',
'public/*',
'app/assets/javascripts/*.js',
'coverage',
'functional-output/*',
'.pnp.cjs',
'.pnp.loader.mjs',
'.stryker-tmp',
],
},
];
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "protected-characteristics-frontend",
"description": "Protected Characteristics web app",
"version": "1.2.176",
"version": "1.2.177",
"license": "MIT",
"engines": {
"node": ">=14.18.1"
Expand Down
17 changes: 0 additions & 17 deletions test/component/testEndPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,5 @@ describe('EndPage', () => {
it('test content loaded on the page', (done) => {
testWrapper.testContent(done);
});

it('test link to return URL is present', (done) => {
const sessionData = {
validParameters: true,
returnUrl: 'http://invoking-service-return-url'
};

testWrapper.agent.post('/prepare-session-field')
.send(sessionData)
.end(() => {
const playbackData = {
returnUrl: 'http://invoking-service-return-url'
};

testWrapper.testDataPlayback(done, playbackData);
});
});
});
});
60 changes: 60 additions & 0 deletions test/unit/middleware/testReturnToService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

const expect = require('chai').expect;
const nock = require('nock');
const sinon = require('sinon');
const returnToService = require('app/middleware/returnToService');

describe('returnToService', () => {
let req = {};
let res = {};

describe('redirect', () => {

beforeEach(() => {
req = {
session: {
form: {
serviceId: 'test'
}
}
};
res = {redirect: sinon.spy()};
});

it('should redirect to the given return URL', () => {
nock('http://localhost:4550')
.get('/return-to-service')
.reply(
200,
{status: ':thumbs_up:'}
);

req.session.returnUrl = 'http://test.com';
returnToService(req,res);

expect(res.redirect.calledOnce).to.equal(true);
expect(res.redirect.args[0][0].href).to.equal('http://test.com/');
nock.cleanAll();

});

it('should redirect to offline if URL not valid', () => {
nock('http://localhost:4550')
.get('/return-to-service')
.reply(
200,
{status: ':thumbs_up:'}
);

req.session.returnUrl = 'http:/[/]test.com';
req.log = sinon.spy();
req.log.error = sinon.spy();

returnToService(req, res);
expect(res.redirect.calledOnce).to.equal(true);
expect(res.redirect.args[0][0]).to.equal('/offline');
nock.cleanAll();
});
});
});

0 comments on commit 1f783e3

Please sign in to comment.