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
22 changes: 15 additions & 7 deletions src/gmp/collection/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ export interface CollectionList<TModel> {
counts: CollectionCounts;
}

export type EntitiesParseFunc<TModel extends Model, TElement = Element> = (
element: TElement,
name: string,
modelClass: ModelClass<TModel>,
) => TModel[];

interface ParseCollectionListOptions<TModel extends Model, TElement = Element> {
pluralName?: string;
entitiesParseFunc?: (
element: TElement,
name: string,
modelClass: ModelClass<TModel>,
) => TModel[];
entitiesParseFunc?: EntitiesParseFunc<TModel, TElement>;
collectionCountParseFunc?: (
element: TElement,
name: string,
Expand All @@ -37,7 +39,7 @@ interface ParseCollectionListOptions<TModel extends Model, TElement = Element> {
filterParseFunc?: (element: FilterElement) => Filter;
}

interface InfoElement {
export interface InfoElement {
info?: Element[];
}

Expand Down Expand Up @@ -68,13 +70,19 @@ interface InfoWithCounts extends InfoElement {
info_count?: ElementCounts;
}

export type InfoEntitiesFilterFunc = (
value: Element,
index: number,
array: Element[],
) => boolean;

const log = logger.getLogger('gmp.collection.parser');

export function parseInfoEntities<TModel extends Model>(
response: InfoElement,
_name: string,
modelClass: ModelClass<TModel>,
filterFunc: (value: Element, index: number, array: Element[]) => boolean,
filterFunc: InfoEntitiesFilterFunc,
) {
if (!isArray(response.info)) {
return [];
Expand Down
159 changes: 159 additions & 0 deletions src/gmp/commands/__tests__/cert-bund-advisories.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/* SPDX-FileCopyrightText: 2025 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {describe, test, expect} from '@gsa/testing';
import CertBundAdvisoriesCommand from 'gmp/commands/cert-bund-advisories';
import {
createAggregatesResponse,
createHttp,
createInfoEntitiesResponse,
} from 'gmp/commands/testing';
import CertBundAdv from 'gmp/models/cert-bund';

describe('CertBundAdvisoriesCommand tests', () => {
test('should fetch cert bund advisories with default params', async () => {
const response = createInfoEntitiesResponse([
{
_id: '1',
name: 'Admin',
cert_bund_adv: {
severity: 10.0,
},
},
{
_id: '2',
name: 'User',
cert_bund_adv: {
severity: 5.0,
},
},
]);
const fakeHttp = createHttp(response);

const cmd = new CertBundAdvisoriesCommand(fakeHttp);
const result = await cmd.get();
expect(fakeHttp.request).toHaveBeenCalledWith('get', {
args: {cmd: 'get_info', info_type: 'cert_bund_adv'},
});
expect(result.data).toEqual([
new CertBundAdv({
id: '1',
name: 'Admin',
severity: 10.0,
}),
new CertBundAdv({
id: '2',
name: 'User',
severity: 5.0,
}),
]);
});

test('should fetch cert bund advisories with custom params', async () => {
const response = createInfoEntitiesResponse([
{
_id: '1',
name: 'Admin',
cert_bund_adv: {
severity: 10.0,
},
},
]);
const fakeHttp = createHttp(response);

const cmd = new CertBundAdvisoriesCommand(fakeHttp);
const result = await cmd.get({filter: "name='Admin'"});
expect(fakeHttp.request).toHaveBeenCalledWith('get', {
args: {
cmd: 'get_info',
info_type: 'cert_bund_adv',
filter: "name='Admin'",
},
});
expect(result.data).toEqual([
new CertBundAdv({
id: '1',
name: 'Admin',
severity: 10.0,
}),
]);
});

test('should fetch all cert bund advisories', async () => {
const response = createInfoEntitiesResponse([
{
_id: '1',
name: 'Admin',
cert_bund_adv: {
severity: 10.0,
},
},
{
_id: '2',
name: 'User',
cert_bund_adv: {
severity: 5.0,
},
},
]);
const fakeHttp = createHttp(response);

const cmd = new CertBundAdvisoriesCommand(fakeHttp);
const result = await cmd.getAll();
expect(fakeHttp.request).toHaveBeenCalledWith('get', {
args: {
cmd: 'get_info',
info_type: 'cert_bund_adv',
filter: 'first=1 rows=-1',
},
});
expect(result.data).toEqual([
new CertBundAdv({
id: '1',
name: 'Admin',
severity: 10.0,
}),
new CertBundAdv({
id: '2',
name: 'User',
severity: 5.0,
}),
]);
});

test('should fetch severity aggregates', async () => {
const response = createAggregatesResponse({});
const fakeHttp = createHttp(response);

const cmd = new CertBundAdvisoriesCommand(fakeHttp);
const result = await cmd.getSeverityAggregates();
expect(fakeHttp.request).toHaveBeenCalledWith('get', {
args: {
cmd: 'get_aggregate',
aggregate_type: 'cert_bund_adv',
group_column: 'severity',
info_type: 'cert_bund_adv',
},
});
expect(result.data).toEqual({groups: []});
});

test('should fetch created aggregates', async () => {
const response = createAggregatesResponse({});
const fakeHttp = createHttp(response);

const cmd = new CertBundAdvisoriesCommand(fakeHttp);
const result = await cmd.getCreatedAggregates();
expect(fakeHttp.request).toHaveBeenCalledWith('get', {
args: {
cmd: 'get_aggregate',
aggregate_type: 'cert_bund_adv',
group_column: 'created',
info_type: 'cert_bund_adv',
},
});
expect(result.data).toEqual({groups: []});
});
});
69 changes: 69 additions & 0 deletions src/gmp/commands/__tests__/cert-bund-advisory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* SPDX-FileCopyrightText: 2025 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {describe, test, expect} from '@gsa/testing';
import CertBundAdvisoryCommand from 'gmp/commands/cert-bund-advisory';
import {
createActionResultResponse,
createHttp,
createInfoResponse,
} from 'gmp/commands/testing';

describe('CertBundAdvisoryCommand tests', () => {
test('should get a cert bund advisory', async () => {
const response = createInfoResponse({
id: '123',
name: 'Test advisory',
comment: 'A test advisory',
});
const fakeHttp = createHttp(response);
const cmd = new CertBundAdvisoryCommand(fakeHttp);
const result = await cmd.get({
id: '123',
});
expect(fakeHttp.request).toHaveBeenCalledWith('get', {
args: {
cmd: 'get_info',
details: '1',
info_type: 'cert_bund_adv',
info_id: '123',
},
});
expect(result.data.id).toEqual('123');
});

test('should allow to clone a cert bund advisory', async () => {
const response = createActionResultResponse({id: '456'});
const fakeHttp = createHttp(response);
const cmd = new CertBundAdvisoryCommand(fakeHttp);
const result = await cmd.clone({id: '123'});
expect(fakeHttp.request).toHaveBeenCalledWith('post', {
data: {
cmd: 'clone',
details: '1',
info_type: 'cert_bund_adv',
id: '123',
resource_type: 'info',
},
});
expect(result.data.id).toEqual('456');
});

test('should allow to delete a cert bund advisory', async () => {
const response = createActionResultResponse({id: '123'});
const fakeHttp = createHttp(response);
const cmd = new CertBundAdvisoryCommand(fakeHttp);
const result = await cmd.delete({id: '123'});
expect(fakeHttp.request).toHaveBeenCalledWith('post', {
data: {
cmd: 'delete_info',
details: '1',
info_type: 'cert_bund_adv',
info_id: '123',
},
});
expect(result).toBeUndefined();
});
});
70 changes: 70 additions & 0 deletions src/gmp/commands/__tests__/cpe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* SPDX-FileCopyrightText: 2025 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {describe, test, expect} from '@gsa/testing';
import CpeCommand from 'gmp/commands/cpe';
import {
createActionResultResponse,
createHttp,
createInfoResponse,
} from 'gmp/commands/testing';

describe('CpeCommand tests', () => {
test('should get a cpe', async () => {
const response = createInfoResponse({
id: '123',
name: 'Test cpe',
cpe: 'cpe:/a:test:test:1.0',
comment: 'A test cpe',
});
const fakeHttp = createHttp(response);
const cmd = new CpeCommand(fakeHttp);
const result = await cmd.get({
id: '123',
});
expect(fakeHttp.request).toHaveBeenCalledWith('get', {
args: {
cmd: 'get_info',
details: '1',
info_type: 'cpe',
info_id: '123',
},
});
expect(result.data.id).toEqual('123');
});

test('should allow to clone a cpe', async () => {
const response = createActionResultResponse({id: '456'});
const fakeHttp = createHttp(response);
const cmd = new CpeCommand(fakeHttp);
const result = await cmd.clone({id: '123'});
expect(fakeHttp.request).toHaveBeenCalledWith('post', {
data: {
cmd: 'clone',
details: '1',
info_type: 'cpe',
id: '123',
resource_type: 'info',
},
});
expect(result.data.id).toEqual('456');
});

test('should allow to delete a cpe', async () => {
const response = createActionResultResponse({id: '123'});
const fakeHttp = createHttp(response);
const cmd = new CpeCommand(fakeHttp);
const result = await cmd.delete({id: '123'});
expect(fakeHttp.request).toHaveBeenCalledWith('post', {
data: {
cmd: 'delete_info',
details: '1',
info_type: 'cpe',
info_id: '123',
},
});
expect(result).toBeUndefined();
});
});
Loading