Ghost/test/unit/web/shared/middleware/validation_upload_spec.js
Hannah Wolfe 1a4506dcf0 Moved validation utils into validation/upload
- These two functions have no dependencies and are only used in valiation/upload
- Co-locating the code makes it easier to move
- Exported them with a new module.exports._test pattern - we'll see about whether this is a good idea
- This is one of many similar changes needed to make it easier to refactor to the existing setup
2020-04-22 07:21:41 +01:00

44 lines
1.8 KiB
JavaScript

const should = require('should');
const uploadValidation = require('../../../../../core/server/web/shared/middlewares/validation/upload')._test;
describe('web utils', function () {
describe('checkFileExists', function () {
it('should return true if file exists in input', function () {
uploadValidation.checkFileExists({mimetype: 'file', path: 'path'}).should.be.true();
});
it('should return false if file does not exist in input', function () {
uploadValidation.checkFileExists({}).should.be.false();
});
it('should return false if file is incorrectly structured', function () {
uploadValidation.checkFileExists({type: 'file'}).should.be.false();
});
});
describe('checkFileIsValid', function () {
it('returns true if file has valid extension and type', function () {
uploadValidation.checkFileIsValid({
name: 'test.txt',
mimetype: 'text',
ext: '.txt'
}, ['text'], ['.txt']).should.be.true();
uploadValidation.checkFileIsValid({
name: 'test.jpg',
mimetype: 'jpeg',
ext: '.jpg'
}, ['text', 'jpeg'], ['.txt', '.jpg']).should.be.true();
});
it('returns false if file has invalid extension', function () {
uploadValidation.checkFileIsValid({name: 'test.txt', mimetype: 'text'}, ['text'], ['.tar']).should.be.false();
uploadValidation.checkFileIsValid({name: 'test', mimetype: 'text'}, ['text'], ['.txt']).should.be.false();
});
it('returns false if file has invalid type', function () {
uploadValidation.checkFileIsValid({name: 'test.txt', mimetype: 'text'}, ['archive'], ['.txt']).should.be.false();
});
});
});