-
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-12395: Validation when creating API key (#4397)
* PP-12395: Validation when creating API key A description is not valid if it's empty or more than 50 chars. However given the text box has a maxLength of 50, the latter validation error can't occur in practice.
- Loading branch information
1 parent
c760810
commit 5bf46dc
Showing
5 changed files
with
113 additions
and
31 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
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 |
---|---|---|
|
@@ -45,37 +45,94 @@ describe('Controller: settings/create-api-key', () => { | |
}) | ||
|
||
describe('post', () => { | ||
before(() => { | ||
nextRequest({ | ||
body: { | ||
description: 'a test api key' | ||
}, | ||
user: { | ||
email: '[email protected]' | ||
} | ||
describe('a valid description', () => { | ||
before(() => { | ||
nextRequest({ | ||
body: { | ||
description: 'a test api key' | ||
}, | ||
user: { | ||
email: '[email protected]' | ||
} | ||
}) | ||
call('post') | ||
}) | ||
call('post') | ||
}) | ||
|
||
it('should submit values to the api keys service', () => { | ||
expect(apiKeysService.createApiKey).to.have.been.calledWith( | ||
sinon.match.any, | ||
'a test api key', | ||
'[email protected]', | ||
TOKEN_SOURCE.API | ||
) | ||
}) | ||
it('should submit values to the api keys service', () => { | ||
expect(apiKeysService.createApiKey).to.have.been.calledWith( | ||
sinon.match.any, | ||
'a test api key', | ||
'[email protected]', | ||
TOKEN_SOURCE.API | ||
) | ||
}) | ||
|
||
it('should call the response method', () => { | ||
expect(mockResponse).to.have.been.calledOnce // eslint-disable-line | ||
it('should call the response method', () => { | ||
expect(mockResponse).to.have.been.calledOnce // eslint-disable-line | ||
}) | ||
|
||
it('should pass context data to the response method', () => { | ||
expect(mockResponse.args[0][2]).to.equal('simplified-account/settings/api-keys/new-api-key-details') | ||
expect(mockResponse.args[0][3]).to.have.property('backToApiKeysLink').to.equal( | ||
formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.apiKeys.index, SERVICE_ID, ACCOUNT_TYPE)) | ||
expect(mockResponse.args[0][3]).to.have.property('apiKey').to.equal(newApiKey) | ||
expect(mockResponse.args[0][3]).to.have.property('description').to.equal('a test api key') | ||
}) | ||
}) | ||
|
||
it('should pass context data to the response method', () => { | ||
expect(mockResponse.args[0][2]).to.equal('simplified-account/settings/api-keys/new-api-key-details') | ||
expect(mockResponse.args[0][3]).to.have.property('backToApiKeysLink').to.equal( | ||
formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.apiKeys.index, SERVICE_ID, ACCOUNT_TYPE)) | ||
expect(mockResponse.args[0][3]).to.have.property('apiKey').to.equal(newApiKey) | ||
expect(mockResponse.args[0][3]).to.have.property('description').to.equal('a test api key') | ||
describe('an invalid description', () => { | ||
function assertMockResponseArgs (errorMessage) { | ||
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(errorMessage) | ||
expect(mockResponse.args[0][3].errors.formErrors.description).to.equal(errorMessage) | ||
expect(mockResponse.args[0][3].backLink).to.equal( | ||
formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.apiKeys.index, SERVICE_ID, ACCOUNT_TYPE)) | ||
} | ||
|
||
describe('empty description', () => { | ||
before(() => { | ||
nextRequest({ | ||
body: { | ||
description: '' | ||
}, | ||
user: { | ||
email: '[email protected]' | ||
} | ||
}) | ||
call('post') | ||
}) | ||
|
||
it('should not call apiKeysService.createApiKey', () => { | ||
sinon.assert.notCalled(apiKeysService.createApiKey) | ||
}) | ||
|
||
it('should pass req, res, template path and context to the response method', () => { | ||
assertMockResponseArgs('Name must not be empty') | ||
}) | ||
}) | ||
|
||
describe('description more than 50 chars', () => { | ||
before(() => { | ||
nextRequest({ | ||
body: { | ||
description: 'more than fifty chars more than fifty chars more than fifty chars more than fifty chars' | ||
}, | ||
user: { | ||
email: '[email protected]' | ||
} | ||
}) | ||
call('post') | ||
}) | ||
|
||
it('should not call apiKeysService.createApiKey', () => { | ||
sinon.assert.notCalled(apiKeysService.createApiKey) | ||
}) | ||
|
||
it('should pass req, res, template path and context to the response method', () => { | ||
assertMockResponseArgs('Name must be 50 characters or fewer') | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
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