import expect from '@kbn/expect' ; import { UserAtSpaceScenarios } from '../../scenarios' ; import { checkAAD , getUrlPrefix , ObjectRemover } from '../../../common/lib' ; import { FtrProviderContext } from '../../../common/ftr_provider_context' ; export default function createActionTests ( { getService } ) { const supertest = getService ( 'supertest' ) ; const supertestWithoutAuth = getService ( 'supertestWithoutAuth' ) ; describe ( 'create' , ( ) => { const objectRemover = new ObjectRemover ( supertest ) ; after ( ( ) => objectRemover . removeAll ( ) ) ; for ( const scenario of UserAtSpaceScenarios ) { const { user , space } = scenario ; describe ( scenario . id , ( ) => { it ( 'should handle create action request appropriately' , async ( ) => { const response = await supertestWithoutAuth . post ( `${ getUrlPrefix ( space . id ) }/api/actions/connector` ) . auth ( user . username , user . password ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { name : 'My action' , connector_type_id : 'test.index-record' , config : { unencrypted : `This value shouldn't get encrypted` , } , secrets : { encrypted : 'This value should be encrypted' , } , } ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'global_read at space1' : case 'space_1_all_alerts_none_actions at space1' : case 'space_1_all at space2' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { statusCode : 403 , error : 'Forbidden' , message : 'Unauthorized to create a "test.index-record" action' , } ) ; break ; case 'superuser at space1' : case 'space_1_all at space1' : case 'space_1_all_with_restricted_fixture at space1' : expect ( response . statusCode ) . to . eql ( 200 ) ; objectRemover . add ( space . id , response . body . id , 'action' , 'actions' ) ; expect ( response . body ) . to . eql ( { id : response . body . id , is_preconfigured : false , is_missing_secrets : false , name : 'My action' , connector_type_id : 'test.index-record' , config : { unencrypted : `This value shouldn't get encrypted` , } , } ) ; expect ( typeof response . body . id ) . to . be ( 'string' ) ; await checkAAD ( { supertest , spaceId : space . id , type : 'action' , id : response . body . id , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( `should handle create action request appropriately when action type isn't registered` , async ( ) => { const response = await supertestWithoutAuth . post ( `${ getUrlPrefix ( space . id ) }/api/actions/connector` ) . set ( 'kbn-xsrf' , 'foo' ) . auth ( user . username , user . password ) . send ( { name : 'My action' , connector_type_id : 'test.unregistered-action-type' , config : { } , } ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'global_read at space1' : case 'space_1_all_alerts_none_actions at space1' : case 'space_1_all at space2' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { statusCode : 403 , error : 'Forbidden' , message : 'Unauthorized to create a "test.unregistered-action-type" action' , } ) ; break ; case 'superuser at space1' : case 'space_1_all at space1' : case 'space_1_all_with_restricted_fixture at space1' : expect ( response . statusCode ) . to . eql ( 400 ) ; expect ( response . body ) . to . eql ( { statusCode : 400 , error : 'Bad Request' , message : 'Action type "test.unregistered-action-type" is not registered.' , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( 'should handle create action request appropriately when payload is empty and invalid' , async ( ) => { const response = await supertestWithoutAuth . post ( `${ getUrlPrefix ( space . id ) }/api/actions/connector` ) . set ( 'kbn-xsrf' , 'foo' ) . auth ( user . username , user . password ) . send ( { } ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'global_read at space1' : case 'space_1_all_alerts_none_actions at space1' : case 'space_1_all at space2' : case 'superuser at space1' : case 'space_1_all at space1' : case 'space_1_all_with_restricted_fixture at space1' : expect ( response . statusCode ) . to . eql ( 400 ) ; expect ( response . body ) . to . eql ( { statusCode : 400 , error : 'Bad Request' , message : '[request body.name]: expected value of type [string] but got [undefined]' , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( `should handle create action request appropriately when config isn't valid` , async ( ) => { const response = await supertestWithoutAuth . post ( `${ getUrlPrefix ( space . id ) }/api/actions/connector` ) . set ( 'kbn-xsrf' , 'foo' ) . auth ( user . username , user . password ) . send ( { name : 'my name' , connector_type_id : 'test.index-record' , config : { unencrypted : 'my unencrypted text' , } , } ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'global_read at space1' : case 'space_1_all_alerts_none_actions at space1' : case 'space_1_all at space2' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { statusCode : 403 , error : 'Forbidden' , message : 'Unauthorized to create a "test.index-record" action' , } ) ; break ; case 'superuser at space1' : case 'space_1_all at space1' : case 'space_1_all_with_restricted_fixture at space1' : expect ( response . statusCode ) . to . eql ( 400 ) ; expect ( response . body ) . to . eql ( { statusCode : 400 , error : 'Bad Request' , message : 'error validating action type secrets: [encrypted]: expected value of type [string] but got [undefined]' , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( `should handle create action requests for action types that are not enabled` , async ( ) => { const response = await supertestWithoutAuth . post ( `${ getUrlPrefix ( space . id ) }/api/actions/connector` ) . set ( 'kbn-xsrf' , 'foo' ) . auth ( user . username , user . password ) . send ( { name : 'my name' , connector_type_id : 'test.not-enabled' , } ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'global_read at space1' : case 'space_1_all_alerts_none_actions at space1' : case 'space_1_all at space2' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { statusCode : 403 , error : 'Forbidden' , message : 'Unauthorized to create a "test.not-enabled" action' , } ) ; break ; case 'superuser at space1' : case 'space_1_all at space1' : case 'space_1_all_with_restricted_fixture at space1' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { statusCode : 403 , error : 'Forbidden' , message : 'action type "test.not-enabled" is not enabled in the Kibana config xpack.actions.enabledActionTypes' , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; } ) ; } } ) ; }