Skip to content

Commit

Permalink
Merge pull request #2425 from saberlynx/create-alerts
Browse files Browse the repository at this point in the history
Create alerts via graphql
  • Loading branch information
bjoernricks authored Aug 28, 2020
2 parents e51bc9f + aba80b7 commit 2e5e036
Show file tree
Hide file tree
Showing 9 changed files with 713 additions and 19 deletions.
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,
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

0 comments on commit 2e5e036

Please sign in to comment.