2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
|
|
|
var assert = require('assert');
|
2016-11-22 14:30:04 +00:00
|
|
|
var expect = require('./helpers/expect');
|
2016-11-15 21:46:23 +00:00
|
|
|
var request = require('supertest');
|
2015-04-20 16:23:44 +00:00
|
|
|
var loopback = require('../');
|
2016-07-07 15:30:57 +00:00
|
|
|
var async = require('async');
|
2017-02-06 11:39:21 +00:00
|
|
|
var url = require('url');
|
2017-03-24 13:57:41 +00:00
|
|
|
var extend = require('util')._extend;
|
2017-04-10 08:36:49 +00:00
|
|
|
const Promise = require('bluebird');
|
|
|
|
const waitForEvent = require('./helpers/wait-for-event');
|
2017-04-07 18:31:11 +00:00
|
|
|
|
|
|
|
var User, AccessToken;
|
2013-07-15 17:56:42 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('User', function() {
|
2016-07-29 18:41:43 +00:00
|
|
|
this.timeout(10000);
|
|
|
|
|
2014-12-22 03:18:36 +00:00
|
|
|
var validCredentialsEmail = 'foo@bar.com';
|
2016-11-15 21:46:23 +00:00
|
|
|
var validCredentials = {email: validCredentialsEmail, password: 'bar'};
|
2016-04-01 09:14:26 +00:00
|
|
|
var validCredentialsEmailVerified = {
|
2016-11-15 21:46:23 +00:00
|
|
|
email: 'foo1@bar.com', password: 'bar1', emailVerified: true};
|
2016-04-01 09:14:26 +00:00
|
|
|
var validCredentialsEmailVerifiedOverREST = {
|
2016-11-15 21:46:23 +00:00
|
|
|
email: 'foo2@bar.com', password: 'bar2', emailVerified: true};
|
2016-12-03 20:25:13 +00:00
|
|
|
var validCredentialsWithRealm = {
|
|
|
|
email: 'foo3@bar.com', password: 'bar', realm: 'foobar'};
|
2016-11-15 21:46:23 +00:00
|
|
|
var validCredentialsWithTTL = {email: 'foo@bar.com', password: 'bar', ttl: 3600};
|
2016-04-01 09:14:26 +00:00
|
|
|
var validCredentialsWithTTLAndScope = {
|
2016-11-15 21:46:23 +00:00
|
|
|
email: 'foo@bar.com', password: 'bar', ttl: 3600, scope: 'all'};
|
|
|
|
var validMixedCaseEmailCredentials = {email: 'Foo@bar.com', password: 'bar'};
|
|
|
|
var invalidCredentials = {email: 'foo1@bar.com', password: 'invalid'};
|
|
|
|
var incompleteCredentials = {password: 'bar1'};
|
2017-04-07 18:31:11 +00:00
|
|
|
var validCredentialsUser, validCredentialsEmailVerifiedUser, user;
|
2014-02-28 21:19:52 +00:00
|
|
|
|
2016-05-02 11:48:12 +00:00
|
|
|
// Create a local app variable to prevent clashes with the global
|
|
|
|
// variable shared by all tests. While this should not be necessary if
|
|
|
|
// the tests were written correctly, it turns out that's not the case :(
|
2016-11-21 20:51:43 +00:00
|
|
|
var app = null;
|
2016-05-02 11:48:12 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
beforeEach(function setupAppAndModels() {
|
2016-05-02 11:48:12 +00:00
|
|
|
// override the global app object provided by test/support.js
|
|
|
|
// and create a local one that does not share state with other tests
|
2016-11-15 21:46:23 +00:00
|
|
|
app = loopback({localRegistry: true, loadBuiltinModels: true});
|
|
|
|
app.set('remoting', {errorHandler: {debug: true, log: false}});
|
|
|
|
app.dataSource('db', {connector: 'memory'});
|
2016-05-02 11:48:12 +00:00
|
|
|
|
|
|
|
// setup Email model, it's needed by User tests
|
|
|
|
app.dataSource('email', {
|
|
|
|
connector: loopback.Mail,
|
2016-11-15 21:46:23 +00:00
|
|
|
transports: [{type: 'STUB'}],
|
2016-05-02 11:48:12 +00:00
|
|
|
});
|
|
|
|
var Email = app.registry.getModel('Email');
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(Email, {dataSource: 'email'});
|
2016-05-02 11:48:12 +00:00
|
|
|
|
|
|
|
// attach User and related models
|
2017-01-05 15:34:15 +00:00
|
|
|
User = app.registry.createModel({
|
|
|
|
name: 'TestUser',
|
2016-05-02 11:48:12 +00:00
|
|
|
base: 'User',
|
2017-01-05 15:34:15 +00:00
|
|
|
properties: {
|
|
|
|
// Use a custom id property to verify that User methods
|
|
|
|
// are correctly looking up the primary key
|
|
|
|
pk: {type: 'String', defaultFn: 'guid', id: true},
|
|
|
|
},
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: 'test-users'},
|
2017-01-05 15:34:15 +00:00
|
|
|
// forceId is set to false for the purpose of updating the same affected user within the
|
|
|
|
// `Email Update` test cases.
|
2016-08-30 19:09:11 +00:00
|
|
|
forceId: false,
|
2016-11-15 21:46:23 +00:00
|
|
|
// Speed up the password hashing algorithm for tests
|
|
|
|
saltWorkFactor: 4,
|
2016-05-02 11:48:12 +00:00
|
|
|
});
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(User, {dataSource: 'db'});
|
2015-04-20 16:23:44 +00:00
|
|
|
|
2016-05-02 11:48:12 +00:00
|
|
|
AccessToken = app.registry.getModel('AccessToken');
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(AccessToken, {dataSource: 'db'});
|
2016-05-02 11:48:12 +00:00
|
|
|
|
|
|
|
User.email = Email;
|
2014-02-14 18:31:30 +00:00
|
|
|
|
|
|
|
// Update the AccessToken relation to use the subclass of User
|
2016-11-15 21:46:23 +00:00
|
|
|
AccessToken.belongsTo(User, {as: 'user', foreignKey: 'userId'});
|
|
|
|
User.hasMany(AccessToken, {as: 'accessTokens', foreignKey: 'userId'});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2016-05-02 11:48:12 +00:00
|
|
|
// Speed up the password hashing algorithm
|
|
|
|
// for tests using the built-in User model
|
|
|
|
User.settings.saltWorkFactor = 4;
|
|
|
|
|
2014-01-23 22:39:15 +00:00
|
|
|
// allow many User.afterRemote's to be called
|
|
|
|
User.setMaxListeners(0);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
app.enableAuth({dataSource: 'db'});
|
|
|
|
app.use(loopback.token({model: AccessToken}));
|
2013-07-16 17:49:25 +00:00
|
|
|
app.use(loopback.rest());
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
// create 2 users: with and without verified email
|
|
|
|
return Promise.map(
|
|
|
|
[validCredentials, validCredentialsEmailVerified],
|
|
|
|
credentials => User.create(credentials)
|
|
|
|
).then(users => {
|
|
|
|
validCredentialsUser = user = users[0];
|
|
|
|
validCredentialsEmailVerifiedUser = users[1];
|
2014-07-07 21:09:45 +00:00
|
|
|
});
|
2013-07-02 23:51:38 +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) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'f@b.com', password: 'bar'}, function(err, user) {
|
2013-07-13 00:03:13 +00:00
|
|
|
assert(!err);
|
2017-01-05 15:34:15 +00:00
|
|
|
assert(user.pk);
|
2013-07-13 00:03:13 +00:00
|
|
|
assert(user.email);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-07-13 00:03:13 +00:00
|
|
|
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;
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'F@b.com', password: 'bar'}, function(err, user) {
|
2015-11-09 21:21:38 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-01-05 15:34:15 +00:00
|
|
|
assert(user.pk);
|
2015-11-09 21:21:38 +00:00
|
|
|
assert.equal(user.email, user.email.toLowerCase());
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Create a new user (email case-sensitive)', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'F@b.com', password: 'bar'}, function(err, user) {
|
2015-11-09 21:21:38 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-01-05 15:34:15 +00:00
|
|
|
assert(user.pk);
|
2015-11-09 21:21:38 +00:00
|
|
|
assert(user.email);
|
|
|
|
assert.notEqual(user.email, user.email.toLowerCase());
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Email is required', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
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);
|
2016-09-06 10:50:03 +00:00
|
|
|
assert.deepEqual(err.details.codes.email, ['presence']);
|
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) {
|
2016-11-15 21:46:23 +00:00
|
|
|
var u = new User({email: '123@456.com'});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'c@d.com'}, function(err) {
|
2013-07-28 20:20:55 +00:00
|
|
|
assert(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-07-28 20:20:55 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Requires a valid email', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'foo@', password: '123'}, function(err) {
|
2013-07-13 00:03:13 +00:00
|
|
|
assert(err);
|
2016-09-06 10:50:03 +00:00
|
|
|
assert.equal(err.name, 'ValidationError');
|
|
|
|
assert.equal(err.statusCode, 422);
|
|
|
|
assert.equal(err.details.context, User.modelName);
|
|
|
|
assert.deepEqual(err.details.codes.email, ['custom.email']);
|
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
|
|
|
|
2016-09-06 10:50:03 +00:00
|
|
|
it('allows TLD domains in email', function() {
|
|
|
|
return User.create({
|
|
|
|
email: 'local@com',
|
|
|
|
password: '123',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Requires a unique email', function(done) {
|
2016-11-15 21:46:23 +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!');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-07-13 00:03:13 +00:00
|
|
|
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;
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'A@b.com', password: 'foobar'}, function(err) {
|
2015-11-09 21:21:38 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'a@b.com', password: 'batbaz'}, function(err) {
|
2015-11-09 21:21:38 +00:00
|
|
|
assert(err, 'should error because the email is not unique!');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Requires a unique email (email case-sensitive)', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'A@b.com', password: 'foobar'}, function(err, user1) {
|
|
|
|
User.create({email: 'a@b.com', password: 'batbaz'}, function(err, user2) {
|
2015-11-09 21:21:38 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
assert.notEqual(user1.email, user2.email);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-19 12:18:21 +00:00
|
|
|
it('Requires a unique username', function(done) {
|
2016-11-15 21:46:23 +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!');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-19 12:18:21 +00:00
|
|
|
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) {
|
2016-11-15 21:46:23 +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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var u = new User({username: 'foo', password: 'bar'});
|
2013-07-16 01:22:33 +00:00
|
|
|
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() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var u1 = new User({username: 'foo', password: 'bar'});
|
2015-02-25 00:36:51 +00:00
|
|
|
assert(u1.password !== 'bar');
|
2016-11-15 21:46:23 +00:00
|
|
|
var u2 = new User({username: 'foo', password: u1.password});
|
2015-02-25 00:36:51 +00:00
|
|
|
assert(u2.password === u1.password);
|
|
|
|
});
|
|
|
|
|
2016-07-07 15:30:57 +00:00
|
|
|
it('invalidates the user\'s accessToken when the user is deleted By id', function(done) {
|
|
|
|
var usersId;
|
|
|
|
async.series([
|
|
|
|
function(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'b@c.com', password: 'bar'}, function(err, user) {
|
2017-01-05 15:34:15 +00:00
|
|
|
usersId = user.pk;
|
2016-07-07 15:30:57 +00:00
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.login({email: 'b@c.com', password: 'bar'}, function(err, accessToken) {
|
2016-07-07 15:30:57 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
assert(accessToken.userId);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
User.deleteById(usersId, function(err) {
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
User.findById(usersId, function(err, userFound) {
|
|
|
|
if (err) return next(err);
|
|
|
|
expect(userFound).to.equal(null);
|
2016-11-15 21:46:23 +00:00
|
|
|
AccessToken.find({where: {userId: usersId}}, function(err, tokens) {
|
2016-07-07 15:30:57 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
expect(tokens.length).to.equal(0);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invalidates the user\'s accessToken when the user is deleted all', function(done) {
|
2016-07-25 21:22:51 +00:00
|
|
|
var userIds = [];
|
2016-11-21 20:51:43 +00:00
|
|
|
var users;
|
2016-07-07 15:30:57 +00:00
|
|
|
async.series([
|
|
|
|
function(next) {
|
2016-07-25 21:22:51 +00:00
|
|
|
User.create([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'myname', email: 'b@c.com', password: 'bar'},
|
|
|
|
{name: 'myname', email: 'd@c.com', password: 'bar'},
|
2016-11-21 20:51:43 +00:00
|
|
|
], function(err, createdUsers) {
|
|
|
|
users = createdUsers;
|
|
|
|
userIds = createdUsers.map(function(u) {
|
2017-01-05 15:34:15 +00:00
|
|
|
return u.pk;
|
2016-07-25 21:22:51 +00:00
|
|
|
});
|
2016-07-07 15:30:57 +00:00
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.login({email: 'b@c.com', password: 'bar'}, function(err, accessToken) {
|
2016-07-25 21:22:51 +00:00
|
|
|
if (err) return next(err);
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, users[0]);
|
2016-07-07 15:30:57 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.login({email: 'd@c.com', password: 'bar'}, function(err, accessToken) {
|
2016-07-25 21:22:51 +00:00
|
|
|
if (err) return next(err);
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, users[1]);
|
2016-07-07 15:30:57 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.deleteAll({name: 'myname'}, function(err, user) {
|
2016-07-07 15:30:57 +00:00
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.find({where: {name: 'myname'}}, function(err, userFound) {
|
2016-07-25 21:22:51 +00:00
|
|
|
if (err) return next(err);
|
2016-07-07 15:30:57 +00:00
|
|
|
expect(userFound.length).to.equal(0);
|
2016-11-15 21:46:23 +00:00
|
|
|
AccessToken.find({where: {userId: {inq: userIds}}}, function(err, tokens) {
|
2016-07-07 15:30:57 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
expect(tokens.length).to.equal(0);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-20 23:57:38 +00:00
|
|
|
describe('custom password hash', function() {
|
2016-04-01 09:14:26 +00:00
|
|
|
var defaultHashPassword, defaultValidatePassword;
|
2014-12-20 23:57:38 +00:00
|
|
|
|
|
|
|
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 {
|
2016-11-15 21:46:23 +00:00
|
|
|
var u = new User({username: 'foo', password: 'aa'});
|
2014-12-20 23:57:38 +00:00
|
|
|
assert(false, 'Error should have been thrown');
|
|
|
|
} catch (e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Hashes the given password', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var u = new User({username: 'foo', password: 'bar'});
|
2014-12-20 23:57:38 +00:00
|
|
|
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) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2014-07-07 21:09:45 +00:00
|
|
|
assert(!res.body.emailVerified);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-07 21:09:45 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2016-08-03 23:01:33 +00:00
|
|
|
describe('Password length validation', function() {
|
|
|
|
var pass72Char = new Array(70).join('a') + '012';
|
|
|
|
var pass73Char = pass72Char + '3';
|
|
|
|
var passTooLong = pass72Char + 'WXYZ1234';
|
|
|
|
|
|
|
|
it('rejects passwords longer than 72 characters', function(done) {
|
|
|
|
try {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'b@c.com', password: pass73Char}, function(err) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return done(err);
|
2016-08-03 23:01:33 +00:00
|
|
|
done(new Error('User.create() should have thrown an error.'));
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
expect(e).to.match(/Password too long/);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects a new user with password longer than 72 characters', function(done) {
|
|
|
|
try {
|
2016-11-15 21:46:23 +00:00
|
|
|
var u = new User({username: 'foo', password: pass73Char});
|
2016-08-03 23:01:33 +00:00
|
|
|
assert(false, 'Error should have been thrown');
|
|
|
|
} catch (e) {
|
|
|
|
expect(e).to.match(/Password too long/);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('accepts passwords that are exactly 72 characters long', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'b@c.com', password: pass72Char}, function(err, user) {
|
2016-08-03 23:01:33 +00:00
|
|
|
if (err) return done(err);
|
2017-01-05 15:34:15 +00:00
|
|
|
User.findById(user.pk, function(err, userFound) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return done(err);
|
2016-08-03 23:01:33 +00:00
|
|
|
assert(userFound);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows login with password exactly 72 characters long', function(done) {
|
2016-11-21 20:51:43 +00:00
|
|
|
User.create({email: 'b@c.com', password: pass72Char}, function(err, user) {
|
2016-08-03 23:01:33 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
User.login({email: 'b@c.com', password: pass72Char}, function(err, accessToken) {
|
2016-08-03 23:01:33 +00:00
|
|
|
if (err) return done(err);
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, user);
|
2016-08-03 23:01:33 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects password reset when password is more than 72 chars', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'b@c.com', password: pass72Char}, function(err) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
User.resetPassword({email: 'b@c.com', password: pass73Char}, function(err) {
|
2016-08-03 23:01:33 +00:00
|
|
|
assert(err);
|
|
|
|
expect(err).to.match(/Password too long/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-03-27 09:26:48 +00:00
|
|
|
|
|
|
|
it('rejects changePassword when new password is longer than 72 chars', function() {
|
|
|
|
return User.create({email: 'test@example.com', password: pass72Char})
|
|
|
|
.then(u => u.changePassword(pass72Char, pass73Char))
|
|
|
|
.then(
|
|
|
|
success => { throw new Error('changePassword should have failed'); },
|
|
|
|
err => {
|
|
|
|
expect(err.message).to.match(/Password too long/);
|
|
|
|
|
|
|
|
// workaround for chai problem
|
|
|
|
// object tested must be an array, an object, or a string,
|
|
|
|
// but error given
|
|
|
|
const props = Object.assign({}, err);
|
|
|
|
expect(props).to.contain({
|
|
|
|
code: 'PASSWORD_TOO_LONG',
|
|
|
|
statusCode: 422,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-04-10 10:20:40 +00:00
|
|
|
|
|
|
|
it('rejects setPassword when new password is longer than 72 chars', function() {
|
|
|
|
return User.create({email: 'test@example.com', password: pass72Char})
|
|
|
|
.then(u => u.setPassword(pass73Char))
|
|
|
|
.then(
|
|
|
|
success => { throw new Error('setPassword should have failed'); },
|
|
|
|
err => {
|
|
|
|
expect(err.message).to.match(/Password too long/);
|
|
|
|
|
|
|
|
// workaround for chai problem
|
|
|
|
// object tested must be an array, an object, or a string,
|
|
|
|
// but error given
|
|
|
|
const props = Object.assign({}, err);
|
|
|
|
expect(props).to.contain({
|
|
|
|
code: 'PASSWORD_TOO_LONG',
|
|
|
|
statusCode: 422,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-08-03 23:01:33 +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);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should be able to find lowercase email with mixed-case email query', function(done) {
|
|
|
|
User.settings.caseSensitiveEmail = false;
|
2016-11-15 21:46:23 +00:00
|
|
|
User.find({where: {email: validMixedCaseEmailCredentials.email}}, function(err, result) {
|
2015-11-09 21:21:38 +00:00
|
|
|
if (err) done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
assert(result[0], 'The query did not find the user');
|
|
|
|
assert.equal(result[0].email, validCredentialsEmail);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-06-26 11:37:00 +00:00
|
|
|
|
|
|
|
it('Should be able to use query filters (email case-sensitivity off)', function(done) {
|
|
|
|
User.settings.caseSensitiveEmail = false;
|
2016-11-15 21:46:23 +00:00
|
|
|
var insensitiveUser = {email: 'insensitive@example.com', password: 'abc'};
|
2016-06-26 11:37:00 +00:00
|
|
|
User.create(insensitiveUser, function(err, user) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.find({where: {email:
|
|
|
|
{inq: [insensitiveUser.email]},
|
2016-06-26 11:37:00 +00:00
|
|
|
}}, function(err, result) {
|
|
|
|
if (err) done(err);
|
|
|
|
assert(result[0], 'The query did not find the user');
|
|
|
|
assert.equal(result[0].email, insensitiveUser.email);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should be able to use query filters (email case-sensitivity on)', function(done) {
|
|
|
|
User.settings.caseSensitiveEmail = true;
|
2016-11-15 21:46:23 +00:00
|
|
|
var sensitiveUser = {email: 'senSiTive@example.com', password: 'abc'};
|
2016-06-26 11:37:00 +00:00
|
|
|
User.create(sensitiveUser, function(err, user) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.find({where: {email:
|
|
|
|
{inq: [sensitiveUser.email]},
|
2016-06-26 11:37:00 +00:00
|
|
|
}}, function(err, result) {
|
|
|
|
if (err) done(err);
|
|
|
|
assert(result[0], 'The query did not find the user');
|
|
|
|
assert.equal(result[0].email, sensitiveUser.email);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-11-09 21:21:38 +00:00
|
|
|
});
|
|
|
|
|
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) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsUser);
|
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) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsUser);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Try to login with invalid email case', function(done) {
|
|
|
|
User.login(validMixedCaseEmailCredentials, function(err, accessToken) {
|
|
|
|
assert(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-11-09 21:21:38 +00:00
|
|
|
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) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsUser);
|
2014-06-06 21:30:18 +00:00
|
|
|
assert.equal(accessToken.ttl, validCredentialsWithTTL.ttl);
|
|
|
|
|
|
|
|
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) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsUser);
|
2015-07-01 11:43:25 +00:00
|
|
|
assert.equal(accessToken.ttl, 120);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
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) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsUser);
|
2015-07-01 11:43:25 +00:00
|
|
|
assert.equal(accessToken.ttl, 120);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
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
|
2016-11-15 21:46:23 +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) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsUser);
|
2014-06-06 21:30:18 +00:00
|
|
|
assert.equal(accessToken.ttl, 1800);
|
|
|
|
|
|
|
|
User.findById(accessToken.userId, function(err, user) {
|
2014-11-21 02:35:36 +00:00
|
|
|
user.createAccessToken(120, function(err, accessToken) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsUser);
|
2014-06-06 21:30:18 +00:00
|
|
|
assert.equal(accessToken.ttl, 60);
|
|
|
|
// Restore create access token
|
|
|
|
User.prototype.createAccessToken = createToken;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-06-06 21:30:18 +00:00
|
|
|
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
|
2017-03-27 12:58:01 +00:00
|
|
|
this.accessTokens.create({ttl: ttl / 2, scopes: [options.scope]}, cb);
|
2015-03-02 22:48:08 +00:00
|
|
|
};
|
|
|
|
User.login(validCredentialsWithTTLAndScope, function(err, accessToken) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsUser);
|
2015-03-02 22:48:08 +00:00
|
|
|
assert.equal(accessToken.ttl, 1800);
|
2017-03-27 12:58:01 +00:00
|
|
|
assert.deepEqual(accessToken.scopes, ['all']);
|
2015-03-02 22:48:08 +00:00
|
|
|
|
|
|
|
User.findById(accessToken.userId, function(err, user) {
|
2016-11-15 21:46:23 +00:00
|
|
|
user.createAccessToken(120, {scope: 'default'}, function(err, accessToken) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsUser);
|
2015-03-02 22:48:08 +00:00
|
|
|
assert.equal(accessToken.ttl, 60);
|
2017-03-27 12:58:01 +00:00
|
|
|
assert.deepEqual(accessToken.scopes, ['default']);
|
2015-03-02 22:48:08 +00:00
|
|
|
// Restore create access token
|
|
|
|
User.prototype.createAccessToken = createToken;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-02 22:48:08 +00:00
|
|
|
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);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-12-22 22:20:15 +00:00
|
|
|
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) {
|
2016-11-21 20:51:43 +00:00
|
|
|
expect(accessToken, 'accessToken').to.not.exist();
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
2016-11-21 20:51:43 +00:00
|
|
|
expect(err, 'err').to.exist();
|
|
|
|
expect(err).to.have.property('code', 'LOGIN_FAILED');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-18 20:26:27 +00:00
|
|
|
it('Login a user providing incomplete credentials', function(done) {
|
|
|
|
User.login(incompleteCredentials, function(err, accessToken) {
|
2016-11-21 20:51:43 +00:00
|
|
|
expect(err, 'err').to.exist();
|
|
|
|
expect(err).to.have.property('code', 'USERNAME_EMAIL_REQUIRED');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-12-18 20:26:27 +00:00
|
|
|
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) {
|
2016-11-21 20:51:43 +00:00
|
|
|
expect(accessToken, 'accessToken').to.not.exist();
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
2016-11-21 20:51:43 +00:00
|
|
|
expect(err, 'err').to.exist();
|
|
|
|
expect(err).to.have.property('code', 'USERNAME_EMAIL_REQUIRED');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
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) {
|
2016-05-05 04:09:06 +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
|
|
|
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsUser);
|
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) {
|
2016-05-05 04:09:06 +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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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) {
|
2016-05-05 04:09:06 +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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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) {
|
2016-05-05 04:09:06 +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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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) {
|
2016-05-05 04:09:06 +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);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-30 13:33:45 +00:00
|
|
|
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) {
|
2016-05-05 04:09:06 +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);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-09-03 05:58:49 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-08-03 23:01:33 +00:00
|
|
|
|
|
|
|
it('allows login with password too long but created in old LB version',
|
|
|
|
function(done) {
|
|
|
|
var bcrypt = require('bcryptjs');
|
|
|
|
var longPassword = new Array(80).join('a');
|
|
|
|
var oldHash = bcrypt.hashSync(longPassword, bcrypt.genSaltSync(1));
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'b@c.com', password: oldHash}, function(err) {
|
2016-08-03 23:01:33 +00:00
|
|
|
if (err) return done(err);
|
2016-11-15 21:46:23 +00:00
|
|
|
User.login({email: 'b@c.com', password: longPassword}, function(err, accessToken) {
|
2016-08-03 23:01:33 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
assert(accessToken.id);
|
|
|
|
// we are logged in, the test passed
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-07-02 23:51:38 +00:00
|
|
|
});
|
2014-07-08 15:54:50 +00:00
|
|
|
|
2016-11-21 20:51:43 +00:00
|
|
|
function assertGoodToken(accessToken, user) {
|
|
|
|
if (accessToken instanceof AccessToken) {
|
|
|
|
accessToken = accessToken.toJSON();
|
|
|
|
}
|
|
|
|
expect(accessToken).to.have.property('userId', user.pk);
|
2014-10-23 18:10:39 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('requires valid and complete credentials for email verification', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.login({email: validCredentialsEmail}, function(err, accessToken) {
|
2014-12-22 03:18:36 +00:00
|
|
|
// strongloop/loopback#931
|
2016-04-01 09:14:26 +00:00
|
|
|
// 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');
|
2017-03-28 09:10:51 +00:00
|
|
|
// as login is failing because of invalid credentials it should to return
|
|
|
|
// the user id in the error message
|
|
|
|
assert.equal(err.details, undefined);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-12-22 03:18:36 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('requires valid and complete credentials for email verification - promise variant',
|
2016-04-01 09:14:26 +00:00
|
|
|
function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.login({email: validCredentialsEmail})
|
2015-07-01 11:43:25 +00:00
|
|
|
.then(function(accessToken) {
|
2016-04-01 09:14:26 +00:00
|
|
|
done();
|
|
|
|
})
|
2015-07-01 11:43:25 +00:00
|
|
|
.catch(function(err) {
|
|
|
|
// strongloop/loopback#931
|
|
|
|
// error message should be "login failed" and not "login failed as the email has not been verified"
|
2016-04-01 09:14:26 +00:00
|
|
|
assert(err && !/verified/.test(err.message),
|
|
|
|
'expecting "login failed" error message, received: "' + err.message + '"');
|
2015-07-01 11:43:25 +00:00
|
|
|
assert.equal(err.code, 'LOGIN_FAILED');
|
2017-03-28 09:10:51 +00:00
|
|
|
assert.equal(err.details, undefined);
|
2014-07-08 15:54:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('does not login a user with unverified email but provides userId', function() {
|
|
|
|
return User.login(validCredentials).then(
|
|
|
|
function(user) {
|
|
|
|
throw new Error('User.login() should have failed');
|
|
|
|
},
|
|
|
|
function(err, accessToken) {
|
|
|
|
err = Object.assign({}, err);
|
|
|
|
expect(err).to.eql({
|
|
|
|
statusCode: 401,
|
|
|
|
code: 'LOGIN_FAILED_EMAIL_NOT_VERIFIED',
|
|
|
|
details: {
|
|
|
|
userId: validCredentialsUser.pk,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2015-07-01 11:43:25 +00:00
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('login a user with verified email', function(done) {
|
2014-11-21 02:35:36 +00:00
|
|
|
User.login(validCredentialsEmailVerified, function(err, accessToken) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsEmailVerifiedUser);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-08 15:54:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('login a user with verified email - promise variant', function(done) {
|
2015-07-01 11:43:25 +00:00
|
|
|
User.login(validCredentialsEmailVerified)
|
|
|
|
.then(function(accessToken) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsEmailVerifiedUser);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('login a user over REST when email verification is required', function(done) {
|
2014-07-08 15:54:50 +00:00
|
|
|
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) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2014-07-08 15:54:50 +00:00
|
|
|
var accessToken = res.body;
|
|
|
|
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsEmailVerifiedUser);
|
2014-07-08 15:54:50 +00:00
|
|
|
assert(accessToken.user === undefined);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('login user over REST require complete and valid credentials ' +
|
2016-04-01 09:14:26 +00:00
|
|
|
'for email verification error message',
|
|
|
|
function(done) {
|
2014-12-22 03:18:36 +00:00
|
|
|
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)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: validCredentialsEmail})
|
2014-12-22 03:18:36 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2014-12-22 03:18:36 +00:00
|
|
|
// strongloop/loopback#931
|
2016-04-01 09:14:26 +00:00
|
|
|
// error message should be "login failed"
|
|
|
|
// and not "login failed as the email has not been verified"
|
2014-12-22 03:18:36 +00:00
|
|
|
var errorResponse = res.body.error;
|
2016-04-01 09:14:26 +00:00
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-12-22 03:18:36 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('login a user over REST without email verification when it is required', function(done) {
|
|
|
|
// make sure the app is configured in production mode
|
|
|
|
app.set('remoting', {errorHandler: {debug: false, log: false}});
|
|
|
|
|
2014-07-08 15:54:50 +00:00
|
|
|
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) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2014-12-18 20:26:27 +00:00
|
|
|
var errorResponse = res.body.error;
|
2017-03-28 09:10:51 +00:00
|
|
|
|
|
|
|
// extracting code and details error response
|
|
|
|
let errorExcerpts = {
|
|
|
|
code: errorResponse.code,
|
|
|
|
details: errorResponse.details,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(errorExcerpts).to.eql({
|
|
|
|
code: 'LOGIN_FAILED_EMAIL_NOT_VERIFIED',
|
|
|
|
details: {
|
|
|
|
userId: validCredentialsUser.pk,
|
|
|
|
},
|
|
|
|
});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-08 15:54:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-10-23 18:10:39 +00:00
|
|
|
|
|
|
|
describe('User.login requiring realm', function() {
|
2016-04-01 09:14:26 +00:00
|
|
|
var User, AccessToken;
|
2014-10-23 18:10:39 +00:00
|
|
|
|
2016-05-02 11:48:12 +00:00
|
|
|
beforeEach(function() {
|
|
|
|
User = app.registry.createModel('RealmUser', {}, {
|
|
|
|
base: 'TestUser',
|
|
|
|
realmRequired: true,
|
|
|
|
realmDelimiter: ':',
|
|
|
|
});
|
|
|
|
|
|
|
|
AccessToken = app.registry.createModel('RealmAccessToken', {}, {
|
|
|
|
base: 'AccessToken',
|
|
|
|
});
|
2014-10-23 18:10:39 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(AccessToken, {dataSource: 'db'});
|
|
|
|
app.model(User, {dataSource: 'db'});
|
2014-10-23 18:10:39 +00:00
|
|
|
|
|
|
|
// Update the AccessToken relation to use the subclass of User
|
2016-11-15 21:46:23 +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',
|
2016-04-01 09:14:26 +00:00
|
|
|
password: 'pass100',
|
2014-10-23 18:10:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var realm2User = {
|
|
|
|
realm: 'realm2',
|
|
|
|
username: 'foo100',
|
|
|
|
email: 'foo100@bar.com',
|
2016-04-01 09:14:26 +00:00
|
|
|
password: 'pass200',
|
2014-10-23 18:10:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var credentialWithoutRealm = {
|
|
|
|
username: 'foo100',
|
|
|
|
email: 'foo100@bar.com',
|
2016-04-01 09:14:26 +00:00
|
|
|
password: 'pass100',
|
2014-10-23 18:10:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var credentialWithBadPass = {
|
|
|
|
realm: 'realm1',
|
|
|
|
username: 'foo100',
|
|
|
|
email: 'foo100@bar.com',
|
2016-04-01 09:14:26 +00:00
|
|
|
password: 'pass001',
|
2014-10-23 18:10:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var credentialWithBadRealm = {
|
|
|
|
realm: 'realm3',
|
|
|
|
username: 'foo100',
|
|
|
|
email: 'foo100@bar.com',
|
2016-04-01 09:14:26 +00:00
|
|
|
password: 'pass100',
|
2014-10-23 18:10:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var credentialWithRealm = {
|
|
|
|
realm: 'realm1',
|
|
|
|
username: 'foo100',
|
2016-04-01 09:14:26 +00:00
|
|
|
password: 'pass100',
|
2014-10-23 18:10:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var credentialRealmInUsername = {
|
|
|
|
username: 'realm1:foo100',
|
2016-04-01 09:14:26 +00:00
|
|
|
password: 'pass100',
|
2014-10-23 18:10:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var credentialRealmInEmail = {
|
|
|
|
email: 'realm1:foo100@bar.com',
|
2016-04-01 09:14:26 +00:00
|
|
|
password: 'pass100',
|
2014-10-23 18:10:39 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
var user1 = null;
|
2014-10-23 18:10:39 +00:00
|
|
|
beforeEach(function(done) {
|
|
|
|
User.create(realm1User, function(err, u) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2014-10-23 18:10:39 +00:00
|
|
|
user1 = u;
|
|
|
|
User.create(realm2User, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-18 21:52:04 +00:00
|
|
|
it('honors unique email for realm', function(done) {
|
|
|
|
User.create(realm1User, function(err, u) {
|
|
|
|
assert(err);
|
|
|
|
assert(err.message.match(/User already exists/) &&
|
|
|
|
err.message.match(/Email already exists/));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-23 18:10:39 +00:00
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, user1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-10-23 18:10:39 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('logs in a user by with realm in username', function(done) {
|
|
|
|
User.login(credentialRealmInUsername, function(err, accessToken) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, user1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-10-23 18:10:39 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('logs in a user by with realm in email', function(done) {
|
|
|
|
User.login(credentialRealmInEmail, function(err, accessToken) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, user1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-10-23 18:10:39 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('User.login with realmRequired but no realmDelimiter', function() {
|
2016-05-02 11:48:12 +00:00
|
|
|
beforeEach(function() {
|
2014-10-23 18:10:39 +00:00
|
|
|
User.settings.realmDelimiter = undefined;
|
|
|
|
});
|
|
|
|
|
2016-05-02 11:48:12 +00:00
|
|
|
afterEach(function() {
|
2014-10-23 18:10:39 +00:00
|
|
|
User.settings.realmDelimiter = ':';
|
|
|
|
});
|
|
|
|
|
|
|
|
it('logs in a user by with realm', function(done) {
|
|
|
|
User.login(credentialWithRealm, function(err, accessToken) {
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, user1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-10-23 18:10:39 +00:00
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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) {
|
2016-11-21 20:51:43 +00:00
|
|
|
User.login(validCredentials, fn);
|
2013-07-12 23:10:15 +00:00
|
|
|
}
|
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
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
it('Logout a user by providing the current accessToken id (using node) - promise variant',
|
|
|
|
function(done) {
|
2015-07-01 11:43:25 +00:00
|
|
|
login(logout);
|
|
|
|
|
|
|
|
function login(fn) {
|
2016-11-21 20:51:43 +00:00
|
|
|
User.login(validCredentials, fn);
|
2015-07-01 11:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2016-11-21 20:51:43 +00:00
|
|
|
.send(validCredentials)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2013-11-13 19:49:08 +00:00
|
|
|
var accessToken = res.body;
|
2016-11-21 20:51:43 +00:00
|
|
|
assertGoodToken(accessToken, validCredentialsUser);
|
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
|
|
|
|
2015-07-01 19:13:09 +00:00
|
|
|
it('fails when accessToken is not provided', function(done) {
|
|
|
|
User.logout(undefined, function(err) {
|
|
|
|
expect(err).to.have.property('message');
|
|
|
|
expect(err).to.have.property('status', 401);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails when accessToken is not found', function(done) {
|
|
|
|
User.logout('expired-access-token', function(err) {
|
|
|
|
expect(err).to.have.property('message');
|
|
|
|
expect(err).to.have.property('status', 401);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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) {
|
2016-05-05 04:09:06 +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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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) {
|
2016-11-15 21:46:23 +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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-07-03 05:37:31 +00:00
|
|
|
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) {
|
2016-11-15 21:46:23 +00:00
|
|
|
var u = new User({username: 'foo', password: 'bar'});
|
2015-07-01 11:43:25 +00:00
|
|
|
u.hasPassword('bar')
|
|
|
|
.then(function(isMatch) {
|
|
|
|
assert(isMatch, 'password doesnt match');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-07-16 01:22:33 +00:00
|
|
|
it('should match a password when saved', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
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) {
|
2017-01-05 15:34:15 +00:00
|
|
|
User.findById(user.pk, function(err, uu) {
|
2014-11-21 02:35:36 +00:00
|
|
|
uu.hasPassword('b', function(err, isMatch) {
|
2013-07-16 01:22:33 +00:00
|
|
|
assert(isMatch);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-07-16 01:22:33 +00:00
|
|
|
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) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.create({email: 'foo@baz.net', username: 'bat', password: 'baz'}, function(err, user) {
|
2017-01-05 15:34:15 +00:00
|
|
|
User.findById(user.pk, function(err, foundUser) {
|
2014-11-21 02:35:36 +00:00
|
|
|
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);
|
2017-01-05 15:34:15 +00:00
|
|
|
User.findById(user.pk, function(err, uu) {
|
2014-11-21 02:35:36 +00:00
|
|
|
uu.hasPassword('baz2', function(err, isMatch) {
|
|
|
|
assert(isMatch);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2017-03-22 14:33:32 +00:00
|
|
|
describe('User.changePassword()', () => {
|
|
|
|
let userId, currentPassword;
|
|
|
|
beforeEach(givenUserIdAndPassword);
|
|
|
|
|
|
|
|
it('changes the password - callback-style', done => {
|
|
|
|
User.changePassword(userId, currentPassword, 'new password', (err) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(arguments.length, 'changePassword callback arguments length')
|
|
|
|
.to.be.at.most(1);
|
|
|
|
|
|
|
|
User.findById(userId, (err, user) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
user.hasPassword('new password', (err, isMatch) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(isMatch, 'user has new password').to.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('changes the password - Promise-style', () => {
|
|
|
|
return User.changePassword(userId, currentPassword, 'new password')
|
|
|
|
.then(() => {
|
|
|
|
expect(arguments.length, 'changePassword promise resolution')
|
|
|
|
.to.equal(0);
|
|
|
|
return User.findById(userId);
|
|
|
|
})
|
|
|
|
.then(user => {
|
|
|
|
return user.hasPassword('new password');
|
|
|
|
})
|
|
|
|
.then(isMatch => {
|
|
|
|
expect(isMatch, 'user has new password').to.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('changes the password - instance method', () => {
|
|
|
|
validCredentialsUser.changePassword(currentPassword, 'new password')
|
|
|
|
.then(() => {
|
|
|
|
expect(arguments.length, 'changePassword promise resolution')
|
|
|
|
.to.equal(0);
|
|
|
|
return User.findById(userId);
|
|
|
|
})
|
|
|
|
.then(user => {
|
|
|
|
return user.hasPassword('new password');
|
|
|
|
})
|
|
|
|
.then(isMatch => {
|
|
|
|
expect(isMatch, 'user has new password').to.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails when current password does not match', () => {
|
|
|
|
return User.changePassword(userId, 'bad password', 'new password').then(
|
|
|
|
success => { throw new Error('changePassword should have failed'); },
|
|
|
|
err => {
|
|
|
|
// workaround for chai problem
|
|
|
|
// object tested must be an array, an object,
|
|
|
|
// or a string, but error given
|
|
|
|
const props = Object.assign({}, err);
|
|
|
|
expect(props).to.contain({
|
|
|
|
code: 'INVALID_PASSWORD',
|
|
|
|
statusCode: 400,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with 401 for unknown user id', () => {
|
|
|
|
return User.changePassword('unknown-id', 'pass', 'pass').then(
|
|
|
|
success => { throw new Error('changePassword should have failed'); },
|
|
|
|
err => {
|
|
|
|
// workaround for chai problem
|
|
|
|
// object tested must be an array, an object, or a string,
|
2017-03-27 09:26:48 +00:00
|
|
|
// but error given
|
2017-03-22 14:33:32 +00:00
|
|
|
const props = Object.assign({}, err);
|
|
|
|
expect(props).to.contain({
|
|
|
|
code: 'USER_NOT_FOUND',
|
|
|
|
statusCode: 401,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('forwards the "options" argument', () => {
|
|
|
|
const options = {testFlag: true};
|
|
|
|
const observedOptions = [];
|
|
|
|
|
|
|
|
saveObservedOptionsForHook('access');
|
|
|
|
saveObservedOptionsForHook('before save');
|
|
|
|
|
|
|
|
return User.changePassword(userId, currentPassword, 'new', options)
|
|
|
|
.then(() => expect(observedOptions).to.eql([
|
|
|
|
// findById
|
|
|
|
{hook: 'access', testFlag: true},
|
|
|
|
|
|
|
|
// "before save" hook prepareForTokenInvalidation
|
2017-04-10 12:07:41 +00:00
|
|
|
{hook: 'access', setPassword: true, testFlag: true},
|
2017-03-22 14:33:32 +00:00
|
|
|
|
|
|
|
// updateAttributes
|
2017-04-10 12:07:41 +00:00
|
|
|
{hook: 'before save', setPassword: true, testFlag: true},
|
2017-03-22 14:33:32 +00:00
|
|
|
|
|
|
|
// validate uniqueness of User.email
|
2017-04-10 12:07:41 +00:00
|
|
|
{hook: 'access', setPassword: true, testFlag: true},
|
2017-03-22 14:33:32 +00:00
|
|
|
]));
|
|
|
|
|
|
|
|
function saveObservedOptionsForHook(name) {
|
|
|
|
User.observe(name, (ctx, next) => {
|
|
|
|
observedOptions.push(Object.assign({hook: name}, ctx.options));
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function givenUserIdAndPassword() {
|
|
|
|
userId = validCredentialsUser.id;
|
|
|
|
currentPassword = validCredentials.password;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-10 10:20:40 +00:00
|
|
|
describe('User.setPassword()', () => {
|
|
|
|
let userId;
|
|
|
|
beforeEach(givenUserId);
|
|
|
|
|
|
|
|
it('changes the password - callback-style', done => {
|
|
|
|
User.setPassword(userId, 'new password', (err) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(arguments.length, 'changePassword callback arguments length')
|
|
|
|
.to.be.at.most(1);
|
|
|
|
|
|
|
|
User.findById(userId, (err, user) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
user.hasPassword('new password', (err, isMatch) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(isMatch, 'user has new password').to.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('changes the password - Promise-style', () => {
|
|
|
|
return User.setPassword(userId, 'new password')
|
|
|
|
.then(() => {
|
|
|
|
expect(arguments.length, 'changePassword promise resolution')
|
|
|
|
.to.equal(0);
|
|
|
|
return User.findById(userId);
|
|
|
|
})
|
|
|
|
.then(user => {
|
|
|
|
return user.hasPassword('new password');
|
|
|
|
})
|
|
|
|
.then(isMatch => {
|
|
|
|
expect(isMatch, 'user has new password').to.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-10 12:07:41 +00:00
|
|
|
it('fails with 401 for unknown users', () => {
|
2017-04-10 10:20:40 +00:00
|
|
|
return User.setPassword('unknown-id', 'pass').then(
|
|
|
|
success => { throw new Error('setPassword should have failed'); },
|
|
|
|
err => {
|
|
|
|
// workaround for chai problem
|
|
|
|
// object tested must be an array, an object, or a string,
|
|
|
|
// but error given
|
|
|
|
const props = Object.assign({}, err);
|
|
|
|
expect(props).to.contain({
|
|
|
|
code: 'USER_NOT_FOUND',
|
|
|
|
statusCode: 401,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('forwards the "options" argument', () => {
|
|
|
|
const options = {testFlag: true};
|
|
|
|
const observedOptions = [];
|
|
|
|
|
|
|
|
saveObservedOptionsForHook('access');
|
|
|
|
saveObservedOptionsForHook('before save');
|
|
|
|
|
|
|
|
return User.setPassword(userId, 'new', options)
|
|
|
|
.then(() => expect(observedOptions).to.eql([
|
|
|
|
// findById
|
|
|
|
{hook: 'access', testFlag: true},
|
|
|
|
|
|
|
|
// "before save" hook prepareForTokenInvalidation
|
2017-04-10 12:07:41 +00:00
|
|
|
{hook: 'access', setPassword: true, testFlag: true},
|
2017-04-10 10:20:40 +00:00
|
|
|
|
|
|
|
// updateAttributes
|
2017-04-10 12:07:41 +00:00
|
|
|
{hook: 'before save', setPassword: true, testFlag: true},
|
2017-04-10 10:20:40 +00:00
|
|
|
|
|
|
|
// validate uniqueness of User.email
|
2017-04-10 12:07:41 +00:00
|
|
|
{hook: 'access', setPassword: true, testFlag: true},
|
2017-04-10 10:20:40 +00:00
|
|
|
]));
|
|
|
|
|
|
|
|
function saveObservedOptionsForHook(name) {
|
|
|
|
User.observe(name, (ctx, next) => {
|
|
|
|
observedOptions.push(Object.assign({hook: name}, ctx.options));
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function givenUserId() {
|
|
|
|
userId = validCredentialsUser.id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
describe('Identity verification', function() {
|
2017-04-07 18:31:11 +00:00
|
|
|
describe('user.verify(verifyOptions, options, cb)', function() {
|
|
|
|
const ctxOptions = {testFlag: true};
|
|
|
|
let verifyOptions;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
// reset verifyOptions before each test
|
|
|
|
verifyOptions = {
|
|
|
|
type: 'email',
|
|
|
|
from: 'noreply@example.org',
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
describe('User.getVerifyOptions()', function() {
|
|
|
|
it('returns default verify options', function(done) {
|
|
|
|
const verifyOptions = User.getVerifyOptions();
|
|
|
|
expect(verifyOptions).to.eql({
|
|
|
|
type: 'email',
|
|
|
|
from: 'noreply@example.com',
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles custom verify options defined via model.settings', function(done) {
|
|
|
|
User.settings.verifyOptions = {
|
|
|
|
type: 'email',
|
|
|
|
from: 'test@example.com',
|
|
|
|
};
|
|
|
|
const verifyOptions = User.getVerifyOptions();
|
|
|
|
expect(verifyOptions).to.eql(User.settings.verifyOptions);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be extended by user', function(done) {
|
|
|
|
User.getVerifyOptions = function() {
|
|
|
|
const base = User.base.getVerifyOptions();
|
|
|
|
return Object.assign({}, base, {
|
|
|
|
redirect: '/redirect',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const verifyOptions = User.getVerifyOptions();
|
|
|
|
expect(verifyOptions).to.eql({
|
|
|
|
type: 'email',
|
|
|
|
from: 'noreply@example.com',
|
|
|
|
redirect: '/redirect',
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('verifies a user\'s email address', function(done) {
|
2013-07-03 05:37:31 +00:00
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions, 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'));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
2013-07-03 05:37:31 +00:00
|
|
|
});
|
|
|
|
});
|
2014-10-24 05:46:23 +00:00
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('verifies a user\'s email address - promise variant', function(done) {
|
2015-07-01 11:43:25 +00:00
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions)
|
2015-07-01 11:43:25 +00:00
|
|
|
.then(function(result) {
|
|
|
|
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'));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2015-07-01 11:43:25 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
2015-07-01 11:43:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('verifies a user\'s email address with custom header', function(done) {
|
2014-10-24 05:46:23 +00:00
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
verifyOptions.headers = {'message-id': 'custom-header-value'};
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions, function(err, result) {
|
2014-10-24 05:46:23 +00:00
|
|
|
assert(result.email);
|
|
|
|
assert.equal(result.email.messageId, 'custom-header-value');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-10-24 05:46:23 +00:00
|
|
|
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/)
|
2016-11-11 13:28:49 +00:00
|
|
|
.expect(200)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2016-11-11 13:28:49 +00:00
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('verifies a user\'s email address with custom template function', function(done) {
|
2016-11-11 13:28:49 +00:00
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
verifyOptions.templateFn = function(verifyOptions, cb) {
|
|
|
|
cb(null, 'custom template - verify url: ' + verifyOptions.verifyHref);
|
2016-11-11 13:28:49 +00:00
|
|
|
};
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions, function(err, result) {
|
2016-11-11 13:28:49 +00:00
|
|
|
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('custom template'));
|
|
|
|
assert(~msg.indexOf('To: bar@bat.com'));
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
|
|
|
.expect('Content-Type', /json/)
|
2014-10-24 05:46:23 +00:00
|
|
|
.expect(200)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
2014-10-24 05:46:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-07 03:27:40 +00:00
|
|
|
it('converts uid value to string', function(done) {
|
|
|
|
const idString = '58be263abc88dd483956030a';
|
|
|
|
let actualVerifyHref;
|
|
|
|
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
verifyOptions.templateFn = function(verifyOptions, cb) {
|
|
|
|
actualVerifyHref = verifyOptions.verifyHref;
|
|
|
|
cb(null, 'dummy body');
|
2017-03-07 03:27:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// replace the string id with an object
|
|
|
|
// TODO: find a better way to do this
|
|
|
|
Object.defineProperty(user, 'pk', {
|
|
|
|
get: function() { return this.__data.pk; },
|
|
|
|
set: function(value) { this.__data.pk = value; },
|
|
|
|
});
|
|
|
|
user.pk = {toString: function() { return idString; }};
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions, function(err, result) {
|
2017-03-07 03:27:40 +00:00
|
|
|
expect(result.uid).to.exist().and.be.an('object');
|
|
|
|
expect(result.uid.toString()).to.equal(idString);
|
|
|
|
const parsed = url.parse(actualVerifyHref, true);
|
|
|
|
expect(parsed.query.uid, 'uid query field').to.eql(idString);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({email: 'bar@bat.com', password: 'bar', pk: idString})
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('verifies a user\'s email address with custom token generator', function(done) {
|
2015-03-12 14:56:43 +00:00
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
verifyOptions.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');
|
|
|
|
});
|
2015-03-12 14:56:43 +00:00
|
|
|
};
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions, function(err, result) {
|
2015-03-12 14:56:43 +00:00
|
|
|
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'));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-12 14:56:43 +00:00
|
|
|
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)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2015-03-12 14:56:43 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
2015-03-12 14:56:43 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('fails if custom token generator returns error', function(done) {
|
2015-03-12 14:56:43 +00:00
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
verifyOptions.generateVerificationToken = function(user, cb) {
|
|
|
|
// let's ensure async execution works on this one
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(new Error('Fake error'));
|
|
|
|
});
|
2015-03-12 14:56:43 +00:00
|
|
|
};
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions, function(err, result) {
|
2015-03-12 14:56:43 +00:00
|
|
|
assert(err);
|
|
|
|
assert.equal(err.message, 'Fake error');
|
|
|
|
assert.equal(result, undefined);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-12 14:56:43 +00:00
|
|
|
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)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2015-03-12 14:56:43 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
2015-03-12 14:56:43 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-18 09:51:17 +00:00
|
|
|
describe('Verification link port-squashing', function() {
|
2017-03-28 09:10:51 +00:00
|
|
|
it('does not squash non-80 ports for HTTP links', function(done) {
|
2015-09-18 09:51:17 +00:00
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
Object.assign(verifyOptions, {
|
2015-09-18 09:51:17 +00:00
|
|
|
host: 'myapp.org',
|
2016-04-01 09:14:26 +00:00
|
|
|
port: 3000,
|
2017-04-07 18:31:11 +00:00
|
|
|
});
|
2015-09-18 09:51:17 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions, function(err, result) {
|
2015-09-18 09:51:17 +00:00
|
|
|
var msg = result.email.response.toString('utf-8');
|
|
|
|
assert(~msg.indexOf('http://myapp.org:3000/'));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-09-18 09:51:17 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2015-09-18 09:51:17 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
2015-09-18 09:51:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('squashes port 80 for HTTP links', function(done) {
|
2015-09-18 09:51:17 +00:00
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
Object.assign(verifyOptions, {
|
2015-09-18 09:51:17 +00:00
|
|
|
host: 'myapp.org',
|
2016-04-01 09:14:26 +00:00
|
|
|
port: 80,
|
2017-04-07 18:31:11 +00:00
|
|
|
});
|
2015-09-18 09:51:17 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions, function(err, result) {
|
2015-09-18 09:51:17 +00:00
|
|
|
var msg = result.email.response.toString('utf-8');
|
|
|
|
assert(~msg.indexOf('http://myapp.org/'));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-09-18 09:51:17 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2015-09-18 09:51:17 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
2015-09-18 09:51:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('does not squash non-443 ports for HTTPS links', function(done) {
|
2015-09-18 09:51:17 +00:00
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
Object.assign(verifyOptions, {
|
2015-09-18 09:51:17 +00:00
|
|
|
host: 'myapp.org',
|
2016-04-01 09:14:26 +00:00
|
|
|
port: 3000,
|
2017-04-07 18:31:11 +00:00
|
|
|
protocol: 'https',
|
|
|
|
});
|
2015-09-18 09:51:17 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions, function(err, result) {
|
2015-09-18 09:51:17 +00:00
|
|
|
var msg = result.email.response.toString('utf-8');
|
|
|
|
assert(~msg.indexOf('https://myapp.org:3000/'));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-09-18 09:51:17 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2015-09-18 09:51:17 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
2015-09-18 09:51:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('squashes port 443 for HTTPS links', function(done) {
|
2015-09-18 09:51:17 +00:00
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
Object.assign(verifyOptions, {
|
2015-09-18 09:51:17 +00:00
|
|
|
host: 'myapp.org',
|
2017-04-07 18:31:11 +00:00
|
|
|
protocol: 'https',
|
2016-04-01 09:14:26 +00:00
|
|
|
port: 443,
|
2017-04-07 18:31:11 +00:00
|
|
|
});
|
2015-09-18 09:51:17 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions, function(err, result) {
|
2015-09-18 09:51:17 +00:00
|
|
|
var msg = result.email.response.toString('utf-8');
|
|
|
|
assert(~msg.indexOf('https://myapp.org/'));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-09-18 09:51:17 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2015-09-18 09:51:17 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
2015-09-18 09:51:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('hides verification tokens from user JSON', function(done) {
|
2016-04-01 09:14:26 +00:00
|
|
|
var user = new User({
|
|
|
|
email: 'bar@bat.com',
|
|
|
|
password: 'bar',
|
|
|
|
verificationToken: 'a-token',
|
|
|
|
});
|
2015-12-03 09:56:32 +00:00
|
|
|
var data = user.toJSON();
|
|
|
|
assert(!('verificationToken' in data));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-12-03 09:56:32 +00:00
|
|
|
done();
|
|
|
|
});
|
2016-09-06 11:55:54 +00:00
|
|
|
|
2017-03-28 09:10:51 +00:00
|
|
|
it('squashes "//" when restApiRoot is "/"', function(done) {
|
2016-09-06 11:55:54 +00:00
|
|
|
var emailBody;
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(user, 'afterRemote should include result');
|
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
Object.assign(verifyOptions, {
|
2016-09-06 11:55:54 +00:00
|
|
|
host: 'myapp.org',
|
|
|
|
port: 3000,
|
|
|
|
restApiRoot: '/',
|
2017-04-07 18:31:11 +00:00
|
|
|
});
|
2016-09-06 11:55:54 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions, function(err, result) {
|
2016-09-06 11:55:54 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
emailBody = result.email.response.toString('utf-8');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/test-users')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: 'user@example.com', password: 'pass'})
|
2016-09-06 11:55:54 +00:00
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(emailBody)
|
|
|
|
.to.contain('http://myapp.org:3000/test-users/confirm');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-12-06 14:58:27 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
it('removes "verifyOptions.template" from Email payload', function() {
|
2016-12-06 14:58:27 +00:00
|
|
|
var MailerMock = {
|
2017-04-07 18:31:11 +00:00
|
|
|
send: function(verifyOptions, cb) { cb(null, verifyOptions); },
|
2016-12-06 14:58:27 +00:00
|
|
|
};
|
2017-04-07 18:31:11 +00:00
|
|
|
verifyOptions.mailer = MailerMock;
|
2016-12-06 14:58:27 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
return user.verify(verifyOptions)
|
2016-12-06 14:58:27 +00:00
|
|
|
.then(function(result) {
|
|
|
|
expect(result.email).to.not.have.property('template');
|
|
|
|
});
|
|
|
|
});
|
2017-02-06 11:39:21 +00:00
|
|
|
|
|
|
|
it('allows hash fragment in redirectUrl', function() {
|
2017-04-07 18:31:11 +00:00
|
|
|
let actualVerifyHref;
|
|
|
|
|
|
|
|
Object.assign(verifyOptions, {
|
|
|
|
redirect: '#/some-path?a=1&b=2',
|
|
|
|
templateFn: (verifyOptions, cb) => {
|
|
|
|
actualVerifyHref = verifyOptions.verifyHref;
|
|
|
|
cb(null, 'dummy body');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return user.verify(verifyOptions)
|
|
|
|
.then(() => actualVerifyHref)
|
2017-02-06 11:39:21 +00:00
|
|
|
.then(verifyHref => {
|
|
|
|
var parsed = url.parse(verifyHref, true);
|
|
|
|
expect(parsed.query.redirect, 'redirect query')
|
|
|
|
.to.equal('#/some-path?a=1&b=2');
|
|
|
|
});
|
|
|
|
});
|
2017-03-04 04:45:02 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
it('verifies that verifyOptions.templateFn receives verifyOptions.verificationToken',
|
|
|
|
function() {
|
|
|
|
let actualVerificationToken;
|
|
|
|
|
|
|
|
Object.assign(verifyOptions, {
|
|
|
|
redirect: '#/some-path?a=1&b=2',
|
|
|
|
templateFn: (verifyOptions, cb) => {
|
|
|
|
actualVerificationToken = verifyOptions.verificationToken;
|
|
|
|
cb(null, 'dummy body');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return user.verify(verifyOptions)
|
|
|
|
.then(() => actualVerificationToken)
|
2017-03-04 04:45:02 +00:00
|
|
|
.then(token => {
|
|
|
|
expect(token).to.exist();
|
|
|
|
});
|
|
|
|
});
|
2017-04-07 18:31:11 +00:00
|
|
|
|
|
|
|
it('forwards the "options" argument to user.save() ' +
|
|
|
|
'when adding verification token', function() {
|
|
|
|
let onBeforeSaveCtx = {};
|
|
|
|
|
|
|
|
// before save operation hook to capture remote ctx when saving
|
|
|
|
// verification token in user instance
|
|
|
|
User.observe('before save', function(ctx, next) {
|
|
|
|
onBeforeSaveCtx = ctx || {};
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
return user.verify(verifyOptions, ctxOptions)
|
|
|
|
.then(() => {
|
|
|
|
// not checking equality since other properties are added by user.save()
|
|
|
|
expect(onBeforeSaveCtx.options).to.contain({testFlag: true});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('forwards the "options" argument to a custom templateFn function', function() {
|
|
|
|
let templateFnOptions;
|
|
|
|
|
|
|
|
// custom templateFn function accepting the options argument
|
|
|
|
verifyOptions.templateFn = (verifyOptions, options, cb) => {
|
|
|
|
templateFnOptions = options;
|
|
|
|
cb(null, 'dummy body');
|
|
|
|
};
|
|
|
|
|
|
|
|
return user.verify(verifyOptions, ctxOptions)
|
|
|
|
.then(() => {
|
|
|
|
// not checking equality since other properties are added by user.save()
|
|
|
|
expect(templateFnOptions).to.contain({testFlag: true});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('forwards the "options" argment to a custom token generator function', function() {
|
|
|
|
let generateTokenOptions;
|
|
|
|
|
|
|
|
// custom generateVerificationToken function accepting the options argument
|
|
|
|
verifyOptions.generateVerificationToken = (user, options, cb) => {
|
|
|
|
generateTokenOptions = options;
|
|
|
|
cb(null, 'dummy token');
|
|
|
|
};
|
|
|
|
|
|
|
|
return user.verify(verifyOptions, ctxOptions)
|
|
|
|
.then(() => {
|
|
|
|
// not checking equality since other properties are added by user.save()
|
|
|
|
expect(generateTokenOptions).to.contain({testFlag: true});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('forwards the "options" argument to a custom mailer function', function() {
|
|
|
|
let mailerOptions;
|
|
|
|
|
|
|
|
// custom mailer function accepting the options argument
|
|
|
|
const mailer = function() {};
|
|
|
|
mailer.send = function(verifyOptions, options, cb) {
|
|
|
|
mailerOptions = options;
|
|
|
|
cb(null, 'dummy result');
|
|
|
|
};
|
|
|
|
verifyOptions.mailer = mailer;
|
|
|
|
|
|
|
|
return user.verify(verifyOptions, ctxOptions)
|
|
|
|
.then(() => {
|
|
|
|
// not checking equality since other properties are added by user.save()
|
|
|
|
expect(mailerOptions).to.contain({testFlag: true});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function givenUser() {
|
|
|
|
return User.create({email: 'test@example.com', password: 'pass'})
|
|
|
|
.then(u => user = u);
|
|
|
|
}
|
2017-03-28 09:10:51 +00:00
|
|
|
|
|
|
|
it('is called over REST method /User/:id/verify', function() {
|
|
|
|
return User.create({email: 'bar@bat.com', password: 'bar'})
|
|
|
|
.then(user => {
|
|
|
|
return request(app)
|
|
|
|
.post('/test-users/' + user.pk + '/verify')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
// we already tested before that User.verify(id) works correctly
|
|
|
|
// having the remote method returning 204 is enough to make sure
|
|
|
|
// User.verify() was called successfully
|
|
|
|
.expect(204);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails over REST method /User/:id/verify with invalid user id', function() {
|
|
|
|
return request(app)
|
|
|
|
.post('/test-users/' + 'invalid-id' + '/verify')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
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() {
|
2017-04-07 18:31:11 +00:00
|
|
|
var verifyOptions;
|
2014-07-17 05:42:05 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
verifyOptions = {
|
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,
|
2016-04-01 09:14:26 +00:00
|
|
|
host: ctx.req.get('host'),
|
2013-07-03 05:37:31 +00:00
|
|
|
};
|
2014-07-17 05:42:05 +00:00
|
|
|
|
2017-04-07 18:31:11 +00:00
|
|
|
user.verify(verifyOptions, function(err, result) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2014-07-17 05:42:05 +00:00
|
|
|
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)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({email: 'bar@bat.com', password: 'bar'})
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +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) +
|
2017-04-07 18:31:11 +00:00
|
|
|
'&redirect=' + encodeURIComponent(verifyOptions.redirect))
|
2014-07-17 05:42:05 +00:00
|
|
|
.expect(302)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2014-07-17 05:42:05 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
2016-06-16 06:20:33 +00:00
|
|
|
it('sets verificationToken to null after confirmation', function(done) {
|
|
|
|
testConfirm(function(result, done) {
|
|
|
|
User.confirm(result.uid, result.token, false, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
// Verify by loading user data stored in the datasource
|
|
|
|
User.findById(result.uid, function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(user).to.have.property('verificationToken', null);
|
|
|
|
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) +
|
2017-04-07 18:31:11 +00:00
|
|
|
'&redirect=' + encodeURIComponent(verifyOptions.redirect))
|
2014-07-17 05:42:05 +00:00
|
|
|
.expect(404)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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' +
|
2017-04-07 18:31:11 +00:00
|
|
|
'&redirect=' + encodeURIComponent(verifyOptions.redirect))
|
2014-07-17 05:42:05 +00:00
|
|
|
.expect(400)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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() {
|
2016-11-30 11:44:30 +00:00
|
|
|
var options = {
|
|
|
|
email: 'foo@bar.com',
|
|
|
|
redirect: 'http://foobar.com/reset-password',
|
|
|
|
};
|
2014-12-23 03:12:50 +00:00
|
|
|
|
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-01 11:43:25 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-10-30 21:59:31 +00:00
|
|
|
it('Reports when email is not found', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.resetPassword({email: 'unknown@email.com'}, function(err) {
|
2015-10-30 21:59:31 +00:00
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'EMAIL_NOT_FOUND');
|
|
|
|
assert.equal(err.statusCode, 404);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-10-30 21:59:31 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-30 11:44:30 +00:00
|
|
|
it('Checks that options exist', function(done) {
|
|
|
|
var calledBack = false;
|
|
|
|
|
|
|
|
User.resetPassword(options, function() {
|
|
|
|
calledBack = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
User.once('resetPasswordRequest', function(info) {
|
|
|
|
assert(info.options);
|
|
|
|
assert.equal(info.options, options);
|
|
|
|
assert(calledBack);
|
|
|
|
|
|
|
|
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({
|
2016-11-30 11:44:30 +00:00
|
|
|
email: options.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);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-30 11:44:30 +00:00
|
|
|
assert.equal(user.email, options.email);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-11-20 18:59:29 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-12-23 03:12:50 +00:00
|
|
|
|
2016-12-11 03:12:54 +00:00
|
|
|
it('calls createAccessToken() to create the token', function(done) {
|
|
|
|
User.prototype.createAccessToken = function(ttl, cb) {
|
|
|
|
cb(null, new AccessToken({id: 'custom-token'}));
|
|
|
|
};
|
|
|
|
|
|
|
|
User.resetPassword({email: options.email}, function() {});
|
|
|
|
|
|
|
|
User.once('resetPasswordRequest', function(info) {
|
|
|
|
expect(info.accessToken.id).to.equal('custom-token');
|
|
|
|
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) {
|
2016-05-05 04:09:06 +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, 'EMAIL_REQUIRED');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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)
|
2016-11-30 11:44:30 +00:00
|
|
|
.send({email: options.email})
|
2014-12-23 03:12:50 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2016-08-02 08:59:21 +00:00
|
|
|
assert.deepEqual(res.body, '');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-12-23 03:12:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-12-03 20:25:13 +00:00
|
|
|
|
2017-04-10 08:36:49 +00:00
|
|
|
it('creates token that allows patching User with new password', () => {
|
|
|
|
return triggerPasswordReset(options.email)
|
|
|
|
.then(info => {
|
|
|
|
// Make a REST request to change the password
|
|
|
|
return request(app)
|
|
|
|
.patch(`/test-users/${info.user.id}`)
|
|
|
|
.set('Authorization', info.accessToken.id)
|
|
|
|
.send({password: 'new-pass'})
|
|
|
|
.expect(200);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
// Call login to verify the password was changed
|
|
|
|
const credentials = {email: options.email, password: 'new-pass'};
|
|
|
|
return User.login(credentials);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates token that allows calling other endpoints too', () => {
|
|
|
|
// Setup a test method that can be executed by $owner only
|
|
|
|
User.prototype.testMethod = function(cb) { cb(null, 'ok'); };
|
|
|
|
User.remoteMethod('prototype.testMethod', {
|
|
|
|
returns: {arg: 'status', type: 'string'},
|
|
|
|
http: {verb: 'get', path: '/test'},
|
|
|
|
});
|
|
|
|
User.settings.acls.push({
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: '$owner',
|
|
|
|
permission: 'ALLOW',
|
|
|
|
property: 'testMethod',
|
|
|
|
});
|
|
|
|
|
|
|
|
return triggerPasswordReset(options.email)
|
|
|
|
.then(info => request(app)
|
|
|
|
.get(`/test-users/${info.user.id}/test`)
|
|
|
|
.set('Authorization', info.accessToken.id)
|
|
|
|
.expect(200));
|
|
|
|
});
|
|
|
|
|
2016-12-03 20:25:13 +00:00
|
|
|
describe('User.resetPassword(options, cb) requiring realm', function() {
|
|
|
|
var realmUser;
|
|
|
|
|
|
|
|
beforeEach(function(done) {
|
|
|
|
User.create(validCredentialsWithRealm, function(err, u) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
realmUser = u;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Reports when email is not found in realm', function(done) {
|
|
|
|
User.resetPassword({
|
|
|
|
email: realmUser.email,
|
|
|
|
realm: 'unknown',
|
|
|
|
}, function(err) {
|
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'EMAIL_NOT_FOUND');
|
|
|
|
assert.equal(err.statusCode, 404);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Creates a temp accessToken to allow user in realm to change password', function(done) {
|
|
|
|
var calledBack = false;
|
|
|
|
|
|
|
|
User.resetPassword({
|
|
|
|
email: realmUser.email,
|
|
|
|
realm: realmUser.realm,
|
|
|
|
}, function() {
|
|
|
|
calledBack = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
User.once('resetPasswordRequest', function(info) {
|
|
|
|
assert(info.email);
|
|
|
|
assert(info.accessToken);
|
|
|
|
assert(info.accessToken.id);
|
|
|
|
assert.equal(info.accessToken.ttl / 60, 15);
|
|
|
|
assert(calledBack);
|
|
|
|
info.accessToken.user(function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
assert.equal(user.email, realmUser.email);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-11-20 18:59:29 +00:00
|
|
|
});
|
|
|
|
});
|
2014-10-10 09:21:15 +00:00
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
describe('AccessToken (session) invalidation', function() {
|
|
|
|
var user, originalUserToken1, originalUserToken2, newUserCreated;
|
|
|
|
var currentEmailCredentials = {email: 'original@example.com', password: 'bar'};
|
|
|
|
var updatedEmailCredentials = {email: 'updated@example.com', password: 'bar'};
|
|
|
|
var newUserCred = {email: 'newuser@example.com', password: 'newpass'};
|
2016-08-30 19:09:11 +00:00
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
beforeEach('create user then login', function createAndLogin(done) {
|
|
|
|
async.series([
|
|
|
|
function createUserWithOriginalEmail(next) {
|
|
|
|
User.create(currentEmailCredentials, function(err, userCreated) {
|
|
|
|
if (err) return next(err);
|
|
|
|
user = userCreated;
|
|
|
|
next();
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
},
|
|
|
|
function firstLoginWithOriginalEmail(next) {
|
|
|
|
User.login(currentEmailCredentials, function(err, accessToken1) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert(accessToken1.userId);
|
2017-01-13 09:40:48 +00:00
|
|
|
originalUserToken1 = accessToken1;
|
2016-12-09 14:36:54 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function secondLoginWithOriginalEmail(next) {
|
|
|
|
User.login(currentEmailCredentials, function(err, accessToken2) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert(accessToken2.userId);
|
2017-01-13 09:40:48 +00:00
|
|
|
originalUserToken2 = accessToken2;
|
2016-12-09 14:36:54 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], done);
|
|
|
|
});
|
2016-08-30 19:09:11 +00:00
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('invalidates sessions when email is changed using `updateAttributes`', function(done) {
|
|
|
|
user.updateAttributes(
|
|
|
|
{email: updatedEmailCredentials.email},
|
|
|
|
function(err, userInstance) {
|
2016-08-30 19:09:11 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
assertNoAccessTokens(done);
|
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
});
|
2016-08-30 19:09:11 +00:00
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('invalidates sessions after `replaceAttributes`', function(done) {
|
|
|
|
// The way how the invalidation is implemented now, all sessions
|
|
|
|
// are invalidated on a full replace
|
|
|
|
user.replaceAttributes(currentEmailCredentials, function(err, userInstance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assertNoAccessTokens(done);
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
});
|
2016-08-30 19:09:11 +00:00
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('invalidates sessions when email is changed using `updateOrCreate`', function(done) {
|
|
|
|
User.updateOrCreate({
|
2017-01-05 15:34:15 +00:00
|
|
|
pk: user.pk,
|
2016-12-09 14:36:54 +00:00
|
|
|
email: updatedEmailCredentials.email,
|
|
|
|
}, function(err, userInstance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assertNoAccessTokens(done);
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
});
|
2016-08-30 19:09:11 +00:00
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('invalidates sessions after `replaceById`', function(done) {
|
|
|
|
// The way how the invalidation is implemented now, all sessions
|
|
|
|
// are invalidated on a full replace
|
2017-01-05 15:34:15 +00:00
|
|
|
User.replaceById(user.pk, currentEmailCredentials, function(err, userInstance) {
|
2016-12-09 14:36:54 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
assertNoAccessTokens(done);
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
});
|
2016-08-30 19:09:11 +00:00
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('invalidates sessions after `replaceOrCreate`', function(done) {
|
|
|
|
// The way how the invalidation is implemented now, all sessions
|
|
|
|
// are invalidated on a full replace
|
|
|
|
User.replaceOrCreate({
|
2017-01-05 15:34:15 +00:00
|
|
|
pk: user.pk,
|
2016-12-09 14:36:54 +00:00
|
|
|
email: currentEmailCredentials.email,
|
|
|
|
password: currentEmailCredentials.password,
|
|
|
|
}, function(err, userInstance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assertNoAccessTokens(done);
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
});
|
2016-08-30 19:09:11 +00:00
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('keeps sessions AS IS if firstName is added using `updateAttributes`', function(done) {
|
|
|
|
user.updateAttributes({'firstName': 'Janny'}, function(err, userInstance) {
|
|
|
|
if (err) return done(err);
|
2017-01-13 09:40:48 +00:00
|
|
|
assertPreservedTokens(done);
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
});
|
2016-08-30 19:09:11 +00:00
|
|
|
|
2017-01-30 10:30:05 +00:00
|
|
|
it('keeps sessions AS IS when calling save() with no changes', function(done) {
|
|
|
|
user.save(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assertPreservedTokens(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('keeps sessions AS IS if firstName is added using `updateOrCreate`', function(done) {
|
|
|
|
User.updateOrCreate({
|
2017-01-05 15:34:15 +00:00
|
|
|
pk: user.pk,
|
2016-12-09 14:36:54 +00:00
|
|
|
firstName: 'Loay',
|
|
|
|
email: currentEmailCredentials.email,
|
|
|
|
}, function(err, userInstance) {
|
|
|
|
if (err) return done(err);
|
2017-01-13 09:40:48 +00:00
|
|
|
assertPreservedTokens(done);
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
});
|
2016-08-30 19:09:11 +00:00
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('keeps sessions AS IS if a new user is created using `create`', function(done) {
|
|
|
|
async.series([
|
|
|
|
function(next) {
|
|
|
|
User.create(newUserCred, function(err, newUserInstance) {
|
2016-08-30 19:09:11 +00:00
|
|
|
if (err) return done(err);
|
2016-12-09 14:36:54 +00:00
|
|
|
newUserCreated = newUserInstance;
|
|
|
|
next();
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
User.login(newUserCred, function(err, newAccessToken) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert(newAccessToken.id);
|
|
|
|
assertPreservedTokens(next);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], done);
|
|
|
|
});
|
2016-08-30 19:09:11 +00:00
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('keeps sessions AS IS if a new user is created using `updateOrCreate`', function(done) {
|
|
|
|
async.series([
|
|
|
|
function(next) {
|
|
|
|
User.create(newUserCred, function(err, newUserInstance2) {
|
|
|
|
if (err) return done(err);
|
|
|
|
newUserCreated = newUserInstance2;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
User.login(newUserCred, function(err, newAccessToken2) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert(newAccessToken2.id);
|
|
|
|
assertPreservedTokens(next);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], done);
|
|
|
|
});
|
2016-08-30 19:09:11 +00:00
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('keeps sessions AS IS if non-email property is changed using updateAll', function(done) {
|
|
|
|
var userPartial;
|
|
|
|
async.series([
|
|
|
|
function createPartialUser(next) {
|
|
|
|
User.create(
|
|
|
|
{email: 'partial@example.com', password: 'pass1', age: 25},
|
|
|
|
function(err, partialInstance) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-12-09 14:36:54 +00:00
|
|
|
userPartial = partialInstance;
|
2016-09-27 13:16:08 +00:00
|
|
|
next();
|
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
},
|
|
|
|
function loginPartiallUser(next) {
|
|
|
|
User.login({email: 'partial@example.com', password: 'pass1'}, function(err, ats) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-12-09 14:36:54 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function updatePartialUser(next) {
|
|
|
|
User.updateAll(
|
2017-01-05 15:34:15 +00:00
|
|
|
{pk: userPartial.pk},
|
2016-12-09 14:36:54 +00:00
|
|
|
{age: userPartial.age + 1},
|
|
|
|
function(err, info) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-09-27 13:16:08 +00:00
|
|
|
next();
|
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
},
|
|
|
|
function verifyTokensOfPartialUser(next) {
|
2017-01-05 15:34:15 +00:00
|
|
|
AccessToken.find({where: {userId: userPartial.pk}}, function(err, tokens1) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-12-09 14:36:54 +00:00
|
|
|
expect(tokens1.length).to.equal(1);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], done);
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('preserves other users\' sessions if their email is untouched', function(done) {
|
2016-08-30 19:09:11 +00:00
|
|
|
var user1, user2, user3;
|
2016-12-09 14:36:54 +00:00
|
|
|
async.series([
|
|
|
|
function(next) {
|
|
|
|
User.create({email: 'user1@example.com', password: 'u1pass'}, function(err, u1) {
|
|
|
|
if (err) return done(err);
|
|
|
|
User.create({email: 'user2@example.com', password: 'u2pass'}, function(err, u2) {
|
2016-08-30 19:09:11 +00:00
|
|
|
if (err) return done(err);
|
2016-12-09 14:36:54 +00:00
|
|
|
User.create({email: 'user3@example.com', password: 'u3pass'}, function(err, u3) {
|
2016-08-30 19:09:11 +00:00
|
|
|
if (err) return done(err);
|
2016-12-09 14:36:54 +00:00
|
|
|
user1 = u1;
|
|
|
|
user2 = u2;
|
|
|
|
user3 = u3;
|
|
|
|
next();
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
User.login(
|
|
|
|
{email: 'user1@example.com', password: 'u1pass'},
|
|
|
|
function(err, accessToken1) {
|
|
|
|
if (err) return next(err);
|
|
|
|
User.login(
|
|
|
|
{email: 'user2@example.com', password: 'u2pass'},
|
|
|
|
function(err, accessToken2) {
|
|
|
|
if (err) return next(err);
|
|
|
|
User.login({email: 'user3@example.com', password: 'u3pass'},
|
|
|
|
function(err, accessToken3) {
|
2016-08-30 19:09:11 +00:00
|
|
|
if (err) return next(err);
|
2016-12-09 14:36:54 +00:00
|
|
|
next();
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
});
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
user2.updateAttribute('email', 'user2Update@b.com', function(err, userInstance) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert.equal(userInstance.email, 'user2Update@b.com');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
2017-01-05 15:34:15 +00:00
|
|
|
AccessToken.find({where: {userId: user1.pk}}, function(err, tokens1) {
|
2016-12-09 14:36:54 +00:00
|
|
|
if (err) return next(err);
|
2017-01-05 15:34:15 +00:00
|
|
|
AccessToken.find({where: {userId: user2.pk}}, function(err, tokens2) {
|
2016-08-30 19:09:11 +00:00
|
|
|
if (err) return next(err);
|
2017-01-05 15:34:15 +00:00
|
|
|
AccessToken.find({where: {userId: user3.pk}}, function(err, tokens3) {
|
2016-08-30 19:09:11 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
expect(tokens1.length).to.equal(1);
|
|
|
|
expect(tokens2.length).to.equal(0);
|
|
|
|
expect(tokens3.length).to.equal(1);
|
|
|
|
next();
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
], done);
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('invalidates correct sessions after changing email using updateAll', function(done) {
|
2016-08-30 19:09:11 +00:00
|
|
|
var userSpecial, userNormal;
|
|
|
|
async.series([
|
|
|
|
function createSpecialUser(next) {
|
|
|
|
User.create(
|
2016-11-15 21:46:23 +00:00
|
|
|
{email: 'special@example.com', password: 'pass1', name: 'Special'},
|
2016-08-30 19:09:11 +00:00
|
|
|
function(err, specialInstance) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-08-30 19:09:11 +00:00
|
|
|
userSpecial = specialInstance;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function loginSpecialUser(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.login({email: 'special@example.com', password: 'pass1'}, function(err, ats) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-08-30 19:09:11 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function updateSpecialUser(next) {
|
|
|
|
User.updateAll(
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Special'},
|
|
|
|
{email: 'superspecial@example.com'}, function(err, info) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-08-30 19:09:11 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function verifyTokensOfSpecialUser(next) {
|
2017-01-05 15:34:15 +00:00
|
|
|
AccessToken.find({where: {userId: userSpecial.pk}}, function(err, tokens1) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return done(err);
|
2016-12-09 14:36:54 +00:00
|
|
|
expect(tokens1.length, 'tokens - special user tokens').to.equal(0);
|
2016-08-30 19:09:11 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
2016-12-09 14:36:54 +00:00
|
|
|
assertPreservedTokens,
|
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invalidates session when password is reset', function(done) {
|
|
|
|
user.updateAttribute('password', 'newPass', function(err, user2) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assertNoAccessTokens(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-13 10:03:06 +00:00
|
|
|
it('preserves current session', function(done) {
|
|
|
|
var options = {accessToken: originalUserToken1};
|
|
|
|
user.updateAttribute('email', 'new@example.com', options, function(err) {
|
|
|
|
if (err) return done(err);
|
2017-01-05 15:34:15 +00:00
|
|
|
AccessToken.find({where: {userId: user.pk}}, function(err, tokens) {
|
2017-01-13 10:03:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
var tokenIds = tokens.map(function(t) { return t.id; });
|
|
|
|
expect(tokenIds).to.eql([originalUserToken1.id]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-24 13:57:41 +00:00
|
|
|
it('forwards the "options" argument', function(done) {
|
|
|
|
var options = {testFlag: true};
|
|
|
|
var observedOptions = [];
|
|
|
|
|
|
|
|
saveObservedOptionsForHook('access', User);
|
|
|
|
saveObservedOptionsForHook('before delete', AccessToken);
|
|
|
|
|
|
|
|
user.updateAttribute('password', 'newPass', options, function(err, updated) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
expect(observedOptions).to.eql([
|
|
|
|
// prepareForTokenInvalidation - load current instance data
|
|
|
|
{hook: 'access', testFlag: true},
|
|
|
|
|
|
|
|
// validate uniqueness of User.email
|
|
|
|
{hook: 'access', testFlag: true},
|
|
|
|
|
|
|
|
// _invalidateAccessTokensOfUsers - deleteAll
|
|
|
|
{hook: 'before delete', testFlag: true},
|
|
|
|
]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
function saveObservedOptionsForHook(name, model) {
|
|
|
|
model.observe(name, function(ctx, next) {
|
|
|
|
observedOptions.push(extend({hook: name}, ctx.options));
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
it('preserves other user sessions if their password is untouched', function(done) {
|
|
|
|
var user1, user2, user1Token;
|
|
|
|
async.series([
|
|
|
|
function(next) {
|
|
|
|
User.create({email: 'user1@example.com', password: 'u1pass'}, function(err, u1) {
|
|
|
|
if (err) return done(err);
|
|
|
|
User.create({email: 'user2@example.com', password: 'u2pass'}, function(err, u2) {
|
|
|
|
if (err) return done(err);
|
|
|
|
user1 = u1;
|
|
|
|
user2 = u2;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
User.login({email: 'user1@example.com', password: 'u1pass'}, function(err, at1) {
|
|
|
|
User.login({email: 'user2@example.com', password: 'u2pass'}, function(err, at2) {
|
|
|
|
assert(at1.userId);
|
|
|
|
assert(at2.userId);
|
|
|
|
user1Token = at1.id;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
user2.updateAttribute('password', 'newPass', function(err, user2Instance) {
|
|
|
|
if (err) return next(err);
|
|
|
|
assert(user2Instance);
|
2016-08-30 19:09:11 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
2016-12-09 14:36:54 +00:00
|
|
|
function(next) {
|
2017-01-05 15:34:15 +00:00
|
|
|
AccessToken.find({where: {userId: user1.pk}}, function(err, tokens1) {
|
2016-12-09 14:36:54 +00:00
|
|
|
if (err) return next(err);
|
2017-01-05 15:34:15 +00:00
|
|
|
AccessToken.find({where: {userId: user2.pk}}, function(err, tokens2) {
|
2016-12-09 14:36:54 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
expect(tokens1.length).to.equal(1);
|
|
|
|
expect(tokens2.length).to.equal(0);
|
|
|
|
assert.equal(tokens1[0].id, user1Token);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], function(err) {
|
|
|
|
done();
|
|
|
|
});
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
2016-12-09 14:36:54 +00:00
|
|
|
|
2017-02-23 11:56:13 +00:00
|
|
|
// See https://github.com/strongloop/loopback/issues/3215
|
|
|
|
it('handles subclassed user with no accessToken relation', () => {
|
|
|
|
// setup a new LoopBack app, we don't want to use shared models
|
|
|
|
app = loopback({localRegistry: true, loadBuiltinModels: true});
|
2017-02-24 13:07:41 +00:00
|
|
|
app.set('_verifyAuthModelRelations', false);
|
2017-02-23 11:56:13 +00:00
|
|
|
app.set('remoting', {errorHandler: {debug: true, log: false}});
|
|
|
|
app.dataSource('db', {connector: 'memory'});
|
|
|
|
const User = app.registry.createModel({
|
|
|
|
name: 'user',
|
|
|
|
base: 'User',
|
|
|
|
});
|
|
|
|
app.model(User, {dataSource: 'db'});
|
|
|
|
app.enableAuth({dataSource: 'db'});
|
|
|
|
expect(app.models.User.modelName).to.eql('user');
|
|
|
|
|
|
|
|
return User.create(validCredentials)
|
|
|
|
.then(u => {
|
|
|
|
u.email = 'updated@example.com';
|
|
|
|
return u.save();
|
|
|
|
// the test passes when save() does not throw any error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-12-09 14:36:54 +00:00
|
|
|
function assertPreservedTokens(done) {
|
2017-01-05 15:34:15 +00:00
|
|
|
AccessToken.find({where: {userId: user.pk}}, function(err, tokens) {
|
2016-12-09 14:36:54 +00:00
|
|
|
if (err) return done(err);
|
2017-01-13 09:40:48 +00:00
|
|
|
var actualIds = tokens.map(function(t) { return t.id; });
|
|
|
|
actualIds.sort();
|
|
|
|
var expectedIds = [originalUserToken1.id, originalUserToken2.id];
|
|
|
|
expectedIds.sort();
|
|
|
|
expect(actualIds).to.eql(expectedIds);
|
2016-12-09 14:36:54 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function assertNoAccessTokens(done) {
|
2017-01-05 15:34:15 +00:00
|
|
|
AccessToken.find({where: {userId: user.pk}}, function(err, tokens) {
|
2016-12-09 14:36:54 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
expect(tokens.length).to.equal(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
2016-08-30 19:09:11 +00:00
|
|
|
});
|
|
|
|
|
2016-09-20 15:10:18 +00:00
|
|
|
describe('Verification after updating email', function() {
|
|
|
|
var NEW_EMAIL = 'updated@example.com';
|
|
|
|
var userInstance;
|
|
|
|
|
|
|
|
beforeEach(createOriginalUser);
|
|
|
|
|
|
|
|
it('sets verification to false after email update if verification is required',
|
|
|
|
function(done) {
|
|
|
|
User.settings.emailVerificationRequired = true;
|
|
|
|
async.series([
|
|
|
|
function updateUser(next) {
|
|
|
|
userInstance.updateAttribute('email', NEW_EMAIL, function(err, info) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-09-20 15:10:18 +00:00
|
|
|
assert.equal(info.email, NEW_EMAIL);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function findUser(next) {
|
2017-01-05 15:34:15 +00:00
|
|
|
User.findById(userInstance.pk, function(err, info) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-09-20 15:10:18 +00:00
|
|
|
assert.equal(info.email, NEW_EMAIL);
|
|
|
|
assert.equal(info.emailVerified, false);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('leaves verification as is after email update if verification is not required',
|
|
|
|
function(done) {
|
|
|
|
User.settings.emailVerificationRequired = false;
|
|
|
|
async.series([
|
|
|
|
function updateUser(next) {
|
|
|
|
userInstance.updateAttribute('email', NEW_EMAIL, function(err, info) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-09-20 15:10:18 +00:00
|
|
|
assert.equal(info.email, NEW_EMAIL);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function findUser(next) {
|
2017-01-05 15:34:15 +00:00
|
|
|
User.findById(userInstance.pk, function(err, info) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-09-20 15:10:18 +00:00
|
|
|
assert.equal(info.email, NEW_EMAIL);
|
|
|
|
assert.equal(info.emailVerified, true);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
2016-12-23 05:04:44 +00:00
|
|
|
it('should not set verification to false after something other than email is updated',
|
|
|
|
function(done) {
|
|
|
|
User.settings.emailVerificationRequired = true;
|
|
|
|
async.series([
|
|
|
|
function updateUser(next) {
|
|
|
|
userInstance.updateAttribute('realm', 'test', function(err, info) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-12-23 05:04:44 +00:00
|
|
|
assert.equal(info.realm, 'test');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function findUser(next) {
|
2017-01-05 15:34:15 +00:00
|
|
|
User.findById(userInstance.pk, function(err, info) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return next(err);
|
2016-12-23 05:04:44 +00:00
|
|
|
assert.equal(info.realm, 'test');
|
|
|
|
assert.equal(info.emailVerified, true);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
2016-09-20 15:10:18 +00:00
|
|
|
function createOriginalUser(done) {
|
|
|
|
var userData = {
|
|
|
|
email: 'original@example.com',
|
|
|
|
password: 'bar',
|
|
|
|
emailVerified: true,
|
|
|
|
};
|
|
|
|
User.create(userData, function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
userInstance = instance;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-08-24 20:30:58 +00:00
|
|
|
describe('password reset with/without email verification', function() {
|
|
|
|
it('allows resetPassword by email if email verification is required and done',
|
|
|
|
function(done) {
|
|
|
|
User.settings.emailVerificationRequired = true;
|
|
|
|
var email = validCredentialsEmailVerified.email;
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
User.resetPassword({email: email}, function(err, info) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return done(err);
|
2016-08-24 20:30:58 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('disallows resetPassword by email if email verification is required and not done',
|
|
|
|
function(done) {
|
|
|
|
User.settings.emailVerificationRequired = true;
|
|
|
|
var email = validCredentialsEmail;
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
User.resetPassword({email: email}, function(err) {
|
2016-08-24 20:30:58 +00:00
|
|
|
assert(err);
|
|
|
|
assert.equal(err.code, 'RESET_FAILED_EMAIL_NOT_VERIFIED');
|
|
|
|
assert.equal(err.statusCode, 401);
|
2017-01-06 11:12:35 +00:00
|
|
|
done();
|
2016-08-24 20:30:58 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows resetPassword by email if email verification is not required',
|
|
|
|
function(done) {
|
|
|
|
User.settings.emailVerificationRequired = false;
|
|
|
|
var email = validCredentialsEmail;
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
User.resetPassword({email: email}, function(err) {
|
2017-01-06 11:12:35 +00:00
|
|
|
if (err) return done(err);
|
2016-08-24 20:30:58 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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');
|
2016-05-02 11:48:12 +00:00
|
|
|
expect(User.email.modelName, 'modelName').to.eql('Email');
|
2014-10-10 09:21:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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() {
|
2016-11-15 21:46:23 +00:00
|
|
|
User2 = loopback.User.extend('User2', {}, {ttl: 10});
|
2014-12-28 08:02:37 +00:00
|
|
|
});
|
|
|
|
it('should override ttl setting in based User model', function() {
|
|
|
|
expect(User2.settings.ttl).to.equal(10);
|
|
|
|
});
|
|
|
|
});
|
2017-04-10 08:36:49 +00:00
|
|
|
|
|
|
|
function triggerPasswordReset(email) {
|
|
|
|
return Promise.all([
|
|
|
|
User.resetPassword({email: email}),
|
|
|
|
waitForEvent(User, 'resetPasswordRequest'),
|
|
|
|
])
|
|
|
|
.spread((reset, info) => info);
|
|
|
|
}
|
2013-10-11 20:37:51 +00:00
|
|
|
});
|