From ee5684b130fcddd3ed62218c053ef7ce10be258f Mon Sep 17 00:00:00 2001 From: SagarRajput-7 <162284829+SagarRajput-7@users.noreply.github.com> Date: Sun, 27 Apr 2025 17:24:56 +0530 Subject: [PATCH] feat: added permission restriction for viewer for planned Maintaince (#7736) * feat: added permission restriction for viewer for planned Maintaince * feat: added test cases --- .../PlannedDowntime/PlannedDowntime.tsx | 36 ++++++++++----- .../__test__/PlannedDowntime.test.tsx | 44 +++++++++++++++++++ 2 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 frontend/src/container/PlannedDowntime/__test__/PlannedDowntime.test.tsx diff --git a/frontend/src/container/PlannedDowntime/PlannedDowntime.tsx b/frontend/src/container/PlannedDowntime/PlannedDowntime.tsx index fa4ea6f29f..46055ade7a 100644 --- a/frontend/src/container/PlannedDowntime/PlannedDowntime.tsx +++ b/frontend/src/container/PlannedDowntime/PlannedDowntime.tsx @@ -3,7 +3,7 @@ import 'dayjs/locale/en'; import { PlusOutlined } from '@ant-design/icons'; import { Color } from '@signozhq/design-tokens'; -import { Button, Flex, Form, Input, Typography } from 'antd'; +import { Button, Flex, Form, Input, Tooltip, Typography } from 'antd'; import getAll from 'api/alerts/getAll'; import { useDeleteDowntimeSchedule } from 'api/plannedDowntime/deleteDowntimeSchedule'; import { @@ -13,8 +13,10 @@ import { import dayjs from 'dayjs'; import { useNotifications } from 'hooks/useNotifications'; import { Search } from 'lucide-react'; +import { useAppContext } from 'providers/App/App'; import React, { ChangeEvent, useEffect, useState } from 'react'; import { useQuery } from 'react-query'; +import { USER_ROLES } from 'types/roles'; import { PlannedDowntimeDeleteModal } from './PlannedDowntimeDeleteModal'; import { PlannedDowntimeForm } from './PlannedDowntimeForm'; @@ -33,6 +35,7 @@ export function PlannedDowntime(): JSX.Element { }); const [isOpen, setIsOpen] = React.useState(false); const [form] = Form.useForm(); + const { user } = useAppContext(); const [initialValues, setInitialValues] = useState< Partial @@ -108,18 +111,27 @@ export function PlannedDowntime(): JSX.Element { value={searchValue} onChange={handleSearch} /> - + +
{ + it('renders the PlannedDowntime component properly', () => { + render(, {}, 'ADMIN'); + + // Check if title is rendered + expect(screen.getByText('Planned Downtime')).toBeInTheDocument(); + + // Check if subtitle is rendered + expect( + screen.getByText('Create and manage planned downtimes.'), + ).toBeInTheDocument(); + + // Check if search input is rendered + expect( + screen.getByPlaceholderText('Search for a planned downtime...'), + ).toBeInTheDocument(); + + // Check if "New downtime" button is enabled for ADMIN + const newDowntimeButton = screen.getByRole('button', { + name: /new downtime/i, + }); + expect(newDowntimeButton).toBeInTheDocument(); + expect(newDowntimeButton).not.toBeDisabled(); + }); + + it('disables the "New downtime" button for users with VIEWER role', () => { + render(, {}, USER_ROLES.VIEWER); + + // Check if "New downtime" button is disabled for VIEWER + const newDowntimeButton = screen.getByRole('button', { + name: /new downtime/i, + }); + expect(newDowntimeButton).toBeInTheDocument(); + expect(newDowntimeButton).toBeDisabled(); + + expect(newDowntimeButton).toHaveAttribute('disabled'); + }); +});