Skip to content

test(model): refactor tests to expose model update bugs #1009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@

var app = require('../../app');
var User = require('./user.model');

var user = new User({
provider: 'local',
name: 'Fake User',
email: '[email protected]',
password: 'password'
});
var user;
var genUser = function() {
user = new User({
provider: 'local',
name: 'Fake User',
email: '[email protected]',
password: 'password'
});
return user;
};

describe('User Model', function() {
before(function() {
// Clear users before testing
return User.remove().exec();
return User.removeAsync();
});

beforeEach(function() {
genUser();
});

afterEach(function() {
return User.remove().exec();
return User.removeAsync();
});

it('should begin with no users', function() {
Expand All @@ -28,21 +35,38 @@ describe('User Model', function() {
it('should fail when saving a duplicate user', function() {
return user.saveAsync()
.then(function() {
var userDup = new User(user);
var userDup = genUser();
return userDup.saveAsync();
}).should.be.rejected;
});

it('should fail when saving without an email', function() {
user.email = '';
return user.saveAsync().should.be.rejected;
describe('#email', function() {
it('should fail when saving without an email', function() {
user.email = '';
return user.saveAsync().should.be.rejected;
});
});

it('should authenticate user if password is valid', function() {
user.authenticate('password').should.be.true;
});
describe('#password', function() {
beforeEach(function() {
return user.saveAsync();
});

it('should authenticate user if valid', function() {
user.authenticate('password').should.be.true;
});

it('should not authenticate user if password is invalid', function() {
user.authenticate('blah').should.not.be.true;
it('should not authenticate user if invalid', function() {
user.authenticate('blah').should.not.be.true;
});

it('should remain the same hash unless the password is updated', function() {
user.name = 'Test User';
return user.saveAsync()
.spread(function(u) {
return u.authenticate('password');
}).should.eventually.be.true;
});
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

var app = require('../../app');
var User = require('../../sqldb').User;

var userTemplate = {
provider: 'local',
name: 'Fake User',
email: '[email protected]',
password: 'password'
var user;
var genUser = function() {
user = User.build({
provider: 'local',
name: 'Fake User',
email: '[email protected]',
password: 'password'
});
return user;
};

var user = User.build(userTemplate);

describe('User Model', function() {
before(function() {
// Sync and clear users before testing
Expand All @@ -20,6 +21,10 @@ describe('User Model', function() {
});
});

beforeEach(function() {
genUser();
});

afterEach(function() {
return User.destroy({ where: {} });
});
Expand All @@ -32,21 +37,38 @@ describe('User Model', function() {
it('should fail when saving a duplicate user', function() {
return user.save()
.then(function() {
var userDup = User.build(userTemplate);
var userDup = genUser();
return userDup.save();
}).should.be.rejected;
});

it('should fail when saving without an email', function() {
user.email = '';
return user.save().should.be.rejected;
describe('#email', function() {
it('should fail when saving without an email', function() {
user.email = '';
return user.save().should.be.rejected;
});
});

it('should authenticate user if password is valid', function() {
user.authenticate('password').should.be.true;
});
describe('#password', function() {
beforeEach(function() {
return user.save();
});

it('should authenticate user if valid', function() {
user.authenticate('password').should.be.true;
});

it('should not authenticate user if password is invalid', function() {
user.authenticate('blah').should.not.be.true;
it('should not authenticate user if invalid', function() {
user.authenticate('blah').should.not.be.true;
});

it('should remain the same hash unless the password is updated', function() {
user.name = 'Test User';
return user.save()
.then(function(u) {
return u.authenticate('password');
}).should.eventually.be.true;
});
});

});