add WalletLock class

This commit is contained in:
Matias Alejo Garcia 2014-08-14 18:25:53 -04:00
parent c804083f89
commit d8e0d50dce
4 changed files with 151 additions and 3 deletions

View File

@ -15,6 +15,7 @@ var StorageLocalEncrypted = module.exports.StorageLocalEncrypted = require('./js
module.exports.WalletFactory = require('./js/models/core/WalletFactory');
module.exports.Wallet = require('./js/models/core/Wallet');
module.exports.WalletLock = require('./js/models/core/WalletLock');
module.exports.version = require('./version');
// test hack :s, will fix

View File

@ -0,0 +1,47 @@
'use strict';
var preconditions = require('preconditions').singleton();
function WalletLock(storage, walletId, timeoutMin) {
preconditions.checkArgument(storage);
preconditions.checkArgument(walletId);
this.sessionId = storage.getSessionId();
this.storage = storage;
this.timeoutMin = timeoutMin || 5;
this.key = WalletLock._keyFor(walletId);
this.keepAlive();
}
WalletLock._keyFor = function(walletId) {
return 'lock' + '::' + walletId;
};
WalletLock.prototype._isLockedByOther = function() {
var wl = this.storage.getGlobal(this.key);
if (!wl || wl.expireTs < Date.now() || wl.sessionId === this.sessionId)
return false;
return true;
};
WalletLock.prototype.keepAlive = function() {
preconditions.checkState(this.sessionId);
if (this._isLockedByOther())
throw new Error('Could not adquire lock');
this.storage.setGlobal(this.key, {
sessionId: this.sessionId,
expireTs: Date.now() + this.timeoutMin * 60 * 1000,
});
};
WalletLock.prototype.release = function() {
this.storage.removeGlobal(this.key);
};
module.exports = WalletLock;

72
test/test.WalletLock.js Normal file
View File

@ -0,0 +1,72 @@
'use strict';
var chai = chai || require('chai');
var should = chai.should();
var sinon = require('sinon');
var is_browser = (typeof process == 'undefined' || typeof process.versions === 'undefined');
if (is_browser) {
var copay = require('copay'); //browser
} else {
var copay = require('../copay'); //node
}
var copayConfig = require('../config');
var WalletLock = copay.WalletLock;
var PrivateKey = copay.PrivateKey;
var Storage = require('./mocks/FakeStorage');
describe('WalletLock model', function() {
var storage = new Storage();
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);
});
it('should fail if locked already', function() {
var w = new WalletLock(storage, 'walletId');
storage.sessionId = 'xxx';
(function() {
new WalletLock(storage, 'walletId')
}).should.throw('adquire lock');
});
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')
should.exist(w2);
});
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')
should.exist(w2);
});
it('should not fail if expired', function() {
var s = new Storage();
var w = new WalletLock(s, 'walletId');
s.storage[Object.keys(s.storage)[0]].expireTs = Date.now() - 60 * 6 * 1000;
s.sessionId = 'xxx';
var w2 = new WalletLock(s, 'walletId')
should.exist(w2);
});
});

View File

@ -38,12 +38,40 @@ var createBundle = function(opts) {
expose: '../js/models/core/WalletFactory'
});
b.require('./js/models/core/Wallet');
b.require('./js/models/core/Wallet', {
expose: '../js/models/core/Wallet'
});
b.require('./js/models/core/Wallet', {
expose: '../../js/models/core/Wallet'
});
<<<<<<< HEAD
=======
b.require('./js/models/core/WalletLock', {
expose: '../js/models/core/WalletLock'
});
b.require('./test/mocks/FakeStorage', {
expose: './mocks/FakeStorage'
});
b.require('./test/mocks/FakeLocalStorage', {
expose: './mocks/FakeLocalStorage'
});
b.require('./js/models/core/Message', {
expose: '../js/models/core/Message'
});
b.require('./test/mocks/FakeBlockchain', {
expose: './mocks/FakeBlockchain'
});
b.require('./test/mocks/FakeNetwork', {
expose: './mocks/FakeNetwork'
});
b.require('./test/mocks/FakePayProServer', {
expose: './mocks/FakePayProServer'
});
b.require('./test/mocks/FakePayProServer', {
expose: '../../mocks/FakePayProServer'
});
b.require('./test/mocks/FakeBuilder', {
expose: './mocks/FakeBuilder'
});
>>>>>>> add WalletLock class
b.require('./js/models/network/WebRTC', {
expose: '../js/models/network/WebRTC'
});