diff --git a/public/app/features/alerting/unified/Silences.test.tsx b/public/app/features/alerting/unified/Silences.test.tsx index d3f1fe9b1a5..efe5d0b6b37 100644 --- a/public/app/features/alerting/unified/Silences.test.tsx +++ b/public/app/features/alerting/unified/Silences.test.tsx @@ -6,7 +6,7 @@ import { Router } from 'react-router-dom'; import { byLabelText, byPlaceholderText, byRole, byTestId, byText } from 'testing-library-selector'; import { dateTime } from '@grafana/data'; -import { locationService, setDataSourceSrv } from '@grafana/runtime'; +import { locationService, setDataSourceSrv, config } from '@grafana/runtime'; import { contextSrv } from 'app/core/services/context_srv'; import { AlertState, MatcherOperator } from 'app/plugins/datasource/alertmanager/types'; import { configureStore } from 'app/store/configureStore'; @@ -70,6 +70,7 @@ const ui = { matcherOperator: (operator: MatcherOperator) => byText(operator, { exact: true }), addMatcherButton: byRole('button', { name: 'Add matcher' }), submit: byText('Submit'), + createdBy: byText(/created by \*/i), }, }; @@ -112,6 +113,11 @@ const resetMocks = () => { mocks.contextSrv.hasAccess.mockImplementation(() => true); }; +const setUserLogged = (isLogged: boolean) => { + config.bootData.user.isSignedIn = isLogged; + config.bootData.user.name = isLogged ? 'admin' : ''; +}; + describe('Silences', () => { beforeAll(resetMocks); afterEach(resetMocks); @@ -210,9 +216,19 @@ describe('Silence edit', () => { afterEach(resetMocks); beforeEach(() => { + setUserLogged(true); setDataSourceSrv(new MockDataSourceSrv(dataSources)); }); + it('Should not render createdBy if user is logged in and has a name', async () => { + renderSilences(baseUrlPath); + await waitFor(() => expect(ui.editor.createdBy.query()).not.toBeInTheDocument()); + }); + it('Should render createdBy if user is not logged or has no name', async () => { + setUserLogged(false); + renderSilences(baseUrlPath); + await waitFor(() => expect(ui.editor.createdBy.get()).toBeInTheDocument()); + }); it( 'prefills the matchers field with matchers params', async () => { diff --git a/public/app/features/alerting/unified/components/silences/SilencesEditor.tsx b/public/app/features/alerting/unified/components/silences/SilencesEditor.tsx index 52fa9d54bdc..2c37eddfd47 100644 --- a/public/app/features/alerting/unified/components/silences/SilencesEditor.tsx +++ b/public/app/features/alerting/unified/components/silences/SilencesEditor.tsx @@ -164,6 +164,7 @@ export const SilencesEditor: FC = ({ silence, alertManagerSourceName }) = 700, [clearErrors, duration, endsAt, prevDuration, setValue, startsAt] ); + const userLogged = Boolean(config.bootData.user.isSignedIn && config.bootData.user.name); return ( @@ -206,6 +207,20 @@ export const SilencesEditor: FC = ({ silence, alertManagerSourceName }) = placeholder="Details about the silence" /> + {!userLogged && ( + + + + )}