From 6fef061d686cd085a7351c3f67a0e8a7a3ad589f Mon Sep 17 00:00:00 2001 From: Marko Haarni Date: Mon, 2 Oct 2023 10:23:30 +0300 Subject: [PATCH] HAI-1946 Only show buttons that user has permissions for in hanke view Show Edit hanke button if user has EDIT permission. Show Add application button if user has EDIT_APPLICATIONS permission. Show End hanke button if user has DELETE permission. Show Cancel hanke button if user has DELETE permission. --- src/domain/hanke/hankeView/HankeView.test.tsx | 44 +++++++++++ src/domain/hanke/hankeView/HankeView.tsx | 73 +++++++++++-------- 2 files changed, 85 insertions(+), 32 deletions(-) diff --git a/src/domain/hanke/hankeView/HankeView.test.tsx b/src/domain/hanke/hankeView/HankeView.test.tsx index db44e8c64..ec7c6a68b 100644 --- a/src/domain/hanke/hankeView/HankeView.test.tsx +++ b/src/domain/hanke/hankeView/HankeView.test.tsx @@ -4,6 +4,22 @@ import { render, screen } from '../../../testUtils/render'; import { waitForLoadingToFinish } from '../../../testUtils/helperFunctions'; import HankeViewContainer from './HankeViewContainer'; import { server } from '../../mocks/test-server'; +import { SignedInUser } from '../hankeUsers/hankeUser'; + +function getViewPermissionForUser() { + server.use( + rest.get('/api/hankkeet/:hankeTunnus/whoami', async (req, res, ctx) => { + return res( + ctx.status(200), + ctx.json({ + hankeKayttajaId: '3fa85f64-5717-4562-b3fc-2c963f66afa6', + kayttooikeustaso: 'KATSELUOIKEUS', + kayttooikeudet: ['VIEW'], + }), + ); + }), + ); +} test('Draft state notification is rendered when hanke is in draft state', async () => { render(); @@ -168,3 +184,31 @@ test('Should navigate to application view when clicking the eye icon', async () expect(window.location.pathname).toBe('/fi/hakemus/2'); expect(screen.queryByText('Mannerheimintien kuopat')).toBeInTheDocument(); }); + +test('Should not show edit hanke button if user does not have EDIT permission', async () => { + getViewPermissionForUser(); + render(); + + await waitForLoadingToFinish(); + + expect(screen.queryByRole('button', { name: 'Muokkaa hanketta' })).not.toBeInTheDocument(); +}); + +test('Should not show add application button if user does not have EDIT_APPLICATIONS permission', async () => { + getViewPermissionForUser(); + render(); + + await waitForLoadingToFinish(); + + expect(screen.queryByRole('button', { name: 'Lisää hakemus' })).not.toBeInTheDocument(); +}); + +test('Should not show end hanke and remove hanke buttons if user does not have DELETE permission', async () => { + getViewPermissionForUser(); + render(); + + await waitForLoadingToFinish(); + + expect(screen.queryByRole('button', { name: 'Päätä hanke' })).not.toBeInTheDocument(); + expect(screen.queryByRole('button', { name: 'Peru hanke' })).not.toBeInTheDocument(); +}); diff --git a/src/domain/hanke/hankeView/HankeView.tsx b/src/domain/hanke/hankeView/HankeView.tsx index 6e1af6c87..503406998 100644 --- a/src/domain/hanke/hankeView/HankeView.tsx +++ b/src/domain/hanke/hankeView/HankeView.tsx @@ -53,6 +53,7 @@ import { import FeatureFlags from '../../../common/components/featureFlags/FeatureFlags'; import { useFeatureFlags } from '../../../common/components/featureFlags/FeatureFlagsContext'; import { SignedInUser } from '../hankeUsers/hankeUser'; +import UserRightsCheck from '../hankeUsers/UserRightsCheck'; type AreaProps = { area: HankeAlue; @@ -216,23 +217,27 @@ const HankeView: React.FC = ({ - - + + + + + + - + + + {!isLoading && isCancelPossible && ( - + + + )}