|
| 1 | +import { |
| 2 | + ApiConfig, |
| 3 | + createDataset, |
| 4 | + CreatedDatasetIdentifiers, |
| 5 | + restrictFile, |
| 6 | + getDatasetFiles, |
| 7 | + WriteError |
| 8 | +} from '../../../src' |
| 9 | +import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' |
| 10 | +import { |
| 11 | + createCollectionViaApi, |
| 12 | + deleteCollectionViaApi |
| 13 | +} from '../../testHelpers/collections/collectionHelper' |
| 14 | +import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper' |
| 15 | +import { uploadFileViaApi } from '../../testHelpers/files/filesHelper' |
| 16 | +import { TestConstants } from '../../testHelpers/TestConstants' |
| 17 | + |
| 18 | +describe('execute', () => { |
| 19 | + const testCollectionAlias = 'restrictFileFunctionalTest' |
| 20 | + let testDatasetIds: CreatedDatasetIdentifiers |
| 21 | + const testTextFile1Name = 'test-file-1.txt' |
| 22 | + |
| 23 | + beforeAll(async () => { |
| 24 | + ApiConfig.init( |
| 25 | + TestConstants.TEST_API_URL, |
| 26 | + DataverseApiAuthMechanism.API_KEY, |
| 27 | + process.env.TEST_API_KEY |
| 28 | + ) |
| 29 | + await createCollectionViaApi(testCollectionAlias) |
| 30 | + |
| 31 | + try { |
| 32 | + testDatasetIds = await createDataset.execute( |
| 33 | + TestConstants.TEST_NEW_DATASET_DTO, |
| 34 | + testCollectionAlias |
| 35 | + ) |
| 36 | + } catch (error) { |
| 37 | + throw new Error('Tests beforeAll(): Error while creating test dataset') |
| 38 | + } |
| 39 | + await uploadFileViaApi(testDatasetIds.numericId, testTextFile1Name).catch(() => { |
| 40 | + throw new Error(`Tests beforeAll(): Error while uploading file ${testTextFile1Name}`) |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + afterAll(async () => { |
| 45 | + try { |
| 46 | + await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId) |
| 47 | + } catch (error) { |
| 48 | + throw new Error('Tests afterAll(): Error while deleting test dataset') |
| 49 | + } |
| 50 | + try { |
| 51 | + await deleteCollectionViaApi(testCollectionAlias) |
| 52 | + } catch (error) { |
| 53 | + throw new Error('Tests afterAll(): Error while deleting test collection') |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + test('should successfully restrict a file', async () => { |
| 58 | + try { |
| 59 | + const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId) |
| 60 | + |
| 61 | + await restrictFile.execute(datasetFiles.files[0].id, true) |
| 62 | + } catch (error) { |
| 63 | + throw new Error('File should be deleted') |
| 64 | + } finally { |
| 65 | + const datasetFilesAfterRestriction = await getDatasetFiles.execute(testDatasetIds.numericId) |
| 66 | + |
| 67 | + expect(datasetFilesAfterRestriction.files[0].restricted).toEqual(true) |
| 68 | + |
| 69 | + // Unrestrict the file for the next test |
| 70 | + await restrictFile.execute(datasetFilesAfterRestriction.files[0].id, false) |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + test('should succesfully unrestrict a file', async () => { |
| 75 | + try { |
| 76 | + const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId) |
| 77 | + |
| 78 | + await restrictFile.execute(datasetFiles.files[0].id, true) |
| 79 | + |
| 80 | + await restrictFile.execute(datasetFiles.files[0].id, false) |
| 81 | + } catch (error) { |
| 82 | + throw new Error('File should be deleted') |
| 83 | + } finally { |
| 84 | + const datasetFilesAfterRestriction = await getDatasetFiles.execute(testDatasetIds.numericId) |
| 85 | + |
| 86 | + expect(datasetFilesAfterRestriction.files[0].restricted).toEqual(false) |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | + test('should throw an error when the file id does not exist', async () => { |
| 91 | + expect.assertions(2) |
| 92 | + let writeError: WriteError | undefined = undefined |
| 93 | + const nonExistentFileId = 5 |
| 94 | + |
| 95 | + try { |
| 96 | + await restrictFile.execute(nonExistentFileId, true) |
| 97 | + throw new Error('Use case should throw an error') |
| 98 | + } catch (error) { |
| 99 | + writeError = error as WriteError |
| 100 | + } finally { |
| 101 | + expect(writeError).toBeInstanceOf(WriteError) |
| 102 | + |
| 103 | + expect(writeError?.message).toEqual( |
| 104 | + `There was an error when writing the resource. Reason was: [400] Could not find datafile with id ${nonExistentFileId}` |
| 105 | + ) |
| 106 | + } |
| 107 | + }) |
| 108 | +}) |
0 commit comments