Skip to content

Commit

Permalink
feat(EditFilesMenu): add dataset permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
MellyGray committed Sep 25, 2023
1 parent 7fa9000 commit 07dc40d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 77 deletions.
4 changes: 0 additions & 4 deletions src/sections/dataset/DatasetProvider.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -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 <></>
}
Expand All @@ -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={<PencilFill className={styles.icon} />}>
<EditFilesOptions files={files} />
</DropdownButton>
Expand Down
1 change: 0 additions & 1 deletion src/stories/dataset/Dataset.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof Dataset> = {
title: 'Pages/Dataset',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof EditFilesMenu> = {
title: 'Sections/Dataset Page/DatasetFiles/FilesTable/EditFilesMenu',
component: EditFilesMenu,
decorators: [WithI18next, WithSettings, WithLoggedInUser, WithFilePermissionsGranted]
decorators: [WithI18next, WithSettings, WithLoggedInUser, WithDatasetAllPermissionsGranted]
}

export default meta
Expand Down
2 changes: 1 addition & 1 deletion tests/component/sections/dataset/Dataset.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 20 in tests/component/sections/dataset/dataset-files/files-table/file-actions/edit-files-menu/EditFilesMenu.spec.tsx

View workflow job for this annotation

GitHub Actions / lint

'fileRepository' is assigned a value but never used. Allowed unused vars must match /^_/u
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 (
<DatasetProvider
repository={datasetRepository}
searchParams={{ persistentId: 'some-persistent-id', version: 'some-version' }}>
{component}
</DatasetProvider>
)
})
}

it('renders the Edit Files menu', () => {
cy.customMount(
<FilePermissionsProvider repository={fileRepository}>
<SessionProvider repository={userRepository}>
<EditFilesMenu files={files} />
</SessionProvider>
</FilePermissionsProvider>
cy.mountAuthenticated(
withDataset(<EditFilesMenu files={files} />, 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(
<FilePermissionsProvider repository={fileRepository}>
<SessionProvider repository={userRepository}>
<EditFilesMenu files={files} />
</SessionProvider>
</FilePermissionsProvider>
)
cy.customMount(withDataset(<EditFilesMenu files={files} />, 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(
<FilePermissionsProvider repository={fileRepository}>
<SessionProvider repository={userRepository}>
<EditFilesMenu files={[]} />
</SessionProvider>
</FilePermissionsProvider>
)
cy.mountAuthenticated(withDataset(<EditFilesMenu files={[]} />, datasetWithUpdatePermissions))

cy.findByRole('button', { name: 'Edit Files' }).should('not.exist')
})

it('renders the Edit Files options', () => {
cy.customMount(
<FilePermissionsProvider repository={fileRepository}>
<SessionProvider repository={userRepository}>
<EditFilesMenu files={files} />
</SessionProvider>
</FilePermissionsProvider>
cy.mountAuthenticated(
withDataset(<EditFilesMenu files={files} />, 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(
<FilePermissionsProvider repository={fileRepository}>
<SessionProvider repository={userRepository}>
<EditFilesMenu files={files} />
</SessionProvider>
</FilePermissionsProvider>
cy.mountAuthenticated(
withDataset(<EditFilesMenu files={files} />, 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(<EditFilesMenu files={files} />, 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(<EditFilesMenu files={files} />, datasetWithUpdatePermissions)
)

cy.findByRole('button', { name: 'Edit Files' }).should('be.disabled')
})
})

0 comments on commit 07dc40d

Please sign in to comment.