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');