-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from IQSS/feat/259-restrict-unrestrict-file-u…
…se-case Restrict File use case
- Loading branch information
Showing
10 changed files
with
331 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { IFilesRepository } from '../repositories/IFilesRepository' | ||
import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
|
||
export class RestrictFile implements UseCase<void> { | ||
constructor(private readonly filesRepository: IFilesRepository) {} | ||
|
||
/** | ||
* Restrict or unrestrict an existing file. | ||
* More detailed information about the file restriction behavior can be found in https://guides.dataverse.org/en/latest/api/native-api.html#restrict-files | ||
* | ||
* @param {number | string} [fileId] - The File identifier, which can be a string (for persistent identifiers), or a number (for numeric identifiers). | ||
* @param {boolean} [restrict] - A boolean value that indicates whether the file should be restricted or unrestricted. | ||
* @returns {Promise<void>} -This method does not return anything upon successful completion. | ||
*/ | ||
async execute(fileId: number | string, restrict: boolean): Promise<void> { | ||
return await this.filesRepository.restrictFile(fileId, restrict) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { | ||
ApiConfig, | ||
createDataset, | ||
CreatedDatasetIdentifiers, | ||
restrictFile, | ||
getDatasetFiles, | ||
WriteError | ||
} from '../../../src' | ||
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' | ||
import { | ||
createCollectionViaApi, | ||
deleteCollectionViaApi | ||
} from '../../testHelpers/collections/collectionHelper' | ||
import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper' | ||
import { uploadFileViaApi } from '../../testHelpers/files/filesHelper' | ||
import { TestConstants } from '../../testHelpers/TestConstants' | ||
|
||
describe('execute', () => { | ||
const testCollectionAlias = 'restrictFileFunctionalTest' | ||
let testDatasetIds: CreatedDatasetIdentifiers | ||
const testTextFile1Name = 'test-file-1.txt' | ||
|
||
beforeAll(async () => { | ||
ApiConfig.init( | ||
TestConstants.TEST_API_URL, | ||
DataverseApiAuthMechanism.API_KEY, | ||
process.env.TEST_API_KEY | ||
) | ||
await createCollectionViaApi(testCollectionAlias) | ||
|
||
try { | ||
testDatasetIds = await createDataset.execute( | ||
TestConstants.TEST_NEW_DATASET_DTO, | ||
testCollectionAlias | ||
) | ||
} catch (error) { | ||
throw new Error('Tests beforeAll(): Error while creating test dataset') | ||
} | ||
await uploadFileViaApi(testDatasetIds.numericId, testTextFile1Name).catch(() => { | ||
throw new Error(`Tests beforeAll(): Error while uploading file ${testTextFile1Name}`) | ||
}) | ||
}) | ||
|
||
afterAll(async () => { | ||
try { | ||
await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId) | ||
} catch (error) { | ||
throw new Error('Tests afterAll(): Error while deleting test dataset') | ||
} | ||
try { | ||
await deleteCollectionViaApi(testCollectionAlias) | ||
} catch (error) { | ||
throw new Error('Tests afterAll(): Error while deleting test collection') | ||
} | ||
}) | ||
|
||
test('should successfully restrict a file', async () => { | ||
try { | ||
const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId) | ||
|
||
await restrictFile.execute(datasetFiles.files[0].id, true) | ||
} catch (error) { | ||
throw new Error('File should be deleted') | ||
} finally { | ||
const datasetFilesAfterRestriction = await getDatasetFiles.execute(testDatasetIds.numericId) | ||
|
||
expect(datasetFilesAfterRestriction.files[0].restricted).toEqual(true) | ||
|
||
// Unrestrict the file for the next test | ||
await restrictFile.execute(datasetFilesAfterRestriction.files[0].id, false) | ||
} | ||
}) | ||
|
||
test('should succesfully unrestrict a file', async () => { | ||
try { | ||
const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId) | ||
|
||
await restrictFile.execute(datasetFiles.files[0].id, true) | ||
|
||
await restrictFile.execute(datasetFiles.files[0].id, false) | ||
} catch (error) { | ||
throw new Error('File should be deleted') | ||
} finally { | ||
const datasetFilesAfterRestriction = await getDatasetFiles.execute(testDatasetIds.numericId) | ||
|
||
expect(datasetFilesAfterRestriction.files[0].restricted).toEqual(false) | ||
} | ||
}) | ||
|
||
test('should throw an error when the file id does not exist', async () => { | ||
expect.assertions(2) | ||
let writeError: WriteError | undefined = undefined | ||
const nonExistentFileId = 5 | ||
|
||
try { | ||
await restrictFile.execute(nonExistentFileId, true) | ||
throw new Error('Use case should throw an error') | ||
} catch (error) { | ||
writeError = error as WriteError | ||
} finally { | ||
expect(writeError).toBeInstanceOf(WriteError) | ||
|
||
expect(writeError?.message).toEqual( | ||
`There was an error when writing the resource. Reason was: [400] Could not find datafile with id ${nonExistentFileId}` | ||
) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.