Last active
August 3, 2017 18:07
-
-
Save jhyland87/4d548b8316395758aa34ca260f3304ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
var _ = require('lodash') | |
var expect = require('chai').expect | |
var Account = require('../models/Account') | |
var testData = { | |
// Default data for test account | |
testAccount: { | |
username: 'john.doe', | |
password: 'FooBar@123', | |
email: '[email protected]' | |
}, | |
// Values that can work for any schema property | |
commonSchema: { | |
invalid: [{ | |
value: '', | |
reason: 'an empty string was provided' | |
},{ | |
value: null, | |
reason: 'a null value was provided' | |
},{ | |
value: undefined, | |
reason: 'an undefined value was provided' | |
}] | |
}, | |
// Valid/invalid values for schema items, as well as the reason | |
schema: { | |
// Values for schema item "username" | |
username: { | |
invalid: [{ | |
value: '', | |
reason: 'an empty string was provided' | |
},{ | |
value: null, | |
reason: 'a null value was provided' | |
},{ | |
value: undefined, | |
reason: 'an undefined value was provided' | |
},{ | |
value: { foo: true }, | |
reason: 'an object was provided (instead of a string)'//'string required, object provided' | |
},{ | |
value: [1,2,3], | |
reason: 'an array was provided (instead of a string)'//'string required, array provided' | |
},{ | |
value: 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz', | |
reason: 'the value provided was too long' | |
},{ | |
value: '12tester', | |
reason: 'value provided starts with a numerical character' | |
},{ | |
value: 'tester123', | |
reason: 'value provided ends with a numerical character' | |
},{ | |
value: '-test', | |
reason: 'value provided starts with a special character' | |
},{ | |
value: 'test-', | |
reason: 'value provided ends with a special character' | |
},{ | |
value: 'john*doe', | |
reason: 'value provided contains an illegal special character (*)' | |
}] | |
}, | |
// Values for schema item "password" | |
password: { | |
invalid: [{ | |
value: '', | |
reason: 'an empty string was provided' | |
},{ | |
value: null, | |
reason: 'a null value was provided' | |
},{ | |
value: undefined, | |
reason: 'an undefined value was provided' | |
}] | |
}, | |
// Values for schema item "email" | |
email: { | |
invalid: [{ | |
value: '', | |
reason: 'an empty string was provided' | |
},{ | |
value: null, | |
reason: 'a null value was provided' | |
},{ | |
value: undefined, | |
reason: 'an undefined value was provided' | |
},{ | |
value: '', | |
reason: 'an empty string was provided' | |
},{ | |
value: null, | |
reason: 'a null value was provided' | |
},{ | |
value: undefined, | |
reason: 'an undefined value was provided' | |
},{ | |
value: 'john<dot>doe<at>site<dot>com', | |
reason: 'value provided contains illegal special chars' | |
},{ | |
value: 'john [email protected]', | |
reason: 'value provided contains spaces' | |
},{ | |
value: 'john@[email protected]', | |
reason: 'value provided contains two @ symbols' | |
}] | |
}, | |
// Values for schema item "status" | |
status: { | |
invalid: [{ | |
value: '', | |
reason: 'an empty string was provided' | |
},{ | |
value: null, | |
reason: 'a null value was provided' | |
},{ | |
value: 'blah', | |
reason: 'the string value provided was an invalid state (blah)' | |
},{ | |
value: '', | |
reason: '' | |
}], | |
valid: [{ | |
value: 'disabled', | |
reason: 'given the valid state "disabled"' | |
},{ | |
value: 'validated', | |
reason: 'given the valid state "validated"' | |
},{ | |
value: 'enabled', | |
reason: 'given the valid state "enabled"' | |
},{ | |
value: undefined, | |
reason: 'an undefined value was provided' | |
}] | |
} | |
} | |
} | |
describe('Account model', function() { | |
describe('schema item', function() { | |
_.each( testData.schema, ( testValues, schemaItem ) => { | |
describe( `"${schemaItem}"`, function() { | |
if ( _.has( testValues, 'invalid' ) && ! _.isEmpty( testValues.invalid ) ){ | |
describe('should be invalid if', function() { | |
_.each( testValues.invalid, ( testVal, testValIdx ) => { | |
it( `${testVal.reason}`, function(done) { | |
var account = new Account() | |
var error | |
_.assignIn( account, testData.testAccount ) | |
account[ schemaItem ] = testVal.value | |
account.validate(function(err) { | |
expect( err.errors[ schemaItem ] ).to.exist | |
done() | |
}) | |
}) | |
}) | |
}) | |
} | |
if ( _.has( testValues, 'valid' ) && ! _.isEmpty( testValues.valid ) ){ | |
describe('should be valid if', function() { | |
_.each( testValues.valid, ( testVal, testValIdx ) => { | |
it( `${testVal.reason}`, function(done) { | |
var account = new Account() | |
var error | |
_.assignIn( account, testData.testAccount ) | |
account[ schemaItem ] = testVal.value | |
account.validate(function(err) { | |
expect( err ).to.be.null | |
done() | |
}) | |
}) | |
}) | |
}) | |
} | |
}) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment