import expect from '@kbn/expect' ; import { Spaces } from '../../scenarios' ; import { FtrProviderContext } from '../../../common/ftr_provider_context' ; import { AlertUtils , checkAAD , getUrlPrefix , getTestAlertData , ObjectRemover , } from '../../../common/lib' ; /**
 * Eventhough security is disabled, this test checks the API behavior.
 */ export default function createUpdateApiKeyTests ( { getService } ) { const supertestWithoutAuth = getService ( 'supertestWithoutAuth' ) ; describe ( 'update_api_key' , ( ) => { const objectRemover = new ObjectRemover ( supertestWithoutAuth ) ; const alertUtils = new AlertUtils ( { space : Spaces . space1 , supertestWithoutAuth } ) ; after ( ( ) => objectRemover . removeAll ( ) ) ; it ( 'should handle update alert api key appropriately' , async ( ) => { const { body : createdAlert } = await supertestWithoutAuth . post ( `${ getUrlPrefix ( Spaces . space1 . id ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( getTestAlertData ( ) ) . expect ( 200 ) ; objectRemover . add ( Spaces . space1 . id , createdAlert . id , 'rule' , 'alerting' ) ; await alertUtils . updateApiKey ( createdAlert . id ) ; const { body : updatedAlert } = await supertestWithoutAuth . get ( `${ getUrlPrefix ( Spaces . space1 . id ) }/api/alerting/rule/${ createdAlert . id }` ) . set ( 'kbn-xsrf' , 'foo' ) . expect ( 200 ) ; expect ( updatedAlert . api_key_owner ) . to . eql ( null ) ; await checkAAD ( { supertest : supertestWithoutAuth , spaceId : Spaces . space1 . id , type : 'alert' , id : createdAlert . id , } ) ; } ) ; it ( `shouldn't update alert api key from another space` , async ( ) => { const { body : createdAlert } = await supertestWithoutAuth . post ( `${ getUrlPrefix ( Spaces . other . id ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( getTestAlertData ( ) ) . expect ( 200 ) ; objectRemover . add ( Spaces . other . id , createdAlert . id , 'rule' , 'alerting' ) ; await alertUtils . getUpdateApiKeyRequest ( createdAlert . id ) . expect ( 404 , { statusCode : 404 , error : 'Not Found' , message : `Saved object [alert/${ createdAlert . id }] not found` , } ) ; } ) ; describe ( 'legacy' , ( ) => { it ( 'should handle update alert api key appropriately' , async ( ) => { const { body : createdAlert } = await supertestWithoutAuth . post ( `${ getUrlPrefix ( Spaces . space1 . id ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( getTestAlertData ( ) ) . expect ( 200 ) ; objectRemover . add ( Spaces . space1 . id , createdAlert . id , 'rule' , 'alerting' ) ; await supertestWithoutAuth . post ( `${ getUrlPrefix ( Spaces . space1 . id ) }/api/alerts/alert/${ createdAlert . id }/_update_api_key` ) . set ( 'kbn-xsrf' , 'foo' ) . expect ( 204 ) ; const { body : updatedAlert } = await supertestWithoutAuth . get ( `${ getUrlPrefix ( Spaces . space1 . id ) }/api/alerting/rule/${ createdAlert . id }` ) . set ( 'kbn-xsrf' , 'foo' ) . expect ( 200 ) ; expect ( updatedAlert . api_key_owner ) . to . eql ( null ) ; await checkAAD ( { supertest : supertestWithoutAuth , spaceId : Spaces . space1 . id , type : 'alert' , id : createdAlert . id , } ) ; } ) ; } ) ; } ) ; }