import expect from '@kbn/expect' ; import { DETECTION_ENGINE_RULES_URL } from '../../../../plugins/security_solution/common/constants' ; import { FtrProviderContext } from '../../common/ftr_provider_context' ; import { binaryToString , createRule , createSignalsIndex , deleteAllAlerts , deleteSignalsIndex , getSimpleRule , getSimpleRuleOutput , removeServerGeneratedProperties , } from '../../utils' ; export default ( { getService } ) : void => { const supertest = getService ( 'supertest' ) ; const log = getService ( 'log' ) ; describe ( 'export_rules' , ( ) => { describe ( 'exporting rules' , ( ) => { beforeEach ( async ( ) => { await createSignalsIndex ( supertest , log ) ; } ) ; afterEach ( async ( ) => { await deleteSignalsIndex ( supertest , log ) ; await deleteAllAlerts ( supertest , log ) ; } ) ; it ( 'should set the response content types to be expected' , async ( ) => { await createRule ( supertest , log , getSimpleRule ( ) ) ; await supertest . post ( `${ DETECTION_ENGINE_RULES_URL }/_export` ) . set ( 'kbn-xsrf' , 'true' ) . send ( ) . expect ( 200 ) . expect ( 'Content-Type' , 'application/ndjson' ) . expect ( 'Content-Disposition' , 'attachment; filename="export.ndjson"' ) ; } ) ; it ( 'should export a single rule with a rule_id' , async ( ) => { await createRule ( supertest , log , getSimpleRule ( ) ) ; const { body } = await supertest . post ( `${ DETECTION_ENGINE_RULES_URL }/_export` ) . set ( 'kbn-xsrf' , 'true' ) . send ( ) . expect ( 200 ) . parse ( binaryToString ) ; const bodySplitAndParsed = JSON . parse ( body . toString ( ) . split ( /\n/ ) [ 0 ] ) ; const bodyToTest = removeServerGeneratedProperties ( bodySplitAndParsed ) ; expect ( bodyToTest ) . to . eql ( getSimpleRuleOutput ( ) ) ; } ) ; it ( 'should export a exported count with a single rule_id' , async ( ) => { await createRule ( supertest , log , getSimpleRule ( ) ) ; const { body } = await supertest . post ( `${ DETECTION_ENGINE_RULES_URL }/_export` ) . set ( 'kbn-xsrf' , 'true' ) . send ( ) . expect ( 200 ) . parse ( binaryToString ) ; const bodySplitAndParsed = JSON . parse ( body . toString ( ) . split ( /\n/ ) [ 1 ] ) ; expect ( bodySplitAndParsed ) . to . eql ( { exported_exception_list_count : 0 , exported_exception_list_item_count : 0 , exported_count : 1 , exported_rules_count : 1 , missing_exception_list_item_count : 0 , missing_exception_list_items : [ ] , missing_exception_lists : [ ] , missing_exception_lists_count : 0 , missing_rules : [ ] , missing_rules_count : 0 , } ) ; } ) ; it ( 'should export exactly two rules given two rules' , async ( ) => { await createRule ( supertest , log , getSimpleRule ( 'rule-1' ) ) ; await createRule ( supertest , log , getSimpleRule ( 'rule-2' ) ) ; const { body } = await supertest . post ( `${ DETECTION_ENGINE_RULES_URL }/_export` ) . set ( 'kbn-xsrf' , 'true' ) . send ( ) . expect ( 200 ) . parse ( binaryToString ) ; const firstRuleParsed = JSON . parse ( body . toString ( ) . split ( /\n/ ) [ 0 ] ) ; const secondRuleParsed = JSON . parse ( body . toString ( ) . split ( /\n/ ) [ 1 ] ) ; const firstRule = removeServerGeneratedProperties ( firstRuleParsed ) ; const secondRule = removeServerGeneratedProperties ( secondRuleParsed ) ; expect ( [ firstRule , secondRule ] ) . to . eql ( [ getSimpleRuleOutput ( 'rule-2' ) , getSimpleRuleOutput ( 'rule-1' ) , ] ) ; } ) ; } ) ; } ) ; } ;