import expect from '@kbn/expect' ; import { UserAtSpaceScenarios } from '../../scenarios' ; import { getUrlPrefix , getTestAlertData , ObjectRemover , getConsumerUnauthorizedErrorMessage , getProducerUnauthorizedErrorMessage , } from '../../../common/lib' ; import { FtrProviderContext } from '../../../common/ftr_provider_context' ; export default function createGetTests ( { getService } ) { const supertest = getService ( 'supertest' ) ; const supertestWithoutAuth = getService ( 'supertestWithoutAuth' ) ; describe ( 'get' , ( ) => { const objectRemover = new ObjectRemover ( supertest ) ; afterEach ( ( ) => objectRemover . removeAll ( ) ) ; for ( const scenario of UserAtSpaceScenarios ) { const { user , space } = scenario ; describe ( scenario . id , ( ) => { it ( 'should handle get alert request appropriately' , async ( ) => { const { body : createdAlert } = await supertest . post ( `${ getUrlPrefix ( space . id ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( getTestAlertData ( ) ) . expect ( 200 ) ; objectRemover . add ( space . id , createdAlert . id , 'rule' , 'alerting' ) ; const response = await supertestWithoutAuth . get ( `${ getUrlPrefix ( space . id ) }/api/alerting/rule/${ createdAlert . id }` ) . auth ( user . username , user . password ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'space_1_all at space2' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { error : 'Forbidden' , message : getConsumerUnauthorizedErrorMessage ( 'get' , 'test.noop' , 'alertsFixture' ) , statusCode : 403 , } ) ; break ; case 'global_read at space1' : case 'superuser at space1' : case 'space_1_all at space1' : case 'space_1_all_alerts_none_actions at space1' : case 'space_1_all_with_restricted_fixture at space1' : expect ( response . statusCode ) . to . eql ( 200 ) ; expect ( response . body ) . to . eql ( { id : createdAlert . id , name : 'abc' , tags : [ 'foo' ] , rule_type_id : 'test.noop' , consumer : 'alertsFixture' , schedule : { interval : '1m' } , enabled : true , actions : [ ] , params : { } , created_by : 'elastic' , scheduled_task_id : response . body . scheduled_task_id , updated_at : response . body . updated_at , created_at : response . body . created_at , throttle : '1m' , notify_when : 'onThrottleInterval' , updated_by : 'elastic' , api_key_owner : 'elastic' , mute_all : false , muted_alert_ids : [ ] , execution_status : response . body . execution_status , } ) ; expect ( Date . parse ( response . body . created_at ) ) . to . be . greaterThan ( 0 ) ; expect ( Date . parse ( response . body . updated_at ) ) . to . be . greaterThan ( 0 ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( 'should handle get alert request appropriately when consumer is the same as producer' , async ( ) => { const { body : createdAlert } = await supertest . post ( `${ getUrlPrefix ( space . id ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( getTestAlertData ( { rule_type_id : 'test.restricted-noop' , consumer : 'alertsRestrictedFixture' , } ) ) . expect ( 200 ) ; objectRemover . add ( space . id , createdAlert . id , 'rule' , 'alerting' ) ; const response = await supertestWithoutAuth . get ( `${ getUrlPrefix ( space . id ) }/api/alerting/rule/${ createdAlert . id }` ) . auth ( user . username , user . password ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'space_1_all at space2' : case 'space_1_all at space1' : case 'space_1_all_alerts_none_actions at space1' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { error : 'Forbidden' , message : getConsumerUnauthorizedErrorMessage ( 'get' , 'test.restricted-noop' , 'alertsRestrictedFixture' ) , statusCode : 403 , } ) ; break ; case 'global_read at space1' : case 'superuser at space1' : case 'space_1_all_with_restricted_fixture at space1' : expect ( response . statusCode ) . to . eql ( 200 ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( 'should handle get alert request appropriately when consumer is not the producer' , async ( ) => { const { body : createdAlert } = await supertest . post ( `${ getUrlPrefix ( space . id ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( getTestAlertData ( { rule_type_id : 'test.unrestricted-noop' , consumer : 'alertsFixture' , } ) ) . expect ( 200 ) ; objectRemover . add ( space . id , createdAlert . id , 'rule' , 'alerting' ) ; const response = await supertestWithoutAuth . get ( `${ getUrlPrefix ( space . id ) }/api/alerting/rule/${ createdAlert . id }` ) . auth ( user . username , user . password ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'space_1_all at space2' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { error : 'Forbidden' , message : getConsumerUnauthorizedErrorMessage ( 'get' , 'test.unrestricted-noop' , 'alertsFixture' ) , statusCode : 403 , } ) ; break ; case 'space_1_all at space1' : case 'space_1_all_alerts_none_actions at space1' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { error : 'Forbidden' , message : getProducerUnauthorizedErrorMessage ( 'get' , 'test.unrestricted-noop' , 'alertsRestrictedFixture' ) , statusCode : 403 , } ) ; break ; case 'superuser at space1' : case 'global_read at space1' : case 'space_1_all_with_restricted_fixture at space1' : expect ( response . statusCode ) . to . eql ( 200 ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( 'should handle get alert request appropriately when consumer is "alerts"' , async ( ) => { const { body : createdAlert } = await supertest . post ( `${ getUrlPrefix ( space . id ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( getTestAlertData ( { rule_type_id : 'test.restricted-noop' , consumer : 'alerts' , } ) ) . expect ( 200 ) ; objectRemover . add ( space . id , createdAlert . id , 'rule' , 'alerting' ) ; const response = await supertestWithoutAuth . get ( `${ getUrlPrefix ( space . id ) }/api/alerting/rule/${ createdAlert . id }` ) . auth ( user . username , user . password ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'space_1_all at space2' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { error : 'Forbidden' , message : getConsumerUnauthorizedErrorMessage ( 'get' , 'test.restricted-noop' , 'alerts' ) , statusCode : 403 , } ) ; break ; case 'space_1_all at space1' : case 'space_1_all_alerts_none_actions at space1' : expect ( response . statusCode ) . to . eql ( 403 ) ; expect ( response . body ) . to . eql ( { error : 'Forbidden' , message : getProducerUnauthorizedErrorMessage ( 'get' , 'test.restricted-noop' , 'alertsRestrictedFixture' ) , statusCode : 403 , } ) ; break ; case 'global_read at space1' : case 'superuser at space1' : case 'space_1_all_with_restricted_fixture at space1' : expect ( response . statusCode ) . to . eql ( 200 ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( `shouldn't get alert from another space` , async ( ) => { const { body : createdAlert } = await supertest . post ( `${ getUrlPrefix ( space . id ) }/api/alerting/rule` ) . set ( 'kbn-xsrf' , 'foo' ) . send ( getTestAlertData ( ) ) . expect ( 200 ) ; objectRemover . add ( space . id , createdAlert . id , 'rule' , 'alerting' ) ; const response = await supertestWithoutAuth . get ( `${ getUrlPrefix ( 'other' ) }/api/alerting/rule/${ createdAlert . id }` ) . auth ( user . username , user . password ) ; expect ( response . statusCode ) . to . eql ( 404 ) ; switch ( scenario . id ) { case 'no_kibana_privileges at space1' : case 'space_1_all at space2' : case 'space_1_all at space1' : case 'space_1_all_alerts_none_actions at space1' : case 'space_1_all_with_restricted_fixture at space1' : case 'global_read at space1' : case 'superuser at space1' : expect ( response . body ) . to . eql ( { statusCode : 404 , error : 'Not Found' , message : `Saved object [alert/${ createdAlert . id }] not found` , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; it ( `should handle get alert request appropriately when alert doesn't exist` , async ( ) => { const response = await supertestWithoutAuth . get ( `${ getUrlPrefix ( space . id ) }/api/alerting/rule/1` ) . auth ( user . username , user . password ) ; switch ( scenario . id ) { case 'no_kibana_privileges 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_alerts_none_actions 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 [alert/1] not found' , } ) ; break ; default : throw new Error ( `Scenario untested: ${ JSON . stringify ( scenario ) }` ) ; } } ) ; } ) ; } } ) ; }