Merge pull request #3609 from sebastianfelipe/fix/user-verify-duplicated-token

Fix handling of user verification options
This commit is contained in:
Miroslav Bajtoš 2017-10-09 14:00:22 +02:00 committed by GitHub
commit 9176ee2e11
2 changed files with 35 additions and 3 deletions

View File

@ -603,11 +603,11 @@ module.exports = function(User) {
*/
User.getVerifyOptions = function() {
const verifyOptions = {
const defaultOptions = {
type: 'email',
from: 'noreply@example.com',
};
return this.settings.verifyOptions || verifyOptions;
return Object.assign({}, this.settings.verifyOptions || defaultOptions);
};
/**
@ -699,11 +699,15 @@ module.exports = function(User) {
var user = this;
var userModel = this.constructor;
var registry = userModel.registry;
verifyOptions = Object.assign({}, verifyOptions);
// final assertion is performed once all options are assigned
assert(typeof verifyOptions === 'object',
'verifyOptions object param required when calling user.verify()');
// Shallow-clone the options object so that we don't override
// the global default options object
verifyOptions = Object.assign({}, verifyOptions);
// Set a default template generation function if none provided
verifyOptions.templateFn = verifyOptions.templateFn || createVerificationEmailBody;

View File

@ -1606,6 +1606,34 @@ describe('User', function() {
done();
});
it('returns same verifyOptions after verify user model', () => {
const defaultOptions = {
type: 'email',
from: 'test@example.com',
};
var verifyOptions = Object.assign({}, defaultOptions);
const user = new User({
email: 'example@example.com',
password: 'pass',
verificationToken: 'example-token',
});
return user
.verify(verifyOptions)
.then(res => expect(verifyOptions).to.eql(defaultOptions));
});
it('getVerifyOptions() always returns the same', () => {
const defaultOptions = {
type: 'email',
from: 'test@example.com',
};
User.settings.verifyOptions = Object.assign({}, defaultOptions);
var verifyOptions = User.getVerifyOptions();
verifyOptions.from = 'newTest@example.com';
verifyOptions.test = 'test';
expect(User.getVerifyOptions()).to.eql(defaultOptions);
});
it('can be extended by user', function(done) {
User.getVerifyOptions = function() {
const base = User.base.getVerifyOptions();