Skip to content

Commit

Permalink
PP-12395: Cypress test for revoking api keys (#4407)
Browse files Browse the repository at this point in the history
* PP-12395: Cypress test for revoking api keys

Cypress tests for #4400
  • Loading branch information
oswaldquek authored Jan 15, 2025
1 parent 9d88f8f commit 87169d9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/views/simplified-account/settings/api-keys/revoke.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
22 changes: 21 additions & 1 deletion test/cypress/stubs/api-keys-stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 87169d9

Please sign in to comment.