fix walletLock test

This commit is contained in:
Matias Alejo Garcia 2014-09-08 10:46:57 -03:00
commit f1e6d6cfc6
8 changed files with 269 additions and 153 deletions

View file

@ -5,8 +5,9 @@ function LocalStorage(opts) {
FakeLocalStorage = {};
FakeLocalStorage.length = 0;
FakeLocalStorage.removeItem = function(key) {
FakeLocalStorage.removeItem = function(key,cb) {
delete ls[key];
cb();
};
FakeLocalStorage.getItem = function(k,cb) {

View file

@ -11,57 +11,65 @@ FakeStorage.prototype._setPassphrase = function(password) {
this.storage.passphrase = password;
};
FakeStorage.prototype.setGlobal = function(id, v) {
FakeStorage.prototype.setGlobal = function(id, v, cb) {
this.storage[id] = typeof v === 'object' ? JSON.stringify(v) : v;
cb();
};
FakeStorage.prototype.getGlobal = function(id) {
return this.storage[id];
FakeStorage.prototype.getGlobal = function(id, cb) {
return cb(this.storage[id]);
};
FakeStorage.prototype.setLastOpened = function(val) {
FakeStorage.prototype.setLastOpened = function(val, cb) {
this.storage['lastOpened'] = val;
return cb();
};
FakeStorage.prototype.getLastOpened = function() {
return this.storage['lastOpened'];
FakeStorage.prototype.getLastOpened = function(cb) {
return cb(this.storage['lastOpened']);
};
FakeStorage.prototype.setLock = function(id) {
this.storage[id + '::lock'] = true;
return cb();
}
FakeStorage.prototype.getLock = function(id) {
return this.storage[id + '::lock'];
FakeStorage.prototype.getLock = function(id,cb) {
return cb(this.storage[id + '::lock']);
}
FakeStorage.prototype.getSessionId = function() {
return this.sessionId || 'aSessionId';
FakeStorage.prototype.getSessionId = function(cb) {
this.sessionId = this.sessionId || 'aSessionId';
return cb(this.sessionId);
};
FakeStorage.prototype.removeLock = function(id) {
FakeStorage.prototype.removeLock = function(id,cb) {
delete this.storage[id + '::lock'];
cb();
}
FakeStorage.prototype.removeGlobal = function(id) {
FakeStorage.prototype.removeGlobal = function(id,cb) {
delete this.storage[id];
cb();
};
FakeStorage.prototype.set = function(wid, id, payload) {
FakeStorage.prototype.set = function(wid, id, payload, cb) {
this.storage[wid + '::' + id] = payload;
if (cb) return cb();
};
FakeStorage.prototype.get = function(wid, id) {
return this.storage[wid + '::' + id];
FakeStorage.prototype.get = function(wid, id, cb) {
return cb(this.storage[wid + '::' + id]);
};
FakeStorage.prototype.clear = function() {
FakeStorage.prototype.clear = function(cb) {
delete this['storage'];
cb();
};
FakeStorage.prototype.getWalletIds = function() {
FakeStorage.prototype.getWalletIds = function(cb) {
var walletIds = [];
var uniq = {};
@ -79,10 +87,10 @@ FakeStorage.prototype.getWalletIds = function() {
}
}
}
return walletIds;
return cb(walletIds);
};
FakeStorage.prototype.deleteWallet = function(walletId) {
FakeStorage.prototype.deleteWallet = function(walletId, cb) {
var toDelete = {};
toDelete['nameFor::' + walletId] = 1;
@ -98,17 +106,17 @@ FakeStorage.prototype.deleteWallet = function(walletId) {
};
FakeStorage.prototype.getName = function(walletId) {
return this.getGlobal('nameFor::' + walletId);
FakeStorage.prototype.getName = function(walletId,cb) {
return this.getGlobal('nameFor::' + walletId,cb);
};
FakeStorage.prototype.setName = function(walletId, name) {
this.setGlobal('nameFor::' + walletId, name);
FakeStorage.prototype.setName = function(walletId, name, cb) {
this.setGlobal('nameFor::' + walletId, name, cb);
};
FakeStorage.prototype.getWallets = function() {
FakeStorage.prototype.getWallets = function(cb) {
var wallets = [];
var ids = this.getWalletIds();
@ -118,14 +126,14 @@ FakeStorage.prototype.getWallets = function() {
name: this.getName(ids[i]),
});
}
return wallets;
return cb(wallets);
};
FakeStorage.prototype.setFromObj = function(walletId, obj) {
FakeStorage.prototype.setFromObj = function(walletId, obj, cb) {
for (var k in obj) {
this.set(walletId, k, obj[k]);
}
this.setName(walletId, obj.opts.name);
this.setName(walletId, obj.opts.name, cb);
};
module.exports = FakeStorage;

View file

@ -39,15 +39,13 @@ describe('Storage model', function() {
s._write(fakeWallet + timeStamp, 'value', function() {
s._read(fakeWallet + timeStamp, function(v) {
v.should.equal('value');
localMock.removeItem(fakeWallet + timeStamp);
done();
localMock.removeItem(fakeWallet + timeStamp, done);
});
});
});
it('should be able to set a value', function(done) {
s.set(fakeWallet, timeStamp, 1, function() {
localMock.removeItem(fakeWallet + '::' + timeStamp);
done();
localMock.removeItem(fakeWallet + '::' + timeStamp, done);
});
});
var getSetData = [
@ -68,11 +66,11 @@ describe('Storage model', function() {
null
];
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(done) {
s.set(fakeWallet, timeStamp, obj, function() {
s.get(fakeWallet, timeStamp, function(obj2) {
JSON.stringify(obj2).should.equal(JSON.stringify(obj));
localMock.removeItem(fakeWallet + '::' + timeStamp);
localMock.removeItem(fakeWallet + '::' + timeStamp, done);
});
});
});
@ -91,15 +89,13 @@ describe('Storage model', function() {
};
var encrypted = storage.export(obj);
encrypted.length.should.be.greaterThan(10);
localMock.removeItem(fakeWallet + '::' + timeStamp);
done();
localMock.removeItem(fakeWallet + '::' + timeStamp, done);
});
});
});
describe('#remove', function(done) {
it('should remove an item', function() {
describe('#remove', function() {
it('should remove an item', function(done) {
var s = new Storage({
storage: localMock,
sessionStorage: sessionMock,
@ -109,8 +105,10 @@ describe('Storage model', function() {
s.get('1', 'hola', function(v) {
v.should.equal('juan');
s.remove('1', 'hola', function() {
should.not.exist(s.get('1', 'hola'));
done();
s.get('1', 'hola', function(v) {
should.not.exist(v);
done();
});
});
})
})
@ -169,62 +167,75 @@ describe('Storage model', function() {
if (is_browser) {
describe('#getSessionId', function() {
it('should get SessionId', function() {
it('should get SessionId', function(done) {
var s = new Storage({
storage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
var sid = s.getSessionId();
should.exist(sid);
var sid2 = s.getSessionId();
sid2.should.equal(sid);
s.getSessionId(function(sid) {
should.exist(sid);
s.getSessionId(function(sid2) {
sid2.should.equal(sid);
done();
});
});
});
});
}
describe('#getWallets', function() {
it('should retreive wallets from storage', function() {
it('should retreive wallets from storage', function(done) {
var s = new Storage({
storage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
s.set('1', "hola", 'juan');
s.set('2', "hola", 'juan');
s.setName(1, 'hola');
s.getWallets()[0].should.deep.equal({
id: '1',
name: 'hola',
});
s.getWallets()[1].should.deep.equal({
id: '2',
name: undefined
s.set('1', "hola", 'juan', function() {
s.set('2', "hola", 'juan', function() {
s.setName(1, 'hola', function() {
s.getWallets(function(ws) {
ws[0].should.deep.equal({
id: '1',
name: 'hola',
});
ws[1].should.deep.equal({
id: '2',
name: undefined
});
done();
});
});
});
});
});
});
describe('#deleteWallet', function() {
describe('#deleteWallet', function(done) {
it('should delete a wallet', function() {
var s = new Storage({
storage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
s.set('1', "hola", 'juan');
s.set('2', "hola", 'juan');
s.setName(1, 'hola');
s.deleteWallet('1');
s.getWallets().length.should.equal(1);
s.getWallets()[0].should.deep.equal({
id: '2',
name: undefined
s.set('1', "hola", 'juan', function() {
s.set('2', "hola", 'juan', function() {
s.deleteWallet('1', function() {
s.getWallets(function(ws) {
s.getWallets().length.should.equal(1);
ws[0].should.deep.equal({
id: '2',
name: undefined
});
done();
});
});
});
});
});
});
describe('#setFromObj', function() {
it('set localstorage from an object', function() {
it('set localstorage from an object', function(done) {
var s = new Storage({
storage: localMock,
sessionStorage: sessionMock,
@ -235,42 +246,57 @@ describe('Storage model', function() {
'opts': {
'name': 'nameid1'
},
}, function() {
s.get('id1', 'key', function(v) {
v.should.equal('val');
done();
});
});
s.get('id1', 'key').should.equal('val');
});
});
describe('#globals', function() {
it('should set, get and remove keys', function() {
it('should set, get and remove keys', function(done) {
var s = new Storage({
storage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
s.setGlobal('a', {
b: 1
}, function() {
s.getGlobal('a', function(v) {
JSON.parse(v).should.deep.equal({
b: 1
});
s.removeGlobal('a', function() {
s.getGlobal('a', function(v) {
should.not.exist(v);
done();
});
});
});
});
JSON.parse(s.getGlobal('a')).should.deep.equal({
b: 1
});
s.removeGlobal('a');
should.not.exist(s.getGlobal('a'));
});
});
describe('session storage', function() {
it('should get a session ID', function() {
it('should get a session ID', function(done) {
var s = new Storage({
storage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
s.getSessionId().length.should.equal(16);
(new Buffer(s.getSessionId(), 'hex')).length.should.equal(8);
s.getSessionId(function(s) {
should.exist(s);
s.length.should.equal(16);
(new Buffer(s, 'hex')).length.should.equal(8);
done();
});
});
});
});

View file

@ -117,16 +117,17 @@ describe('WalletFactory model', function() {
wf.version.should.equal('0.0.1');
});
it('#_checkRead should return false', function() {
var wf = new WalletFactory(config);
wf._checkRead('dummy').should.equal(false);
wf.read('dummy').should.equal(false);
});
it('should be able to create wallets', function() {
it('should be able to create wallets', function(done) {
var wf = new WalletFactory(config, '0.0.1');
var w = wf.create();
should.exist(w);
wf.create(null, function(err, w) {
console.log('[test.WalletFactory.js.123]'); //TODO
should.not.exist(err);
should.exist(w);
w.should.be.instanceof('WalletFactory');
done();
});
});
it('should be able to create wallets with given pk', function() {

View file

@ -15,8 +15,11 @@ var WalletLock = copay.WalletLock;
var PrivateKey = copay.PrivateKey;
var Storage = require('./mocks/FakeStorage');
var storage;
describe('WalletLock model', function() {
var storage = new Storage();
beforeEach(function() {
storage = new Storage();
});
it('should fail with missing args', function() {
(function() {
@ -36,45 +39,70 @@ describe('WalletLock model', function() {
should.exist(w);
});
it('should NOT fail if locked already', function() {
var w = new WalletLock(storage, 'walletId');
storage.sessionId = 'xxx';
var w2= new WalletLock(storage, 'walletId');
should.exist(w2);
it('should generate a sessionId with init', function(done) {
var w = new WalletLock(storage, 'id');
var spy = sinon.spy(storage, 'getSessionId');
w.init(function() {
spy.calledOnce.should.equal(true);
done();
});
});
it('should change status of previously openned wallet', function() {
it('#keepAlive should call getsessionId if not called before', function(done) {
var w = new WalletLock(storage, 'id');
var spy = sinon.spy(storage, 'getSessionId');
w.keepAlive(function() {
spy.calledOnce.should.equal(true);
done();
});
});
it('should NOT fail if locked already by me', function(done) {
var w = new WalletLock(storage, 'walletId2');
w.keepAlive(function() {
var w2 = new WalletLock(storage, 'walletId2');
w2.init(function() {
w2.keepAlive(function() {
w.sessionId.should.equal(w2.sessionId);
should.exist(w2);
done();
});
});
})
});
it('should FAIL if locked by someone else', function(done) {
storage.sessionId = 'session1';
var w = new WalletLock(storage, 'walletId');
storage.sessionId = 'xxx';
var w2= new WalletLock(storage, 'walletId');
w2.keepAlive();
(function() {w.keepAlive();}).should.throw('already open');
});
it('should not fail if locked by me', function() {
var s = new Storage();
var w = new WalletLock(s, 'walletId');
var w2 = new WalletLock(s, 'walletId')
w2.keepAlive();
should.exist(w2);
});
it('should not fail if expired', function() {
var s = new Storage();
var w = new WalletLock(s, 'walletId');
var k = Object.keys(s.storage)[0];
var v = JSON.parse(s.storage[k]);
v.expireTs = Date.now() - 60 * 6 * 1000;
s.storage[k] = JSON.stringify(v);
s.sessionId = 'xxx';
var w2 = new WalletLock(s, 'walletId')
should.exist(w2);
});
w.keepAlive(function() {
storage.sessionId = 'session2';
var w2 = new WalletLock(storage, 'walletId');
w2.keepAlive(function(locked) {
w2.sessionId.should.equal('session2');
should.exist(locked);
locked.message.should.contain('LOCKED');
done();
});
});
})
it('should FAIL if locked by someone else but expired', function(done) {
storage.sessionId = 'session1';
var w = new WalletLock(storage, 'walletId');
w.keepAlive(function() {
storage.sessionId = 'session2';
var json = JSON.parse(storage.storage['lock::walletId']);
json.expireTs -= 3600 * 1000;
storage.storage['lock::walletId'] = JSON.stringify(json);
var w2 = new WalletLock(storage, 'walletId');
w2.keepAlive(function(locked) {
w2.sessionId.should.equal('session2');
should.not.exist(locked);
done();
});
});
})
});