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

Adjust task icons to work for audits and remove audit icons #2070

Merged
merged 12 commits into from
Mar 27, 2020
180 changes: 177 additions & 3 deletions gsa/src/web/pages/audits/__tests__/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Audit, {AUDIT_STATUS} from 'gmp/models/audit';
setLocale('en');

const caps = new Capabilities(['everything']);
const wrongCaps = new Capabilities(['authenticate']);
const wrongCaps = new Capabilities(['get_task']);

describe('Audit Actions tests', () => {
// deactivate console.error for tests
Expand All @@ -45,6 +45,7 @@ describe('Audit Actions tests', () => {
last_report: {report: {_id: 'id'}},
permissions: {permission: [{name: 'everything'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('Audit Actions tests', () => {
last_report: {report: {_id: 'id'}},
permissions: {permission: [{name: 'everything'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -150,8 +152,9 @@ describe('Audit Actions tests', () => {
status: AUDIT_STATUS.done,
alterable: '0',
last_report: {report: {_id: 'id'}},
permissions: {permission: [{name: 'authenticate'}]},
permissions: {permission: [{name: 'get_task'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -186,7 +189,174 @@ describe('Audit Actions tests', () => {
expect(handleAuditStart).not.toHaveBeenCalledWith(audit);
expect(icons[0]).toHaveAttribute(
'title',
'Permission to start Audit denied',
'Permission to start audit denied',
);

fireEvent.click(icons[1]);
expect(handleAuditResume).not.toHaveBeenCalledWith(audit);
expect(icons[1]).toHaveAttribute('title', 'Audit is not stopped');

fireEvent.click(icons[2]);
expect(handleAuditDelete).not.toHaveBeenCalledWith(audit);
expect(icons[2]).toHaveAttribute(
'title',
'Permission to move Audit to trashcan denied',
);

fireEvent.click(icons[3]);
expect(handleAuditEdit).not.toHaveBeenCalledWith(audit);
expect(icons[3]).toHaveAttribute(
'title',
'Permission to edit Audit denied',
);

fireEvent.click(icons[4]);
expect(handleAuditClone).not.toHaveBeenCalledWith(audit);
expect(icons[4]).toHaveAttribute(
'title',
'Permission to clone Audit denied',
);

fireEvent.click(icons[5]);
expect(handleAuditDownload).toHaveBeenCalledWith(audit);
expect(icons[5]).toHaveAttribute('title', 'Export Audit');

fireEvent.click(icons[6]);
expect(handleReportDownload).toHaveBeenCalledWith(audit);
expect(icons[6]).toHaveAttribute(
'title',
'Download Greenbone Compliance Report',
);
});

test('should not call click handlers for stopped audit without permissions', () => {
const audit = Audit.fromElement({
status: AUDIT_STATUS.stopped,
alterable: '0',
last_report: {report: {_id: 'id'}},
permissions: {permission: [{name: 'get_task'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
const handleAuditDelete = jest.fn();
const handleAuditDownload = jest.fn();
const handleAuditEdit = jest.fn();
const handleAuditResume = jest.fn();
const handleAuditStart = jest.fn();
const handleAuditStop = jest.fn();
const handleReportDownload = jest.fn();

const {render} = rendererWith({capabilities: wrongCaps});
const {getAllByTestId} = render(
<Actions
entity={audit}
gcrFormatDefined={true}
links={true}
onAuditCloneClick={handleAuditClone}
onAuditDeleteClick={handleAuditDelete}
onAuditDownloadClick={handleAuditDownload}
onAuditEditClick={handleAuditEdit}
onAuditResumeClick={handleAuditResume}
onAuditStartClick={handleAuditStart}
onAuditStopClick={handleAuditStop}
onReportDownloadClick={handleReportDownload}
/>,
);

const icons = getAllByTestId('svg-icon');

fireEvent.click(icons[0]);
expect(handleAuditStart).not.toHaveBeenCalledWith(audit);
sarahd93 marked this conversation as resolved.
Show resolved Hide resolved
expect(icons[0]).toHaveAttribute(
'title',
'Permission to start audit denied',
);

fireEvent.click(icons[1]);
expect(handleAuditResume).not.toHaveBeenCalledWith(audit);
expect(icons[1]).toHaveAttribute(
'title',
'Permission to resume audit denied',
);

fireEvent.click(icons[2]);
expect(handleAuditDelete).not.toHaveBeenCalledWith(audit);
expect(icons[2]).toHaveAttribute(
'title',
'Permission to move Audit to trashcan denied',
);

fireEvent.click(icons[3]);
expect(handleAuditEdit).not.toHaveBeenCalledWith(audit);
expect(icons[3]).toHaveAttribute(
'title',
'Permission to edit Audit denied',
);

fireEvent.click(icons[4]);
expect(handleAuditClone).not.toHaveBeenCalledWith(audit);
expect(icons[4]).toHaveAttribute(
'title',
'Permission to clone Audit denied',
);

fireEvent.click(icons[5]);
expect(handleAuditDownload).toHaveBeenCalledWith(audit);
expect(icons[5]).toHaveAttribute('title', 'Export Audit');

fireEvent.click(icons[6]);
expect(handleReportDownload).toHaveBeenCalledWith(audit);
expect(icons[6]).toHaveAttribute(
'title',
'Download Greenbone Compliance Report',
);
});

test('should not call click handlers for running audit without permissions', () => {
const audit = Audit.fromElement({
status: AUDIT_STATUS.running,
alterable: '0',
last_report: {report: {_id: 'id'}},
permissions: {permission: [{name: 'get_task'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
const handleAuditDelete = jest.fn();
const handleAuditDownload = jest.fn();
const handleAuditEdit = jest.fn();
const handleAuditResume = jest.fn();
const handleAuditStart = jest.fn();
const handleAuditStop = jest.fn();
const handleReportDownload = jest.fn();

const {render} = rendererWith({capabilities: wrongCaps});
const {getAllByTestId} = render(
<Actions
entity={audit}
gcrFormatDefined={true}
links={true}
onAuditCloneClick={handleAuditClone}
onAuditDeleteClick={handleAuditDelete}
onAuditDownloadClick={handleAuditDownload}
onAuditEditClick={handleAuditEdit}
onAuditResumeClick={handleAuditResume}
onAuditStartClick={handleAuditStart}
onAuditStopClick={handleAuditStop}
onReportDownloadClick={handleReportDownload}
/>,
);

const icons = getAllByTestId('svg-icon');

fireEvent.click(icons[0]);
expect(handleAuditStop).not.toHaveBeenCalledWith(audit);
sarahd93 marked this conversation as resolved.
Show resolved Hide resolved
expect(icons[0]).toHaveAttribute(
'title',
'Permission to stop audit denied',
);

fireEvent.click(icons[1]);
Expand Down Expand Up @@ -233,6 +403,7 @@ describe('Audit Actions tests', () => {
in_use: true,
permissions: {permission: [{name: 'everything'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -300,6 +471,7 @@ describe('Audit Actions tests', () => {
last_report: {report: {_id: 'id'}},
permissions: {permission: [{name: 'everything'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -369,6 +541,7 @@ describe('Audit Actions tests', () => {
last_report: {report: {_id: 'id'}},
permissions: {permission: [{name: 'everything'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -419,6 +592,7 @@ describe('Audit Actions tests', () => {
name: 'schedule1',
permissions: {permission: [{name: 'everything'}]},
},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down
19 changes: 16 additions & 3 deletions gsa/src/web/pages/audits/__tests__/detailspage.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ const policy = Policy.fromElement({
scanner: {name: 'scanner1', type: '0'},
policy_type: OPENVAS_SCAN_CONFIG_TYPE,
tasks: {
task: [{id: '12345', name: 'foo'}, {id: '678910', name: 'audit2'}],
task: [
{id: '12345', name: 'foo'},
{id: '678910', name: 'audit2'},
],
},
});

Expand Down Expand Up @@ -113,6 +116,7 @@ const audit = Audit.fromElement({
scanner: {_id: '1516', name: 'scanner1', type: '2'},
config: policy,
preferences: preferences,
usage_type: 'audit',
});

const audit2 = Audit.fromElement({
Expand All @@ -133,6 +137,7 @@ const audit2 = Audit.fromElement({
scanner: {_id: '1516', name: 'scanner1', type: '2'},
config: policy,
preferences: preferences,
usage_type: 'audit',
});

const audit3 = Audit.fromElement({
Expand All @@ -152,6 +157,7 @@ const audit3 = Audit.fromElement({
scanner: {_id: '1516', name: 'scanner1', type: '2'},
config: policy,
preferences: preferences,
usage_type: 'audit',
});

const audit4 = Audit.fromElement({
Expand All @@ -173,6 +179,7 @@ const audit4 = Audit.fromElement({
scanner: {_id: '1516', name: 'scanner1', type: '2'},
config: policy,
preferences: preferences,
usage_type: 'audit',
});

const audit5 = Audit.fromElement({
Expand All @@ -194,6 +201,7 @@ const audit5 = Audit.fromElement({
scanner: {_id: '1516', name: 'scanner1', type: '2'},
config: policy,
preferences: preferences,
usage_type: 'audit',
});

const audit5Id = {
Expand All @@ -218,6 +226,7 @@ const audit6 = Audit.fromElement({
scanner: {_id: '1516', name: 'scanner1', type: '2'},
config: policy,
preferences: preferences,
usage_type: 'audit',
});

const audit7 = Audit.fromElement({
Expand All @@ -244,6 +253,7 @@ const audit7 = Audit.fromElement({
},
schedule_periods: '1',
preferences: preferences,
usage_type: 'audit',
});

const caps = new Capabilities(['everything']);
Expand Down Expand Up @@ -903,8 +913,11 @@ describe('Audit ToolBarIcons tests', () => {
expect(icons[5]).toHaveAttribute('title', 'Export Audit as XML');

fireEvent.click(icons[6]);
expect(handleAuditStartClick).toHaveBeenCalledWith(audit6);
expect(icons[6]).toHaveAttribute('title', 'Start');
expect(handleAuditStartClick).not.toHaveBeenCalled();
expect(icons[6]).toHaveAttribute(
'title',
'Permission to start audit denied',
);

fireEvent.click(icons[7]);
expect(handleAuditResumeClick).not.toHaveBeenCalled();
Expand Down
14 changes: 12 additions & 2 deletions gsa/src/web/pages/audits/__tests__/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('Audit Row tests', () => {
last_report: lastReport,
permissions: {permission: [{name: 'everything'}]},
target: {_id: '5678', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -159,6 +160,7 @@ describe('Audit Row tests', () => {
target: {_id: 'id', name: 'target'},
scanner: {_id: 'id', name: 'scanner', type: GMP_SCANNER_TYPE},
observers: 'user',
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -221,6 +223,7 @@ describe('Audit Row tests', () => {
alterable: '0',
permissions: {permission: [{name: 'everything'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -323,6 +326,7 @@ describe('Audit Row tests', () => {
current_report: currentReport,
permissions: {permission: [{name: 'everything'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -430,6 +434,7 @@ describe('Audit Row tests', () => {
last_report: lastReport,
permissions: {permission: [{name: 'everything'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -539,6 +544,7 @@ describe('Audit Row tests', () => {
last_report: lastReport,
permissions: {permission: [{name: 'everything'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -648,6 +654,7 @@ describe('Audit Row tests', () => {
last_report: lastReport,
permissions: {permission: [{name: 'get_tasks'}]},
target: {_id: 'id', name: 'target'},
usage_type: 'audit',
});

const handleAuditClone = jest.fn();
Expand Down Expand Up @@ -716,8 +723,11 @@ describe('Audit Row tests', () => {

// Actions
fireEvent.click(icons[1]);
expect(handleAuditStart).toHaveBeenCalledWith(audit);
expect(icons[1]).toHaveAttribute('title', 'Start');
expect(handleAuditStart).not.toHaveBeenCalled();
expect(icons[1]).toHaveAttribute(
'title',
'Permission to start audit denied',
);

fireEvent.click(icons[2]);
expect(handleAuditResume).not.toHaveBeenCalled();
Expand Down
Loading