Fixed API Key Tests (#127236)

* Added code to remove existing API keys before and after all tests.

* Fixed delete function.

* Fixing nits in PR.

* Fixed test.

* Removed await keywords per nits and broke out clearAllApiKeys() to a helper file.

* Added types for typescript

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
John Dorlus 2022-03-24 11:45:31 -04:00 committed by GitHub
parent 658a2ed893
commit b887d3812a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 4 deletions

View file

@ -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.');
}
}

View file

@ -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');
// Delete any API keys created outside of these tests
@ -51,6 +53,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();
@ -95,8 +101,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');
});