Merge pull request #245 from matiu/bug/tests

fix tests
This commit is contained in:
Gustavo Maximiliano Cortez 2014-11-12 15:54:24 -03:00
commit ea9c670126
2 changed files with 52 additions and 28 deletions

View File

@ -281,7 +281,7 @@ emailPlugin.createVerificationSecret = function (email, callback) {
return callback(null, secret); return callback(null, secret);
}); });
} else { } else {
return callback(); return callback(err);
} }
}); });
}; };
@ -420,10 +420,11 @@ emailPlugin.processPost = function(request, response, email, key, passphrase, re
emailPlugin.getCredentialsFromRequest = function(request) { emailPlugin.getCredentialsFromRequest = function(request) {
if (!request.header('authorization')) { var auth =request.header('authorization');
if (! auth) {
return emailPlugin.errors.INVALID_REQUEST; return emailPlugin.errors.INVALID_REQUEST;
} }
var authHeader = new Buffer(request.header('authorization'), 'base64').toString('utf8'); var authHeader = new Buffer(auth, 'base64').toString('utf8');
var splitIndex = authHeader.indexOf(':'); var splitIndex = authHeader.indexOf(':');
if (splitIndex === -1) { if (splitIndex === -1) {
return emailPlugin.errors.INVALID_REQUEST; return emailPlugin.errors.INVALID_REQUEST;
@ -438,18 +439,21 @@ emailPlugin.getCredentialsFromRequest = function(request) {
* Retrieve a record from the database * Retrieve a record from the database
*/ */
emailPlugin.retrieve = function (request, response) { emailPlugin.retrieve = function (request, response) {
var credentialsResult = emailPlugin.getCredentialsFromRequest(request); var credentialsResult = emailPlugin.getCredentialsFromRequest(request);
if (_.contains(emailPlugin.errors, credentialsResult)) { if (_.contains(emailPlugin.errors, credentialsResult)) {
return emailPlugin.returnError(credentialsResult); return emailPlugin.returnError(credentialsResult, response);
} }
var email = credentialsResult.email; var email = credentialsResult.email;
var passphrase = credentialsResult.passphrase; var passphrase = credentialsResult.passphrase;
var key = request.param('key'); var key = request.param('key');
console.log('[emailstore.js.450:key:]',key); //TODO
if (!passphrase || !email || !key) { if (!passphrase || !email || !key) {
return emailPlugin.returnError(emailPlugin.errors.MISSING_PARAMETER, response); return emailPlugin.returnError(emailPlugin.errors.MISSING_PARAMETER, response);
} }
console.log('[emailstore.js.453]'); //TODO
emailPlugin.retrieveDataByEmailAndPassphrase(email, key, passphrase, function (err, value) { emailPlugin.retrieveDataByEmailAndPassphrase(email, key, passphrase, function (err, value) {
if (err) { if (err) {
return emailPlugin.returnError(err, response); return emailPlugin.returnError(err, response);

View File

@ -19,7 +19,7 @@ describe('emailstore test', function() {
var leveldb_stub = sinon.stub(); var leveldb_stub = sinon.stub();
leveldb_stub.put = sinon.stub(); leveldb_stub.put = sinon.stub();
leveldb_stub.get = sinon.stub(); leveldb_stub.get = sinon.stub();
leveldb_stub.remove = sinon.stub(); leveldb_stub.del = sinon.stub();
var email_stub = sinon.stub(); var email_stub = sinon.stub();
email_stub.sendMail = sinon.stub(); email_stub.sendMail = sinon.stub();
@ -45,7 +45,14 @@ describe('emailstore test', function() {
request.param = sinon.stub(); request.param = sinon.stub();
response = sinon.stub(); response = sinon.stub();
response.send = sinon.stub(); response.send = sinon.stub();
response.status = sinon.stub(); response.status = sinon.stub().returns({
json: function() {
return {
end: function() {
}
}
}
});
response.json = sinon.stub(); response.json = sinon.stub();
response.end = sinon.stub(); response.end = sinon.stub();
response.redirect = sinon.stub(); response.redirect = sinon.stub();
@ -76,7 +83,9 @@ describe('emailstore test', function() {
}); });
it('returns false when an email doesn\'t exist', function(done) { it('returns false when an email doesn\'t exist', function(done) {
leveldb_stub.get.onFirstCall().callsArgWith(1, {notFound: true}); leveldb_stub.get.onFirstCall().callsArgWith(1, {
notFound: true
});
plugin.exists(fakeEmail, function(err, exists) { plugin.exists(fakeEmail, function(err, exists) {
leveldb_stub.get.firstCall.args[0].should.equal(fakeEmailKey); leveldb_stub.get.firstCall.args[0].should.equal(fakeEmailKey);
@ -162,8 +171,7 @@ describe('emailstore test', function() {
var fakeEmail = 'fake@email.com'; var fakeEmail = 'fake@email.com';
var fakeKey = 'nameForData'; var fakeKey = 'nameForData';
var fakeRecord = 'fakeRecord'; var fakeRecord = 'fakeRecord';
var expectedKey = 'emailstore-' var expectedKey = 'emailstore-' + bitcore.util.twoSha256(fakeEmail + '#' + fakeKey).toString('hex');
+ bitcore.util.twoSha256(fakeEmail + '#' + fakeKey).toString('hex');
beforeEach(function() { beforeEach(function() {
leveldb_stub.get.reset(); leveldb_stub.get.reset();
@ -193,7 +201,11 @@ describe('emailstore test', function() {
var sendVerificationEmail = sinon.stub(plugin, 'sendVerificationEmail'); var sendVerificationEmail = sinon.stub(plugin, 'sendVerificationEmail');
var fakeEmail = 'fake@email.com'; var fakeEmail = 'fake@email.com';
var fakeRandom = 'fakerandom'; var fakeRandom = 'fakerandom';
var randomBytes = {toString: function() { return fakeRandom; }}; var randomBytes = {
toString: function() {
return fakeRandom;
}
};
beforeEach(function() { beforeEach(function() {
leveldb_stub.get.reset(); leveldb_stub.get.reset();
@ -205,7 +217,9 @@ describe('emailstore test', function() {
}); });
var setupLevelDb = function() { var setupLevelDb = function() {
leveldb_stub.get.onFirstCall().callsArgWith(1, {notFound: true}); leveldb_stub.get.onFirstCall().callsArgWith(1, {
notFound: true
});
leveldb_stub.put.onFirstCall().callsArg(2); leveldb_stub.put.onFirstCall().callsArg(2);
}; };
@ -226,7 +240,9 @@ describe('emailstore test', function() {
}); });
}); });
it('returns internal error on put database error', function(done) { it('returns internal error on put database error', function(done) {
leveldb_stub.get.onFirstCall().callsArgWith(1, {notFound: true}); leveldb_stub.get.onFirstCall().callsArgWith(1, {
notFound: true
});
leveldb_stub.put.onFirstCall().callsArgWith(2, 'error'); leveldb_stub.put.onFirstCall().callsArgWith(2, 'error');
plugin.createVerificationSecretAndSendEmail(fakeEmail, function(err) { plugin.createVerificationSecretAndSendEmail(fakeEmail, function(err) {
err.should.equal(plugin.errors.INTERNAL_ERROR); err.should.equal(plugin.errors.INTERNAL_ERROR);
@ -254,8 +270,7 @@ describe('emailstore test', function() {
var keyParam = 'key'; var keyParam = 'key';
var recordParam = 'record'; var recordParam = 'record';
beforeEach(function() { beforeEach(function() {
var data = ('email=' + emailParam + '&secret=' + secretParam var data = ('email=' + emailParam + '&secret=' + secretParam + '&record=' + recordParam + '&key=' + keyParam);
+ '&record=' + recordParam + '&key=' + keyParam);
request.on.onFirstCall().callsArgWith(1, data); request.on.onFirstCall().callsArgWith(1, data);
request.on.onFirstCall().returnsThis(); request.on.onFirstCall().returnsThis();
request.on.onSecondCall().callsArg(1); request.on.onSecondCall().callsArg(1);
@ -263,6 +278,7 @@ describe('emailstore test', function() {
}); });
it('should allow new registrations', function() { it('should allow new registrations', function() {
var originalCredentials = plugin.getCredentialsFromRequest;
plugin.getCredentialsFromRequest = sinon.mock(); plugin.getCredentialsFromRequest = sinon.mock();
plugin.getCredentialsFromRequest.onFirstCall().returns({ plugin.getCredentialsFromRequest.onFirstCall().returns({
email: emailParam, email: emailParam,
@ -278,7 +294,7 @@ describe('emailstore test', function() {
plugin.createVerificationSecretAndSendEmail.onFirstCall().callsArg(1); plugin.createVerificationSecretAndSendEmail.onFirstCall().callsArg(1);
response.send.onFirstCall().returnsThis(); response.send.onFirstCall().returnsThis();
plugin.post(request, response); plugin.save(request, response);
assert(plugin.exists.firstCall.args[0] === emailParam); assert(plugin.exists.firstCall.args[0] === emailParam);
assert(plugin.savePassphrase.firstCall.args[0] === emailParam); assert(plugin.savePassphrase.firstCall.args[0] === emailParam);
@ -287,9 +303,11 @@ describe('emailstore test', function() {
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); assert(plugin.createVerificationSecretAndSendEmail.firstCall.args[0] === emailParam);
plugin.getCredentialsFromRequest = originalCredentials;
}); });
it('should allow to overwrite data', function() { it('should allow to overwrite data', function() {
var originalCredentials = plugin.getCredentialsFromRequest;
plugin.getCredentialsFromRequest = sinon.mock(); plugin.getCredentialsFromRequest = sinon.mock();
plugin.getCredentialsFromRequest.onFirstCall().returns({ plugin.getCredentialsFromRequest.onFirstCall().returns({
email: emailParam, email: emailParam,
@ -305,7 +323,7 @@ describe('emailstore test', function() {
plugin.createVerificationSecretAndSendEmail.onFirstCall().callsArg(1); plugin.createVerificationSecretAndSendEmail.onFirstCall().callsArg(1);
response.send.onFirstCall().returnsThis(); response.send.onFirstCall().returnsThis();
plugin.post(request, response); plugin.save(request, response);
assert(plugin.exists.firstCall.args[0] === emailParam); assert(plugin.exists.firstCall.args[0] === emailParam);
assert(plugin.checkPassphrase.firstCall.args[0] === emailParam); assert(plugin.checkPassphrase.firstCall.args[0] === emailParam);
@ -314,6 +332,7 @@ describe('emailstore test', function() {
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); assert(plugin.createVerificationSecretAndSendEmail.firstCall.args[0] === emailParam);
plugin.getCredentialsFromRequest = originalCredentials;
}); });
}); });
@ -327,14 +346,14 @@ describe('emailstore test', function() {
request.param.onSecondCall().returns(secret); request.param.onSecondCall().returns(secret);
leveldb_stub.put = sinon.stub(); leveldb_stub.put = sinon.stub();
leveldb_stub.get = sinon.stub(); leveldb_stub.get = sinon.stub();
leveldb_stub.remove = sinon.stub();
leveldb_stub.put.onFirstCall().callsArg(2); leveldb_stub.put.onFirstCall().callsArg(2);
leveldb_stub.remove.onFirstCall().callsArg(1); leveldb_stub.del.onFirstCall().callsArg(1);
response.json.returnsThis(); response.json.returnsThis();
}); });
it('should validate correctly an email if the secret matches', function() { it('should validate correctly an email if the secret matches', function() {
leveldb_stub.get.onFirstCall().callsArgWith(1, null, secret); leveldb_stub.get.onFirstCall().callsArgWith(1, null, secret);
leveldb_stub.del = sinon.stub().yields(null);
response.redirect = sinon.stub(); response.redirect = sinon.stub();
plugin.validate(request, response); plugin.validate(request, response);
@ -351,7 +370,9 @@ describe('emailstore test', function() {
plugin.validate(request, response); plugin.validate(request, response);
assert(response.status.firstCall.calledWith(plugin.errors.INVALID_CODE.code)); assert(response.status.firstCall.calledWith(plugin.errors.INVALID_CODE.code));
assert(response.json.firstCall.calledWith({error: 'The provided code is invalid'})); assert(response.json.firstCall.calledWith({
error: 'The provided code is invalid'
}));
assert(response.end.calledOnce); assert(response.end.calledOnce);
}); });
}); });
@ -359,21 +380,21 @@ describe('emailstore test', function() {
describe('when retrieving data', function() { describe('when retrieving data', function() {
it('should validate the secret and return the data', function() { it('should validate the secret and return the data', function() {
request.param.onFirstCall().returns('email'); request.header = sinon.stub();
request.param.onSecondCall().returns('key'); request.header.onFirstCall().returns(new Buffer('email:pass', 'utf8').toString('base64'));
request.param.onThirdCall().returns('secret'); request.param.onFirstCall().returns('key');
plugin.retrieveDataByEmailAndPassphrase = sinon.stub(); plugin.retrieveDataByEmailAndPassphrase = sinon.stub();
plugin.retrieveDataByEmailAndPassphrase.onFirstCall().callsArgWith(3, null, 'encrypted'); plugin.retrieveDataByEmailAndPassphrase.onFirstCall().callsArgWith(3, null, 'encrypted');
response.send.onFirstCall().returnsThis(); response.send.onFirstCall().returnsThis();
plugin.get(request, response); plugin.retrieve(request, response);
request.header.calledOnce.should.equal(true);
assert(request.param.firstCall.args[0] === 'email'); assert(request.header.firstCall.args[0] === 'authorization');
assert(request.param.secondCall.args[0] === 'key');
assert(request.param.thirdCall.args[0] === 'secret');
assert(plugin.retrieveDataByEmailAndPassphrase.firstCall.args[0] === 'email'); assert(plugin.retrieveDataByEmailAndPassphrase.firstCall.args[0] === 'email');
assert(plugin.retrieveDataByEmailAndPassphrase.firstCall.args[1] === 'key'); assert(plugin.retrieveDataByEmailAndPassphrase.firstCall.args[1] === 'key');
assert(plugin.retrieveDataByEmailAndPassphrase.firstCall.args[2] === 'secret'); assert(plugin.retrieveDataByEmailAndPassphrase.firstCall.args[2] === 'pass');
assert(response.send.firstCall.args[0] === 'encrypted'); assert(response.send.firstCall.args[0] === 'encrypted');
assert(response.end.calledOnce); assert(response.end.calledOnce);
}); });
@ -425,4 +446,3 @@ describe('emailstore test', function() {
}); });
}); });
}); });