2015-04-20 16:23:44 +00:00
|
|
|
require('./support');
|
|
|
|
var loopback = require('../');
|
2014-01-23 22:39:15 +00:00
|
|
|
var User;
|
2015-04-20 16:23:44 +00:00
|
|
|
var AccessToken;
|
2013-10-12 02:06:16 +00:00
|
|
|
var MailConnector = require('../lib/connectors/mail');
|
2013-07-02 23:51:38 +00:00
|
|
|
|
2013-07-16 17:49:25 +00:00
|
|
|
var userMemory = loopback.createDataSource({
|
2014-02-14 18:31:30 +00:00
|
|
|
connector: 'memory'
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
2013-07-15 17:56:42 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('User', function() {
|
2014-12-22 03:18:36 +00:00
|
|
|
var validCredentialsEmail = 'foo@bar.com';
|
|
|
|
var validCredentials = {email: validCredentialsEmail, password: 'bar'};
|
2014-07-07 21:09:45 +00:00
|
|
|
var validCredentialsEmailVerified = {email: 'foo1@bar.com', password: 'bar1', emailVerified: true};
|
|
|
|
var validCredentialsEmailVerifiedOverREST = {email: 'foo2@bar.com', password: 'bar2', emailVerified: true};
|
2014-06-06 21:30:18 +00:00
|
|
|
var validCredentialsWithTTL = {email: 'foo@bar.com', password: 'bar', ttl: 3600};
|
2015-03-02 22:48:08 +00:00
|
|
|
var validCredentialsWithTTLAndScope = {email: 'foo@bar.com', password: 'bar', ttl: 3600, scope: 'all'};
|
2015-11-09 21:21:38 +00:00
|
|
|
var validMixedCaseEmailCredentials = {email: 'Foo@bar.com', password: 'bar'};
|
2014-12-22 20:41:49 +00:00
|
|
|
var invalidCredentials = {email: 'foo1@bar.com', password: 'invalid'};
|
2014-02-28 21:19:52 +00:00
|
|
|
var incompleteCredentials = {password: 'bar1'};
|
|
|
|
|
2015-04-20 16:23:44 +00:00
|
|
|
var defaultApp;
|
|
|
|
|
2014-01-23 22:39:15 +00:00
|
|
|
beforeEach(function() {
|
2015-04-20 16:23:44 +00:00
|
|
|
// FIXME: [rfeng] Remove loopback.User.app so that remote hooks don't go
|
|
|
|
// to the wrong app instance
|
|
|
|
defaultApp = loopback.User.app;
|
|
|
|
loopback.User.app = null;
|
|
|
|
User = loopback.User.extend('TestUser', {}, {http: {path: 'test-users'}});
|
|
|
|
AccessToken = loopback.AccessToken.extend('TestAccessToken');
|
2014-01-23 22:39:15 +00:00
|
|
|
User.email = loopback.Email.extend('email');
|
|
|
|
loopback.autoAttach();
|
2014-02-14 18:31:30 +00:00
|
|
|
|
|
|
|
// Update the AccessToken relation to use the subclass of User
|
2015-04-20 16:23:44 +00:00
|
|
|
AccessToken.belongsTo(User, {as: 'user', foreignKey: 'userId'});
|
|
|
|
User.hasMany(AccessToken, {as: 'accessTokens', foreignKey: 'userId'});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-01-23 22:39:15 +00:00
|
|
|
// allow many User.afterRemote's to be called
|
|
|
|
User.setMaxListeners(0);
|
2014-02-14 18:31:30 +00:00
|
|
|
|
2013-11-19 18:29:02 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
beforeEach(function(done) {
|
2014-10-23 11:16:53 +00:00
|
|
|
app.enableAuth();
|
2015-04-20 16:23:44 +00:00
|
|
|
app.use(loopback.token({model: AccessToken}));
|
2013-07-16 17:49:25 +00:00
|
|
|
app.use(loopback.rest());
|
2013-07-16 01:22:33 +00:00
|
|
|
app.model(User);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-07 21:09:45 +00:00
|
|
|
User.create(validCredentials, function(err, user) {
|
|
|
|
User.create(validCredentialsEmailVerified, done);
|
|
|
|
});
|
2013-07-02 23:51:38 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
afterEach(function(done) {
|
2015-04-20 16:23:44 +00:00
|
|
|
loopback.User.app = defaultApp;
|
2014-11-21 02:35:36 +00:00
|
|
|
User.destroyAll(function(err) {
|
2013-11-13 19:49:08 +00:00
|
|
|
User.accessToken.destroyAll(done);
|
2013-07-13 00:03:13 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('User.create', function() {
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Create a new user', function(done) {
|
2014-11-21 02:35:36 +00:00
|
|
|
User.create({email: 'f@b.com', password: 'bar'}, function(err, user) {
|
2013-07-13 00:03:13 +00:00
|
|
|
assert(!err);
|
|
|
|
assert(user.id);
|
|
|
|
assert(user.email);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-12-04 22:41:25 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
it('Create a new user (email case-sensitivity off)', function(done) {
|
|
|
|
User.settings.caseSensitiveEmail = false;
|
|
|
|
User.create({email: 'F@b.com', password: 'bar'}, function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert(user.id);
|
|
|
|
assert.equal(user.email, user.email.toLowerCase());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Create a new user (email case-sensitive)', function(done) {
|
|
|
|
User.create({email: 'F@b.com', password: 'bar'}, function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert(user.id);
|
|
|
|
assert(user.email);
|
|
|
|
assert.notEqual(user.email, user.email.toLowerCase());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('credentials/challenges are object types', function(done) {
|
2014-07-15 15:20:47 +00:00
|
|
|
User.create({email: 'f1@b.com', password: 'bar1',
|
|
|
|
credentials: {cert: 'xxxxx', key: '111'},
|
|
|
|
challenges: {x: 'X', a: 1}
|
2014-11-21 02:35:36 +00:00
|
|
|
}, function(err, user) {
|
2014-07-15 15:20:47 +00:00
|
|
|
assert(!err);
|
2014-11-21 02:35:36 +00:00
|
|
|
User.findById(user.id, function(err, user) {
|
2014-07-15 15:20:47 +00:00
|
|
|
assert(user.id);
|
|
|
|
assert(user.email);
|
|
|
|
assert.deepEqual(user.credentials, {cert: 'xxxxx', key: '111'});
|
|
|
|
assert.deepEqual(user.challenges, {x: 'X', a: 1});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Email is required', function(done) {
|
|
|
|
User.create({password: '123'}, function(err) {
|
2014-05-20 21:39:28 +00:00
|
|
|
assert(err);
|
2014-11-21 01:46:21 +00:00
|
|
|
assert.equal(err.name, 'ValidationError');
|
2014-05-20 21:39:28 +00:00
|
|
|
assert.equal(err.statusCode, 422);
|
2015-04-20 16:23:44 +00:00
|
|
|
assert.equal(err.details.context, User.modelName);
|
2014-05-20 21:39:28 +00:00
|
|
|
assert.deepEqual(err.details.codes.email, [
|
|
|
|
'presence',
|
2015-01-30 16:52:45 +00:00
|
|
|
'format.null'
|
2014-05-20 21:39:28 +00:00
|
|
|
]);
|
2013-12-04 22:41:25 +00:00
|
|
|
|
2013-07-28 20:20:55 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-28 20:20:55 +00:00
|
|
|
// will change in future versions where password will be optional by default
|
|
|
|
it('Password is required', function(done) {
|
2014-11-21 01:46:21 +00:00
|
|
|
var u = new User({email: '123@456.com'});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
User.create({email: 'c@d.com'}, function(err) {
|
2013-07-28 20:20:55 +00:00
|
|
|
assert(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Requires a valid email', function(done) {
|
2014-11-21 02:35:36 +00:00
|
|
|
User.create({email: 'foo@', password: '123'}, function(err) {
|
2013-07-13 00:03:13 +00:00
|
|
|
assert(err);
|
2013-07-28 20:20:55 +00:00
|
|
|
done();
|
2013-07-13 00:03:13 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Requires a unique email', function(done) {
|
2014-11-21 02:35:36 +00:00
|
|
|
User.create({email: 'a@b.com', password: 'foobar'}, function() {
|
|
|
|
User.create({email: 'a@b.com', password: 'batbaz'}, function(err) {
|
2013-07-13 00:03:13 +00:00
|
|
|
assert(err, 'should error because the email is not unique!');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-19 12:18:21 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
it('Requires a unique email (email case-sensitivity off)', function(done) {
|
|
|
|
User.settings.caseSensitiveEmail = false;
|
|
|
|
User.create({email: 'A@b.com', password: 'foobar'}, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
User.create({email: 'a@b.com', password: 'batbaz'}, function(err) {
|
|
|
|
assert(err, 'should error because the email is not unique!');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Requires a unique email (email case-sensitive)', function(done) {
|
|
|
|
User.create({email: 'A@b.com', password: 'foobar'}, function(err, user1) {
|
|
|
|
User.create({email: 'a@b.com', password: 'batbaz'}, function(err, user2) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert.notEqual(user1.email, user2.email);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-19 12:18:21 +00:00
|
|
|
it('Requires a unique username', function(done) {
|
2014-11-21 02:35:36 +00:00
|
|
|
User.create({email: 'a@b.com', username: 'abc', password: 'foobar'}, function() {
|
|
|
|
User.create({email: 'b@b.com', username: 'abc', password: 'batbaz'}, function(err) {
|
2014-07-19 12:18:21 +00:00
|
|
|
assert(err, 'should error because the username is not unique!');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
});
|
|
|
|
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Requires a password to login with basic auth', function(done) {
|
2014-11-21 02:35:36 +00:00
|
|
|
User.create({email: 'b@c.com'}, function(err) {
|
|
|
|
User.login({email: 'b@c.com'}, function(err, accessToken) {
|
2013-11-13 19:49:08 +00:00
|
|
|
assert(!accessToken, 'should not create a accessToken without a valid password');
|
2013-07-13 00:03:13 +00:00
|
|
|
assert(err, 'should not login without a password');
|
2014-12-18 20:26:27 +00:00
|
|
|
assert.equal(err.code, 'LOGIN_FAILED');
|
2013-07-13 00:03:13 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Hashes the given password', function() {
|
2013-07-16 01:22:33 +00:00
|
|
|
var u = new User({username: 'foo', password: 'bar'});
|
|
|
|
assert(u.password !== 'bar');
|
|
|
|
});
|
2014-07-07 21:09:45 +00:00
|
|
|
|
2015-02-25 00:36:51 +00:00
|
|
|
it('does not hash the password if it\'s already hashed', function() {
|
|
|
|
var u1 = new User({username: 'foo', password: 'bar'});
|
|
|
|
assert(u1.password !== 'bar');
|
|
|
|
var u2 = new User({username: 'foo', password: u1.password});
|
|
|
|
assert(u2.password === u1.password);
|
|
|
|
});
|
|
|
|
|
2014-12-20 23:57:38 +00:00
|
|
|
describe('custom password hash', function() {
|
|
|
|
var defaultHashPassword;
|
|
|
|
var defaultValidatePassword;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
defaultHashPassword = User.hashPassword;
|
2015-04-20 16:23:44 +00:00
|
|
|
defaultValidatePassword = User.validatePassword;
|
2014-12-20 23:57:38 +00:00
|
|
|
|
|
|
|
User.hashPassword = function(plain) {
|
|
|
|
return plain.toUpperCase();
|
|
|
|
};
|
|
|
|
|
|
|
|
User.validatePassword = function(plain) {
|
|
|
|
if (!plain || plain.length < 3) {
|
|
|
|
throw new Error('Password must have at least 3 chars');
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
User.hashPassword = defaultHashPassword;
|
2015-04-20 16:23:44 +00:00
|
|
|
User.validatePassword = defaultValidatePassword;
|
2014-12-20 23:57:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Reports invalid password', function() {
|
|
|
|
try {
|
|
|
|
var u = new User({username: 'foo', password: 'aa'});
|
|
|
|
assert(false, 'Error should have been thrown');
|
|
|
|
} catch (e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Hashes the given password', function() {
|
|
|
|
var u = new User({username: 'foo', password: 'bar'});
|
|
|
|
assert(u.password === 'BAR');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-07 21:09:45 +00:00
|
|
|
it('Create a user over REST should remove emailVerified property', function(done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users')
|
2014-07-07 21:09:45 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send(validCredentialsEmailVerifiedOverREST)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-12-22 20:48:12 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-07-07 21:09:45 +00:00
|
|
|
assert(!res.body.emailVerified);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
describe('Access-hook for queries with email NOT case-sensitive', function() {
|
|
|
|
it('Should not throw an error if the query does not contain {where: }', function(done) {
|
|
|
|
User.find({}, function(err) {
|
|
|
|
if (err) done(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should be able to find lowercase email with mixed-case email query', function(done) {
|
|
|
|
User.settings.caseSensitiveEmail = false;
|
|
|
|
User.find({where:{email: validMixedCaseEmailCredentials.email}}, function(err, result) {
|
|
|
|
if (err) done(err);
|
|
|
|
assert(result[0], 'The query did not find the user');
|
|
|
|
assert.equal(result[0].email, validCredentialsEmail);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-07-03 05:37:31 +00:00
|
|
|
describe('User.login', function() {
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Login a user by providing credentials', function(done) {
|
2014-11-21 02:35:36 +00:00
|
|
|
User.login(validCredentials, function(err, accessToken) {
|
2013-11-15 02:34:51 +00:00
|
|
|
assert(accessToken.userId);
|
2013-11-13 19:49:08 +00:00
|
|
|
assert(accessToken.id);
|
2013-11-14 21:01:47 +00:00
|
|
|
assert.equal(accessToken.id.length, 64);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-28 20:20:55 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-06-06 21:30:18 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
it('Login a user by providing email credentials (email case-sensitivity off)', function(done) {
|
|
|
|
User.settings.caseSensitiveEmail = false;
|
|
|
|
User.login(validMixedCaseEmailCredentials, function(err, accessToken) {
|
|
|
|
assert(accessToken.userId);
|
|
|
|
assert(accessToken.id);
|
|
|
|
assert.equal(accessToken.id.length, 64);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Try to login with invalid email case', function(done) {
|
|
|
|
User.login(validMixedCaseEmailCredentials, function(err, accessToken) {
|
|
|
|
assert(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-06-06 21:30:18 +00:00
|
|
|
it('Login a user by providing credentials with TTL', function(done) {
|
2014-11-21 02:35:36 +00:00
|
|
|
User.login(validCredentialsWithTTL, function(err, accessToken) {
|
2014-06-06 21:30:18 +00:00
|
|
|
assert(accessToken.userId);
|
|
|
|
assert(accessToken.id);
|
|
|
|
assert.equal(accessToken.ttl, validCredentialsWithTTL.ttl);
|
|
|
|
assert.equal(accessToken.id.length, 64);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
it('honors default `createAccessToken` implementation', function(done) {
|
|
|
|
User.login(validCredentialsWithTTL, function(err, accessToken) {
|
|
|
|
assert(accessToken.userId);
|
|
|
|
assert(accessToken.id);
|
|
|
|
|
|
|
|
User.findById(accessToken.userId, function(err, user) {
|
|
|
|
user.createAccessToken(120, function(err, accessToken) {
|
|
|
|
assert(accessToken.userId);
|
|
|
|
assert(accessToken.id);
|
|
|
|
assert.equal(accessToken.ttl, 120);
|
|
|
|
assert.equal(accessToken.id.length, 64);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('honors default `createAccessToken` implementation - promise variant', function(done) {
|
|
|
|
User.login(validCredentialsWithTTL, function(err, accessToken) {
|
|
|
|
assert(accessToken.userId);
|
|
|
|
assert(accessToken.id);
|
|
|
|
|
|
|
|
User.findById(accessToken.userId, function(err, user) {
|
|
|
|
user.createAccessToken(120)
|
|
|
|
.then(function(accessToken) {
|
|
|
|
assert(accessToken.userId);
|
|
|
|
assert(accessToken.id);
|
|
|
|
assert.equal(accessToken.ttl, 120);
|
|
|
|
assert.equal(accessToken.id.length, 64);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-06-06 21:30:18 +00:00
|
|
|
it('Login a user using a custom createAccessToken', function(done) {
|
|
|
|
var createToken = User.prototype.createAccessToken; // Save the original method
|
|
|
|
// Override createAccessToken
|
|
|
|
User.prototype.createAccessToken = function(ttl, cb) {
|
|
|
|
// Reduce the ttl by half for testing purpose
|
2014-11-21 02:35:36 +00:00
|
|
|
this.accessTokens.create({ttl: ttl / 2 }, cb);
|
2014-06-06 21:30:18 +00:00
|
|
|
};
|
2014-11-21 02:35:36 +00:00
|
|
|
User.login(validCredentialsWithTTL, function(err, accessToken) {
|
2014-06-06 21:30:18 +00:00
|
|
|
assert(accessToken.userId);
|
|
|
|
assert(accessToken.id);
|
|
|
|
assert.equal(accessToken.ttl, 1800);
|
|
|
|
assert.equal(accessToken.id.length, 64);
|
|
|
|
|
|
|
|
User.findById(accessToken.userId, function(err, user) {
|
2014-11-21 02:35:36 +00:00
|
|
|
user.createAccessToken(120, function(err, accessToken) {
|
2014-06-06 21:30:18 +00:00
|
|
|
assert(accessToken.userId);
|
|
|
|
assert(accessToken.id);
|
|
|
|
assert.equal(accessToken.ttl, 60);
|
|
|
|
assert.equal(accessToken.id.length, 64);
|
|
|
|
// Restore create access token
|
|
|
|
User.prototype.createAccessToken = createToken;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-07 21:09:45 +00:00
|
|
|
|
2015-03-02 22:48:08 +00:00
|
|
|
it('Login a user using a custom createAccessToken with options',
|
|
|
|
function(done) {
|
|
|
|
var createToken = User.prototype.createAccessToken; // Save the original method
|
|
|
|
// Override createAccessToken
|
|
|
|
User.prototype.createAccessToken = function(ttl, options, cb) {
|
|
|
|
// Reduce the ttl by half for testing purpose
|
|
|
|
this.accessTokens.create({ttl: ttl / 2, scopes: options.scope}, cb);
|
|
|
|
};
|
|
|
|
User.login(validCredentialsWithTTLAndScope, function(err, accessToken) {
|
|
|
|
assert(accessToken.userId);
|
|
|
|
assert(accessToken.id);
|
|
|
|
assert.equal(accessToken.ttl, 1800);
|
|
|
|
assert.equal(accessToken.id.length, 64);
|
|
|
|
assert.equal(accessToken.scopes, 'all');
|
|
|
|
|
|
|
|
User.findById(accessToken.userId, function(err, user) {
|
|
|
|
user.createAccessToken(120, {scope: 'default'}, function(err, accessToken) {
|
|
|
|
assert(accessToken.userId);
|
|
|
|
assert(accessToken.id);
|
|
|
|
assert.equal(accessToken.ttl, 60);
|
|
|
|
assert.equal(accessToken.id.length, 64);
|
|
|
|
assert.equal(accessToken.scopes, 'default');
|
|
|
|
// Restore create access token
|
|
|
|
User.prototype.createAccessToken = createToken;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-22 22:20:15 +00:00
|
|
|
it('Login should only allow correct credentials', function(done) {
|
|
|
|
User.login(invalidCredentials, function(err, accessToken) {
|
|
|
|
assert(err);
|
2014-12-18 20:26:27 +00:00
|
|
|
assert.equal(err.code, 'LOGIN_FAILED');
|
2014-12-22 22:20:15 +00:00
|
|
|
assert(!accessToken);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
it('Login should only allow correct credentials - promise variant', function(done) {
|
|
|
|
User.login(invalidCredentials)
|
|
|
|
.then(function(accessToken) {
|
|
|
|
assert(!accessToken);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'LOGIN_FAILED');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-18 20:26:27 +00:00
|
|
|
it('Login a user providing incomplete credentials', function(done) {
|
|
|
|
User.login(incompleteCredentials, function(err, accessToken) {
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'USERNAME_EMAIL_REQUIRED');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
it('Login a user providing incomplete credentials - promise variant', function(done) {
|
|
|
|
User.login(incompleteCredentials)
|
|
|
|
.then(function(accessToken) {
|
|
|
|
assert(!accessToken);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'USERNAME_EMAIL_REQUIRED');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-07-28 20:20:55 +00:00
|
|
|
it('Login a user over REST by providing credentials', function(done) {
|
2013-07-02 23:51:38 +00:00
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/login')
|
2013-07-02 23:51:38 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2014-01-30 13:33:45 +00:00
|
|
|
.send(validCredentials)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-12-22 20:48:12 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2013-11-13 19:49:08 +00:00
|
|
|
var accessToken = res.body;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-11-15 02:34:51 +00:00
|
|
|
assert(accessToken.userId);
|
2013-11-13 19:49:08 +00:00
|
|
|
assert(accessToken.id);
|
2013-11-14 21:01:47 +00:00
|
|
|
assert.equal(accessToken.id.length, 64);
|
2014-01-30 13:33:45 +00:00
|
|
|
assert(accessToken.user === undefined);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-02 23:51:38 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-01-30 13:33:45 +00:00
|
|
|
|
2014-02-28 21:19:52 +00:00
|
|
|
it('Login a user over REST by providing invalid credentials', function(done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/login')
|
2014-02-28 21:19:52 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(401)
|
|
|
|
.send(invalidCredentials)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-12-22 20:39:47 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-12-18 20:26:27 +00:00
|
|
|
var errorResponse = res.body.error;
|
|
|
|
assert.equal(errorResponse.code, 'LOGIN_FAILED');
|
2014-02-28 21:19:52 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Login a user over REST by providing incomplete credentials', function(done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/login')
|
2014-02-28 21:19:52 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(400)
|
|
|
|
.send(incompleteCredentials)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-12-22 20:48:12 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-12-18 20:26:27 +00:00
|
|
|
var errorResponse = res.body.error;
|
|
|
|
assert.equal(errorResponse.code, 'USERNAME_EMAIL_REQUIRED');
|
2014-02-28 21:19:52 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Login a user over REST with the wrong Content-Type', function(done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/login')
|
2014-02-28 21:19:52 +00:00
|
|
|
.set('Content-Type', null)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(400)
|
2014-12-22 21:49:12 +00:00
|
|
|
.send(JSON.stringify(validCredentials))
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-12-22 20:48:12 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-12-18 20:26:27 +00:00
|
|
|
var errorResponse = res.body.error;
|
|
|
|
assert.equal(errorResponse.code, 'USERNAME_EMAIL_REQUIRED');
|
2014-02-28 21:19:52 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-30 13:33:45 +00:00
|
|
|
it('Returns current user when `include` is `USER`', function(done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/login?include=USER')
|
2014-01-30 13:33:45 +00:00
|
|
|
.send(validCredentials)
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.end(function(err, res) {
|
2014-12-22 20:48:12 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-01-30 13:33:45 +00:00
|
|
|
var token = res.body;
|
|
|
|
expect(token.user, 'body.user').to.not.equal(undefined);
|
|
|
|
expect(token.user, 'body.user')
|
|
|
|
.to.have.property('email', validCredentials.email);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-03 05:58:49 +00:00
|
|
|
it('should handle multiple `include`', function(done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/login?include=USER&include=Post')
|
2014-09-03 05:58:49 +00:00
|
|
|
.send(validCredentials)
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.end(function(err, res) {
|
2014-12-22 20:48:12 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-09-03 05:58:49 +00:00
|
|
|
var token = res.body;
|
|
|
|
expect(token.user, 'body.user').to.not.equal(undefined);
|
|
|
|
expect(token.user, 'body.user')
|
|
|
|
.to.have.property('email', validCredentials.email);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-07-02 23:51:38 +00:00
|
|
|
});
|
2014-07-08 15:54:50 +00:00
|
|
|
|
2014-10-23 18:10:39 +00:00
|
|
|
function assertGoodToken(accessToken) {
|
|
|
|
assert(accessToken.userId);
|
|
|
|
assert(accessToken.id);
|
|
|
|
assert.equal(accessToken.id.length, 64);
|
|
|
|
}
|
|
|
|
|
2014-07-08 15:54:50 +00:00
|
|
|
describe('User.login requiring email verification', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
User.settings.emailVerificationRequired = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
User.settings.emailVerificationRequired = false;
|
|
|
|
});
|
|
|
|
|
2014-12-22 03:18:36 +00:00
|
|
|
it('Require valid and complete credentials for email verification error', function(done) {
|
|
|
|
User.login({ email: validCredentialsEmail }, function(err, accessToken) {
|
|
|
|
// strongloop/loopback#931
|
|
|
|
// error message should be "login failed" and not "login failed as the email has not been verified"
|
|
|
|
assert(err && !/verified/.test(err.message), ('expecting "login failed" error message, received: "' + err.message + '"'));
|
2014-12-18 20:26:27 +00:00
|
|
|
assert.equal(err.code, 'LOGIN_FAILED');
|
2014-12-22 03:18:36 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
it('Require valid and complete credentials for email verification error - promise variant', function(done) {
|
|
|
|
User.login({ email: validCredentialsEmail })
|
|
|
|
.then(function(accessToken) {
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
// strongloop/loopback#931
|
|
|
|
// error message should be "login failed" and not "login failed as the email has not been verified"
|
|
|
|
assert(err && !/verified/.test(err.message), ('expecting "login failed" error message, received: "' + err.message + '"'));
|
|
|
|
assert.equal(err.code, 'LOGIN_FAILED');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-08 15:54:50 +00:00
|
|
|
it('Login a user by without email verification', function(done) {
|
2014-11-21 02:35:36 +00:00
|
|
|
User.login(validCredentials, function(err, accessToken) {
|
2014-07-08 15:54:50 +00:00
|
|
|
assert(err);
|
2014-12-18 20:26:27 +00:00
|
|
|
assert.equal(err.code, 'LOGIN_FAILED_EMAIL_NOT_VERIFIED');
|
2014-07-08 15:54:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
it('Login a user by without email verification - promise variant', function(done) {
|
|
|
|
User.login(validCredentials)
|
|
|
|
.then(function(err, accessToken) {
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'LOGIN_FAILED_EMAIL_NOT_VERIFIED');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-08 15:54:50 +00:00
|
|
|
it('Login a user by with email verification', function(done) {
|
2014-11-21 02:35:36 +00:00
|
|
|
User.login(validCredentialsEmailVerified, function(err, accessToken) {
|
2014-10-23 18:10:39 +00:00
|
|
|
assertGoodToken(accessToken);
|
2014-07-08 15:54:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
it('Login a user by with email verification - promise variant', function(done) {
|
|
|
|
User.login(validCredentialsEmailVerified)
|
|
|
|
.then(function(accessToken) {
|
|
|
|
assertGoodToken(accessToken);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-08 15:54:50 +00:00
|
|
|
it('Login a user over REST when email verification is required', function(done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/login')
|
2014-07-08 15:54:50 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send(validCredentialsEmailVerified)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-12-22 20:48:12 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-07-08 15:54:50 +00:00
|
|
|
var accessToken = res.body;
|
|
|
|
|
2014-10-23 18:10:39 +00:00
|
|
|
assertGoodToken(accessToken);
|
2014-07-08 15:54:50 +00:00
|
|
|
assert(accessToken.user === undefined);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-22 03:18:36 +00:00
|
|
|
it('Login a user over REST require complete and valid credentials for email verification error message', function(done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/login')
|
2014-12-22 03:18:36 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(401)
|
|
|
|
.send({ email: validCredentialsEmail })
|
|
|
|
.end(function(err, res) {
|
2014-12-18 20:26:27 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-12-22 03:18:36 +00:00
|
|
|
// strongloop/loopback#931
|
|
|
|
// error message should be "login failed" and not "login failed as the email has not been verified"
|
|
|
|
var errorResponse = res.body.error;
|
|
|
|
assert(errorResponse && !/verified/.test(errorResponse.message), ('expecting "login failed" error message, received: "' + errorResponse.message + '"'));
|
2014-12-18 20:26:27 +00:00
|
|
|
assert.equal(errorResponse.code, 'LOGIN_FAILED');
|
2014-12-22 03:18:36 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-08 15:54:50 +00:00
|
|
|
it('Login a user over REST without email verification when it is required', function(done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/login')
|
2014-07-08 15:54:50 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(401)
|
|
|
|
.send(validCredentials)
|
|
|
|
.end(function(err, res) {
|
2014-12-22 20:48:12 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-12-18 20:26:27 +00:00
|
|
|
var errorResponse = res.body.error;
|
|
|
|
assert.equal(errorResponse.code, 'LOGIN_FAILED_EMAIL_NOT_VERIFIED');
|
2014-07-08 15:54:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2014-10-23 18:10:39 +00:00
|
|
|
|
|
|
|
describe('User.login requiring realm', function() {
|
2014-11-21 02:35:36 +00:00
|
|
|
var User;
|
|
|
|
var AccessToken;
|
2014-10-23 18:10:39 +00:00
|
|
|
|
|
|
|
before(function() {
|
|
|
|
User = loopback.User.extend('RealmUser', {},
|
|
|
|
{realmRequired: true, realmDelimiter: ':'});
|
|
|
|
AccessToken = loopback.AccessToken.extend('RealmAccessToken');
|
|
|
|
|
|
|
|
loopback.autoAttach();
|
|
|
|
|
|
|
|
// Update the AccessToken relation to use the subclass of User
|
2015-04-20 16:23:44 +00:00
|
|
|
AccessToken.belongsTo(User, {as: 'user', foreignKey: 'userId'});
|
|
|
|
User.hasMany(AccessToken, {as: 'accessTokens', foreignKey: 'userId'});
|
2014-10-23 18:10:39 +00:00
|
|
|
|
|
|
|
// allow many User.afterRemote's to be called
|
|
|
|
User.setMaxListeners(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
var realm1User = {
|
|
|
|
realm: 'realm1',
|
|
|
|
username: 'foo100',
|
|
|
|
email: 'foo100@bar.com',
|
|
|
|
password: 'pass100'
|
|
|
|
};
|
|
|
|
|
|
|
|
var realm2User = {
|
|
|
|
realm: 'realm2',
|
|
|
|
username: 'foo100',
|
|
|
|
email: 'foo100@bar.com',
|
|
|
|
password: 'pass200'
|
|
|
|
};
|
|
|
|
|
|
|
|
var credentialWithoutRealm = {
|
|
|
|
username: 'foo100',
|
|
|
|
email: 'foo100@bar.com',
|
|
|
|
password: 'pass100'
|
|
|
|
};
|
|
|
|
|
|
|
|
var credentialWithBadPass = {
|
|
|
|
realm: 'realm1',
|
|
|
|
username: 'foo100',
|
|
|
|
email: 'foo100@bar.com',
|
|
|
|
password: 'pass001'
|
|
|
|
};
|
|
|
|
|
|
|
|
var credentialWithBadRealm = {
|
|
|
|
realm: 'realm3',
|
|
|
|
username: 'foo100',
|
|
|
|
email: 'foo100@bar.com',
|
|
|
|
password: 'pass100'
|
|
|
|
};
|
|
|
|
|
|
|
|
var credentialWithRealm = {
|
|
|
|
realm: 'realm1',
|
|
|
|
username: 'foo100',
|
|
|
|
password: 'pass100'
|
|
|
|
};
|
|
|
|
|
|
|
|
var credentialRealmInUsername = {
|
|
|
|
username: 'realm1:foo100',
|
|
|
|
password: 'pass100'
|
|
|
|
};
|
|
|
|
|
|
|
|
var credentialRealmInEmail = {
|
|
|
|
email: 'realm1:foo100@bar.com',
|
|
|
|
password: 'pass100'
|
|
|
|
};
|
|
|
|
|
|
|
|
var user1;
|
|
|
|
beforeEach(function(done) {
|
|
|
|
User.create(realm1User, function(err, u) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
user1 = u;
|
|
|
|
User.create(realm2User, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function(done) {
|
|
|
|
User.deleteAll({realm: 'realm1'}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
User.deleteAll({realm: 'realm2'}, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects a user by without realm', function(done) {
|
|
|
|
User.login(credentialWithoutRealm, function(err, accessToken) {
|
|
|
|
assert(err);
|
2014-12-18 20:26:27 +00:00
|
|
|
assert.equal(err.code, 'REALM_REQUIRED');
|
2014-10-23 18:10:39 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects a user by with bad realm', function(done) {
|
|
|
|
User.login(credentialWithBadRealm, function(err, accessToken) {
|
|
|
|
assert(err);
|
2014-12-18 20:26:27 +00:00
|
|
|
assert.equal(err.code, 'LOGIN_FAILED');
|
2014-10-23 18:10:39 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects a user by with bad pass', function(done) {
|
|
|
|
User.login(credentialWithBadPass, function(err, accessToken) {
|
|
|
|
assert(err);
|
2014-12-18 20:26:27 +00:00
|
|
|
assert.equal(err.code, 'LOGIN_FAILED');
|
2014-10-23 18:10:39 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('logs in a user by with realm', function(done) {
|
|
|
|
User.login(credentialWithRealm, function(err, accessToken) {
|
|
|
|
assertGoodToken(accessToken);
|
|
|
|
assert.equal(accessToken.userId, user1.id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('logs in a user by with realm in username', function(done) {
|
|
|
|
User.login(credentialRealmInUsername, function(err, accessToken) {
|
|
|
|
assertGoodToken(accessToken);
|
|
|
|
assert.equal(accessToken.userId, user1.id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('logs in a user by with realm in email', function(done) {
|
|
|
|
User.login(credentialRealmInEmail, function(err, accessToken) {
|
|
|
|
assertGoodToken(accessToken);
|
|
|
|
assert.equal(accessToken.userId, user1.id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('User.login with realmRequired but no realmDelimiter', function() {
|
|
|
|
before(function() {
|
|
|
|
User.settings.realmDelimiter = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
|
|
|
User.settings.realmDelimiter = ':';
|
|
|
|
});
|
|
|
|
|
|
|
|
it('logs in a user by with realm', function(done) {
|
|
|
|
User.login(credentialWithRealm, function(err, accessToken) {
|
|
|
|
assertGoodToken(accessToken);
|
|
|
|
assert.equal(accessToken.userId, user1.id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects a user by with realm in email if realmDelimiter is not set',
|
|
|
|
function(done) {
|
|
|
|
User.login(credentialRealmInEmail, function(err, accessToken) {
|
|
|
|
assert(err);
|
2014-12-18 20:26:27 +00:00
|
|
|
assert.equal(err.code, 'REALM_REQUIRED');
|
2014-10-23 18:10:39 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-03 05:37:31 +00:00
|
|
|
describe('User.logout', function() {
|
2013-11-13 19:49:08 +00:00
|
|
|
it('Logout a user by providing the current accessToken id (using node)', function(done) {
|
2013-07-12 23:10:15 +00:00
|
|
|
login(logout);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-12 23:10:15 +00:00
|
|
|
function login(fn) {
|
|
|
|
User.login({email: 'foo@bar.com', password: 'bar'}, fn);
|
|
|
|
}
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-11-13 19:49:08 +00:00
|
|
|
function logout(err, accessToken) {
|
|
|
|
User.logout(accessToken.id, verify(accessToken.id, done));
|
2013-07-12 23:10:15 +00:00
|
|
|
}
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
it('Logout a user by providing the current accessToken id (using node) - promise variant', function(done) {
|
|
|
|
login(logout);
|
|
|
|
|
|
|
|
function login(fn) {
|
|
|
|
User.login({email: 'foo@bar.com', password: 'bar'}, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
function logout(err, accessToken) {
|
|
|
|
User.logout(accessToken.id)
|
|
|
|
.then(function() {
|
|
|
|
verify(accessToken.id, done);
|
|
|
|
})
|
|
|
|
.catch(done(err));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-11-13 19:49:08 +00:00
|
|
|
it('Logout a user by providing the current accessToken id (over rest)', function(done) {
|
2013-07-03 05:37:31 +00:00
|
|
|
login(logout);
|
|
|
|
function login(fn) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/login')
|
2013-07-03 05:37:31 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({email: 'foo@bar.com', password: 'bar'})
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-12-22 20:48:12 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2013-11-13 19:49:08 +00:00
|
|
|
var accessToken = res.body;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-11-15 02:34:51 +00:00
|
|
|
assert(accessToken.userId);
|
2013-11-13 19:49:08 +00:00
|
|
|
assert(accessToken.id);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-11-13 19:49:08 +00:00
|
|
|
fn(null, accessToken.id);
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
|
|
|
}
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-12-18 05:34:30 +00:00
|
|
|
function logout(err, token) {
|
2013-07-03 05:37:31 +00:00
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/logout')
|
2013-12-18 05:34:30 +00:00
|
|
|
.set('Authorization', token)
|
2013-07-25 00:21:15 +00:00
|
|
|
.expect(204)
|
2013-12-18 05:34:30 +00:00
|
|
|
.end(verify(token, done));
|
2013-07-03 05:37:31 +00:00
|
|
|
}
|
2013-07-12 23:10:15 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-12-18 05:34:30 +00:00
|
|
|
function verify(token, done) {
|
|
|
|
assert(token);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
return function(err) {
|
2014-12-22 22:24:30 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
AccessToken.findById(token, function(err, accessToken) {
|
2013-11-13 19:49:08 +00:00
|
|
|
assert(!accessToken, 'accessToken should not exist after logging out');
|
2013-07-12 23:10:15 +00:00
|
|
|
done(err);
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
};
|
2013-07-12 23:10:15 +00:00
|
|
|
}
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('user.hasPassword(plain, fn)', function() {
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Determine if the password matches the stored password', function(done) {
|
2013-07-03 05:37:31 +00:00
|
|
|
var u = new User({username: 'foo', password: 'bar'});
|
2014-11-21 02:35:36 +00:00
|
|
|
u.hasPassword('bar', function(err, isMatch) {
|
2013-07-03 05:37:31 +00:00
|
|
|
assert(isMatch, 'password doesnt match');
|
|
|
|
done();
|
2014-11-21 01:46:21 +00:00
|
|
|
});
|
2013-07-15 21:07:17 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
it('Determine if the password matches the stored password - promise variant', function(done) {
|
|
|
|
var u = new User({username: 'foo', password: 'bar'});
|
|
|
|
u.hasPassword('bar')
|
|
|
|
.then(function(isMatch) {
|
|
|
|
assert(isMatch, 'password doesnt match');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-07-16 01:22:33 +00:00
|
|
|
it('should match a password when saved', function(done) {
|
|
|
|
var u = new User({username: 'a', password: 'b', email: 'z@z.net'});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
u.save(function(err, user) {
|
|
|
|
User.findById(user.id, function(err, uu) {
|
|
|
|
uu.hasPassword('b', function(err, isMatch) {
|
2013-07-16 01:22:33 +00:00
|
|
|
assert(isMatch);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-15 21:07:17 +00:00
|
|
|
it('should match a password after it is changed', function(done) {
|
2014-11-21 02:35:36 +00:00
|
|
|
User.create({email: 'foo@baz.net', username: 'bat', password: 'baz'}, function(err, user) {
|
|
|
|
User.findById(user.id, function(err, foundUser) {
|
|
|
|
assert(foundUser);
|
|
|
|
foundUser.hasPassword('baz', function(err, isMatch) {
|
|
|
|
assert(isMatch);
|
|
|
|
foundUser.password = 'baz2';
|
|
|
|
foundUser.save(function(err, updatedUser) {
|
|
|
|
updatedUser.hasPassword('baz2', function(err, isMatch) {
|
|
|
|
assert(isMatch);
|
|
|
|
User.findById(user.id, function(err, uu) {
|
|
|
|
uu.hasPassword('baz2', function(err, isMatch) {
|
|
|
|
assert(isMatch);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Verification', function() {
|
2014-07-17 05:42:05 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('user.verify(options, fn)', function() {
|
2013-07-03 05:37:31 +00:00
|
|
|
it('Verify a user\'s email address', function(done) {
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-03 05:37:31 +00:00
|
|
|
var options = {
|
|
|
|
type: 'email',
|
|
|
|
to: user.email,
|
|
|
|
from: 'noreply@myapp.org',
|
|
|
|
redirect: '/',
|
|
|
|
protocol: ctx.req.protocol,
|
|
|
|
host: ctx.req.get('host')
|
|
|
|
};
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
user.verify(options, function(err, result) {
|
2013-07-03 05:37:31 +00:00
|
|
|
assert(result.email);
|
2014-07-15 23:46:43 +00:00
|
|
|
assert(result.email.response);
|
2013-07-03 05:37:31 +00:00
|
|
|
assert(result.token);
|
2014-07-27 05:39:42 +00:00
|
|
|
var msg = result.email.response.toString('utf-8');
|
2015-04-20 16:23:44 +00:00
|
|
|
assert(~msg.indexOf('/api/test-users/confirm'));
|
2014-07-27 05:39:42 +00:00
|
|
|
assert(~msg.indexOf('To: bar@bat.com'));
|
2013-07-03 05:37:31 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-03 05:37:31 +00:00
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users')
|
2013-07-03 05:37:31 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2013-07-15 17:56:42 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-12-22 20:48:12 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
|
|
|
});
|
2014-10-24 05:46:23 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
it('Verify a user\'s email address - promise variant', function(done) {
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
type: 'email',
|
|
|
|
to: user.email,
|
|
|
|
from: 'noreply@myapp.org',
|
|
|
|
redirect: '/',
|
|
|
|
protocol: ctx.req.protocol,
|
|
|
|
host: ctx.req.get('host')
|
|
|
|
};
|
|
|
|
|
|
|
|
user.verify(options)
|
|
|
|
.then(function(result) {
|
|
|
|
console.log('here in then function');
|
|
|
|
assert(result.email);
|
|
|
|
assert(result.email.response);
|
|
|
|
assert(result.token);
|
|
|
|
var msg = result.email.response.toString('utf-8');
|
|
|
|
assert(~msg.indexOf('/api/test-users/confirm'));
|
|
|
|
assert(~msg.indexOf('To: bar@bat.com'));
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-24 05:46:23 +00:00
|
|
|
it('Verify a user\'s email address with custom header', function(done) {
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-10-24 05:46:23 +00:00
|
|
|
var options = {
|
|
|
|
type: 'email',
|
|
|
|
to: user.email,
|
|
|
|
from: 'noreply@myapp.org',
|
|
|
|
redirect: '/',
|
|
|
|
protocol: ctx.req.protocol,
|
|
|
|
host: ctx.req.get('host'),
|
|
|
|
headers: {'message-id':'custom-header-value'}
|
|
|
|
};
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
user.verify(options, function(err, result) {
|
2014-10-24 05:46:23 +00:00
|
|
|
assert(result.email);
|
|
|
|
assert.equal(result.email.messageId, 'custom-header-value');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-10-24 05:46:23 +00:00
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users')
|
2014-10-24 05:46:23 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-12-22 20:48:12 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-10-24 05:46:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-12 14:56:43 +00:00
|
|
|
it('Verify a user\'s email address with custom token generator', function(done) {
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
type: 'email',
|
|
|
|
to: user.email,
|
|
|
|
from: 'noreply@myapp.org',
|
|
|
|
redirect: '/',
|
|
|
|
protocol: ctx.req.protocol,
|
|
|
|
host: ctx.req.get('host'),
|
|
|
|
generateVerificationToken: function(user, cb) {
|
|
|
|
assert(user);
|
|
|
|
assert.equal(user.email, 'bar@bat.com');
|
|
|
|
assert(cb);
|
|
|
|
assert.equal(typeof cb, 'function');
|
|
|
|
// let's ensure async execution works on this one
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(null, 'token-123456');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
user.verify(options, function(err, result) {
|
|
|
|
assert(result.email);
|
|
|
|
assert(result.email.response);
|
|
|
|
assert(result.token);
|
|
|
|
assert.equal(result.token, 'token-123456');
|
|
|
|
var msg = result.email.response.toString('utf-8');
|
|
|
|
assert(~msg.indexOf('token-123456'));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users')
|
2015-03-12 14:56:43 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Fails if custom token generator returns error', function(done) {
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
type: 'email',
|
|
|
|
to: user.email,
|
|
|
|
from: 'noreply@myapp.org',
|
|
|
|
redirect: '/',
|
|
|
|
protocol: ctx.req.protocol,
|
|
|
|
host: ctx.req.get('host'),
|
|
|
|
generateVerificationToken: function(user, cb) {
|
|
|
|
// let's ensure async execution works on this one
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(new Error('Fake error'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
user.verify(options, function(err, result) {
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.message, 'Fake error');
|
|
|
|
assert.equal(result, undefined);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users')
|
2015-03-12 14:56:43 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-18 09:51:17 +00:00
|
|
|
describe('Verification link port-squashing', function() {
|
|
|
|
it('Do not squash non-80 ports for HTTP links', function(done) {
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
type: 'email',
|
|
|
|
to: user.email,
|
|
|
|
from: 'noreply@myapp.org',
|
|
|
|
redirect: '/',
|
|
|
|
protocol: 'http',
|
|
|
|
host: 'myapp.org',
|
|
|
|
port: 3000
|
|
|
|
};
|
|
|
|
|
|
|
|
user.verify(options, function(err, result) {
|
|
|
|
var msg = result.email.response.toString('utf-8');
|
|
|
|
assert(~msg.indexOf('http://myapp.org:3000/'));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Squash port 80 for HTTP links', function(done) {
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
type: 'email',
|
|
|
|
to: user.email,
|
|
|
|
from: 'noreply@myapp.org',
|
|
|
|
redirect: '/',
|
|
|
|
protocol: 'http',
|
|
|
|
host: 'myapp.org',
|
|
|
|
port: 80
|
|
|
|
};
|
|
|
|
|
|
|
|
user.verify(options, function(err, result) {
|
|
|
|
var msg = result.email.response.toString('utf-8');
|
|
|
|
assert(~msg.indexOf('http://myapp.org/'));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Do not squash non-443 ports for HTTPS links', function(done) {
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
type: 'email',
|
|
|
|
to: user.email,
|
|
|
|
from: 'noreply@myapp.org',
|
|
|
|
redirect: '/',
|
|
|
|
protocol: 'https',
|
|
|
|
host: 'myapp.org',
|
|
|
|
port: 3000
|
|
|
|
};
|
|
|
|
|
|
|
|
user.verify(options, function(err, result) {
|
|
|
|
var msg = result.email.response.toString('utf-8');
|
|
|
|
assert(~msg.indexOf('https://myapp.org:3000/'));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Squash port 443 for HTTPS links', function(done) {
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
type: 'email',
|
|
|
|
to: user.email,
|
|
|
|
from: 'noreply@myapp.org',
|
|
|
|
redirect: '/',
|
|
|
|
protocol: 'https',
|
|
|
|
host: 'myapp.org',
|
|
|
|
port: 443
|
|
|
|
};
|
|
|
|
|
|
|
|
user.verify(options, function(err, result) {
|
|
|
|
var msg = result.email.response.toString('utf-8');
|
|
|
|
assert(~msg.indexOf('https://myapp.org/'));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
2014-07-17 05:42:05 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('User.confirm(options, fn)', function() {
|
2014-07-17 05:42:05 +00:00
|
|
|
var options;
|
|
|
|
|
|
|
|
function testConfirm(testFunc, done) {
|
2014-11-21 02:35:36 +00:00
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
2013-07-03 05:37:31 +00:00
|
|
|
assert(user, 'afterRemote should include result');
|
2014-07-17 05:42:05 +00:00
|
|
|
|
|
|
|
options = {
|
2013-07-03 05:37:31 +00:00
|
|
|
type: 'email',
|
|
|
|
to: user.email,
|
|
|
|
from: 'noreply@myapp.org',
|
|
|
|
redirect: 'http://foo.com/bar',
|
|
|
|
protocol: ctx.req.protocol,
|
|
|
|
host: ctx.req.get('host')
|
|
|
|
};
|
2014-07-17 05:42:05 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
user.verify(options, function(err, result) {
|
2014-07-17 05:42:05 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
testFunc(result, done);
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
|
|
|
});
|
2014-07-17 05:42:05 +00:00
|
|
|
|
2013-07-03 05:37:31 +00:00
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users')
|
2013-07-03 05:37:31 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(302)
|
2013-07-15 17:56:42 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-07-17 05:42:05 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
2014-07-17 05:42:05 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Confirm a user verification', function(done) {
|
|
|
|
testConfirm(function(result, done) {
|
2014-07-17 05:42:05 +00:00
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.get('/test-users/confirm?uid=' + (result.uid) +
|
2015-02-20 14:31:15 +00:00
|
|
|
'&token=' + encodeURIComponent(result.token) +
|
|
|
|
'&redirect=' + encodeURIComponent(options.redirect))
|
2014-07-17 05:42:05 +00:00
|
|
|
.expect(302)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-07-17 05:42:05 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
2015-02-23 09:55:25 +00:00
|
|
|
it('Should report 302 when redirect url is set', function(done) {
|
|
|
|
testConfirm(function(result, done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.get('/test-users/confirm?uid=' + (result.uid) +
|
2015-02-23 09:55:25 +00:00
|
|
|
'&token=' + encodeURIComponent(result.token) +
|
|
|
|
'&redirect=http://foo.com/bar')
|
|
|
|
.expect(302)
|
|
|
|
.expect('Location', 'http://foo.com/bar')
|
|
|
|
.end(done);
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should report 204 when redirect url is not set', function(done) {
|
|
|
|
testConfirm(function(result, done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.get('/test-users/confirm?uid=' + (result.uid) +
|
2015-02-23 09:55:25 +00:00
|
|
|
'&token=' + encodeURIComponent(result.token))
|
|
|
|
.expect(204)
|
|
|
|
.end(done);
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Report error for invalid user id during verification', function(done) {
|
|
|
|
testConfirm(function(result, done) {
|
2014-07-17 05:42:05 +00:00
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.get('/test-users/confirm?uid=' + (result.uid + '_invalid') +
|
2015-02-20 14:31:15 +00:00
|
|
|
'&token=' + encodeURIComponent(result.token) +
|
|
|
|
'&redirect=' + encodeURIComponent(options.redirect))
|
2014-07-17 05:42:05 +00:00
|
|
|
.expect(404)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-07-17 05:42:05 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-12-18 20:26:27 +00:00
|
|
|
var errorResponse = res.body.error;
|
|
|
|
assert(errorResponse);
|
|
|
|
assert.equal(errorResponse.code, 'USER_NOT_FOUND');
|
2014-07-17 05:42:05 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Report error for invalid token during verification', function(done) {
|
|
|
|
testConfirm(function(result, done) {
|
2014-07-17 05:42:05 +00:00
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.get('/test-users/confirm?uid=' + result.uid +
|
2015-02-20 14:31:15 +00:00
|
|
|
'&token=' + encodeURIComponent(result.token) + '_invalid' +
|
|
|
|
'&redirect=' + encodeURIComponent(options.redirect))
|
2014-07-17 05:42:05 +00:00
|
|
|
.expect(400)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2014-12-22 20:48:12 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-12-18 20:26:27 +00:00
|
|
|
var errorResponse = res.body.error;
|
|
|
|
assert(errorResponse);
|
|
|
|
assert.equal(errorResponse.code, 'INVALID_TOKEN');
|
2014-07-17 05:42:05 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}, done);
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-11-20 18:59:29 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Password Reset', function() {
|
|
|
|
describe('User.resetPassword(options, cb)', function() {
|
2014-12-23 03:12:50 +00:00
|
|
|
var email = 'foo@bar.com';
|
|
|
|
|
|
|
|
it('Requires email address to reset password', function(done) {
|
|
|
|
User.resetPassword({ }, function(err) {
|
|
|
|
assert(err);
|
2014-12-18 20:26:27 +00:00
|
|
|
assert.equal(err.code, 'EMAIL_REQUIRED');
|
2014-12-23 03:12:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
it('Requires email address to reset password - promise variant', function(done) {
|
|
|
|
User.resetPassword({ })
|
|
|
|
.then(function() {
|
|
|
|
throw new Error('Error should NOT be thrown');
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'EMAIL_REQUIRED');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-10-30 21:59:31 +00:00
|
|
|
it('Reports when email is not found', function(done) {
|
|
|
|
User.resetPassword({ email: 'unknown@email.com' }, function(err) {
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'EMAIL_NOT_FOUND');
|
|
|
|
assert.equal(err.statusCode, 404);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Creates a temp accessToken to allow a user to change password', function(done) {
|
2013-11-20 18:59:29 +00:00
|
|
|
var calledBack = false;
|
|
|
|
|
|
|
|
User.resetPassword({
|
|
|
|
email: email
|
2014-11-21 02:35:36 +00:00
|
|
|
}, function() {
|
2013-11-20 18:59:29 +00:00
|
|
|
calledBack = true;
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
User.once('resetPasswordRequest', function(info) {
|
2013-11-20 18:59:29 +00:00
|
|
|
assert(info.email);
|
|
|
|
assert(info.accessToken);
|
|
|
|
assert(info.accessToken.id);
|
|
|
|
assert.equal(info.accessToken.ttl / 60, 15);
|
|
|
|
assert(calledBack);
|
2014-11-21 02:35:36 +00:00
|
|
|
info.accessToken.user(function(err, user) {
|
2015-04-07 11:43:26 +00:00
|
|
|
if (err) return done(err);
|
2013-11-20 18:59:29 +00:00
|
|
|
assert.equal(user.email, email);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-12-23 03:12:50 +00:00
|
|
|
|
|
|
|
it('Password reset over REST rejected without email address', function(done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/reset')
|
2014-12-23 03:12:50 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(400)
|
|
|
|
.send({ })
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2014-12-18 20:26:27 +00:00
|
|
|
var errorResponse = res.body.error;
|
|
|
|
assert(errorResponse);
|
|
|
|
assert.equal(errorResponse.code, 'EMAIL_REQUIRED');
|
2014-12-23 03:12:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Password reset over REST requires email address', function(done) {
|
|
|
|
request(app)
|
2015-04-20 16:23:44 +00:00
|
|
|
.post('/test-users/reset')
|
2014-12-23 03:12:50 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(204)
|
|
|
|
.send({ email: email })
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
assert.deepEqual(res.body, { });
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-11-20 18:59:29 +00:00
|
|
|
});
|
|
|
|
});
|
2014-10-10 09:21:15 +00:00
|
|
|
|
|
|
|
describe('ctor', function() {
|
|
|
|
it('exports default Email model', function() {
|
|
|
|
expect(User.email, 'User.email').to.be.a('function');
|
|
|
|
expect(User.email.modelName, 'modelName').to.eql('email');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('exports default AccessToken model', function() {
|
|
|
|
expect(User.accessToken, 'User.accessToken').to.be.a('function');
|
|
|
|
expect(User.accessToken.modelName, 'modelName').to.eql('AccessToken');
|
|
|
|
});
|
|
|
|
});
|
2014-12-28 08:02:37 +00:00
|
|
|
|
|
|
|
describe('ttl', function() {
|
|
|
|
var User2;
|
|
|
|
beforeEach(function() {
|
|
|
|
User2 = loopback.User.extend('User2', {}, { ttl: 10 });
|
|
|
|
});
|
|
|
|
it('should override ttl setting in based User model', function() {
|
|
|
|
expect(User2.settings.ttl).to.equal(10);
|
|
|
|
});
|
|
|
|
});
|
2013-10-11 20:37:51 +00:00
|
|
|
});
|