Skip to content

Commit

Permalink
[APM] Write tests for the Custom Link API (elastic#60899) (elastic#61962
Browse files Browse the repository at this point in the history
)

* adding test to custom link api

* adding api tests

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
cauemarcondes and elasticmachine authored Mar 31, 2020
1 parent 813f42a commit c4e18a8
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
144 changes: 144 additions & 0 deletions x-pack/test/api_integration/apis/apm/custom_link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
// import querystring from 'querystring';
// import {isEmpty} from 'lodash'
import URL from 'url';
import expect from '@kbn/expect';
import { CustomLink } from '../../../../plugins/apm/common/custom_link/custom_link_types';
import { FtrProviderContext } from '../../ftr_provider_context';

export default function customLinksTests({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const log = getService('log');

function searchCustomLinks(filters?: any) {
const path = URL.format({
pathname: `/api/apm/settings/custom_links`,
query: filters,
});
return supertest.get(path).set('kbn-xsrf', 'foo');
}

async function createCustomLink(customLink: CustomLink) {
log.debug('creating configuration', customLink);
const res = await supertest
.post(`/api/apm/settings/custom_links`)
.send(customLink)
.set('kbn-xsrf', 'foo');

throwOnError(res);

return res;
}

async function updateCustomLink(id: string, customLink: CustomLink) {
log.debug('updating configuration', id, customLink);
const res = await supertest
.put(`/api/apm/settings/custom_links/${id}`)
.send(customLink)
.set('kbn-xsrf', 'foo');

throwOnError(res);

return res;
}

async function deleteCustomLink(id: string) {
log.debug('deleting configuration', id);
const res = await supertest
.delete(`/api/apm/settings/custom_links/${id}`)
.set('kbn-xsrf', 'foo');

throwOnError(res);

return res;
}

function throwOnError(res: any) {
const { statusCode, req, body } = res;
if (statusCode !== 200) {
throw new Error(`
Endpoint: ${req.method} ${req.path}
Service: ${JSON.stringify(res.request._data.service)}
Status code: ${statusCode}
Response: ${body.message}`);
}
}

describe('custom links', () => {
before(async () => {
const customLink = {
url: 'https://elastic.co',
label: 'with filters',
filters: [
{ key: 'service.name', value: 'baz' },
{ key: 'transaction.type', value: 'qux' },
],
} as CustomLink;
await createCustomLink(customLink);
});
it('fetches a custom link', async () => {
const { status, body } = await searchCustomLinks({
'service.name': 'baz',
'transaction.type': 'qux',
});
const { label, url, filters } = body[0];

expect(status).to.equal(200);
expect({ label, url, filters }).to.eql({
label: 'with filters',
url: 'https://elastic.co',
filters: [
{ key: 'service.name', value: 'baz' },
{ key: 'transaction.type', value: 'qux' },
],
});
});
it('updates a custom link', async () => {
let { status, body } = await searchCustomLinks({
'service.name': 'baz',
'transaction.type': 'qux',
});
expect(status).to.equal(200);
await updateCustomLink(body[0].id, {
label: 'foo',
url: 'https://elastic.co?service.name={{service.name}}',
filters: [
{ key: 'service.name', value: 'quz' },
{ key: 'transaction.name', value: 'bar' },
],
});
({ status, body } = await searchCustomLinks({
'service.name': 'quz',
'transaction.name': 'bar',
}));
const { label, url, filters } = body[0];
expect(status).to.equal(200);
expect({ label, url, filters }).to.eql({
label: 'foo',
url: 'https://elastic.co?service.name={{service.name}}',
filters: [
{ key: 'service.name', value: 'quz' },
{ key: 'transaction.name', value: 'bar' },
],
});
});
it('deletes a custom link', async () => {
let { status, body } = await searchCustomLinks({
'service.name': 'quz',
'transaction.name': 'bar',
});
expect(status).to.equal(200);
await deleteCustomLink(body[0].id);
({ status, body } = await searchCustomLinks({
'service.name': 'quz',
'transaction.name': 'bar',
}));
expect(status).to.equal(200);
expect(body).to.eql([]);
});
});
}
15 changes: 15 additions & 0 deletions x-pack/test/api_integration/apis/apm/feature_controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,27 @@ export default function featureControlsTests({ getService }: FtrProviderContext)
log.error(JSON.stringify(res, null, 2));
},
},
{
req: {
url: `/api/apm/settings/custom_links`,
},
expectForbidden: expect404,
expectResponse: expect200,
},
{
req: {
url: `/api/apm/settings/custom_links/transaction`,
},
expectForbidden: expect404,
expectResponse: expect200,
},
];

const elasticsearchPrivileges = {
indices: [
{ names: ['apm-*'], privileges: ['read', 'view_index_metadata'] },
{ names: ['.apm-agent-configuration'], privileges: ['read', 'write', 'view_index_metadata'] },
{ names: ['.apm-custom-link'], privileges: ['read', 'write', 'view_index_metadata'] },
],
};

Expand Down
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/apm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export default function apmApiIntegrationTests({ loadTestFile }: FtrProviderCont
describe('APM specs', () => {
loadTestFile(require.resolve('./feature_controls'));
loadTestFile(require.resolve('./agent_configuration'));
loadTestFile(require.resolve('./custom_link'));
});
}

0 comments on commit c4e18a8

Please sign in to comment.