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

210 fix e2e tests #216

Merged
merged 14 commits into from
Nov 23, 2023
Merged
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
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion src/files/infrastructure/mappers/DomainFileMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 46 additions & 20 deletions tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }[] } } }
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
],
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
30 changes: 30 additions & 0 deletions tests/e2e-integration/shared/TestsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -25,4 +26,33 @@ export class TestsUtils {
static logout() {
return new UserJSDataverseRepository().removeAuthenticated()
}

static async waitForNoLocks(persistentId: string, maxRetries = 20, delay = 1000): Promise<void> {
await this.checkForLocks(persistentId, maxRetries, delay)
}

private static async checkForLocks(
persistentId: string,
maxRetries: number,
delay: number
): Promise<void> {
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.')
}
}
60 changes: 45 additions & 15 deletions tests/e2e-integration/shared/datasets/DatasetHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,24 @@ export class DatasetHelper extends DataverseApiHelper {
return this.request<DatasetResponse>(`/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 }
}
Expand All @@ -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<DatasetResponse> {
Expand Down Expand Up @@ -92,7 +110,15 @@ export class DatasetHelper extends DataverseApiHelper {
datasetPersistentId: string,
fileData: FileData
): Promise<DatasetFileResponse> {
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,
Expand All @@ -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,
Expand Down