-
Notifications
You must be signed in to change notification settings - Fork 10
Restrict File use case #266
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
Changes from all commits
aa8d7b2
8faf1ba
70c0469
48b9b6d
d8a28a3
42607cc
464b1f1
f283815
3b3f380
8e979e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the UI, the restrict file modal includes an option of changing the default fileAccessRequest value, and adding termsOfAccessForRestrictedFiles. (these values are also in the Terms tab). Do we want to add this as optional params to the restrict method? Or from the frontend, should it be handled by making a separate API call to update Dataset Terms of Use?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question, the API is not accepting params and I think it's ok.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, we don't have a use case for updating terms of use yet, so yes would be good to work on that separately. |
||
| return await this.filesRepository.restrictFile(fileId, restrict) | ||
| } | ||
| } | ||
| 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}` | ||
| ) | ||
| } | ||
| }) | ||
| }) |

Uh oh!
There was an error while loading. Please reload this page.