Skip to content

Commit

Permalink
test: migrate file-validation util tests to TypeScript (#1578)
Browse files Browse the repository at this point in the history
  • Loading branch information
karrui authored Apr 8, 2021
1 parent b01d60a commit 76c66d1
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 112 deletions.
112 changes: 0 additions & 112 deletions tests/unit/backend/utils/file-validation.spec.js

This file was deleted.

108 changes: 108 additions & 0 deletions tests/unit/backend/utils/file-validation.spec.ts
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'])
})
})
})

0 comments on commit 76c66d1

Please sign in to comment.