UNPKG

joi-image-extension

Version:
271 lines (199 loc) 8.75 kB
'use strict'; function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } const fs = require('fs'); const path = require('path'); const util = require('util'); const { assert } = require('chai'); const BaseJoi = require('joi'); const ImageExtension = require('./index'); const Joi = BaseJoi.extend(ImageExtension); const readFileAsync = util.promisify(fs.readFile); describe('Joi Image Extension', function () { const getTestImages = (() => { var _ref = _asyncToGenerator(function* () { const images = ['../test/100x100.gif', '../test/100x100.jpg', '../test/100x100.png']; return Promise.all(images.map(function (image) { return readFileAsync(path.join(__dirname, image)); })); }); return function getTestImages() { return _ref.apply(this, arguments); }; })(); it('should fail for images below width minimum', _asyncToGenerator(function* () { const schema = Joi.image().minWidth(150); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNotNull(result.error); assert.equal(result.error.details[0].type, 'image.minWidth'); }); })); it('should pass for images equal to width minimum', _asyncToGenerator(function* () { const schema = Joi.image().minWidth(100); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNull(result.error); }); })); it('should pass for images above width minimum', _asyncToGenerator(function* () { const schema = Joi.image().minWidth(99); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNull(result.error); }); })); it('should pass for images below width maximum', _asyncToGenerator(function* () { const schema = Joi.image().maxWidth(150); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNull(result.error); }); })); it('should pass for images equal to width maximum', _asyncToGenerator(function* () { const schema = Joi.image().maxWidth(100); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNull(result.error); }); })); it('should fail for images above width maximum', _asyncToGenerator(function* () { const schema = Joi.image().maxWidth(99); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNotNull(result.error); assert.equal(result.error.details[0].type, 'image.maxWidth'); }); })); it('should fail for images below height minimum', _asyncToGenerator(function* () { const schema = Joi.image().minHeight(150); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNotNull(result.error); assert.equal(result.error.details[0].type, 'image.minHeight'); }); })); it('should pass for images equal to height minimum', _asyncToGenerator(function* () { const schema = Joi.image().minHeight(100); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNull(result.error); }); })); it('should pass for images above height minimum', _asyncToGenerator(function* () { const schema = Joi.image().minHeight(99); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNull(result.error); }); })); it('should pass for images below height maximum', _asyncToGenerator(function* () { const schema = Joi.image().maxHeight(150); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNull(result.error); }); })); it('should pass for images equal to height maximum', _asyncToGenerator(function* () { const schema = Joi.image().maxHeight(100); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNull(result.error); }); })); it('should fail for images above height maximum', _asyncToGenerator(function* () { const schema = Joi.image().maxHeight(99); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNotNull(result.error); assert.equal(result.error.details[0].type, 'image.maxHeight'); }); })); it('should fail for images below minimum dimensions', _asyncToGenerator(function* () { const schema = Joi.image().minDimensions(150, 150); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNotNull(result.error); assert.equal(result.error.details[0].type, 'image.minDimensions'); }); })); it('should pass for images equal to minimum dimensions', _asyncToGenerator(function* () { const schema = Joi.image().minDimensions(100, 100); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNull(result.error); }); })); it('should pass for images above minimum dimensions', _asyncToGenerator(function* () { const schema = Joi.image().minDimensions(99, 99); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNull(result.error); }); })); it('should pass for images below maximum dimensions', _asyncToGenerator(function* () { const schema = Joi.image().maxDimensions(150, 150); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNull(result.error); }); })); it('should pass for images equal to maximum dimensions', _asyncToGenerator(function* () { const schema = Joi.image().maxDimensions(100, 100); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNull(result.error); }); })); it('should fail for images above maximum dimensions', _asyncToGenerator(function* () { const schema = Joi.image().maxDimensions(99, 99); const images = yield getTestImages(); images.forEach(function (image) { const result = schema.validate(image); assert.isNotNull(result.error); assert.equal(result.error.details[0].type, 'image.maxDimensions'); }); })); it('should pass for images in the specified allowed types', _asyncToGenerator(function* () { const schema = Joi.image().allowTypes('jpg'); const image = yield readFileAsync(path.join(__dirname, '../test/100x100.jpg')); const result = schema.validate(image); assert.isNull(result.error); })); it('should fail for images not in the specified allowed types', _asyncToGenerator(function* () { const schema = Joi.image().allowTypes(['jpg', 'png']); const image = yield readFileAsync(path.join(__dirname, '../test/100x100.gif')); const result = schema.validate(image); assert.isNotNull(result.error); assert.equal(result.error.details[0].type, 'image.allowTypes'); })); it('should fail for images in the specified disallowed types', _asyncToGenerator(function* () { const schema = Joi.image().disallowTypes('jpg'); const image = yield readFileAsync(path.join(__dirname, '../test/100x100.jpg')); const result = schema.validate(image); assert.isNotNull(result.error); assert.equal(result.error.details[0].type, 'image.disallowTypes'); })); it('should pass for images not in the specified disallowed types', _asyncToGenerator(function* () { const schema = Joi.image().disallowTypes(['png']); const image = yield readFileAsync(path.join(__dirname, '../test/100x100.jpg')); const result = schema.validate(image); assert.isNull(result.error); })); });