From 27a8cdfcee15e382f5d8bbf4d388ebc1b6b3ba2c Mon Sep 17 00:00:00 2001 From: Antonio <antonio.coelho@elastic.co> Date: Wed, 22 Jan 2025 17:15:43 +0100 Subject: [PATCH] Fixed an error with missing uids in the cases detail page (#207228) Fixes #206801 ## Summary When opening the case detail page we retrieve user profile info for the different case user actions. If the uid stored in ES is an empty string for any of these user actions, we get an error that looks like this:  ### Steps to reproduce/test (thanks @jcger ) 1. Create a user with the `system_indices_superuser` role 2. Create a case and assign a user to it 3. Get the ID of the assignment user action from the case above ``` GET .kibana_alerting_cases/_search { "query": { "bool": { "filter": [ { "term": { "type": "cases-user-actions" } }, { "term": { "cases-user-actions.type": "assignees" } }, { "nested": { "path": "references", "query": { "bool": { "filter": [ { "term": { "references.type": "cases" } }, { "term": { "references.id": "<case_id>" } } ] } } } } ] } } } ``` 4. Manually set the `uid` of the assignee to `""` ``` POST .kibana_alerting_cases/_update/<cases-user-actions-id> { "script": { "source": """ ctx._source["cases-user-actions"].payload.assignees[0].uid = ""; """ } } ``` After this PR the popup should **not** appear anymore. (cherry picked from commit d8e5cbf67f2bae859a18f956c22205b78d3da5aa) --- .../server/client/user_actions/users.test.ts | 61 +++++++++++++++++++ .../cases/server/client/user_actions/users.ts | 16 ++--- 2 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 x-pack/platform/plugins/shared/cases/server/client/user_actions/users.test.ts diff --git a/x-pack/platform/plugins/shared/cases/server/client/user_actions/users.test.ts b/x-pack/platform/plugins/shared/cases/server/client/user_actions/users.test.ts new file mode 100644 index 0000000000000..61744b8455435 --- /dev/null +++ b/x-pack/platform/plugins/shared/cases/server/client/user_actions/users.test.ts @@ -0,0 +1,61 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { createUserActionServiceMock } from '../../services/mocks'; +import { createMockClient } from '../metrics/test_utils/client'; +import { createCasesClientMockArgs } from '../mocks'; +import { getUsers } from './users'; +import { mockCases } from '../../mocks'; +import type { CaseResolveResponse } from '../../../common/types/api'; +import { getUserProfiles } from '../cases/utils'; + +jest.mock('../cases/utils'); + +const getUserProfilesMock = getUserProfiles as jest.Mock; + +describe('getUsers', () => { + const casesClient = createMockClient(); + + casesClient.cases.resolve.mockResolvedValue({ + case: mockCases[0].attributes, + } as CaseResolveResponse); + + const clientArgs = createCasesClientMockArgs(); + const userActionService = createUserActionServiceMock(); + + userActionService.getUsers.mockResolvedValue({ + participants: [ + { + id: 'foo', + owner: 'bar', + user: { email: '', full_name: '', username: '', profile_uid: '' }, + }, + { + id: 'foo', + owner: 'bar', + user: { email: '', full_name: '', username: '', profile_uid: 'some_profile_id' }, + }, + ], + assignedAndUnassignedUsers: new Set([]), + }); + getUserProfilesMock.mockResolvedValue(new Map()); + clientArgs.services.userActionService = userActionService; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('removes empty uids from getUserProfiles call', async () => { + await getUsers({ caseId: 'test-case' }, casesClient, clientArgs); + + expect(getUserProfilesMock).toHaveBeenCalledWith( + expect.any(Object), + new Set(['some_profile_id']), + expect.any(String) + ); + }); +}); diff --git a/x-pack/platform/plugins/shared/cases/server/client/user_actions/users.ts b/x-pack/platform/plugins/shared/cases/server/client/user_actions/users.ts index cc189f987afe1..834a0bb0edb6b 100644 --- a/x-pack/platform/plugins/shared/cases/server/client/user_actions/users.ts +++ b/x-pack/platform/plugins/shared/cases/server/client/user_actions/users.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { isString } from 'lodash'; +import { isEmpty, isString } from 'lodash'; import type { UserProfileAvatarData, UserProfileWithAvatar } from '@kbn/user-profile-components'; import type { GetCaseUsersResponse } from '../../../common/types/api'; import { GetCaseUsersResponseRt } from '../../../common/types/api'; @@ -57,12 +57,14 @@ export const getUsers = async ( const reporter = theCase.case.created_by; const reporterProfileIdAsArray = reporter.profile_uid != null ? [reporter.profile_uid] : []; - const userProfileUids = new Set([ - ...assignedAndUnassignedUsers, - ...participantsUids, - ...assigneesUids, - ...reporterProfileIdAsArray, - ]); + const userProfileUids = new Set( + [ + ...assignedAndUnassignedUsers, + ...participantsUids, + ...assigneesUids, + ...reporterProfileIdAsArray, + ].filter((uid) => !isEmpty(uid)) + ); const userProfiles = await getUserProfiles(securityStartPlugin, userProfileUids, 'avatar');