UNPKG

hazmat

Version:

Validation and sanitization of input parameters

185 lines (153 loc) 5.95 kB
var Hazmat = process.env.HAZMAT_COV ? require("../lib-cov/hazmat.js") : require("../lib/hazmat.js"); var expect = require("expect.js"); var _ = require('underscore'); var noop = function(){}; describe('hazmat', function() { before(function() { }); after(function() { }); describe('create', function() { before(function() { }); after(function() { }); it('should be a function', function() { expect(Hazmat.create).to.be.a('function'); }); it('should throw an exception if config parameter is not hash', function() { var error = null; try { var hazmat = Hazmat.create('bad config'); } catch (err) { error = err; } expect(error).not.to.be(null); }); it('should create hazmat object', function() { var error = null; try { var hazmat = Hazmat.create(); expect(hazmat).to.be.an('object'); expect(hazmat.safeString).to.be.a('function'); expect(hazmat.safeDomId).to.be.a('function'); expect(hazmat.safeFunction).to.be.a('function'); } catch (err) { console.log(err) error = err; } expect(error).to.be(null); }); }); describe('static functions', function() { describe('isDomId', function(){ it('should correctly validate proper dom ids', function() { expect(Hazmat.isDomId('foo')).to.be.ok(); expect(Hazmat.isDomId('foo')).to.be.ok(); expect(Hazmat.isDomId('_Foo_111')).to.be.ok(); }); it('should correctly detect bad dom ids', function() { expect(Hazmat.isDomId('foo ')).not.to.be.ok(); expect(Hazmat.isDomId('^*&^')).not.to.be.ok(); expect(Hazmat.isDomId('')).not.to.be.ok(); }); }); describe('fixDomId', function() { it('should fix dom ids', function() { expect(Hazmat.fixDomId('foo ')).to.be('foo'); expect(Hazmat.fixDomId('foo _() 123')).to.be('foo_123'); }); it('should fail to fix bad data', function() { expect(Hazmat.fixDomId('')).not.to.be.ok(); expect(Hazmat.fixDomId(' ')).not.to.be.ok(); expect(Hazmat.fixDomId(18)).not.to.be.ok(); expect(Hazmat.fixDomId([1313123])).not.to.be.ok(); }); }); }); describe('instance functions', function() { var fail, warn, hazmat; before(function(){ fail = false; warn = false; hazmat = Hazmat.create({fail:function(){fail = true}, warn:function(){warn = true}}); }); describe('safeString', function(){ it('should safely return valid string', function() { expect(hazmat.safeString('param','foo')).to.be('foo'); expect(hazmat.safeString('param','foo bar')).to.be('foo bar'); }); it('should safely fallback to default value if available and issue a warning', function() { warn = false; expect(hazmat.safeString('param',5,'default')).to.be('default'); expect(warn).to.be(true); warn = false; expect(hazmat.safeString('param',5, function(v) {return v.toString()})).to.be('5'); expect(warn).to.be(true); }); it('should fail if no default is available or if default is bad', function() { fail = false; expect(hazmat.safeString('param',5,5)).not.to.be.ok(); expect(fail).to.be(true); fail = false; expect(hazmat.safeString('param',5,_.identity)).not.to.be.ok(); expect(fail).to.be(true); }); it('should eat exceptions in fallback function', function() { fail = false; expect(hazmat.safeString('param',5,function() {throw new Error('catch me')})).not.to.be.ok(); expect(fail).to.be(true); }); }); describe('safeDomId', function(){ it('should safely return valid DOM ID', function() { expect(hazmat.safeDomId('param','foo')).to.be('foo'); expect(hazmat.safeDomId('param','foo_bar')).to.be('foo_bar'); }); it('should safely fallback to default value if available and issue a warning', function() { warn = false; expect(hazmat.safeDomId('param','xx xx','foo')).to.be('foo'); expect(warn).to.be(true); warn = false; expect(hazmat.safeDomId('param',5, function(v) {return 'id'+v.toString()})).to.be('id5'); expect(warn).to.be(true); }); it('should fail if no default is available or if default is bad', function() { fail = false; expect(hazmat.safeDomId('param','xx xx',5)).not.to.be.ok(); expect(fail).to.be(true); fail = false; expect(hazmat.safeDomId('param','xx xx',_.identity)).not.to.be.ok(); expect(fail).to.be(true); }); it('should eat exceptions in fallback function', function() { fail = false; expect(hazmat.safeDomId('param','xx xx',function() {throw new Error('catch me')})).not.to.be.ok(); expect(fail).to.be(true); }); }); describe('safeFunction', function(){ it('should safely return valid function', function() { expect(hazmat.safeFunction('param', expect)).to.be(expect); expect(hazmat.safeFunction('param',_.identity)).to.be(_.identity); }); it('should safely fallback to default value if available and issue a warning', function() { warn = false; expect(hazmat.safeFunction('param',5,_.identity)).to.be(_.identity); expect(warn).to.be(true); }); it('should fail if no default is available or if default is bad', function() { fail = false; expect(hazmat.safeFunction('param',5,5)).not.to.be.ok(); expect(fail).to.be(true); }); it('should not eval fallback function', function() { fail = false; expect(hazmat.safeFunction('param',5,function() {throw new Error('catch me')})).to.be.ok(); expect(fail).to.be(false); }); }); }); });