Skip to content

Commit

Permalink
[8.16] [Security Solution][Bug] Incorrect message as Duplicate entry?…
Browse files Browse the repository at this point in the history
… shown on creating New Knowledge Base index. (#198892) (#199045) (#199062)

# Backport

This will backport the following commits from `main` to `8.16`:
- [[Security Solution][Bug] Incorrect message as Duplicate entry? shown
on creating New Knowledge Base index. (#198892)
(#199045)](#199045)

<!--- Backport version: 9.4.3 -->

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

<!--BACKPORT [{"author":{"name":"Ievgen
Sorokopud","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-05T21:57:30Z","message":"[Security
Solution][Bug] Incorrect message as Duplicate entry? shown on creating
New Knowledge Base index. (#198892) (#199045)\n\n## Summary\r\n\r\nBUG:
https://github.com/elastic/kibana/issues/198892\r\n\r\nThis PR fixes the
BUG where we would show `Duplicate entry?`\r\nconfirmation modal on new
document creation. This happens because the\r\nstate is not being reset
properly on flyout close and we use previous\r\nvalues in the new entry
creation flow.\r\n\r\n### To test\r\n\r\n1. Create a global entry
(document or index)\r\n2. Edit the entry from 1\r\n3. Switch global to
private\r\n4. Save entry => This will trigger `Duplicate entry?`
confirmation modal\r\n(save it)\r\n5. Press `New > Index/Document`\r\n6.
Fill out required fields\r\n7. Press `Save` button\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"8ebe78857b11a20f4be5cdfbeb6b38897f270f19","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:
SecuritySolution","Team:Security Generative
AI","v8.16.0","backport:version","v8.17.0"],"title":"[Security
Solution][Bug] Incorrect message as Duplicate entry? shown on creating
New Knowledge Base index.
(#198892)","number":199045,"url":"https://github.com/elastic/kibana/pull/199045","mergeCommit":{"message":"[Security
Solution][Bug] Incorrect message as Duplicate entry? shown on creating
New Knowledge Base index. (#198892) (#199045)\n\n## Summary\r\n\r\nBUG:
https://github.com/elastic/kibana/issues/198892\r\n\r\nThis PR fixes the
BUG where we would show `Duplicate entry?`\r\nconfirmation modal on new
document creation. This happens because the\r\nstate is not being reset
properly on flyout close and we use previous\r\nvalues in the new entry
creation flow.\r\n\r\n### To test\r\n\r\n1. Create a global entry
(document or index)\r\n2. Edit the entry from 1\r\n3. Switch global to
private\r\n4. Save entry => This will trigger `Duplicate entry?`
confirmation modal\r\n(save it)\r\n5. Press `New > Index/Document`\r\n6.
Fill out required fields\r\n7. Press `Save` button\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"8ebe78857b11a20f4be5cdfbeb6b38897f270f19"}},"sourceBranch":"main","suggestedTargetBranches":["8.16","8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/199045","number":199045,"mergeCommit":{"message":"[Security
Solution][Bug] Incorrect message as Duplicate entry? shown on creating
New Knowledge Base index. (#198892) (#199045)\n\n## Summary\r\n\r\nBUG:
https://github.com/elastic/kibana/issues/198892\r\n\r\nThis PR fixes the
BUG where we would show `Duplicate entry?`\r\nconfirmation modal on new
document creation. This happens because the\r\nstate is not being reset
properly on flyout close and we use previous\r\nvalues in the new entry
creation flow.\r\n\r\n### To test\r\n\r\n1. Create a global entry
(document or index)\r\n2. Edit the entry from 1\r\n3. Switch global to
private\r\n4. Save entry => This will trigger `Duplicate entry?`
confirmation modal\r\n(save it)\r\n5. Press `New > Index/Document`\r\n6.
Fill out required fields\r\n7. Press `Save` button\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"8ebe78857b11a20f4be5cdfbeb6b38897f270f19"}},{"branch":"8.16","label":"v8.16.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Ievgen Sorokopud <[email protected]>
  • Loading branch information
kibanamachine and e40pud authored Nov 5, 2024
1 parent 2a042c2 commit 9b18b75
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,63 @@ describe('KnowledgeBaseSettingsManagement', () => {
expect(mockCreateEntry).toHaveBeenCalledWith({ ...mockData[3], users: undefined });
});

it('does not show duplicate entry modal on new document entry creation', async () => {
// Covers the BUG: https://github.com/elastic/kibana/issues/198892
const closeFlyoutMock = jest.fn();
(useFlyoutModalVisibility as jest.Mock).mockReturnValue({
isFlyoutOpen: true,
openFlyout: jest.fn(),
closeFlyout: closeFlyoutMock,
});
render(<KnowledgeBaseSettingsManagement dataViews={mockDataViews} />, {
wrapper,
});

await waitFor(() => {
fireEvent.click(screen.getAllByTestId('edit-button')[3]);
});
expect(screen.getByTestId('flyout')).toBeVisible();

await waitFor(() => {
expect(screen.getByText('Edit document entry')).toBeInTheDocument();
});

await waitFor(() => {
fireEvent.click(screen.getByTestId('sharing-select'));
fireEvent.click(screen.getByTestId('sharing-private-option'));
fireEvent.click(screen.getByTestId('save-button'));
});

expect(screen.getByTestId('create-duplicate-entry-modal')).toBeInTheDocument();
await waitFor(() => {
fireEvent.click(screen.getByTestId('confirmModalConfirmButton'));
});
expect(screen.queryByTestId('create-duplicate-entry-modal')).not.toBeInTheDocument();
await waitFor(() => {
expect(mockCreateEntry).toHaveBeenCalledTimes(1);
});

// Create a new document entry
await waitFor(() => {
fireEvent.click(screen.getByTestId('addEntry'));
});
await waitFor(() => {
fireEvent.click(screen.getByTestId('addDocument'));
});

expect(screen.getByTestId('flyout')).toBeVisible();

await userEvent.type(screen.getByTestId('entryNameInput'), 'hi');
await userEvent.type(screen.getByTestId('entryMarkdownInput'), 'hi');

await waitFor(() => {
fireEvent.click(screen.getByTestId('save-button'));
});

expect(screen.queryByTestId('create-duplicate-entry-modal')).not.toBeInTheDocument();
expect(closeFlyoutMock).toHaveBeenCalled();
});

it('shows warning icon for index entries with missing indices', async () => {
render(<KnowledgeBaseSettingsManagement dataViews={mockDataViews} />, {
wrapper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,31 @@ export const KnowledgeBaseSettingsManagement: React.FC<Params> = React.memo(({ d
isRefetching: kbStatus?.is_setup_in_progress,
});

const resetStateAndCloseFlyout = useCallback(() => {
setOriginalEntry(undefined);
setSelectedEntry(undefined);
setDuplicateKBItem(null);
closeFlyout();
}, [closeFlyout]);

// Flyout Save/Cancel Actions
const onSaveConfirmed = useCallback(async () => {
if (isKnowledgeBaseEntryResponse(selectedEntry)) {
await updateEntries([selectedEntry]);
closeFlyout();
resetStateAndCloseFlyout();
} else if (isKnowledgeBaseEntryCreateProps(selectedEntry)) {
if (originalEntry) {
setDuplicateKBItem(selectedEntry);
return;
}
await createEntry(selectedEntry);
closeFlyout();
resetStateAndCloseFlyout();
}
}, [selectedEntry, originalEntry, updateEntries, closeFlyout, createEntry]);
}, [selectedEntry, updateEntries, resetStateAndCloseFlyout, originalEntry, createEntry]);

const onSaveCancelled = useCallback(() => {
setOriginalEntry(undefined);
setSelectedEntry(undefined);
closeFlyout();
}, [closeFlyout]);
resetStateAndCloseFlyout();
}, [resetStateAndCloseFlyout]);

const { value: existingIndices } = useAsync(() => {
const indices: string[] = [];
Expand Down Expand Up @@ -323,10 +328,9 @@ export const KnowledgeBaseSettingsManagement: React.FC<Params> = React.memo(({ d
const handleDuplicateEntry = useCallback(async () => {
if (duplicateKBItem) {
await createEntry(duplicateKBItem);
closeFlyout();
setDuplicateKBItem(null);
resetStateAndCloseFlyout();
}
}, [closeFlyout, createEntry, duplicateKBItem]);
}, [createEntry, duplicateKBItem, resetStateAndCloseFlyout]);

if (!enableKnowledgeBaseByDefault) {
return (
Expand Down

0 comments on commit 9b18b75

Please sign in to comment.