Skip to content

Commit

Permalink
[APM] Improve api tests (#95636)
Browse files Browse the repository at this point in the history
* [APM] Improve api tests

* Fix typo
  • Loading branch information
sorenlouv authored Mar 29, 2021
1 parent 6b96f93 commit 0defebd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 46 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/apm/public/components/shared/search_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function DebugQueryCallout() {
<EuiLink href={advancedSettingsUrl}>
{i18n.translate(
'xpack.apm.searchBar.inspectEsQueriesEnabled.callout.description.advancedSettings',
{ defaultMessage: 'Advanced Setting' }
{ defaultMessage: 'Advanced Settings' }
)}
</EuiLink>
),
Expand Down
25 changes: 17 additions & 8 deletions x-pack/test/apm_api_integration/common/apm_api_supertest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -35,16 +36,24 @@ export function createApmApiSupertest(st: supertest.SuperTest<supertest.Test>) {

// 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Expand Down
25 changes: 15 additions & 10 deletions x-pack/test/apm_api_integration/tests/settings/custom_link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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<ApmApiError>(() => 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."`
);
});
});

Expand Down Expand Up @@ -184,3 +180,12 @@ export default function customLinksTests({ getService }: FtrProviderContext) {
});
}
}

async function expectToReject<T extends Error>(fn: () => Promise<any>): Promise<T> {
try {
await fn();
} catch (e) {
return e;
}
throw new Error(`Expected fn to throw`);
}

0 comments on commit 0defebd

Please sign in to comment.