Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiqueclarke committed Nov 21, 2022
1 parent 2648dd1 commit 5ebdfed
Showing 1 changed file with 139 additions and 11 deletions.
150 changes: 139 additions & 11 deletions x-pack/test/api_integration/apis/synthetics/get_monitor_overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,39 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import uuid from 'uuid';
import { SimpleSavedObject } from '@kbn/core/public';
import { SyntheticsMonitor, MonitorFields } from '@kbn/synthetics-plugin/common/runtime_types';
import {
ConfigKey,
SyntheticsMonitor,
MonitorFields,
} from '@kbn/synthetics-plugin/common/runtime_types';
import { SYNTHETICS_API_URLS, API_URLS } from '@kbn/synthetics-plugin/common/constants';
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';
import { getFixtureJson } from '../uptime/rest/helper/get_fixture_json';

export default function ({ getService }: FtrProviderContext) {
// Failing: See https://github.com/elastic/kibana/issues/145270
describe.skip('GetMonitorsOverview', function () {
describe('GetMonitorsOverview', function () {
this.tags('skipCloud');

const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const security = getService('security');

const username = 'admin';
const roleName = `synthetics_admin`;
const password = `${username}-password`;
const SPACE_ID = `test-space-${uuid.v4()}`;
const SPACE_NAME = `test-space-name ${uuid.v4()}`;

let _monitors: MonitorFields[];
let monitors: MonitorFields[];

const deleteMonitor = async (id: string) => {
try {
await supertest
.delete(`${API_URLS.SYNTHETICS_MONITORS}/${id}`)
.delete(`/s/${SPACE_ID}${API_URLS.SYNTHETICS_MONITORS}/${id}`)
.set('kbn-xsrf', 'true')
.expect(200);
} catch (e) {
Expand All @@ -36,7 +47,7 @@ export default function ({ getService }: FtrProviderContext) {

const saveMonitor = async (monitor: MonitorFields) => {
const res = await supertest
.post(API_URLS.SYNTHETICS_MONITORS)
.post(`/s/${SPACE_ID}${API_URLS.SYNTHETICS_MONITORS}`)
.set('kbn-xsrf', 'true')
.send(monitor);

Expand All @@ -45,8 +56,24 @@ export default function ({ getService }: FtrProviderContext) {

before(async () => {
await supertest.post(API_URLS.SYNTHETICS_ENABLEMENT).set('kbn-xsrf', 'true').expect(200);
await kibanaServer.spaces.create({ id: SPACE_ID, name: SPACE_NAME });
await security.role.create(roleName, {
kibana: [
{
feature: {
uptime: ['all'],
},
spaces: ['*'],
},
],
});
await security.user.create(username, {
password,
roles: [roleName],
full_name: 'a kibana user',
});
const { body } = await supertest
.get(API_URLS.SYNTHETICS_MONITORS)
.get(`/s/${SPACE_ID}${API_URLS.SYNTHETICS_MONITORS}`)
.set('kbn-xsrf', 'true')
.expect(200);
await Promise.all([
Expand All @@ -68,6 +95,12 @@ export default function ({ getService }: FtrProviderContext) {
}
});

after(async () => {
await kibanaServer.spaces.delete(SPACE_ID);
await security.user.delete(username);
await security.role.delete(roleName);
});

describe('returns total number of monitor combinations', () => {
it('returns the correct response', async () => {
let savedMonitors: SimpleSavedObject[] = [];
Expand All @@ -76,7 +109,7 @@ export default function ({ getService }: FtrProviderContext) {
savedMonitors = savedResponse;

const apiResponse = await supertest.get(
SYNTHETICS_API_URLS.SYNTHETICS_OVERVIEW + '?perPage=20'
`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_OVERVIEW}`
);

expect(apiResponse.body.total).eql(monitors.length * 2);
Expand All @@ -99,9 +132,11 @@ export default function ({ getService }: FtrProviderContext) {
const savedResponse = await Promise.all(monitors.map(saveMonitor));
savedMonitors = savedResponse;

const apiResponse = await supertest.get(SYNTHETICS_API_URLS.SYNTHETICS_OVERVIEW).query({
query: '19',
});
const apiResponse = await supertest
.get(`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_OVERVIEW}`)
.query({
query: '19',
});

expect(apiResponse.body.total).eql(2);
expect(apiResponse.body.allMonitorIds.sort()).eql(
Expand All @@ -119,5 +154,98 @@ export default function ({ getService }: FtrProviderContext) {
}
});
});

describe('Overview Item', () => {
it('returns the correct response', async () => {
let savedMonitors: Array<SimpleSavedObject<SyntheticsMonitor>> = [];
const customHeartbeatId = 'example_custom_heartbeat_id';
try {
const savedResponse = await Promise.all(
[
monitors[0],
{ ...monitors[1], custom_heartbeat_id: 'example_custom_heartbeat_id' },
].map(saveMonitor)
);
savedMonitors = savedResponse;

const apiResponse = await supertest.get(
`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_OVERVIEW}`
);
expect(apiResponse.body.monitors).eql([
{
id: savedMonitors[0].attributes[ConfigKey.MONITOR_QUERY_ID],
configId: savedMonitors[0].id,
name: 'test-monitor-name 0',
location: {
id: 'eu-west-01',
label: 'Europe West',
geo: {
lat: 33.2343132435,
lon: 73.2342343434,
},
url: 'https://example-url.com',
isServiceManaged: true,
},
isEnabled: true,
},
{
id: savedMonitors[0].attributes[ConfigKey.MONITOR_QUERY_ID],
configId: savedMonitors[0].id,
name: 'test-monitor-name 0',
location: {
id: 'eu-west-02',
label: 'Europe West',
geo: {
lat: 33.2343132435,
lon: 73.2342343434,
},
url: 'https://example-url.com',
isServiceManaged: true,
},
isEnabled: true,
},
{
id: savedMonitors[1].attributes[ConfigKey.MONITOR_QUERY_ID],
configId: savedMonitors[1].id,
name: 'test-monitor-name 1',
location: {
id: 'eu-west-01',
label: 'Europe West',
geo: {
lat: 33.2343132435,
lon: 73.2342343434,
},
url: 'https://example-url.com',
isServiceManaged: true,
},
isEnabled: true,
},
{
id: savedMonitors[1].attributes[ConfigKey.MONITOR_QUERY_ID],
configId: savedMonitors[1].id,
name: 'test-monitor-name 1',
location: {
id: 'eu-west-02',
label: 'Europe West',
geo: {
lat: 33.2343132435,
lon: 73.2342343434,
},
url: 'https://example-url.com',
isServiceManaged: true,
},
isEnabled: true,
},
]);
expect(savedMonitors[1].attributes[ConfigKey.MONITOR_QUERY_ID]).eql(customHeartbeatId);
} finally {
await Promise.all(
savedMonitors.map((monitor) => {
return deleteMonitor(monitor.id);
})
);
}
});
});
});
}

0 comments on commit 5ebdfed

Please sign in to comment.