added tests

This commit is contained in:
Ivan Socolsky 2014-12-10 13:26:15 -03:00
parent 946eafc96d
commit 83e6d9bad0
2 changed files with 37 additions and 4 deletions

View File

@ -761,7 +761,6 @@
}, response);
}
var secret;
if (_.isObject(value)) {
if (moment().isAfter(value.expires)) {
return emailPlugin.returnError(emailPlugin.errors.REGISTRATION_EXPIRED, response);

View File

@ -8,6 +8,7 @@ var bitcore = require('bitcore');
var logger = require('../lib/logger').logger;
var should = chai.should;
var expect = chai.expect;
var moment = require('moment');
logger.transports.console.level = 'non';
@ -225,9 +226,12 @@ describe('emailstore test', function() {
it('saves data under the expected key', function(done) {
setupLevelDb();
var clock = sinon.useFakeTimers();
plugin.createVerificationSecretAndSendEmail(fakeEmail, function(err) {
leveldb_stub.put.firstCall.args[1].should.equal(fakeRandom);
var arg = leveldb_stub.put.firstCall.args[1];
arg.secret.should.equal(fakeRandom);
arg.expires.isSame(moment().add(7, 'days')).should.be.true;
clock.restore();
done();
});
});
@ -363,7 +367,7 @@ describe('emailstore test', function() {
response.json.returnsThis();
});
it('should validate correctly an email if the secret matches', function() {
it('should validate correctly an email if the secret matches (without expiration date)', function() {
leveldb_stub.get.onFirstCall().callsArgWith(1, null, secret);
leveldb_stub.del = sinon.stub().yields(null);
response.redirect = sinon.stub();
@ -373,6 +377,19 @@ describe('emailstore test', function() {
assert(response.redirect.firstCall.calledWith(plugin.redirectUrl));
});
it('should validate correctly an email if the secret matches (using expiration date)', function() {
leveldb_stub.get.onFirstCall().callsArgWith(1, null, {
secret: secret,
expires: moment().add(7, 'days')
});
leveldb_stub.del = sinon.stub().yields(null);
response.redirect = sinon.stub();
plugin.validate(request, response);
assert(response.redirect.firstCall.calledWith(plugin.redirectUrl));
});
it('should fail to validate an email if the secret doesn\'t match', function() {
var invalid = '3';
leveldb_stub.get.onFirstCall().callsArgWith(1, null, invalid);
@ -387,6 +404,23 @@ describe('emailstore test', function() {
}));
assert(response.end.calledOnce);
});
it('should fail to validate an email if the secret has expired', function() {
leveldb_stub.get.onFirstCall().callsArgWith(1, null, {
secret: secret,
expires: moment().subtract(2, 'days')
});
response.status.returnsThis();
response.json.returnsThis();
plugin.validate(request, response);
assert(response.status.firstCall.calledWith(plugin.errors.REGISTRATION_EXPIRED.code));
assert(response.json.firstCall.calledWith({
error: 'Registration expired'
}));
assert(response.end.calledOnce);
});
});
describe('removing items', function() {