From 2f4dedd561cf71742858a6c609c29424e530898d Mon Sep 17 00:00:00 2001 From: Qxisylolo Date: Fri, 22 Nov 2024 17:20:45 +0800 Subject: [PATCH] resolve tests Signed-off-by: Qxisylolo --- src/core/types/saved_objects.ts | 2 +- ...space_saved_objects_client_wrapper.test.ts | 37 ++-- ...space_saved_objects_client_wrapper.test.ts | 163 +++++++++++------- .../workspace_saved_objects_client_wrapper.ts | 29 +++- 4 files changed, 135 insertions(+), 96 deletions(-) diff --git a/src/core/types/saved_objects.ts b/src/core/types/saved_objects.ts index 5feaec20ec60..e63f380d9fb0 100644 --- a/src/core/types/saved_objects.ts +++ b/src/core/types/saved_objects.ts @@ -100,7 +100,7 @@ export interface SavedObject { updated_at?: string; error?: SavedObjectError; /** {@inheritdoc SavedObjectAttributes} */ - attributes: T | null; + attributes: T; /** {@inheritdoc SavedObjectReference} */ references: SavedObjectReference[]; /** {@inheritdoc SavedObjectsMigrationVersion} */ diff --git a/src/plugins/workspace/server/saved_objects/integration_tests/workspace_saved_objects_client_wrapper.test.ts b/src/plugins/workspace/server/saved_objects/integration_tests/workspace_saved_objects_client_wrapper.test.ts index 82c943545aca..8a4a142c3e8c 100644 --- a/src/plugins/workspace/server/saved_objects/integration_tests/workspace_saved_objects_client_wrapper.test.ts +++ b/src/plugins/workspace/server/saved_objects/integration_tests/workspace_saved_objects_client_wrapper.test.ts @@ -200,27 +200,24 @@ describe('WorkspaceSavedObjectsClientWrapper', () => { describe('bulkGet', () => { it('should throw forbidden error when user not permitted', async () => { - let error; - try { - await notPermittedSavedObjectedClient.bulkGet([ - { type: 'dashboard', id: 'inner-workspace-dashboard-1' }, - ]); - } catch (e) { - error = e; - } - expect(error).not.toBeUndefined(); - expect(SavedObjectsErrorHelpers.isForbiddenError(error)).toBe(true); + const result = await notPermittedSavedObjectedClient.bulkGet([ + { type: 'dashboard', id: 'acl-controlled-dashboard-2' }, + ]); - error = undefined; - try { - await notPermittedSavedObjectedClient.bulkGet([ - { type: 'dashboard', id: 'acl-controlled-dashboard-2' }, - ]); - } catch (e) { - error = e; - } - expect(error).not.toBeUndefined(); - expect(SavedObjectsErrorHelpers.isForbiddenError(error)).toBe(true); + expect(result.saved_objects).toEqual([ + { + ...result.saved_objects[0], + id: 'acl-controlled-dashboard-2', + type: 'dashboard', + attributes: {}, + error: { + error: 'Invalid saved objects permission', + statusCode: 403, + message: 'Permission denied', + }, + workspaces: [], + }, + ]); }); it('should return consistent dashboard when user permitted', async () => { diff --git a/src/plugins/workspace/server/saved_objects/workspace_saved_objects_client_wrapper.test.ts b/src/plugins/workspace/server/saved_objects/workspace_saved_objects_client_wrapper.test.ts index 153db6d831c3..8a7dcd62c964 100644 --- a/src/plugins/workspace/server/saved_objects/workspace_saved_objects_client_wrapper.test.ts +++ b/src/plugins/workspace/server/saved_objects/workspace_saved_objects_client_wrapper.test.ts @@ -797,17 +797,17 @@ describe('WorkspaceSavedObjectsClientWrapper', () => { expect(result.saved_objects).toEqual([ { - id: '', + id: 'not-permitted-dashboard', type: 'dashboard', - attributes: null, + attributes: {}, error: { - error: 'Invalid saved objects permission', + error: 'Forbidden', + message: 'Invalid saved objects permission', statusCode: 403, - message: 'Permission denied', }, + workspaces: [], permissions: {}, references: [], - workspaces: [], }, ]); }); @@ -854,36 +854,51 @@ describe('WorkspaceSavedObjectsClientWrapper', () => { }); it('should validate data source or data connection workspace field', async () => { const { wrapper } = generateWorkspaceSavedObjectsClientWrapper(); - let errorCatched; - try { - await wrapper.bulkGet([ - { - type: 'data-source', - id: 'workspace-1-data-source', + const result1 = await wrapper.bulkGet([ + { + type: 'data-source', + id: 'workspace-1-data-source', + }, + ]); + + expect(result1.saved_objects).toEqual([ + { + id: 'workspace-1-data-source', + type: 'data-source', + attributes: {}, + error: { + error: 'Forbidden', + message: 'Invalid data source permission, please associate it to current workspace', + statusCode: 403, }, - ]); - } catch (e) { - errorCatched = e; - } - expect(errorCatched?.message).toEqual( - 'Invalid data source permission, please associate it to current workspace' - ); + workspaces: [], + references: [], + }, + ]); - try { - await wrapper.bulkGet([ - { - type: 'data-connection', - id: 'workspace-1-data-connection', + const result2 = await wrapper.bulkGet([ + { + type: 'data-connection', + id: 'workspace-1-data-connection', + }, + ]); + + expect(result2.saved_objects).toEqual([ + { + type: 'data-connection', + id: 'workspace-1-data-connection', + attributes: {}, + error: { + error: 'Forbidden', + message: 'Invalid data source permission, please associate it to current workspace', + statusCode: 403, }, - ]); - } catch (e) { - errorCatched = e; - } - expect(errorCatched?.message).toEqual( - 'Invalid data source permission, please associate it to current workspace' - ); + workspaces: [], + references: [], + }, + ]); - let result = await await wrapper.bulkGet([ + let result = await wrapper.bulkGet([ { type: 'data-source', id: 'workspace-2-data-source', @@ -971,46 +986,62 @@ describe('WorkspaceSavedObjectsClientWrapper', () => { it('should throw permission error when tried to bulk get global data source or data connection', async () => { const { wrapper, requestMock } = generateWorkspaceSavedObjectsClientWrapper(); updateWorkspaceState(requestMock, { requestWorkspaceId: undefined }); - let errorCatched; - try { - await wrapper.bulkGet([{ type: 'data-source', id: 'global-data-source' }]); - } catch (e) { - errorCatched = e; - } - expect(errorCatched?.message).toEqual( - 'Invalid data source permission, please associate it to current workspace' - ); - try { - await wrapper.bulkGet([{ type: 'data-connection', id: 'global-data-connection' }]); - } catch (e) { - errorCatched = e; - } - expect(errorCatched?.message).toEqual( - 'Invalid data source permission, please associate it to current workspace' - ); + + const result = await wrapper.bulkGet([{ type: 'data-source', id: 'global-data-source' }]); + expect(result.saved_objects).toEqual([ + { + attributes: {}, + id: 'global-data-source', + type: 'data-source', + references: [], + workspaces: [], + error: { + error: 'Forbidden', + message: 'Invalid data source permission, please associate it to current workspace', + statusCode: 403, + }, + }, + ]); }); it('should throw permission error when tried to bulk get a empty workspace global data source or data connection', async () => { const { wrapper, requestMock } = generateWorkspaceSavedObjectsClientWrapper(); updateWorkspaceState(requestMock, { requestWorkspaceId: undefined }); - let errorCatched; - try { - await wrapper.bulkGet([ - { type: 'data-source', id: 'global-data-source-empty-workspaces' }, - ]); - } catch (e) { - errorCatched = e; - } - expect(errorCatched?.message).toEqual( - 'Invalid data source permission, please associate it to current workspace' - ); - try { - await wrapper.bulkGet([ - { type: 'data-connection', id: 'global-data-connection-empty-workspaces' }, - ]); - } catch (e) { - errorCatched = e; - } + const result = await wrapper.bulkGet([ + { type: 'data-source', id: 'global-data-source-empty-workspaces' }, + ]); + expect(result.saved_objects).toEqual([ + { + attributes: {}, + id: 'global-data-source-empty-workspaces', + type: 'data-source', + references: [], + workspaces: [], + error: { + error: 'Forbidden', + message: 'Invalid data source permission, please associate it to current workspace', + statusCode: 403, + }, + }, + ]); + + const result1 = await wrapper.bulkGet([ + { type: 'data-source', id: 'global-data-source-empty-workspaces' }, + ]); + expect(result1.saved_objects).toEqual([ + { + attributes: {}, + id: 'global-data-source-empty-workspaces', + type: 'data-source', + references: [], + workspaces: [], + error: { + error: 'Forbidden', + message: 'Invalid data source permission, please associate it to current workspace', + statusCode: 403, + }, + }, + ]); }); }); describe('find', () => { diff --git a/src/plugins/workspace/server/saved_objects/workspace_saved_objects_client_wrapper.ts b/src/plugins/workspace/server/saved_objects/workspace_saved_objects_client_wrapper.ts index c13dfd8af680..14b3bd68e728 100644 --- a/src/plugins/workspace/server/saved_objects/workspace_saved_objects_client_wrapper.ts +++ b/src/plugins/workspace/server/saved_objects/workspace_saved_objects_client_wrapper.ts @@ -502,7 +502,7 @@ export class WorkspaceSavedObjectsClientWrapper { wrapperOptions.request, getWorkspacesFromSavedObjects(objectToBulkGet.saved_objects) ); - + let flag = true; const processedObjects = await Promise.all( objectToBulkGet.saved_objects.map(async (object) => { if (validateIsWorkspaceDataSourceAndConnectionObjectType(object.type)) { @@ -511,8 +511,17 @@ export class WorkspaceSavedObjectsClientWrapper { wrapperOptions.request ); if (!hasPermission) { + flag = false; ACLAuditor?.increment(ACLAuditorStateKey.VALIDATE_FAILURE, 1); - throw generateDataSourcePermissionError(); + return { + ...object, + workspaces: [], + attributes: {} as T, + error: { + ...generateDataSourcePermissionError().output.payload, + statusCode: 403, + }, + }; } } @@ -525,15 +534,15 @@ export class WorkspaceSavedObjectsClientWrapper { false )) ) { + flag = false; ACLAuditor?.increment(ACLAuditorStateKey.VALIDATE_FAILURE, 1); return { ...object, workspaces: [], - attributes: null, + attributes: {} as T, error: { - error: 'Invalid saved objects permission', + ...generateSavedObjectsPermissionError().output.payload, statusCode: 403, - message: 'Permission denied', }, }; } @@ -541,10 +550,12 @@ export class WorkspaceSavedObjectsClientWrapper { }) ); - ACLAuditor?.increment( - ACLAuditorStateKey.VALIDATE_SUCCESS, - objectToBulkGet.saved_objects.length - ); + if (flag) { + ACLAuditor?.increment( + ACLAuditorStateKey.VALIDATE_SUCCESS, + objectToBulkGet.saved_objects.length + ); + } return { saved_objects: processedObjects }; };