-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PP-13313: Validations for Worldpay credentials for a MOTO gateway acc…
…ount (#4371) * PP-13313: Worldpay details index page This commit deals with a moto-enabled gateway account that hasn't configured the one_off_customer_initiated credentials yet.
- Loading branch information
1 parent
f2fb514
commit fbba0ad
Showing
10 changed files
with
292 additions
and
12 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...mplified-account/settings/worldpay-details/credentials/worldpay-credentials.controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { response } = require('@utils/response') | ||
const formatSimplifiedAccountPathsFor = require('@utils/simplified-account/format/format-simplified-account-paths-for') | ||
const paths = require('@root/paths') | ||
const { body, validationResult } = require('express-validator') | ||
const formatValidationErrors = require('@utils/simplified-account/format/format-validation-errors') | ||
|
||
function get (req, res) { | ||
return response(req, res, 'simplified-account/settings/worldpay-details/credentials', { | ||
backLink: formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.worldpayDetails.index, | ||
req.service.externalId, req.account.type) | ||
}) | ||
} | ||
|
||
const worldpayCredentialsValidations = [ | ||
body('merchantCode').not().isEmpty().withMessage('Enter your merchant code').bail() | ||
.custom((value, { req }) => { | ||
if (req.account.allowMoto && !value.endsWith('MOTO') && !value.endsWith('MOTOGBP')) { | ||
throw new Error('Enter a MOTO merchant code. MOTO payments are enabled for the account') | ||
} | ||
return true | ||
}), | ||
body('username').not().isEmpty().withMessage('Enter your username'), | ||
body('password').not().isEmpty().withMessage('Enter your password') | ||
] | ||
|
||
async function post (req, res) { | ||
await Promise.all(worldpayCredentialsValidations.map(validation => validation.run(req))) | ||
const validationErrors = validationResult(req) | ||
if (!validationErrors.isEmpty()) { | ||
const formattedErrors = formatValidationErrors(validationErrors) | ||
return errorResponse(req, res, { | ||
summary: formattedErrors.errorSummary, | ||
formErrors: formattedErrors.formErrors | ||
}) | ||
} | ||
} | ||
|
||
const errorResponse = (req, res, errors) => { | ||
return response(req, res, 'simplified-account/settings/worldpay-details/credentials', { | ||
errors, | ||
merchantCode: req.body.merchantCode, | ||
username: req.body.username, | ||
password: req.body.password, | ||
backLink: formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.worldpayDetails.index, | ||
req.service.externalId, req.account.type) | ||
}) | ||
} | ||
|
||
module.exports = { | ||
get, | ||
post | ||
} |
127 changes: 127 additions & 0 deletions
127
...ied-account/settings/worldpay-details/credentials/worldpay-credentials.controller.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
const ControllerTestBuilder = require('@test/test-helpers/simplified-account/controllers/ControllerTestBuilder.class') | ||
const Service = require('@models/Service.class') | ||
const GatewayAccount = require('@models/GatewayAccount.class') | ||
const sinon = require('sinon') | ||
const { expect } = require('chai') | ||
const formatSimplifiedAccountPathsFor = require('@utils/simplified-account/format/format-simplified-account-paths-for') | ||
const paths = require('@root/paths') | ||
|
||
const ACCOUNT_TYPE = 'live' | ||
const SERVICE_ID = 'service-id-123abc' | ||
|
||
const mockResponse = sinon.spy() | ||
|
||
const { req, res, nextRequest, call } = new ControllerTestBuilder('@controllers/simplified-account/settings/worldpay-details/credentials/worldpay-credentials.controller') | ||
.withService(new Service({ | ||
external_id: SERVICE_ID | ||
})) | ||
.withAccount(new GatewayAccount({ | ||
type: ACCOUNT_TYPE, | ||
allow_moto: true, | ||
gateway_account_id: 1, | ||
gateway_account_credentials: [{ | ||
external_id: 'creds-id', | ||
payment_provider: 'worldpay', | ||
state: 'CREATED', | ||
created_date: '2024-11-29T11:58:36.214Z', | ||
gateway_account_id: 1, | ||
credentials: {} | ||
}] | ||
})) | ||
.withStubs({ | ||
'@utils/response': { response: mockResponse } | ||
}) | ||
.build() | ||
|
||
describe('Controller: settings/worldpay-details/credentials', () => { | ||
describe('get', () => { | ||
before(() => { | ||
call('get') | ||
}) | ||
|
||
it('should call the response method', () => { | ||
expect(mockResponse.called).to.be.true // eslint-disable-line | ||
}) | ||
|
||
it('should pass req, res and template path to the response method', () => { | ||
expect(mockResponse.args[0][0]).to.deep.equal(req) | ||
expect(mockResponse.args[0][1]).to.deep.equal(res) | ||
expect(mockResponse.args[0][2]).to.equal('simplified-account/settings/worldpay-details/credentials') | ||
}) | ||
|
||
it('should pass context data to the response method', () => { | ||
expect(mockResponse.args[0][3]).to.have.property('backLink').to.equal( | ||
formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.worldpayDetails.index, SERVICE_ID, ACCOUNT_TYPE) | ||
) | ||
}) | ||
}) | ||
|
||
describe('post', () => { | ||
describe('for MOTO gateway accounts', () => { | ||
describe('when submitting invalid data', () => { | ||
it('should render the form with validation errors when input fields are missing', async () => { | ||
nextRequest({ | ||
body: { | ||
merchantCode: '', | ||
username: '', | ||
password: '' | ||
} | ||
}) | ||
await call('post') | ||
|
||
expect(mockResponse).to.have.been.calledWith( | ||
sinon.match.any, | ||
sinon.match.any, | ||
'simplified-account/settings/worldpay-details/credentials', | ||
{ | ||
errors: { | ||
summary: [ | ||
{ text: 'Enter your username', href: '#username' }, | ||
{ text: 'Enter your password', href: '#password' }, | ||
{ text: 'Enter your merchant code', href: '#merchant-code' } | ||
], | ||
formErrors: { | ||
username: 'Enter your username', | ||
password: 'Enter your password', // pragma: allowlist secret | ||
merchantCode: 'Enter your merchant code' | ||
} | ||
}, | ||
merchantCode: '', | ||
username: '', | ||
password: '', | ||
backLink: formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.worldpayDetails.index, SERVICE_ID, ACCOUNT_TYPE) | ||
}) | ||
}) | ||
it('should render the form with MOTO validation error when merchant code is invalid', async () => { | ||
nextRequest({ | ||
body: { | ||
merchantCode: 'invalid-merchant-code', | ||
username: 'username', | ||
password: 'password' // pragma: allowlist secret | ||
} | ||
}) | ||
await call('post') | ||
|
||
expect(mockResponse).to.have.been.calledWith( | ||
sinon.match.any, | ||
sinon.match.any, | ||
'simplified-account/settings/worldpay-details/credentials', | ||
{ | ||
errors: { | ||
summary: [ | ||
{ text: 'Enter a MOTO merchant code. MOTO payments are enabled for the account', href: '#merchant-code' } | ||
], | ||
formErrors: { | ||
merchantCode: 'Enter a MOTO merchant code. MOTO payments are enabled for the account' | ||
} | ||
}, | ||
merchantCode: 'invalid-merchant-code', | ||
username: 'username', | ||
password: 'password', // pragma: allowlist secret | ||
backLink: formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.worldpayDetails.index, SERVICE_ID, ACCOUNT_TYPE) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/views/simplified-account/settings/worldpay-details/credentials.njk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
{% extends "../settings-layout.njk" %} | ||
|
||
{% block settingsPageTitle %} | ||
Worldpay details | ||
{% endblock %} | ||
|
||
{% block settingsContent %} | ||
|
||
{{ govukBackLink({ | ||
text: "Back", | ||
href: backLink | ||
}) }} | ||
|
||
{% if errors %} | ||
{{ govukErrorSummary({ | ||
titleText: "There is a problem", | ||
errorList: errors.summary | ||
}) }} | ||
{% endif %} | ||
|
||
<h1 class="govuk-heading-l">Your Worldpay credentials</h1> | ||
|
||
<p class="govuk-body govuk-!-margin-bottom-6 hint-and-body-width"> | ||
Go to your <a class="govuk-link" href="https://secure.worldpay.com/sso/public/auth/login.html">Worldpay account</a> | ||
to get the details you need to enter here or | ||
<a class="govuk-link" href="https://docs.payments.service.gov.uk/switching_to_live/set_up_a_live_worldpay_account/#connect-your-live-account-to-worldpay">read more in our documentation</a>. | ||
</p> | ||
|
||
<form id="credentials-form" method="post" novalidate> | ||
<input id="csrf" name="csrfToken" type="hidden" value="{{csrf}}" /> | ||
|
||
{{ govukInput({ | ||
label: { | ||
text: 'Merchant code' | ||
}, | ||
id: 'merchantCode', | ||
name: 'merchantCode', | ||
classes: 'govuk-input--width-20', | ||
type: 'text', | ||
value: merchantCode, | ||
errorMessage: errors.formErrors.merchantCode and { | ||
text: errors.formErrors.merchantCode | ||
} | ||
}) | ||
}} | ||
|
||
{{ govukInput({ | ||
label: { | ||
text: 'Username' | ||
}, | ||
id: 'username', | ||
name: 'username', | ||
classes: 'govuk-input--width-20', | ||
type: 'text', | ||
value: username, | ||
errorMessage: errors.formErrors.username and { | ||
text: errors.formErrors.username | ||
}, | ||
autocomplete: 'off' | ||
}) | ||
}} | ||
|
||
{{ govukInput({ | ||
label: { | ||
text: 'Password' | ||
}, | ||
id: 'password', | ||
name: 'password', | ||
classes: "govuk-input--width-20", | ||
type: 'password', | ||
value: password, | ||
errorMessage: errors.formErrors.password and { | ||
text: errors.formErrors.password | ||
}, | ||
autocomplete: 'off' | ||
}) | ||
}} | ||
|
||
{{ | ||
govukButton({ | ||
text: 'Save credentials', | ||
attributes: { | ||
id: 'submitCredentials' | ||
} | ||
}) | ||
}} | ||
</form> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters