Merge pull request #1307 from matiu/feature/drive

Feature/ Async storage + Google Drive example
This commit is contained in:
Manuel Aráoz 2014-09-22 09:57:49 -03:00
commit 3a79f039cd
38 changed files with 1980 additions and 1192 deletions

View file

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

View file

@ -1,131 +0,0 @@
var FakeStorage = function() {
this.reset();
};
FakeStorage.prototype.reset = function(password) {
this.storage = {};
};
FakeStorage.prototype._setPassphrase = function(password) {
this.storage.passphrase = password;
};
FakeStorage.prototype.setGlobal = function(id, v) {
this.storage[id] = typeof v === 'object' ? JSON.stringify(v) : v;
};
FakeStorage.prototype.getGlobal = function(id) {
return this.storage[id];
};
FakeStorage.prototype.setLastOpened = function(val) {
this.storage['lastOpened'] = val;
};
FakeStorage.prototype.getLastOpened = function() {
return this.storage['lastOpened'];
};
FakeStorage.prototype.setLock = function(id) {
this.storage[id + '::lock'] = true;
}
FakeStorage.prototype.getLock = function(id) {
return this.storage[id + '::lock'];
}
FakeStorage.prototype.getSessionId = function() {
return this.sessionId || 'aSessionId';
};
FakeStorage.prototype.removeLock = function(id) {
delete this.storage[id + '::lock'];
}
FakeStorage.prototype.removeGlobal = function(id) {
delete this.storage[id];
};
FakeStorage.prototype.set = function(wid, id, payload) {
this.storage[wid + '::' + id] = payload;
};
FakeStorage.prototype.get = function(wid, id) {
return this.storage[wid + '::' + id];
};
FakeStorage.prototype.clear = function() {
delete this['storage'];
};
FakeStorage.prototype.getWalletIds = function() {
var walletIds = [];
var uniq = {};
for (var ii in this.storage) {
var split = ii.split('::');
if (split.length == 2) {
var walletId = split[0];
if (!walletId || walletId === 'nameFor' || walletId ==='lock')
continue;
if (typeof uniq[walletId] === 'undefined') {
walletIds.push(walletId);
uniq[walletId] = 1;
}
}
}
return walletIds;
};
FakeStorage.prototype.deleteWallet = function(walletId) {
var toDelete = {};
toDelete['nameFor::' + walletId] = 1;
for (var key in this.storage) {
var split = key.split('::');
if (split.length == 2 && split[0] === walletId) {
toDelete[key] = 1;
}
}
for (var i in toDelete) {
this.removeGlobal(i);
}
};
FakeStorage.prototype.getName = function(walletId) {
return this.getGlobal('nameFor::' + walletId);
};
FakeStorage.prototype.setName = function(walletId, name) {
this.setGlobal('nameFor::' + walletId, name);
};
FakeStorage.prototype.getWallets = function() {
var wallets = [];
var ids = this.getWalletIds();
for (var i in ids) {
wallets.push({
id: ids[i],
name: this.getName(ids[i]),
});
}
return wallets;
};
FakeStorage.prototype.setFromObj = function(walletId, obj) {
for (var k in obj) {
this.set(walletId, k, obj[k]);
}
this.setName(walletId, obj.opts.name);
};
module.exports = FakeStorage;

View file

@ -1,249 +0,0 @@
'use strict';
var chai = chai || require('chai');
var should = chai.should();
var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined';
var copay = copay || require('../copay');
var LocalEncrypted = copay.StorageLocalEncrypted;
var fakeWallet = 'fake-wallet-id';
var timeStamp = Date.now();
var localMock = require('./mocks/FakeLocalStorage');
var sessionMock = require('./mocks/FakeLocalStorage');
describe('Storage/LocalEncrypted model', function() {
var s = new LocalEncrypted({
localStorage: localMock,
sessionStorage: sessionMock,
});
s._setPassphrase('mysupercoolpassword');
it('should create an instance', function() {
var s2 = new LocalEncrypted({
localStorage: localMock,
sessionStorage: sessionMock,
});
should.exist(s2);
});
it('should fail when encrypting without a password', function() {
var s2 = new LocalEncrypted({
localStorage: localMock,
sessionStorage: sessionMock,
});
(function() {
s2.set(fakeWallet, timeStamp, 1);
}).should.throw();
});
it('should be able to encrypt and decrypt', function() {
s._write(fakeWallet + timeStamp, 'value');
s._read(fakeWallet + timeStamp).should.equal('value');
localMock.removeItem(fakeWallet + timeStamp);
});
it('should be able to set a value', function() {
s.set(fakeWallet, timeStamp, 1);
localMock.removeItem(fakeWallet + '::' + timeStamp);
});
var getSetData = [
1, 1000, -15, -1000,
0.1, -0.5, -0.5e-10, Math.PI,
'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b',
'1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC', 'OP_DUP OP_HASH160 80ad90d4035', [1, 2, 3, 4, 5, 6], {
x: 1,
y: 2
}, {
x: 'hi',
y: null
}, {
a: {},
b: [],
c: [1, 2, 'hi']
},
null
];
getSetData.forEach(function(obj) {
it('should be able to set a value and get it for ' + JSON.stringify(obj), function() {
s.set(fakeWallet, timeStamp, obj);
var obj2 = s.get(fakeWallet, timeStamp);
JSON.stringify(obj2).should.equal(JSON.stringify(obj));
localMock.removeItem(fakeWallet + '::' + timeStamp);
});
});
describe('#export', function() {
it('should export the encrypted wallet', function() {
var storage = new LocalEncrypted({
localStorage: localMock,
sessionStorage: sessionMock,
password: 'password',
});
storage.set(fakeWallet, timeStamp, 'testval');
var obj = {
test: 'testval'
};
var encrypted = storage.export(obj);
encrypted.length.should.be.greaterThan(10);
localMock.removeItem(fakeWallet + '::' + timeStamp);
//encrypted.slice(0,6).should.equal("53616c");
});
});
describe('#remove', function() {
it('should remove an item', function() {
var s = new LocalEncrypted({
localStorage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
s.set('1', "hola", 'juan');
s.get('1', 'hola').should.equal('juan');
s.remove('1', 'hola');
should.not.exist(s.get('1', 'hola'));
});
});
describe('#getWalletIds', function() {
it('should get wallet ids', function() {
var s = new LocalEncrypted({
localStorage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
s.set('1', "hola", 'juan');
s.set('2', "hola", 'juan');
s.getWalletIds().should.deep.equal(['1', '2']);
});
});
describe('#getName #setName', function() {
it('should get/set names', function() {
var s = new LocalEncrypted({
localStorage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
s.setName(1, 'hola');
s.getName(1).should.equal('hola');
});
});
describe('#getLastOpened #setLastOpened', function() {
it('should get/set names', function() {
var s = new LocalEncrypted({
localStorage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
s.setLastOpened('hey');
s.getLastOpened().should.equal('hey');
});
});
if (is_browser) {
describe('#getSessionId', function() {
it('should get SessionId', function() {
var s = new LocalEncrypted({
localStorage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
var sid = s.getSessionId();
should.exist(sid);
var sid2 = s.getSessionId();
sid2.should.equal(sid);
});
});
}
describe('#getWallets', function() {
it('should retreive wallets from storage', function() {
var s = new LocalEncrypted({
localStorage: 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
});
});
});
describe('#deleteWallet', function() {
it('should delete a wallet', function() {
var s = new LocalEncrypted({
localStorage: 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
});
});
});
describe('#setFromObj', function() {
it('set localstorage from an object', function() {
var s = new LocalEncrypted({
localStorage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
s.setFromObj('id1', {
'key': 'val',
'opts': {
'name': 'nameid1'
},
});
s.get('id1', 'key').should.equal('val');
});
});
describe('#globals', function() {
it('should set, get and remove keys', function() {
var s = new LocalEncrypted({
localStorage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
s.setGlobal('a', {
b: 1
});
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() {
var s = new LocalEncrypted({
localStorage: localMock,
sessionStorage: sessionMock,
password: 'password'
});
s.getSessionId().length.should.equal(16);
(new Buffer(s.getSessionId(),'hex')).length.should.equal(8);
});
});
});

View file

@ -36,5 +36,4 @@ describe('Passphrase model', function() {
done();
});
});
});

View file

@ -11,7 +11,6 @@ if (is_browser) {
}
var Wallet = copay.Wallet;
var PrivateKey = copay.PrivateKey;
var Storage = require('./mocks/FakeStorage');
var Network = require('./mocks/FakeNetwork');
var Blockchain = require('./mocks/FakeBlockchain');
var bitcore = bitcore || require('bitcore');
@ -21,6 +20,10 @@ var Address = bitcore.Address;
var PayPro = bitcore.PayPro;
var bignum = bitcore.Bignum;
var startServer = copay.FakePayProServer; // TODO should be require('./mocks/FakePayProServer');
var localMock = require('./mocks/FakeLocalStorage');
var sessionMock = require('./mocks/FakeLocalStorage');
var Storage = copay.Storage;
var server;
@ -30,6 +33,7 @@ var walletConfig = {
spendUnconfirmed: true,
reconnectDelay: 100,
networkName: 'testnet',
storage: require('./mocks/FakeLocalStorage').storageParams,
};
var getNewEpk = function() {
@ -41,6 +45,7 @@ var getNewEpk = function() {
};
describe('PayPro (in Wallet) model', function() {
if (!is_browser) {
var createW = function(N, conf) {
var c = JSON.parse(JSON.stringify(conf || walletConfig));
@ -64,6 +69,7 @@ describe('PayPro (in Wallet) model', function() {
});
var storage = new Storage(walletConfig.storage);
storage.setPassphrase('xxx');
var network = new Network(walletConfig.network);
var blockchain = new Blockchain(walletConfig.blockchain);
c.storage = storage;

321
test/test.Storage.js Normal file
View file

@ -0,0 +1,321 @@
'use strict';
var chai = chai || require('chai');
var sinon = require('sinon');
var should = chai.should();
var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined';
var copay = copay || require('../copay');
var Storage = copay.Storage;
var fakeWallet = 'fake-wallet-id';
var timeStamp = Date.now();
describe('Storage model', function() {
var s;
beforeEach(function() {
s = new Storage(require('./mocks/FakeLocalStorage').storageParams);
s.setPassphrase('mysupercoolpassword');
s.storage.clear();
s.sessionStorage.clear();
});
it('should create an instance', function() {
var s2 = new Storage(require('./mocks/FakeLocalStorage').storageParams);
should.exist(s2);
});
it('should fail when encrypting without a password', function() {
var s2 = new Storage(require('./mocks/FakeLocalStorage').storageParams);
(function() {
s2.set(fakeWallet, timeStamp, 1, function() {});
}).should.throw('NOPASSPHRASE');
});
it('should be able to encrypt and decrypt', function(done) {
s._write(fakeWallet + timeStamp, 'value', function() {
s._read(fakeWallet + timeStamp, function(v) {
v.should.equal('value');
done();
});
});
});
it('should be able to set a value', function(done) {
s.set(fakeWallet, timeStamp, 1, function() {
done();
});
});
var getSetData = [
1, 1000, -15, -1000,
0.1, -0.5, -0.5e-10, Math.PI,
'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b',
'1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC', 'OP_DUP OP_HASH160 80ad90d4035', [1, 2, 3, 4, 5, 6], {
x: 1,
y: 2
}, {
x: 'hi',
y: null
}, {
a: {},
b: [],
c: [1, 2, 'hi']
},
null
];
getSetData.forEach(function(obj) {
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));
done();
});
});
});
});
describe('#export', function() {
it('should export the encrypted wallet', function(done) {
s.set(fakeWallet, timeStamp, 'testval', function() {
var obj = {
test: 'testval'
};
var encrypted = s.export(obj);
encrypted.length.should.be.greaterThan(10);
done();
});
});
});
describe('#remove', function() {
it('should remove an item', function(done) {
s.set('1', "hola", 'juan', function() {
s.get('1', 'hola', function(v) {
v.should.equal('juan');
s.remove('1', 'hola', function() {
s.get('1', 'hola', function(v) {
should.not.exist(v);
done();
});
});
})
})
});
});
describe('#getWalletIds', function() {
it('should get wallet ids', function(done) {
s.set('1', "hola", 'juan', function() {
s.set('2', "hola", 'juan', function() {
s.getWalletIds(function(v) {
v.should.deep.equal(['1', '2']);
done();
});
});
});
});
});
describe('#getName #setName', function() {
it('should get/set names', function(done) {
s.setName(1, 'hola', function() {
s.getName(1, function(v) {
v.should.equal('hola');
done();
});
});
});
});
describe('#getLastOpened #setLastOpened', function() {
it('should get/set last opened', function() {
s.setLastOpened('hey', function() {
s.getLastOpened(function(v) {
v.should.equal('hey');
});
});
});
});
if (is_browser) {
describe('#getSessionId', function() {
it('should get SessionId', function(done) {
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(done) {
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();
});
});
});
});
});
it('should retreive wallets from storage (with delay)', function(done) {
s.set('1', "hola", 'juan', function() {
s.set('2', "hola", 'juan', function() {
s.setName(1, 'hola', function() {
var orig = s.getName.bind(s);
s.getName = function(wid, cb) {
setTimeout(function() {
orig(wid, cb);
},1);
};
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() {
it('should fail to delete a unexisting wallet', function(done) {
s.set('1', "hola", 'juan', function() {
s.set('2', "hola", 'juan', function() {
s.deleteWallet('3', function(err) {
err.toString().should.include('WNOTFOUND');
done();
});
});
});
});
it('should delete a wallet', function(done) {
s.set('1', "hola", 'juan', function() {
s.set('2', "hola", 'juan', function() {
s.deleteWallet('1', function(err) {
should.not.exist(err);
s.getWallets(function(ws) {
ws.length.should.equal(1);
ws[0].should.deep.equal({
id: '2',
name: undefined
});
done();
});
});
});
});
});
});
describe('#setFromObj', function() {
it('set localstorage from an object', function(done) {
s.setFromObj('id1', {
'key': 'val',
'opts': {
'name': 'nameid1'
},
}, function() {
s.get('id1', 'key', function(v) {
v.should.equal('val');
done();
});
});
});
});
describe('#globals', function() {
it('should set, get and remove keys', function(done) {
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();
});
});
});
});
});
});
describe('session storage', function() {
it('should get a session ID', function(done) {
s.getSessionId(function(s) {
should.exist(s);
s.length.should.equal(16);
(new Buffer(s, 'hex')).length.should.equal(8);
done();
});
});
});
describe('#import', function() {
it('should not be able to decrypt with wrong password', function() {
s.setPassphrase('xxx');
var wo = s.import(encryptedLegacy1);
should.not.exist(wo);
});
it('should be able to decrypt an old backup', function() {
s.setPassphrase(legacyPassword1);
var wo = s.import(encryptedLegacy1);
should.exist(wo);
wo.opts.id.should.equal('48ba2f1ffdfe9708');
wo.opts.spendUnconfirmed.should.equal(true);
wo.opts.requiredCopayers.should.equal(1);
wo.opts.totalCopayers.should.equal(1);
wo.opts.name.should.equal('pepe wallet');
wo.opts.version.should.equal('0.4.7');
wo.publicKeyRing.walletId.should.equal('48ba2f1ffdfe9708');
wo.publicKeyRing.networkName.should.equal('testnet');
wo.publicKeyRing.requiredCopayers.should.equal(1);
wo.publicKeyRing.totalCopayers.should.equal(1);
wo.publicKeyRing.indexes.length.should.equal(2);
JSON.stringify(wo.publicKeyRing.indexes[0]).should.equal('{"copayerIndex":2147483647,"changeIndex":0,"receiveIndex":1}');
JSON.stringify(wo.publicKeyRing.indexes[1]).should.equal('{"copayerIndex":0,"changeIndex":0,"receiveIndex":1}');
wo.publicKeyRing.copayersBackup.length.should.equal(1);
wo.publicKeyRing.copayersBackup[0].should.equal('0298f65b2694c55f9048bc05f10368242727c7f9d2065cbd788c3ecde1ec57f33f');
wo.publicKeyRing.copayersExtPubKeys.length.should.equal(1);
wo.publicKeyRing.copayersExtPubKeys[0].should.equal('tpubD9SGoP7CXsqSKTiQxCZSCpicDcophqnE4yuqjfw5M9tAR3fSjT9GDGwPEUFCN7SSmRKGDLZgKQePYFaLWyK32akeSan45TNTd8sgef9Ymh6');
wo.privateKey.extendedPrivateKeyString.should.equal('tprv8ZgxMBicQKsPfQCscb7CtJKzixxcVSyrCVcfr3WCFbtT8kYTzNubhjQ5R7AuYJgPCcSH4R8T34YVxeohKGhAB9wbB4eFBbQFjUpjGCqptHm');
wo.privateKey.networkName.should.equal('testnet');
});
});
});
var legacyPassword1 = '1DUpLRbuVpgLkcEY8gY8iod/SmA7+OheGZJ9PtvmTlvNE0FkEWpCKW9STdzXYJqbn0wiAapE4ojHNYj2hjYYAQ==';
var encryptedLegacy1 = 'U2FsdGVkX19yGM1uBAIzQa8Po/dvUicmxt1YyRk/S97PcZ6I6rHMp9dMagIrehg4Qd6JHn/ustmFHS7vmBYj0EBpf6rdXiQezaWnVAJS9/xYjAO36EFUbl+NmUanuwujAxgYdSP/sNssRLeInvExmZYW993EEclxkwL6YUyX66kKsxGQo2oWng0NreBJNhFmrbOEWeFje2PiWP57oUjKsurFzwpluAAarUTYSLud+nXeabC7opzOP5yqniWBMJz0Ou8gpNCWCMhG/P9F9ccVPY7juyd0Hf41FVse8nd2++axKB57+paozLdO+HRfV6zkMqC3h8gWY7LkS75j3bvqcTw9LhXmzE0Sz21n9yDnRpA4chiAvtwQvvBGgj1pFMKhNQU6Obac9ZwKYzUTgdDn3Uzg1UlDzgyOh9S89rbRTV84WB+hXwhuVluWzbNNYV3vXe5PFrocVktIrtS3xQh+k/7my4A6/gRRrzNYpKrUASJqDS/9u9WBkG35xD63J/qXjtG2M0YPwbI57BK1IK4K510b8V72lz5U2XQrIC4ldBwni1rpSavwCJV9xF6hUdOmNV8fZsVHP0NeN1PYlLkSb2QgfuoWnkcsJerwuFR7GZC/i6efrswtpO0wMEQr/J0CLbeXlHAru6xxjCBhWoJvZpMGw72zgnDLoyMNsEVglNhx/VlV9ZMYkkdaEYAxPOEIyZdQ5MS+2jEAlXf818n/xzJSVrniCn9be8EPePvkw35pivprvy09vbW4cKsWBKvgIyoT6A3OhUOCCS8E9cg0WAjjav2EymrbKmGWRHaiD+EoJqaDg6s20zhHn1YEa/YwvGGSB5+Hg8baLHD8ZASvxz4cFFAAVZrBUedRFgHzqwaMUlFXLgueivWUj7RXlIw6GuNhLoo1QkhZMacf23hrFxxQYvGBRw1hekBuDmcsGWljA28udBxBd5f9i+3gErttMLJ6IPaud590uvrxRIclu0Sz9R2EQX64YJxqDtLpMY0PjddSMu8vaDRpK9/ZSrnz/xrXsyabaafz4rE/ItFXjwFUFkvtmuauHTz6nmuKjVfxvNLNAiKb/gI7vQyUhnTbKIApe7XyJsjedNDtZqsPoJRIzdDmrZYxGStbAZ7HThqFJlSJ9NPNhH+E2jm3TwL5mwt0fFZ5h+p497lHMtIcKffESo7KNa2juSVNMDREk0NcyxGXGiVB2FWl4sLdvyhcsVq0I7tmW6OGZKRf8W49GCJXq6Ie69DJ9LB1DO67NV1jsYbsLx9uhE2yEmpWZ3jkoCV/Eas4grxt0CGN6EavzQ==';

View file

@ -13,7 +13,7 @@ if (is_browser) {
var copayConfig = require('../config');
var Wallet = copay.Wallet;
var PrivateKey = copay.PrivateKey;
var Storage = require('./mocks/FakeStorage');
var Storage = copay.Storage;
var Network = require('./mocks/FakeNetwork');
var Blockchain = require('./mocks/FakeBlockchain');
var Builder = require('./mocks/FakeBuilder');
@ -28,6 +28,7 @@ var walletConfig = {
spendUnconfirmed: true,
reconnectDelay: 100,
networkName: 'testnet',
storage: require('./mocks/FakeLocalStorage').storageParams,
};
var getNewEpk = function() {
@ -81,6 +82,7 @@ describe('Wallet model', function() {
});
var storage = new Storage(walletConfig.storage);
storage.setPassphrase('xxx');
var network = new Network(walletConfig.network);
var blockchain = new Blockchain(walletConfig.blockchain);
c.storage = storage;
@ -341,8 +343,10 @@ describe('Wallet model', function() {
// non stored options
o.opts.reconnectDelay = 100;
var s = new Storage(walletConfig.storage);
s.setPassphrase('xxx');
var w2 = Wallet.fromObj(o,
new Storage(walletConfig.storage),
s,
new Network(walletConfig.network),
new Blockchain(walletConfig.blockchain));
should.exist(w2);

File diff suppressed because one or more lines are too long

View file

@ -11,12 +11,20 @@ if (is_browser) {
}
var copayConfig = require('../config');
var WalletLock = copay.WalletLock;
var PrivateKey = copay.PrivateKey;
var Storage = require('./mocks/FakeStorage');
var Storage = copay.Storage;
var storage;
describe('WalletLock model', function() {
var storage = new Storage();
beforeEach(function() {
storage = new Storage(require('./mocks/FakeLocalStorage').storageParams);
storage.setPassphrase('mysupercoolpassword');
storage.storage.clear();
storage.sessionStorage.clear();
});
it('should fail with missing args', function() {
(function() {
@ -36,45 +44,68 @@ describe('WalletLock model', function() {
should.exist(w);
});
it('should NOT fail if locked already', function() {
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('#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) {
var w = new WalletLock(storage, 'walletId');
storage.sessionId = 'xxx';
var w2= new WalletLock(storage, 'walletId');
should.exist(w2);
});
w.keepAlive(function() {
storage.setSessionId('session2', function() {
var w2 = new WalletLock(storage, 'walletId');
w2.keepAlive(function(locked) {
should.exist(locked);
locked.message.should.contain('LOCKED');
done();
});
});
});
})
it('should change status of previously openned wallet', function() {
storage.sessionId = 'session1';
it('should FAIL if locked by someone else but expired', function(done) {
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.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();
});
});
});
})
});

View file

@ -15,16 +15,19 @@ saveAs = function(blob, filename) {
var startServer = require('../../mocks/FakePayProServer');
describe("Unit: Controllers", function() {
config.plugins.LocalStorage=true;
config.plugins.GoogleDrive=null;
var invalidForm = {
$invalid: true
};
var scope;
var server;
beforeEach(module('copayApp.services'));
beforeEach(module('copayApp.controllers'));
beforeEach(angular.mock.module('copayApp'));
var walletConfig = {
requiredCopayers: 3,
@ -36,6 +39,7 @@ describe("Unit: Controllers", function() {
alternativeIsoCode: 'LOL'
};
describe('More Controller', function() {
var ctrl;
beforeEach(inject(function($controller, $rootScope) {
@ -72,11 +76,6 @@ describe("Unit: Controllers", function() {
expect(saveAsLastCall.filename).equal('myTESTwullet-testID-keybackup.json.aes');
});
it('Backup controller #delete', function() {
expect(scope.wallet).not.equal(undefined);
scope.deleteWallet();
expect(scope.wallet).equal(undefined);
});
});
describe('Create Controller', function() {

View file

@ -6,6 +6,8 @@
var sinon = require('sinon');
var preconditions = require('preconditions').singleton();
beforeEach(angular.mock.module('copayApp'));
describe("Unit: Walletfactory Service", function() {
beforeEach(angular.mock.module('copayApp.services'));
it('should contain a walletFactory service', inject(function(walletFactory) {