[APM] Improve api tests (#95636)

* [APM] Improve api tests

* Fix typo
This commit is contained in:
Søren Louv-Jansen 2021-03-30 00:12:46 +02:00 committed by GitHub
parent 6b96f93720
commit 0defebd649
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 46 deletions

View file

@ -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>
),

View file

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

View file

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

View file

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