123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- import { dateTime } from '@grafana/data';
- import { AlertRuleDTO, AlertRulesState, NotificationChannelState, NotifierDTO } from 'app/types';
- import { reducerTester } from '../../../../test/core/redux/reducerTester';
- import {
- alertRulesReducer,
- initialChannelState,
- initialState,
- loadAlertRules,
- loadedAlertRules,
- notificationChannelReducer,
- setSearchQuery,
- notificationChannelLoaded,
- } from './reducers';
- describe('Alert rules', () => {
- const realDateNow = Date.now.bind(global.Date);
- const anchorUnix = dateTime('2019-09-04T10:01:01+02:00').valueOf();
- const dateNowStub = jest.fn(() => anchorUnix);
- global.Date.now = dateNowStub;
- const newStateDate = dateTime().subtract(1, 'y');
- const newStateDateFormatted = newStateDate.format('YYYY-MM-DD');
- const newStateDateAge = newStateDate.fromNow(true);
- const payload: AlertRuleDTO[] = [
- {
- id: 2,
- dashboardId: 7,
- dashboardUid: 'ggHbN42mk',
- dashboardSlug: 'alerting-with-testdata',
- panelId: 4,
- name: 'TestData - Always Alerting',
- state: 'alerting',
- newStateDate: `${newStateDateFormatted}T10:00:30+02:00`,
- evalDate: '0001-01-01T00:00:00Z',
- evalData: { evalMatches: [{ metric: 'A-series', tags: null, value: 215 }] },
- executionError: '',
- url: '/d/ggHbN42mk/alerting-with-testdata',
- },
- {
- id: 1,
- dashboardId: 7,
- dashboardUid: 'ggHbN42mk',
- dashboardSlug: 'alerting-with-testdata',
- panelId: 3,
- name: 'TestData - Always OK',
- state: 'ok',
- newStateDate: `${newStateDateFormatted}T10:01:01+02:00`,
- evalDate: '0001-01-01T00:00:00Z',
- evalData: {},
- executionError: '',
- url: '/d/ggHbN42mk/alerting-with-testdata',
- },
- {
- id: 3,
- dashboardId: 7,
- dashboardUid: 'ggHbN42mk',
- dashboardSlug: 'alerting-with-testdata',
- panelId: 3,
- name: 'TestData - ok',
- state: 'ok',
- newStateDate: `${newStateDateFormatted}T10:01:01+02:00`,
- evalDate: '0001-01-01T00:00:00Z',
- evalData: {},
- executionError: 'error',
- url: '/d/ggHbN42mk/alerting-with-testdata',
- },
- {
- id: 4,
- dashboardId: 7,
- dashboardUid: 'ggHbN42mk',
- dashboardSlug: 'alerting-with-testdata',
- panelId: 3,
- name: 'TestData - Paused',
- state: 'paused',
- newStateDate: `${newStateDateFormatted}T10:01:01+02:00`,
- evalDate: '0001-01-01T00:00:00Z',
- evalData: {},
- executionError: 'error',
- url: '/d/ggHbN42mk/alerting-with-testdata',
- },
- {
- id: 5,
- dashboardId: 7,
- dashboardUid: 'ggHbN42mk',
- dashboardSlug: 'alerting-with-testdata',
- panelId: 3,
- name: 'TestData - Ok',
- state: 'ok',
- newStateDate: `${newStateDateFormatted}T10:01:01+02:00`,
- evalDate: '0001-01-01T00:00:00Z',
- evalData: {
- noData: true,
- },
- executionError: 'error',
- url: '/d/ggHbN42mk/alerting-with-testdata',
- },
- ];
- afterAll(() => {
- global.Date.now = realDateNow;
- });
- describe('when loadAlertRules is dispatched', () => {
- it('then state should be correct', () => {
- reducerTester<AlertRulesState>()
- .givenReducer(alertRulesReducer, { ...initialState })
- .whenActionIsDispatched(loadAlertRules())
- .thenStateShouldEqual({ ...initialState, isLoading: true });
- });
- });
- describe('when setSearchQuery is dispatched', () => {
- it('then state should be correct', () => {
- reducerTester<AlertRulesState>()
- .givenReducer(alertRulesReducer, { ...initialState })
- .whenActionIsDispatched(setSearchQuery('query'))
- .thenStateShouldEqual({ ...initialState, searchQuery: 'query' });
- });
- });
- describe('when loadedAlertRules is dispatched', () => {
- it('then state should be correct', () => {
- reducerTester<AlertRulesState>()
- .givenReducer(alertRulesReducer, { ...initialState, isLoading: true })
- .whenActionIsDispatched(loadedAlertRules(payload))
- .thenStateShouldEqual({
- ...initialState,
- isLoading: false,
- items: [
- {
- dashboardId: 7,
- dashboardSlug: 'alerting-with-testdata',
- dashboardUid: 'ggHbN42mk',
- evalData: {
- evalMatches: [
- {
- metric: 'A-series',
- tags: null,
- value: 215,
- },
- ],
- },
- evalDate: '0001-01-01T00:00:00Z',
- executionError: '',
- id: 2,
- name: 'TestData - Always Alerting',
- newStateDate: `${newStateDateFormatted}T10:00:30+02:00`,
- panelId: 4,
- state: 'alerting',
- stateAge: newStateDateAge,
- stateClass: 'alert-state-critical',
- stateIcon: 'heart-break',
- stateText: 'ALERTING',
- url: '/d/ggHbN42mk/alerting-with-testdata',
- },
- {
- dashboardId: 7,
- dashboardSlug: 'alerting-with-testdata',
- dashboardUid: 'ggHbN42mk',
- evalData: {},
- evalDate: '0001-01-01T00:00:00Z',
- executionError: '',
- id: 1,
- name: 'TestData - Always OK',
- newStateDate: `${newStateDateFormatted}T10:01:01+02:00`,
- panelId: 3,
- state: 'ok',
- stateAge: newStateDateAge,
- stateClass: 'alert-state-ok',
- stateIcon: 'heart',
- stateText: 'OK',
- url: '/d/ggHbN42mk/alerting-with-testdata',
- },
- {
- dashboardId: 7,
- dashboardSlug: 'alerting-with-testdata',
- dashboardUid: 'ggHbN42mk',
- evalData: {},
- evalDate: '0001-01-01T00:00:00Z',
- executionError: 'error',
- id: 3,
- info: 'Execution Error: error',
- name: 'TestData - ok',
- newStateDate: `${newStateDateFormatted}T10:01:01+02:00`,
- panelId: 3,
- state: 'ok',
- stateAge: newStateDateAge,
- stateClass: 'alert-state-ok',
- stateIcon: 'heart',
- stateText: 'OK',
- url: '/d/ggHbN42mk/alerting-with-testdata',
- },
- {
- dashboardId: 7,
- dashboardSlug: 'alerting-with-testdata',
- dashboardUid: 'ggHbN42mk',
- evalData: {},
- evalDate: '0001-01-01T00:00:00Z',
- executionError: 'error',
- id: 4,
- name: 'TestData - Paused',
- newStateDate: `${newStateDateFormatted}T10:01:01+02:00`,
- panelId: 3,
- state: 'paused',
- stateAge: newStateDateAge,
- stateClass: 'alert-state-paused',
- stateIcon: 'pause',
- stateText: 'PAUSED',
- url: '/d/ggHbN42mk/alerting-with-testdata',
- },
- {
- dashboardId: 7,
- dashboardSlug: 'alerting-with-testdata',
- dashboardUid: 'ggHbN42mk',
- evalData: {
- noData: true,
- },
- evalDate: '0001-01-01T00:00:00Z',
- executionError: 'error',
- id: 5,
- info: 'Query returned no data',
- name: 'TestData - Ok',
- newStateDate: `${newStateDateFormatted}T10:01:01+02:00`,
- panelId: 3,
- state: 'ok',
- stateAge: newStateDateAge,
- stateClass: 'alert-state-ok',
- stateIcon: 'heart',
- stateText: 'OK',
- url: '/d/ggHbN42mk/alerting-with-testdata',
- },
- ],
- });
- });
- });
- });
- describe('Notification channel', () => {
- const notifiers: NotifierDTO[] = [
- {
- type: 'webhook',
- name: 'webhook',
- heading: 'Webhook settings',
- description: 'Sends HTTP POST request to a URL',
- info: '',
- options: [
- {
- element: 'input',
- inputType: 'text',
- label: 'Url',
- description: '',
- placeholder: '',
- propertyName: 'url',
- showWhen: { field: '', is: '' },
- required: true,
- validationRule: '',
- secure: false,
- dependsOn: '',
- },
- {
- element: 'select',
- inputType: '',
- label: 'Http Method',
- description: '',
- placeholder: '',
- propertyName: 'httpMethod',
- selectOptions: [
- { value: 'POST', label: 'POST' },
- { value: 'PUT', label: 'PUT' },
- ],
- showWhen: { field: '', is: '' },
- required: false,
- validationRule: '',
- secure: false,
- dependsOn: '',
- },
- {
- element: 'input',
- inputType: 'text',
- label: 'Username',
- description: '',
- placeholder: '',
- propertyName: 'username',
- showWhen: { field: '', is: '' },
- required: false,
- validationRule: '',
- secure: false,
- dependsOn: '',
- },
- {
- element: 'input',
- inputType: 'password',
- label: 'Password',
- description: '',
- placeholder: '',
- propertyName: 'password',
- showWhen: { field: '', is: '' },
- required: false,
- validationRule: '',
- secure: true,
- dependsOn: '',
- },
- ],
- },
- ];
- describe('Load notification channel', () => {
- it('should migrate non secure settings to secure fields', () => {
- const payload = {
- id: 2,
- uid: '9L3FrrHGk',
- name: 'Webhook test',
- type: 'webhook',
- isDefault: false,
- sendReminder: false,
- disableResolveMessage: false,
- frequency: '',
- created: '2020-08-28T08:49:24Z',
- updated: '2020-08-28T08:49:24Z',
- settings: {
- autoResolve: true,
- httpMethod: 'POST',
- password: 'asdf',
- severity: 'critical',
- uploadImage: true,
- url: 'http://localhost.webhook',
- username: 'asdf',
- },
- };
- const expected = {
- id: 2,
- uid: '9L3FrrHGk',
- name: 'Webhook test',
- type: 'webhook',
- isDefault: false,
- sendReminder: false,
- disableResolveMessage: false,
- frequency: '',
- created: '2020-08-28T08:49:24Z',
- updated: '2020-08-28T08:49:24Z',
- secureSettings: {
- password: 'asdf',
- },
- settings: {
- autoResolve: true,
- httpMethod: 'POST',
- password: '',
- severity: 'critical',
- uploadImage: true,
- url: 'http://localhost.webhook',
- username: 'asdf',
- },
- };
- reducerTester<NotificationChannelState>()
- .givenReducer(notificationChannelReducer, { ...initialChannelState, notifiers: notifiers })
- .whenActionIsDispatched(notificationChannelLoaded(payload))
- .thenStateShouldEqual({
- ...initialChannelState,
- notifiers: notifiers,
- notificationChannel: expected,
- });
- });
- it('should handle already secure field', () => {
- const payload = {
- id: 2,
- uid: '9L3FrrHGk',
- name: 'Webhook test',
- type: 'webhook',
- isDefault: false,
- sendReminder: false,
- disableResolveMessage: false,
- frequency: '',
- created: '2020-08-28T08:49:24Z',
- updated: '2020-08-28T08:49:24Z',
- secureFields: {
- password: true,
- },
- settings: {
- autoResolve: true,
- httpMethod: 'POST',
- password: '',
- severity: 'critical',
- uploadImage: true,
- url: 'http://localhost.webhook',
- username: 'asdf',
- },
- };
- const expected = {
- id: 2,
- uid: '9L3FrrHGk',
- name: 'Webhook test',
- type: 'webhook',
- isDefault: false,
- sendReminder: false,
- disableResolveMessage: false,
- frequency: '',
- created: '2020-08-28T08:49:24Z',
- updated: '2020-08-28T08:49:24Z',
- secureFields: {
- password: true,
- },
- settings: {
- autoResolve: true,
- httpMethod: 'POST',
- password: '',
- severity: 'critical',
- uploadImage: true,
- url: 'http://localhost.webhook',
- username: 'asdf',
- },
- };
- reducerTester<NotificationChannelState>()
- .givenReducer(notificationChannelReducer, { ...initialChannelState, notifiers: notifiers })
- .whenActionIsDispatched(notificationChannelLoaded(payload))
- .thenStateShouldEqual({
- ...initialChannelState,
- notifiers: notifiers,
- notificationChannel: expected,
- });
- });
- });
- });
|