Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Created August 3, 2017 17:15
Show Gist options
  • Save jhyland87/f438415d4d19372a4a4d049758edfaef to your computer and use it in GitHub Desktop.
Save jhyland87/f438415d4d19372a4a4d049758edfaef to your computer and use it in GitHub Desktop.
var _ = require('lodash')
var expect = require('chai').expect
var Account = require('../models/Account')
describe('Account model', function() {
describe('schema item', function() {
// Account.username
describe('"username"', function() {
// Account.username INVALID tests
describe('should be invalid if', function() {
it('value is undefined', function(done) {
var account = new Account()
var error
account.password = 'foobar'
account.email = '[email protected]'
account.validate(function(err) {
expect( err.errors.username ).to.exist
done()
})
})
it('value is null', function(done) {
var account = new Account()
var error
account.username = null
account.password = 'foobar'
account.email = '[email protected]'
account.validate(function(err) {
expect( err.errors.username ).to.exist
done()
})
})
it('value is not a string', function(done) {
var account = new Account()
var error
account.username = { foo: true }
account.password = 'asdfadsfadsf'
account.email = '[email protected]'
account.validate(function( err ) {
//console.log('>>>>>>>>>>>>>>>>>>')
//console.log('Account model schema item "username" should be invalid if value is not a string')
//console.log('err:',err)
//console.log('>>>>>>>>>>>>>>>>>>')
expect( err.errors.username ).to.exist
done()
})
})
it('value is less than 3 characters', function(done) {
var account = new Account()
var error
account.username = 'hi'
account.password = 'password123'
account.email = '[email protected]'
account.validate(function( err ) {
expect( err.errors.username ).to.exist
done()
})
})
it('value is longer than 35 characters', function(done) {
var account = new Account()
var error
account.username = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
account.password = 'password123'
account.email = '[email protected]'
account.validate(function( err ) {
expect( err.errors.username ).to.exist
done()
})
})
})
// Account.username VALID tests
describe('should be valid if', function() {
it('value is a unique string between 3 and 35 characters', function(done) {
var account = new Account()
var error
account.username = 'zinggg'
account.password = 'foobar'
account.email = '[email protected]'
account.validate(function(err) {
//console.log('>>>>>>>>>>>>>>>>>>')
//console.log('err:',err)
//console.log('>>>>>>>>>>>>>>>>>>')
expect( err ).to.be.null
done()
})
})
})
})
// Account.password
describe('"password"', function() {
// Account.username INVALID tests
describe('should be invalid if', function() {
it('value is undefined', function(done) {
var account = new Account()
var error
account.username = 'john.doe'
account.email = '[email protected]'
account.validate(function(err) {
expect( err.errors.password ).to.exist
done()
})
})
it('value is null', function(done) {
var account = new Account()
var error
account.username = 'john.doe'
account.password = null
account.email = '[email protected]'
account.validate(function(err) {
expect( err.errors.password ).to.exist
done()
})
})
})
})
// Account.email
describe('"email"', function() {
// Account.email INVALID tests
describe('should be invalid if', function() {
it('value is undefined', function(done) {
var account = new Account()
var error
account.username = 'john.doe'
account.password = 'Password$123'
account.validate(function(err) {
expect( err.errors.email ).to.exist
done()
})
})
it('value is null', function(done) {
var account = new Account()
var error
account.username = 'john.doe'
account.password = 'Password$123'
account.email = null
account.validate(function(err) {
expect( err.errors.email ).to.exist
done()
})
})
it('value is an invalid email (sasset.com)', function(done) {
var account = new Account()
var error
account.username = 'john.doe'
account.password = 'Password$123'
account.email = 'sasset.com'
account.validate(function(err) {
expect( err.errors.email ).to.exist
done()
})
})
it('value is shorter than 4 characters', function(done) {
var account = new Account()
var error
account.username = 'john.doe'
account.password = 'Password$123'
account.email = 'a@b'
account.validate(function(err) {
expect( err.errors.email ).to.exist
done()
})
})
it('value is longer than 255 characters', function(done) {
var str = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789"
var account = new Account()
var error
account.username = 'john.doe'
account.password = 'Password$123'
account.email = `${str}.${str}.${str}@${str}.${str}.com`
account.validate(function(err) {
expect( err.errors.email ).to.exist
done()
})
})
it('value is an invalid email (contains spaces)', function(done) {
var account = new Account()
var error
account.username = 'john.doe'
account.password = 'Password$123'
account.email = 'john [email protected]'
account.validate(function(err) {
expect( err.errors.email ).to.exist
done()
})
})
it('value is an invalid email (contains special chars)', function(done) {
var account = new Account()
var error
account.username = 'john.doe'
account.password = 'Password$123'
account.email = 'john<dot>doe<at>site<dot>com'
account.validate(function(err) {
expect( err.errors.email ).to.exist
done()
})
})
_.each([{
email: 'john<dot>doe<at>site<dot>com',
reason: 'contains illegal special chars'
},{
email: 'john [email protected]',
reason: 'contains spaces'
},{
email: 'john@[email protected]',
reason: 'contains two @ symbols'
}], e => {
it(`value is the invalid email "${e.email}" (${e.reason})`, function(done) {
var account = new Account()
var error
account.username = 'john.doe'
account.password = 'Password$123'
account.email = e.email
account.validate(function(err) {
expect( err.errors.email ).to.exist
done()
})
})
})
})
})
/*
describe('Password should be invalid if', function() {
it('value is undefined or null', function(done) {
var account = new Account()
var error
account.username = 'foobar'
account.email = '[email protected]'
account.validate(function(err) {
expect( err.errors.password ).to.exist
done()
})
})
it('value is not a string', function(done) {
var account = new Account()
var error
account.username = 'foobar'
account.password = { foo: 'bar' }
account.email = '[email protected]'
account.validate(function(err) {
expect( err.errors.password ).to.exist
done()
})
})
})
describe('Email', function() {
it('invalid email was provided', function(done) {
var account = new Account()
var error
account.username = 'jhyldfdsand87'
account.password = 'foobar'
account.email = 'jhydfland8com'
account.validate(function(err) {
expect( err.errors.email ).to.exist
done()
})
})
})
it('should contain validation errors for username/password/email if no account data is provided', function(done) {
var account = new Account()
var error
account.validate(function(err) {
expect( err.errors )
.to.have.all.keys( 'username', 'password', 'email' )
done()
})
})
*/
})
})
/*
var User = db.model('User', userSchema);
var user = new User();
var error;
user.phone = '555.0123';
user.name = 'test';
user.validate(function(error) {
assert.ok(error);
assert.equal(error.errors['phone'].message,
'555.0123 is not a valid phone number!');
assert.equal(error.errors['name'].message,
'Validator failed for path `name` with value `test`');
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment