File size: 1,331 Bytes
e8c33fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const test = require('node:test');
const assert = require('node:assert/strict');
const path = require('path');

test('getFileKindChecker rejects svg mime type', () => {
  const IMAGE_EXTENSIONS = new Set(['jpg', 'jpeg', 'png', 'webp']);
  const IMAGE_MIME_TYPES = new Set(['image/jpeg', 'image/jpg', 'image/png', 'image/x-png', 'image/webp']);

  const isImageAllowed = (file) => {
    const ext = path.extname(file.originalname || '').toLowerCase().replace('.', '');
    const mime = String(file.mimetype || '').toLowerCase();
    return IMAGE_EXTENSIONS.has(ext) && IMAGE_MIME_TYPES.has(mime);
  };

  assert.equal(isImageAllowed({ originalname: 'evil.svg', mimetype: 'image/svg+xml' }), false, 'SVG must be rejected');
  assert.equal(isImageAllowed({ originalname: 'photo.jpg', mimetype: 'image/jpeg' }), true, 'JPEG must be allowed');
  assert.equal(isImageAllowed({ originalname: 'photo.png', mimetype: 'image/png' }), true, 'PNG must be allowed');
});

test('createUpload is a function that returns multer middleware', () => {
  const createUpload = require('../middleware/uploadMiddleware');
  assert.equal(typeof createUpload, 'function', 'createUpload must be a function');
  const upload = createUpload({ kind: 'image' });
  assert.equal(typeof upload.single, 'function', 'returned object must have .single method');
});