Merge pull request #385 from offlinehacker/master

Validate username uniqueness
This commit is contained in:
Raymond Feng 2014-07-21 15:26:01 -07:00
commit 74e9ff75e3
2 changed files with 12 additions and 1 deletions

View File

@ -527,10 +527,12 @@ User.setup = function () {
UserModel.email = require('./email'); UserModel.email = require('./email');
UserModel.accessToken = require('./access-token'); UserModel.accessToken = require('./access-token');
UserModel.validatesUniquenessOf('email', {message: 'Email already exists'}); // email validation regex
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
UserModel.validatesUniquenessOf('email', {message: 'Email already exists'});
UserModel.validatesFormatOf('email', {with: re, message: 'Must provide a valid email'}); UserModel.validatesFormatOf('email', {with: re, message: 'Must provide a valid email'});
UserModel.validatesUniquenessOf('username', {message: 'User already exists'});
return UserModel; return UserModel;
} }

View File

@ -110,6 +110,15 @@ describe('User', function(){
}); });
}); });
it('Requires a unique username', function(done) {
User.create({email: 'a@b.com', username: 'abc', password: 'foobar'}, function () {
User.create({email: 'b@b.com', username: 'abc', password: 'batbaz'}, function (err) {
assert(err, 'should error because the username is not unique!');
done();
});
});
});
it('Requires a password to login with basic auth', function(done) { it('Requires a password to login with basic auth', function(done) {
User.create({email: 'b@c.com'}, function (err) { User.create({email: 'b@c.com'}, function (err) {
User.login({email: 'b@c.com'}, function (err, accessToken) { User.login({email: 'b@c.com'}, function (err, accessToken) {