-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate document visualization menu
- Loading branch information
1 parent
89eff5c
commit a64fe83
Showing
7 changed files
with
182 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export type Rubric = { | ||
idAttribute: string; | ||
}; | ||
|
||
export type Sims = { | ||
creators: string[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/packages/modules-operations/document/visualization/Menu.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
src/packages/modules-operations/document/visualization/Menu.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters