@@ -154,26 +154,35 @@ describe('SavedObjectsRepository', () => {
154154 validateDoc : jest . fn ( ) ,
155155 } ) ;
156156
157- const getMockGetResponse = ( { type, id, references, namespace } ) => ( {
158- // NOTE: Elasticsearch returns more fields (_index, _type) but the SavedObjectsRepository method ignores these
159- found : true ,
160- _id : `${ registry . isSingleNamespace ( type ) && namespace ? `${ namespace } :` : '' } ${ type } :${ id } ` ,
161- ...mockVersionProps ,
162- _source : {
163- ...( registry . isSingleNamespace ( type ) && { namespace } ) ,
164- ...( registry . isMultiNamespace ( type ) && { namespaces : [ namespace ?? 'default' ] } ) ,
165- type,
166- [ type ] : { title : 'Testing' } ,
167- references,
168- specialProperty : 'specialValue' ,
169- ...mockTimestampFields ,
170- } ,
171- } ) ;
157+ const getMockGetResponse = ( { type, id, references, namespace : objectNamespace } , namespace ) => {
158+ const namespaceId =
159+ // eslint-disable-next-line no-nested-ternary
160+ objectNamespace !== undefined
161+ ? objectNamespace !== 'default'
162+ ? objectNamespace
163+ : undefined
164+ : namespace ;
165+ return {
166+ // NOTE: Elasticsearch returns more fields (_index, _type) but the SavedObjectsRepository method ignores these
167+ found : true ,
168+ _id : `${
169+ registry . isSingleNamespace ( type ) && namespaceId ? `${ namespaceId } :` : ''
170+ } ${ type } :${ id } `,
171+ ...mockVersionProps ,
172+ _source : {
173+ ...( registry . isSingleNamespace ( type ) && { namespace : namespaceId } ) ,
174+ ...( registry . isMultiNamespace ( type ) && { namespaces : [ namespaceId ?? 'default' ] } ) ,
175+ type,
176+ [ type ] : { title : 'Testing' } ,
177+ references,
178+ specialProperty : 'specialValue' ,
179+ ...mockTimestampFields ,
180+ } ,
181+ } ;
182+ } ;
172183
173184 const getMockMgetResponse = ( objects , namespace ) => ( {
174- docs : objects . map ( ( obj ) =>
175- obj . found === false ? obj : getMockGetResponse ( { ...obj , namespace } )
176- ) ,
185+ docs : objects . map ( ( obj ) => ( obj . found === false ? obj : getMockGetResponse ( obj , namespace ) ) ) ,
177186 } ) ;
178187
179188 expect . extend ( {
@@ -1311,29 +1320,57 @@ describe('SavedObjectsRepository', () => {
13111320 const getId = ( type , id ) => `${ namespace } :${ type } :${ id } ` ;
13121321 await bulkUpdateSuccess ( [ obj1 , obj2 ] , { namespace } ) ;
13131322 expectClientCallArgsAction ( [ obj1 , obj2 ] , { method : 'update' , getId } ) ;
1323+
1324+ jest . clearAllMocks ( ) ;
1325+ // test again with object namespace string that supersedes the operation's namespace ID
1326+ await bulkUpdateSuccess ( [
1327+ { ...obj1 , namespace } ,
1328+ { ...obj2 , namespace } ,
1329+ ] ) ;
1330+ expectClientCallArgsAction ( [ obj1 , obj2 ] , { method : 'update' , getId } ) ;
13141331 } ) ;
13151332
13161333 it ( `doesn't prepend namespace to the id when providing no namespace for single-namespace type` , async ( ) => {
13171334 const getId = ( type , id ) => `${ type } :${ id } ` ;
13181335 await bulkUpdateSuccess ( [ obj1 , obj2 ] ) ;
13191336 expectClientCallArgsAction ( [ obj1 , obj2 ] , { method : 'update' , getId } ) ;
1337+
1338+ jest . clearAllMocks ( ) ;
1339+ // test again with object namespace string that supersedes the operation's namespace ID
1340+ await bulkUpdateSuccess (
1341+ [
1342+ { ...obj1 , namespace : 'default' } ,
1343+ { ...obj2 , namespace : 'default' } ,
1344+ ] ,
1345+ { namespace }
1346+ ) ;
1347+ expectClientCallArgsAction ( [ obj1 , obj2 ] , { method : 'update' , getId } ) ;
13201348 } ) ;
13211349
13221350 it ( `doesn't prepend namespace to the id when not using single-namespace type` , async ( ) => {
13231351 const getId = ( type , id ) => `${ type } :${ id } ` ;
1324- const objects1 = [ { ...obj1 , type : NAMESPACE_AGNOSTIC_TYPE } ] ;
1325- await bulkUpdateSuccess ( objects1 , { namespace } ) ;
1326- expectClientCallArgsAction ( objects1 , { method : 'update' , getId } ) ;
1327- client . bulk . mockClear ( ) ;
13281352 const overrides = {
13291353 // bulkUpdate uses a preflight `get` request for multi-namespace saved objects, and specifies that version on `update`
13301354 // we aren't testing for this here, but we need to include Jest assertions so this test doesn't fail
13311355 if_primary_term : expect . any ( Number ) ,
13321356 if_seq_no : expect . any ( Number ) ,
13331357 } ;
1334- const objects2 = [ { ...obj2 , type : MULTI_NAMESPACE_TYPE } ] ;
1335- await bulkUpdateSuccess ( objects2 , { namespace } ) ;
1336- expectClientCallArgsAction ( objects2 , { method : 'update' , getId, overrides } , 2 ) ;
1358+ const _obj1 = { ...obj1 , type : NAMESPACE_AGNOSTIC_TYPE } ;
1359+ const _obj2 = { ...obj2 , type : MULTI_NAMESPACE_TYPE } ;
1360+
1361+ await bulkUpdateSuccess ( [ _obj1 ] , { namespace } ) ;
1362+ expectClientCallArgsAction ( [ _obj1 ] , { method : 'update' , getId } ) ;
1363+ client . bulk . mockClear ( ) ;
1364+ await bulkUpdateSuccess ( [ _obj2 ] , { namespace } ) ;
1365+ expectClientCallArgsAction ( [ _obj2 ] , { method : 'update' , getId, overrides } , 2 ) ;
1366+
1367+ jest . clearAllMocks ( ) ;
1368+ // test again with object namespace string that supersedes the operation's namespace ID
1369+ await bulkUpdateSuccess ( [ { ..._obj1 , namespace } ] ) ;
1370+ expectClientCallArgsAction ( [ _obj1 ] , { method : 'update' , getId } ) ;
1371+ client . bulk . mockClear ( ) ;
1372+ await bulkUpdateSuccess ( [ { ..._obj2 , namespace } ] ) ;
1373+ expectClientCallArgsAction ( [ _obj2 ] , { method : 'update' , getId, overrides } , 2 ) ;
13371374 } ) ;
13381375 } ) ;
13391376
@@ -1684,11 +1721,7 @@ describe('SavedObjectsRepository', () => {
16841721 } ) ;
16851722
16861723 it ( `throws when there is a conflict with an existing multi-namespace saved object (get)` , async ( ) => {
1687- const response = getMockGetResponse ( {
1688- type : MULTI_NAMESPACE_TYPE ,
1689- id,
1690- namespace : 'bar-namespace' ,
1691- } ) ;
1724+ const response = getMockGetResponse ( { type : MULTI_NAMESPACE_TYPE , id } , 'bar-namespace' ) ;
16921725 client . get . mockResolvedValueOnce (
16931726 elasticsearchClientMock . createSuccessTransportRequestPromise ( response )
16941727 ) ;
@@ -1785,7 +1818,7 @@ describe('SavedObjectsRepository', () => {
17851818
17861819 const deleteSuccess = async ( type , id , options ) => {
17871820 if ( registry . isMultiNamespace ( type ) ) {
1788- const mockGetResponse = getMockGetResponse ( { type, id, namespace : options ?. namespace } ) ;
1821+ const mockGetResponse = getMockGetResponse ( { type, id } , options ?. namespace ) ;
17891822 client . get . mockResolvedValueOnce (
17901823 elasticsearchClientMock . createSuccessTransportRequestPromise ( mockGetResponse )
17911824 ) ;
@@ -1911,7 +1944,7 @@ describe('SavedObjectsRepository', () => {
19111944 } ) ;
19121945
19131946 it ( `throws when the type is multi-namespace and the document exists, but not in this namespace` , async ( ) => {
1914- const response = getMockGetResponse ( { type : MULTI_NAMESPACE_TYPE , id, namespace } ) ;
1947+ const response = getMockGetResponse ( { type : MULTI_NAMESPACE_TYPE , id } , namespace ) ;
19151948 client . get . mockResolvedValueOnce (
19161949 elasticsearchClientMock . createSuccessTransportRequestPromise ( response )
19171950 ) ;
@@ -2440,7 +2473,7 @@ describe('SavedObjectsRepository', () => {
24402473 const namespace = 'foo-namespace' ;
24412474
24422475 const getSuccess = async ( type , id , options ) => {
2443- const response = getMockGetResponse ( { type, id, namespace : options ?. namespace } ) ;
2476+ const response = getMockGetResponse ( { type, id } , options ?. namespace ) ;
24442477 client . get . mockResolvedValueOnce (
24452478 elasticsearchClientMock . createSuccessTransportRequestPromise ( response )
24462479 ) ;
@@ -2529,7 +2562,7 @@ describe('SavedObjectsRepository', () => {
25292562 } ) ;
25302563
25312564 it ( `throws when type is multi-namespace and the document exists, but not in this namespace` , async ( ) => {
2532- const response = getMockGetResponse ( { type : MULTI_NAMESPACE_TYPE , id, namespace } ) ;
2565+ const response = getMockGetResponse ( { type : MULTI_NAMESPACE_TYPE , id } , namespace ) ;
25332566 client . get . mockResolvedValueOnce (
25342567 elasticsearchClientMock . createSuccessTransportRequestPromise ( response )
25352568 ) ;
@@ -2579,7 +2612,7 @@ describe('SavedObjectsRepository', () => {
25792612 const incrementCounterSuccess = async ( type , id , field , options ) => {
25802613 const isMultiNamespace = registry . isMultiNamespace ( type ) ;
25812614 if ( isMultiNamespace ) {
2582- const response = getMockGetResponse ( { type, id, namespace : options ?. namespace } ) ;
2615+ const response = getMockGetResponse ( { type, id } , options ?. namespace ) ;
25832616 client . get . mockResolvedValueOnce (
25842617 elasticsearchClientMock . createSuccessTransportRequestPromise ( response )
25852618 ) ;
@@ -2716,11 +2749,7 @@ describe('SavedObjectsRepository', () => {
27162749 } ) ;
27172750
27182751 it ( `throws when there is a conflict with an existing multi-namespace saved object (get)` , async ( ) => {
2719- const response = getMockGetResponse ( {
2720- type : MULTI_NAMESPACE_TYPE ,
2721- id,
2722- namespace : 'bar-namespace' ,
2723- } ) ;
2752+ const response = getMockGetResponse ( { type : MULTI_NAMESPACE_TYPE , id } , 'bar-namespace' ) ;
27242753 client . get . mockResolvedValueOnce (
27252754 elasticsearchClientMock . createSuccessTransportRequestPromise ( response )
27262755 ) ;
@@ -3152,7 +3181,7 @@ describe('SavedObjectsRepository', () => {
31523181
31533182 const updateSuccess = async ( type , id , attributes , options ) => {
31543183 if ( registry . isMultiNamespace ( type ) ) {
3155- const mockGetResponse = getMockGetResponse ( { type, id, namespace : options ?. namespace } ) ;
3184+ const mockGetResponse = getMockGetResponse ( { type, id } , options ?. namespace ) ;
31563185 client . get . mockResolvedValueOnce (
31573186 elasticsearchClientMock . createSuccessTransportRequestPromise ( mockGetResponse )
31583187 ) ;
@@ -3349,7 +3378,7 @@ describe('SavedObjectsRepository', () => {
33493378 } ) ;
33503379
33513380 it ( `throws when type is multi-namespace and the document exists, but not in this namespace` , async ( ) => {
3352- const response = getMockGetResponse ( { type : MULTI_NAMESPACE_TYPE , id, namespace } ) ;
3381+ const response = getMockGetResponse ( { type : MULTI_NAMESPACE_TYPE , id } , namespace ) ;
33533382 client . get . mockResolvedValueOnce (
33543383 elasticsearchClientMock . createSuccessTransportRequestPromise ( response )
33553384 ) ;
0 commit comments