Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
84 changes: 84 additions & 0 deletions src/web/hooks/use-query/__tests__/AgentGroups.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* SPDX-FileCopyrightText: 2026 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {describe, expect, test, testing} from '@gsa/testing';
import {fireEvent, rendererWith, screen, waitFor} from 'web/testing';
import CollectionCounts from 'gmp/collection/collection-counts';
import QueryFilter from 'gmp/models/filter/query-filter';
import {createSession} from 'gmp/testing';
import {
useCloneAgentGroup,
useCreateAgentGroup,
useDeleteAgentGroup,
useGetAgentGroups,
useSaveAgentGroup,
} from 'web/hooks/use-query/agent-groups';

const filter = QueryFilter.fromString('name~group');
const group = {id: 'group-1', name: 'Agent Group 1'};

const createGmp = () => ({
session: createSession({token: 'test-token'}),
settings: {},
agentgroups: {
get: testing.fn().mockResolvedValue({
data: [group],
meta: {
filter,
counts: new CollectionCounts({all: 1, filtered: 1, length: 1}),
},
}),
},
agentgroup: {
create: testing.fn().mockResolvedValue({data: {id: 'group-2'}}),
clone: testing.fn().mockResolvedValue({data: {id: 'group-2'}}),
save: testing.fn().mockResolvedValue(undefined),
delete: testing.fn().mockResolvedValue(undefined),
},
});

describe('agent group query hooks', () => {
test('should fetch agent groups with a filter', async () => {
const gmp = createGmp();
const {render} = rendererWith({gmp, router: true});

const TestComponent = () => {
const {data} = useGetAgentGroups({filter});
return <div data-testid="group">{data?.entities[0]?.name}</div>;
};

render(<TestComponent />);

await waitFor(() => {
expect(screen.getByTestId('group')).toHaveTextContent('Agent Group 1');
});

expect(gmp.agentgroups.get).toHaveBeenCalledWith({filter});
});

test.each([
['create', useCreateAgentGroup, 'create', {name: 'New Group'}],
['clone', useCloneAgentGroup, 'clone', {id: 'group-1'}],
['save', useSaveAgentGroup, 'save', {id: 'group-1', name: 'Saved Group'}],
['delete', useDeleteAgentGroup, 'delete', {id: 'group-1'}],
])('should %s an agent group', async (_name, useMutation, method, input) => {
const gmp = createGmp();
const {render} = rendererWith({gmp, router: true});

const TestComponent = () => {
const mutation = useMutation({});
return (
<button onClick={() => mutation.mutate(input as never)}>Run</button>
);
};

render(<TestComponent />);
fireEvent.click(screen.getByRole('button', {name: 'Run'}));

await waitFor(() => {
expect(gmp.agentgroup[method].mock.calls[0][0]).toEqual(input);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* SPDX-FileCopyrightText: 2026 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {describe, expect, test, testing} from '@gsa/testing';
import {rendererWith, screen, waitFor} from 'web/testing';
import {createSession} from 'gmp/testing';
import useGetInstallInstructions from 'web/hooks/use-query/agent-install-instructions';
import {type InstallInstructionsData} from 'web/pages/agent-remote-installer/types';

const instructions: InstallInstructionsData = {
_version: '1.0',
lang: 'de',
title: 'Agent Installation',
sections: [],
};

const InstructionsComponent = ({
enabled,
scannerId,
}: {
enabled?: boolean;
scannerId?: string;
}) => {
const {data} = useGetInstallInstructions({enabled, scannerId});

return <div data-testid="title">{data?.title}</div>;
};

const createGmp = ({token}: {token?: string} = {token: 'test-token'}) => ({
session: createSession({token}),
settings: {},
agentinstallersinstructions: {
getInstructions: testing.fn().mockResolvedValue({data: instructions}),
},
});

describe('useGetInstallInstructions', () => {
test('should fetch installation instructions', async () => {
const gmp = createGmp();
const {render} = rendererWith({
gmp,
language: 'de-DE',
router: true,
});

render(<InstructionsComponent scannerId="scanner-1" />);

await waitFor(() => {
expect(screen.getByTestId('title')).toHaveTextContent(
'Agent Installation',
);
});

expect(
gmp.agentinstallersinstructions.getInstructions,
).toHaveBeenCalledWith({
lang: 'de',
scannerId: 'scanner-1',
originUrl: globalThis.location.origin,
});
});

test('should not fetch when there is no session token', () => {
const gmp = createGmp({token: undefined});
const {render} = rendererWith({gmp, router: true});

render(<InstructionsComponent />);

expect(
gmp.agentinstallersinstructions.getInstructions,
).not.toHaveBeenCalled();
});

test('should not fetch when disabled', () => {
const gmp = createGmp();
const {render} = rendererWith({gmp, router: true});

render(<InstructionsComponent enabled={false} />);

expect(
gmp.agentinstallersinstructions.getInstructions,
).not.toHaveBeenCalled();
});
});
82 changes: 82 additions & 0 deletions src/web/hooks/use-query/__tests__/AgentInstallers.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* SPDX-FileCopyrightText: 2026 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {describe, expect, test, testing} from '@gsa/testing';
import {rendererWith, screen, waitFor} from 'web/testing';
import CollectionCounts from 'gmp/collection/collection-counts';
import AgentInstaller from 'gmp/models/agent-installer';
import QueryFilter from 'gmp/models/filter/query-filter';
import {createSession} from 'gmp/testing';
import {useGetAgentInstallers} from 'web/hooks/use-query/agent-installers';

const filter = QueryFilter.fromString('name~agent');

const installers = [
new AgentInstaller({id: 'installer-1', name: 'Agent Installer 1'}),
new AgentInstaller({id: 'installer-2', name: 'Agent Installer 2'}),
];

const InstallersComponent = ({enabled}: {enabled?: boolean}) => {
const {data} = useGetAgentInstallers({enabled, filter});

return (
<div data-testid="installers">
{data?.entities.map(installer => (
<div key={installer.id} data-testid="installer">
{installer.name}
</div>
))}
</div>
);
};

const createGmp = ({token}: {token?: string} = {token: 'test-token'}) => ({
session: createSession({token}),
settings: {},
agentinstallers: {
get: testing.fn().mockResolvedValue({
data: installers,
meta: {
filter,
counts: new CollectionCounts({all: 2, filtered: 2, length: 2}),
},
}),
},
});

describe('useGetAgentInstallers', () => {
test('should fetch agent installers with a filter', async () => {
const gmp = createGmp();
const {render} = rendererWith({gmp, router: true});

render(<InstallersComponent />);

await waitFor(() => {
expect(screen.getAllByTestId('installer')).toHaveLength(2);
});

expect(gmp.agentinstallers.get).toHaveBeenCalledWith({filter});
expect(screen.getByText('Agent Installer 1')).toBeInTheDocument();
expect(screen.getByText('Agent Installer 2')).toBeInTheDocument();
});

test('should not fetch when there is no session token', () => {
const gmp = createGmp({token: undefined});
const {render} = rendererWith({gmp, router: true});

render(<InstallersComponent />);

expect(gmp.agentinstallers.get).not.toHaveBeenCalled();
});

test('should not fetch when disabled', () => {
const gmp = createGmp();
const {render} = rendererWith({gmp, router: true});

render(<InstallersComponent enabled={false} />);

expect(gmp.agentinstallers.get).not.toHaveBeenCalled();
});
});
Loading
Loading