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 updateActionTests ( { getService } ) { const supertest = getService ( 'supertest' ) ; const supertestWithoutAuth = getService ( 'supertestWithoutAuth' ) ; describe ( 'update' , ( ) => { const objectRemover = new ObjectRemover ( supertest ) ; after ( ( ) => objectRemover . removeAll ( ) ) ; for ( const scenario of UserAtSpaceScenarios ) { const { user , space } = scenario ; describe ( scenario . id , ( ) => { it ( 'should handle update action request appropriately' , async ( ) => { const { body : createdAction } = await supertest . post ( `${ getUrlPrefix ( space . id ) }/api/actions/connector` ) . 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' , } , } ) . expect ( 200 ) ; objectRemover . add ( space . id , createdAction . id , 'action' , 'actions' ) ; const response = await supertestWithoutAuth . put ( `${ getUrlPrefix ( space . id ) }/api/actions/connector/${ createdAction . id }` ) . auth ( user . username , user . password ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { name : 'My action updated' , 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 'space_1_all_alerts_none_actions at space1' : case 'space_1_all at space2' : case 'global_read at space1' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { statusCode : 403 , error : 'Forbidden' , message : 'Unauthorized to update actions' , } ) ; 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 ) ; expect ( response . body ) . to . eql ( { id : createdAction . id , is_preconfigured : false , connector_type_id : 'test.index-record' , is_missing_secrets : false , name : 'My action updated' , config : { unencrypted : `This value shouldn't get encrypted` , } , } ) ; await checkAAD ( { supertest , spaceId : space . id , type : 'action' , id : createdAction . id , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( `shouldn't update action from another space` , async ( ) => { const { body : createdAction } = await supertest . post ( `${ getUrlPrefix ( space . id ) }/api/actions/connector` ) . 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' , } , } ) . expect ( 200 ) ; objectRemover . add ( space . id , createdAction . id , 'action' , 'actions' ) ; const response = await supertestWithoutAuth . put ( `${ getUrlPrefix ( 'other' ) }/api/actions/connector/${ createdAction . id }` ) . auth ( user . username , user . password ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { name : 'My action updated' , 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 'space_1_all_alerts_none_actions at space1' : case 'space_1_all at space2' : case 'global_read 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 : 'Unauthorized to update actions' , } ) ; break ; case 'superuser at space1' : expect ( response . statusCode ) . to . eql ( 404 ) ; expect ( response . body ) . to . eql ( { statusCode : 404 , error : 'Not Found' , message : `Saved object [action/${ createdAction . id }] not found` , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( 'should handle update action request appropriately when passing a null config' , async ( ) => { const response = await supertestWithoutAuth . put ( `${ getUrlPrefix ( space . id ) }/api/actions/connector/1` ) . set ( 'kbn-xsrf' , 'foo' ) . auth ( user . username , user . password ) . send ( { name : 'My action updated' , config : null , } ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'space_1_all_alerts_none_actions at space1' : case 'space_1_all at space2' : case 'global_read at space1' : 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.config]: expected value of type [object] but got [null]' , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( `should handle update action request appropriately when action doesn't exist` , async ( ) => { const response = await supertestWithoutAuth . put ( `${ getUrlPrefix ( space . id ) }/api/actions/connector/1` ) . set ( 'kbn-xsrf' , 'foo' ) . auth ( user . username , user . password ) . send ( { name : 'My action updated' , 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 'space_1_all_alerts_none_actions at space1' : case 'space_1_all at space2' : case 'global_read at space1' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { statusCode : 403 , error : 'Forbidden' , message : 'Unauthorized to update actions' , } ) ; 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 ( 404 ) ; expect ( response . body ) . to . eql ( { statusCode : 404 , error : 'Not Found' , message : 'Saved object [action/1] not found' , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( 'should handle update action request appropriately when payload is empty and invalid' , async ( ) => { const response = await supertestWithoutAuth . put ( `${ getUrlPrefix ( space . id ) }/api/actions/connector/1` ) . set ( 'kbn-xsrf' , 'foo' ) . auth ( user . username , user . password ) . send ( { } ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'space_1_all_alerts_none_actions at space1' : case 'space_1_all at space2' : case 'global_read at space1' : 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 update action request appropriately when secrets are not valid' , async ( ) => { const { body : createdAction } = await supertest . post ( `${ getUrlPrefix ( space . id ) }/api/actions/connector` ) . 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' , } , } ) . expect ( 200 ) ; objectRemover . add ( space . id , createdAction . id , 'action' , 'actions' ) ; const response = await supertestWithoutAuth . put ( `${ getUrlPrefix ( space . id ) }/api/actions/connector/${ createdAction . id }` ) . set ( 'kbn-xsrf' , 'foo' ) . auth ( user . username , user . password ) . send ( { name : 'My action updated' , config : { unencrypted : `This value shouldn't get encrypted` , } , secrets : { encrypted : 42 , } , } ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'space_1_all_alerts_none_actions at space1' : case 'space_1_all at space2' : case 'global_read at space1' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { statusCode : 403 , error : 'Forbidden' , message : 'Unauthorized to update actions' , } ) ; 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 [number]' , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( `shouldn't update action from preconfigured list` , async ( ) => { const response = await supertestWithoutAuth . put ( `${ getUrlPrefix ( space . id ) }/api/actions/connector/custom-system-abc-connector` ) . auth ( user . username , user . password ) . set ( 'kbn-xsrf' , 'foo' ) . send ( { name : 'My action updated' , 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 'space_1_all_alerts_none_actions at space1' : case 'space_1_all at space2' : case 'global_read at space1' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { statusCode : 403 , error : 'Forbidden' , message : 'Unauthorized to update actions' , } ) ; break ; case 'superuser at space1' : case 'space_1_all at space1' : case 'space_1_all_with_restricted_fixture at space1' : expect ( response . body ) . to . eql ( { statusCode : 400 , error : 'Bad Request' , message : `Preconfigured action custom-system-abc-connector is not allowed to update.` , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; } ) ; } } ) ; }