From 88dbeda13f7bdaff3fff581da280592f1b299b07 Mon Sep 17 00:00:00 2001 From: Kar Rui Lau Date: Wed, 7 Apr 2021 18:05:39 +0800 Subject: [PATCH] test: migrate file-validation util tests to TypeScript --- .../backend/utils/file-validation.spec.js | 112 ------------------ .../backend/utils/file-validation.spec.ts | 108 +++++++++++++++++ 2 files changed, 108 insertions(+), 112 deletions(-) delete mode 100644 tests/unit/backend/utils/file-validation.spec.js create mode 100644 tests/unit/backend/utils/file-validation.spec.ts diff --git a/tests/unit/backend/utils/file-validation.spec.js b/tests/unit/backend/utils/file-validation.spec.js deleted file mode 100644 index 7441dd5587..0000000000 --- a/tests/unit/backend/utils/file-validation.spec.js +++ /dev/null @@ -1,112 +0,0 @@ -const { - getFileExtension, - isInvalidFileExtension, - getInvalidFileExtensionsInZip, -} = require('../../../../dist/backend/shared/util/file-validation') - -describe('getFileExtension', () => { - const tests = [ - { - name: 'handles file name with extension', - input: 'image.jpg', - expected: '.jpg', - }, - { - name: 'handles file name with no extension', - input: 'image', - expected: '', - }, - { - name: 'handles file name with multiple dots', - input: 'file.a.txt', - expected: '.txt', - }, - { - name: 'handles file name with multiple consecutive dots', - input: 'file...a.zip', - expected: '.zip', - }, - ] - tests.forEach((t) => { - it(t.name, () => { - const actual = getFileExtension(t.input) - expect(actual).toEqual(t.expected) - }) - }) -}) - -describe('isInvalidFileExtension', () => { - const tests = [ - { - name: 'should return false when given valid extension', - input: '.jpg', - expected: false, - }, - { - name: 'should return true when given invalid extension', - input: '.invalid', - expected: true, - }, - { - name: 'should return false when given valid extension that is mixed case', - input: '.jPG', - expected: false, - }, - { - name: - 'should return true when given invalid extension that is mixed case', - input: '.InvalId', - expected: true, - }, - ] - tests.forEach((t) => { - it(t.name, () => { - const actual = isInvalidFileExtension(t.input) - expect(actual).toEqual(t.expected) - }) - }) -}) - -describe('getInvalidFileExtensionsInZip on server', () => { - const fs = require('fs') - const tests = [ - { - name: 'should return [] when there is only valid files', - file: './tests/unit/backend/resources/onlyvalid.zip', - expected: [], - }, - { - name: 'should return invalid extensions when there is only invalid ext', - file: './tests/unit/backend/resources/onlyinvalid.zip', - expected: ['.a', '.abc', '.py'], - }, - { - name: 'should return only invalid extensions', - file: './tests/unit/backend/resources/invalidandvalid.zip', - expected: ['.a', '.oo'], - }, - { - name: 'should exclude repeated invalid extensions', - file: './tests/unit/backend/resources/repeated.zip', - expected: ['.a'], - }, - { - name: 'should exclude folders', - file: './tests/unit/backend/resources/folder.zip', - expected: [], - }, - { - name: 'should include invalid extensions in nested zip files', - file: './tests/unit/backend/resources/nestedInvalid.zip', - expected: ['.a', '.oo'], - }, - ] - tests.forEach((t) => { - it(t.name, async () => { - const fn = getInvalidFileExtensionsInZip('server') - const file = fs.readFileSync(t.file, 'binary') - const actual = await fn(file) - expect(actual).toEqual(t.expected) - }) - }) -}) diff --git a/tests/unit/backend/utils/file-validation.spec.ts b/tests/unit/backend/utils/file-validation.spec.ts new file mode 100644 index 0000000000..20b51dfed8 --- /dev/null +++ b/tests/unit/backend/utils/file-validation.spec.ts @@ -0,0 +1,108 @@ +import fs from 'fs' + +import { FilePlatforms } from 'src/shared/constants' +import { + getFileExtension, + getInvalidFileExtensionsInZip, + isInvalidFileExtension, +} from 'src/shared/util/file-validation' + +describe('File validation utils', () => { + describe('getFileExtension', () => { + it('should handle file name with extension', () => { + const actual = getFileExtension('image.jpg') + expect(actual).toEqual('.jpg') + }) + + it('should handle file name with no extension', async () => { + const actual = getFileExtension('image-no-extension') + expect(actual).toEqual('') + }) + + it('should handle file name with multiple periods', async () => { + const actual = getFileExtension('file.a.txt') + expect(actual).toEqual('.txt') + }) + + it('should handle file name with consecutive periodsa', async () => { + const actual = getFileExtension('file....a.zip') + expect(actual).toEqual('.zip') + }) + }) + + describe('isInvalidFileExtension', () => { + it('should return false when given valid extension', () => { + const actual = isInvalidFileExtension('.jpg') + expect(actual).toEqual(false) + }) + + it('should return true when given invalid extension', () => { + const actual = isInvalidFileExtension('.invalid') + expect(actual).toEqual(true) + }) + + it('should return false when given valid extension that is mixed case', () => { + const actual = isInvalidFileExtension('.jPG') + expect(actual).toEqual(false) + }) + + it('should return true when given invalid extension that is mixed case', () => { + const actual = isInvalidFileExtension('.sPoNgEbOb') + expect(actual).toEqual(true) + }) + }) + + describe('getInvalidFileExtensionsInZip on server', () => { + it('should return empty array when there is only valid files', async () => { + const fn = getInvalidFileExtensionsInZip(FilePlatforms.Server) + const file = fs.readFileSync( + './tests/unit/backend/resources/onlyvalid.zip', + ) + const actual = await fn(file) + expect(actual).toEqual([]) + }) + + it('should return invalid extensions when zipped files are all invalid file extensions', async () => { + const fn = getInvalidFileExtensionsInZip(FilePlatforms.Server) + const file = fs.readFileSync( + './tests/unit/backend/resources/onlyinvalid.zip', + ) + const actual = await fn(file) + expect(actual).toEqual(['.a', '.abc', '.py']) + }) + + it('should return only invalid extensions when zip has some valid file extensions', async () => { + const fn = getInvalidFileExtensionsInZip(FilePlatforms.Server) + const file = fs.readFileSync( + './tests/unit/backend/resources/invalidandvalid.zip', + ) + const actual = await fn(file) + expect(actual).toEqual(['.a', '.oo']) + }) + + it('should exclude repeated invalid extensions', async () => { + const fn = getInvalidFileExtensionsInZip(FilePlatforms.Server) + const file = fs.readFileSync( + './tests/unit/backend/resources/repeated.zip', + ) + const actual = await fn(file) + expect(actual).toEqual(['.a']) + }) + + it('should exclude folders', async () => { + const fn = getInvalidFileExtensionsInZip(FilePlatforms.Server) + const file = fs.readFileSync('./tests/unit/backend/resources/folder.zip') + const actual = await fn(file) + expect(actual).toEqual([]) + }) + + it('should include invalid extensions in nested zip files', async () => { + const fn = getInvalidFileExtensionsInZip(FilePlatforms.Server) + const file = fs.readFileSync( + './tests/unit/backend/resources/nestedInvalid.zip', + ) + const actual = await fn(file) + expect(actual).toEqual(['.a', '.oo']) + }) + }) +})