Wallet/test/WalletLock.js

99 lines
2.6 KiB
JavaScript
Raw Normal View History

2014-08-14 18:25:53 -04:00
'use strict';
var WalletLock = copay.WalletLock;
var PrivateKey = copay.PrivateKey;
2014-09-17 09:42:23 -03:00
var Storage = copay.Storage;
2014-09-08 10:46:57 -03:00
var storage;
2014-08-14 18:25:53 -04:00
describe('WalletLock model', function() {
2014-09-17 12:10:26 -03:00
2014-09-08 10:46:57 -03:00
beforeEach(function() {
2014-09-25 01:58:27 -03:00
storage = new Storage(requireMock('FakeLocalStorage').storageParams);
2014-09-18 16:38:18 -03:00
storage.setPassphrase('mysupercoolpassword');
2014-09-27 18:00:27 -03:00
storage.clearAll();
2014-09-08 10:46:57 -03:00
});
2014-08-14 18:25:53 -04:00
it('should fail with missing args', function() {
(function() {
new WalletLock()
}).should.throw('Argument');
});
it('should fail with missing args (case 2)', function() {
(function() {
new WalletLock(storage)
}).should.throw('Argument');
});
it('should create an instance', function() {
var w = new WalletLock(storage, 'id');
should.exist(w);
});
2014-08-15 18:55:26 -04:00
2014-09-08 10:46:57 -03:00
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();
});
2014-08-14 18:25:53 -04:00
});
2014-09-08 10:46:57 -03:00
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();
});
2014-08-14 18:25:53 -04:00
});
2014-08-15 18:55:26 -04:00
2014-09-08 10:46:57 -03:00
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();
});
});
})
2014-08-14 18:25:53 -04:00
});
2014-09-08 10:46:57 -03:00
it('should FAIL if locked by someone else', function(done) {
var w = new WalletLock(storage, 'walletId');
w.keepAlive(function() {
2014-09-17 12:10:26 -03:00
storage.setSessionId('session2', function() {
var w2 = new WalletLock(storage, 'walletId');
w2.keepAlive(function(locked) {
should.exist(locked);
locked.message.should.contain('LOCKED');
done();
});
2014-09-08 10:46:57 -03:00
});
});
})
it('should FAIL if locked by someone else but expired', function(done) {
var w = new WalletLock(storage, 'walletId');
w.keepAlive(function() {
2014-09-17 12:10:26 -03:00
storage.setSessionId('session2', function() {
2014-09-27 18:00:27 -03:00
var json = JSON.parse(storage.db.ls['lock::walletId']);
2014-09-17 12:10:26 -03:00
json.expireTs -= 3600 * 1000;
2014-09-27 18:00:27 -03:00
storage.db.ls['lock::walletId'] = JSON.stringify(json);
2014-09-17 12:10:26 -03:00
var w2 = new WalletLock(storage, 'walletId');
w2.keepAlive(function(locked) {
w2.sessionId.should.equal('session2');
should.not.exist(locked);
done();
});
2014-09-08 10:46:57 -03:00
});
});
})
});