diff --git a/package-lock.json b/package-lock.json index 9cab908e7..ae9b44949 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,6 +24,7 @@ "i18next": "22.4.9", "i18next-browser-languagedetector": "7.0.1", "i18next-http-backend": "2.1.1", + "moment-timezone": "^0.5.43", "react-bootstrap": "2.7.2", "react-bootstrap-icons": "1.10.3", "react-i18next": "12.1.5", @@ -31965,6 +31966,25 @@ "integrity": "sha512-23g5BFj4zdQL/b6tor7Ji+QY4pEfNH784BMslY9Qb0UnJWRAt+lQGLYmRaM0KDBwIG23ffEBELhZDP2rhi9f/Q==", "dev": true }, + "node_modules/moment": { + "version": "2.29.4", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz", + "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==", + "engines": { + "node": "*" + } + }, + "node_modules/moment-timezone": { + "version": "0.5.43", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.43.tgz", + "integrity": "sha512-72j3aNyuIsDxdF1i7CEgV2FfxM1r6aaqJyLB2vwb33mXYyoyLly+F1zbWqhA3/bVIoJ4szlUoMbUnVdid32NUQ==", + "dependencies": { + "moment": "^2.29.4" + }, + "engines": { + "node": "*" + } + }, "node_modules/mri": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/mri/-/mri-1.2.0.tgz", diff --git a/package.json b/package.json index 0c61160a5..6c826d6b0 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "i18next": "22.4.9", "i18next-browser-languagedetector": "7.0.1", "i18next-http-backend": "2.1.1", + "moment-timezone": "^0.5.43", "react-bootstrap": "2.7.2", "react-bootstrap-icons": "1.10.3", "react-i18next": "12.1.5", diff --git a/src/files/infrastructure/mappers/DomainFileMapper.ts b/src/files/infrastructure/mappers/DomainFileMapper.ts index cda15bf92..71082838d 100644 --- a/src/files/infrastructure/mappers/DomainFileMapper.ts +++ b/src/files/infrastructure/mappers/DomainFileMapper.ts @@ -13,7 +13,10 @@ import { import { FileType } from '../../domain/models/File' export class DomainFileMapper { - static toJSPagination(paginationInfo: FilePaginationInfo): { limit?: number; offset?: number } { + static toJSPagination(paginationInfo: FilePaginationInfo): { + limit?: number + offset?: number + } { return { limit: paginationInfo.pageSize, offset: (paginationInfo.page - 1) * paginationInfo.pageSize diff --git a/tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx b/tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx index ef743d439..4d963130f 100644 --- a/tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx +++ b/tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx @@ -2,6 +2,7 @@ import { DatasetLabelValue } from '../../../../../src/dataset/domain/models/Data import { TestsUtils } from '../../../shared/TestsUtils' import { DatasetHelper } from '../../../shared/datasets/DatasetHelper' import { FileHelper } from '../../../shared/files/FileHelper' +import moment from 'moment-timezone' type Dataset = { datasetVersion: { metadataBlocks: { citation: { fields: { value: string }[] } } } @@ -265,39 +266,64 @@ describe('Dataset', () => { cy.findByText('Restricted with access Icon').should('not.exist') cy.findByText('Restricted File Icon').should('exist') - cy.findByRole('button', { name: 'Access File' }).should('exist').click() + // 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', () => { - cy.wrap( - DatasetHelper.createWithFiles(FileHelper.createMany(1)).then((dataset) => - DatasetHelper.embargoFiles( - dataset.persistentId, - [dataset.files ? dataset.files[0].id : 0], - '2100-10-20' + cy.window().then((win) => { + // Get the browser's locale from the window object + const browserLocale = win.navigator.language + + // Create a moment object in UTC and set the time to 12 AM (midnight) + const utcDate = moment.utc().startOf('day') + + // Add 100 years to the UTC date + utcDate.add(100, 'years') + const dateString = utcDate.format('YYYY-MM-DD') + + // Use the browser's locale to format the date using Intl.DateTimeFormat + const options: Intl.DateTimeFormatOptions = { + year: 'numeric', + month: 'short', + day: 'numeric' + } + const expectedDate = new Intl.DateTimeFormat(browserLocale, options).format( + utcDate.toDate() + ) + + cy.wrap( + DatasetHelper.createWithFiles(FileHelper.createMany(1)).then((dataset) => + DatasetHelper.embargoFiles( + dataset.persistentId, + [dataset.files ? dataset.files[0].id : 0], + dateString + ) ) ) - ) - .its('persistentId') - .then((persistentId: string) => { - cy.wait(1500) // Wait for the files to be embargoed + .its('persistentId') + .then((persistentId: string) => { + cy.wait(1500) // Wait for the files to be embargoed - cy.visit(`/spa/datasets?persistentId=${persistentId}`) + cy.visit(`/spa/datasets?persistentId=${persistentId}`) - cy.wait(1500) // Wait for the files to be loaded + cy.wait(1500) // Wait for the files to be loaded - cy.findByText('Files').should('exist') + cy.findByText('Files').should('exist') - cy.findByText(/Deposited/).should('exist') - cy.findByText('Draft: will be embargoed until Oct 20, 2100').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('exist').click() + cy.findByText('Embargoed').should('exist') + }) + }) }) it('applies filters to the Files Table in the correct order', () => { diff --git a/tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts b/tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts index 65f01dbfc..796d88044 100644 --- a/tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts +++ b/tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts @@ -113,9 +113,7 @@ describe('Dataset JSDataverse Repository', () => { it('gets the dataset by persistentId and version number', async () => { const datasetResponse = await DatasetHelper.create() await DatasetHelper.publish(datasetResponse.persistentId) - - await TestsUtils.wait(1500) - + await TestsUtils.waitForNoLocks(datasetResponse.persistentId) await datasetRepository .getByPersistentId(datasetResponse.persistentId, '1.0') .then((dataset) => { @@ -177,7 +175,7 @@ describe('Dataset JSDataverse Repository', () => { const datasetResponse = await DatasetHelper.create() await DatasetHelper.publish(datasetResponse.persistentId) - await TestsUtils.wait(1500) + await TestsUtils.waitForNoLocks(datasetResponse.persistentId) await DatasetHelper.setCitationDateFieldType(datasetResponse.persistentId, 'dateOfDeposit') diff --git a/tests/e2e-integration/integration/files/FileJSDataverseRepository.spec.ts b/tests/e2e-integration/integration/files/FileJSDataverseRepository.spec.ts index d08c7bbd4..09c7de97c 100644 --- a/tests/e2e-integration/integration/files/FileJSDataverseRepository.spec.ts +++ b/tests/e2e-integration/integration/files/FileJSDataverseRepository.spec.ts @@ -129,7 +129,7 @@ describe('File JSDataverse Repository', () => { if (!dataset) throw new Error('Dataset not found') await DatasetHelper.publish(dataset.persistentId) - await TestsUtils.wait(1500) // Wait for the dataset to be published + await TestsUtils.waitForNoLocks(dataset.persistentId) // Wait for the dataset to be published await fileRepository .getAllByDatasetPersistentId( @@ -163,7 +163,7 @@ describe('File JSDataverse Repository', () => { if (!dataset) throw new Error('Dataset not found') await DatasetHelper.publish(dataset.persistentId) - await TestsUtils.wait(1500) // Wait for the dataset to be published + await TestsUtils.waitForNoLocks(dataset.persistentId) // Wait for the dataset to be published DatasetHelper.deaccession(dataset.persistentId) await TestsUtils.wait(1500) // Wait for the dataset to be deaccessioned @@ -198,7 +198,7 @@ describe('File JSDataverse Repository', () => { if (!datasetResponse.files) throw new Error('Files not found') await DatasetHelper.publish(datasetResponse.persistentId) - await TestsUtils.wait(1500) // Wait for the dataset to be published + await TestsUtils.waitForNoLocks(datasetResponse.persistentId) // Wait for the dataset to be published const dataset = await datasetRepository.getByPersistentId(datasetResponse.persistentId) if (!dataset) throw new Error('Dataset not found') @@ -237,8 +237,7 @@ describe('File JSDataverse Repository', () => { it('gets all the files by dataset persistentId after adding tag labels to the files', async () => { const datasetResponse = await DatasetHelper.createWithFiles(FileHelper.createMany(1, 'csv')) if (!datasetResponse.files) throw new Error('Files not found') - await TestsUtils.wait(1500) // Wait for the tabular data to be ingested - + await TestsUtils.waitForNoLocks(datasetResponse.persistentId) // Wait for the tabular data to be ingested const dataset = await datasetRepository.getByPersistentId(datasetResponse.persistentId) if (!dataset) throw new Error('Dataset not found') @@ -282,7 +281,7 @@ describe('File JSDataverse Repository', () => { [datasetResponse.files[0].id, datasetResponse.files[1].id, datasetResponse.files[2].id], embargoDate ) - await TestsUtils.wait(1500) // Wait for the files to be embargoed + await TestsUtils.waitForNoLocks(datasetResponse.persistentId) // Wait for the files to be embargoed await fileRepository .getAllByDatasetPersistentId(dataset.persistentId, dataset.version) @@ -507,11 +506,11 @@ describe('File JSDataverse Repository', () => { total: 6, perAccess: [ { - access: FileAccessOption.RESTRICTED, + access: FileAccessOption.PUBLIC, count: 3 }, { - access: FileAccessOption.PUBLIC, + access: FileAccessOption.RESTRICTED, count: 3 } ], @@ -673,7 +672,7 @@ describe('File JSDataverse Repository', () => { ) if (!dataset) throw new Error('Dataset not found') - await TestsUtils.wait(2500) // wait for the files to be ingested + await TestsUtils.waitForNoLocks(dataset.persistentId) // wait for the files to be ingested const expectedTotalDownloadSize = await fileRepository .getAllByDatasetPersistentId(dataset.persistentId, dataset.version) @@ -711,7 +710,7 @@ describe('File JSDataverse Repository', () => { ) if (!dataset) throw new Error('Dataset not found') - await TestsUtils.wait(2500) // wait for the files to be ingested + await TestsUtils.waitForNoLocks(dataset.persistentId) // wait for the files to be ingested const expectedTotalDownloadSize = await fileRepository .getAllByDatasetPersistentId( diff --git a/tests/e2e-integration/shared/TestsUtils.ts b/tests/e2e-integration/shared/TestsUtils.ts index 1ad0c0bdb..f6508dcfd 100644 --- a/tests/e2e-integration/shared/TestsUtils.ts +++ b/tests/e2e-integration/shared/TestsUtils.ts @@ -2,6 +2,7 @@ import { ApiConfig } from '@iqss/dataverse-client-javascript/dist/core' import { DataverseApiHelper } from './DataverseApiHelper' import { DataverseApiAuthMechanism } from '@iqss/dataverse-client-javascript/dist/core/infra/repositories/ApiConfig' import { UserJSDataverseRepository } from '../../../src/users/infrastructure/repositories/UserJSDataverseRepository' +import { DatasetHelper } from './datasets/DatasetHelper' export class TestsUtils { static readonly DATAVERSE_BACKEND_URL = @@ -25,4 +26,33 @@ export class TestsUtils { static logout() { return new UserJSDataverseRepository().removeAuthenticated() } + + static async waitForNoLocks(persistentId: string, maxRetries = 20, delay = 1000): Promise { + await this.checkForLocks(persistentId, maxRetries, delay) + } + + private static async checkForLocks( + persistentId: string, + maxRetries: number, + delay: number + ): Promise { + let retry = 0 + + while (retry < maxRetries) { + const response = await DatasetHelper.getLocks(persistentId) + console.log('Checking locks: ' + JSON.stringify(response)) + + // The response will have a single key if there are no locks + if (Object.keys(response).length === 1) { + console.log('No locks found.') + return + } + + retry++ + await this.wait(delay) + } + + console.log('Max retries reached.') + throw new Error('Max retries reached.') + } } diff --git a/tests/e2e-integration/shared/datasets/DatasetHelper.ts b/tests/e2e-integration/shared/datasets/DatasetHelper.ts index 32143ec12..45b9eb03b 100644 --- a/tests/e2e-integration/shared/datasets/DatasetHelper.ts +++ b/tests/e2e-integration/shared/datasets/DatasetHelper.ts @@ -17,11 +17,24 @@ export class DatasetHelper extends DataverseApiHelper { return this.request(`/dataverses/root/datasets`, 'POST', newDatasetData) } - static async publish(persistentId: string): Promise<{ status: string; persistentId: string }> { - const response = await this.request<{ status: string }>( - `/datasets/:persistentId/actions/:publish?persistentId=${persistentId}&type=major`, - 'POST' - ) + static async publish(persistentId: string): Promise<{ + status: string + persistentId: string + }> { + const response = await this.request<{ + status: string + }>(`/datasets/:persistentId/actions/:publish?persistentId=${persistentId}&type=major`, 'POST') + + return { ...response, persistentId } + } + + static async getLocks(persistentId: string): Promise<{ + status: string + persistentId: string + }> { + const response = await this.request<{ + status: string + }>(`/datasets/:persistentId/locks?persistentId=${persistentId}`, 'GET') return { ...response, persistentId } } @@ -45,15 +58,20 @@ export class DatasetHelper extends DataverseApiHelper { .click() } - static async createPrivateUrl(id: string): Promise<{ token: string }> { - return this.request<{ token: string }>(`/datasets/${id}/privateUrl`, 'POST') + static async createPrivateUrl(id: string): Promise<{ + token: string + }> { + return this.request<{ + token: string + }>(`/datasets/${id}/privateUrl`, 'POST') } - static async createPrivateUrlAnonymized(id: string): Promise<{ token: string }> { - return this.request<{ token: string }>( - `/datasets/${id}/privateUrl?anonymizedAccess=true`, - 'POST' - ) + static async createPrivateUrlAnonymized(id: string): Promise<{ + token: string + }> { + return this.request<{ + token: string + }>(`/datasets/${id}/privateUrl?anonymizedAccess=true`, 'POST') } static async createWithFiles(filesData: FileData[]): Promise { @@ -92,7 +110,15 @@ export class DatasetHelper extends DataverseApiHelper { datasetPersistentId: string, fileData: FileData ): Promise { - const { files } = await this.request<{ files: [{ dataFile: { id: number } }] }>( + const { files } = await this.request<{ + files: [ + { + dataFile: { + id: number + } + } + ] + }>( `/datasets/:persistentId/add?persistentId=${datasetPersistentId}`, 'POST', fileData, @@ -108,8 +134,12 @@ export class DatasetHelper extends DataverseApiHelper { static async setCitationDateFieldType( persistentId: string, fieldType: string - ): Promise<{ status: string }> { - return this.request<{ status: string }>( + ): Promise<{ + status: string + }> { + return this.request<{ + status: string + }>( `/datasets/:persistentId/citationdate?persistentId=${persistentId}`, 'PUT', fieldType,