Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate file-validation util tests to TypeScript #1578

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'])
})
})
})