-
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.
- Loading branch information
1 parent
054a76a
commit 9f4bbc9
Showing
11 changed files
with
171 additions
and
15 deletions.
There are no files selected for viewing
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
36 changes: 36 additions & 0 deletions
36
app/controllers/simplified-account/settings/api-keys/change-name/change-name.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,36 @@ | ||
const { response } = require('@utils/response') | ||
const formatSimplifiedAccountPathsFor = require('../../../../../utils/simplified-account/format/format-simplified-account-paths-for') | ||
const paths = require('@root/paths') | ||
const { changeApiKeyName } = require('@services/api-keys.service') | ||
const DESCRIPTION_VALIDATION = require('@controllers/simplified-account/settings/api-keys/validations') | ||
const { 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/api-keys/api-key-name', { | ||
backLink: formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.apiKeys.index, req.service.externalId, req.account.type) | ||
}) | ||
} | ||
|
||
async function post (req, res) { | ||
await Promise.all(DESCRIPTION_VALIDATION.map(validation => validation.run(req))) | ||
const errors = validationResult(req) | ||
|
||
if (!errors.isEmpty()) { | ||
const formattedErrors = formatValidationErrors(errors) | ||
return response(req, res, 'simplified-account/settings/api-keys/api-key-name', { | ||
errors: { | ||
summary: formattedErrors.errorSummary, | ||
formErrors: formattedErrors.formErrors | ||
}, | ||
backLink: formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.apiKeys.index, req.service.externalId, req.account.type) | ||
}) | ||
} | ||
|
||
const tokenLink = req.params.tokenLink | ||
const description = req.body.description | ||
await changeApiKeyName(tokenLink, description) | ||
res.redirect(formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.apiKeys.index, req.service.externalId, req.account.type)) | ||
} | ||
|
||
module.exports = { get, post } |
96 changes: 96 additions & 0 deletions
96
...ntrollers/simplified-account/settings/api-keys/change-name/change-name.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,96 @@ | ||
const ControllerTestBuilder = require('@test/test-helpers/simplified-account/controllers/ControllerTestBuilder.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 apiKeysService = { | ||
changeApiKeyName: sinon.stub().resolves() | ||
} | ||
const { | ||
req, | ||
res, | ||
nextRequest, | ||
call | ||
} = new ControllerTestBuilder('@controllers/simplified-account/settings/api-keys/change-name/change-name.controller') | ||
.withServiceExternalId(SERVICE_ID) | ||
.withAccountType(ACCOUNT_TYPE) | ||
.withStubs({ | ||
'@utils/response': { response: mockResponse }, | ||
'@services/api-keys.service': apiKeysService | ||
}) | ||
.build() | ||
|
||
describe('Controller: settings/api-keys/change-name', () => { | ||
describe('get', () => { | ||
before(() => { | ||
call('get') | ||
}) | ||
|
||
it('should call the response method', () => { | ||
expect(mockResponse).to.have.been.calledOnce // eslint-disable-line | ||
}) | ||
|
||
it('should pass req, res, template path and context to the response method', () => { | ||
expect(mockResponse).to.have.been.calledWith(req, res, 'simplified-account/settings/api-keys/api-key-name', | ||
{ backLink: formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.apiKeys.index, SERVICE_ID, ACCOUNT_TYPE) }) | ||
}) | ||
}) | ||
|
||
describe('post', () => { | ||
describe('a valid description', () => { | ||
before(() => { | ||
nextRequest({ | ||
body: { | ||
description: 'a test api key' | ||
}, | ||
params: { | ||
tokenLink: '123-456-abc' | ||
} | ||
}) | ||
call('post') | ||
}) | ||
|
||
it('should submit values to the api keys service', () => { | ||
expect(apiKeysService.changeApiKeyName).to.have.been.calledWith('123-456-abc', 'a test api key') | ||
}) | ||
|
||
it('should redirect to the api keys index page', () => { | ||
expect(res.redirect.calledOnce).to.be.true // eslint-disable-line | ||
expect(res.redirect.args[0][0]).to.include( | ||
formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.apiKeys.index, SERVICE_ID, ACCOUNT_TYPE) | ||
) | ||
}) | ||
}) | ||
|
||
describe('an invalid description', () => { | ||
before(() => { | ||
nextRequest({ | ||
body: { | ||
description: '' | ||
}, | ||
params: { | ||
tokenLink: '123-456-abc' | ||
} | ||
}) | ||
call('post') | ||
}) | ||
|
||
it('should not call apiKeysService.changeApiKeyName', () => { | ||
sinon.assert.notCalled(apiKeysService.changeApiKeyName) | ||
}) | ||
|
||
it('should pass req, res, template path and context to the response method', () => { | ||
expect(mockResponse.calledOnce).to.be.true // eslint-disable-line | ||
expect(mockResponse.args[0][2]).to.equal('simplified-account/settings/api-keys/api-key-name') | ||
expect(mockResponse.args[0][3].errors.summary[0].text).to.equal('Name must not be empty') | ||
expect(mockResponse.args[0][3].errors.formErrors.description).to.equal('Name must not be empty') | ||
expect(mockResponse.args[0][3].backLink).to.equal( | ||
formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.apiKeys.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
11 changes: 11 additions & 0 deletions
11
app/controllers/simplified-account/settings/api-keys/validations.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,11 @@ | ||
const { body } = require('express-validator') | ||
const DESCRIPTION_VALIDATION = [ | ||
body('description') | ||
.trim() | ||
.notEmpty() | ||
.withMessage('Name must not be empty') | ||
.isLength({ max: 50 }) | ||
.withMessage('Name must be 50 characters or fewer') | ||
] | ||
|
||
module.exports = DESCRIPTION_VALIDATION |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ | |
actions: { | ||
items: [ | ||
{ | ||
href: '#', | ||
href: key.changeNameLink, | ||
text: 'Change name' | ||
}, | ||
{ | ||
|