Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

236 cypress timing error #241

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/files/domain/models/File.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import FileTypeToFriendlyTypeMap from './FileTypeToFriendlyTypeMap'
import { FileUserPermissions } from './FileUserPermissions'

export enum FileSizeUnit {
BYTES = 'B',
Expand Down Expand Up @@ -115,6 +116,7 @@ export enum FileLabelType {
CATEGORY = 'category',
TAG = 'tag'
}

export interface FileLabel {
type: FileLabelType
value: string
Expand Down Expand Up @@ -158,6 +160,7 @@ export class File {
readonly labels: FileLabel[],
public readonly isDeleted: boolean,
public readonly ingest: FileIngest,
public userPermissions?: FileUserPermissions,
readonly checksum?: FileChecksum,
public thumbnail?: string,
readonly directory?: string,
Expand Down
10 changes: 4 additions & 6 deletions src/files/domain/useCases/checkFileDownloadPermission.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { FileRepository } from '../repositories/FileRepository'
import { File, FilePublishingStatus } from '../models/File'

// TODO: remove async when ready and then remove eslint-disable
// eslint-disable-next-line @typescript-eslint/require-await
export async function checkFileDownloadPermission(
fileRepository: FileRepository,
file: File
): Promise<boolean> {
if (file.version.publishingStatus === FilePublishingStatus.DEACCESSIONED) {
return fileRepository.getUserPermissionsById(file.id).then((permissions) => {
return permissions.canEditDataset
})
return file.userPermissions?.canEditDataset || false
}

const isRestricted = file.access.restricted || file.access.latestVersionRestricted
if (!isRestricted && !file.isActivelyEmbargoed) {
return true
}

return fileRepository.getUserPermissionsById(file.id).then((permissions) => {
return permissions.canDownloadFile
})
return file.userPermissions?.canDownloadFile || false
}
7 changes: 3 additions & 4 deletions src/files/domain/useCases/checkFileEditDatasetPermission.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { FileRepository } from '../repositories/FileRepository'
import { File } from '../models/File'

// TODO: remove async when ready and then remove eslint-disable
// eslint-disable-next-line @typescript-eslint/require-await
export async function checkFileEditDatasetPermission(
fileRepository: FileRepository,
file: File
): Promise<boolean> {
return fileRepository.getUserPermissionsById(file.id).then((permissions) => {
return permissions.canEditDataset
})
return file.userPermissions?.canEditDataset || false
}
18 changes: 18 additions & 0 deletions src/files/infrastructure/FileJSDataverseRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class FileJSDataverseRepository implements FileRepository {
.then((jsFiles) => jsFiles.map((jsFile) => JSFileMapper.toFile(jsFile, datasetVersion)))
.then((files) => FileJSDataverseRepository.getAllWithDownloadCount(files))
.then((files) => FileJSDataverseRepository.getAllWithThumbnail(files))
.then((files) => FileJSDataverseRepository.getAllWithPermissions(files))
.catch((error: ReadError) => {
throw new Error(error.message)
})
Expand Down Expand Up @@ -83,6 +84,23 @@ export class FileJSDataverseRepository implements FileRepository {
)
}

private static getAllWithPermissions(files: File[]): Promise<File[]> {
console.log('BEGIN getAllWithPermissions')
return Promise.all(
files.map((file) =>
getFileUserPermissions.execute(file.id).then((jsFileUserPermissions) => {
file.userPermissions = {
fileId: file.id,
canDownloadFile: jsFileUserPermissions.canDownloadFile,
canEditDataset: jsFileUserPermissions.canEditOwnerDataset
}
console.log('jsFileUserPermissions', jsFileUserPermissions)
return file
})
)
)
}

private static getThumbnailById(id: number): Promise<string | undefined> {
return fetch(`${this.DATAVERSE_BACKEND_URL}/api/access/datafile/${id}?imageThumb=400`)
.then((response) => {
Expand Down
1 change: 1 addition & 0 deletions src/files/infrastructure/mappers/JSFileMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class JSFileMapper {
this.toFileLabels(jsFile.categories, jsFile.tabularTags),
false, // TODO - Implement this when it is added to js-dataverse
{ status: FileIngestStatus.NONE }, // TODO - Implement this when it is added to js-dataverse
undefined, // TODO - revisit this, do I need to have a method here?
this.toFileChecksum(jsFile.checksum),
this.toFileThumbnail(),
this.toFileDirectory(jsFile.directoryLabel),
Expand Down
25 changes: 18 additions & 7 deletions src/sections/dataset/dataset-files/useFiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { FileCriteria } from '../../../files/domain/models/FileCriteria'
import { FilesCountInfo } from '../../../files/domain/models/FilesCountInfo'
import { getFilesCountInfoByDatasetPersistentId } from '../../../files/domain/useCases/getFilesCountInfoByDatasetPersistentId'
import { FilePaginationInfo } from '../../../files/domain/models/FilePaginationInfo'
import { useFilePermissions } from '../../file/file-permissions/FilePermissionsContext'
import { FilePermission } from '../../../files/domain/models/FileUserPermissions'
import { DatasetVersion } from '../../../dataset/domain/models/Dataset'
import { getFilesTotalDownloadSize } from '../../../files/domain/useCases/getFilesTotalDownloadSize'

Expand All @@ -19,7 +17,6 @@ export function useFiles(
paginationInfo: FilePaginationInfo,
criteria?: FileCriteria
) {
const { fetchFilesPermission } = useFilePermissions()
const [files, setFiles] = useState<File[]>([])
const [isLoading, setIsLoading] = useState<boolean>(true)
const [filesCountInfo, setFilesCountInfo] = useState<FilesCountInfo>()
Expand All @@ -44,6 +41,7 @@ export function useFiles(
}

const getFiles = (filesCount: FilesCountInfo) => {
console.log('GET FILES', filesCount)
if (filesCount) {
if (filesCount.total === 0) {
setIsLoading(false)
Expand All @@ -58,11 +56,9 @@ export function useFiles(
)
.then((files: File[]) => {
setFiles(files)
setIsLoading(false)
return files
})
.then((files: File[]) =>
fetchFilesPermission(FilePermission.DOWNLOAD_FILE, files).then(() => setIsLoading(false))
)
.catch(() => {
throw new Error('There was an error getting the files')
})
Expand All @@ -71,7 +67,15 @@ export function useFiles(

useEffect(() => {
setIsLoading(true)

console.log(
'USE EFFECT FILE COUNT INFO',
filesRepository,
datasetPersistentId,
datasetVersion,
paginationInfo.page,
paginationInfo.pageSize,
criteria
)
getFilesCountInfo()
.then((filesCount) => getFiles(filesCount))
.catch(() => {
Expand All @@ -88,6 +92,13 @@ export function useFiles(
])

useEffect(() => {
console.log(
'USE EFFECT FILE DOWNLOAD SIZE',
filesRepository,
datasetPersistentId,
datasetVersion,
criteria
)
getFilesTotalDownloadSize(filesRepository, datasetPersistentId, datasetVersion, criteria)
.then((filesTotalDownloadSize: number) => {
setFilesTotalDownloadSize(filesTotalDownloadSize)
Expand Down
22 changes: 11 additions & 11 deletions src/sections/file/file-permissions/useFileDownloadPermission.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { useEffect, useState } from 'react'
import { FilePermission } from '../../../files/domain/models/FileUserPermissions'
import { useFilePermissions } from './FilePermissionsContext'
//import { FilePermission } from '../../../files/domain/models/FileUserPermissions'

import { File } from '../../../files/domain/models/File'

export function useFileDownloadPermission(file: File) {
const { checkSessionUserHasFilePermission } = useFilePermissions()
const [sessionUserHasFileDownloadPermission, setSessionUserHasFileDownloadPermission] =
useState<boolean>(false)

useEffect(() => {
checkSessionUserHasFilePermission(FilePermission.DOWNLOAD_FILE, file)
.then((hasPermission) => {
setSessionUserHasFileDownloadPermission(hasPermission)
})
.catch((error) => {
setSessionUserHasFileDownloadPermission(false)
console.error('There was an error getting the file download permission', error)
})
/*checkSessionUserHasFilePermission(FilePermission.DOWNLOAD_FILE, file)
.then((hasPermission) => {
setSessionUserHasFileDownloadPermission(hasPermission)
})
.catch((error) => {
setSessionUserHasFileDownloadPermission(false)
console.error('There was an error getting the file download permission', error)
})*/
setSessionUserHasFileDownloadPermission(file.userPermissions?.canDownloadFile || false)
}, [file])

return { sessionUserHasFileDownloadPermission }
Expand Down
3 changes: 3 additions & 0 deletions tests/component/files/domain/models/FileMother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class FileEmbargoMother {
return new FileEmbargo(dateAvailable)
}
}

export class FileIngestMother {
static create(props?: Partial<FileIngest>): FileIngest {
return {
Expand Down Expand Up @@ -139,6 +140,7 @@ export class FileMother {
fileMockedData.labels,
fileMockedData.isDeleted,
fileMockedData.ingest,
fileMockedData.userPermissions,
fileMockedData.checksum,
fileMockedData.thumbnail,
fileMockedData.directory,
Expand Down Expand Up @@ -348,6 +350,7 @@ export class FileMother {
}
})
}

static createDeleted(): File {
return this.createDefault({
isDeleted: true
Expand Down
66 changes: 43 additions & 23 deletions tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import { FileHelper } from '../../../shared/files/FileHelper'
import moment from 'moment-timezone'

type Dataset = {
datasetVersion: { metadataBlocks: { citation: { fields: { value: string }[] } } }
datasetVersion: {
metadataBlocks: {
citation: {
fields: {
value: string
}[]
}
}
}
}

describe('Dataset', () => {
Expand Down Expand Up @@ -265,7 +273,9 @@ describe('Dataset', () => {
cy.findByText('Restricted File Icon').should('not.exist')
cy.findByText('Restricted with access Icon').should('exist')

cy.findByRole('button', { name: 'Access File' }).should('exist').click()
cy.findByRole('button', { name: 'Access File' }).as('accessFileButton')
cy.get('@accessFileButton').should('be.visible')
cy.get('@accessFileButton').click()
cy.findByText('Restricted with Access Granted').should('exist')

cy.findByRole('button', { name: 'File Options' }).should('exist').click()
Expand All @@ -281,28 +291,34 @@ describe('Dataset', () => {
)
.its('persistentId')
.then((persistentId: string) => {
/*
http://localhost:8000/api/v1/access/datafile/894/userPermissions
*/
cy.wait(1500) // Wait for the dataset to be published

cy.wrap(TestsUtils.logout())

cy.visit(`/spa/datasets?persistentId=${persistentId}`)
cy.intercept('/api/v1/access/datafile/*/userPermissions').as('userPermissions')

cy.wait(1500) // Wait for the files to be loaded
cy.visit(`/spa/datasets?persistentId=${persistentId}`)

cy.findByText('Files').should('exist')
cy.wait('@userPermissions').then(() => {
cy.wait('@userPermissions').then(() => {
cy.findByText('Files').should('exist')

cy.findByText('Restricted with access Icon').should('not.exist')
cy.findByText('Restricted File Icon').should('exist')
cy.findByText('Restricted with access Icon').should('not.exist')
cy.findByText('Restricted File Icon').should('exist')

// use alias below to avoid a timing error
cy.findByRole('button', { name: 'Access File' }).as('accessButton')
cy.get('@accessButton').should('exist')
cy.get('@accessButton').click()
cy.findByText('Restricted').should('exist')
// use alias below to avoid a timing error
cy.findByRole('button', { name: 'Access File' }).as('accessButton')
cy.get('@accessButton').should('exist')
cy.get('@accessButton').click()
cy.findByText('Restricted').should('exist')
})
})
})
})

it('loads the embargoed files', () => {
it.only('loads the embargoed files', () => {
cy.window().then((win) => {
// Get the browser's locale from the window object
const browserLocale = win.navigator.language
Expand Down Expand Up @@ -337,19 +353,23 @@ describe('Dataset', () => {
.then((persistentId: string) => {
cy.wait(1500) // Wait for the files to be embargoed

cy.visit(`/spa/datasets?persistentId=${persistentId}`)

cy.wait(1500) // Wait for the files to be loaded
cy.intercept('/api/v1/access/datafile/*/userPermissions').as('userPermissions')

cy.findByText('Files').should('exist')
cy.visit(`/spa/datasets?persistentId=${persistentId}`)

cy.findByText(/Deposited/).should('exist')
cy.findByText(`Draft: will be embargoed until ${expectedDate}`).should('exist')
cy.wait('@userPermissions').then(() => {
cy.wait('@userPermissions').then(() => {
cy.findByText('Files').should('exist')
cy.findByText(/Deposited/).should('exist')
cy.findByText(`Draft: will be embargoed until ${expectedDate}`).should('exist')

cy.findByText('Edit Files').should('exist')
cy.findByText('Edit Files').should('exist')

cy.findByRole('button', { name: 'Access File' }).should('exist').click()
cy.findByText('Embargoed').should('exist')
cy.findByRole('button', { name: 'Access File' }).should('be.visible')
cy.findByRole('button', { name: 'Access File' }).should('be.visible').click()
cy.findByText('Embargoed').should('exist')
})
})
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const expectedFile = new File(
[],
false,
{ status: FileIngestStatus.NONE },
undefined,
{
algorithm: 'MD5',
value: '0187a54071542738aa47939e8218e5f2'
Expand Down
Loading