-
-
Notifications
You must be signed in to change notification settings - Fork 11
feat(uploads): add createFromBuffer for programmatic GridFS storage #3290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+260
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
80117d7
feat(uploads): add createFromBuffer for programmatic GridFS storage (…
PierreBrisorgueil d4c3dc3
fix(uploads): address review feedback — input validation, stream safe…
PierreBrisorgueil cbccbb6
test(uploads): add empty buffer test and clarify runtime config pattern
PierreBrisorgueil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
modules/uploads/tests/uploads.createFromBuffer.unit.tests.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| /** | ||
| * Module dependencies. | ||
| */ | ||
| import { jest, beforeEach, afterEach } from '@jest/globals'; | ||
|
|
||
| /** | ||
| * Unit tests for uploads createFromBuffer service | ||
| */ | ||
| describe('Uploads createFromBuffer unit tests:', () => { | ||
| let UploadsService; | ||
| let mockGridfs; | ||
| let mockConfig; | ||
|
|
||
| const fakeFile = { | ||
| _id: '507f1f77bcf86cd799439011', | ||
| filename: 'abc123.jpeg', | ||
| contentType: 'image/jpeg', | ||
| metadata: { kind: 'snapshot', contentType: 'image/jpeg' }, | ||
| length: 1024, | ||
| }; | ||
|
|
||
| beforeEach(async () => { | ||
| jest.resetModules(); | ||
|
|
||
| mockGridfs = { | ||
| createFromBuffer: jest.fn().mockResolvedValue(fakeFile), | ||
| getStorage: jest.fn(), | ||
| }; | ||
|
|
||
| mockConfig = { | ||
| uploads: { | ||
| snapshot: { | ||
| kind: 'snapshot', | ||
| formats: ['image/jpeg', 'image/png'], | ||
| limits: { fileSize: 5 * 1024 * 1024 }, | ||
| }, | ||
| avatar: { | ||
| kind: 'avatar', | ||
| formats: ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'], | ||
| limits: { fileSize: 1 * 1024 * 1024 }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| jest.unstable_mockModule('../../../lib/services/gridfs.js', () => ({ | ||
| default: mockGridfs, | ||
| })); | ||
|
|
||
| jest.unstable_mockModule('../../../config/index.js', () => ({ | ||
| default: mockConfig, | ||
| })); | ||
|
|
||
| jest.unstable_mockModule('../../../lib/services/multer.js', () => ({ | ||
| default: { generateFileName: jest.fn() }, | ||
| })); | ||
|
|
||
| jest.unstable_mockModule('../repositories/uploads.repository.js', () => ({ | ||
| default: { | ||
| get: jest.fn(), | ||
| getStream: jest.fn(), | ||
| update: jest.fn(), | ||
| remove: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| UploadsService = (await import('../services/uploads.service.js')).default; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test('should store a valid JPEG buffer and return upload document', async () => { | ||
| const buffer = Buffer.alloc(1024); | ||
| const result = await UploadsService.createFromBuffer(buffer, 'image/jpeg', 'snapshot', { user: '507f1f77bcf86cd799439011' }); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result.contentType).toBe('image/jpeg'); | ||
| expect(result.metadata.kind).toBe('snapshot'); | ||
| expect(mockGridfs.createFromBuffer).toHaveBeenCalledTimes(1); | ||
|
|
||
| const [buf, filename, contentType, metadata] = mockGridfs.createFromBuffer.mock.calls[0]; | ||
| expect(buf).toBe(buffer); | ||
| expect(filename).toMatch(/^[a-f0-9]{64}\.jpeg$/); | ||
| expect(contentType).toBe('image/jpeg'); | ||
| expect(metadata.kind).toBe('snapshot'); | ||
| expect(metadata.user).toBe('507f1f77bcf86cd799439011'); | ||
| }); | ||
|
|
||
| test('should store a valid PNG buffer and return upload document', async () => { | ||
| const buffer = Buffer.alloc(512); | ||
| mockGridfs.createFromBuffer.mockResolvedValue({ ...fakeFile, contentType: 'image/png' }); | ||
|
|
||
| const result = await UploadsService.createFromBuffer(buffer, 'image/png', 'snapshot'); | ||
| expect(result).toBeDefined(); | ||
| expect(result.contentType).toBe('image/png'); | ||
|
|
||
| const [, filename] = mockGridfs.createFromBuffer.mock.calls[0]; | ||
| expect(filename).toMatch(/^[a-f0-9]{64}\.png$/); | ||
| }); | ||
|
|
||
| test('should throw error when buffer exceeds size limit', async () => { | ||
| const oversizedBuffer = Buffer.alloc(6 * 1024 * 1024); // 6 MB > 5 MB limit | ||
|
|
||
| await expect( | ||
| UploadsService.createFromBuffer(oversizedBuffer, 'image/jpeg', 'snapshot'), | ||
| ).rejects.toThrow(/buffer size .* exceeds limit/); | ||
PierreBrisorgueil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| expect(mockGridfs.createFromBuffer).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('should throw error when content type is not allowed for kind', async () => { | ||
| const buffer = Buffer.alloc(1024); | ||
|
|
||
| await expect( | ||
| UploadsService.createFromBuffer(buffer, 'application/pdf', 'snapshot'), | ||
| ).rejects.toThrow(/content type .* not allowed/); | ||
|
|
||
| expect(mockGridfs.createFromBuffer).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('should throw error when kind is unknown', async () => { | ||
| const buffer = Buffer.alloc(1024); | ||
|
|
||
| await expect( | ||
| UploadsService.createFromBuffer(buffer, 'image/jpeg', 'unknown'), | ||
| ).rejects.toThrow(/unknown kind/); | ||
|
|
||
| expect(mockGridfs.createFromBuffer).not.toHaveBeenCalled(); | ||
| }); | ||
PierreBrisorgueil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| test('should throw error when buffer is null or undefined', async () => { | ||
| await expect( | ||
| UploadsService.createFromBuffer(null, 'image/jpeg', 'snapshot'), | ||
| ).rejects.toThrow(/buffer is required/); | ||
|
|
||
| await expect( | ||
| UploadsService.createFromBuffer(undefined, 'image/jpeg', 'snapshot'), | ||
| ).rejects.toThrow(/buffer is required/); | ||
|
|
||
| expect(mockGridfs.createFromBuffer).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('should throw error when buffer is not a Buffer', async () => { | ||
| await expect( | ||
| UploadsService.createFromBuffer('not a buffer', 'image/jpeg', 'snapshot'), | ||
| ).rejects.toThrow(/buffer is required/); | ||
|
|
||
| expect(mockGridfs.createFromBuffer).not.toHaveBeenCalled(); | ||
| }); | ||
PierreBrisorgueil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| test('should accept empty buffer (0-byte file)', async () => { | ||
| const emptyBuffer = Buffer.alloc(0); | ||
| mockGridfs.createFromBuffer.mockResolvedValue({ ...fakeFile, length: 0 }); | ||
|
|
||
| const result = await UploadsService.createFromBuffer(emptyBuffer, 'image/jpeg', 'snapshot'); | ||
| expect(result).toBeDefined(); | ||
| expect(result.length).toBe(0); | ||
| expect(mockGridfs.createFromBuffer).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('should throw error when kind has no formats configured', async () => { | ||
| // Adding 'broken' kind at runtime — service reads config dynamically via module reference | ||
| mockConfig.uploads.broken = { kind: 'broken', limits: { fileSize: 1024 } }; | ||
|
|
||
| await expect( | ||
| UploadsService.createFromBuffer(Buffer.alloc(10), 'image/jpeg', 'broken'), | ||
| ).rejects.toThrow(/no formats configured/); | ||
|
|
||
| expect(mockGridfs.createFromBuffer).not.toHaveBeenCalled(); | ||
| }); | ||
PierreBrisorgueil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.