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:

![Screenshot 2025-01-20 at 12 34
54](https://github.com/user-attachments/assets/175c6920-a4fb-4588-9668-1ba7d73f14f3)


### 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.
This commit is contained in:
Antonio 2025-01-22 17:15:43 +01:00 committed by GitHub
parent 27a26fae4b
commit d8e5cbf67f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 70 additions and 7 deletions

View file

@ -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)
);
});
});

View file

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