diff --git a/plugins/emailstore.js b/plugins/emailstore.js index 0973081..5064c7d 100644 --- a/plugins/emailstore.js +++ b/plugins/emailstore.js @@ -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); diff --git a/test/test.EmailStore.js b/test/test.EmailStore.js index 759b9f7..086808d 100644 --- a/test/test.EmailStore.js +++ b/test/test.EmailStore.js @@ -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';