import expect from '@kbn/expect' ; import { AlertExecutionStatusErrorReasons } from '../../../../../plugins/alerting/common' ; import { Spaces } from '../../scenarios' ; import { getUrlPrefix , getTestAlertData , ObjectRemover } from '../../../common/lib' ; import { FtrProviderContext } from '../../../common/ftr_provider_context' ; export default function executionStatusAlertTests ( { getService } ) { const retry = getService ( 'retry' ) ; const supertest = getService ( 'supertest' ) ; const spaceId = Spaces [ 0 ] . id ; describe ( 'executionStatus' , ( ) => { const objectRemover = new ObjectRemover ( supertest ) ; after ( async ( ) => await objectRemover . removeAll ( ) ) ; it ( 'should eventually have error reason "decrypt" when appropriate' , async ( ) => { const response = await supertest . post ( `${ getUrlPrefix ( spaceId ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( getTestAlertData ( { rule_type_id : 'test.noop' , schedule : { interval : '1s' } , } ) ) ; expect ( response . status ) . to . eql ( 200 ) ; const alertId = response . body . id ; objectRemover . add ( spaceId , alertId , 'rule' , 'alerting' ) ; let executionStatus = await waitForStatus ( alertId , new Set ( [ 'ok' ] ) , 10000 ) ; await retry . try ( async ( ) => { await supertest . put ( `${ getUrlPrefix ( spaceId ) }/api/alerts_fixture/saved_object/alert/${ alertId }` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { attributes : { name : 'bar' , } , } ) . expect ( 200 ) ; } ) ; executionStatus = await waitForStatus ( alertId , new Set ( [ 'error' ] ) ) ; expect ( executionStatus . error ) . to . be . ok ( ) ; expect ( executionStatus . error . reason ) . to . be ( AlertExecutionStatusErrorReasons . Decrypt ) ; expect ( executionStatus . error . message ) . to . be ( 'Unable to decrypt attribute "apiKey"' ) ; } ) ; } ) ; const WaitForStatusIncrement = 500 ; async function waitForStatus ( id , statuses , waitMillis = 10000 ) { if ( waitMillis < 0 ) { expect ( ) . fail ( `waiting for alert ${ id } statuses ${ Array . from ( statuses ) } timed out` ) ; } const response = await supertest . get ( `${ getUrlPrefix ( spaceId ) }/api/alerting/rule/${ id }` ) ; expect ( response . status ) . to . eql ( 200 ) ; const { status } = response . body . execution_status ; if ( statuses . has ( status ) ) return response . body . execution_status ; console . log ( `waitForStatus(${ Array . from ( statuses ) }): got ${ JSON . stringify ( response . body . execution_status ) }, retrying` ) ; await delay ( WaitForStatusIncrement ) ; return await waitForStatus ( id , statuses , waitMillis - WaitForStatusIncrement ) ; } } async function delay ( millis ) { await new Promise ( ( resolve ) => setTimeout ( resolve , millis ) ) ; }