diff --git a/plugins/emailstore.js b/plugins/emailstore.js index 6fe7cdf..3a760d4 100644 --- a/plugins/emailstore.js +++ b/plugins/emailstore.js @@ -281,7 +281,7 @@ emailPlugin.createVerificationSecret = function (email, callback) { return callback(null, secret); }); } else { - return callback(); + return callback(err); } }); }; @@ -420,10 +420,11 @@ emailPlugin.processPost = function(request, response, email, key, passphrase, re emailPlugin.getCredentialsFromRequest = function(request) { - if (!request.header('authorization')) { + var auth =request.header('authorization'); + if (! auth) { 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(':'); if (splitIndex === -1) { return emailPlugin.errors.INVALID_REQUEST; @@ -438,18 +439,21 @@ emailPlugin.getCredentialsFromRequest = function(request) { * Retrieve a record from the database */ emailPlugin.retrieve = function (request, response) { + var credentialsResult = emailPlugin.getCredentialsFromRequest(request); if (_.contains(emailPlugin.errors, credentialsResult)) { - return emailPlugin.returnError(credentialsResult); + return emailPlugin.returnError(credentialsResult, response); } var email = credentialsResult.email; var passphrase = credentialsResult.passphrase; var key = request.param('key'); +console.log('[emailstore.js.450:key:]',key); //TODO if (!passphrase || !email || !key) { return emailPlugin.returnError(emailPlugin.errors.MISSING_PARAMETER, response); } +console.log('[emailstore.js.453]'); //TODO emailPlugin.retrieveDataByEmailAndPassphrase(email, key, passphrase, function (err, value) { if (err) { return emailPlugin.returnError(err, response); diff --git a/test/test.EmailStore.js b/test/test.EmailStore.js index 608e649..fdeb4e6 100644 --- a/test/test.EmailStore.js +++ b/test/test.EmailStore.js @@ -19,7 +19,7 @@ describe('emailstore test', function() { var leveldb_stub = sinon.stub(); leveldb_stub.put = sinon.stub(); leveldb_stub.get = sinon.stub(); - leveldb_stub.remove = sinon.stub(); + leveldb_stub.del = sinon.stub(); var email_stub = sinon.stub(); email_stub.sendMail = sinon.stub(); @@ -45,7 +45,14 @@ describe('emailstore test', function() { request.param = sinon.stub(); response = 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.end = 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) { - leveldb_stub.get.onFirstCall().callsArgWith(1, {notFound: true}); + leveldb_stub.get.onFirstCall().callsArgWith(1, { + notFound: true + }); plugin.exists(fakeEmail, function(err, exists) { leveldb_stub.get.firstCall.args[0].should.equal(fakeEmailKey); @@ -162,8 +171,7 @@ describe('emailstore test', function() { var fakeEmail = 'fake@email.com'; var fakeKey = 'nameForData'; var fakeRecord = 'fakeRecord'; - var expectedKey = 'emailstore-' - + bitcore.util.twoSha256(fakeEmail + '#' + fakeKey).toString('hex'); + var expectedKey = 'emailstore-' + bitcore.util.twoSha256(fakeEmail + '#' + fakeKey).toString('hex'); beforeEach(function() { leveldb_stub.get.reset(); @@ -193,7 +201,11 @@ describe('emailstore test', function() { var sendVerificationEmail = sinon.stub(plugin, 'sendVerificationEmail'); var fakeEmail = 'fake@email.com'; var fakeRandom = 'fakerandom'; - var randomBytes = {toString: function() { return fakeRandom; }}; + var randomBytes = { + toString: function() { + return fakeRandom; + } + }; beforeEach(function() { leveldb_stub.get.reset(); @@ -205,7 +217,9 @@ describe('emailstore test', 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); }; @@ -226,7 +240,9 @@ describe('emailstore test', function() { }); }); 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'); plugin.createVerificationSecretAndSendEmail(fakeEmail, function(err) { err.should.equal(plugin.errors.INTERNAL_ERROR); @@ -254,8 +270,7 @@ describe('emailstore test', function() { var keyParam = 'key'; var recordParam = 'record'; beforeEach(function() { - var data = ('email=' + emailParam + '&secret=' + secretParam - + '&record=' + recordParam + '&key=' + keyParam); + var data = ('email=' + emailParam + '&secret=' + secretParam + '&record=' + recordParam + '&key=' + keyParam); request.on.onFirstCall().callsArgWith(1, data); request.on.onFirstCall().returnsThis(); request.on.onSecondCall().callsArg(1); @@ -263,6 +278,7 @@ describe('emailstore test', function() { }); it('should allow new registrations', function() { + var originalCredentials = plugin.getCredentialsFromRequest; plugin.getCredentialsFromRequest = sinon.mock(); plugin.getCredentialsFromRequest.onFirstCall().returns({ email: emailParam, @@ -278,7 +294,7 @@ describe('emailstore test', function() { plugin.createVerificationSecretAndSendEmail.onFirstCall().callsArg(1); response.send.onFirstCall().returnsThis(); - plugin.post(request, response); + plugin.save(request, response); assert(plugin.exists.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[2] === recordParam); assert(plugin.createVerificationSecretAndSendEmail.firstCall.args[0] === emailParam); + plugin.getCredentialsFromRequest = originalCredentials; }); it('should allow to overwrite data', function() { + var originalCredentials = plugin.getCredentialsFromRequest; plugin.getCredentialsFromRequest = sinon.mock(); plugin.getCredentialsFromRequest.onFirstCall().returns({ email: emailParam, @@ -305,7 +323,7 @@ describe('emailstore test', function() { plugin.createVerificationSecretAndSendEmail.onFirstCall().callsArg(1); response.send.onFirstCall().returnsThis(); - plugin.post(request, response); + plugin.save(request, response); assert(plugin.exists.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[2] === recordParam); assert(plugin.createVerificationSecretAndSendEmail.firstCall.args[0] === emailParam); + plugin.getCredentialsFromRequest = originalCredentials; }); }); @@ -327,14 +346,14 @@ describe('emailstore test', function() { request.param.onSecondCall().returns(secret); leveldb_stub.put = sinon.stub(); leveldb_stub.get = sinon.stub(); - leveldb_stub.remove = sinon.stub(); leveldb_stub.put.onFirstCall().callsArg(2); - leveldb_stub.remove.onFirstCall().callsArg(1); + leveldb_stub.del.onFirstCall().callsArg(1); response.json.returnsThis(); }); it('should validate correctly an email if the secret matches', function() { leveldb_stub.get.onFirstCall().callsArgWith(1, null, secret); + leveldb_stub.del = sinon.stub().yields(null); response.redirect = sinon.stub(); plugin.validate(request, response); @@ -351,7 +370,9 @@ describe('emailstore test', function() { plugin.validate(request, response); 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); }); }); @@ -359,21 +380,21 @@ describe('emailstore test', function() { describe('when retrieving data', function() { it('should validate the secret and return the data', function() { - request.param.onFirstCall().returns('email'); - request.param.onSecondCall().returns('key'); - request.param.onThirdCall().returns('secret'); + request.header = sinon.stub(); + request.header.onFirstCall().returns(new Buffer('email:pass', 'utf8').toString('base64')); + request.param.onFirstCall().returns('key'); + plugin.retrieveDataByEmailAndPassphrase = sinon.stub(); plugin.retrieveDataByEmailAndPassphrase.onFirstCall().callsArgWith(3, null, 'encrypted'); 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.param.secondCall.args[0] === 'key'); - assert(request.param.thirdCall.args[0] === 'secret'); + assert(request.header.firstCall.args[0] === 'authorization'); assert(plugin.retrieveDataByEmailAndPassphrase.firstCall.args[0] === 'email'); 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.end.calledOnce); }); @@ -425,4 +446,3 @@ describe('emailstore test', function() { }); }); }); -