Skip to content

Commit

Permalink
feat: migrate document visualization menu
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Aug 26, 2024
1 parent 89eff5c commit a64fe83
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/packages/auth/components/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const mapDispatchToProps = {
type AuthDumbTypes = {
userRoles?: string[];
userStamp?: string;
roles: string[];
roles: Array<string | [string, (value: string) => boolean]>;
fallback?: any;
complementaryCheck?: boolean;
loadUserStamp?: any;
Expand Down Expand Up @@ -49,7 +49,7 @@ export function AuthDumb({
const isAuthorized = !!roles.find((role) => {
if (Array.isArray(role)) {
const [r, check] = role;
return userRoles?.includes(r) && check(userStamp);
return userRoles?.includes(r) && check(userStamp!);
}
return userRoles?.includes(role);
});
Expand Down
4 changes: 4 additions & 0 deletions src/packages/model/Sims.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export type Rubric = {
idAttribute: string;
};

export type Sims = {
creators: string[];
};
6 changes: 6 additions & 0 deletions src/packages/model/operations/document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Sims } from '../Sims';

export type Document = {
id: string;
sims: Sims[];
};
8 changes: 4 additions & 4 deletions src/packages/modules-operations/document/menu.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
SERIES_CONTRIBUTOR,
} from '../../auth/roles';

describe('Structures Home Page Menu', () => {
describe('Document Home Page Menu', () => {
it('an admin can create a new structure if he does not have the Gestionnaire_structures_RMESGNCS role', () => {
render(
<RBACMock roles={[ADMIN]}>
Expand All @@ -19,7 +19,7 @@ describe('Structures Home Page Menu', () => {
screen.getByText('New Document');
});

it('a user with INDICATOR_CONTRIBUTOR role can create a structure', () => {
it('a user with INDICATOR_CONTRIBUTOR role can create a document', () => {
render(
<RBACMock roles={[INDICATOR_CONTRIBUTOR]}>
<Menu />
Expand All @@ -30,7 +30,7 @@ describe('Structures Home Page Menu', () => {
screen.getByText('New Document');
});

it('a user with SERIES_CONTRIBUTOR role can create a structure', () => {
it('a user with SERIES_CONTRIBUTOR role can create a document', () => {
render(
<RBACMock roles={[SERIES_CONTRIBUTOR]}>
<Menu />
Expand All @@ -41,7 +41,7 @@ describe('Structures Home Page Menu', () => {
screen.getByText('New Document');
});

it('a user without Admin or INDICATOR_CONTRIBUTOR or SERIES_CONTRIBUTOR role cannot create a structure', () => {
it('a user without Admin or INDICATOR_CONTRIBUTOR or SERIES_CONTRIBUTOR role cannot create a document', () => {
render(
<RBACMock roles={[]}>
<Menu />
Expand Down
102 changes: 102 additions & 0 deletions src/packages/modules-operations/document/visualization/Menu.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { render, screen } from '@testing-library/react';
import { Menu } from './Menu';
import { RBACMock } from '../../../tests-utils/rbac';
import {
ADMIN,
INDICATOR_CONTRIBUTOR,
SERIES_CONTRIBUTOR,
} from '../../../auth/roles';
import { Document } from '../../../model/operations/document';
describe('Document Visualization Page Menu', () => {
it('an admin can create a new structure if he does not have the Gestionnaire_structures_RMESGNCS role', () => {
render(
<RBACMock roles={[ADMIN]}>
<Menu document={{} as Document} type="type" />
</RBACMock>
);

screen.getByText('Update');
});

it('a user with INDICATOR_CONTRIBUTOR role can update a document if the stamp match', () => {
render(
<RBACMock roles={[INDICATOR_CONTRIBUTOR]}>
<Menu
document={
{
id: '1',
sims: [{ creators: ['stamp'] }],
} as Document
}
type="type"
/>
</RBACMock>
);

screen.getByText('Update');
});

it('a user with INDICATOR_CONTRIBUTOR role cannot update a document if the stamp does not match', () => {
render(
<RBACMock roles={[INDICATOR_CONTRIBUTOR]}>
<Menu
document={
{
id: '1',
sims: [{ creators: ['fake'] }],
} as Document
}
type="type"
/>
</RBACMock>
);

expect(screen.queryByText('Update')).toBeNull();
});

it('a user with SERIES_CONTRIBUTOR role can update a document if the stamp match', () => {
render(
<RBACMock roles={[SERIES_CONTRIBUTOR]}>
<Menu
document={
{
id: '1',
sims: [{ creators: ['stamp'] }],
} as Document
}
type="type"
/>
</RBACMock>
);

screen.getByText('Update');
});

it('a user with SERIES_CONTRIBUTOR role cannot update a document if the stamp does not match', () => {
render(
<RBACMock roles={[SERIES_CONTRIBUTOR]}>
<Menu
document={
{
id: '1',
sims: [{ creators: ['fake'] }],
} as Document
}
type="type"
/>
</RBACMock>
);

expect(screen.queryByText('Update')).toBeNull();
});

it('a user without Admin or INDICATOR_CONTRIBUTOR or SERIES_CONTRIBUTOR role cannot create a document', () => {
render(
<RBACMock roles={[]}>
<Menu document={{} as any} type="type" />
</RBACMock>
);

expect(screen.queryByText('Update')).toBeNull();
});
});
61 changes: 61 additions & 0 deletions src/packages/modules-operations/document/visualization/Menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { ActionToolbar, Button, ReturnButton } from '@inseefr/wilco';
import Auth from '../../../auth/components/auth';
import {
ADMIN,
INDICATOR_CONTRIBUTOR,
SERIES_CONTRIBUTOR,
} from '../../../auth/roles';
import D from '../../../deprecated-locales/build-dictionary';
import { useGoBack } from '../../../utils/hooks/useGoBack';
import { Document } from '../../../model/operations/document';

type MenuTypes = {
document: Document;
type: string;
};

const checkContributorRight = (document: Document) => {
return (stamp: string) => {
const sims = document.sims;
if (sims?.length === 0) {
return true;
}
const stamps = sims.map(({ creators }) => creators);
for (let i = 1; i < stamps.length; i++) {
// we first check if all stamps array have the same size.
if (stamps[i - 1].length !== stamps[i].length) {
return false;
}
if (
stamps[i - 1].length > 0 &&
stamps[i - 1].filter((s) => stamps[i].includes(s)).length === 0
) {
return false;
}
}
return stamps[0].includes(stamp);
};
};

export const Menu = ({ document, type }: Readonly<MenuTypes>) => {
const goBack = useGoBack();

return (
<ActionToolbar>
<ReturnButton action={() => goBack('/operations/documents')} />

<Auth
roles={[
ADMIN,
[SERIES_CONTRIBUTOR, checkContributorRight(document)],
[INDICATOR_CONTRIBUTOR, checkContributorRight(document)],
]}
>
<Button
action={`/operations/${type}/${document.id}/modify`}
label={D.btnUpdate}
/>
</Auth>
</ActionToolbar>
);
};
52 changes: 3 additions & 49 deletions src/packages/modules-operations/document/visualization/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,21 @@
import { Button, ActionToolbar, ReturnButton } from '@inseefr/wilco';
import { Loading, PageTitleBlock, CheckSecondLang } from '../../../components';

import { useGoBack } from '../../../utils/hooks/useGoBack';
import { loadCodesList } from '../../../redux/actions/operations/utils/setup';

import D from '../../../deprecated-locales';
import { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useParams, useRouteMatch } from 'react-router-dom';
import OperationsDocumentVisualization from './home';
import {
ADMIN,
INDICATOR_CONTRIBUTOR,
SERIES_CONTRIBUTOR,
} from '../../../auth/roles';

import { getLocales } from '../../../redux/selectors';
import { getSecondLang } from '../../../redux/second-lang';
import { GeneralApi } from '../../../sdk/general-api';
import Auth from '../../../auth/components/auth';
import { Menu } from './Menu';

function getPath(path) {
return path.includes('document') ? 'document' : 'link';
}

const checkContributorRight = (document) => {
return (stamp) => {
const sims = document.sims;
if (sims?.length === 0) {
return true;
}
const stamps = sims.map(({ creators }) => creators);
for (let i = 1; i < stamps.length; i++) {
// we first check if all stamps array have the same size.
if (stamps[i - 1].length !== stamps[i].length) {
return false;
}
if (
stamps[i - 1].length > 0 &&
stamps[i - 1].filter((s) => stamps[i].includes(s)).length === 0
) {
return false;
}
}
return stamps[0].includes(stamp);
};
};

const DocumentationVisualizationContainer = () => {
const { id } = useParams();
const { path } = useRouteMatch();
Expand All @@ -56,7 +26,6 @@ const DocumentationVisualizationContainer = () => {
(state) => state.operationsCodesList.results['ISO-639']
);
const dispatch = useDispatch();
const goBack = useGoBack();

const [document, setDocument] = useState({});
useEffect(() => {
Expand Down Expand Up @@ -84,22 +53,7 @@ const DocumentationVisualizationContainer = () => {
secondLang={secondLang}
/>

<ActionToolbar>
<ReturnButton action={() => goBack('/operations/documents')} />

<Auth
roles={[
ADMIN,
[SERIES_CONTRIBUTOR, checkContributorRight(document)],
[INDICATOR_CONTRIBUTOR, checkContributorRight(document)],
]}
>
<Button
action={`/operations/${type}/${document.id}/modify`}
label={D.btnUpdate}
/>
</Auth>
</ActionToolbar>
<Menu document={document} type={type} />
<CheckSecondLang />

<OperationsDocumentVisualization
Expand Down

0 comments on commit a64fe83

Please sign in to comment.