start fixing tests

This commit is contained in:
Matias Alejo Garcia 2014-09-04 18:07:09 -03:00
parent e0db19a40a
commit b3ed2a2ea8
4 changed files with 89 additions and 52 deletions

View File

@ -3,6 +3,8 @@ var preconditions = require('preconditions').singleton();
var CryptoJS = require('node-cryptojs-aes').CryptoJS; var CryptoJS = require('node-cryptojs-aes').CryptoJS;
var bitcore = require('bitcore'); var bitcore = require('bitcore');
var preconditions = require('preconditions').instance(); var preconditions = require('preconditions').instance();
var _ = require('underscore');
var id = 0; var id = 0;
function Storage(opts) { function Storage(opts) {
@ -19,14 +21,14 @@ function Storage(opts) {
console.log('Error in storage:', e); //TODO console.log('Error in storage:', e); //TODO
}; };
preconditions.checkState(this.localStorage, 'No localstorage found'); preconditions.checkState(this.storage, 'No storage defined');
preconditions.checkState(this.sessionStorage, 'No sessionStorage found'); preconditions.checkState(this.sessionStorage, 'No sessionStorage defined');
} }
var pps = {}; var pps = {};
Storage.prototype._getPassphrase = function() { Storage.prototype._getPassphrase = function() {
if (!pps[this.__uniqueid]) if (!pps[this.__uniqueid])
throw new Error('No passprase set'); throw new Error('NOPASSPHRASE: No passphrase set');
return pps[this.__uniqueid]; return pps[this.__uniqueid];
} }
@ -147,23 +149,29 @@ console.log('[Storage.js.142:keys:]',keys); //TODO
// set value for key // set value for key
Storage.prototype.set = function(walletId, k, v, cb) { Storage.prototype.set = function(walletId, k, v, cb) {
preconditions.checkArgument(walletId && k && !_.isUndefined(v) && cb);
this._write(this._key(walletId, k), v, cb); this._write(this._key(walletId, k), v, cb);
}; };
// remove value for key // remove value for key
Storage.prototype.remove = function(walletId, k, cb) { Storage.prototype.remove = function(walletId, k, cb) {
preconditions.checkArgument(walletId && k && cb);
this.removeGlobal(this._key(walletId, k), cb); this.removeGlobal(this._key(walletId, k), cb);
}; };
Storage.prototype.setName = function(walletId, name, cb) { Storage.prototype.setName = function(walletId, name, cb) {
preconditions.checkArgument(walletId && name && cb);
this.setGlobal('nameFor::' + walletId, name, cb); this.setGlobal('nameFor::' + walletId, name, cb);
}; };
Storage.prototype.getName = function(walletId, cb) { Storage.prototype.getName = function(walletId, cb) {
preconditions.checkArgument(walletId && cb);
this.getGlobal('nameFor::' + walletId, cb); this.getGlobal('nameFor::' + walletId, cb);
}; };
Storage.prototype.getWalletIds = function(cb) { Storage.prototype.getWalletIds = function(cb) {
preconditions.checkArgument(cb);
var walletIds = []; var walletIds = [];
var uniq = {}; var uniq = {};
@ -188,6 +196,8 @@ Storage.prototype.getWalletIds = function(cb) {
}; };
Storage.prototype.getWallets = function(cb) { Storage.prototype.getWallets = function(cb) {
preconditions.checkArgument(cb);
var wallets = []; var wallets = [];
var self = this; var self = this;
@ -212,6 +222,8 @@ Storage.prototype.getWallets = function(cb) {
}; };
Storage.prototype.deleteWallet = function(walletId) { Storage.prototype.deleteWallet = function(walletId) {
preconditions.checkArgument(walletId);
var toDelete = {}; var toDelete = {};
toDelete['nameFor::' + walletId] = 1; toDelete['nameFor::' + walletId] = 1;

View File

@ -20,7 +20,6 @@ WalletLock._keyFor = function(walletId) {
WalletLock.prototype._isLockedByOther = function(cb) { WalletLock.prototype._isLockedByOther = function(cb) {
var self = this; var self = this;
console.log('[WalletLock.js.22]'); //TODO
this.storage.getGlobal(this.key, function(json) { this.storage.getGlobal(this.key, function(json) {
var wl = json ? JSON.parse(json) : null; var wl = json ? JSON.parse(json) : null;
var t = wl ? (Date.now() - wl.expireTs) : false; var t = wl ? (Date.now() - wl.expireTs) : false;

View File

@ -1,27 +1,26 @@
//localstorage Mock //localstorage Mock
ls = {}; ls = {};
function LocalStorage(opts) {} function LocalStorage(opts) {
}
FakeLocalStorage = {}; FakeLocalStorage = {};
FakeLocalStorage.length = 0; FakeLocalStorage.length = 0;
FakeLocalStorage.removeItem = function(key) { FakeLocalStorage.removeItem = function(key) {
delete ls[key]; delete ls[key];
this.length = Object.keys(ls).length;
}; };
FakeLocalStorage.getItem = function(k) { FakeLocalStorage.getItem = function(k,cb) {
return ls[k]; return cb(ls[k]);
}; };
FakeLocalStorage.key = function(i) { FakeLocalStorage.allKeys = function(cb) {
return Object.keys(ls)[i]; return cb(Object.keys(ls));
}; };
FakeLocalStorage.setItem = function(k, v) { FakeLocalStorage.setItem = function(k, v,cb) {
ls[k] = v; ls[k] = v;
this.key[this.length] = k; return cb();
this.length = Object.keys(ls).length;
}; };
module.exports = FakeLocalStorage; module.exports = FakeLocalStorage;

View File

@ -26,22 +26,29 @@ describe('Storage model', function() {
should.exist(s2); should.exist(s2);
}); });
it('should fail when encrypting without a password', function() { it('should fail when encrypting without a password', function() {
var s2 = new Storage({ var s2 = new Storage({
storage: localMock, storage: localMock,
sessionStorage: sessionMock, sessionStorage: sessionMock,
}); });
(function() { (function() {
s2.set(fakeWallet, timeStamp, 1); s2.set(fakeWallet, timeStamp, 1, function() {});
}).should.throw(); }).should.throw('NOPASSPHRASE');
}); });
it('should be able to encrypt and decrypt', function() { it('should be able to encrypt and decrypt', function(done) {
s._write(fakeWallet + timeStamp, 'value'); s._write(fakeWallet + timeStamp, 'value', function() {
s._read(fakeWallet + timeStamp).should.equal('value'); s._read(fakeWallet + timeStamp, function(v) {
localMock.removeItem(fakeWallet + timeStamp); v.should.equal('value');
localMock.removeItem(fakeWallet + timeStamp);
done();
});
});
}); });
it('should be able to set a value', function() { it('should be able to set a value', function(done) {
s.set(fakeWallet, timeStamp, 1); s.set(fakeWallet, timeStamp, 1, function() {
localMock.removeItem(fakeWallet + '::' + timeStamp); localMock.removeItem(fakeWallet + '::' + timeStamp);
done();
});
}); });
var getSetData = [ var getSetData = [
1, 1000, -15, -1000, 1, 1000, -15, -1000,
@ -62,81 +69,101 @@ describe('Storage model', function() {
]; ];
getSetData.forEach(function(obj) { getSetData.forEach(function(obj) {
it('should be able to set a value and get it for ' + JSON.stringify(obj), function() { it('should be able to set a value and get it for ' + JSON.stringify(obj), function() {
s.set(fakeWallet, timeStamp, obj); s.set(fakeWallet, timeStamp, obj, function() {
var obj2 = s.get(fakeWallet, timeStamp); s.get(fakeWallet, timeStamp, function(obj2) {
JSON.stringify(obj2).should.equal(JSON.stringify(obj)); JSON.stringify(obj2).should.equal(JSON.stringify(obj));
localMock.removeItem(fakeWallet + '::' + timeStamp); localMock.removeItem(fakeWallet + '::' + timeStamp);
});
});
}); });
}); });
describe('#export', function() { describe('#export', function() {
it('should export the encrypted wallet', function() { it('should export the encrypted wallet', function(done) {
var storage = new Storage({ var storage = new Storage({
storage: localMock, storage: localMock,
sessionStorage: sessionMock, sessionStorage: sessionMock,
password: 'password', password: 'password',
}); });
storage.set(fakeWallet, timeStamp, 'testval'); storage.set(fakeWallet, timeStamp, 'testval', function() {
var obj = { var obj = {
test: 'testval' test: 'testval'
}; };
var encrypted = storage.export(obj); var encrypted = storage.export(obj);
encrypted.length.should.be.greaterThan(10); encrypted.length.should.be.greaterThan(10);
localMock.removeItem(fakeWallet + '::' + timeStamp); localMock.removeItem(fakeWallet + '::' + timeStamp);
//encrypted.slice(0,6).should.equal("53616c"); done();
});
}); });
}); });
describe('#remove', function() { describe('#remove', function(done) {
it('should remove an item', function() { it('should remove an item', function() {
var s = new Storage({ var s = new Storage({
storage: localMock, storage: localMock,
sessionStorage: sessionMock, sessionStorage: sessionMock,
password: 'password' password: 'password'
}); });
s.set('1', "hola", 'juan'); s.set('1', "hola", 'juan', function() {
s.get('1', 'hola').should.equal('juan'); s.get('1', 'hola', function(v) {
s.remove('1', 'hola'); v.should.equal('juan');
s.remove('1', 'hola', function() {
should.not.exist(s.get('1', 'hola')); should.not.exist(s.get('1', 'hola'));
done();
});
})
})
}); });
}); });
describe('#getWalletIds', function() { describe('#getWalletIds', function() {
it('should get wallet ids', function() { it('should get wallet ids', function(done) {
var s = new Storage({ var s = new Storage({
storage: localMock, storage: localMock,
sessionStorage: sessionMock, sessionStorage: sessionMock,
password: 'password' password: 'password'
}); });
s.set('1', "hola", 'juan'); s.set('1', "hola", 'juan', function() {
s.set('2', "hola", 'juan'); s.set('2', "hola", 'juan', function() {
s.getWalletIds().should.deep.equal(['1', '2']); s.getWalletIds(function(v) {
v.should.deep.equal(['1', '2']);
done();
});
});
});
}); });
}); });
describe('#getName #setName', function() { describe('#getName #setName', function() {
it('should get/set names', function() { it('should get/set names', function(done) {
var s = new Storage({ var s = new Storage({
storage: localMock, storage: localMock,
sessionStorage: sessionMock, sessionStorage: sessionMock,
password: 'password' password: 'password'
}); });
s.setName(1, 'hola'); s.setName(1, 'hola', function() {
s.getName(1).should.equal('hola'); s.getName(1, function(v) {
v.should.equal('hola');
done();
});
});
}); });
}); });
describe('#getLastOpened #setLastOpened', function() { describe('#getLastOpened #setLastOpened', function() {
it('should get/set names', function() { it('should get/set last opened', function() {
var s = new Storage({ var s = new Storage({
storage: localMock, storage: localMock,
sessionStorage: sessionMock, sessionStorage: sessionMock,
password: 'password' password: 'password'
}); });
s.setLastOpened('hey'); s.setLastOpened('hey', function() {
s.getLastOpened().should.equal('hey'); s.getLastOpened(function(v) {
v.should.equal('hey');
});
});
}); });
}); });
@ -243,7 +270,7 @@ describe('Storage model', function() {
password: 'password' password: 'password'
}); });
s.getSessionId().length.should.equal(16); s.getSessionId().length.should.equal(16);
(new Buffer(s.getSessionId(),'hex')).length.should.equal(8); (new Buffer(s.getSessionId(), 'hex')).length.should.equal(8);
}); });
}); });
}); });