From 0defebd64901b313347b347c2d264c9c543718dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Louv-Jansen?= Date: Tue, 30 Mar 2021 00:12:46 +0200 Subject: [PATCH] [APM] Improve api tests (#95636) * [APM] Improve api tests * Fix typo --- .../public/components/shared/search_bar.tsx | 2 +- .../common/apm_api_supertest.ts | 25 +++++++++----- .../tests/settings/agent_configuration.ts | 33 ++++--------------- .../tests/settings/custom_link.ts | 25 ++++++++------ 4 files changed, 39 insertions(+), 46 deletions(-) diff --git a/x-pack/plugins/apm/public/components/shared/search_bar.tsx b/x-pack/plugins/apm/public/components/shared/search_bar.tsx index 1018b9eca211..aeb2a2c6390f 100644 --- a/x-pack/plugins/apm/public/components/shared/search_bar.tsx +++ b/x-pack/plugins/apm/public/components/shared/search_bar.tsx @@ -70,7 +70,7 @@ function DebugQueryCallout() { {i18n.translate( 'xpack.apm.searchBar.inspectEsQueriesEnabled.callout.description.advancedSettings', - { defaultMessage: 'Advanced Setting' } + { defaultMessage: 'Advanced Settings' } )} ), diff --git a/x-pack/test/apm_api_integration/common/apm_api_supertest.ts b/x-pack/test/apm_api_integration/common/apm_api_supertest.ts index 76eab7ab85cf..542982778dff 100644 --- a/x-pack/test/apm_api_integration/common/apm_api_supertest.ts +++ b/x-pack/test/apm_api_integration/common/apm_api_supertest.ts @@ -7,6 +7,7 @@ import { format } from 'url'; import supertest from 'supertest'; +import request from 'superagent'; import { MaybeParams } from '../../../plugins/apm/server/routes/typings'; import { parseEndpoint } from '../../../plugins/apm/common/apm_api/parse_endpoint'; import { APMAPI } from '../../../plugins/apm/server/routes/create_apm_api'; @@ -35,16 +36,24 @@ export function createApmApiSupertest(st: supertest.SuperTest) { // supertest doesn't throw on http errors if (res.status !== 200) { - const e = new Error( - `Unhandled ApmApiSupertest error. Status: "${ - res.status - }". Endpoint: "${endpoint}". ${JSON.stringify(res.body)}` - ); - // @ts-expect-error - e.res = res; - throw e; + throw new ApmApiError(res, endpoint); } return res; }; } + +export class ApmApiError extends Error { + res: request.Response; + + constructor(res: request.Response, endpoint: string) { + super( + `Unhandled ApmApiError. +Status: "${res.status}" +Endpoint: "${endpoint}" +Body: ${JSON.stringify(res.body)}` + ); + + this.res = res; + } +} diff --git a/x-pack/test/apm_api_integration/tests/settings/agent_configuration.ts b/x-pack/test/apm_api_integration/tests/settings/agent_configuration.ts index fbd60c0f1ab1..16c1cd92f2ea 100644 --- a/x-pack/test/apm_api_integration/tests/settings/agent_configuration.ts +++ b/x-pack/test/apm_api_integration/tests/settings/agent_configuration.ts @@ -103,41 +103,20 @@ export default function agentConfigurationTests({ getService }: FtrProviderConte describe('as a read-only user', () => { const newConfig = { service: {}, settings: { transaction_sample_rate: '0.55' } }; - it('throws when attempting to create config', async () => { - try { - await createConfiguration(newConfig, { user: 'read' }); - - // ensure that `createConfiguration` throws - expect(true).to.be(false); - } catch (e) { - expect(e.res.statusCode).to.be(403); - } + it('does not allow creating config', async () => { + await expectStatusCode(() => createConfiguration(newConfig, { user: 'read' }), 403); }); describe('when a configuration already exists', () => { before(async () => createConfiguration(newConfig)); after(async () => deleteConfiguration(newConfig)); - it('throws when attempting to update config', async () => { - try { - await updateConfiguration(newConfig, { user: 'read' }); - - // ensure that `updateConfiguration` throws - expect(true).to.be(false); - } catch (e) { - expect(e.res.statusCode).to.be(403); - } + it('does not allow updating the config', async () => { + await expectStatusCode(() => updateConfiguration(newConfig, { user: 'read' }), 403); }); - it('throws when attempting to delete config', async () => { - try { - await deleteConfiguration(newConfig, { user: 'read' }); - - // ensure that line above throws - expect(true).to.be(false); - } catch (e) { - expect(e.res.statusCode).to.be(403); - } + it('does not allow deleting the config', async () => { + await expectStatusCode(() => deleteConfiguration(newConfig, { user: 'read' }), 403); }); }); }); diff --git a/x-pack/test/apm_api_integration/tests/settings/custom_link.ts b/x-pack/test/apm_api_integration/tests/settings/custom_link.ts index c975a8219ddd..7f1fb7df6839 100644 --- a/x-pack/test/apm_api_integration/tests/settings/custom_link.ts +++ b/x-pack/test/apm_api_integration/tests/settings/custom_link.ts @@ -9,7 +9,7 @@ import expect from '@kbn/expect'; import { CustomLink } from '../../../../plugins/apm/common/custom_link/custom_link_types'; import { FtrProviderContext } from '../../common/ftr_provider_context'; import { registry } from '../../common/registry'; -import { createApmApiSupertest } from '../../common/apm_api_supertest'; +import { ApmApiError, createApmApiSupertest } from '../../common/apm_api_supertest'; export default function customLinksTests({ getService }: FtrProviderContext) { const supertestRead = createApmApiSupertest(getService('supertest')); @@ -29,15 +29,11 @@ export default function customLinksTests({ getService }: FtrProviderContext) { ], } as CustomLink; - try { - await createCustomLink(customLink); - expect(true).to.be(false); - } catch (e) { - expect(e.res.status).to.be(403); - expectSnapshot(e.res.body.message).toMatchInline( - `"To create custom links, you must be subscribed to an Elastic Gold license or above. With it, you'll have the ability to create custom links to improve your workflow when analyzing your services."` - ); - } + const err = await expectToReject(() => createCustomLink(customLink)); + expect(err.res.status).to.be(403); + expectSnapshot(err.res.body.message).toMatchInline( + `"To create custom links, you must be subscribed to an Elastic Gold license or above. With it, you'll have the ability to create custom links to improve your workflow when analyzing your services."` + ); }); }); @@ -184,3 +180,12 @@ export default function customLinksTests({ getService }: FtrProviderContext) { }); } } + +async function expectToReject(fn: () => Promise): Promise { + try { + await fn(); + } catch (e) { + return e; + } + throw new Error(`Expected fn to throw`); +}