Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PP-12395: Cypress test for API keys landing page #4392

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ 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 apiKeys = [{ description: 'my token', createdBy: 'system generated', issuedDate: '12 Dec 2024' }]
const apiKeysService = {
getActiveKeys: sinon.stub().resolves(tokens)
getActiveKeys: sinon.stub().resolves(apiKeys)
}

const {
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('Controller: settings/api-keys', () => {

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)
expect(mockResponse.args[0][3]).to.have.property('activeKeys').to.deep.equal(apiKeys)
})
})
})
9 changes: 9 additions & 0 deletions app/models/Token.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class Token {
return this
}

toJson () {
return {
...this.description && { description: this.description },
...this.createdBy && { created_by: this.createdBy },
...this.issuedDate && { issued_date: this.issuedDate },
...this.lastUsed && { last_used: this.lastUsed }
}
}

static fromJson (data) {
if (!data) {
return undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const userStubs = require('@test/cypress/stubs/user-stubs')
const ROLES = require('@test/fixtures/roles.fixtures')
const gatewayAccountStubs = require('@test/cypress/stubs/gateway-account-stubs')
const apiKeysStubs = require('@test/cypress/stubs/api-keys-stubs')
const { Token } = require('@models/Token.class')

const USER_EXTERNAL_ID = 'user-123-abc'
const SERVICE_EXTERNAL_ID = 'service-456-def'
const GATEWAY_ACCOUNT_ID = 11
const ACCOUNT_TYPE = 'test'

const setupStubs = (role = 'admin', apiKeys = []) => {
cy.task('setupStubs', [
userStubs.getUserSuccess({
userExternalId: USER_EXTERNAL_ID,
gatewayAccountId: GATEWAY_ACCOUNT_ID,
serviceName: { en: 'My cool service' },
serviceExternalId: SERVICE_EXTERNAL_ID,
role: ROLES[role],
features: 'degatewayaccountification' // TODO remove features once simplified accounts are live
}),
gatewayAccountStubs.getAccountByServiceIdAndAccountType(SERVICE_EXTERNAL_ID, ACCOUNT_TYPE, { gateway_account_id: GATEWAY_ACCOUNT_ID }),
apiKeysStubs.getApiKeysForGatewayAccount(GATEWAY_ACCOUNT_ID, apiKeys)
])
}

describe('Settings - API keys', () => {
beforeEach(() => {
cy.setEncryptedCookies(USER_EXTERNAL_ID)
})

describe('for an admin user', () => {

describe('when there are no active API keys', () => {
beforeEach(() => {
setupStubs()
})
it('should show appropriate buttons and text', () => {
cy.visit(`/simplified/service/${SERVICE_EXTERNAL_ID}/account/${ACCOUNT_TYPE}/settings/api-keys`)

cy.get('#api-keys').should('have.text', 'API keys')
cy.get('.service-settings-pane')
.find('a')
.contains('Create a new API key')
.should('exist')
cy.get('.service-settings-pane')
.find('h2')
.contains('There are no active test API keys')
.should('exist')
cy.get('.service-settings-pane')
.find('a')
.contains('Show revoked API keys')
.should('exist')
})
})

describe('when there are active API keys', () => {
const apiKeys = [
new Token().withCreatedBy('system generated').withDescription('description').withIssuedDate('12 Dec 2024'),
new Token().withCreatedBy('algae bra').withDescription('mathematical clothes').withIssuedDate('10 Dec 2024')
]
beforeEach(() => {
setupStubs('admin', apiKeys)
})
it('should show appropriate buttons and text', () => {
cy.visit(`/simplified/service/${SERVICE_EXTERNAL_ID}/account/${ACCOUNT_TYPE}/settings/api-keys`)

cy.get('#api-keys').should('have.text', 'API keys')
cy.get('.service-settings-pane')
.find('a')
.contains('Create a new API key')
.should('exist')
cy.get('.service-settings-pane')
.find('h2')
.contains('Active test API keys (2)')
.should('exist')
cy.get('.service-settings-pane')
.find('a')
.contains('Show revoked API keys')
.should('exist')
Comment on lines +65 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably test for more than just these bits of text - it should test that the summary cards are shown with the correct content - this could be in a separate test though to aid readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call - test added

})
})
})

describe('for a non-admin user', () => {
beforeEach(() => {
setupStubs('view-only')
})
it('should return forbidden when visiting the url directly', () => {
cy.request({
url: `/simplified/service/${SERVICE_EXTERNAL_ID}/account/${ACCOUNT_TYPE}/settings/api-keys`,
failOnStatusCode: false
}).then((response) => {
expect(response.status).to.eq(403)
})
})

it('should not show API keys link in the navigation panel', () => {
cy.visit(`/simplified/service/${SERVICE_EXTERNAL_ID}/account/${ACCOUNT_TYPE}/settings`)
cy.get('#api-keys').should('not.exist')
})
})
})
18 changes: 18 additions & 0 deletions test/cypress/stubs/api-keys-stubs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { stubBuilder } = require('@test/cypress/stubs/stub-builder')

/**
* @param {number} gatewayAccountId
* @param {[Token]} tokens
*/
function getApiKeysForGatewayAccount (gatewayAccountId, tokens = []) {
const path = `/v1/frontend/auth/${gatewayAccountId}`
return stubBuilder('GET', path, 200, {
response: {
tokens: tokens.map(t => t.toJson())
}
})
}

module.exports = {
getApiKeysForGatewayAccount
}
Loading