Skip to content

Commit

Permalink
Use kibana.yml for flag, fix and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaweiWu committed Oct 13, 2024
1 parent cb8f068 commit 70c8baa
Show file tree
Hide file tree
Showing 21 changed files with 163 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
export * from './alerts';
export * from './i18n_weekdays';
export * from './routes';
export * from './rule_form_flag';

export const VIEW_LICENSE_OPTIONS_LINK = 'https://www.elastic.co/subscriptions';

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ jest.mock('../../common/hooks/use_load_rule_type_aad_template_fields', () => ({
useLoadRuleTypeAadTemplateField: jest.fn(),
}));

jest.mock('../utils/get_authorized_rule_types', () => ({
getAvailableRuleTypes: jest.fn(),
}));

jest.mock('../../common/hooks/use_fetch_flapping_settings', () => ({
useFetchFlappingSettings: jest.fn(),
}));
Expand All @@ -63,7 +59,6 @@ const { useLoadRuleTypeAadTemplateField } = jest.requireMock(
'../../common/hooks/use_load_rule_type_aad_template_fields'
);
const { useLoadRuleTypesQuery } = jest.requireMock('../../common/hooks/use_load_rule_types_query');
const { getAvailableRuleTypes } = jest.requireMock('../utils/get_authorized_rule_types');
const { useFetchFlappingSettings } = jest.requireMock(
'../../common/hooks/use_fetch_flapping_settings'
);
Expand Down Expand Up @@ -168,13 +163,6 @@ useLoadRuleTypesQuery.mockReturnValue({
},
});

getAvailableRuleTypes.mockReturnValue([
{
ruleType: indexThresholdRuleType,
ruleTypeModel: indexThresholdRuleTypeModel,
},
]);

const mockConnector = {
id: 'test-connector',
name: 'Test',
Expand Down Expand Up @@ -236,7 +224,7 @@ const toastsMock = jest.fn();
const ruleTypeRegistryMock: RuleTypeRegistryContract = {
has: jest.fn(),
register: jest.fn(),
get: jest.fn(),
get: jest.fn().mockReturnValue(indexThresholdRuleTypeModel),
list: jest.fn(),
};

Expand Down Expand Up @@ -272,6 +260,7 @@ describe('useLoadDependencies', () => {
isLoading: false,
isInitialLoading: false,
ruleType: indexThresholdRuleType,
ruleTypes: [...ruleTypeIndex.values()],
ruleTypeModel: indexThresholdRuleTypeModel,
uiConfig: uiConfigMock,
healthCheckError: null,
Expand Down Expand Up @@ -317,39 +306,6 @@ describe('useLoadDependencies', () => {
});
});

test('should call getAvailableRuleTypes with the correct params', async () => {
const { result } = renderHook(
() => {
return useLoadDependencies({
http: httpMock as unknown as HttpStart,
toasts: toastsMock as unknown as ToastsStart,
ruleTypeRegistry: ruleTypeRegistryMock,
validConsumers: ['stackAlerts', 'logs'],
consumer: 'logs',
capabilities: {
actions: {
show: true,
save: true,
execute: true,
},
} as unknown as ApplicationStart['capabilities'],
});
},
{ wrapper }
);

await waitFor(() => {
return expect(result.current.isInitialLoading).toEqual(false);
});

expect(getAvailableRuleTypes).toBeCalledWith({
consumer: 'logs',
ruleTypeRegistry: ruleTypeRegistryMock,
ruleTypes: [indexThresholdRuleType],
validConsumers: ['stackAlerts', 'logs'],
});
});

test('should call resolve rule with the correct params', async () => {
const { result } = renderHook(
() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,18 @@ describe('ruleActions', () => {
getActionTypeModel('1', {
id: 'actionType-1',
validateParams: mockValidate,
defaultActionParams: {
key: 'value',
},
})
);
actionTypeRegistry.register(
getActionTypeModel('2', {
id: 'actionType-2',
validateParams: mockValidate,
defaultActionParams: {
key: 'value',
},
})
);

Expand Down Expand Up @@ -150,6 +156,10 @@ describe('ruleActions', () => {
selectedRuleType: {
id: 'selectedRuleTypeId',
defaultActionGroupId: 'test',
recoveryActionGroup: {
id: 'test-recovery-group-id',
name: 'test-recovery-group',
},
producer: 'stackAlerts',
},
connectors: mockConnectors,
Expand Down Expand Up @@ -222,7 +232,7 @@ describe('ruleActions', () => {
frequency: { notifyWhen: 'onActionGroupChange', summary: false, throttle: null },
group: 'test',
id: 'connector-1',
params: {},
params: { key: 'value' },
uuid: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
},
type: 'addAction',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ describe('ruleActionsItem', () => {

await userEvent.click(screen.getByText('onTimeframeChange'));

expect(mockOnChange).toHaveBeenCalledTimes(1);
expect(mockOnChange).toHaveBeenCalledTimes(2);

expect(mockOnChange).toHaveBeenCalledWith({
payload: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,14 @@ describe('RuleAlertDelay', () => {
expect(mockOnChange).not.toHaveBeenCalled();
});

test('Should call onChange with null if empty string is typed', () => {
test('Should not call onChange if empty string is typed', () => {
render(<RuleAlertDelay />);
fireEvent.change(screen.getByTestId('alertDelayInput'), {
target: {
value: '',
},
});
expect(mockOnChange).toHaveBeenCalledWith({
type: 'setAlertDelay',
payload: null,
});
expect(mockOnChange).not.toHaveBeenCalled();
});

test('Should display error when input is invalid', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const ruleType = {
recoveryActionGroup: 'recovered',
producer: 'logs',
authorizedConsumers: {
alerting: { read: true, all: true },
alerts: { read: true, all: true },
test: { read: true, all: true },
stackAlerts: { read: true, all: true },
logs: { read: true, all: true },
Expand Down Expand Up @@ -101,6 +101,7 @@ const plugins = {
application: {
capabilities: {
rulesSettings: {
readFlappingSettingsUI: true,
writeFlappingSettingsUI: true,
},
},
Expand Down Expand Up @@ -133,12 +134,14 @@ describe('Rule Definition', () => {
active: 5,
},
notifyWhen: null,
consumer: 'stackAlerts',
consumer: 'alerts',
ruleTypeId: '.es-query',
},
selectedRuleType: ruleType,
selectedRuleTypeModel: ruleModel,
canShowConsumerSelection: true,
validConsumers: ['logs', 'stackAlerts'],
availableRuleTypes: [ruleType],
});

render(<RuleDefinition />);
Expand All @@ -164,13 +167,16 @@ describe('Rule Definition', () => {
active: 5,
},
notifyWhen: null,
consumer: 'stackAlerts',
consumer: 'alerts',
ruleTypeId: '.es-query',
},
selectedRuleType: ruleType,
selectedRuleTypeModel: {
...ruleModel,
documentationUrl: null,
},
availableRuleTypes: [ruleType],
validConsumers: ['logs', 'stackAlerts'],
});
render(<RuleDefinition />);

Expand All @@ -191,6 +197,7 @@ describe('Rule Definition', () => {
},
notifyWhen: null,
consumer: 'stackAlerts',
ruleTypeId: '.es-query',
},
selectedRuleType: ruleType,
selectedRuleTypeModel: ruleModel,
Expand All @@ -215,9 +222,11 @@ describe('Rule Definition', () => {
},
notifyWhen: null,
consumer: 'stackAlerts',
ruleTypeId: '.es-query',
},
selectedRuleType: ruleType,
selectedRuleTypeModel: ruleModel,
availableRuleTypes: [ruleType],
canShowConsumerSelect: true,
validConsumers: ['logs'],
});
Expand All @@ -241,9 +250,11 @@ describe('Rule Definition', () => {
},
notifyWhen: null,
consumer: 'stackAlerts',
ruleTypeId: '.es-query',
},
selectedRuleType: ruleType,
selectedRuleTypeModel: ruleModel,
availableRuleTypes: [ruleType],
canShowConsumerSelect: true,
validConsumers: ['logs', 'observability'],
});
Expand All @@ -267,9 +278,11 @@ describe('Rule Definition', () => {
},
notifyWhen: null,
consumer: 'stackAlerts',
ruleTypeId: '.es-query',
},
selectedRuleType: ruleType,
selectedRuleTypeModel: ruleModel,
availableRuleTypes: [ruleType],
});

render(<RuleDefinition />);
Expand All @@ -292,9 +305,11 @@ describe('Rule Definition', () => {
},
notifyWhen: null,
consumer: 'stackAlerts',
ruleTypeId: '.es-query',
},
selectedRuleType: ruleType,
selectedRuleTypeModel: ruleModel,
availableRuleTypes: [ruleType],
});

render(<RuleDefinition />);
Expand Down Expand Up @@ -326,9 +341,11 @@ describe('Rule Definition', () => {
},
notifyWhen: null,
consumer: 'stackAlerts',
ruleTypeId: '.es-query',
},
selectedRuleType: ruleType,
selectedRuleTypeModel: ruleModel,
availableRuleTypes: [ruleType],
canShowConsumerSelection: true,
validConsumers: ['logs', 'stackAlerts'],
});
Expand All @@ -339,6 +356,48 @@ describe('Rule Definition', () => {
expect(screen.getByTestId('ruleSettingsFlappingForm')).toBeInTheDocument();
});

test('should hide flapping if the user does not have read access', async () => {
useRuleFormState.mockReturnValue({
plugins: {
charts: {} as ChartsPluginSetup,
data: {} as DataPublicPluginStart,
dataViews: {} as DataViewsPublicPluginStart,
unifiedSearch: {} as UnifiedSearchPublicPluginStart,
docLinks: {} as DocLinksStart,
application: {
capabilities: {
rulesSettings: {
readFlappingSettingsUI: false,
writeFlappingSettingsUI: true,
},
},
},
},
formData: {
id: 'test-id',
params: {},
schedule: {
interval: '1m',
},
alertDelay: {
active: 5,
},
notifyWhen: null,
consumer: 'stackAlerts',
ruleTypeId: '.es-query',
},
selectedRuleType: ruleType,
selectedRuleTypeModel: ruleModel,
availableRuleTypes: [ruleType],
canShowConsumerSelection: true,
validConsumers: ['logs', 'stackAlerts'],
});

render(<RuleDefinition />);

expect(screen.queryByTestId('ruleDefinitionFlappingFormGroup')).not.toBeInTheDocument();
});

test('should allow flapping to be changed', async () => {
useRuleFormState.mockReturnValue({
plugins,
Expand All @@ -353,9 +412,11 @@ describe('Rule Definition', () => {
},
notifyWhen: null,
consumer: 'stackAlerts',
ruleTypeId: '.es-query',
},
selectedRuleType: ruleType,
selectedRuleTypeModel: ruleModel,
availableRuleTypes: [ruleType],
canShowConsumerSelection: true,
validConsumers: ['logs', 'stackAlerts'],
});
Expand Down Expand Up @@ -389,9 +450,11 @@ describe('Rule Definition', () => {
},
notifyWhen: null,
consumer: 'stackAlerts',
ruleTypeId: '.es-query',
},
selectedRuleType: ruleType,
selectedRuleTypeModel: ruleModel,
availableRuleTypes: [ruleType],
canShowConsumerSelection: true,
validConsumers: ['logs', 'stackAlerts'],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,9 @@ export const RuleDefinition = () => {
if (consumer !== ALERTING_FEATURE_ID) {
return [];
}

const selectedAvailableRuleType = availableRuleTypes.find((ruleType) => {
return ruleType.id === selectedRuleType.id;
});

if (!selectedAvailableRuleType?.authorizedConsumers) {
return [];
}
Expand Down Expand Up @@ -323,6 +321,7 @@ export const RuleDefinition = () => {
</EuiDescribedFormGroup>
{IS_RULE_SPECIFIC_FLAPPING_ENABLED && readFlappingSettingsUI && (
<EuiDescribedFormGroup
data-test-subj="ruleDefinitionFlappingFormGroup"
fullWidth
title={<h4>{ALERT_FLAPPING_DETECTION_TITLE}</h4>}
description={
Expand Down
Loading

0 comments on commit 70c8baa

Please sign in to comment.