From 90094e5e868d261354a039f37a1de0bfb7b1dddd Mon Sep 17 00:00:00 2001 From: Jaka Hudoklin Date: Sat, 19 Jul 2014 14:18:21 +0200 Subject: [PATCH] Validate username uniqueness Signed-off-by: Jaka Hudoklin --- lib/models/user.js | 4 +++- test/user.test.js | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/models/user.js b/lib/models/user.js index d1bf7e83..357c70f2 100644 --- a/lib/models/user.js +++ b/lib/models/user.js @@ -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; } diff --git a/test/user.test.js b/test/user.test.js index 8b2e7b92..63c9a606 100644 --- a/test/user.test.js +++ b/test/user.test.js @@ -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) {