copay/test/WalletLock.js

101 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-08-14 15:25:53 -07:00
'use strict';
var WalletLock = copay.WalletLock;
var PrivateKey = copay.PrivateKey;
2014-09-17 05:42:23 -07:00
var Storage = copay.Storage;
2014-09-08 06:46:57 -07:00
var storage;
2014-08-14 15:25:53 -07:00
describe('WalletLock model', function() {
2014-09-17 08:10:26 -07:00
2014-09-08 06:46:57 -07:00
beforeEach(function() {
2014-09-24 21:58:27 -07:00
storage = new Storage(requireMock('FakeLocalStorage').storageParams);
2014-09-18 12:38:18 -07:00
storage.setPassphrase('mysupercoolpassword');
2014-09-17 08:10:26 -07:00
storage.storage.clear();
storage.sessionStorage.clear();
2014-09-08 06:46:57 -07:00
});
2014-08-14 15:25:53 -07: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 15:55:26 -07:00
2014-09-08 06:46:57 -07: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 15:25:53 -07:00
});
2014-09-08 06:46:57 -07: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 15:25:53 -07:00
});
2014-08-15 15:55:26 -07:00
2014-09-08 06:46:57 -07: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 15:25:53 -07:00
});
2014-09-08 06:46:57 -07:00
it('should FAIL if locked by someone else', function(done) {
var w = new WalletLock(storage, 'walletId');
w.keepAlive(function() {
2014-09-17 08:10:26 -07: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 06:46:57 -07: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 08:10:26 -07:00
storage.setSessionId('session2', function() {
var json = JSON.parse(storage.storage.ls['lock::walletId']);
json.expireTs -= 3600 * 1000;
storage.storage.ls['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();
});
2014-09-08 06:46:57 -07:00
});
});
})
});