|
| 1 | +import * as crypto from 'crypto' |
1 | 2 | import { FilesRepository } from '../../../src/files/infra/repositories/FilesRepository' |
2 | 3 | import { |
3 | 4 | ApiConfig, |
@@ -39,10 +40,12 @@ import { |
39 | 40 | import { |
40 | 41 | createCollectionViaApi, |
41 | 42 | deleteCollectionViaApi, |
| 43 | + publishCollectionViaApi, |
42 | 44 | setStorageDriverViaApi |
43 | 45 | } from '../../testHelpers/collections/collectionHelper' |
44 | 46 | import { RestrictFileDTO } from '../../../src/files/domain/dtos/RestrictFileDTO' |
45 | 47 | import { DatasetsRepository } from '../../../src/datasets/infra/repositories/DatasetsRepository' |
| 48 | +import { DirectUploadClient } from '../../../src/files/infra/clients/DirectUploadClient' |
46 | 49 |
|
47 | 50 | describe('FilesRepository', () => { |
48 | 51 | const sut: FilesRepository = new FilesRepository() |
@@ -840,6 +843,169 @@ describe('FilesRepository', () => { |
840 | 843 | }) |
841 | 844 | }) |
842 | 845 |
|
| 846 | + describe('isFileDeleted', () => { |
| 847 | + const testTextFile1Name = 'test-file-1.txt' |
| 848 | + const testTextFile2Name = 'test-file-2.txt' |
| 849 | + const testCollectionAlias = 'isFileDeletedTestCollection' |
| 850 | + |
| 851 | + let deleFileTestDatasetIds: CreatedDatasetIdentifiers |
| 852 | + let fileId: number |
| 853 | + let singlepartFile: File |
| 854 | + |
| 855 | + const createTestFileUploadDestination = async (file: File, datasetId: number) => { |
| 856 | + const destination = await sut.getFileUploadDestination(datasetId, file) |
| 857 | + destination.urls = destination.urls.map((url) => url.replace('localstack', 'localhost')) |
| 858 | + return destination |
| 859 | + } |
| 860 | + |
| 861 | + const calculateBlobChecksum = (blob: Buffer): string => { |
| 862 | + return crypto.createHash('md5').update(blob).digest('hex') |
| 863 | + } |
| 864 | + |
| 865 | + beforeAll(async () => { |
| 866 | + await createCollectionViaApi(testCollectionAlias) |
| 867 | + await setStorageDriverViaApi(testCollectionAlias, 'LocalStack') |
| 868 | + await publishCollectionViaApi(testCollectionAlias) |
| 869 | + |
| 870 | + deleFileTestDatasetIds = await createDataset.execute( |
| 871 | + TestConstants.TEST_NEW_DATASET_DTO, |
| 872 | + testCollectionAlias |
| 873 | + ) |
| 874 | + |
| 875 | + singlepartFile = await createSinglepartFileBlob() |
| 876 | + |
| 877 | + await uploadFileViaApi(deleFileTestDatasetIds.numericId, testTextFile1Name) |
| 878 | + |
| 879 | + const datasetFiles = await sut.getDatasetFiles( |
| 880 | + deleFileTestDatasetIds.numericId, |
| 881 | + latestDatasetVersionId, |
| 882 | + false, |
| 883 | + FileOrderCriteria.NAME_AZ |
| 884 | + ) |
| 885 | + fileId = datasetFiles.files[0].id |
| 886 | + }) |
| 887 | + |
| 888 | + describe('Basic deletion scenarios', () => { |
| 889 | + test('should return False if a file has not been deleted', async () => { |
| 890 | + const hasBeenDeleted = await sut.isFileDeleted(fileId) |
| 891 | + expect(hasBeenDeleted).toBe(false) |
| 892 | + }) |
| 893 | + |
| 894 | + test('should return error if the dataset is unpublished and the file has been deleted', async () => { |
| 895 | + await sut.deleteFile(fileId) |
| 896 | + |
| 897 | + const expectedError = new ReadError(`[404] File with ID ${nonExistentFiledId} not found.`) |
| 898 | + await expect(sut.isFileDeleted(nonExistentFiledId)).rejects.toThrow(expectedError) |
| 899 | + }) |
| 900 | + |
| 901 | + test('should return correctly when the file has or has not been deleted, in a published dataset', async () => { |
| 902 | + await uploadFileViaApi(deleFileTestDatasetIds.numericId, testTextFile1Name) |
| 903 | + await publishDatasetViaApi(deleFileTestDatasetIds.numericId) |
| 904 | + await waitForNoLocks(deleFileTestDatasetIds.numericId, 10) |
| 905 | + |
| 906 | + const datasetFiles = await sut.getDatasetFiles( |
| 907 | + deleFileTestDatasetIds.numericId, |
| 908 | + latestDatasetVersionId, |
| 909 | + false, |
| 910 | + FileOrderCriteria.NAME_AZ |
| 911 | + ) |
| 912 | + fileId = datasetFiles.files[0].id |
| 913 | + |
| 914 | + const fileHasNotBeenDeleted = await sut.isFileDeleted(fileId) |
| 915 | + expect(fileHasNotBeenDeleted).toBe(false) |
| 916 | + |
| 917 | + await sut.deleteFile(fileId) |
| 918 | + |
| 919 | + const fileHasBeenDeleted = await sut.isFileDeleted(fileId) |
| 920 | + expect(fileHasBeenDeleted).toBe(true) |
| 921 | + }) |
| 922 | + |
| 923 | + test('should return error when file does not exist', async () => { |
| 924 | + const expectedError = new ReadError(`[404] File with ID ${nonExistentFiledId} not found.`) |
| 925 | + await expect(sut.isFileDeleted(nonExistentFiledId)).rejects.toThrow(expectedError) |
| 926 | + }) |
| 927 | + }) |
| 928 | + |
| 929 | + describe('File replacement scenario', () => { |
| 930 | + test('should return True when file has been replaced', async () => { |
| 931 | + const directUploadSut = new DirectUploadClient(sut) |
| 932 | + const progressMock = jest.fn() |
| 933 | + const abortController = new AbortController() |
| 934 | + |
| 935 | + // Upload original file |
| 936 | + const originalBuffer = Buffer.from(await singlepartFile.arrayBuffer()) |
| 937 | + const originalDestination = await createTestFileUploadDestination( |
| 938 | + singlepartFile, |
| 939 | + deleFileTestDatasetIds.numericId |
| 940 | + ) |
| 941 | + |
| 942 | + const originalStorageId = await directUploadSut.uploadFile( |
| 943 | + deleFileTestDatasetIds.numericId, |
| 944 | + singlepartFile, |
| 945 | + progressMock, |
| 946 | + abortController, |
| 947 | + originalDestination |
| 948 | + ) |
| 949 | + |
| 950 | + const originalUploadedFileDTO = { |
| 951 | + fileName: singlepartFile.name, |
| 952 | + storageId: originalStorageId, |
| 953 | + checksumType: 'md5', |
| 954 | + checksumValue: calculateBlobChecksum(originalBuffer), |
| 955 | + mimeType: singlepartFile.type |
| 956 | + } |
| 957 | + |
| 958 | + await sut.addUploadedFilesToDataset(deleFileTestDatasetIds.numericId, [ |
| 959 | + originalUploadedFileDTO |
| 960 | + ]) |
| 961 | + |
| 962 | + const originalFileId = ( |
| 963 | + await sut.getDatasetFiles( |
| 964 | + deleFileTestDatasetIds.numericId, |
| 965 | + DatasetNotNumberedVersion.LATEST, |
| 966 | + true, |
| 967 | + FileOrderCriteria.NAME_AZ |
| 968 | + ) |
| 969 | + ).files[0].id |
| 970 | + |
| 971 | + // Create and upload replacement file |
| 972 | + const newFileBlob = await createSinglepartFileBlob(testTextFile2Name, 2000) |
| 973 | + const newBuffer = Buffer.from(await newFileBlob.arrayBuffer()) |
| 974 | + const newDestination = await createTestFileUploadDestination( |
| 975 | + newFileBlob, |
| 976 | + deleFileTestDatasetIds.numericId |
| 977 | + ) |
| 978 | + |
| 979 | + const newStorageId = await directUploadSut.uploadFile( |
| 980 | + deleFileTestDatasetIds.numericId, |
| 981 | + newFileBlob, |
| 982 | + progressMock, |
| 983 | + abortController, |
| 984 | + newDestination |
| 985 | + ) |
| 986 | + |
| 987 | + const newUploadedFileDTO = { |
| 988 | + fileName: newFileBlob.name, |
| 989 | + storageId: newStorageId, |
| 990 | + checksumType: 'md5', |
| 991 | + checksumValue: calculateBlobChecksum(newBuffer), |
| 992 | + mimeType: newFileBlob.type |
| 993 | + } |
| 994 | + |
| 995 | + await publishDatasetViaApi(deleFileTestDatasetIds.numericId) |
| 996 | + await waitForNoLocks(deleFileTestDatasetIds.numericId, 10) |
| 997 | + |
| 998 | + await sut.replaceFile(originalFileId, newUploadedFileDTO) |
| 999 | + |
| 1000 | + const isDeleted = await sut.isFileDeleted(originalFileId) |
| 1001 | + expect(isDeleted).toBe(true) |
| 1002 | + |
| 1003 | + await deletePublishedDatasetViaApi(deleFileTestDatasetIds.persistentId) |
| 1004 | + await deleteCollectionViaApi(testCollectionAlias) |
| 1005 | + }) |
| 1006 | + }) |
| 1007 | + }) |
| 1008 | + |
843 | 1009 | describe('restrictFile', () => { |
844 | 1010 | let restrictFileDatasetIds: CreatedDatasetIdentifiers |
845 | 1011 | const testTextFile1Name = 'test-file-1.txt' |
|
0 commit comments