Merge pull request #279 from isocolsky/email_resend
[EmailStore plugin] Email resend
This commit is contained in:
commit
327abc65de
@ -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);
|
||||
|
||||
@ -406,6 +406,43 @@ describe('emailstore test', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('resend validation email', function () {
|
||||
var email = 'fake@email.com';
|
||||
var secret = '123';
|
||||
beforeEach(function() {
|
||||
leveldb_stub.get.reset();
|
||||
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;
|
||||
});
|
||||
|
||||
it('should resend validation email when pending (old style secret)', function () {
|
||||
plugin.authorizeRequestWithoutKey = sinon.stub().callsArgWith(1, null, email);
|
||||
leveldb_stub.get.onFirstCall().callsArgWith(1, null, secret);
|
||||
plugin.sendVerificationEmail = sinon.spy();
|
||||
plugin.resendEmail(request, response);
|
||||
plugin.sendVerificationEmail.calledOnce.should.be.true;
|
||||
plugin.sendVerificationEmail.calledWith(email, secret).should.be.true;
|
||||
});
|
||||
|
||||
it('should not resend when email is no longer pending', function () {
|
||||
plugin.authorizeRequestWithoutKey = sinon.stub().callsArgWith(1, null, email);
|
||||
leveldb_stub.get.onFirstCall().callsArgWith(1, { notFound: true });
|
||||
plugin.sendVerificationEmail = sinon.spy();
|
||||
plugin.resendEmail(request, response);
|
||||
plugin.sendVerificationEmail.should.not.be.called;
|
||||
});
|
||||
});
|
||||
|
||||
describe('removing items', function() {
|
||||
var fakeEmail = 'fake@email.com';
|
||||
var fakeKey = 'nameForData';
|
||||
|
||||
Loading…
Reference in New Issue
Block a user