From a2dc047800eedd93f2b63ab2b499cab7422b8d5d Mon Sep 17 00:00:00 2001 From: GPortas Date: Thu, 19 Oct 2023 14:29:45 +0100 Subject: [PATCH] Added: integration tests and tweaks to GetDatasetLocks --- src/datasets/domain/models/DatasetLock.ts | 2 +- .../transformers/datasetLocksTransformers.ts | 2 +- .../datasets/DatasetsRepository.test.ts | 44 +++++++++++++++++-- test/testHelpers/datasets/datasetHelper.ts | 8 ++++ .../testHelpers/datasets/datasetLockHelper.ts | 4 +- test/testHelpers/datasets/test-dataset.json | 5 +++ 6 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/datasets/domain/models/DatasetLock.ts b/src/datasets/domain/models/DatasetLock.ts index 0651f2d7..428c452b 100644 --- a/src/datasets/domain/models/DatasetLock.ts +++ b/src/datasets/domain/models/DatasetLock.ts @@ -2,7 +2,7 @@ export interface DatasetLock { lockType: DatasetLockType; date?: string; userId: string; - datasetId: string; + datasetPersistentId: string; message?: string; } diff --git a/src/datasets/infra/repositories/transformers/datasetLocksTransformers.ts b/src/datasets/infra/repositories/transformers/datasetLocksTransformers.ts index 5df95404..14e9d4e1 100644 --- a/src/datasets/infra/repositories/transformers/datasetLocksTransformers.ts +++ b/src/datasets/infra/repositories/transformers/datasetLocksTransformers.ts @@ -17,7 +17,7 @@ const transformDatasetLockPayloadToDatasetLock = (datasetLockPayload: any): Data lockType: datasetLockPayload.lockType as DatasetLockType, ...(datasetLockPayload.date && { date: datasetLockPayload.date }), userId: datasetLockPayload.user, - datasetId: datasetLockPayload.dataset, + datasetPersistentId: datasetLockPayload.dataset, ...(datasetLockPayload.message && { message: datasetLockPayload.message }), }; }; diff --git a/test/integration/datasets/DatasetsRepository.test.ts b/test/integration/datasets/DatasetsRepository.test.ts index c488f9ee..de20aeee 100644 --- a/test/integration/datasets/DatasetsRepository.test.ts +++ b/test/integration/datasets/DatasetsRepository.test.ts @@ -2,9 +2,13 @@ import { DatasetsRepository } from '../../../src/datasets/infra/repositories/Dat import { assert } from 'sinon'; import { ApiConfig, DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'; import { TestConstants } from '../../testHelpers/TestConstants'; -import { createDatasetViaApi, createPrivateUrlViaApi } from '../../testHelpers/datasets/datasetHelper'; +import { + createDatasetViaApi, + createPrivateUrlViaApi, + publishDatasetViaApi, +} from '../../testHelpers/datasets/datasetHelper'; import { ReadError } from '../../../src/core/domain/repositories/ReadError'; -import { DatasetNotNumberedVersion } from '../../../src/datasets'; +import { DatasetNotNumberedVersion, DatasetLockType } from '../../../src/datasets'; describe('DatasetsRepository', () => { const sut: DatasetsRepository = new DatasetsRepository(); @@ -150,7 +154,7 @@ describe('DatasetsRepository', () => { assert.match(actual.canDeleteDatasetDraft, true); }); - test('should return error when file does not exist', async () => { + test('should return error when dataset does not exist', async () => { let error: ReadError = undefined; await sut.getDatasetUserPermissions(nonExistentTestDatasetId).catch((e) => (error = e)); @@ -161,5 +165,39 @@ describe('DatasetsRepository', () => { ); }); }); + + describe('getDatasetLocks', () => { + test('should return list of dataset locks by dataset id for a dataset while publishing', async () => { + // We create a new dataset + await createDatasetViaApi() + .then() + .catch(() => { + assert.fail('Error while creating test Dataset'); + }); + const createdDatasetId = 3; + // We publish the new test dataset so it will create a lock during publishing + await publishDatasetViaApi(createdDatasetId) + .then() + .catch(() => { + assert.fail('Error while publishing test Dataset'); + }); + const actual = await sut.getDatasetLocks(createdDatasetId); + assert.match(actual.length, 1); + assert.match(actual[0].lockType, DatasetLockType.FINALIZE_PUBLICATION); + assert.match(actual[0].userId, 'dataverseAdmin'); + assert.match(actual[0].message, 'Publishing the dataset; Validating Datafiles Asynchronously'); + }); + + test('should return error when dataset does not exist', async () => { + let error: ReadError = undefined; + + await sut.getDatasetLocks(nonExistentTestDatasetId).catch((e) => (error = e)); + + assert.match( + error.message, + `There was an error when reading the resource. Reason was: [404] Dataset with ID ${nonExistentTestDatasetId} not found.`, + ); + }); + }); }); }); diff --git a/test/testHelpers/datasets/datasetHelper.ts b/test/testHelpers/datasets/datasetHelper.ts index 54ef8388..dc1119a9 100644 --- a/test/testHelpers/datasets/datasetHelper.ts +++ b/test/testHelpers/datasets/datasetHelper.ts @@ -204,6 +204,14 @@ export const createDatasetViaApi = async (): Promise => { ); }; +export const publishDatasetViaApi = async (datasetId: number): Promise => { + return await axios.post( + `${TestConstants.TEST_API_URL}/datasets/${datasetId}/actions/:publish?type=major`, + {}, + DATAVERSE_API_REQUEST_HEADERS, + ); +}; + export const createPrivateUrlViaApi = async (datasetId: number): Promise => { return await axios.post( `${TestConstants.TEST_API_URL}/datasets/${datasetId}/privateUrl`, diff --git a/test/testHelpers/datasets/datasetLockHelper.ts b/test/testHelpers/datasets/datasetLockHelper.ts index e0f42eeb..5aa667b9 100644 --- a/test/testHelpers/datasets/datasetLockHelper.ts +++ b/test/testHelpers/datasets/datasetLockHelper.ts @@ -5,7 +5,7 @@ export const createDatasetLockModel = (): DatasetLock => { lockType: DatasetLockType.EDIT_IN_PROGRESS, date: '2023-05-15T08:21:03Z', userId: '1', - datasetId: '2', + datasetPersistentId: 'doi:10.5072/FK2/QYOVTJ', message: 'Test.', }; }; @@ -15,7 +15,7 @@ export const createDatasetLockPayload = (): any => { lockType: DatasetLockType.EDIT_IN_PROGRESS.toString(), date: '2023-05-15T08:21:03Z', user: '1', - dataset: '2', + dataset: 'doi:10.5072/FK2/QYOVTJ', message: 'Test.', }; }; diff --git a/test/testHelpers/datasets/test-dataset.json b/test/testHelpers/datasets/test-dataset.json index 24a8f78f..3c11cb34 100644 --- a/test/testHelpers/datasets/test-dataset.json +++ b/test/testHelpers/datasets/test-dataset.json @@ -1,5 +1,10 @@ { "datasetVersion": { + "license": { + "name": "CC0 1.0", + "uri": "http://creativecommons.org/publicdomain/zero/1.0", + "iconUri": "https://licensebuttons.net/p/zero/1.0/88x31.png" + }, "metadataBlocks": { "citation": { "fields": [