Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 = 'john.doe@fake.com'
- 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 = 'john.doe@fake.com'
- 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 = 'john.doe@fake.com'
- 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 = 'john.doe@fake.com'
- 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 = 'jhydland87@idkwtf.com'
- 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 = 'jhylanadd87@idkwtf.com'
- 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 = 'jhyland87@idkwtf.com'
- 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 = 'jhyland87@idkwtf.com'
- 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 doe@site.com'
- 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 doe@site.com',
- reason: 'contains spaces'
- },{
- email: 'john@doe@site.com',
- 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 = 'jhyaland87@idkwtf.com'
- 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 = 'jhyadfland87@idkwtf.com'
- 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`');
- });
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement