remove profile on email send fail
This commit is contained in:
parent
4e2e2c5ba9
commit
e28047db1b
@ -52,6 +52,10 @@
|
|||||||
code: 406,
|
code: 406,
|
||||||
message: 'User quota exceeded',
|
message: 'User quota exceeded',
|
||||||
},
|
},
|
||||||
|
COULD_NOT_CREATE: {
|
||||||
|
code: 500,
|
||||||
|
message: 'Could not create profile',
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
var EMAIL_TO_PASSPHRASE = 'email-to-passphrase-';
|
var EMAIL_TO_PASSPHRASE = 'email-to-passphrase-';
|
||||||
@ -141,7 +145,7 @@
|
|||||||
* @param {string} email - the user's email
|
* @param {string} email - the user's email
|
||||||
* @param {string} secret - the verification secret
|
* @param {string} secret - the verification secret
|
||||||
*/
|
*/
|
||||||
emailPlugin.sendVerificationEmail = function(email, secret) {
|
emailPlugin.sendVerificationEmail = function(email, secret, callback) {
|
||||||
var confirmUrl = emailPlugin.makeConfirmUrl(email, secret);
|
var confirmUrl = emailPlugin.makeConfirmUrl(email, secret);
|
||||||
|
|
||||||
logger.debug('ConfirmUrl:',confirmUrl);
|
logger.debug('ConfirmUrl:',confirmUrl);
|
||||||
@ -176,9 +180,10 @@
|
|||||||
emailPlugin.email.sendMail(mailOptions, function(err, info) {
|
emailPlugin.email.sendMail(mailOptions, function(err, info) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('An error occurred when trying to send email to ' + email, err);
|
logger.error('An error occurred when trying to send email to ' + email, err);
|
||||||
} else {
|
return callback(err);
|
||||||
logger.info('Message sent: ', info ? info : '');
|
|
||||||
}
|
}
|
||||||
|
logger.info('Message sent: ', info ? info : '');
|
||||||
|
return callback(err, info);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -351,7 +356,12 @@
|
|||||||
return callback(emailPlugin.errors.INTERNAL_ERROR);
|
return callback(emailPlugin.errors.INTERNAL_ERROR);
|
||||||
}
|
}
|
||||||
if (secret) {
|
if (secret) {
|
||||||
emailPlugin.sendVerificationEmail(email, secret);
|
emailPlugin.sendVerificationEmail(email, secret, function (err, res) {
|
||||||
|
if (err) {
|
||||||
|
logger.error('error sending verification email', email, secret, err);
|
||||||
|
return callback(emailPlugin.errors.INTERNAL_ERROR);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
@ -478,7 +488,9 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
emailPlugin.processPost = function(request, response, email, key, passphrase, record) {
|
emailPlugin.processPost = function(request, response, email, key, passphrase, record) {
|
||||||
|
var isNewProfile = true;
|
||||||
var isConfirmed = false;
|
var isConfirmed = false;
|
||||||
|
var errorCreating = false;
|
||||||
|
|
||||||
async.series([
|
async.series([
|
||||||
/**
|
/**
|
||||||
@ -486,18 +498,14 @@
|
|||||||
*/
|
*/
|
||||||
function(callback) {
|
function(callback) {
|
||||||
emailPlugin.exists(email, function(err, exists) {
|
emailPlugin.exists(email, function(err, exists) {
|
||||||
if (err) {
|
if (err) return callback(err);
|
||||||
return callback(err);
|
|
||||||
} else if (exists) {
|
if (exists) {
|
||||||
emailPlugin.checkPassphrase(email, passphrase, function(err, match) {
|
emailPlugin.checkPassphrase(email, passphrase, function(err, match) {
|
||||||
if (err) {
|
if (err) return callback(err);
|
||||||
return callback(err);
|
if (!match) return callback(emailPlugin.errors.EMAIL_TAKEN);
|
||||||
}
|
isNewProfile = false;
|
||||||
if (match) {
|
return callback();
|
||||||
return callback();
|
|
||||||
} else {
|
|
||||||
return callback(emailPlugin.errors.EMAIL_TAKEN);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
emailPlugin.savePassphrase(email, passphrase, function(err) {
|
emailPlugin.savePassphrase(email, passphrase, function(err) {
|
||||||
@ -538,20 +546,25 @@
|
|||||||
* Create and store the verification secret. If successful, send a verification email.
|
* Create and store the verification secret. If successful, send a verification email.
|
||||||
*/
|
*/
|
||||||
function(callback) {
|
function(callback) {
|
||||||
|
if (!isNewProfile || isConfirmed) return callback();
|
||||||
|
|
||||||
emailPlugin.createVerificationSecretAndSendEmail(email, function(err) {
|
emailPlugin.createVerificationSecretAndSendEmail(email, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback({
|
errorCreating = true;
|
||||||
code: 500,
|
return callback(err);
|
||||||
message: err
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
callback();
|
|
||||||
}
|
}
|
||||||
|
return callback();
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
],
|
],
|
||||||
function(err) {
|
function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
if (isNewProfile && !isConfirmed && errorCreating) {
|
||||||
|
emailPlugin.deleteWholeProfile(email, function(err) {
|
||||||
|
return emailPlugin.returnError(emailPlugin.errors.COULD_NOT_CREATE, response);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
emailPlugin.returnError(err, response);
|
emailPlugin.returnError(err, response);
|
||||||
} else {
|
} else {
|
||||||
response.json({
|
response.json({
|
||||||
|
|||||||
@ -268,7 +268,6 @@ describe('emailstore test', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('on registration', function() {
|
describe('on registration', function() {
|
||||||
|
|
||||||
var emailParam = 'email';
|
var emailParam = 'email';
|
||||||
var secretParam = 'secret';
|
var secretParam = 'secret';
|
||||||
var keyParam = 'key';
|
var keyParam = 'key';
|
||||||
@ -336,7 +335,6 @@ describe('emailstore test', function() {
|
|||||||
plugin.saveEncryptedData = sinon.stub();
|
plugin.saveEncryptedData = sinon.stub();
|
||||||
plugin.saveEncryptedData.onFirstCall().callsArg(3);
|
plugin.saveEncryptedData.onFirstCall().callsArg(3);
|
||||||
plugin.createVerificationSecretAndSendEmail = sinon.stub();
|
plugin.createVerificationSecretAndSendEmail = sinon.stub();
|
||||||
plugin.createVerificationSecretAndSendEmail.onFirstCall().callsArg(1);
|
|
||||||
response.send.onFirstCall().returnsThis();
|
response.send.onFirstCall().returnsThis();
|
||||||
|
|
||||||
plugin.save(request, response);
|
plugin.save(request, response);
|
||||||
@ -347,9 +345,51 @@ describe('emailstore test', function() {
|
|||||||
assert(plugin.saveEncryptedData.firstCall.args[0] === emailParam);
|
assert(plugin.saveEncryptedData.firstCall.args[0] === emailParam);
|
||||||
assert(plugin.saveEncryptedData.firstCall.args[1] === keyParam);
|
assert(plugin.saveEncryptedData.firstCall.args[1] === keyParam);
|
||||||
assert(plugin.saveEncryptedData.firstCall.args[2] === recordParam);
|
assert(plugin.saveEncryptedData.firstCall.args[2] === recordParam);
|
||||||
assert(plugin.createVerificationSecretAndSendEmail.firstCall.args[0] === emailParam);
|
plugin.createVerificationSecretAndSendEmail.called.should.be.false;
|
||||||
plugin.getCredentialsFromRequest = originalCredentials;
|
plugin.getCredentialsFromRequest = originalCredentials;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should delete profile on error sending verification email', function() {
|
||||||
|
var originalCredentials = plugin.getCredentialsFromRequest;
|
||||||
|
plugin.getCredentialsFromRequest = sinon.mock();
|
||||||
|
plugin.getCredentialsFromRequest.onFirstCall().returns({
|
||||||
|
email: emailParam,
|
||||||
|
passphrase: secretParam
|
||||||
|
});
|
||||||
|
plugin.exists = sinon.stub();
|
||||||
|
plugin.exists.onFirstCall().callsArgWith(1, null, false);
|
||||||
|
plugin.savePassphrase = sinon.stub();
|
||||||
|
plugin.savePassphrase.onFirstCall().callsArg(2);
|
||||||
|
plugin.isConfirmed = sinon.stub();
|
||||||
|
plugin.isConfirmed.onFirstCall().callsArgWith(1, null, false);
|
||||||
|
plugin.checkSizeQuota = sinon.stub();
|
||||||
|
plugin.checkSizeQuota.onFirstCall().callsArgWith(3, null);
|
||||||
|
plugin.checkAndUpdateItemQuota = sinon.stub();
|
||||||
|
plugin.checkAndUpdateItemQuota.onFirstCall().callsArgWith(3, null);
|
||||||
|
plugin.saveEncryptedData = sinon.stub();
|
||||||
|
plugin.saveEncryptedData.onFirstCall().callsArg(3);
|
||||||
|
plugin.createVerificationSecretAndSendEmail = sinon.stub();
|
||||||
|
plugin.createVerificationSecretAndSendEmail.onFirstCall().callsArgWith(1, 'error');
|
||||||
|
var deleteWholeProfile = sinon.stub(plugin, 'deleteWholeProfile');
|
||||||
|
deleteWholeProfile.onFirstCall().callsArg(1);
|
||||||
|
response.send.onFirstCall().returnsThis();
|
||||||
|
|
||||||
|
plugin.save(request, response);
|
||||||
|
|
||||||
|
assert(plugin.exists.firstCall.args[0] === emailParam);
|
||||||
|
assert(plugin.savePassphrase.firstCall.args[0] === emailParam);
|
||||||
|
assert(plugin.savePassphrase.firstCall.args[1] === secretParam);
|
||||||
|
assert(plugin.saveEncryptedData.firstCall.args[0] === emailParam);
|
||||||
|
assert(plugin.saveEncryptedData.firstCall.args[1] === keyParam);
|
||||||
|
assert(plugin.saveEncryptedData.firstCall.args[2] === recordParam);
|
||||||
|
assert(plugin.createVerificationSecretAndSendEmail.firstCall.args[0] === emailParam);
|
||||||
|
assert(deleteWholeProfile.firstCall.args[0] === emailParam);
|
||||||
|
plugin.getCredentialsFromRequest = originalCredentials;
|
||||||
|
});
|
||||||
|
|
||||||
|
after(function () {
|
||||||
|
plugin.deleteWholeProfile.restore();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when validating email', function() {
|
describe('when validating email', function() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user