import expect from '@kbn/expect' ; import { deepFreeze } from '@kbn/std' ; import { SuperTest } from 'supertest' ; import { SavedObjectsCollectMultiNamespaceReferencesResponse , SavedObjectReferenceWithContext , } from '../../../../../src/core/server' ; import { MULTI_NAMESPACE_SAVED_OBJECT_TEST_CASES as CASES } from '../lib/saved_object_test_cases' ; import { SPACES } from '../lib/spaces' ; import { expectResponses , getUrlPrefix , } from '../../../saved_object_api_integration/common/lib/saved_object_test_utils' ; import { ExpectResponseBody , TestDefinition , TestSuite , } from '../../../saved_object_api_integration/common/lib/types' ; export interface GetShareableReferencesTestDefinition extends TestDefinition { request : { objects : Array < { type : string ; id : string ; } > ; } ; } export type GetShareableReferencesTestSuite = TestSuite < GetShareableReferencesTestDefinition > ; export interface GetShareableReferencesTestCase { objects : Array < { type : string ; id : string ; } > ; expectedResults : SavedObjectReferenceWithContext [ ] ; } const { DEFAULT : { spaceId : DEFAULT_SPACE_ID } , SPACE_1 : { spaceId : SPACE_1_ID } , SPACE_2 : { spaceId : SPACE_2_ID } , } = SPACES ; export const TEST_CASE_OBJECTS = deepFreeze ( { SHAREABLE_TYPE : { type : 'sharedtype' , id : CASES . EACH_SPACE . id } , SHAREABLE_TYPE_DOES_NOT_EXIST : { type : 'sharedtype' , id : 'does-not-exist' } , NON_SHAREABLE_TYPE : { type : 'isolatedtype' , id : 'my_isolated_object' } , } ) ; export const EXPECTED_RESULTS = { IN_DEFAULT_SPACE : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , spaces : [ DEFAULT_SPACE_ID , SPACE_1_ID , SPACE_2_ID ] , inboundReferences : [ { type : 'sharedtype' , id : CASES . DEFAULT_ONLY . id , name : 'refname' } ] , } , { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE_DOES_NOT_EXIST , spaces : [ ] , inboundReferences : [ ] , isMissing : true , } , { ... TEST_CASE_OBJECTS . NON_SHAREABLE_TYPE , spaces : [ ] , inboundReferences : [ ] } , { type : 'sharedtype' , id : CASES . DEFAULT_ONLY . id , spaces : [ DEFAULT_SPACE_ID ] , inboundReferences : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , name : 'refname' } ] , spacesWithMatchingAliases : [ SPACE_1_ID , SPACE_2_ID ] , } , { type : 'sharedtype' , id : CASES . SPACE_1_ONLY . id , spaces : [ ] , inboundReferences : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , name : 'refname' } ] , isMissing : true , } , { type : 'sharedtype' , id : CASES . SPACE_2_ONLY . id , spaces : [ ] , inboundReferences : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , name : 'refname' } ] , isMissing : true , } , { type : 'sharedtype' , id : CASES . ALL_SPACES . id , spaces : [ '*' ] , inboundReferences : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , name : 'refname' } ] , } , ] , IN_SPACE_1 : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , spaces : [ DEFAULT_SPACE_ID , SPACE_1_ID , SPACE_2_ID ] , inboundReferences : [ { type : 'sharedtype' , id : CASES . SPACE_1_ONLY . id , name : 'refname' } ] , } , { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE_DOES_NOT_EXIST , spaces : [ ] , inboundReferences : [ ] , isMissing : true , } , { ... TEST_CASE_OBJECTS . NON_SHAREABLE_TYPE , spaces : [ ] , inboundReferences : [ ] } , { type : 'sharedtype' , id : CASES . DEFAULT_ONLY . id , spaces : [ ] , inboundReferences : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , name : 'refname' } ] , isMissing : true , } , { type : 'sharedtype' , id : CASES . SPACE_1_ONLY . id , spaces : [ SPACE_1_ID ] , inboundReferences : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , name : 'refname' } ] , } , { type : 'sharedtype' , id : CASES . SPACE_2_ONLY . id , spaces : [ ] , inboundReferences : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , name : 'refname' } ] , isMissing : true , } , { type : 'sharedtype' , id : CASES . ALL_SPACES . id , spaces : [ '*' ] , inboundReferences : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , name : 'refname' } ] , } , ] , IN_SPACE_2 : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , spaces : [ DEFAULT_SPACE_ID , SPACE_1_ID , SPACE_2_ID ] , inboundReferences : [ { type : 'sharedtype' , id : CASES . SPACE_2_ONLY . id , name : 'refname' } ] , } , { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE_DOES_NOT_EXIST , spaces : [ ] , inboundReferences : [ ] , isMissing : true , } , { ... TEST_CASE_OBJECTS . NON_SHAREABLE_TYPE , spaces : [ ] , inboundReferences : [ ] } , { type : 'sharedtype' , id : CASES . DEFAULT_ONLY . id , spaces : [ ] , inboundReferences : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , name : 'refname' } ] , isMissing : true , } , { type : 'sharedtype' , id : CASES . SPACE_1_ONLY . id , spaces : [ ] , inboundReferences : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , name : 'refname' } ] , isMissing : true , } , { type : 'sharedtype' , id : CASES . SPACE_2_ONLY . id , spaces : [ SPACE_2_ID ] , inboundReferences : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , name : 'refname' } ] , } , { type : 'sharedtype' , id : CASES . ALL_SPACES . id , spaces : [ '*' ] , inboundReferences : [ { ... TEST_CASE_OBJECTS . SHAREABLE_TYPE , name : 'refname' } ] , } , ] , } ; const createRequest = ( { objects } ) => ( { objects } ) ; const getTestTitle = ( { objects } ) => { const objStr = objects . map ( ( { type , id } ) => `${ type }:${ id }` ) . join ( ',' ) ; return `{objects: [${ objStr }]}` ; } ; const getRedactedSpaces = ( authorizedSpace , spaces ) => { if ( ! authorizedSpace ) { return spaces ; } const redactedSpaces = spaces . map ( ( x ) => ( x !== authorizedSpace && x !== '*' ? '?' : x ) ) ; return redactedSpaces . sort ( ( a , b ) => ( a === '?' ? 1 : b === '?' ? - 1 : 0 ) ) ; } ; export function getShareableReferencesTestSuiteFactory ( esArchiver , supertest ) { const expectForbidden = expectResponses . forbiddenTypes ( 'share_to_space' ) ; const expectResponseBody = ( testCase , statusCode , authorizedSpace ? ) : ExpectResponseBody => async ( response ) => { if ( statusCode === 403 ) { const types = testCase . objects . map ( ( x ) => x . type ) ; await expectForbidden ( types ) ( response ) ; } else { const { expectedResults } = testCase ; const apiResponse = response . body as SavedObjectsCollectMultiNamespaceReferencesResponse ; expect ( apiResponse . objects ) . to . have . length ( expectedResults . length ) ; expectedResults . forEach ( ( expectedResult , i ) => { const { spaces , spacesWithMatchingAliases } = expectedResult ; const expectedSpaces = getRedactedSpaces ( authorizedSpace , spaces ) ; const expectedSpacesWithMatchingAliases = spacesWithMatchingAliases && getRedactedSpaces ( authorizedSpace , spacesWithMatchingAliases ) ; const expected = { ... expectedResult , spaces : expectedSpaces , ... ( expectedSpacesWithMatchingAliases && { spacesWithMatchingAliases : expectedSpacesWithMatchingAliases , } ) , } ; expect ( apiResponse . objects [ i ] ) . to . eql ( expected ) ; } ) ; } } ; const createTestDefinitions = ( testCases , forbidden , options = { } ) : GetShareableReferencesTestDefinition [ ] => { const cases = Array . isArray ( testCases ) ? testCases : [ testCases ] ; const responseStatusCode = forbidden ? 403 : 200 ; return cases . map ( ( x ) => ( { title : getTestTitle ( x ) , responseStatusCode , request : createRequest ( x ) , responseBody : options ?. responseBodyOverride || expectResponseBody ( x , responseStatusCode , options . authorizedSpace ) , } ) ) ; } ; const makeGetShareableReferencesTest = ( describeFn ) => ( description , definition ) => { const { user , spaceId = SPACES . DEFAULT . spaceId , tests } = definition ; describeFn ( description , ( ) => { before ( ( ) => esArchiver . load ( 'x-pack/test/spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces' ) ) ; after ( ( ) => esArchiver . unload ( 'x-pack/test/spaces_api_integration/common/fixtures/es_archiver/saved_objects/spaces' ) ) ; for ( const test of tests ) { it ( `should return ${ test . responseStatusCode } ${ test . title }` , async ( ) => { const requestBody = test . request ; await supertest . post ( `${ getUrlPrefix ( spaceId ) }/api/spaces/_get_shareable_references` ) . auth ( user ?. username , user ?. password ) . send ( requestBody ) . expect ( test . responseStatusCode ) . then ( test . responseBody ) ; } ) ; } } ) ; } ; const addTests = makeGetShareableReferencesTest ( describe ) ; addTests . only = makeGetShareableReferencesTest ( describe . only ) ; return { addTests , createTestDefinitions , } ; }