Validate uniqueness and format of User email.
This commit is contained in:
parent
16617a3737
commit
d9b5daba0e
12
README.md
12
README.md
|
@ -98,6 +98,12 @@ Define an asteroid model.
|
|||
|
||||
### Validation (expiremental)
|
||||
|
||||
#### Model.validatesFormatOf(property, options)
|
||||
|
||||
Require a model to include a property that matches the given format.
|
||||
|
||||
User.validatesFormat('name', {with: /\w+/});
|
||||
|
||||
#### Model.validatesPresenceOf(properties...)
|
||||
|
||||
Require a model to include a property to be considered valid.
|
||||
|
@ -933,8 +939,10 @@ User.login({"email": "foo@bar.com", "password": "bar"}, function(err, session) {
|
|||
});
|
||||
});
|
||||
|
||||
// logout a user by username (server side only)
|
||||
|
||||
// logout a user (server side only)
|
||||
User.findOne({email: 'foo@bar.com'}, function(err, user) {
|
||||
user.logout();
|
||||
});
|
||||
|
||||
**REST**
|
||||
|
||||
|
|
|
@ -287,6 +287,11 @@ User.setup = function () {
|
|||
UserModel.email = require('./email');
|
||||
UserModel.session = require('./session');
|
||||
|
||||
UserModel.validatesUniquenessOf('email', {message: 'Email already exists'});
|
||||
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.validatesFormatOf('email', {with: re, message: 'Must provide a valid email'});
|
||||
|
||||
return UserModel;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@ asteroid.User.session.attachTo(userMemory);
|
|||
asteroid.User.email.setup({transports: [{type: 'STUB'}]});
|
||||
|
||||
describe('User', function(){
|
||||
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
app.use(asteroid.cookieParser());
|
||||
|
@ -21,7 +23,49 @@ describe('User', function(){
|
|||
});
|
||||
|
||||
afterEach(function (done) {
|
||||
Session.destroyAll(done);
|
||||
asteroid.User.destroyAll(function (err) {
|
||||
Session.destroyAll(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('User.create', function(){
|
||||
it('Create a new user.', function(done) {
|
||||
User.create({email: 'f@b.com'}, function (err, user) {
|
||||
assert(!err);
|
||||
assert(user.id);
|
||||
assert(user.email);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('Requires a valid email.', function(done) {
|
||||
User.create({}, function (err) {
|
||||
assert(err);
|
||||
User.create({email: 'foo@'}, function (err) {
|
||||
assert(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Requires a unique email.', function(done) {
|
||||
User.create({email: 'a@b.com'}, function () {
|
||||
User.create({email: 'a@b.com'}, function (err) {
|
||||
assert(err, 'should error because the email 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, session) {
|
||||
assert(!session, 'should not create a session without a valid password');
|
||||
assert(err, 'should not login without a password');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('User.login', function() {
|
||||
|
|
Loading…
Reference in New Issue