resend validation email

This commit is contained in:
Ivan Socolsky 2014-12-16 20:54:30 -03:00
parent 4e2e2c5ba9
commit 9c4eef5912
2 changed files with 53 additions and 8 deletions

View File

@ -722,6 +722,39 @@
});
};
emailPlugin._parseSecret = function (value) {
var obj = null;
try {
obj = JSON.parse(value);
} catch (e) {}
if (obj && _.isObject(obj)) {
return obj.secret;
}
return value;
};
emailPlugin.resendEmail = function(request, response) {
emailPlugin.authorizeRequestWithoutKey(request, function(err, email) {
if (err) {
return emailPlugin.returnError(err, response);
}
emailPlugin.db.get(pendingKey(email), function(err, value) {
if (err) {
logger.error('error retrieving secret for email', email, err);
return emailPlugin.returnError(err, response);
}
var secret = emailPlugin._parseSecret(value);
emailPlugin.sendVerificationEmail(email, secret);
return response.json({
success: true
}).end();
});
});
};
/**
* Marks an email as validated
@ -751,14 +784,7 @@
}, response);
}
var parsed = null;
try {
parsed = JSON.parse(value);
} catch (e) {}
if (parsed && _.isObject(parsed)) {
value = parsed.secret;
}
value = emailPlugin._parseSecret(value);
if (value !== secret) {
return emailPlugin.returnError(emailPlugin.errors.INVALID_CODE, response);

View File

@ -406,6 +406,25 @@ describe('emailstore test', function() {
});
});
describe.only('resend validation email', function () {
var email = 'fake@email.com';
var secret = '123';
beforeEach(function() {
request.param.onFirstCall().returns(email);
response.json.returnsThis();
response.redirect = sinon.stub();
});
it('should resend validation email when pending', function () {
plugin.authorizeRequestWithoutKey = sinon.stub().callsArgWith(1, null, email);
leveldb_stub.get.onFirstCall().callsArgWith(1, null, JSON.stringify({ secret: secret, created: new Date() }));
plugin.sendVerificationEmail = sinon.spy();
plugin.resendEmail(request, response);
plugin.sendVerificationEmail.calledOnce.should.be.true;
plugin.sendVerificationEmail.calledWith(email, secret).should.be.true;
});
});
describe('removing items', function() {
var fakeEmail = 'fake@email.com';
var fakeKey = 'nameForData';