Validate username uniqueness

Signed-off-by: Jaka Hudoklin <jakahudoklin@gmail.com>
This commit is contained in:
Jaka Hudoklin 2014-07-19 14:18:21 +02:00
parent 4f7a9869e0
commit 90094e5e86
2 changed files with 12 additions and 1 deletions

View File

@ -527,10 +527,12 @@ User.setup = function () {
UserModel.email = require('./email');
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,}))$/;
UserModel.validatesUniquenessOf('email', {message: 'Email already exists'});
UserModel.validatesFormatOf('email', {with: re, message: 'Must provide a valid email'});
UserModel.validatesUniquenessOf('username', {message: 'User already exists'});
return UserModel;
}

View File

@ -109,6 +109,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) {
User.create({email: 'b@c.com'}, function (err) {