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

Create alerts via graphql #2425

Merged
merged 30 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3189bcb
Export alert field arrays
saberlynx Aug 20, 2020
ecc13d3
Add convertDict function and fix import
saberlynx Aug 20, 2020
1199deb
Fix handleSaveAlert args
saberlynx Aug 20, 2020
e651320
Add convertConditionEnum
saberlynx Aug 20, 2020
0cd8a98
[Add support for event enum
saberlynx Aug 20, 2020
63acff9
Support method enum
saberlynx Aug 20, 2020
1577073
Add all event enum types
saberlynx Aug 20, 2020
b7d4b73
Encode sourcefire connector file in base64
saberlynx Aug 24, 2020
5ccc53d
Get rid of data url declaration
saberlynx Aug 24, 2020
aca9ece
Refetch alerts
saberlynx Aug 25, 2020
7af8d61
Parse secinfo type and direction enum
saberlynx Aug 26, 2020
ce1c36c
Convert feed event enum
saberlynx Aug 26, 2020
a3eb278
Support task status enum
saberlynx Aug 26, 2020
596ed4a
Support DeltaType enum
saberlynx Aug 26, 2020
b4426f4
Return null if delta_report_id is emptystring
saberlynx Aug 26, 2020
d241bf2
Parse ports into int
saberlynx Aug 26, 2020
bcd7c2f
Make create alert mocks
saberlynx Aug 27, 2020
4fb1aa0
add create alert test
saberlynx Aug 27, 2020
111da82
Remove console logs
saberlynx Aug 27, 2020
38ae959
Use isString to parse ports
saberlynx Aug 27, 2020
71db437
Fix linter warning
saberlynx Aug 27, 2020
cce6636
Correct name of button in test component
saberlynx Aug 27, 2020
5da9d9a
Update CHANGELOG.md
saberlynx Aug 27, 2020
22fcafa
Separate parsing functions into own import
saberlynx Aug 27, 2020
3420b0a
Add event, condition and method enum tests
saberlynx Aug 27, 2020
5158990
Test all enums
saberlynx Aug 27, 2020
1622b28
Test convert methoddata
saberlynx Aug 27, 2020
e830a9d
Test condition data and event data
saberlynx Aug 27, 2020
f79d1e4
Merge branch 'master' into create-alerts
saberlynx Aug 28, 2020
aba80b7
Merge branch 'master' into create-alerts
saberlynx Aug 28, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [21.04] - unreleased

### Added
- Create alerts in task dialog via graphql [#2425](https://github.com/greenbone/gsa/pull/2425)
- Added missing fields for getScanners query and parseObject() for scanner model [#2301](https://github.com/greenbone/gsa/pull/2301)

### Changed
Expand Down
6 changes: 3 additions & 3 deletions gsa/src/gmp/commands/alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import EntityCommand from './entity';

const log = logger.getLogger('gmp.commands.alerts');

const event_data_fields = ['status', 'feed_event', 'secinfo_type'];
const method_data_fields = [
export const event_data_fields = ['status', 'feed_event', 'secinfo_type'];
export const method_data_fields = [
'composer_include_notes',
'composer_include_overrides',
'details_url',
Expand Down Expand Up @@ -88,7 +88,7 @@ const method_data_fields = [
'vfire_call_impact_name',
'vfire_call_urgency_name',
];
const condition_data_fields = [
export const condition_data_fields = [
'severity',
'direction',
'at_least_filter_id',
Expand Down
29 changes: 28 additions & 1 deletion gsa/src/web/graphql/__mocks__/alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import {deepFreeze, createGenericQueryMock} from 'web/utils/testing';

import {GET_ALERTS} from '../alerts';
import {GET_ALERTS, CREATE_ALERT} from '../alerts';

const alert1 = deepFreeze({
id: '1',
Expand Down Expand Up @@ -120,3 +120,30 @@ const mockAlerts = {

export const createGetAlertsQueryMock = () =>
createGenericQueryMock(GET_ALERTS, {alerts: mockAlerts});

const createAlertResult = {
createAlert: {
id: '12345',
status: 200,
},
};

export const createAlertInput = {
name: 'foo',
event: 'NEW_SECINFO_ARRIVED',
condition: 'ALWAYS',
method: 'HTTP_GET',
methodData: {
URL: 'yourdomain.com',
composer_include_notes: 1,
composer_include_overrides: 0,
Comment on lines +138 to +139
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not be send via the api. At least not as methodData. We need to think about the api changes for the content composer sometime in future.

delta_report_id: '23456',
delta_type: 'PREVIOUS',
details_url: 'https://secinfo.greenbone.net/etc',
},
};

export const createCreateAlertQueryMock = () =>
createGenericQueryMock(CREATE_ALERT, createAlertResult, {
input: createAlertInput,
});
51 changes: 48 additions & 3 deletions gsa/src/web/graphql/__tests__/alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import React from 'react';
import React, {useState} from 'react';

import {isDefined} from 'gmp/utils/identity';
import Button from 'web/components/form/button';

import {rendererWith, screen, wait, fireEvent} from 'web/utils/testing';

import {useLazyGetAlerts} from '../alerts';
import {createGetAlertsQueryMock} from '../__mocks__/alerts';
import {useLazyGetAlerts, useCreateAlert} from '../alerts';
import {
createGetAlertsQueryMock,
createCreateAlertQueryMock,
createAlertInput,
} from '../__mocks__/alerts';

const GetLazyAlertsComponent = () => {
const [getAlerts, {counts, loading, alerts}] = useLazyGetAlerts();
Expand Down Expand Up @@ -100,3 +105,43 @@ describe('useLazyGetAlert tests', () => {
expect(screen.getByTestId('length')).toHaveTextContent(2);
});
});

const CreateAlertComponent = () => {
const [notification, setNotification] = useState('');

const [createAlert] = useCreateAlert();

const handleCreateResult = id => {
setNotification(`Alert created with id ${id}.`);
};

return (
<div>
<Button
title={'Create alert'}
onClick={() => createAlert(createAlertInput).then(handleCreateResult)}
/>
<h3 data-testid="notification">{notification}</h3>
</div>
);
};

describe('useCreateAlert test', () => {
test('should create an alert', async () => {
const [queryMock, resultFunc] = createCreateAlertQueryMock();

const {render} = rendererWith({queryMocks: [queryMock]});

const {element} = render(<CreateAlertComponent />);

const buttons = element.querySelectorAll('button');
fireEvent.click(buttons[0]);

await wait();

expect(resultFunc).toHaveBeenCalled();
expect(screen.getByTestId('notification')).toHaveTextContent(
'Alert created with id 12345.',
);
});
});
27 changes: 26 additions & 1 deletion gsa/src/web/graphql/alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {useCallback} from 'react';

import gql from 'graphql-tag';

import {useLazyQuery} from '@apollo/react-hooks';
import {useLazyQuery, useMutation} from '@apollo/react-hooks';

import CollectionCounts from 'gmp/collection/collectioncounts';

Expand Down Expand Up @@ -129,3 +129,28 @@ export const useLazyGetAlerts = (variables, options) => {
const pageInfo = data?.alerts?.pageInfo;
return [getAlerts, {...other, counts, alerts, pageInfo}];
};

export const CREATE_ALERT = gql`
mutation createAlert($input: CreateAlertInput!) {
createAlert(input: $input) {
id
}
}
`;

export const useCreateAlert = options => {
const [queryCreateAlert, {data, ...other}] = useMutation(
CREATE_ALERT,
options,
);
const createAlert = useCallback(
// eslint-disable-next-line no-shadow
(inputObject, options) =>
queryCreateAlert({...options, variables: {input: inputObject}}).then(
result => result.data.createAlert.id,
),
[queryCreateAlert],
);
const alertId = data?.createAlert?.id;
return [createAlert, {...other, id: alertId}];
};
Loading