From 07dc40dc61e3644a1b5c6ad86472a6c8f894e471 Mon Sep 17 00:00:00 2001 From: MellyGray Date: Mon, 25 Sep 2023 18:19:54 +0200 Subject: [PATCH] feat(EditFilesMenu): add dataset permissions --- src/sections/dataset/DatasetProvider.tsx | 4 - .../edit-files-menu/EditFilesMenu.tsx | 10 +- src/stories/dataset/Dataset.stories.tsx | 1 - .../edit-files-menu/EditFilesMenu.stories.tsx | 4 +- .../sections/dataset/Dataset.spec.tsx | 2 +- .../DatasetUploadFilesButton.spec.tsx | 2 - .../edit-files-menu/EditFilesMenu.spec.tsx | 121 +++++++++--------- 7 files changed, 67 insertions(+), 77 deletions(-) diff --git a/src/sections/dataset/DatasetProvider.tsx b/src/sections/dataset/DatasetProvider.tsx index 73e465014..a01655ab7 100644 --- a/src/sections/dataset/DatasetProvider.tsx +++ b/src/sections/dataset/DatasetProvider.tsx @@ -1,9 +1,5 @@ import { PropsWithChildren, useEffect, useState } from 'react' -import { User } from '../../users/domain/models/User' import { DatasetContext } from './DatasetContext' -import { getUser } from '../../users/domain/useCases/getUser' -import { UserRepository } from '../../users/domain/repositories/UserRepository' -import { logOut } from '../../users/domain/useCases/logOut' import { DatasetRepository } from '../../dataset/domain/repositories/DatasetRepository' import { Dataset } from '../../dataset/domain/models/Dataset' import { useLoading } from '../loading/LoadingContext' diff --git a/src/sections/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.tsx b/src/sections/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.tsx index 2ab92b860..c255662df 100644 --- a/src/sections/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.tsx +++ b/src/sections/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.tsx @@ -5,7 +5,7 @@ import styles from './EditFilesMenu.module.scss' import { EditFilesOptions } from './EditFilesOptions' import { File } from '../../../../../../files/domain/models/File' import { useTranslation } from 'react-i18next' -import { useFileEditDatasetPermission } from '../../../../../file/file-permissions/useFileEditDatasetPermission' +import { useDataset } from '../../../../DatasetContext' interface EditFilesMenuProps { files: File[] @@ -14,14 +14,12 @@ const MINIMUM_FILES_COUNT_TO_SHOW_EDIT_FILES_BUTTON = 1 export function EditFilesMenu({ files }: EditFilesMenuProps) { const { t } = useTranslation('files') const { user } = useSession() - const { sessionUserHasEditDatasetPermission } = useFileEditDatasetPermission(files[0] || {}) - const datasetHasValidTermsOfAccess = true // TODO - Implement terms of access validation - const datasetLockedFromEdits = false // TODO - Ask Guillermo if this a dataset property coming from the api + const { dataset } = useDataset() if ( files.length < MINIMUM_FILES_COUNT_TO_SHOW_EDIT_FILES_BUTTON || !user || - !sessionUserHasEditDatasetPermission + !dataset?.permissions.canUpdateDataset ) { return <> } @@ -30,7 +28,7 @@ export function EditFilesMenu({ files }: EditFilesMenuProps) { variant="secondary" id="edit-files-menu" title={t('actions.editFilesMenu.title')} - disabled={datasetLockedFromEdits || !datasetHasValidTermsOfAccess} + disabled={dataset.isLockedFromEdits || !dataset.hasValidTermsOfAccess} icon={}> diff --git a/src/stories/dataset/Dataset.stories.tsx b/src/stories/dataset/Dataset.stories.tsx index f17bfb3ee..44e7a423e 100644 --- a/src/stories/dataset/Dataset.stories.tsx +++ b/src/stories/dataset/Dataset.stories.tsx @@ -14,7 +14,6 @@ import { WithFilePermissionsGranted } from '../files/file-permission/WithFilePer import { WithDataset } from './WithDataset' import { WithDatasetDraftAsOwner } from './WithDatasetDraftAsOwner' import { WithDatasetNotFound } from './WithDatasetNotFound' -import { WithDatasetAllPermissionsGranted } from './WithDatasetAllPermissionsGranted' const meta: Meta = { title: 'Pages/Dataset', diff --git a/src/stories/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.stories.tsx b/src/stories/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.stories.tsx index e85af7831..a50a9cff0 100644 --- a/src/stories/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.stories.tsx +++ b/src/stories/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.stories.tsx @@ -4,12 +4,12 @@ import { WithSettings } from '../../../../../WithSettings' import { FileMother } from '../../../../../../../tests/component/files/domain/models/FileMother' import { EditFilesMenu } from '../../../../../../sections/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu' import { WithLoggedInUser } from '../../../../../WithLoggedInUser' -import { WithFilePermissionsGranted } from '../../../../../files/file-permission/WithFilePermissionsGranted' +import { WithDatasetAllPermissionsGranted } from '../../../../WithDatasetAllPermissionsGranted' const meta: Meta = { title: 'Sections/Dataset Page/DatasetFiles/FilesTable/EditFilesMenu', component: EditFilesMenu, - decorators: [WithI18next, WithSettings, WithLoggedInUser, WithFilePermissionsGranted] + decorators: [WithI18next, WithSettings, WithLoggedInUser, WithDatasetAllPermissionsGranted] } export default meta diff --git a/tests/component/sections/dataset/Dataset.spec.tsx b/tests/component/sections/dataset/Dataset.spec.tsx index aa6cb6b7d..d068854c6 100644 --- a/tests/component/sections/dataset/Dataset.spec.tsx +++ b/tests/component/sections/dataset/Dataset.spec.tsx @@ -17,7 +17,7 @@ describe('Dataset', () => { const mountWithDataset = ( component: ReactNode, dataset: DatasetModel | undefined, - anonymizedView: boolean = false + anonymizedView = false ) => { const searchParams = anonymizedView ? { privateUrlToken: 'some-private-url-token' } diff --git a/tests/component/sections/dataset/dataset-files/dataset-upload-files-button/DatasetUploadFilesButton.spec.tsx b/tests/component/sections/dataset/dataset-files/dataset-upload-files-button/DatasetUploadFilesButton.spec.tsx index a24914468..24c3ff587 100644 --- a/tests/component/sections/dataset/dataset-files/dataset-upload-files-button/DatasetUploadFilesButton.spec.tsx +++ b/tests/component/sections/dataset/dataset-files/dataset-upload-files-button/DatasetUploadFilesButton.spec.tsx @@ -14,8 +14,6 @@ const datasetWithUpdatePermissions = DatasetMother.create({ permissions: DatasetPermissionsMother.createWithUpdateDatasetAllowed() }) describe('DatasetUploadFilesButton', () => { - beforeEach(() => {}) - const withDataset = (component: ReactNode, dataset: DatasetModel | undefined) => { datasetRepository.getByPersistentId = cy.stub().resolves(dataset) datasetRepository.getByPrivateUrlToken = cy.stub().resolves(dataset) diff --git a/tests/component/sections/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.spec.tsx b/tests/component/sections/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.spec.tsx index db841720a..bbff4919a 100644 --- a/tests/component/sections/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.spec.tsx +++ b/tests/component/sections/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.spec.tsx @@ -1,102 +1,101 @@ import { EditFilesMenu } from '../../../../../../../../src/sections/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu' -import { UserMother } from '../../../../../../users/domain/models/UserMother' -import { UserRepository } from '../../../../../../../../src/users/domain/repositories/UserRepository' -import { SessionProvider } from '../../../../../../../../src/sections/session/SessionProvider' import { FileMother } from '../../../../../../files/domain/models/FileMother' import { FileRepository } from '../../../../../../../../src/files/domain/repositories/FileRepository' -import { FileUserPermissionsMother } from '../../../../../../files/domain/models/FileUserPermissionsMother' -import { FilePermissionsProvider } from '../../../../../../../../src/sections/file/file-permissions/FilePermissionsProvider' +import { ReactNode } from 'react' +import { Dataset as DatasetModel } from '../../../../../../../../src/dataset/domain/models/Dataset' +import { DatasetProvider } from '../../../../../../../../src/sections/dataset/DatasetProvider' +import { DatasetRepository } from '../../../../../../../../src/dataset/domain/repositories/DatasetRepository' +import { + DatasetLockMother, + DatasetMother, + DatasetPermissionsMother +} from '../../../../../../dataset/domain/models/DatasetMother' -const user = UserMother.create() -const userRepository = {} as UserRepository +const datasetRepository: DatasetRepository = {} as DatasetRepository +const datasetWithUpdatePermissions = DatasetMother.create({ + permissions: DatasetPermissionsMother.createWithUpdateDatasetAllowed(), + hasValidTermsOfAccess: true +}) const files = FileMother.createMany(2) const fileRepository: FileRepository = {} as FileRepository describe('EditFilesMenu', () => { - beforeEach(() => { - userRepository.getAuthenticated = cy.stub().resolves(user) - userRepository.removeAuthenticated = cy.stub().resolves() - fileRepository.getUserPermissionsById = cy.stub().resolves( - FileUserPermissionsMother.create({ - fileId: files[0].id, - canEditDataset: true - }) + const withDataset = (component: ReactNode, dataset: DatasetModel | undefined) => { + datasetRepository.getByPersistentId = cy.stub().resolves(dataset) + datasetRepository.getByPrivateUrlToken = cy.stub().resolves(dataset) + + return ( + + {component} + ) - }) + } + it('renders the Edit Files menu', () => { - cy.customMount( - - - - - + cy.mountAuthenticated( + withDataset(, datasetWithUpdatePermissions) ) cy.findByRole('button', { name: 'Edit Files' }).should('exist') }) it('does not render the Edit Files menu when the user is not authenticated', () => { - userRepository.getAuthenticated = cy.stub().resolves(null) - - cy.customMount( - - - - - - ) + cy.customMount(withDataset(, datasetWithUpdatePermissions)) cy.findByRole('button', { name: 'Edit Files' }).should('not.exist') }) it('does not render the Edit Files menu when there are no files in the dataset', () => { - cy.customMount( - - - - - - ) + cy.mountAuthenticated(withDataset(, datasetWithUpdatePermissions)) cy.findByRole('button', { name: 'Edit Files' }).should('not.exist') }) it('renders the Edit Files options', () => { - cy.customMount( - - - - - + cy.mountAuthenticated( + withDataset(, datasetWithUpdatePermissions) ) cy.findByRole('button', { name: 'Edit Files' }).click() cy.findByRole('button', { name: 'Metadata' }).should('exist') }) - it.skip('does not render the Edit Files menu when the user does not have update dataset permissions', () => { - fileRepository.getUserPermissionsById = cy.stub().resolves( - FileUserPermissionsMother.create({ - fileId: files[0].id, - canEditDataset: false - }) - ) + it('does not render the Edit Files menu when the user does not have update dataset permissions', () => { + const datasetWithNoUpdatePermissions = DatasetMother.create({ + permissions: DatasetPermissionsMother.createWithUpdateDatasetNotAllowed() + }) - cy.customMount( - - - - - + cy.mountAuthenticated( + withDataset(, datasetWithNoUpdatePermissions) ) cy.findByRole('button', { name: 'Edit Files' }).should('not.exist') }) - it.skip('renders the disabled Edit Files menu when the dataset is locked from edits', () => { - // TODO: Implement this test + it('renders the disabled Edit Files menu when the dataset is locked from edits', () => { + const datasetWithUpdatePermissions = DatasetMother.create({ + permissions: DatasetPermissionsMother.createWithUpdateDatasetAllowed(), + locks: [DatasetLockMother.createLockedInEditInProgress()] + }) + + cy.mountAuthenticated( + withDataset(, datasetWithUpdatePermissions) + ) + + cy.findByRole('button', { name: 'Edit Files' }).should('be.disabled') }) - it.skip('renders the disabled Edit Files menu when the dataset does not have valid terms of access', () => { - // TODO: Implement this test + it('renders the disabled Edit Files menu when the dataset does not have valid terms of access', () => { + const datasetWithUpdatePermissions = DatasetMother.create({ + permissions: DatasetPermissionsMother.createWithUpdateDatasetAllowed(), + hasValidTermsOfAccess: false + }) + + cy.mountAuthenticated( + withDataset(, datasetWithUpdatePermissions) + ) + + cy.findByRole('button', { name: 'Edit Files' }).should('be.disabled') }) })