@@ -58,41 +58,48 @@ describe('ObjectStoreStorage', () => {
5858 } ) ;
5959
6060 describe ( 'save' , ( ) => {
61- it ( 'should save a file to Object Store and return the URL' , async ( ) => {
62- const mockFile = {
63- contents : Buffer . from ( 'test content' ) ,
64- type : 'image/jpeg' ,
65- path : '/local/path/to/file.jpg'
66- } ;
61+ let fs ;
62+ const mockFile = {
63+ fieldname : 'file' ,
64+ originalname : '0-umnaaktb3eos233i.png' ,
65+ encoding : '7bit' ,
66+ mimetype : 'image/png' ,
67+ destination : '/tmp' ,
68+ filename : '3bab289aad1a167c811b5767350facdf' ,
69+ path : '/tmp/3bab289aad1a167c811b5767350facdf' ,
70+ size : 7158 ,
71+ name : '0-umnaaktb3eos233i.png' ,
72+ type : 'image/png' ,
73+ ext : '.png'
74+ } ;
6775
68- const mockUniqueFileName = 'test-file-123.jpg' ;
69- const mockUrl = '/test-file-123.jpg' ;
76+ beforeEach ( ( ) => {
77+ fs = require ( 'fs' ) ;
78+ fs . promises . readFile = jest . fn ( ) ;
79+ } ) ;
7080
71- // Mock the getUniqueFileName method to return a test filename
72- objectStoreStorage . getUniqueFileName = jest . fn ( ) . mockResolvedValue ( mockUniqueFileName ) ;
81+ it ( 'should save a file to Object Store and return the URL' , async ( ) => {
82+ const mockFileContent = Buffer . from ( 'test file content' ) ;
7383
74- // Mock the S3 client send to resolve with successful response
84+ fs . promises . readFile . mockResolvedValue ( mockFileContent ) ;
85+ objectStoreStorage . getUniqueSecureFilePath = jest . fn ( ) . mockResolvedValue ( mockFile . name ) ;
7586 mockS3Client . send . mockResolvedValue ( { } ) ;
7687
7788 const result = await objectStoreStorage . save ( mockFile , '/images' ) ;
7889
79- expect ( objectStoreStorage . getUniqueFileName ) . toHaveBeenCalledWith ( mockFile , '/images' ) ;
90+ expect ( fs . promises . readFile ) . toHaveBeenCalledWith ( mockFile . path ) ;
91+ expect ( objectStoreStorage . getUniqueSecureFilePath ) . toHaveBeenCalledWith ( mockFile , '/images' ) ;
8092 expect ( mockS3Client . send ) . toHaveBeenCalledWith ( expect . any ( PutObjectCommand ) ) ;
81- expect ( result ) . toBe ( mockUrl ) ;
93+
94+ expect ( result ) . toBe ( `/${ mockFile . name } ` ) ;
8295 } ) ;
8396
8497 it ( 'should handle save errors properly' , async ( ) => {
85- const mockFile = {
86- contents : Buffer . from ( 'test content' ) ,
87- type : 'image/jpeg' ,
88- path : '/local/path/to/file.jpg'
89- } ;
90- const mockUniqueFileName = 'test-file-123.jpg' ;
91-
92- // Mock the getUniqueFileName method to return a test filename
93- objectStoreStorage . getUniqueFileName = jest . fn ( ) . mockResolvedValue ( mockUniqueFileName ) ;
98+ const mockFileContent = Buffer . from ( 'test file content' ) ;
99+ const mockUniqueFileName = 'test-file-123.png' ;
94100
95- // Mock an error from the S3 client
101+ fs . promises . readFile . mockResolvedValue ( mockFileContent ) ;
102+ objectStoreStorage . getUniqueSecureFilePath = jest . fn ( ) . mockResolvedValue ( mockUniqueFileName ) ;
96103 mockS3Client . send . mockRejectedValue ( new Error ( 'Save failed' ) ) ;
97104
98105 await expect ( objectStoreStorage . save ( mockFile , '/images' ) ) . rejects . toThrow ( 'Failed to save file to Object Store' ) ;
0 commit comments