From 77881d8b478e0bc9f603092886f365550352775e Mon Sep 17 00:00:00 2001 From: Nathaniel Steers Date: Fri, 11 Oct 2024 15:28:41 +0100 Subject: [PATCH] [squash] service name controller tests, remove degateway references --- .../service-name.controller.test.js | 224 ++++++++++++++++++ .../user/degateway/post.controller.js | 2 +- .../simplified-account-strategy.middleware.js | 7 - .../team-members/team-member-profile.njk | 6 +- 4 files changed, 228 insertions(+), 11 deletions(-) diff --git a/app/controllers/simplified-account/settings/service-name/service-name.controller.test.js b/app/controllers/simplified-account/settings/service-name/service-name.controller.test.js index e69de29bb2..5b6943ea9f 100644 --- a/app/controllers/simplified-account/settings/service-name/service-name.controller.test.js +++ b/app/controllers/simplified-account/settings/service-name/service-name.controller.test.js @@ -0,0 +1,224 @@ +'use strict' + +const { SERVICE_NAME_MAX_LENGTH } = require('../../../../utils/validation/server-side-form-validations') + +const proxyquire = require('proxyquire') +const sinon = require('sinon') +const paths = require('../../../../paths') +const { expect } = require('chai') + +const ACCOUNT_TYPE = 'test' +const SERVICE_ID = 'service-id-123abc' +const EN_SERVICE_NAME = 'My Cool Service' +const CY_SERVICE_NAME = 'Fy Ngwasanaeth Cwl' + +let req, res, responseStub, updateServiceNameStub, serviceNameController + +const getController = (stubs = {}) => { + return proxyquire('./service-name.controller', { + '../../../../utils/response': { response: stubs.response }, + '../../../../services/service.service': { updateServiceName: stubs.updateServiceName }, + '../../../../utils/simplified-account/format/format-validation-errors': stubs.formatValidationErrors || (() => ({})) + }) +} + +const setupTest = (method, additionalReqProps = {}, additionalStubs = {}) => { + responseStub = sinon.spy() + updateServiceNameStub = sinon.stub().resolves() + serviceNameController = getController({ + response: responseStub, + updateServiceName: updateServiceNameStub, + ...additionalStubs + }) + res = { + redirect: sinon.spy() + } + req = { + account: { + service_id: SERVICE_ID, + type: ACCOUNT_TYPE + }, + service: { + serviceName: { + en: EN_SERVICE_NAME, + cy: CY_SERVICE_NAME + } + }, + ...additionalReqProps + } + serviceNameController[method](req, res) +} + +describe('Controller: settings/service-name', () => { + describe('get', () => { + before(() => setupTest('get')) + + it('should call the response method', () => { + expect(responseStub.called).to.equal(true) + }) + + it('should pass req, res and template path to the response method', () => { + expect(responseStub.args[0]).to.include(req) + expect(responseStub.args[0]).to.include(res) + expect(responseStub.args[0]).to.include('simplified-account/settings/service-name/index') + }) + + it(`should pass context data to the response method`, () => { + expect(responseStub.args[0][3]).to.have.property('service_name_en').to.equal(EN_SERVICE_NAME) + expect(responseStub.args[0][3]).to.have.property('service_name_cy').to.equal(CY_SERVICE_NAME) + expect(responseStub.args[0][3]).to.have.property('manage_en').to.contain(`service/${SERVICE_ID}/account/${ACCOUNT_TYPE}`) + expect(responseStub.args[0][3]).to.have.property('manage_en').to.not.contain('?cy=true') + expect(responseStub.args[0][3]).to.have.property('manage_cy').to.contain(`service/${SERVICE_ID}/account/${ACCOUNT_TYPE}`) + expect(responseStub.args[0][3]).to.have.property('manage_cy').to.contain('?cy=true') + }) + }) + + describe('getEditServiceName', () => { + const testEditServiceName = (isWelsh) => { + const queryParams = isWelsh ? { cy: 'true' } : {} + before(() => setupTest('getEditServiceName', { query: queryParams })) + + it('should call the response method', () => { + expect(responseStub.called).to.equal(true) + }) + + it('should pass req, res and template path to the response method', () => { + expect(responseStub.args[0]).to.include(req) + expect(responseStub.args[0]).to.include(res) + expect(responseStub.args[0]).to.include('simplified-account/settings/service-name/edit-service-name') + }) + + it('should pass context data to the response method', () => { + const context = responseStub.args[0][3] + expect(context).to.have.property('edit_cy').to.equal(isWelsh) + expect(context).to.have.property('back_link').to.contain(paths.simplifiedAccount.settings.index) + expect(context).to.have.property('submit_link').to.contain(paths.simplifiedAccount.settings.serviceName.edit) + expect(context).to.have.property('service_name').to.equal(isWelsh ? CY_SERVICE_NAME : EN_SERVICE_NAME) + }) + } + + describe('when editing Welsh service name', () => { + testEditServiceName(true) + }) + + describe('when editing English service name', () => { + testEditServiceName(false) + }) + }) + + describe('postEditServiceName', () => { + describe('when submitting a valid English service name', () => { + before(() => { + setupTest('postEditServiceName', { + body: { + 'service-name-input': 'New English Name', + cy: 'false' + } + }) + }) + + it('should update the service name', () => { + expect(updateServiceNameStub.calledWith(SERVICE_ID, 'New English Name', CY_SERVICE_NAME)).to.be.true // eslint-disable-line + }) + + it('should redirect to the service name index page', () => { + expect(res.redirect.calledOnce).to.be.true // eslint-disable-line + expect(res.redirect.args[0][0]).to.include(paths.simplifiedAccount.settings.serviceName.index) + }) + }) + + describe('when submitting a valid Welsh service name', () => { + before(() => { + setupTest('postEditServiceName', { + body: { + 'service-name-input': 'New Welsh Name', + cy: 'true' + } + }) + }) + + it('should update the service name', () => { + expect(updateServiceNameStub.calledWith(SERVICE_ID, EN_SERVICE_NAME, 'New Welsh Name')).to.be.true // eslint-disable-line + }) + + it('should redirect to the service name index page', () => { + expect(res.redirect.calledOnce).to.be.true // eslint-disable-line + expect(res.redirect.args[0][0]).to.include(paths.simplifiedAccount.settings.serviceName.index) + }) + }) + + describe('when submitting an invalid service name', () => { + const mockFormatValidationErrors = sinon.stub().returns({ + errorSummary: ['Error summary'], + formErrors: { 'service-name-input': 'Error message' } + }) + + before(() => { + setupTest('postEditServiceName', { + body: { + 'service-name-input': 'A'.repeat(SERVICE_NAME_MAX_LENGTH + 1), + cy: 'false' + } + }, + { + formatValidationErrors: mockFormatValidationErrors + }) + }) + + it('should not update the service name', () => { + expect(updateServiceNameStub.called).to.be.false // eslint-disable-line + }) + + it('should render the edit page with errors', () => { + expect(responseStub.calledOnce).to.be.true // eslint-disable-line + const [ , , template, context] = responseStub.args[0] + expect(template).to.equal('simplified-account/settings/service-name/edit-service-name') + expect(context.errors).to.deep.equal({ + summary: ['Error summary'], + formErrors: { 'service-name-input': 'Error message' } + }) + }) + }) + + describe('when submitting an empty English service name', () => { + before(() => { + setupTest('postEditServiceName', { + body: { + 'service-name-input': '', + cy: 'false' + } + }) + }) + + it('should not update the service name', () => { + expect(updateServiceNameStub.called).to.be.false // eslint-disable-line + }) + + it('should render the edit page with errors', () => { + expect(responseStub.calledOnce).to.be.true // eslint-disable-line + const [, , template] = responseStub.args[0] + expect(template).to.equal('simplified-account/settings/service-name/edit-service-name') + }) + }) + + describe('when submitting an empty Welsh service name', () => { + before(() => { + setupTest('postEditServiceName', { + body: { + 'service-name-input': '', + cy: 'true' + } + }) + }) + + it('should update the service name with an empty Welsh name', () => { + expect(updateServiceNameStub.calledWith(SERVICE_ID, EN_SERVICE_NAME, '')).to.be.true // eslint-disable-line + }) + + it('should redirect to the service name index page', () => { + expect(res.redirect.calledOnce).to.be.true // eslint-disable-line + expect(res.redirect.args[0][0]).to.include(paths.simplifiedAccount.settings.serviceName.index) + }) + }) + }) +}) diff --git a/app/controllers/user/degateway/post.controller.js b/app/controllers/user/degateway/post.controller.js index 409e190ed9..dd99419567 100644 --- a/app/controllers/user/degateway/post.controller.js +++ b/app/controllers/user/degateway/post.controller.js @@ -22,7 +22,7 @@ module.exports = async function postDegatewayPreference (req, res, next) { degatewayPreference === 'enabled' ? featureSet.add('degatewayaccountification') : featureSet.delete('degatewayaccountification') const updatedFeatures = Array.from(featureSet).join(',') await userService.updateFeatures(req.user.externalId, (updatedFeatures !== '' ? updatedFeatures : null)) - req.flash('generic', 'Degateway preference updated') + req.flash('generic', 'Account simplification preference updated') return res.redirect(paths.user.profile.index) } catch (err) { next(err) diff --git a/app/middleware/simplified-account/simplified-account-strategy.middleware.js b/app/middleware/simplified-account/simplified-account-strategy.middleware.js index b7e8c51030..8a3fcd93aa 100644 --- a/app/middleware/simplified-account/simplified-account-strategy.middleware.js +++ b/app/middleware/simplified-account/simplified-account-strategy.middleware.js @@ -1,8 +1,6 @@ 'use strict' -// EXTERNAL IMPORTS const _ = require('lodash') const { keys } = require('@govuk-pay/pay-js-commons').logging -// LOCAL IMPORTS const { addField } = require('../../services/clients/base/request-context') const Connector = require('../../services/clients/connector.client.js').ConnectorClient const { getSwitchingCredentialIfExists } = require('../../utils/credentials') @@ -68,7 +66,6 @@ module.exports = async function getSimplifiedAccount (req, res, next) { if (req.user) { const serviceExternalId = req.params[SERVICE_EXTERNAL_ID] const accountType = req.params[ACCOUNT_TYPE] - const environment = req.params[ENVIRONMENT_ID] if (!serviceExternalId || !accountType) { throw new Error('Could not resolve service external ID or gateway account type from request params') @@ -88,10 +85,6 @@ module.exports = async function getSimplifiedAccount (req, res, next) { req.service = service addField(keys.SERVICE_EXTERNAL_ID, service.externalId) } - - if (environment) { - req.isLive = environment === 'live' - } } next() diff --git a/app/views/team-members/team-member-profile.njk b/app/views/team-members/team-member-profile.njk index a83bca7458..6ed8f67ff4 100644 --- a/app/views/team-members/team-member-profile.njk +++ b/app/views/team-members/team-member-profile.njk @@ -47,7 +47,7 @@ My profile - GOV.UK Pay
{{telephone_number}} - Change mobile number + Change mobile number
@@ -64,7 +64,7 @@ My profile - GOV.UK Pay Text message {% endif %} - Change sign-in method + Change sign-in method @@ -76,7 +76,7 @@ My profile - GOV.UK Pay
{{degatewayaccountification}} - Change Account simplification preference + Change Account simplification preference