import expect from '@kbn/expect' ; import { superUser , obsOnlySpacesAll , secOnlyRead } 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 APM_ALERT_INDEX = '.alerts-observability-apm' ; const SECURITY_SOLUTION_ALERT_INDEX = '.alerts-security.alerts' ; const getAPMIndexName = async ( user , space , expected = 200 ) => { const { body : indexNames } = await supertestWithoutAuth . get ( `${ getSpaceUrlPrefix ( space ) }${ ALERTS_INDEX_URL }?features=apm` ) . auth ( user . username , user . password ) . set ( 'kbn-xsrf' , 'true' ) . expect ( expected ) ; return indexNames ; } ; const getSecuritySolutionIndexName = async ( user , space , expectedStatusCode = 200 ) => { const { body : indexNames } = await supertestWithoutAuth . get ( `${ getSpaceUrlPrefix ( space ) }${ ALERTS_INDEX_URL }?features=siem` ) . auth ( user . username , user . password ) . set ( 'kbn-xsrf' , 'true' ) . expect ( expectedStatusCode ) ; return indexNames ; } ; describe ( 'Alert - Get Index - RBAC - spaces' , ( ) => { before ( async ( ) => { await esArchiver . load ( 'x-pack/test/functional/es_archives/rule_registry/alerts' ) ; } ) ; describe ( 'Users:' , ( ) => { it ( `${ obsOnlySpacesAll . username } should be able to access the APM alert in ${ SPACE1 }` , async ( ) => { const indexNames = await getAPMIndexName ( obsOnlySpacesAll , SPACE1 ) ; const observabilityIndex = indexNames ?. index_name ?. find ( ( indexName ) => indexName === APM_ALERT_INDEX ) ; expect ( observabilityIndex ) . to . eql ( APM_ALERT_INDEX ) ; } ) ; it ( `${ superUser . username } should be able to access the APM alert in ${ SPACE1 }` , async ( ) => { const indexNames = await getAPMIndexName ( superUser , SPACE1 ) ; const observabilityIndex = indexNames ?. index_name ?. find ( ( indexName ) => indexName === APM_ALERT_INDEX ) ; expect ( observabilityIndex ) . to . eql ( APM_ALERT_INDEX ) ; } ) ; it ( `${ secOnlyRead . username } should NOT be able to access the APM alert in ${ SPACE1 }` , async ( ) => { const indexNames = await getAPMIndexName ( secOnlyRead , SPACE1 ) ; expect ( indexNames ?. index_name ?. length ) . to . eql ( 0 ) ; } ) ; it ( `${ secOnlyRead . username } should be able to access the security solution alert in ${ SPACE1 }` , async ( ) => { const indexNames = await getSecuritySolutionIndexName ( secOnlyRead , SPACE1 ) ; const securitySolution = indexNames ?. index_name ?. find ( ( indexName ) => indexName . startsWith ( SECURITY_SOLUTION_ALERT_INDEX ) ) ; expect ( securitySolution ) . to . eql ( `${ SECURITY_SOLUTION_ALERT_INDEX }-${ SPACE1 }` ) ; } ) ; } ) ; } ) ; } ;