Skip to content

Commit

Permalink
[squash] service name controller tests, remove degateway references
Browse files Browse the repository at this point in the history
  • Loading branch information
nlsteers committed Oct 11, 2024
1 parent db300db commit 77881d8
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
})
2 changes: 1 addition & 1 deletion app/controllers/user/degateway/post.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions app/views/team-members/team-member-profile.njk
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ My profile - GOV.UK Pay
<dd class="profile-settings__value govuk-table__cell" id="telephone-number">
{{telephone_number}}
<span class="profile-settings__change-link">
<a class="govuk-link" href="{{routes.user.profile.phoneNumber }}" id="change-phone-link">Change <span class="govuk-visually-hidden">mobile number</span></a>
<a class="govuk-link govuk-link--no-visited-state" href="{{routes.user.profile.phoneNumber }}" id="change-phone-link">Change <span class="govuk-visually-hidden">mobile number</span></a>
</span>
</dd>
</div>
Expand All @@ -64,7 +64,7 @@ My profile - GOV.UK Pay
Text message
{% endif %}
<span class="profile-settings__change-link">
<a class="govuk-link" href="{{two_factor_auth_link}}">Change <span class="govuk-visually-hidden">sign-in method</span></a>
<a class="govuk-link govuk-link--no-visited-state" href="{{two_factor_auth_link}}">Change <span class="govuk-visually-hidden">sign-in method</span></a>
</span>
</dd>
</div>
Expand All @@ -76,7 +76,7 @@ My profile - GOV.UK Pay
<dd class="profile-settings__value govuk-table__cell" id="degatewayaccountification">
{{degatewayaccountification}}
<span class="profile-settings__change-link">
<a class="govuk-link" href="{{ routes.user.profile.degateway }}" id="change-degateway-preference-link">Change <span class="govuk-visually-hidden">Account simplification preference</span></a>
<a class="govuk-link govuk-link--no-visited-state" href="{{ routes.user.profile.degateway }}" id="change-degateway-preference-link">Change <span class="govuk-visually-hidden">Account simplification preference</span></a>
</span>
</dd>
</div>
Expand Down

0 comments on commit 77881d8

Please sign in to comment.