Skip to content

Commit

Permalink
Fixed failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Apr 16, 2020
1 parent 31384ce commit 420253c
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const registerProviderActionsRoute = (
eventLogService.registerProviderActions(provider, actions);
logger.info(`registered`);
} catch (e) {
response.badRequest({ body: e });
return response.badRequest({ body: e });
}
return response.ok({ body: {} });
}
Expand Down Expand Up @@ -117,16 +117,22 @@ export const getProviderActionsRoute = (
) => {
router.get(
{
path: `/api/log_event_fixture/getProviderActions`,
validate: {},
path: `/api/log_event_fixture/{provider}/getProviderActions`,
validate: {
params: (value: any, { ok }: RouteValidationResultFactory) => ok(value),
},
},
async function(
context: RequestHandlerContext,
req: KibanaRequest<any, any, any, any>,
res: KibanaResponseFactory
): Promise<IKibanaResponse<any>> {
const { provider } = req.params as { provider: string };

logger.info(`test if get all provider actions is registered`);
return res.ok({ body: { actions: eventLogService.getProviderActions() } });
return res.ok({
body: { actions: [...(eventLogService.getProviderActions().get(provider) ?? [])] },
});
}
);
};
Expand Down Expand Up @@ -220,3 +226,61 @@ export const isEventLogServiceLoggingEntriesRoute = (
}
);
};

export const startTimingEventLoggerRoute = (
router: IRouter,
eventLogService: IEventLogService,
logger: Logger
) => {
router.post(
{
path: '/api/log_event_fixture/startTimingEventLogger',
validate: {
body: value => ({ value }),
},
options: { authRequired: false },
},
(context, request, response) => {
const event: IValidatedEvent = request.body;
try {
logger.info(`started timing for event: ${event}`);

const eventLogger = eventLogService.getLogger(event);
eventLogger.startTiming(event);
logger.info(`registered`);
} catch (e) {
return response.badRequest({ body: e });
}
return response.ok({ body: {} });
}
);
};

export const stopTimingEventLoggerRoute = (
router: IRouter,
eventLogService: IEventLogService,
logger: Logger
) => {
router.post(
{
path: '/api/log_event_fixture/stopTimingEventLogger',
validate: {
body: value => ({ value }),
},
options: { authRequired: false },
},
(context, request, response) => {
const event: IValidatedEvent = request.body;
try {
logger.info(`started timing for event: ${event}`);

const eventLogger = eventLogService.getLogger(event);
eventLogger.stopTiming(event);
logger.info(`registered`);
} catch (e) {
return response.badRequest({ body: e });
}
return response.ok({ body: {} });
}
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
isIndexingEntriesRoute,
isEventLogServiceLoggingEntriesRoute,
isEventLogServiceEnabledRoute,
startTimingEventLoggerRoute,
stopTimingEventLoggerRoute,
} from './init_routes';

// this plugin's dependendencies
Expand Down Expand Up @@ -60,6 +62,8 @@ export class EventLogFixturePlugin
isIndexingEntriesRoute(router, eventLog, this.logger);
isEventLogServiceLoggingEntriesRoute(router, eventLog, this.logger);
isEventLogServiceEnabledRoute(router, eventLog, this.logger);
startTimingEventLoggerRoute(router, eventLog, this.logger);
stopTimingEventLoggerRoute(router, eventLog, this.logger);
}

public start() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import * as Rx from 'rxjs';
import { startWith, switchMap, take } from 'rxjs/operators';
import expect from '@kbn/expect/expect.js';
import { IEvent } from '../../../../plugins/event_log/server';
import { IEventLogger } from '../../../../plugins/event_log/server/types';
import { FtrProviderContext } from '../../ftr_provider_context';

export default function({ getService }: FtrProviderContext) {
const es = getService('legacyEs');
const supertest = getService('supertest');
const log = getService('log');
const config = getService('config');
const retry = getService('retry');

describe('Event Log service API', () => {
it('should check if it is enabled', async () => {
Expand Down Expand Up @@ -45,74 +45,97 @@ export default function({ getService }: FtrProviderContext) {
);
});

it('should return error message if provider is regostered', async () => {
await registerProviderActions('duplication', ['action1', 'action2']);
it('should be able to check if provider actions is registered', async () => {
const initResult = await isProviderActionRegistered('provider3', 'action1');

const result = await registerProviderActions('duplication', ['action3']);
expect(result.body).to.be.eql('');
if (!initResult.body.isProviderActionRegistered) {
await registerProviderActions('provider3', ['action1']);
}
const result1 = await isProviderActionRegistered('provider3', 'action1');
expect(result1.body.isProviderActionRegistered).to.be.eql(true);

const result = await isProviderActionRegistered('provider3', 'action2');
expect(result.body.isProviderActionRegistered).to.be.eql(false);
});

it('should allow to register provider actions and return all provider actions', async () => {
await registerProviderActions('provider1', ['action1', 'action2']);
it('should return error message if provider is registered', async () => {
const initResult = await isProviderActionRegistered('duplication', 'action1');

const providerActions = await getProviderActions();
expect(providerActions.body.actions).to.be.eql({ provider1: ['action1', 'action2'] });
if (!initResult.body.isProviderActionRegistered) {
await registerProviderActions('duplication', ['action1', 'action2']);
}

const result = await registerProviderActions('duplication', ['action1', 'action2']);
expect(result.badRequest).to.be.eql(true);
});

it('should allow to get event logger if it is registered', async () => {
await registerProviderActions('provider2', ['action1', 'action2']);
const eventLogger = await getEventLogger('provider2');
expect(eventLogger.body.eventLogger).not.be(null);
it('should allow to register provider actions and return all provider actions', async () => {
const initResult = await isProviderActionRegistered('provider1', 'action1');

if (!initResult.body.isProviderActionRegistered) {
await registerProviderActions('provider1', ['action1', 'action2']);
}

const eventLoggerNotExist = await getEventLogger('000');
expect(eventLoggerNotExist.body.eventLogger).to.be(null);
const providerActions = await getProviderActions('provider1');
expect(providerActions.body.actions).to.be.eql(['action1', 'action2']);
});

it('should be able to check if provider actions is registered', async () => {
await registerProviderActions('provider3', ['action1']);
const result = await isProviderActionRegistered('provider3', 'action2');
expect(result.body.isProviderActionRegistered).to.be.eql(false);
it('should allow to get event logger event log service', async () => {
const initResult = await isProviderActionRegistered('provider2', 'action1');

const result1 = await isProviderActionRegistered('provider3', 'action1');
expect(result1.body.isProviderActionRegistered).to.be.eql(true);
if (!initResult.body.isProviderActionRegistered) {
await registerProviderActions('provider2', ['action1', 'action2']);
}
const eventLogger = await getEventLogger('provider2');
expect(eventLogger.body.eventLogger.initialProperties).to.be.eql({
event: { provider: 'provider2' },
});
});

it('should allow write an event to index document if indexing entries is enabled', async () => {
await registerProviderActions('provider4', ['action1', 'action2']);
const eventLogger = (await getEventLogger('provider4')).body.eventLogger as IEventLogger;
const initResult = await isProviderActionRegistered('provider4', 'action1');

if (!initResult.body.isProviderActionRegistered) {
await registerProviderActions('provider4', ['action1', 'action2']);
}

const eventId = '1';
const event: IEvent = {
event: { action: 'action1', provider: 'provider4' },
kibana: { saved_objects: [{ type: 'action', id: eventId }] },
kibana: { saved_objects: [{ type: 'event_log_test', id: eventId }] },
};
eventLogger.startTiming(event);
await startTimingEventLogger(event);
setTimeout(() => log.debug('doing some action'), 500);
eventLogger.stopTiming(event);
eventLogger.logEvent(event);

const uri = `/api/event_log/provider4/${eventId}/_find`;
log.debug(`calling ${uri}`);
const result = await supertest
.get(uri)
.set('kbn-xsrf', 'foo')
.expect(200);
expect(result.body.data).to.be.eql([event]);
await stopTimingEventLogger(event);
await logTestEvent(eventId, event);

await retry.try(async () => {
const uri = `/api/event_log/event_log_test/${eventId}/_find`;
log.debug(`calling ${uri}`);
const result = await supertest
.get(uri)
.set('kbn-xsrf', 'foo')
.expect(200);
expect(result.body.data.length).to.be.eql(1);
});
});

it('should allow write an event to system logger if logging entries is enabled', async () => {
await registerProviderActions('provider5', ['test']);
const eventLogger = (await getEventLogger('provider5')).body.eventLogger as IEventLogger;
const initResult = await isProviderActionRegistered('provider5', 'test');

if (!initResult.body.isProviderActionRegistered) {
await registerProviderActions('provider5', ['test']);
}

const eventId = '1';
const event: IEvent = {
event: { action: 'action1', provider: 'provider5' },
kibana: { saved_objects: [{ type: 'action', id: eventId }] },
};
eventLogger.startTiming(event);
await startTimingEventLogger(event);
setTimeout(() => log.debug('doing some action'), 500);
eventLogger.stopTiming(event);
eventLogger.logEvent(event);
await stopTimingEventLogger(event);
await logTestEvent(eventId, event);

const promise = log
.getWritten$()
Expand All @@ -133,8 +156,7 @@ export default function({ getService }: FtrProviderContext) {
return await supertest
.post(`/api/log_event_fixture/${provider}/_registerProviderActions`)
.set('kbn-xsrf', 'xxx')
.send(actions)
.expect(200);
.send(actions);
}

async function isProviderActionRegistered(provider: string, action: string) {
Expand All @@ -145,10 +167,10 @@ export default function({ getService }: FtrProviderContext) {
.expect(200);
}

async function getProviderActions() {
log.debug(`getProviderActions`);
async function getProviderActions(provider: string) {
log.debug(`getProviderActions ${provider}`);
return await supertest
.get(`/api/log_event_fixture/getProviderActions`)
.get(`/api/log_event_fixture/${provider}/getProviderActions`)
.set('kbn-xsrf', 'xxx')
.expect(200);
}
Expand Down Expand Up @@ -184,4 +206,31 @@ export default function({ getService }: FtrProviderContext) {
.set('kbn-xsrf', 'foo')
.expect(200);
}

async function startTimingEventLogger(event: IEvent) {
log.debug(`startTimingEventLogger ${event}`);
return await supertest
.post(`/api/log_event_fixture/startTimingEventLogger`)
.set('kbn-xsrf', 'xxx')
.send(event)
.expect(200);
}

async function stopTimingEventLogger(event: IEvent) {
log.debug(`stopTimingEventLogger ${event}`);
return await supertest
.post(`/api/log_event_fixture/stopTimingEventLogger`)
.set('kbn-xsrf', 'xxx')
.send(event)
.expect(200);
}

async function logTestEvent(id: string, event: IEvent) {
log.debug(`Logging Event for Saved Object ${id}`);
return await supertest
.post(`/api/log_event_fixture/${id}/_log`)
.set('kbn-xsrf', 'foo')
.send(event)
.expect(200);
}
}

0 comments on commit 420253c

Please sign in to comment.