import expect from '@kbn/expect' ; import { parse } from 'url' ; import { FtrProviderContext } from '../../ftr_provider_context' ; export default ( { getPageObjects , getService } ) => { const pageObjects = getPageObjects ( [ 'common' , 'security' , 'roleMappings' ] ) ; const security = getService ( 'security' ) ; const testSubjects = getService ( 'testSubjects' ) ; const browser = getService ( 'browser' ) ; const aceEditor = getService ( 'aceEditor' ) ; describe ( 'Role Mappings' , function ( ) { before ( async ( ) => { const existingMappings = await security . roleMappings . getAll ( ) ; await Promise . all ( existingMappings . map ( ( m ) => security . roleMappings . delete ( m . name ) ) ) ; await pageObjects . common . navigateToApp ( 'roleMappings' ) ; } ) ; it ( 'displays a message when no role mappings exist' , async ( ) => { await testSubjects . existOrFail ( 'roleMappingsEmptyPrompt' ) ; await testSubjects . existOrFail ( 'createRoleMappingButton' ) ; } ) ; it ( 'allows a role mapping to be created' , async ( ) => { await testSubjects . click ( 'createRoleMappingButton' ) ; await testSubjects . setValue ( 'roleMappingFormNameInput' , 'new_role_mapping' ) ; await pageObjects . security . selectRole ( 'superuser' ) ; await testSubjects . click ( 'roleMappingsAddRuleButton' ) ; await testSubjects . click ( 'roleMappingsJSONRuleEditorButton' ) ; await aceEditor . setValue ( 'roleMappingsJSONEditor' , JSON . stringify ( { all : [ { field : { username : '*' , } , } , { field : { 'metadata.foo.bar' : 'baz' , } , } , { except : { any : [ { field : { dn : 'foo' , } , } , { field : { dn : 'bar' , } , } , ] , } , } , ] , } ) ) ; await testSubjects . click ( 'roleMappingsVisualRuleEditorButton' ) ; await testSubjects . click ( 'saveRoleMappingButton' ) ; await testSubjects . existOrFail ( 'savedRoleMappingSuccessToast' ) ; } ) ; it ( 'allows a role mapping to be deleted' , async ( ) => { await testSubjects . click ( 'euiCollapsedItemActionsButton' ) ; await testSubjects . click ( 'deleteRoleMappingButton-new_role_mapping' ) ; await testSubjects . click ( 'confirmModalConfirmButton' ) ; await testSubjects . existOrFail ( 'deletedRoleMappingSuccessToast' ) ; } ) ; it ( 'displays an error and returns to the listing page when navigating to a role mapping which does not exist' , async ( ) => { await pageObjects . common . navigateToUrl ( 'management' , 'security/role_mappings/edit/i-do-not-exist' , { ensureCurrentUrl : false , shouldUseHashForSubUrl : false } ) ; await testSubjects . existOrFail ( 'errorLoadingRoleMappingEditorToast' ) ; const url = parse ( await browser . getCurrentUrl ( ) ) ; expect ( url . pathname ) . to . eql ( '/app/management/security/role_mappings/' ) ; } ) ; describe ( 'with role mappings' , ( ) => { const mappings = [ { name : 'a_enabled_role_mapping' , enabled : true , roles : [ 'superuser' ] , rules : { field : { username : '*' , } , } , metadata : { } , } , { name : 'b_disabled_role_mapping' , enabled : false , role_templates : [ { template : { source : 'superuser' } } ] , rules : { field : { username : '*' , } , } , metadata : { } , } , ] ; before ( async ( ) => { await Promise . all ( mappings . map ( ( mapping ) => { const { name , ... payload } = mapping ; return security . roleMappings . create ( name , payload ) ; } ) ) ; await pageObjects . common . navigateToApp ( 'roleMappings' ) ; } ) ; after ( async ( ) => { await Promise . all ( mappings . map ( ( mapping ) => security . roleMappings . delete ( mapping . name ) ) ) ; } ) ; it ( 'displays a table of all role mappings' , async ( ) => { const rows = await testSubjects . findAll ( 'roleMappingRow' ) ; expect ( rows . length ) . to . eql ( mappings . length ) ; for ( let i = 0 ; i < rows . length ; i ++ ) { const row = rows [ i ] ; const mapping = mappings [ i ] ; const name = await ( await testSubjects . findDescendant ( 'roleMappingName' , row ) ) . getVisibleText ( ) ; const enabled = ( await ( await testSubjects . findDescendant ( 'roleMappingEnabled' , row ) ) . getVisibleText ( ) ) === 'Enabled' ; expect ( name ) . to . eql ( mapping . name ) ; expect ( enabled ) . to . eql ( mapping . enabled ) ; } } ) ; it ( 'allows a role mapping to be cloned' , async ( ) => { await testSubjects . click ( 'cloneRoleMappingButton-a_enabled_role_mapping' ) ; await testSubjects . setValue ( 'roleMappingFormNameInput' , 'cloned_role_mapping' ) ; await testSubjects . click ( 'saveRoleMappingButton' ) ; await testSubjects . existOrFail ( 'savedRoleMappingSuccessToast' ) ; const rows = await testSubjects . findAll ( 'roleMappingRow' ) ; expect ( rows . length ) . to . eql ( mappings . length + 1 ) ; } ) ; it ( 'allows a role mapping to be edited' , async ( ) => { await testSubjects . click ( 'roleMappingName' ) ; await testSubjects . click ( 'saveRoleMappingButton' ) ; await testSubjects . existOrFail ( 'savedRoleMappingSuccessToast' ) ; } ) ; } ) ; } ) ; } ;