[8.16] [Security assistant] Update conversations api improvement (#206828) (#206865)

# Backport

This will backport the following commits from `main` to `8.16`:
- [[Security assistant] Update conversations api improvement
(#206828)](https://github.com/elastic/kibana/pull/206828)

<!--- Backport version: 9.6.4 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Steph
Milovic","email":"stephanie.milovic@elastic.co"},"sourceCommit":{"committedDate":"2025-01-15T20:36:33Z","message":"[Security
assistant] Update conversations api improvement
(#206828)","sha":"56ce5d45c02436fc2cc31b20f1ccf29823bacf59","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:all-open","Team:Security
Generative AI"],"title":"[Security assistant] Update conversations api
improvement","number":206828,"url":"https://github.com/elastic/kibana/pull/206828","mergeCommit":{"message":"[Security
assistant] Update conversations api improvement
(#206828)","sha":"56ce5d45c02436fc2cc31b20f1ccf29823bacf59"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/206828","number":206828,"mergeCommit":{"message":"[Security
assistant] Update conversations api improvement
(#206828)","sha":"56ce5d45c02436fc2cc31b20f1ccf29823bacf59"}},{"url":"https://github.com/elastic/kibana/pull/206861","number":206861,"branch":"8.17","state":"OPEN"},{"url":"https://github.com/elastic/kibana/pull/206862","number":206862,"branch":"8.x","state":"OPEN"}]}]
BACKPORT-->
This commit is contained in:
Steph Milovic 2025-01-15 17:30:01 -07:00 committed by GitHub
parent 6f206300e1
commit 00ffc91bce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View file

@ -15,6 +15,7 @@ import {
} from '../../__mocks__/conversations_schema.mock';
import { updateConversationRoute } from './update_route';
import { AuthenticatedUser } from '@kbn/core-security-common';
import expect from 'expect';
describe('Update conversation route', () => {
let server: ReturnType<typeof serverMock.create>;
@ -28,6 +29,7 @@ describe('Update conversation route', () => {
} as AuthenticatedUser;
beforeEach(() => {
jest.clearAllMocks();
server = serverMock.create();
({ clients, context } = requestContextMock.createTools());
@ -151,5 +153,27 @@ describe('Update conversation route', () => {
`messages.0.role: Invalid enum value. Expected 'system' | 'user' | 'assistant', received 'invalid'`
);
});
// validate id is correct, check happens on L71 in update_route.ts
test('validates id is correct', async () => {
const request = requestMock.create({
method: 'put',
path: ELASTIC_AI_ASSISTANT_CONVERSATIONS_URL_BY_ID,
body: {
...getUpdateConversationSchemaMock(),
id: 'invalid-id',
},
params: { id: 'real-id' },
});
await server.inject(request, requestContextMock.convertContext(context));
expect(
clients.elasticAssistant.getAIAssistantConversationsDataClient.updateConversation
).toHaveBeenCalledWith(
expect.objectContaining({
conversationUpdateProps: expect.objectContaining({ id: 'real-id' }),
})
);
});
});
});

View file

@ -66,7 +66,7 @@ export const updateConversationRoute = (router: ElasticAssistantPluginRouter) =>
});
}
const conversation = await dataClient?.updateConversation({
conversationUpdateProps: request.body,
conversationUpdateProps: { ...request.body, id },
});
if (conversation == null) {
return assistantResponse.error({