-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: migrate file-validation util tests to TypeScript (#1578)
- Loading branch information
Showing
2 changed files
with
108 additions
and
112 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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,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']) | ||
}) | ||
}) | ||
}) |