diff --git a/x-pack/test/functional/apps/api_keys/api_keys_helpers.ts b/x-pack/test/functional/apps/api_keys/api_keys_helpers.ts new file mode 100644 index 0000000000000..5c9fdb65a503b --- /dev/null +++ b/x-pack/test/functional/apps/api_keys/api_keys_helpers.ts @@ -0,0 +1,22 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { Client } from '@elastic/elasticsearch'; +import { ToolingLog } from '@kbn/dev-utils'; + +export default async function clearAllApiKeys(esClient: Client, logger: ToolingLog) { + const existingKeys = await esClient.security.queryApiKeys(); + if (existingKeys.count > 0) { + await Promise.all( + existingKeys.api_keys.map(async (key) => { + esClient.security.invalidateApiKey({ ids: [key.id] }); + }) + ); + } else { + logger.debug('No API keys to delete.'); + } +} diff --git a/x-pack/test/functional/apps/api_keys/home_page.ts b/x-pack/test/functional/apps/api_keys/home_page.ts index 5907247527585..bf155dc3ddab4 100644 --- a/x-pack/test/functional/apps/api_keys/home_page.ts +++ b/x-pack/test/functional/apps/api_keys/home_page.ts @@ -6,9 +6,11 @@ */ import expect from '@kbn/expect'; +import clearAllApiKeys from './api_keys_helpers'; import { FtrProviderContext } from '../../ftr_provider_context'; export default ({ getPageObjects, getService }: FtrProviderContext) => { + const es = getService('es'); const pageObjects = getPageObjects(['common', 'apiKeys']); const log = getService('log'); const security = getService('security'); @@ -18,6 +20,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { describe('Home page', function () { before(async () => { + await clearAllApiKeys(es, log); await security.testUser.setRoles(['kibana_admin']); await pageObjects.common.navigateToApp('apiKeys'); }); @@ -39,8 +42,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { describe('creates API key', function () { before(async () => { - await security.testUser.setRoles(['kibana_admin']); - await security.testUser.setRoles(['test_api_keys']); + await security.testUser.setRoles(['kibana_admin', 'test_api_keys']); await pageObjects.common.navigateToApp('apiKeys'); }); @@ -48,6 +50,10 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { await pageObjects.apiKeys.deleteAllApiKeyOneByOne(); }); + after(async () => { + await clearAllApiKeys(es, log); + }); + it('when submitting form, close dialog and displays new api key', async () => { const apiKeyName = 'Happy API Key'; await pageObjects.apiKeys.clickOnPromptCreateApiKey(); @@ -92,8 +98,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { describe('deletes API key(s)', function () { before(async () => { - await security.testUser.setRoles(['kibana_admin']); - await security.testUser.setRoles(['test_api_keys']); + await security.testUser.setRoles(['kibana_admin', 'test_api_keys']); await pageObjects.common.navigateToApp('apiKeys'); });