Skip to content

Commit

Permalink
create FilePermissions interface, update FileDownloadOptions unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ekraffmiller committed Jan 8, 2024
1 parent 2a44d99 commit 708d58a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 49 deletions.
9 changes: 6 additions & 3 deletions src/files/domain/models/File.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import FileTypeToFriendlyTypeMap from './FileTypeToFriendlyTypeMap'
import { FileUserPermissions } from './FileUserPermissions'

export enum FileSizeUnit {
BYTES = 'B',
Expand All @@ -9,6 +8,10 @@ export enum FileSizeUnit {
TERABYTES = 'TB',
PETABYTES = 'PB'
}
export interface FilePermissions {
canDownloadFile: boolean
canEditDataset: boolean
}

export class FileSize {
static readonly multiplier = {
Expand Down Expand Up @@ -183,7 +186,7 @@ export class File {
public readonly isDeleted: boolean,
public readonly ingest: FileIngest,
public readonly downloadUrls: FileDownloadUrls,
public userPermissions?: FileUserPermissions,
public filePermissions?: FilePermissions,
public thumbnail?: string,
readonly directory?: string,
readonly embargo?: FileEmbargo,
Expand All @@ -196,7 +199,7 @@ export class File {
return `/file?id=${this.id}&version=${this.version.number}`
}
get userHasDownloadPermission(): boolean {
return this.userPermissions?.canDownloadFile || false
return this.filePermissions?.canDownloadFile || false
}

get isActivelyEmbargoed(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/files/infrastructure/FileJSDataverseRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FileRepository } from '../domain/repositories/FileRepository'
import { File, FileDownloadMode } from '../domain/models/File'
import { File, FileDownloadMode, FilePermissions } from '../domain/models/File'

Check failure on line 2 in src/files/infrastructure/FileJSDataverseRepository.ts

View workflow job for this annotation

GitHub Actions / lint

'FilePermissions' is defined but never used
import { FilesCountInfo } from '../domain/models/FilesCountInfo'
import { FileUserPermissions } from '../domain/models/FileUserPermissions'
import {
Expand Down
14 changes: 12 additions & 2 deletions src/files/infrastructure/mappers/JSFileMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
FileSizeUnit,
FileTabularData,
FileType,
FileVersion
FileVersion,
FilePermissions
} from '../../domain/models/File'
import {
File as JSFile,
Expand Down Expand Up @@ -60,7 +61,7 @@ export class JSFileMapper {
this.toFileIsDeleted(jsFile.deleted),
{ status: FileIngestStatus.NONE }, // TODO - Implement this when it is added to js-dataverse
this.toFileOriginalFileDownloadUrl(jsFile.id),
this.toFileUserPermissions(jsFile.id, fileUserPermissions),
this.toFilePermissions(fileUserPermissions),
this.toFileThumbnail(thumbnail),
this.toFileDirectory(jsFile.directoryLabel),
this.toFileEmbargo(jsFile.embargo),
Expand All @@ -70,6 +71,15 @@ export class JSFileMapper {
)
}

static toFilePermissions(jsFileUserPermissions: {
canDownloadFile: boolean
canEditOwnerDataset: boolean
}): FilePermissions {
return {
canDownloadFile: jsFileUserPermissions.canDownloadFile,
canEditDataset: jsFileUserPermissions.canEditOwnerDataset
}
}
static toFileUserPermissions(
jsFileId: number,
jsFileUserPermissions: {
Expand Down
28 changes: 26 additions & 2 deletions tests/component/files/domain/models/FileMother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
FileSizeUnit,
FilePublishingStatus,
FileType,
FileChecksum
FileChecksum,
FilePermissions
} from '../../../../../src/files/domain/models/File'
import FileTypeToFriendlyTypeMap from '../../../../../src/files/domain/models/FileTypeToFriendlyTypeMap'

Expand All @@ -24,7 +25,24 @@ const createFakeFileLabel = (): FileLabel => ({
type: faker.helpers.arrayElement(Object.values(FileLabelType)),
value: faker.lorem.word()
})
export class FilePermissionsMother {
static create(props?: Partial<FilePermissions>): FilePermissions {
return {
canDownloadFile: faker.datatype.boolean(),
canEditDataset: faker.datatype.boolean(),
...props
}
}

static createWithFileDownloadAllowed(): FilePermissions {
console.log('createWithFileDownloadAllowed')
return this.create({ canDownloadFile: true })
}

static createWithFileDownloadNotAllowed(): FilePermissions {
return this.create({ canDownloadFile: false })
}
}
export class FileEmbargoMother {
static create(dateAvailable?: Date): FileEmbargo {
return new FileEmbargo(dateAvailable ?? faker.date.future())
Expand Down Expand Up @@ -106,6 +124,7 @@ export class FileMother {
: [],
checksum: FileChecksumMother.create(),
thumbnail: thumbnail,
filePermissions: FilePermissionsMother.create(),
directory: valueOrUndefined<string>(faker.system.directoryPath()),
embargo: valueOrUndefined<FileEmbargo>(FileEmbargoMother.create()),
tabularData:
Expand Down Expand Up @@ -140,6 +159,7 @@ export class FileMother {
fileMockedData.isDeleted,
fileMockedData.ingest,
fileMockedData.downloadUrls,
fileMockedData.filePermissions,
fileMockedData.thumbnail,
fileMockedData.directory,
fileMockedData.embargo,
Expand Down Expand Up @@ -173,7 +193,10 @@ export class FileMother {
canBeRequested: false,
requested: false
},
permissions: { canDownload: true },
filePermissions: {
canDownloadFile: false,
canEditDataset: false
},
labels: [],
checksum: undefined,
thumbnail: undefined,
Expand Down Expand Up @@ -233,6 +256,7 @@ export class FileMother {
}

static createNonTabular(props?: Partial<File>): File {
console.log('createNonTabular', props)
return this.createDefault({
type: new FileType('text/plain'),
tabularData: undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,42 @@
import { FileDownloadOptions } from '../../../../../../../../../../src/sections/dataset/dataset-files/files-table/file-actions/file-actions-cell/file-action-buttons/access-file-menu/FileDownloadOptions'
import { FileMother } from '../../../../../../../../files/domain/models/FileMother'
import { FileUserPermissionsMother } from '../../../../../../../../files/domain/models/FileUserPermissionsMother'
import { FilePermissionsProvider } from '../../../../../../../../../../src/sections/file/file-permissions/FilePermissionsProvider'
import { FileRepository } from '../../../../../../../../../../src/files/domain/repositories/FileRepository'
import {
FileMother,
FilePermissionsMother
} from '../../../../../../../../files/domain/models/FileMother'

const fileNonTabular = FileMother.createNonTabular()
const fileTabular = FileMother.createTabular()
const fileRepository = {} as FileRepository
describe('FileDownloadOptions', () => {
beforeEach(() => {
fileRepository.getUserPermissionsById = cy.stub().resolves(
FileUserPermissionsMother.create({
canDownloadFile: true
})
)
})

it('renders the download options header', () => {
cy.customMount(
<FilePermissionsProvider repository={fileRepository}>
<FileDownloadOptions file={fileNonTabular} />
</FilePermissionsProvider>
)
const fileWithPermission = FileMother.createNonTabular({
filePermissions: FilePermissionsMother.createWithFileDownloadAllowed()
})
cy.customMount(<FileDownloadOptions file={fileWithPermission} />)

cy.findByRole('heading', { name: 'Download Options' }).should('exist')
})

it('does not render the download options if the user does not have permissions', () => {
fileRepository.getUserPermissionsById = cy.stub().resolves(
FileUserPermissionsMother.create({
canDownloadFile: false
})
)

cy.customMount(
<FilePermissionsProvider repository={fileRepository}>
<FileDownloadOptions file={fileNonTabular} />
</FilePermissionsProvider>
)
const fileWithoutPermission = FileMother.createNonTabular({
filePermissions: FilePermissionsMother.createWithFileDownloadNotAllowed()
})
cy.customMount(<FileDownloadOptions file={fileWithoutPermission} />)

cy.findByRole('heading', { name: 'Download Options' }).should('not.exist')
})

it('renders the download options for a non-tabular file', () => {
cy.customMount(
<FilePermissionsProvider repository={fileRepository}>
<FileDownloadOptions file={fileNonTabular} />{' '}
</FilePermissionsProvider>
)
const nonTabular = FileMother.createNonTabular({
filePermissions: FilePermissionsMother.createWithFileDownloadAllowed()
})
cy.customMount(<FileDownloadOptions file={nonTabular} />)

cy.findByRole('link', { name: 'Plain Text' }).should('exist')
})

it('renders the download options for a tabular file', () => {
cy.customMount(
<FilePermissionsProvider repository={fileRepository}>
<FileDownloadOptions file={fileTabular} />
</FilePermissionsProvider>
)
const nonTabular = FileMother.createTabular({
filePermissions: FilePermissionsMother.createWithFileDownloadAllowed()
})
cy.customMount(<FileDownloadOptions file={nonTabular} />)

cy.findByRole('link', { name: 'Comma Separated Values (Original File Format)' }).should('exist')
})
Expand Down

0 comments on commit 708d58a

Please sign in to comment.