Skip to content

Commit

Permalink
HAI-1946 Only show buttons that user has permissions for in hanke view
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markohaarni committed Oct 2, 2023
1 parent 1445f4e commit 6fef061
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 32 deletions.
44 changes: 44 additions & 0 deletions src/domain/hanke/hankeView/HankeView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<SignedInUser>({
hankeKayttajaId: '3fa85f64-5717-4562-b3fc-2c963f66afa6',
kayttooikeustaso: 'KATSELUOIKEUS',
kayttooikeudet: ['VIEW'],
}),
);
}),
);
}

test('Draft state notification is rendered when hanke is in draft state', async () => {
render(<HankeViewContainer hankeTunnus="HAI22-1" />);
Expand Down Expand Up @@ -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(<HankeViewContainer hankeTunnus="HAI22-2" />);

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(<HankeViewContainer hankeTunnus="HAI22-2" />);

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(<HankeViewContainer hankeTunnus="HAI22-2" />);

await waitForLoadingToFinish();

expect(screen.queryByRole('button', { name: 'Päätä hanke' })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Peru hanke' })).not.toBeInTheDocument();
});
73 changes: 41 additions & 32 deletions src/domain/hanke/hankeView/HankeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -216,23 +217,27 @@ const HankeView: React.FC<Props> = ({

<InformationViewHeaderButtons>
<FeatureFlags flags={['hanke']}>
<Button
onClick={onEditHanke}
variant="primary"
iconLeft={<IconPen aria-hidden="true" />}
theme="coat"
>
{t('hankeList:buttons:edit')}
</Button>
<Button
variant="primary"
iconLeft={<IconPlusCircle aria-hidden="true" />}
theme="coat"
onClick={addApplication}
disabled={!isHankeValid}
>
{t('hankeList:buttons:addApplication')}
</Button>
<UserRightsCheck requiredRight="EDIT" hankeTunnus={hankeData.hankeTunnus}>
<Button
onClick={onEditHanke}
variant="primary"
iconLeft={<IconPen aria-hidden="true" />}
theme="coat"
>
{t('hankeList:buttons:edit')}
</Button>
</UserRightsCheck>
<UserRightsCheck requiredRight="EDIT_APPLICATIONS" hankeTunnus={hankeData.hankeTunnus}>
<Button
variant="primary"
iconLeft={<IconPlusCircle aria-hidden="true" />}
theme="coat"
onClick={addApplication}
disabled={!isHankeValid}
>
{t('hankeList:buttons:addApplication')}
</Button>
</UserRightsCheck>
</FeatureFlags>
<FeatureFlags flags={['hanke', 'accessRights']}>
<Button
Expand All @@ -245,23 +250,27 @@ const HankeView: React.FC<Props> = ({
</Button>
</FeatureFlags>
<FeatureFlags flags={['hanke']}>
<Button
variant="primary"
iconLeft={<IconCross aria-hidden="true" />}
theme="black"
disabled={!isHankeValid}
>
{t('hankeList:buttons:endHanke')}
</Button>
<UserRightsCheck requiredRight="DELETE" hankeTunnus={hankeData.hankeTunnus}>
<Button
variant="primary"
iconLeft={<IconCross aria-hidden="true" />}
theme="black"
disabled={!isHankeValid}
>
{t('hankeList:buttons:endHanke')}
</Button>
</UserRightsCheck>
</FeatureFlags>
{!isLoading && isCancelPossible && (
<Button
onClick={onCancelHanke}
variant="danger"
iconLeft={<IconTrash aria-hidden="true" />}
>
{t('hankeForm:cancelButton')}
</Button>
<UserRightsCheck requiredRight="DELETE" hankeTunnus={hankeData.hankeTunnus}>
<Button
onClick={onCancelHanke}
variant="danger"
iconLeft={<IconTrash aria-hidden="true" />}
>
{t('hankeForm:cancelButton')}
</Button>
</UserRightsCheck>
)}
</InformationViewHeaderButtons>
</InformationViewHeader>
Expand Down

0 comments on commit 6fef061

Please sign in to comment.