From 87169d9e3eb3e4a65a3e44bf2b5a6d3e7a7525b5 Mon Sep 17 00:00:00 2001 From: oswaldquek Date: Wed, 15 Jan 2025 14:38:34 +0000 Subject: [PATCH] PP-12395: Cypress test for revoking api keys (#4407) * PP-12395: Cypress test for revoking api keys Cypress tests for https://github.com/alphagov/pay-selfservice/pull/4400 --- .../settings/api-keys/revoke.njk | 2 +- .../service-settings/api-keys/api-keys.cy.js | 57 +++++++++++++++++++ test/cypress/stubs/api-keys-stubs.js | 22 ++++++- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/views/simplified-account/settings/api-keys/revoke.njk b/src/views/simplified-account/settings/api-keys/revoke.njk index de3ff89d6..ad0c89ba1 100644 --- a/src/views/simplified-account/settings/api-keys/revoke.njk +++ b/src/views/simplified-account/settings/api-keys/revoke.njk @@ -14,7 +14,7 @@ errorMessage: { text: errors.formErrors['revokeApiKey'] } if errors.formErrors['revokeApiKey'] else false, fieldset: { legend: { - text: 'Are you sure you want to revoke ' + description, + text: 'Are you sure you want to revoke ' + description + '?', isPageHeading: true, classes: 'govuk-fieldset__legend--l govuk-!-font-weight-bold' } diff --git a/test/cypress/integration/simplified-account/service-settings/api-keys/api-keys.cy.js b/test/cypress/integration/simplified-account/service-settings/api-keys/api-keys.cy.js index a2b14d724..eebe30e6f 100644 --- a/test/cypress/integration/simplified-account/service-settings/api-keys/api-keys.cy.js +++ b/test/cypress/integration/simplified-account/service-settings/api-keys/api-keys.cy.js @@ -166,6 +166,63 @@ describe('Settings - API keys', () => { }) }) + describe('revoke an api key', () => { + const TOKEN_LINK = 'token-link-2' + const DESCRIPTION = 'my api key' + const apiKeys = [ + new Token().withCreatedBy('joe bloggs').withDescription(DESCRIPTION) + .withIssuedDate('10 Dec 2024').withLastUsed('10 Dec 2024').withTokenLink(TOKEN_LINK) + ] + + beforeEach(() => { + setupStubs('admin', apiKeys) + cy.task('setupStubs', [ + apiKeysStubs.getKeyByTokenLink(GATEWAY_ACCOUNT_ID, TOKEN_LINK, DESCRIPTION), + apiKeysStubs.revokeKey(GATEWAY_ACCOUNT_ID, TOKEN_LINK) + ]) + }) + + it('should show validation errors if nothing is selected', () => { + cy.visit(`/simplified/service/${SERVICE_EXTERNAL_ID}/account/${ACCOUNT_TYPE}/settings/api-keys`) + cy.get('.govuk-summary-card').within(() => { + cy.contains('h2', DESCRIPTION).should('exist') + cy.contains('a', 'Revoke').click() + }) + cy.contains('button', 'Save changes').click() + cy.url().should('include', `/simplified/service/${SERVICE_EXTERNAL_ID}/account/${ACCOUNT_TYPE}/settings/api-keys/revoke/${TOKEN_LINK}`) + cy.get('.govuk-error-summary').within(() => { + cy.contains('h2', 'There is a problem').should('exist') + cy.contains('a', `Confirm if you want to revoke ${DESCRIPTION}`).should('exist') + }) + }) + + it('should revoke the api key successfully when Yes is selected', () => { + cy.visit(`/simplified/service/${SERVICE_EXTERNAL_ID}/account/${ACCOUNT_TYPE}/settings/api-keys`) + cy.get('.govuk-summary-card').within(() => { + cy.contains('h2', DESCRIPTION).should('exist') + cy.contains('a', 'Revoke').click() + }) + cy.get('input[type="radio"][value="Yes"]').check() + cy.contains('button', 'Save changes').click() + cy.url().should('include', `/simplified/service/${SERVICE_EXTERNAL_ID}/account/${ACCOUNT_TYPE}/settings/api-keys`) + cy.contains('h1', 'Test API keys').should('exist') + cy.contains('p.govuk-notification-banner__heading', `${DESCRIPTION} was successfully revoked`).should('exist') + }) + + it('should not revoke the api key when No is selected', () => { + cy.visit(`/simplified/service/${SERVICE_EXTERNAL_ID}/account/${ACCOUNT_TYPE}/settings/api-keys`) + cy.get('.govuk-summary-card').within(() => { + cy.contains('h2', DESCRIPTION).should('exist') + cy.contains('a', 'Revoke').click() + }) + cy.get('input[type="radio"][value="No"]').check() + cy.contains('button', 'Save changes').click() + cy.url().should('include', `/simplified/service/${SERVICE_EXTERNAL_ID}/account/${ACCOUNT_TYPE}/settings/api-keys`) + cy.contains('h1', 'Test API keys').should('exist') + cy.contains('p.govuk-notification-banner__heading', `${DESCRIPTION} was successfully revoked`).should('not.exist') + }) + }) + describe('re-name an api key', () => { const NEW_API_KEY_NAME = 'api key description' // pragma: allowlist secret const TOKEN_LINK = 'token-link-1' diff --git a/test/cypress/stubs/api-keys-stubs.js b/test/cypress/stubs/api-keys-stubs.js index 06f21e862..9b89a11d9 100644 --- a/test/cypress/stubs/api-keys-stubs.js +++ b/test/cypress/stubs/api-keys-stubs.js @@ -46,8 +46,28 @@ function changeApiKeyName (tokenLink, description) { }) } +function getKeyByTokenLink (gatewayAccountId, tokenLink, description) { + const path = `/v1/frontend/auth/${gatewayAccountId}/${tokenLink}` + return stubBuilder('GET', path, 200, { + response: { + description + } + }) +} + +function revokeKey (gatewayAccountId, tokenLink) { + const path = `/v1/frontend/auth/${gatewayAccountId}` + return stubBuilder('DELETE', path, 200, { + request: { + token_link: tokenLink + } + }) +} + module.exports = { changeApiKeyName, createApiKey, - getApiKeysForGatewayAccount + getApiKeysForGatewayAccount, + getKeyByTokenLink, + revokeKey }