Skip to content

Instantly share code, notes, and snippets.

@JanPaulEttles
Created April 14, 2013 10:20
Show Gist options
  • Select an option

  • Save JanPaulEttles/5382218 to your computer and use it in GitHub Desktop.

Select an option

Save JanPaulEttles/5382218 to your computer and use it in GitHub Desktop.
//test_validator.js
var validator = require('./validator');
exports['validate'] = function (test) {
//these tests need to fail with an error being thrown
test.throws(function () { validator.validate(); }, 'Expected a string', 'value passed is not a string');
test.throws(function () { validator.validate(null); }, 'Expected a string', 'value passed is not a string');
test.throws(function () { validator.validate(true); }, 'Expected a string', 'value passed is not a string');
test.throws(function () { validator.validate([]); }, 'Expected a string', 'value passed is not a string');
test.throws(function () { validator.validate({}); }, 'Expected a string', 'value passed is not a string');
test.throws(function () { validator.validate(0123); }, 'Expected a string', 'value passed is not a string');
test.throws(function () { validator.validate(0123456); }, 'Expected a string', 'value passed is not a string');
test.throws(function () { validator.validate(01234567); }, 'Expected a string', 'value passed is not a string');
test.throws(function () { validator.validate(012345678); }, 'Expected a string', 'value passed is not a string');
test.throws(function () { validator.validate(0123456789); }, 'Expected a string', 'value passed is not a string');
test.throws(function () { validator.validate(01234567890); }, 'Expected a string', 'value passed is not a string');
test.throws(function () { validator.validate('a'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('abcd'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('abcdefg'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('abcdefghi'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('A'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('ABCD'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('ABCDEFG'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('ABCDEFGHI'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('ABCDEFGHIJK'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('0'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('0123'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('0123456'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('012345678'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('01234567891'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('!$%#'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('!"£$%^&'); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('!"£$%^&*('); }, 'String too short', 'less than min length');
test.throws(function () { validator.validate('!"£$%^&*()_'); }, 'String too short', 'less than min length');
//these tests need to fail
test.ok(!validator.validate('abcdefghijkl'), 'only lower case');
test.ok(!validator.validate('ABCDEFGHIJKL'), 'only upper case');
test.ok(!validator.validate('abcdefGHIJKL'), 'only lower and upper case');
test.ok(!validator.validate('012345678910'), 'only numerics');
test.ok(!validator.validate('abcdef012345'), 'only lower case and numeric');
test.ok(!validator.validate('GHIJKL012345'), 'only upper case and numeric');
test.ok(!validator.validate('abcdeFGHIJ01234'), 'only lower, upper case and numeric');
test.ok(!validator.validate('!"£$%^&*()_+'), 'only specials');
test.ok(!validator.validate('abcdef!"£$%^'), 'only lower case and specials');
test.ok(!validator.validate('ABCDEF!"£$%^'), 'only upper case and specials');
test.ok(!validator.validate('abcdefABCDEF!"£$%^'), 'only lower, upper case and specials');
test.ok(!validator.validate('012345!"£$%^'), 'only numberics and specials');
test.ok(!validator.validate('012345ABCDEF!"£$%^'), 'only upper case, numerics and specials');
test.ok(!validator.validate('abcdef012345!"£$%^'), 'only lower case, numerics and specials');
//these tests need to pass
test.ok(validator.validate('Ab0!cdefghij'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('aBcd0!fghijk'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('Abcd0!efghijkl'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('01234!fghIJKL0123'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('!fghIJKL0123'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('fghIJKL0123!'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('KL01234!fghij'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('!"£$%fghijABCDE01234'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0!bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0"bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0£bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0$bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0%bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0^bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0&bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0*bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0(bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0)bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0_bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0+bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0-bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0=bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0{bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0}bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0[bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0]bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0;bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0\'bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0#bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0:bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0@bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0~bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0,bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0.bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0/bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0<bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0>bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0\\bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0?bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
test.ok(validator.validate('A0|bcdeFGHIJ'), 'string, good length, upper, lower, numeric, special');
for(var i = 0; i < 500000; i++) {
test.ok(validator.validate(generatePassword()), 'string, good length, upper, lower, numeric, special');
}
test.done();
};
//could have used something like this, to produce an alphnumeric: Math.random().toString(35).slice(-12);
var namespace = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"£$%^&\*()_\+\-={}\[\];\'#:@~,\.\/<>\\?\|';
function generatePassword() {
var password = namespace.charAt(Math.floor(Math.random() * 10));
password += namespace.charAt(10 + Math.floor(Math.random() * 26));
password += namespace.charAt(36 + Math.floor(Math.random() * 26));
password += namespace.charAt(62 + Math.floor(Math.random() * 32));
do {
password += namespace.charAt(Math.floor(Math.random() * namespace.length));
} while (password.length < 12);
return password;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment