describe('saving encrypted data', function() { var fakeEmail = '*****@*****.**'; var fakeKey = 'nameForData'; var fakeRecord = 'fakeRecord'; var expectedKey = 'emailstore-' + bitcore.util.twoSha256(fakeEmail + '#' + fakeKey).toString('hex'); beforeEach(function() { leveldb_stub.get.reset(); leveldb_stub.put.reset(); }); it('saves data under the expected key', function(done) { leveldb_stub.put.onFirstCall().callsArgWith(2); plugin.saveEncryptedData(fakeEmail, fakeKey, fakeRecord, function(err) { leveldb_stub.put.firstCall.args[0].should.equal(expectedKey); done(); }); }); it('fails with INTERNAL_ERROR on database error', function(done) { leveldb_stub.put.onFirstCall().callsArgWith(2, 'error'); plugin.saveEncryptedData(fakeEmail, fakeKey, fakeRecord, function(err) { err.should.equal(plugin.errors.INTERNAL_ERROR); done(); }); }); });
describe('exists', function() { var fakeEmail = '*****@*****.**'; var fakeEmailKey = 'email-to-passphrase-' + bitcore.util.twoSha256(fakeEmail).toString('hex'); beforeEach(function() { leveldb_stub.get.reset(); }); it('validates that an email is already registered', function(done) { leveldb_stub.get.onFirstCall().callsArg(1); plugin.exists(fakeEmail, function(err, exists) { leveldb_stub.get.firstCall.args[0].should.equal(fakeEmailKey); exists.should.equal(true); done(); }); }); it('returns false when an email doesn\'t exist', function(done) { leveldb_stub.get.onFirstCall().callsArgWith(1, { notFound: true }); plugin.exists(fakeEmail, function(err, exists) { leveldb_stub.get.firstCall.args[0].should.equal(fakeEmailKey); exists.should.equal(false); done(); }); }); it('returns an internal error if database query couldn\'t be made', function(done) { leveldb_stub.get.onFirstCall().callsArgWith(1, 'error'); plugin.exists(fakeEmail, function(err, exists) { err.should.equal(plugin.errors.INTERNAL_ERROR); done(); }); }); });
var emailToPassphrase = function(email) { return EMAIL_TO_PASSPHRASE + bitcore.util.twoSha256(email).toString('hex'); };
var validatedKey = function(email) { return VALIDATED + bitcore.util.twoSha256(email).toString('hex'); };
var countKey = function(email) { return ITEMS_COUNT + bitcore.util.twoSha256(email).toString('hex'); };
var valueKey = function(email, key) { return STORED_VALUE + bitcore.util.twoSha256(email + SEPARATOR + key).toString('hex'); };