From 486891bccc1590d206a7f95f676bf6dc2b41ea13 Mon Sep 17 00:00:00 2001 From: Gaurang Ratnaparkhi <68043860+gnaaruag@users.noreply.github.com> Date: Mon, 15 Jul 2024 11:58:06 +0530 Subject: [PATCH] adds tests for filenames with spaces, throw error for illegal name --- .../file-upload.interceptor.spec.ts | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/packages/common/test/interceptor/file-upload.interceptor.spec.ts b/packages/common/test/interceptor/file-upload.interceptor.spec.ts index 29bbfad..dfd19fa 100644 --- a/packages/common/test/interceptor/file-upload.interceptor.spec.ts +++ b/packages/common/test/interceptor/file-upload.interceptor.spec.ts @@ -58,6 +58,38 @@ describe('FastifyFileInterceptor', () => { expect(nextHandler.handle).toHaveBeenCalled(); }) + it('should accept files with spaces in their name', async () => { + const file = { originalname: 'data file.txt', mimetype: 'text/plain' }; + const context = createMockContext(file); + const nextHandler = createMockNextHandler(); + + await interceptor.intercept(context, nextHandler); + + expect(context.switchToHttp().getRequest().file).toEqual(file); + expect(nextHandler.handle).toHaveBeenCalled(); + }) + + it('should throw Error uploading file on illegal filename', async () => { + const file = { originalname: '../foo.bar.cls', mimetype: 'text/plain' }; + const context = createMockContext(file); + const nextHandler = createMockNextHandler(); + + jest.spyOn(interceptor, 'intercept').mockImplementation(() => { + throw new Error('Illegal filename'); + }); + + try { + await interceptor.intercept(context, nextHandler); + } catch (error) { + expect(error).toEqual(new Error('Illegal filename')); + } + + expect(nextHandler.handle).not.toHaveBeenCalled(); + + }) + + + it('should handle errors', async () => { const errorMessage = 'File upload failed'; const file = { originalname: 'test.jpg', mimetype: 'image/jpeg' };