From b3ed2a2ea82a174dbd5e3997423585b956333387 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 4 Sep 2014 18:07:09 -0300 Subject: [PATCH] start fixing tests --- js/models/Storage.js | 18 +++++- js/models/core/WalletLock.js | 1 - test/mocks/FakeLocalStorage.js | 17 +++--- test/test.Storage.js | 105 +++++++++++++++++++++------------ 4 files changed, 89 insertions(+), 52 deletions(-) diff --git a/js/models/Storage.js b/js/models/Storage.js index b9cb9fa75..4d24928f7 100644 --- a/js/models/Storage.js +++ b/js/models/Storage.js @@ -3,6 +3,8 @@ var preconditions = require('preconditions').singleton(); var CryptoJS = require('node-cryptojs-aes').CryptoJS; var bitcore = require('bitcore'); var preconditions = require('preconditions').instance(); +var _ = require('underscore'); + var id = 0; function Storage(opts) { @@ -19,14 +21,14 @@ function Storage(opts) { console.log('Error in storage:', e); //TODO }; - preconditions.checkState(this.localStorage, 'No localstorage found'); - preconditions.checkState(this.sessionStorage, 'No sessionStorage found'); + preconditions.checkState(this.storage, 'No storage defined'); + preconditions.checkState(this.sessionStorage, 'No sessionStorage defined'); } var pps = {}; Storage.prototype._getPassphrase = function() { if (!pps[this.__uniqueid]) - throw new Error('No passprase set'); + throw new Error('NOPASSPHRASE: No passphrase set'); return pps[this.__uniqueid]; } @@ -147,23 +149,29 @@ console.log('[Storage.js.142:keys:]',keys); //TODO // set value for key Storage.prototype.set = function(walletId, k, v, cb) { + preconditions.checkArgument(walletId && k && !_.isUndefined(v) && cb); + this._write(this._key(walletId, k), v, cb); }; // remove value for key Storage.prototype.remove = function(walletId, k, cb) { + preconditions.checkArgument(walletId && k && cb); this.removeGlobal(this._key(walletId, k), cb); }; Storage.prototype.setName = function(walletId, name, cb) { + preconditions.checkArgument(walletId && name && cb); this.setGlobal('nameFor::' + walletId, name, cb); }; Storage.prototype.getName = function(walletId, cb) { + preconditions.checkArgument(walletId && cb); this.getGlobal('nameFor::' + walletId, cb); }; Storage.prototype.getWalletIds = function(cb) { + preconditions.checkArgument(cb); var walletIds = []; var uniq = {}; @@ -188,6 +196,8 @@ Storage.prototype.getWalletIds = function(cb) { }; Storage.prototype.getWallets = function(cb) { + preconditions.checkArgument(cb); + var wallets = []; var self = this; @@ -212,6 +222,8 @@ Storage.prototype.getWallets = function(cb) { }; Storage.prototype.deleteWallet = function(walletId) { + preconditions.checkArgument(walletId); + var toDelete = {}; toDelete['nameFor::' + walletId] = 1; diff --git a/js/models/core/WalletLock.js b/js/models/core/WalletLock.js index a8a1729b7..66e2458d2 100644 --- a/js/models/core/WalletLock.js +++ b/js/models/core/WalletLock.js @@ -20,7 +20,6 @@ WalletLock._keyFor = function(walletId) { WalletLock.prototype._isLockedByOther = function(cb) { var self = this; - console.log('[WalletLock.js.22]'); //TODO this.storage.getGlobal(this.key, function(json) { var wl = json ? JSON.parse(json) : null; var t = wl ? (Date.now() - wl.expireTs) : false; diff --git a/test/mocks/FakeLocalStorage.js b/test/mocks/FakeLocalStorage.js index e39280719..a276abaec 100644 --- a/test/mocks/FakeLocalStorage.js +++ b/test/mocks/FakeLocalStorage.js @@ -1,27 +1,26 @@ //localstorage Mock ls = {}; -function LocalStorage(opts) {} +function LocalStorage(opts) { +} FakeLocalStorage = {}; FakeLocalStorage.length = 0; FakeLocalStorage.removeItem = function(key) { delete ls[key]; - this.length = Object.keys(ls).length; }; -FakeLocalStorage.getItem = function(k) { - return ls[k]; +FakeLocalStorage.getItem = function(k,cb) { + return cb(ls[k]); }; -FakeLocalStorage.key = function(i) { - return Object.keys(ls)[i]; +FakeLocalStorage.allKeys = function(cb) { + return cb(Object.keys(ls)); }; -FakeLocalStorage.setItem = function(k, v) { +FakeLocalStorage.setItem = function(k, v,cb) { ls[k] = v; - this.key[this.length] = k; - this.length = Object.keys(ls).length; + return cb(); }; module.exports = FakeLocalStorage; diff --git a/test/test.Storage.js b/test/test.Storage.js index 0ad668bff..fd03c8cee 100644 --- a/test/test.Storage.js +++ b/test/test.Storage.js @@ -26,22 +26,29 @@ describe('Storage model', function() { should.exist(s2); }); it('should fail when encrypting without a password', function() { + var s2 = new Storage({ storage: localMock, sessionStorage: sessionMock, }); (function() { - s2.set(fakeWallet, timeStamp, 1); - }).should.throw(); + s2.set(fakeWallet, timeStamp, 1, function() {}); + }).should.throw('NOPASSPHRASE'); }); - it('should be able to encrypt and decrypt', function() { - s._write(fakeWallet + timeStamp, 'value'); - s._read(fakeWallet + timeStamp).should.equal('value'); - localMock.removeItem(fakeWallet + timeStamp); + it('should be able to encrypt and decrypt', function(done) { + s._write(fakeWallet + timeStamp, 'value', function() { + s._read(fakeWallet + timeStamp, function(v) { + v.should.equal('value'); + localMock.removeItem(fakeWallet + timeStamp); + done(); + }); + }); }); - it('should be able to set a value', function() { - s.set(fakeWallet, timeStamp, 1); - localMock.removeItem(fakeWallet + '::' + timeStamp); + it('should be able to set a value', function(done) { + s.set(fakeWallet, timeStamp, 1, function() { + localMock.removeItem(fakeWallet + '::' + timeStamp); + done(); + }); }); var getSetData = [ 1, 1000, -15, -1000, @@ -62,81 +69,101 @@ describe('Storage model', function() { ]; getSetData.forEach(function(obj) { it('should be able to set a value and get it for ' + JSON.stringify(obj), function() { - s.set(fakeWallet, timeStamp, obj); - var obj2 = s.get(fakeWallet, timeStamp); - JSON.stringify(obj2).should.equal(JSON.stringify(obj)); - localMock.removeItem(fakeWallet + '::' + timeStamp); + s.set(fakeWallet, timeStamp, obj, function() { + s.get(fakeWallet, timeStamp, function(obj2) { + JSON.stringify(obj2).should.equal(JSON.stringify(obj)); + localMock.removeItem(fakeWallet + '::' + timeStamp); + }); + }); }); }); describe('#export', function() { - it('should export the encrypted wallet', function() { + it('should export the encrypted wallet', function(done) { var storage = new Storage({ storage: localMock, sessionStorage: sessionMock, password: 'password', }); - storage.set(fakeWallet, timeStamp, 'testval'); - var obj = { - test: 'testval' - }; - var encrypted = storage.export(obj); - encrypted.length.should.be.greaterThan(10); - localMock.removeItem(fakeWallet + '::' + timeStamp); - //encrypted.slice(0,6).should.equal("53616c"); + storage.set(fakeWallet, timeStamp, 'testval', function() { + var obj = { + test: 'testval' + }; + var encrypted = storage.export(obj); + encrypted.length.should.be.greaterThan(10); + localMock.removeItem(fakeWallet + '::' + timeStamp); + done(); + + }); }); }); - describe('#remove', function() { + describe('#remove', function(done) { it('should remove an item', function() { var s = new Storage({ storage: localMock, sessionStorage: sessionMock, password: 'password' }); - s.set('1', "hola", 'juan'); - s.get('1', 'hola').should.equal('juan'); - s.remove('1', 'hola'); - - should.not.exist(s.get('1', 'hola')); + s.set('1', "hola", 'juan', function() { + s.get('1', 'hola', function(v) { + v.should.equal('juan'); + s.remove('1', 'hola', function() { + should.not.exist(s.get('1', 'hola')); + done(); + }); + }) + }) }); }); describe('#getWalletIds', function() { - it('should get wallet ids', function() { + it('should get wallet ids', function(done) { var s = new Storage({ storage: localMock, sessionStorage: sessionMock, password: 'password' }); - s.set('1', "hola", 'juan'); - s.set('2', "hola", 'juan'); - s.getWalletIds().should.deep.equal(['1', '2']); + s.set('1', "hola", 'juan', function() { + s.set('2', "hola", 'juan', function() { + s.getWalletIds(function(v) { + v.should.deep.equal(['1', '2']); + done(); + }); + }); + }); }); }); describe('#getName #setName', function() { - it('should get/set names', function() { + it('should get/set names', function(done) { var s = new Storage({ storage: localMock, sessionStorage: sessionMock, password: 'password' }); - s.setName(1, 'hola'); - s.getName(1).should.equal('hola'); + s.setName(1, 'hola', function() { + s.getName(1, function(v) { + v.should.equal('hola'); + done(); + }); + }); }); }); describe('#getLastOpened #setLastOpened', function() { - it('should get/set names', function() { + it('should get/set last opened', function() { var s = new Storage({ storage: localMock, sessionStorage: sessionMock, password: 'password' }); - s.setLastOpened('hey'); - s.getLastOpened().should.equal('hey'); + s.setLastOpened('hey', function() { + s.getLastOpened(function(v) { + v.should.equal('hey'); + }); + }); }); }); @@ -243,7 +270,7 @@ describe('Storage model', function() { password: 'password' }); s.getSessionId().length.should.equal(16); - (new Buffer(s.getSessionId(),'hex')).length.should.equal(8); + (new Buffer(s.getSessionId(), 'hex')).length.should.equal(8); }); }); });