Skip to content

Commit

Permalink
[8.15] [MX] Mute alert options with read only permission (#191283) (#…
Browse files Browse the repository at this point in the history
…191740)

# Backport

This will backport the following commits from `main` to `8.15`:
- [[MX] Mute alert options with read only permission
(#191283)](#191283)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Julia","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-08-29T14:35:52Z","message":"[MX]
Mute alert options with read only permission (#191283)\n\nIssue:
https://github.com/elastic/kibana/issues/191060\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios.","sha":"2d81ab694e079c4cc1d9ebc3b8586f4bfe7bb20a","branchLabelMapping":{"^v8.16.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:ResponseOps","ci:project-deploy-observability","Team:obs-ux-management","v8.15.0","v8.16.0"],"title":"[MX]
Mute alert options with read only
permission","number":191283,"url":"https://github.com/elastic/kibana/pull/191283","mergeCommit":{"message":"[MX]
Mute alert options with read only permission (#191283)\n\nIssue:
https://github.com/elastic/kibana/issues/191060\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios.","sha":"2d81ab694e079c4cc1d9ebc3b8586f4bfe7bb20a"}},"sourceBranch":"main","suggestedTargetBranches":["8.15"],"targetPullRequestStates":[{"branch":"8.15","label":"v8.15.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/191283","number":191283,"mergeCommit":{"message":"[MX]
Mute alert options with read only permission (#191283)\n\nIssue:
https://github.com/elastic/kibana/issues/191060\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios.","sha":"2d81ab694e079c4cc1d9ebc3b8586f4bfe7bb20a"}}]}]
BACKPORT-->

Co-authored-by: Julia <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
3 people authored Sep 2, 2024
1 parent ab6bb9c commit 44f30cc
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import { unifiedSearchPluginMock } from '@kbn/unified-search-plugin/public/mocks
import type { AlertActionsProps } from '@kbn/triggers-actions-ui-plugin/public/types';
import { getAlertsTableDefaultAlertActionsLazy } from '@kbn/triggers-actions-ui-plugin/public/common/get_alerts_table_default_row_actions';
import { lensPluginMock } from '@kbn/lens-plugin/public/mocks';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
logger: {
log: () => {},
warn: () => {},
error: () => {},
},
});

const triggersActionsUiStartMock = {
createStart() {
Expand All @@ -29,7 +43,11 @@ const triggersActionsUiStartMock = {
<div data-test-subj="alerts-state-table">mocked component</div>
)),
getAlertsTableDefaultAlertActions: (props: AlertActionsProps) => {
return getAlertsTableDefaultAlertActionsLazy(props);
return (
<QueryClientProvider client={queryClient}>
{getAlertsTableDefaultAlertActionsLazy(props)}
</QueryClientProvider>
);
},
getAddRuleFlyout: jest.fn(() => <div data-test-subj="add-rule-flyout">mocked component</div>),
getEditRuleFlyout: jest.fn(() => (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';
import DefaultAlertActions from './default_alert_actions';
import { render, screen } from '@testing-library/react';
import type { AlertActionsProps } from '../../../../types';

jest.mock('../../../hooks/use_load_rule_types_query', () => ({
useLoadRuleTypesQuery: jest.fn(),
}));
jest.mock('./view_rule_details_alert_action', () => {
return {
ViewRuleDetailsAlertAction: () => (
<div data-test-subj="viewRuleDetailsAlertAction">{'ViewRuleDetailsAlertAction'}</div>
),
};
});
jest.mock('./view_alert_details_alert_action', () => {
return {
ViewAlertDetailsAlertAction: () => (
<div data-test-subj="viewAlertDetailsAlertAction">{'ViewAlertDetailsAlertAction'}</div>
),
};
});
jest.mock('./mute_alert_action', () => {
return { MuteAlertAction: () => <div data-test-subj="muteAlertAction">{'MuteAlertAction'}</div> };
});
jest.mock('./mark_as_untracked_alert_action', () => {
return {
MarkAsUntrackedAlertAction: () => (
<div data-test-subj="markAsUntrackedAlertAction">{'MarkAsUntrackedAlertAction'}</div>
),
};
});

const { useLoadRuleTypesQuery } = jest.requireMock('../../../hooks/use_load_rule_types_query');
const props = { alert: {}, refresh: jest.fn() } as unknown as AlertActionsProps;

describe('DefaultAlertActions component', () => {
it('should show "Mute" and "Marked as untracted" option', async () => {
useLoadRuleTypesQuery.mockReturnValue({ authorizedToCreateAnyRules: true });

render(<DefaultAlertActions {...props} />);

expect(await screen.findByText('MuteAlertAction')).toBeInTheDocument();
expect(await screen.findByText('MarkAsUntrackedAlertAction')).toBeInTheDocument();
});

it('should hide "Mute" and "Marked as untracted" option', async () => {
useLoadRuleTypesQuery.mockReturnValue({ authorizedToCreateAnyRules: false });

render(<DefaultAlertActions {...props} />);

expect(screen.queryByText('MuteAlertAction')).not.toBeInTheDocument();
expect(screen.queryByText('MarkAsUntrackedAlertAction')).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@ import type { AlertActionsProps } from '../../../../types';
import { ViewAlertDetailsAlertAction } from './view_alert_details_alert_action';
import { MuteAlertAction } from './mute_alert_action';
import { MarkAsUntrackedAlertAction } from './mark_as_untracked_alert_action';
import { useLoadRuleTypesQuery } from '../../../hooks/use_load_rule_types_query';

/**
* Common alerts table row actions
*/
export const DefaultAlertActions = (props: AlertActionsProps) => {
const { authorizedToCreateAnyRules } = useLoadRuleTypesQuery({
filteredRuleTypes: [],
});

return (
<>
<ViewRuleDetailsAlertAction {...props} />
<ViewAlertDetailsAlertAction {...props} />
<MarkAsUntrackedAlertAction {...props} />
<MuteAlertAction {...props} />
{authorizedToCreateAnyRules && <MarkAsUntrackedAlertAction {...props} />}
{authorizedToCreateAnyRules && <MuteAlertAction {...props} />}
</>
);
};
Expand Down

0 comments on commit 44f30cc

Please sign in to comment.