import expect from '@kbn/expect' ; import { PrePackagedRulesAndTimelinesSchema } from '../../../../plugins/security_solution/common/detection_engine/schemas/response' ; import { DETECTION_ENGINE_PREPACKAGED_URL } from '../../../../plugins/security_solution/common/constants' ; import { FtrProviderContext } from '../../common/ftr_provider_context' ; import { createSignalsIndex , deleteAllAlerts , deleteAllTimelines , deleteSignalsIndex , installPrePackagedRules , waitFor , } from '../../utils' ; export default ( { getService } ) : void => { const es = getService ( 'es' ) ; const supertest = getService ( 'supertest' ) ; const log = getService ( 'log' ) ; describe ( 'add_prepackaged_rules' , ( ) => { describe ( 'creating prepackaged rules' , ( ) => { beforeEach ( async ( ) => { await createSignalsIndex ( supertest , log ) ; } ) ; afterEach ( async ( ) => { await deleteSignalsIndex ( supertest , log ) ; await deleteAllAlerts ( supertest , log ) ; await deleteAllTimelines ( es ) ; } ) ; it ( 'should create the prepackaged rules and return a count greater than zero, rules_updated to be zero, and contain the correct keys' , async ( ) => { let responseBody ; await waitFor ( async ( ) => { const { body , status } = await supertest . put ( DETECTION_ENGINE_PREPACKAGED_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( ) ; if ( status === 200 ) { responseBody = body ; } return status === 200 ; } , DETECTION_ENGINE_PREPACKAGED_URL , log ) ; const prepackagedRules = responseBody as PrePackagedRulesAndTimelinesSchema ; expect ( prepackagedRules . rules_installed ) . to . be . greaterThan ( 0 ) ; expect ( prepackagedRules . rules_updated ) . to . eql ( 0 ) ; expect ( Object . keys ( prepackagedRules ) ) . to . eql ( [ 'rules_installed' , 'rules_updated' , 'timelines_installed' , 'timelines_updated' , ] ) ; } ) ; it ( 'should be possible to call the API twice and the second time the number of rules installed should be zero as well as timeline' , async ( ) => { await installPrePackagedRules ( supertest , log ) ; await waitFor ( async ( ) => { const { body } = await supertest . get ( `${ DETECTION_ENGINE_PREPACKAGED_URL }/_status` ) . set ( 'kbn-xsrf' , 'true' ) . expect ( 200 ) ; return body . rules_not_installed === 0 ; } , `${ DETECTION_ENGINE_PREPACKAGED_URL }/_status` , log ) ; let responseBody ; await waitFor ( async ( ) => { const { body , status } = await supertest . put ( DETECTION_ENGINE_PREPACKAGED_URL ) . set ( 'kbn-xsrf' , 'true' ) . send ( ) ; if ( status === 200 ) { responseBody = body ; } return status === 200 ; } , DETECTION_ENGINE_PREPACKAGED_URL , log ) ; const prepackagedRules = responseBody as PrePackagedRulesAndTimelinesSchema ; expect ( prepackagedRules . rules_installed ) . to . eql ( 0 ) ; expect ( prepackagedRules . timelines_installed ) . to . eql ( 0 ) ; } ) ; } ) ; } ) ; } ;