import { SPACES } from '../../common/lib/spaces' ; import { testCaseFailures , getTestScenarios , } from '../../../saved_object_api_integration/common/lib/saved_object_test_utils' ; import { TestUser } from '../../../saved_object_api_integration/common/lib/types' ; import { MULTI_NAMESPACE_SAVED_OBJECT_TEST_CASES as CASES } from '../../common/lib/saved_object_test_cases' ; import { updateObjectsSpacesTestSuiteFactory , UpdateObjectsSpacesTestDefinition , UpdateObjectsSpacesTestCase , } from '../../common/suites/update_objects_spaces' ; import { FtrProviderContext } from '../../common/ftr_provider_context' ; const { DEFAULT : { spaceId : DEFAULT_SPACE_ID } , SPACE_1 : { spaceId : SPACE_1_ID } , SPACE_2 : { spaceId : SPACE_2_ID } , } = SPACES ; const { fail404 } = testCaseFailures ; const createTestCases = ( spaceId ) : UpdateObjectsSpacesTestCase [ ] => { const eachSpace = [ DEFAULT_SPACE_ID , SPACE_1_ID , SPACE_2_ID ] ; return [ { objects : [ CASES . EACH_SPACE ] , spacesToAdd : [ '*' ] , spacesToRemove : [ ] , } , { objects : [ { id : CASES . EACH_SPACE . id , existingNamespaces : [ ... eachSpace , '*' ] } ] , spacesToAdd : [ ] , spacesToRemove : [ '*' ] , } , { objects : [ { ... CASES . DEFAULT_ONLY , ... fail404 ( spaceId !== DEFAULT_SPACE_ID ) } ] , spacesToAdd : [ SPACE_1_ID , SPACE_2_ID ] , spacesToRemove : [ ] , } , { objects : [ { ... CASES . SPACE_1_ONLY , ... fail404 ( spaceId !== SPACE_1_ID ) } ] , spacesToAdd : [ DEFAULT_SPACE_ID , SPACE_2_ID ] , spacesToRemove : [ ] , } , { objects : [ { ... CASES . SPACE_2_ONLY , ... fail404 ( spaceId !== SPACE_2_ID ) } ] , spacesToAdd : [ DEFAULT_SPACE_ID , SPACE_1_ID ] , spacesToRemove : [ ] , } , { objects : [ { id : CASES . DEFAULT_ONLY . id , existingNamespaces : eachSpace , ... fail404 ( spaceId !== DEFAULT_SPACE_ID ) , } , { id : CASES . SPACE_1_ONLY . id , existingNamespaces : eachSpace , ... fail404 ( spaceId !== SPACE_1_ID ) , } , { id : CASES . SPACE_2_ONLY . id , existingNamespaces : eachSpace , ... fail404 ( spaceId !== SPACE_2_ID ) , } , ] , spacesToAdd : [ ] , spacesToRemove : [ DEFAULT_SPACE_ID , SPACE_1_ID , SPACE_2_ID ] , } , { objects : [ { ... CASES . DEFAULT_AND_SPACE_1 , ... fail404 ( spaceId === SPACE_2_ID ) } , { ... CASES . DEFAULT_AND_SPACE_2 , ... fail404 ( spaceId === SPACE_1_ID ) } , { ... CASES . SPACE_1_AND_SPACE_2 , ... fail404 ( spaceId === DEFAULT_SPACE_ID ) } , CASES . ALL_SPACES , { ... CASES . DOES_NOT_EXIST , ... fail404 ( ) } , ] , spacesToAdd : [ spaceId ] , spacesToRemove : [ ] , } , { objects : [ { ... CASES . DEFAULT_AND_SPACE_1 , ... fail404 ( spaceId === SPACE_2_ID ) } , { ... CASES . DEFAULT_AND_SPACE_2 , ... fail404 ( spaceId === SPACE_1_ID ) } , { ... CASES . SPACE_1_AND_SPACE_2 , ... fail404 ( spaceId === DEFAULT_SPACE_ID ) } , { id : CASES . ALL_SPACES . id , existingNamespaces : [ '*' , spaceId ] } , { ... CASES . DOES_NOT_EXIST , ... fail404 ( ) } , ] , spacesToAdd : [ ] , spacesToRemove : [ spaceId ] , } , ] ; } ; const calculateSingleSpaceAuthZ = ( testCases , spaceId ) => { const targetsThisSpace = [ ] ; const targetsOtherSpace = [ ] ; for ( const testCase of testCases ) { const { spacesToAdd , spacesToRemove } = testCase ; const spacesToAddOrRemove = [ ... spacesToAdd , ... spacesToRemove ] ; if ( spacesToAddOrRemove . length === 1 && spacesToAddOrRemove [ 0 ] === spaceId ) { targetsThisSpace . push ( testCase ) ; } else { targetsOtherSpace . push ( testCase ) ; } } return { targetsThisSpace , targetsOtherSpace } ; } ; export default function ( { getService } ) { const supertest = getService ( 'supertestWithoutAuth' ) ; const esArchiver = getService ( 'esArchiver' ) ; const es = getService ( 'es' ) ; const { addTests , createTestDefinitions } = updateObjectsSpacesTestSuiteFactory ( es , esArchiver , supertest ) ; const createTests = ( spaceId ) => { const testCases = createTestCases ( spaceId ) ; const { targetsThisSpace , targetsOtherSpace } = calculateSingleSpaceAuthZ ( testCases , spaceId ) ; return { unauthorized : createTestDefinitions ( testCases , true ) , authorizedThisSpace : [ createTestDefinitions ( targetsOtherSpace , true ) , createTestDefinitions ( targetsThisSpace , false , { authorizedSpace : spaceId } ) , ] . flat ( ) , authorizedGlobally : createTestDefinitions ( testCases , false ) , } ; } ; describe ( '_update_objects_spaces' , ( ) => { getTestScenarios ( ) . securityAndSpaces . forEach ( ( { spaceId , users } ) => { const suffix = ` targeting the ${ spaceId } space` ; const { unauthorized , authorizedThisSpace , authorizedGlobally } = createTests ( spaceId ) ; const _addTests = ( user , tests ) => { addTests ( `${ user . description }${ suffix }` , { user , spaceId , tests } ) ; } ; [ users . noAccess , users . legacyAll , users . dualRead , users . readGlobally , users . readAtSpace , users . allAtOtherSpace , ] . forEach ( ( user ) => { _addTests ( user , unauthorized ) ; } ) ; _addTests ( users . allAtSpace , authorizedThisSpace ) ; [ users . dualAll , users . allGlobally , users . superuser ] . forEach ( ( user ) => { _addTests ( user , authorizedGlobally ) ; } ) ; } ) ; } ) ; }