import expect from '@kbn/expect' ; import { superUser } from '../../../common/lib/authentication/users' ; import type { User } from '../../../common/lib/authentication/types' ; import { FtrProviderContext } from '../../../common/ftr_provider_context' ; import { getSpaceUrlPrefix } from '../../../common/lib/authentication/spaces' ; export default ( { getService } ) => { const supertestWithoutAuth = getService ( 'supertestWithoutAuth' ) ; const esArchiver = getService ( 'esArchiver' ) ; const TEST_URL = '/internal/rac/alerts' ; const ALERTS_INDEX_URL = `${ TEST_URL }/index` ; const SPACE1 = 'space1' ; const SPACE2 = 'space2' ; const APM_ALERT_ID = 'NoxgpHkBqbdrfX07MqXV' ; const APM_ALERT_INDEX = '.alerts-observability.apm.alerts' ; const SECURITY_SOLUTION_ALERT_INDEX = '.alerts-security.alerts' ; const getAPMIndexName = async ( user ) => { const { body : indexNames } = await supertestWithoutAuth . get ( `${ getSpaceUrlPrefix ( SPACE1 ) }${ ALERTS_INDEX_URL }` ) . set ( 'kbn-xsrf' , 'true' ) . expect ( 200 ) ; const observabilityIndex = indexNames ?. index_name ?. find ( ( indexName ) => indexName === APM_ALERT_INDEX ) ; expect ( observabilityIndex ) . to . eql ( APM_ALERT_INDEX ) ; } ; const getSecuritySolutionIndexName = async ( user ) => { const { body : indexNames } = await supertestWithoutAuth . get ( `${ getSpaceUrlPrefix ( SPACE1 ) }${ ALERTS_INDEX_URL }` ) . set ( 'kbn-xsrf' , 'true' ) . expect ( 200 ) ; const securitySolution = indexNames ?. index_name ?. find ( ( indexName ) => indexName . startsWith ( SECURITY_SOLUTION_ALERT_INDEX ) ) ; expect ( securitySolution ) . to . eql ( `${ SECURITY_SOLUTION_ALERT_INDEX }-${ SPACE1 }` ) ; } ; describe ( 'Alerts - GET - RBAC' , ( ) => { before ( async ( ) => { await getSecuritySolutionIndexName ( superUser ) ; await getAPMIndexName ( superUser ) ; await esArchiver . load ( 'x-pack/test/functional/es_archives/rule_registry/alerts' ) ; } ) ; after ( async ( ) => { await esArchiver . unload ( 'x-pack/test/functional/es_archives/rule_registry/alerts' ) ; } ) ; it ( 'should return a 404 when superuser accesses not-existent alert' , async ( ) => { await supertestWithoutAuth . get ( `${ getSpaceUrlPrefix ( ) }${ TEST_URL }?id=myfakeid&index=${ APM_ALERT_INDEX }` ) . set ( 'kbn-xsrf' , 'true' ) . expect ( 404 ) ; } ) ; it ( 'should return a 404 when superuser accesses not-existent alerts as data index' , async ( ) => { await supertestWithoutAuth . get ( `${ getSpaceUrlPrefix ( ) }${ TEST_URL }?id=${ APM_ALERT_ID }&index=myfakeindex` ) . set ( 'kbn-xsrf' , 'true' ) . expect ( 404 ) ; } ) ; it ( `${ superUser . username } should be able to access alert ${ APM_ALERT_ID } in ${ SPACE2 }/${ APM_ALERT_INDEX }` , async ( ) => { await supertestWithoutAuth . get ( `${ getSpaceUrlPrefix ( SPACE2 ) }${ TEST_URL }?id=${ APM_ALERT_ID }&index=${ APM_ALERT_INDEX }` ) . set ( 'kbn-xsrf' , 'true' ) . expect ( 200 ) ; } ) ; } ) ; } ;