Merge pull request #385 from offlinehacker/master
Validate username uniqueness
This commit is contained in:
commit
74e9ff75e3
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
User.create({email: 'b@c.com'}, function (err) {
|
||||
User.login({email: 'b@c.com'}, function (err, accessToken) {
|
||||
|
|
Loading…
Reference in New Issue