From 3e09ffc239215999f28bed0d3f7a7cebf4a92657 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Sat, 23 Nov 2024 03:01:35 +1100 Subject: [PATCH] [8.17] Unskips api key functional tests in MKI (#196572) (#201394) # Backport This will backport the following commits from `main` to `8.17`: - [Unskips api key functional tests in MKI (#196572)](https://github.com/elastic/kibana/pull/196572) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Jeramy Soucy --- .../api_keys/api_keys_grid/api_keys_table.tsx | 2 +- .../functional/page_objects/api_keys_page.ts | 18 ++++++++- .../common/platform_security/api_keys.ts | 38 ++++++------------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/x-pack/plugins/security/public/management/api_keys/api_keys_grid/api_keys_table.tsx b/x-pack/plugins/security/public/management/api_keys/api_keys_grid/api_keys_table.tsx index 63124b4db2d44..f536b5838bab7 100644 --- a/x-pack/plugins/security/public/management/api_keys/api_keys_grid/api_keys_table.tsx +++ b/x-pack/plugins/security/public/management/api_keys/api_keys_grid/api_keys_table.tsx @@ -230,7 +230,7 @@ export const ApiKeysTable: FunctionComponent = ({ color: 'danger', onClick: (item) => onDelete([item]), available: deletable, - 'data-test-subj': 'apiKeysTableDeleteAction', + 'data-test-subj': (item) => `apiKeysTableDeleteAction-${item.name}`, }, ], }); diff --git a/x-pack/test/functional/page_objects/api_keys_page.ts b/x-pack/test/functional/page_objects/api_keys_page.ts index 8f74f927b976f..9b196f70eeef0 100644 --- a/x-pack/test/functional/page_objects/api_keys_page.ts +++ b/x-pack/test/functional/page_objects/api_keys_page.ts @@ -78,15 +78,25 @@ export function ApiKeysPageProvider({ getService }: FtrProviderContext) { return euiCallOutHeader.getVisibleText(); }, + async isPromptPage() { + return await testSubjects.exists('apiKeysCreatePromptButton'); + }, + async getApiKeysFirstPromptTitle() { const titlePromptElem = await find.byCssSelector('.euiEmptyPrompt .euiTitle'); return await titlePromptElem.getVisibleText(); }, + async deleteApiKeyByName(apiKeyName: string) { + await testSubjects.click(`apiKeysTableDeleteAction-${apiKeyName}`); + await testSubjects.click('confirmModalConfirmButton'); + await testSubjects.waitForDeleted(`apiKeyRowName-${apiKeyName}`); + }, + async deleteAllApiKeyOneByOne() { - const hasApiKeysToDelete = await testSubjects.exists('apiKeysTableDeleteAction'); + const hasApiKeysToDelete = await testSubjects.exists('*apiKeysTableDeleteAction'); if (hasApiKeysToDelete) { - const apiKeysToDelete = await testSubjects.findAll('apiKeysTableDeleteAction'); + const apiKeysToDelete = await testSubjects.findAll('*apiKeysTableDeleteAction'); for (const element of apiKeysToDelete) { await element.click(); await testSubjects.click('confirmModalConfirmButton'); @@ -113,6 +123,10 @@ export function ApiKeysPageProvider({ getService }: FtrProviderContext) { await testSubjects.existOrFail(`apiKeyRowName-${apiKeyName}`); }, + async doesApiKeyExist(apiKeyName: string) { + return await testSubjects.exists(`apiKeyRowName-${apiKeyName}`); + }, + async getMetadataSwitch() { return await testSubjects.find('apiKeysMetadataSwitch'); }, diff --git a/x-pack/test_serverless/functional/test_suites/common/platform_security/api_keys.ts b/x-pack/test_serverless/functional/test_suites/common/platform_security/api_keys.ts index 897299bb265ed..3a6cc43e31486 100644 --- a/x-pack/test_serverless/functional/test_suites/common/platform_security/api_keys.ts +++ b/x-pack/test_serverless/functional/test_suites/common/platform_security/api_keys.ts @@ -6,47 +6,31 @@ */ import expect from '@kbn/expect'; -import { Client } from '@elastic/elasticsearch'; -import { ToolingLog } from '@kbn/tooling-log'; import { FtrProviderContext } from '../../../ftr_provider_context'; -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) => { - await esClient.security.invalidateApiKey({ ids: [key.id] }); - }) - ); - } else { - logger.debug('No API keys to delete.'); - } -} - export default ({ getPageObjects, getService }: FtrProviderContext) => { const pageObjects = getPageObjects(['common', 'svlCommonPage', 'apiKeys']); const browser = getService('browser'); - const es = getService('es'); - const log = getService('log'); describe('API keys', function () { - // TimeoutError: Waiting for element to be located By(css selector, [data-test-subj="apiKeysCreatePromptButton"]) Wait timed out after 10028ms - this.tags(['failsOnMKI']); before(async () => { await pageObjects.svlCommonPage.loginAsAdmin(); }); - after(async () => { - await clearAllApiKeys(es, log); - }); - it('should create and delete API keys correctly', async () => { await pageObjects.common.navigateToUrl('management', 'security/api_keys', { shouldUseHashForSubUrl: false, }); - const apiKeyName = 'Happy API Key'; - await pageObjects.apiKeys.clickOnPromptCreateApiKey(); + // name needs to be unique because we will confirm deletion by name + const apiKeyName = `API Key ${Date.now()}`; + + // If there are any existing API keys (e.g. will occur on projects created with QAF), + // the table will be displayed. Otherwise, the empty prompt is displayed. + const isPromptPage = await pageObjects.apiKeys.isPromptPage(); + if (isPromptPage) await pageObjects.apiKeys.clickOnPromptCreateApiKey(); + else await pageObjects.apiKeys.clickOnTableCreateApiKey(); + expect(await browser.getCurrentUrl()).to.contain('app/management/security/api_keys/create'); expect(await pageObjects.apiKeys.getFlyoutTitleText()).to.be('Create API key'); @@ -61,7 +45,9 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { expect(await pageObjects.apiKeys.isApiKeyModalExists()).to.be(false); expect(newApiKeyCreation).to.be(`Created API key '${apiKeyName}'`); - await pageObjects.apiKeys.deleteAllApiKeyOneByOne(); + await pageObjects.apiKeys.ensureApiKeyExists(apiKeyName); + await pageObjects.apiKeys.deleteApiKeyByName(apiKeyName); + expect(await pageObjects.apiKeys.doesApiKeyExist(apiKeyName)).to.be(false); }); }); };