Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HAI-1946 Only show buttons that user has permissions for in hanke view #380

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading