-
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: API Keys landing page (#4385)
* PP-12395: API keys landing page
- Loading branch information
1 parent
6153ad0
commit 54b4cc8
Showing
8 changed files
with
213 additions
and
2 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
app/controllers/simplified-account/settings/api-keys/api-keys.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,14 @@ | ||
const { response } = require('@utils/response') | ||
const apiKeysService = require('@services/api-keys.service') | ||
|
||
async function get (req, res) { | ||
const activeKeys = await apiKeysService.getActiveKeys(req.account.id) | ||
return response(req, res, 'simplified-account/settings/api-keys/index', { | ||
accountType: req.account.type, | ||
activeKeys, | ||
createApiKeyLink: '#', | ||
showRevokedKeysLink: '#' | ||
}) | ||
} | ||
|
||
module.exports = { get } |
45 changes: 45 additions & 0 deletions
45
app/controllers/simplified-account/settings/api-keys/api-keys.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,45 @@ | ||
const ControllerTestBuilder = require('@test/test-helpers/simplified-account/controllers/ControllerTestBuilder.class') | ||
const sinon = require('sinon') | ||
const { expect } = require('chai') | ||
const ACCOUNT_TYPE = 'live' | ||
const SERVICE_ID = 'service-id-123abc' | ||
|
||
const mockResponse = sinon.spy() | ||
const tokens = [{ description: 'my token', createdBy: 'system generated', issuedDate: '12 Dec 2024' }] | ||
const apiKeysService = { | ||
getActiveKeys: sinon.stub().resolves(tokens) | ||
} | ||
|
||
const { | ||
req, | ||
res, | ||
call | ||
} = new ControllerTestBuilder('@controllers/simplified-account/settings/api-keys/api-keys.controller') | ||
.withServiceExternalId(SERVICE_ID) | ||
.withAccountType(ACCOUNT_TYPE) | ||
.withStubs({ | ||
'@utils/response': { response: mockResponse }, | ||
'@services/api-keys.service': apiKeysService | ||
}) | ||
.build() | ||
|
||
describe('Controller: settings/api-keys', () => { | ||
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 and template path to the response method', () => { | ||
expect(mockResponse).to.have.been.calledWith(req, res, 'simplified-account/settings/api-keys/index') | ||
}) | ||
|
||
it('should pass context data to the response method', () => { | ||
expect(mockResponse.args[0][3]).to.have.property('accountType').to.equal('live') | ||
expect(mockResponse.args[0][3]).to.have.property('activeKeys').to.deep.equal(tokens) | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class Token { | ||
withDescription (description) { | ||
this.description = description | ||
return this | ||
} | ||
|
||
withCreatedBy (createdBy) { | ||
this.createdBy = createdBy | ||
return this | ||
} | ||
|
||
withIssuedDate (issuedDate) { | ||
this.issuedDate = issuedDate | ||
return this | ||
} | ||
|
||
withLastUsed (lastUsed) { | ||
this.lastUsed = lastUsed | ||
return this | ||
} | ||
|
||
static fromJson (data) { | ||
if (!data) { | ||
return undefined | ||
} | ||
return new Token() | ||
.withDescription(data?.description) | ||
.withCreatedBy(data?.created_by) | ||
.withIssuedDate(data?.issued_date) | ||
.withLastUsed(data?.last_used) | ||
} | ||
} | ||
|
||
module.exports.Token = Token |
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,18 @@ | ||
const publicAuthClient = require('@services/clients/public-auth.client') | ||
const { Token } = require('@models/Token.class') | ||
|
||
/** | ||
* Gets the list of active api keys for a gateway account | ||
* @param {string} gatewayAccountId | ||
* @returns {[Token]} | ||
*/ | ||
const getActiveKeys = async (gatewayAccountId) => { | ||
const publicAuthData = await publicAuthClient.getActiveTokensForAccount({ | ||
accountId: gatewayAccountId | ||
}) | ||
return publicAuthData.tokens.map(tokenData => Token.fromJson(tokenData)) | ||
} | ||
|
||
module.exports = { | ||
getActiveKeys | ||
} |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
{% extends "../settings-layout.njk" %} | ||
|
||
{% block settingsPageTitle %} | ||
{% if accountType === 'test' %}Test{% else %}Live{% endif %} API keys | ||
{% endblock %} | ||
|
||
{% block settingsContent %} | ||
|
||
<h1 class="govuk-heading-l page-title"> | ||
{% if accountType === 'test' %}Test{% else %}Live{% endif %} API keys | ||
</h1> | ||
|
||
{{ govukWarningText( | ||
{ | ||
text: 'Do not use a test API key on your live account. It will not work properly.', | ||
iconFallbackText: 'Warning' | ||
} if accountType === 'test' else { | ||
text: 'Do not use a live API key on your test account. It will not work properly.', | ||
iconFallbackText: 'Warning' | ||
} | ||
) }} | ||
|
||
<p class="govuk-body">Use these to connect your digital service to Pay and to access the reporting API.</p> | ||
<p class="govuk-body"> | ||
You do not need API keys to use | ||
<a class="govuk-link" href="https://www.payments.service.gov.uk/govuk-payment-pages/">payment links</a>. | ||
</p> | ||
|
||
{{ govukButton({ | ||
href: createApiKeyLink, | ||
text: 'Create a new API key', | ||
classes: 'govuk-!-margin-top-2' | ||
}) }} | ||
|
||
{% if activeKeys.length == 0 %} | ||
<h2 class="govuk-heading-m govuk-!-margin-top-2"> | ||
There are no active {{ accountType }} API keys | ||
</h2> | ||
{% else %} | ||
<h2 class="govuk-heading-m govuk-!-margin-top-2"> | ||
Active {{ accountType }} API keys ({{ activeKeys.length }}) | ||
</h2> | ||
{% for key in activeKeys %} | ||
{{ govukSummaryList({ | ||
card: { | ||
title: { | ||
text: key.description | ||
}, | ||
actions: { | ||
items: [ | ||
{ | ||
href: '#', | ||
text: 'Change name' | ||
}, | ||
{ | ||
href: '#', | ||
text: 'Revoke' | ||
} | ||
] | ||
} | ||
}, | ||
rows: [ | ||
{ | ||
key: { | ||
text: 'Created by' | ||
}, | ||
value: { | ||
text: key.createdBy | ||
} | ||
}, | ||
{ | ||
key: { | ||
text: 'Date created' | ||
}, | ||
value: { | ||
text: key.issuedDate | ||
} | ||
}, | ||
{ | ||
key: { | ||
text: 'Last used' | ||
}, | ||
value: { | ||
text: key.lastUsed | ||
} | ||
} | ||
] | ||
}) }} | ||
{% endfor %} | ||
{% endif %} | ||
|
||
{{ govukButton({ | ||
href: showRevokedKeysLink, | ||
text: 'Show revoked API keys', | ||
classes: 'govuk-!-margin-top-2 govuk-button--secondary' | ||
}) }} | ||
{% endblock %} |