Skip to content

Commit 65fb0ea

Browse files
Merge pull request #14980 from Borewit/fix-FileTypeValidator-unit-test
test(common): Fix invalid file type test
2 parents be5a076 + de798f6 commit 65fb0ea

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

packages/common/test/pipes/file/file-type.validator.spec.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,49 @@ import { expect } from 'chai';
22
import { IFile } from '../../../../common/pipes/file/interfaces';
33
import { FileTypeValidator } from '../../../pipes';
44

5+
const pngBuffer = Buffer.from([
6+
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49,
7+
0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06,
8+
0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00, 0x0a, 0x49, 0x44,
9+
0x41, 0x54, 0x78, 0x9c, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0d,
10+
0x0a, 0x2d, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42,
11+
0x60, 0x82,
12+
]);
13+
14+
const jpegBuffer = Buffer.from([
15+
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46,
16+
]);
17+
518
describe('FileTypeValidator', () => {
619
describe('isValid', () => {
7-
it('should return true when the file buffer matches the specified type', async () => {
8-
const fileTypeValidator = new FileTypeValidator({
9-
fileType: 'image/jpeg',
20+
describe('support file types', () => {
21+
async function testFileByMimeType(mimeType, fileData) {
22+
const fileTypeValidator = new FileTypeValidator({
23+
fileType: mimeType,
24+
});
25+
26+
const requestFile = {
27+
mimetype: mimeType,
28+
buffer: fileData,
29+
} as IFile;
30+
31+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(true);
32+
}
33+
34+
it('should be able to validate a JPEG file', () => {
35+
return testFileByMimeType('image/jpeg', jpegBuffer);
1036
});
1137

12-
const jpegBuffer = Buffer.from([
13-
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46,
14-
]);
15-
const requestFile = {
16-
mimetype: 'image/jpeg',
17-
buffer: jpegBuffer,
18-
} as IFile;
19-
20-
expect(await fileTypeValidator.isValid(requestFile)).to.equal(true);
38+
it('should be able to validate a PNG file', () => {
39+
return testFileByMimeType('image/png', pngBuffer);
40+
});
2141
});
2242

2343
it('should return true when the file buffer matches the specified file extension', async () => {
2444
const fileTypeValidator = new FileTypeValidator({
2545
fileType: 'jpeg',
2646
});
2747

28-
const jpegBuffer = Buffer.from([
29-
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46,
30-
]);
3148
const requestFile = {
3249
mimetype: 'image/jpeg',
3350
buffer: jpegBuffer,

0 commit comments

Comments
 (0)