import { KibanaRequest } from 'src/core/server' ; import { EventLogClient } from './event_log_client' ; import { contextMock } from './es/context.mock' ; import { merge } from 'lodash' ; import moment from 'moment' ; describe ( 'EventLogStart' , ( ) => { describe ( 'findEventsBySavedObjectIds' , ( ) => { test ( 'verifies that the user can access the specified saved object' , async ( ) => { const esContext = contextMock . create ( ) ; const savedObjectGetter = jest . fn ( ) ; const eventLogClient = new EventLogClient ( { esContext , savedObjectGetter , request : FakeRequest ( ) , } ) ; savedObjectGetter . mockResolvedValueOnce ( { id : 'saved-object-id' , type : 'saved-object-type' , attributes : { } , references : [ ] , } ) ; await eventLogClient . findEventsBySavedObjectIds ( 'saved-object-type' , [ 'saved-object-id' ] ) ; expect ( savedObjectGetter ) . toHaveBeenCalledWith ( 'saved-object-type' , [ 'saved-object-id' ] ) ; } ) ; test ( 'throws when the user doesnt have permission to access the specified saved object' , async ( ) => { const esContext = contextMock . create ( ) ; const savedObjectGetter = jest . fn ( ) ; const eventLogClient = new EventLogClient ( { esContext , savedObjectGetter , request : FakeRequest ( ) , } ) ; savedObjectGetter . mockRejectedValue ( new Error ( 'Fail' ) ) ; expect ( eventLogClient . findEventsBySavedObjectIds ( 'saved-object-type' , [ 'saved-object-id' ] ) ) . rejects . toMatchInlineSnapshot ( `[Error: Fail]` ) ; } ) ; test ( 'fetches all event that reference the saved object' , async ( ) => { const esContext = contextMock . create ( ) ; const savedObjectGetter = jest . fn ( ) ; const eventLogClient = new EventLogClient ( { esContext , savedObjectGetter , request : FakeRequest ( ) , } ) ; savedObjectGetter . mockResolvedValueOnce ( { id : 'saved-object-id' , type : 'saved-object-type' , attributes : { } , references : [ ] , } ) ; const expectedEvents = [ fakeEvent ( { kibana : { saved_objects : [ { id : 'saved-object-id' , type : 'saved-object-type' , } , { type : 'action' , id : '1' , } , ] , } , } ) , fakeEvent ( { kibana : { saved_objects : [ { id : 'saved-object-id' , type : 'saved-object-type' , } , { type : 'action' , id : '2' , } , ] , } , } ) , ] ; const result = { page : 0 , per_page : 10 , total : expectedEvents . length , data : expectedEvents , } ; esContext . esAdapter . queryEventsBySavedObjects . mockResolvedValue ( result ) ; expect ( await eventLogClient . findEventsBySavedObjectIds ( 'saved-object-type' , [ 'saved-object-id' ] , undefined , [ 'legacy-id' ] ) ) . toEqual ( result ) ; expect ( esContext . esAdapter . queryEventsBySavedObjects ) . toHaveBeenCalledWith ( { index : esContext . esNames . indexPattern , namespace : undefined , type : 'saved-object-type' , ids : [ 'saved-object-id' ] , findOptions : { page : 1 , per_page : 10 , sort_field : '@timestamp' , sort_order : 'asc' , } , legacyIds : [ 'legacy-id' ] , } ) ; } ) ; test ( 'fetches all events in time frame that reference the saved object' , async ( ) => { const esContext = contextMock . create ( ) ; const savedObjectGetter = jest . fn ( ) ; const eventLogClient = new EventLogClient ( { esContext , savedObjectGetter , request : FakeRequest ( ) , } ) ; savedObjectGetter . mockResolvedValueOnce ( { id : 'saved-object-id' , type : 'saved-object-type' , attributes : { } , references : [ ] , } ) ; const expectedEvents = [ fakeEvent ( { kibana : { saved_objects : [ { id : 'saved-object-id' , type : 'saved-object-type' , } , { type : 'action' , id : '1' , } , ] , } , } ) , fakeEvent ( { kibana : { saved_objects : [ { id : 'saved-object-id' , type : 'saved-object-type' , } , { type : 'action' , id : '2' , } , ] , } , } ) , ] ; const result = { page : 0 , per_page : 10 , total : expectedEvents . length , data : expectedEvents , } ; esContext . esAdapter . queryEventsBySavedObjects . mockResolvedValue ( result ) ; const start = moment ( ) . subtract ( 1 , 'days' ) . toISOString ( ) ; const end = moment ( ) . add ( 1 , 'days' ) . toISOString ( ) ; expect ( await eventLogClient . findEventsBySavedObjectIds ( 'saved-object-type' , [ 'saved-object-id' ] , { start , end , } , [ 'legacy-id' ] ) ) . toEqual ( result ) ; expect ( esContext . esAdapter . queryEventsBySavedObjects ) . toHaveBeenCalledWith ( { index : esContext . esNames . indexPattern , namespace : undefined , type : 'saved-object-type' , ids : [ 'saved-object-id' ] , findOptions : { page : 1 , per_page : 10 , sort_field : '@timestamp' , sort_order : 'asc' , start , end , } , legacyIds : [ 'legacy-id' ] , } ) ; } ) ; test ( 'validates that the start date is valid' , async ( ) => { const esContext = contextMock . create ( ) ; const savedObjectGetter = jest . fn ( ) ; const eventLogClient = new EventLogClient ( { esContext , savedObjectGetter , request : FakeRequest ( ) , } ) ; savedObjectGetter . mockResolvedValueOnce ( { id : 'saved-object-id' , type : 'saved-object-type' , attributes : { } , references : [ ] , } ) ; esContext . esAdapter . queryEventsBySavedObjects . mockResolvedValue ( { page : 0 , per_page : 0 , total : 0 , data : [ ] , } ) ; expect ( eventLogClient . findEventsBySavedObjectIds ( 'saved-object-type' , [ 'saved-object-id' ] , { start : 'not a date string' , } ) ) . rejects . toMatchInlineSnapshot ( `[Error: [start]: Invalid Date]` ) ; } ) ; test ( 'validates that the end date is valid' , async ( ) => { const esContext = contextMock . create ( ) ; const savedObjectGetter = jest . fn ( ) ; const eventLogClient = new EventLogClient ( { esContext , savedObjectGetter , request : FakeRequest ( ) , } ) ; savedObjectGetter . mockResolvedValueOnce ( { id : 'saved-object-id' , type : 'saved-object-type' , attributes : { } , references : [ ] , } ) ; esContext . esAdapter . queryEventsBySavedObjects . mockResolvedValue ( { page : 0 , per_page : 0 , total : 0 , data : [ ] , } ) ; expect ( eventLogClient . findEventsBySavedObjectIds ( 'saved-object-type' , [ 'saved-object-id' ] , { end : 'not a date string' , } ) ) . rejects . toMatchInlineSnapshot ( `[Error: [end]: Invalid Date]` ) ; } ) ; } ) ; } ) ; function fakeEvent ( overrides = { } ) { return merge ( { event : { provider : 'actions' , action : 'execute' , start : '2020-03-30T14:55:47.054Z' , end : '2020-03-30T14:55:47.055Z' , duration : 1000000 , } , kibana : { namespace : 'default' , saved_objects : [ { type : 'action' , id : '968f1b82-0414-4a10-becc-56b6473e4a29' , } , ] , server_uuid : '5b2de169-2785-441b-ae8c-186a1936b17d' , } , message : 'action executed: .server-log:968f1b82-0414-4a10-becc-56b6473e4a29: logger' , '@timestamp' : '2020-03-30T14:55:47.055Z' , ecs : { version : '1.3.1' , } , } , overrides ) ; } function FakeRequest ( ) { const savedObjectGetter = jest . fn ( ) ; return { headers : { } , getBasePath : ( ) => '' , path : '/' , route : { settings : { } } , url : { href : '/' , } , raw : { req : { url : '/' , } , } , getSavedObjectsClient : ( ) => savedObjectGetter , } as unknown as KibanaRequest ; }