This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #756 from lirantal/feature/users_module_tests
Refactoring Tests for User model
- Loading branch information
Showing
1 changed file
with
60 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,33 +17,33 @@ var user, user2, user3; | |
*/ | ||
describe('User Model Unit Tests:', function () { | ||
before(function (done) { | ||
user = new User({ | ||
user = { | ||
firstName: 'Full', | ||
lastName: 'Name', | ||
displayName: 'Full Name', | ||
email: '[email protected]', | ||
username: 'username', | ||
password: 'password', | ||
provider: 'local' | ||
}); | ||
user2 = new User({ | ||
}; | ||
user2 = { | ||
firstName: 'Full', | ||
lastName: 'Name', | ||
displayName: 'Full Name', | ||
email: '[email protected]', | ||
username: 'username', | ||
password: 'password', | ||
provider: 'local' | ||
}); | ||
user3 = new User({ | ||
}; | ||
user3 = { | ||
firstName: 'Different', | ||
lastName: 'User', | ||
displayName: 'Full Different Name', | ||
email: '[email protected]', | ||
username: 'different_username', | ||
password: 'different_password', | ||
provider: 'local' | ||
}); | ||
}; | ||
|
||
done(); | ||
}); | ||
|
@@ -57,59 +57,92 @@ describe('User Model Unit Tests:', function () { | |
}); | ||
|
||
it('should be able to save without problems', function (done) { | ||
user.save(done); | ||
var _user = new User(user); | ||
|
||
_user.save(function (err) { | ||
should.not.exist(err); | ||
_user.remove(function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should fail to save an existing user again', function (done) { | ||
user.save(function () { | ||
user2.save(function (err) { | ||
var _user = new User(user); | ||
var _user2 = new User(user2); | ||
|
||
_user.save(function () { | ||
_user2.save(function (err) { | ||
should.exist(err); | ||
done(); | ||
_user.remove(function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should be able to show an error when try to save without first name', function (done) { | ||
user.firstName = ''; | ||
return user.save(function (err) { | ||
var _user = new User(user); | ||
|
||
_user.firstName = ''; | ||
_user.save(function (err) { | ||
should.exist(err); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should confirm that saving user model doesnt change the password', function (done) { | ||
user.firstName = 'test'; | ||
var passwordBefore = user.password; | ||
return user.save(function (err) { | ||
var passwordAfter = user.password; | ||
passwordBefore.should.equal(passwordAfter); | ||
done(); | ||
var _user = new User(user); | ||
|
||
_user.save(function (err) { | ||
should.not.exist(err); | ||
var passwordBefore = _user.password; | ||
_user.firstName = 'test'; | ||
_user.save(function (err) { | ||
var passwordAfter = _user.password; | ||
passwordBefore.should.equal(passwordAfter); | ||
_user.remove(function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should be able to save 2 different users', function (done) { | ||
user.remove(function (err) { | ||
var _user = new User(user); | ||
var _user3 = new User(user3); | ||
|
||
_user.save(function (err) { | ||
should.not.exist(err); | ||
user.save(function (err) { | ||
user3.save(function (err) { | ||
_user3.save(function (err) { | ||
should.not.exist(err); | ||
_user3.remove(function (err) { | ||
should.not.exist(err); | ||
user3.remove(function (err) { | ||
_user.remove(function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
|
||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should not be able to save different user with the same email address', function (done) { | ||
user.remove(function (err) { | ||
var _user = new User(user); | ||
var _user3 = new User(user3); | ||
|
||
_user.remove(function (err) { | ||
should.not.exist(err); | ||
user.save(function (err) { | ||
user3.email = user.email; | ||
user3.save(function (err) { | ||
_user.save(function (err) { | ||
var user3_email = _user3.email; | ||
_user3.email = _user.email; | ||
_user3.save(function (err) { | ||
should.exist(err); | ||
// Restoring the original email for test3 so it can be used in later tests | ||
_user3.email = user3_email; | ||
done(); | ||
}); | ||
}); | ||
|