Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security solution] Knowledge base unit tests #200207

Merged
merged 57 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 55 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
378092d
get/create api tests
stephmilovic Oct 28, 2024
ced9ee2
wip
stephmilovic Oct 28, 2024
b1b819b
more
stephmilovic Oct 28, 2024
9fa0a34
fix route tests
stephmilovic Oct 28, 2024
3a483bf
more
stephmilovic Oct 28, 2024
f1c35d7
wip
stephmilovic Oct 29, 2024
484b72d
the rest
stephmilovic Oct 29, 2024
5c6f486
update comments
stephmilovic Oct 29, 2024
af04dbc
fix tests
stephmilovic Oct 29, 2024
d5371de
fix types
stephmilovic Oct 29, 2024
be86eb0
delete unused code
stephmilovic Oct 29, 2024
2047e08
adjustments
stephmilovic Oct 29, 2024
3016ce9
Merge branch 'kb_flag_remove' into kb_tests
stephmilovic Oct 30, 2024
1cc6f5f
fix after flag
stephmilovic Oct 30, 2024
38e98b4
wip
stephmilovic Oct 30, 2024
601ebb6
rm more legacy
stephmilovic Oct 30, 2024
0a63bed
Merge branch 'kb_flag_remove' into kb_tests
stephmilovic Oct 30, 2024
15edb92
more tests
stephmilovic Oct 30, 2024
b237d69
wip
stephmilovic Oct 30, 2024
c8f41da
Merge branch 'main' into kb_flag_remove
stephmilovic Oct 30, 2024
9bb6607
merge w Patryk
stephmilovic Oct 30, 2024
0229827
update docs
stephmilovic Oct 30, 2024
b6964dc
Merge branch 'main' into kb_flag_remove
stephmilovic Oct 31, 2024
c3a71c4
rm unused consts
stephmilovic Oct 31, 2024
da4fc00
Merge branch 'main' into kb_flag_remove
stephmilovic Nov 4, 2024
0ef6052
fix merge
stephmilovic Nov 4, 2024
c60d573
further cleaning
stephmilovic Nov 4, 2024
52f9783
Merge branch 'main' into kb_flag_remove
stephmilovic Nov 4, 2024
6140209
more cleaning
stephmilovic Nov 4, 2024
6427d3c
even more better
stephmilovic Nov 4, 2024
3b6980a
Merge branch 'kb_flag_remove' into kb_tests
stephmilovic Nov 4, 2024
b302d85
fix type
stephmilovic Nov 4, 2024
0b0d417
fix mock
stephmilovic Nov 4, 2024
1d9b629
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Nov 4, 2024
306620d
test fixing
stephmilovic Nov 4, 2024
596fb9e
rm delete knowledge base route and params
stephmilovic Nov 4, 2024
5424b36
another rm kb delete
stephmilovic Nov 4, 2024
7d7aebc
rm unused arg
stephmilovic Nov 4, 2024
170d43e
Merge branch 'kb_flag_remove' into kb_tests
stephmilovic Nov 4, 2024
4497f45
fix merge
stephmilovic Nov 4, 2024
1bd9663
fix i18n
stephmilovic Nov 4, 2024
e585512
fix up bulk actions tests
stephmilovic Nov 4, 2024
f304bef
use common user variable
stephmilovic Nov 4, 2024
74f733d
more tests
stephmilovic Nov 5, 2024
73f0529
client tests
stephmilovic Nov 5, 2024
ac1358f
Merge branch 'main' into kb_flag_remove
stephmilovic Nov 5, 2024
f9101f3
Merge branch 'kb_flag_remove' into kb_tests
stephmilovic Nov 5, 2024
59cc8d4
Merge branch 'main' into kb_tests
stephmilovic Nov 14, 2024
1fb4a1a
rm line changes
stephmilovic Nov 14, 2024
81e21b7
fix types
stephmilovic Nov 14, 2024
9b2cd27
fix test and add more
stephmilovic Nov 14, 2024
829ff37
whoops, meant to remove this on last commit
stephmilovic Nov 14, 2024
0293286
Merge branch 'main' into kb_tests
stephmilovic Nov 14, 2024
8efbbac
[CI] Auto-commit changed files from 'make api-docs'
kibanamachine Nov 14, 2024
db8f88c
Merge branch 'main' into kb_tests
elasticmachine Nov 18, 2024
2efeb72
prcomments
stephmilovic Nov 18, 2024
2749902
Merge branch 'main' into kb_tests
stephmilovic Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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 { act, renderHook } from '@testing-library/react-hooks';
import {
useCreateKnowledgeBaseEntry,
UseCreateKnowledgeBaseEntryParams,
} from './use_create_knowledge_base_entry';
import { useInvalidateKnowledgeBaseEntries } from './use_knowledge_base_entries';

jest.mock('./use_knowledge_base_entries', () => ({
useInvalidateKnowledgeBaseEntries: jest.fn(),
}));

jest.mock('@tanstack/react-query', () => ({
useMutation: jest.fn().mockImplementation((queryKey, fn, opts) => {
return {
mutate: async (variables: unknown) => {
try {
const res = await fn(variables);
opts.onSuccess(res);
opts.onSettled();
return Promise.resolve(res);
} catch (e) {
opts.onError(e);
opts.onSettled();
}
},
};
}),
}));

const http = {
post: jest.fn(),
};
const toasts = {
addError: jest.fn(),
addSuccess: jest.fn(),
};
const defaultProps = { http, toasts } as unknown as UseCreateKnowledgeBaseEntryParams;
const defaultArgs = { title: 'Test Entry' };
describe('useCreateKnowledgeBaseEntry', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should call the mutation function on success', async () => {
const invalidateKnowledgeBaseEntries = jest.fn();
(useInvalidateKnowledgeBaseEntries as jest.Mock).mockReturnValue(
invalidateKnowledgeBaseEntries
);
http.post.mockResolvedValue({});

const { result } = renderHook(() => useCreateKnowledgeBaseEntry(defaultProps));

await act(async () => {
// @ts-ignore
await result.current.mutate(defaultArgs);
});

expect(http.post).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
body: JSON.stringify(defaultArgs),
})
);
expect(toasts.addSuccess).toHaveBeenCalledWith({
title: expect.any(String),
});
expect(invalidateKnowledgeBaseEntries).toHaveBeenCalled();
});

it('should call the onError function on error', async () => {
const error = new Error('Test Error');
http.post.mockRejectedValue(error);

const { result } = renderHook(() => useCreateKnowledgeBaseEntry(defaultProps));

await act(async () => {
// @ts-ignore
await result.current.mutate(defaultArgs);
});

expect(toasts.addError).toHaveBeenCalledWith(error, {
title: expect.any(String),
});
});

it('should call the onSettled function after mutation', async () => {
const invalidateKnowledgeBaseEntries = jest.fn();
(useInvalidateKnowledgeBaseEntries as jest.Mock).mockReturnValue(
invalidateKnowledgeBaseEntries
);
http.post.mockResolvedValue({});

const { result } = renderHook(() => useCreateKnowledgeBaseEntry(defaultProps));

await act(async () => {
// @ts-ignore
await result.current.mutate(defaultArgs);
});

expect(invalidateKnowledgeBaseEntries).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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 { act, renderHook } from '@testing-library/react-hooks';
import {
useDeleteKnowledgeBaseEntries,
UseDeleteKnowledgeEntriesParams,
} from './use_delete_knowledge_base_entries';
import { useInvalidateKnowledgeBaseEntries } from './use_knowledge_base_entries';

jest.mock('./use_knowledge_base_entries', () => ({
useInvalidateKnowledgeBaseEntries: jest.fn(),
}));

jest.mock('@tanstack/react-query', () => ({
useMutation: jest.fn().mockImplementation((queryKey, fn, opts) => {
return {
mutate: async (variables: unknown) => {
try {
const res = await fn(variables);
opts.onSuccess(res);
opts.onSettled();
return Promise.resolve(res);
} catch (e) {
opts.onError(e);
opts.onSettled();
}
},
};
}),
}));

const http = {
post: jest.fn(),
};
const toasts = {
addError: jest.fn(),
};
const defaultProps = { http, toasts } as unknown as UseDeleteKnowledgeEntriesParams;
const defaultArgs = { ids: ['1'], query: '' };

describe('useDeleteKnowledgeBaseEntries', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should call the mutation function on success', async () => {
const invalidateKnowledgeBaseEntries = jest.fn();
(useInvalidateKnowledgeBaseEntries as jest.Mock).mockReturnValue(
invalidateKnowledgeBaseEntries
);
http.post.mockResolvedValue({});

const { result } = renderHook(() => useDeleteKnowledgeBaseEntries(defaultProps));

await act(async () => {
// @ts-ignore
await result.current.mutate(defaultArgs);
});

expect(http.post).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
body: JSON.stringify({ delete: { query: '', ids: ['1'] } }),
version: '1',
})
);
expect(invalidateKnowledgeBaseEntries).toHaveBeenCalled();
});

it('should call the onError function on error', async () => {
const error = new Error('Test Error');
http.post.mockRejectedValue(error);

const { result } = renderHook(() => useDeleteKnowledgeBaseEntries(defaultProps));

await act(async () => {
// @ts-ignore
await result.current.mutate(defaultArgs);
});

expect(toasts.addError).toHaveBeenCalledWith(error, {
title: expect.any(String),
});
});

it('should call the onSettled function after mutation', async () => {
const invalidateKnowledgeBaseEntries = jest.fn();
(useInvalidateKnowledgeBaseEntries as jest.Mock).mockReturnValue(
invalidateKnowledgeBaseEntries
);
http.post.mockResolvedValue({});

const { result } = renderHook(() => useDeleteKnowledgeBaseEntries(defaultProps));

await act(async () => {
// @ts-ignore
await result.current.mutate(defaultArgs);
});

expect(invalidateKnowledgeBaseEntries).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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 { renderHook } from '@testing-library/react-hooks';
import { useKnowledgeBaseEntries } from './use_knowledge_base_entries';
import { HttpSetup } from '@kbn/core/public';
import { IToasts } from '@kbn/core-notifications-browser';
import { TestProviders } from '../../../../mock/test_providers/test_providers';

describe('useKnowledgeBaseEntries', () => {
const httpMock: HttpSetup = {
fetch: jest.fn(),
} as unknown as HttpSetup;
const toastsMock: IToasts = {
addError: jest.fn(),
} as unknown as IToasts;

it('fetches knowledge base entries successfully', async () => {
(httpMock.fetch as jest.Mock).mockResolvedValue({
page: 1,
perPage: 100,
total: 1,
data: [{ id: '1', title: 'Entry 1' }],
});

const { result, waitForNextUpdate } = renderHook(
() => useKnowledgeBaseEntries({ http: httpMock, enabled: true }),
{
wrapper: TestProviders,
}
);
expect(result.current.fetchStatus).toEqual('fetching');

await waitForNextUpdate();

expect(result.current.data).toEqual({
page: 1,
perPage: 100,
total: 1,
data: [{ id: '1', title: 'Entry 1' }],
});
});

it('handles fetch error', async () => {
const error = new Error('Fetch error');
(httpMock.fetch as jest.Mock).mockRejectedValue(error);

const { waitForNextUpdate } = renderHook(
() => useKnowledgeBaseEntries({ http: httpMock, toasts: toastsMock, enabled: true }),
{
wrapper: TestProviders,
}
);

await waitForNextUpdate();

expect(toastsMock.addError).toHaveBeenCalledWith(error, {
title: 'Error fetching Knowledge Base entries',
});
});

it('does not fetch when disabled', async () => {
const { result } = renderHook(
() => useKnowledgeBaseEntries({ http: httpMock, enabled: false }),
{
wrapper: TestProviders,
}
);

expect(result.current.fetchStatus).toEqual('idle');
});
});
Loading