forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security solution] Reinstall product documentation callout (elastic#…
- Loading branch information
1 parent
54436e3
commit ac45771
Showing
20 changed files
with
460 additions
and
40 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
...k/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/product_docs/const.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export const REACT_QUERY_KEYS = { | ||
GET_PRODUCT_DOC_STATUS: 'get_product_doc_status', | ||
INSTALL_PRODUCT_DOC: 'install_product_doc', | ||
}; |
62 changes: 62 additions & 0 deletions
62
.../kbn-elastic-assistant/impl/assistant/api/product_docs/use_get_product_doc_status.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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 { waitFor, renderHook } from '@testing-library/react'; | ||
import { useGetProductDocStatus } from './use_get_product_doc_status'; | ||
import { useAssistantContext } from '../../../..'; | ||
import { TestProviders } from '../../../mock/test_providers/test_providers'; | ||
|
||
jest.mock('../../../..', () => ({ | ||
useAssistantContext: jest.fn(), | ||
})); | ||
|
||
describe('useGetProductDocStatus', () => { | ||
const mockGetStatus = jest.fn(); | ||
|
||
beforeEach(() => { | ||
(useAssistantContext as jest.Mock).mockReturnValue({ | ||
productDocBase: { | ||
installation: { | ||
getStatus: mockGetStatus, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns loading state initially', async () => { | ||
mockGetStatus.mockResolvedValueOnce('status'); | ||
const { result } = renderHook(() => useGetProductDocStatus(), { | ||
wrapper: TestProviders, | ||
}); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
await waitFor(() => result.current.isSuccess); | ||
}); | ||
|
||
it('returns success state with data', async () => { | ||
mockGetStatus.mockResolvedValueOnce('status'); | ||
const { result } = renderHook(() => useGetProductDocStatus(), { | ||
wrapper: TestProviders, | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.status).toBe('status'); | ||
expect(result.current.isSuccess).toBe(true); | ||
}); | ||
}); | ||
|
||
it('returns error state when query fails', async () => { | ||
mockGetStatus.mockRejectedValueOnce(new Error('error')); | ||
const { result } = renderHook(() => useGetProductDocStatus(), { | ||
wrapper: TestProviders, | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isError).toBe(true); | ||
}); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
...hared/kbn-elastic-assistant/impl/assistant/api/product_docs/use_get_product_doc_status.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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 { useQuery } from '@tanstack/react-query'; | ||
import { REACT_QUERY_KEYS } from './const'; | ||
import { useAssistantContext } from '../../../..'; | ||
|
||
export function useGetProductDocStatus() { | ||
const { productDocBase } = useAssistantContext(); | ||
|
||
const { isLoading, isError, isSuccess, isRefetching, data, refetch } = useQuery({ | ||
queryKey: [REACT_QUERY_KEYS.GET_PRODUCT_DOC_STATUS], | ||
queryFn: async () => { | ||
return productDocBase.installation.getStatus(); | ||
}, | ||
keepPreviousData: false, | ||
refetchOnWindowFocus: false, | ||
}); | ||
|
||
return { | ||
status: data, | ||
refetch, | ||
isLoading, | ||
isRefetching, | ||
isSuccess, | ||
isError, | ||
}; | ||
} |
67 changes: 67 additions & 0 deletions
67
...red/kbn-elastic-assistant/impl/assistant/api/product_docs/use_install_product_doc.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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 { waitFor, renderHook } from '@testing-library/react'; | ||
import { useInstallProductDoc } from './use_install_product_doc'; | ||
import { useAssistantContext } from '../../../..'; | ||
import { TestProviders } from '../../../mock/test_providers/test_providers'; | ||
|
||
jest.mock('../../../..', () => ({ | ||
useAssistantContext: jest.fn(), | ||
})); | ||
|
||
describe('useInstallProductDoc', () => { | ||
const mockInstall = jest.fn(); | ||
const mockAddSuccess = jest.fn(); | ||
const mockAddError = jest.fn(); | ||
|
||
beforeEach(() => { | ||
(useAssistantContext as jest.Mock).mockReturnValue({ | ||
productDocBase: { | ||
installation: { | ||
install: mockInstall, | ||
}, | ||
}, | ||
toasts: { | ||
addSuccess: mockAddSuccess, | ||
addError: mockAddError, | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns success state and shows success toast on successful installation', async () => { | ||
mockInstall.mockResolvedValueOnce({}); | ||
const { result } = renderHook(() => useInstallProductDoc(), { | ||
wrapper: TestProviders, | ||
}); | ||
|
||
result.current.mutate(); | ||
await waitFor(() => result.current.isSuccess); | ||
|
||
expect(mockAddSuccess).toHaveBeenCalledWith( | ||
'The Elastic documentation was successfully installed' | ||
); | ||
}); | ||
|
||
it('returns error state and shows error toast on failed installation', async () => { | ||
const error = new Error('error message'); | ||
mockInstall.mockRejectedValueOnce(error); | ||
const { result } = renderHook(() => useInstallProductDoc(), { | ||
wrapper: TestProviders, | ||
}); | ||
|
||
result.current.mutate(); | ||
await waitFor(() => result.current.isError); | ||
|
||
expect(mockAddError).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
message: 'error message', | ||
}), | ||
{ title: 'Something went wrong while installing the Elastic documentation' } | ||
); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
...s/shared/kbn-elastic-assistant/impl/assistant/api/product_docs/use_install_product_doc.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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 { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
import { i18n } from '@kbn/i18n'; | ||
import type { IHttpFetchError, ResponseErrorBody } from '@kbn/core/public'; | ||
import type { PerformInstallResponse } from '@kbn/product-doc-base-plugin/common/http_api/installation'; | ||
import { REACT_QUERY_KEYS } from './const'; | ||
import { useAssistantContext } from '../../../..'; | ||
|
||
type ServerError = IHttpFetchError<ResponseErrorBody>; | ||
|
||
export function useInstallProductDoc() { | ||
const { productDocBase, toasts } = useAssistantContext(); | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation<PerformInstallResponse, ServerError, void>( | ||
[REACT_QUERY_KEYS.INSTALL_PRODUCT_DOC], | ||
() => { | ||
return productDocBase.installation.install(); | ||
}, | ||
{ | ||
onSuccess: () => { | ||
toasts?.addSuccess( | ||
i18n.translate('xpack.elasticAssistant.kb.installProductDoc.successNotification', { | ||
defaultMessage: 'The Elastic documentation was successfully installed', | ||
}) | ||
); | ||
|
||
queryClient.invalidateQueries({ | ||
queryKey: [REACT_QUERY_KEYS.GET_PRODUCT_DOC_STATUS], | ||
refetchType: 'all', | ||
}); | ||
}, | ||
onError: (error) => { | ||
toasts?.addError(new Error(error.body?.message ?? error.message), { | ||
title: i18n.translate('xpack.elasticAssistant.kb.installProductDoc.errorNotification', { | ||
defaultMessage: 'Something went wrong while installing the Elastic documentation', | ||
}), | ||
}); | ||
}, | ||
} | ||
); | ||
} |
87 changes: 87 additions & 0 deletions
87
...shared/kbn-elastic-assistant/impl/assistant/settings/product_documentation/index.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
import { ProductDocumentationManagement } from '.'; | ||
import * as i18n from './translations'; | ||
import { useInstallProductDoc } from '../../api/product_docs/use_install_product_doc'; | ||
import { useGetProductDocStatus } from '../../api/product_docs/use_get_product_doc_status'; | ||
|
||
jest.mock('../../api/product_docs/use_install_product_doc'); | ||
jest.mock('../../api/product_docs/use_get_product_doc_status'); | ||
|
||
describe('ProductDocumentationManagement', () => { | ||
const mockInstallProductDoc = jest.fn().mockResolvedValue({}); | ||
|
||
beforeEach(() => { | ||
(useInstallProductDoc as jest.Mock).mockReturnValue({ mutateAsync: mockInstallProductDoc }); | ||
(useGetProductDocStatus as jest.Mock).mockReturnValue({ status: null, isLoading: false }); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders loading spinner when status is loading', async () => { | ||
(useGetProductDocStatus as jest.Mock).mockReturnValue({ | ||
status: { overall: 'not_installed' }, | ||
isLoading: true, | ||
}); | ||
render(<ProductDocumentationManagement />); | ||
expect(screen.getByTestId('statusLoading')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders install button when not installed', () => { | ||
(useGetProductDocStatus as jest.Mock).mockReturnValue({ | ||
status: { overall: 'not_installed' }, | ||
isLoading: false, | ||
}); | ||
render(<ProductDocumentationManagement />); | ||
expect(screen.getByText(i18n.INSTALL)).toBeInTheDocument(); | ||
}); | ||
|
||
it('does not render anything when already installed', () => { | ||
(useGetProductDocStatus as jest.Mock).mockReturnValue({ | ||
status: { overall: 'installed' }, | ||
isLoading: false, | ||
}); | ||
const { container } = render(<ProductDocumentationManagement />); | ||
expect(container).toBeEmptyDOMElement(); | ||
}); | ||
|
||
it('shows installing spinner and text when installing', async () => { | ||
(useGetProductDocStatus as jest.Mock).mockReturnValue({ | ||
status: { overall: 'not_installed' }, | ||
isLoading: false, | ||
}); | ||
render(<ProductDocumentationManagement />); | ||
fireEvent.click(screen.getByText(i18n.INSTALL)); | ||
await waitFor(() => { | ||
expect(screen.getByTestId('installing')).toBeInTheDocument(); | ||
expect(screen.getByText(i18n.INSTALLING)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('sets installed state to true after successful installation', async () => { | ||
(useGetProductDocStatus as jest.Mock).mockReturnValue({ | ||
status: { overall: 'not_installed' }, | ||
isLoading: false, | ||
}); | ||
mockInstallProductDoc.mockResolvedValueOnce({}); | ||
render(<ProductDocumentationManagement />); | ||
fireEvent.click(screen.getByText(i18n.INSTALL)); | ||
await waitFor(() => expect(screen.queryByText(i18n.INSTALL)).not.toBeInTheDocument()); | ||
}); | ||
|
||
it('sets installed state to false after failed installation', async () => { | ||
(useGetProductDocStatus as jest.Mock).mockReturnValue({ | ||
status: { overall: 'not_installed' }, | ||
isLoading: false, | ||
}); | ||
mockInstallProductDoc.mockRejectedValueOnce(new Error('Installation failed')); | ||
render(<ProductDocumentationManagement />); | ||
fireEvent.click(screen.getByText(i18n.INSTALL)); | ||
await waitFor(() => expect(screen.getByText(i18n.INSTALL)).toBeInTheDocument()); | ||
}); | ||
}); |
Oops, something went wrong.