1+ import * as crypto from 'crypto'
12import { FilesRepository } from '../../../src/files/infra/repositories/FilesRepository'
23import {
34 ApiConfig ,
@@ -43,6 +44,7 @@ import {
4344} from '../../testHelpers/collections/collectionHelper'
4445import { RestrictFileDTO } from '../../../src/files/domain/dtos/RestrictFileDTO'
4546import { DatasetsRepository } from '../../../src/datasets/infra/repositories/DatasetsRepository'
47+ import { DirectUploadClient } from '../../../src/files/infra/clients/DirectUploadClient'
4648
4749describe ( 'FilesRepository' , ( ) => {
4850 const sut : FilesRepository = new FilesRepository ( )
@@ -840,9 +842,10 @@ describe('FilesRepository', () => {
840842 } )
841843 } )
842844
843- describe ( 'fileHasBeenDeleted ' , ( ) => {
845+ describe ( 'getFileHasBeenDeleted ' , ( ) => {
844846 let deleFileTestDatasetIds : CreatedDatasetIdentifiers
845847 const testTextFile1Name = 'test-file-1.txt'
848+ const testTextFile2Name = 'test-file-2.txt'
846849 let fileId : number
847850
848851 beforeAll ( async ( ) => {
@@ -870,15 +873,15 @@ describe('FilesRepository', () => {
870873 } )
871874
872875 test ( 'should return False if a file has not been deleted' , async ( ) => {
873- const hasBeenDeleted = await sut . fileHasBeenDeleted ( fileId )
876+ const hasBeenDeleted = await sut . getFileHasBeenDeleted ( fileId )
874877 expect ( hasBeenDeleted ) . toBe ( false )
875878 } )
876879
877880 test ( 'should return error if the dataset is unpublished and the file has been deleted' , async ( ) => {
878881 await sut . deleteFile ( fileId )
879882
880883 const expectedError = new ReadError ( `[404] File with ID ${ nonExistentFiledId } not found.` )
881- await expect ( sut . fileHasBeenDeleted ( nonExistentFiledId ) ) . rejects . toThrow ( expectedError )
884+ await expect ( sut . getFileHasBeenDeleted ( nonExistentFiledId ) ) . rejects . toThrow ( expectedError )
882885 } )
883886
884887 test ( 'should return True when the dataset is published and the file has not been deleted' , async ( ) => {
@@ -899,22 +902,93 @@ describe('FilesRepository', () => {
899902 )
900903 fileId = datasetFiles . files [ 0 ] . id
901904
902- const notDeleted = await sut . fileHasBeenDeleted ( fileId )
905+ const notDeleted = await sut . getFileHasBeenDeleted ( fileId )
903906 expect ( notDeleted ) . toBe ( false )
904907
905908 await sut . deleteFile ( fileId )
906909
907- const hasBeenDeleted = await sut . fileHasBeenDeleted ( fileId )
910+ const hasBeenDeleted = await sut . getFileHasBeenDeleted ( fileId )
908911 expect ( hasBeenDeleted ) . toBe ( true )
909912 } )
910913
911- test ( 'should return error when file does not exist' , async ( ) => {
912- const expectedError = new ReadError ( `[404] File with ID ${ nonExistentFiledId } not found.` )
914+ test ( 'should return True when file has been replaced' , async ( ) => {
915+ const directUploadSut : DirectUploadClient = new DirectUploadClient ( sut )
916+
917+ // Upload original file
918+ await uploadFileViaApi ( deleFileTestDatasetIds . numericId , testTextFile1Name ) . catch ( ( ) => {
919+ throw new Error ( `Error while uploading file ${ testTextFile1Name } ` )
920+ } )
921+
922+ const datasetFiles = await sut . getDatasetFiles (
923+ deleFileTestDatasetIds . numericId ,
924+ latestDatasetVersionId ,
925+ false ,
926+ FileOrderCriteria . NAME_AZ
927+ )
928+ const originalFileId = datasetFiles . files [ 0 ] . id
929+
930+ const createTestFileUploadDestination = async ( file : File , testDatasetId : number ) => {
931+ const filesRepository = new FilesRepository ( )
932+ const destination = await filesRepository . getFileUploadDestination ( testDatasetId , file )
933+ destination . urls . forEach ( ( destinationUrl , index ) => {
934+ destination . urls [ index ] = destinationUrl . replace ( 'localstack' , 'localhost' )
935+ } )
936+ return destination
937+ }
938+
939+ // Create a new file and replace the original file
940+ const newFileBlob = await createSinglepartFileBlob ( testTextFile2Name , 2000 )
941+ const newDestination = await createTestFileUploadDestination (
942+ newFileBlob ,
943+ deleFileTestDatasetIds . numericId
944+ )
945+
946+ const progressMock = jest . fn ( )
947+ const abortController = new AbortController ( )
948+
949+ const newStorageId = await directUploadSut . uploadFile (
950+ deleFileTestDatasetIds . numericId ,
951+ newFileBlob ,
952+ progressMock ,
953+ abortController ,
954+ newDestination
955+ )
913956
914- await expect ( sut . fileHasBeenDeleted ( nonExistentFiledId ) ) . rejects . toThrow ( expectedError )
957+ const fileArrayBuffer = await newFileBlob . arrayBuffer ( )
958+ const fileBuffer = Buffer . from ( fileArrayBuffer )
959+
960+ const calculateBlobChecksum = ( blob : Buffer ) : string => {
961+ const hash = crypto . createHash ( 'md5' )
962+ hash . update ( blob )
963+ return hash . digest ( 'hex' )
964+ }
965+
966+ const newUploadedFileDTO = {
967+ fileName : newFileBlob . name ,
968+ storageId : newStorageId ,
969+ checksumType : 'md5' ,
970+ checksumValue : calculateBlobChecksum ( fileBuffer ) ,
971+ mimeType : newFileBlob . type
972+ }
973+
974+ //Publish the dataset and check if the original file has been deleted
975+ await publishDatasetViaApi ( deleFileTestDatasetIds . numericId )
976+
977+ await waitForNoLocks ( deleFileTestDatasetIds . numericId , 10 )
978+
979+ await sut . replaceFile ( originalFileId , newUploadedFileDTO )
980+
981+ const isDeleted = await sut . getFileHasBeenDeleted ( originalFileId )
982+ expect ( isDeleted ) . toBe ( true )
915983 } )
916984 } )
917985
986+ test ( 'should return error when file does not exist' , async ( ) => {
987+ const expectedError = new ReadError ( `[404] File with ID ${ nonExistentFiledId } not found.` )
988+
989+ await expect ( sut . getFileHasBeenDeleted ( nonExistentFiledId ) ) . rejects . toThrow ( expectedError )
990+ } )
991+
918992 describe ( 'restrictFile' , ( ) => {
919993 let restrictFileDatasetIds : CreatedDatasetIdentifiers
920994 const testTextFile1Name = 'test-file-1.txt'
0 commit comments